pub struct FileDesc { /* private fields */ }
Expand description
Thin wrapper around an open file descriptor.
The wrapped file descriptor will be closed when the wrapper is dropped.
Implementations§
Source§impl FileDesc
impl FileDesc
Sourcepub fn new(fd: OwnedFd) -> Self
pub fn new(fd: OwnedFd) -> Self
Create FileDesc
from an owned file descriptor.
This does not do anything to the file descriptor other than wrap it.
Notably, it does not set the close-on-exec
flag.
Sourcepub unsafe fn from_raw_fd(fd: RawFd) -> Self
pub unsafe fn from_raw_fd(fd: RawFd) -> Self
Wrap a raw file descriptor in a FileDesc
.
This does not do anything to the file descriptor other than wrapping it.
Notably, it does not set the close-on-exec
flag.
§Safety
The input must be a valid file descriptor.
The file descriptor must not be closed as long as it is managed by the created FileDesc
.
Sourcepub fn duplicate_from<T: AsFd>(other: T) -> Result<Self>
pub fn duplicate_from<T: AsFd>(other: T) -> Result<Self>
Duplicate a file descriptor from an object that has a file descriptor.
The new file descriptor will have the close-on-exec
flag set.
If the platform supports it, the flag will be set atomically.
The duplicated FileDesc
will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.
Sourcepub unsafe fn duplicate_raw_fd(fd: RawFd) -> Result<Self>
pub unsafe fn duplicate_raw_fd(fd: RawFd) -> Result<Self>
Duplicate a raw file descriptor and wrap it in a FileDesc
.
The new file descriptor will have the close-on-exec
flag set.
If the platform supports it, the flag will be set atomically.
The duplicated FileDesc
will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.
§Safety
The file descriptor must be valid, and duplicating it must not violate the safety requirements of any object already using the file descriptor.
Sourcepub fn as_fd(&self) -> BorrowedFd<'_>
pub fn as_fd(&self) -> BorrowedFd<'_>
Get the file descriptor.
This function does not release ownership of the underlying file descriptor.
The file descriptor will still be closed when the FileDesc
is dropped.
Sourcepub fn into_fd(self) -> OwnedFd
pub fn into_fd(self) -> OwnedFd
Release and get the raw file descriptor.
This function releases ownership of the underlying file descriptor. The file descriptor will not be closed.
Sourcepub fn as_raw_fd(&self) -> RawFd
pub fn as_raw_fd(&self) -> RawFd
Get the raw file descriptor.
This function does not release ownership of the underlying file descriptor.
The file descriptor will still be closed when the FileDesc
is dropped.
Sourcepub fn into_raw_fd(self) -> RawFd
pub fn into_raw_fd(self) -> RawFd
Release and get the raw file descriptor.
This function releases ownership of the underlying file descriptor. The file descriptor will not be closed.
Sourcepub fn duplicate(&self) -> Result<Self>
pub fn duplicate(&self) -> Result<Self>
Try to duplicate the file descriptor.
The duplicated FileDesc
will be the sole owner of the new file descriptor, but it will share ownership of the underlying kernel object.
The new file descriptor will have the close-on-exec
flag set.
If the platform supports it, the flag will be set atomically.
Sourcepub fn set_close_on_exec(&self, close_on_exec: bool) -> Result<()>
pub fn set_close_on_exec(&self, close_on_exec: bool) -> Result<()>
Change the close-on-exec flag of the file descriptor.
You should always try to create file descriptors with the close-on-exec flag already set atomically instead of changing it later with this function.
Setting the flag later on introduces a race condition if another thread forks before the call to set_close_on_exec
finishes.
You can use this without any race condition to disable the close-on-exec
flag after forking but before executing a new program.
Sourcepub fn get_close_on_exec(&self) -> Result<bool>
pub fn get_close_on_exec(&self) -> Result<bool>
Check the close-on-exec flag of the file descriptor.