@@ -4,6 +4,7 @@ use crate::io::{self, Error, ErrorKind};
4
4
use crate :: mem;
5
5
use crate :: num:: NonZeroI32 ;
6
6
use crate :: os:: raw:: NonZero_c_int ;
7
+ use crate :: os:: unix:: io:: FromRawFd ;
7
8
use crate :: ptr;
8
9
use crate :: sys;
9
10
use crate :: sys:: cvt;
@@ -97,7 +98,9 @@ impl Command {
97
98
drop ( env_lock) ;
98
99
drop ( output) ;
99
100
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) } ;
101
104
let mut bytes = [ 0 ; 8 ] ;
102
105
103
106
// loop to handle EINTR
@@ -446,7 +449,8 @@ impl Command {
446
449
None => None ,
447
450
} ;
448
451
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 ) } ;
450
454
451
455
struct PosixSpawnFileActions < ' a > ( & ' a mut MaybeUninit < libc:: posix_spawn_file_actions_t > ) ;
452
456
@@ -545,14 +549,16 @@ pub struct Process {
545
549
546
550
impl Process {
547
551
#[ 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 {
549
553
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) } ) ) ;
551
557
Process { pid, status : None , pidfd }
552
558
}
553
559
554
560
#[ 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 {
556
562
Process { pid, status : None }
557
563
}
558
564
0 commit comments