@@ -4,6 +4,7 @@ use crate::io::{self, Error, ErrorKind};
44use crate :: mem;
55use crate :: num:: NonZeroI32 ;
66use crate :: os:: raw:: NonZero_c_int ;
7+ use crate :: os:: unix:: io:: FromRawFd ;
78use crate :: ptr;
89use crate :: sys;
910use crate :: sys:: cvt;
@@ -97,7 +98,9 @@ impl Command {
9798 drop ( env_lock) ;
9899 drop ( output) ;
99100
100- let mut p = Process :: new ( pid, pidfd) ;
101+ // Safety: We obtained the pidfd from calling `clone3` with
102+ // `CLONE_PIDFD` so it's valid an otherwise unowned.
103+ let mut p = unsafe { Process :: new ( pid, pidfd) } ;
101104 let mut bytes = [ 0 ; 8 ] ;
102105
103106 // loop to handle EINTR
@@ -446,7 +449,8 @@ impl Command {
446449 None => None ,
447450 } ;
448451
449- let mut p = Process :: new ( 0 , -1 ) ;
452+ // Safety: -1 indicates we don't have a pidfd.
453+ let mut p = unsafe { Process :: new ( 0 , -1 ) } ;
450454
451455 struct PosixSpawnFileActions < ' a > ( & ' a mut MaybeUninit < libc:: posix_spawn_file_actions_t > ) ;
452456
@@ -545,14 +549,16 @@ pub struct Process {
545549
546550impl Process {
547551 #[ cfg( target_os = "linux" ) ]
548- fn new ( pid : pid_t , pidfd : pid_t ) -> Self {
552+ unsafe fn new ( pid : pid_t , pidfd : pid_t ) -> Self {
549553 use crate :: sys_common:: FromInner ;
550- let pidfd = ( pidfd >= 0 ) . then ( || PidFd :: from_inner ( sys:: fd:: FileDesc :: new ( pidfd) ) ) ;
554+ // Safety: If `pidfd` is nonnegative, we assume it's valid and otherwise unowned.
555+ let pidfd = ( pidfd >= 0 )
556+ . then ( || PidFd :: from_inner ( unsafe { sys:: fd:: FileDesc :: from_raw_fd ( pidfd) } ) ) ;
551557 Process { pid, status : None , pidfd }
552558 }
553559
554560 #[ cfg( not( target_os = "linux" ) ) ]
555- fn new ( pid : pid_t , _pidfd : pid_t ) -> Self {
561+ unsafe fn new ( pid : pid_t , _pidfd : pid_t ) -> Self {
556562 Process { pid, status : None }
557563 }
558564
0 commit comments