sys_util: impl From<UnixSeqpacket> for SafeDescriptor

BUG=b:177267762
TEST=run vk and gl apps on volteer

Change-Id: Icfd3994ef62f0c27b817a3824da18977a943cc00
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3315233
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Chia-I Wu <olv@google.com>
This commit is contained in:
Chia-I Wu 2021-12-03 15:28:28 -08:00 committed by Commit Bot
parent 64ba89ac84
commit 2bcec5c3d9
2 changed files with 17 additions and 2 deletions

View file

@ -13,7 +13,7 @@ use std::os::unix::net::{UnixDatagram, UnixStream};
use serde::{Deserialize, Serialize};
use crate::net::UnlinkUnixSeqpacketListener;
use crate::net::{UnixSeqpacket, UnlinkUnixSeqpacketListener};
use crate::{errno_result, PollToken, Result};
pub type RawDescriptor = RawFd;
@ -168,6 +168,13 @@ impl From<SafeDescriptor> for UnixStream {
}
}
impl From<UnixSeqpacket> for SafeDescriptor {
fn from(s: UnixSeqpacket) -> Self {
// Safe because we own the UnixSeqpacket at this point.
unsafe { SafeDescriptor::from_raw_descriptor(s.into_raw_descriptor()) }
}
}
/// For use cases where a simple wrapper around a RawDescriptor is needed.
/// This is a simply a wrapper and does not manage the lifetime of the descriptor.
/// Most usages should prefer SafeDescriptor or using a RawDescriptor directly

View file

@ -27,7 +27,7 @@ use crate::{
sock_ctrl_msg::{ScmSocket, SCM_SOCKET_MAX_FD_COUNT},
FromRawDescriptor,
};
use crate::{AsRawDescriptor, RawDescriptor};
use crate::{AsRawDescriptor, IntoRawDescriptor, RawDescriptor};
/// Assist in handling both IP version 4 and IP version 6.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@ -614,6 +614,14 @@ impl FromRawDescriptor for UnixSeqpacket {
}
}
impl IntoRawDescriptor for UnixSeqpacket {
fn into_raw_descriptor(self) -> RawDescriptor {
let fd = self.fd;
mem::forget(self);
fd
}
}
impl AsRawFd for UnixSeqpacket {
fn as_raw_fd(&self) -> RawFd {
self.fd