set CLOEXEC on pidfd received from child process

This commit is contained in:
The 8472 2023-11-16 01:35:51 +01:00
parent 531cb83fcf
commit 10127d9eb5
2 changed files with 7 additions and 3 deletions

View File

@ -748,7 +748,7 @@ impl Command {
msg.msg_controllen = mem::size_of::<Cmsg>() as _; msg.msg_controllen = mem::size_of::<Cmsg>() as _;
msg.msg_control = &mut cmsg as *mut _ as *mut _; msg.msg_control = &mut cmsg as *mut _ as *mut _;
match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, 0)) { match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, libc::MSG_CMSG_CLOEXEC)) {
Err(_) => return -1, Err(_) => return -1,
Ok(_) => {} Ok(_) => {}
} }

View File

@ -64,7 +64,7 @@ fn test_command_fork_no_unwind() {
#[test] #[test]
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn test_command_pidfd() { fn test_command_pidfd() {
use crate::os::fd::RawFd; use crate::os::fd::{AsRawFd, RawFd};
use crate::os::linux::process::{ChildExt, CommandExt}; use crate::os::linux::process::{ChildExt, CommandExt};
use crate::process::Command; use crate::process::Command;
@ -82,6 +82,10 @@ fn test_command_pidfd() {
// but only check if we know that the kernel supports pidfds // but only check if we know that the kernel supports pidfds
if pidfd_open_available { if pidfd_open_available {
assert!(child.pidfd().is_ok()) assert!(child.pidfd().is_ok());
}
if let Ok(pidfd) = child.pidfd() {
let flags = super::cvt(unsafe { libc::fcntl(pidfd.as_raw_fd(), libc::F_GETFD) }).unwrap();
assert!(flags & libc::FD_CLOEXEC != 0);
} }
} }