From 2bcec5c3d9603861c43a8cff1e7ea70e0a1dd214 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Fri, 3 Dec 2021 15:28:28 -0800 Subject: [PATCH] sys_util: impl From 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 Tested-by: kokoro Commit-Queue: Chia-I Wu --- common/sys_util/src/descriptor.rs | 9 ++++++++- common/sys_util/src/net.rs | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/common/sys_util/src/descriptor.rs b/common/sys_util/src/descriptor.rs index 45ae618e64..b83bfd47da 100644 --- a/common/sys_util/src/descriptor.rs +++ b/common/sys_util/src/descriptor.rs @@ -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 for UnixStream { } } +impl From 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 diff --git a/common/sys_util/src/net.rs b/common/sys_util/src/net.rs index 294f1155e3..ef703f42a9 100644 --- a/common/sys_util/src/net.rs +++ b/common/sys_util/src/net.rs @@ -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