From: Dietmar Maurer Date: Thu, 18 Nov 2021 12:06:34 +0000 (+0100) Subject: proxmox: add fd_change_cloexec X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=9f4c20f3d2dfb6f42acdb174b415df903d5006d8;p=proxmox.git proxmox: add fd_change_cloexec --- diff --git a/proxmox/src/tools/fd.rs b/proxmox/src/tools/fd.rs index e13172d5..582f884f 100644 --- a/proxmox/src/tools/fd.rs +++ b/proxmox/src/tools/fd.rs @@ -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(()) +}