mirror of
https://git.proxmox.com/git/proxmox
synced 2025-06-02 07:29:30 +00:00
proxmox: add fd_change_cloexec
This commit is contained in:
parent
3a378a34bb
commit
9f4c20f3d2
@ -7,6 +7,7 @@ use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
use nix::fcntl::OFlag;
|
||||
use nix::sys::stat::Mode;
|
||||
use nix::NixPath;
|
||||
use nix::fcntl::{fcntl, FdFlag, F_GETFD, F_SETFD};
|
||||
|
||||
/// Guard a raw file descriptor with a drop handler. This is mostly useful when access to an owned
|
||||
/// `RawFd` is required without the corresponding handler object (such as when only the file
|
||||
@ -194,3 +195,11 @@ impl<'a, T: ?Sized + AsRawFd> From<&'a T> for BorrowedFd<'a> {
|
||||
Self::new(fd)
|
||||
}
|
||||
}
|
||||
|
||||
/// Change the `O_CLOEXEC` flag of an existing file descriptor.
|
||||
pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), anyhow::Error> {
|
||||
let mut flags = unsafe { FdFlag::from_bits_unchecked(fcntl(fd, F_GETFD)?) };
|
||||
flags.set(FdFlag::FD_CLOEXEC, on);
|
||||
fcntl(fd, F_SETFD(flags))?;
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user