Skip to content

Commit

Permalink
Don't set cmsg fields in msghdr if we have no cmsg to send
Browse files Browse the repository at this point in the history
  • Loading branch information
the8472 committed Nov 19, 2023
1 parent 12efa53 commit f34e7f4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions library/std/src/sys/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,12 @@ impl Command {

msg.msg_iov = &mut iov as *mut _ as *mut _;
msg.msg_iovlen = 1;
msg.msg_controllen = mem::size_of_val(&cmsg.buf) as _;
msg.msg_control = &mut cmsg.buf as *mut _ as *mut _;

// only attach cmsg if we successfully acquired the pidfd
if pidfd >= 0 {
msg.msg_controllen = mem::size_of_val(&cmsg.buf) as _;
msg.msg_control = &mut cmsg.buf as *mut _ as *mut _;

let hdr = CMSG_FIRSTHDR(&mut msg as *mut _ as *mut _);
(*hdr).cmsg_level = SOL_SOCKET;
(*hdr).cmsg_type = SCM_RIGHTS;
Expand All @@ -719,7 +720,7 @@ impl Command {
// so we get a consistent SEQPACKET order
match cvt_r(|| libc::sendmsg(sock.as_raw(), &msg, 0)) {
Ok(0) => {}
_ => rtabort!("failed to communicate with parent process"),
other => rtabort!("failed to communicate with parent process. {:?}", other),
}
}
}
Expand Down

0 comments on commit f34e7f4

Please sign in to comment.