Skip to content

Commit 10127d9

Browse files
committed
set CLOEXEC on pidfd received from child process
1 parent 531cb83 commit 10127d9

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

library/std/src/sys/unix/process/process_unix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl Command {
748748
msg.msg_controllen = mem::size_of::<Cmsg>() as _;
749749
msg.msg_control = &mut cmsg as *mut _ as *mut _;
750750

751-
match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, 0)) {
751+
match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, libc::MSG_CMSG_CLOEXEC)) {
752752
Err(_) => return -1,
753753
Ok(_) => {}
754754
}

library/std/src/sys/unix/process/process_unix/tests.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn test_command_fork_no_unwind() {
6464
#[test]
6565
#[cfg(target_os = "linux")]
6666
fn test_command_pidfd() {
67-
use crate::os::fd::RawFd;
67+
use crate::os::fd::{AsRawFd, RawFd};
6868
use crate::os::linux::process::{ChildExt, CommandExt};
6969
use crate::process::Command;
7070

@@ -82,6 +82,10 @@ fn test_command_pidfd() {
8282

8383
// but only check if we know that the kernel supports pidfds
8484
if pidfd_open_available {
85-
assert!(child.pidfd().is_ok())
85+
assert!(child.pidfd().is_ok());
86+
}
87+
if let Ok(pidfd) = child.pidfd() {
88+
let flags = super::cvt(unsafe { libc::fcntl(pidfd.as_raw_fd(), libc::F_GETFD) }).unwrap();
89+
assert!(flags & libc::FD_CLOEXEC != 0);
8690
}
8791
}

0 commit comments

Comments
 (0)