diff options
Diffstat (limited to 'base/src/sys')
-rw-r--r-- | base/src/sys/unix/descriptor.rs | 27 | ||||
-rw-r--r-- | base/src/sys/windows/tube.rs | 12 |
2 files changed, 25 insertions, 14 deletions
diff --git a/base/src/sys/unix/descriptor.rs b/base/src/sys/unix/descriptor.rs index 26a9cd2c3..fd97efe6e 100644 --- a/base/src/sys/unix/descriptor.rs +++ b/base/src/sys/unix/descriptor.rs @@ -32,24 +32,25 @@ pub type RawDescriptor = RawFd; pub const INVALID_DESCRIPTOR: RawDescriptor = -1; -/// Clones `descriptor`, returning a new `RawDescriptor` that refers to the same open file -/// description as `descriptor`. The cloned descriptor will have the `FD_CLOEXEC` flag set but will -/// not share any other file descriptor flags with `descriptor`. -pub fn clone_descriptor(descriptor: &dyn AsRawDescriptor) -> Result<RawDescriptor> { - clone_fd(&descriptor.as_raw_descriptor()) +/// Clones `descriptor`, returning a new `SafeDescriptor` that refers to the same file +/// `descriptor`. The cloned descriptor will have the `FD_CLOEXEC` flag set but will not share any +/// other file descriptor flags with `descriptor`. +pub fn clone_descriptor(descriptor: &(impl AsRawDescriptor + ?Sized)) -> Result<SafeDescriptor> { + clone_fd(descriptor.as_raw_descriptor()) } -/// Clones `fd`, returning a new file descriptor that refers to the same open file description as -/// `fd`. The cloned fd will have the `FD_CLOEXEC` flag set but will not share any other file -/// descriptor flags with `fd`. -fn clone_fd(fd: &dyn AsRawFd) -> Result<RawFd> { +/// Clones `fd`, returning a new file descriptor that refers to the same open file as `fd`. The +/// cloned fd will have the `FD_CLOEXEC` flag set but will not share any other file descriptor +/// flags with `fd`. +fn clone_fd(fd: RawFd) -> Result<SafeDescriptor> { // SAFETY: // Safe because this doesn't modify any memory and we check the return value. - let ret = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_DUPFD_CLOEXEC, 0) }; + let ret = unsafe { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, 0) }; if ret < 0 { errno_result() } else { - Ok(ret) + // SAFETY: We just dup'd the FD and so have exclusive access. + Ok(unsafe { SafeDescriptor::from_raw_descriptor(ret) }) } } @@ -108,9 +109,7 @@ impl TryFrom<&dyn AsRawFd> for SafeDescriptor { type Error = std::io::Error; fn try_from(fd: &dyn AsRawFd) -> std::result::Result<Self, Self::Error> { - Ok(SafeDescriptor { - descriptor: clone_fd(fd)?, - }) + Ok(clone_fd(fd.as_raw_fd())?) } } diff --git a/base/src/sys/windows/tube.rs b/base/src/sys/windows/tube.rs index ed61c2f39..b65350837 100644 --- a/base/src/sys/windows/tube.rs +++ b/base/src/sys/windows/tube.rs @@ -384,6 +384,18 @@ impl AsRawDescriptor for RecvTube { } } +impl CloseNotifier for SendTube { + fn get_close_notifier(&self) -> &dyn AsRawDescriptor { + self.0.get_close_notifier() + } +} + +impl CloseNotifier for RecvTube { + fn get_close_notifier(&self) -> &dyn AsRawDescriptor { + self.0.get_close_notifier() + } +} + /// A request to duplicate a handle to a target process. #[derive(Serialize, Deserialize, Debug)] pub struct DuplicateHandleRequest { |