unix.rs - source (original) (raw)

std/sys/process/unix/

unix.rs

1#[cfg(target_os = "vxworks")]
2use libc::RTP_ID as pid_t;
3#[cfg(not(target_os = "vxworks"))]
4use libc::{c_int, pid_t};
5#[cfg(not(any(
6    target_os = "vxworks",
7    target_os = "l4re",
8    target_os = "tvos",
9    target_os = "watchos",
10)))]
11use libc::{gid_t, uid_t};
12
13use super::common::*;
14use crate::io::{self, Error, ErrorKind};
15use crate::num::NonZero;
16use crate::sys::cvt;
17#[cfg(target_os = "linux")]
18use crate::sys::pal::linux::pidfd::PidFd;
19use crate::{fmt, mem, sys};
20
21cfg_if::cfg_if! {
22    if #[cfg(target_os = "nto")] {
23        use crate::thread;
24        use libc::{c_char, posix_spawn_file_actions_t, posix_spawnattr_t};
25        use crate::time::Duration;
26        use crate::sync::LazyLock;
27        // Get smallest amount of time we can sleep.
28        // Return a common value if it cannot be determined.
29        fn get_clock_resolution() -> Duration {
30            static MIN_DELAY: LazyLock<Duration, fn() -> Duration> = LazyLock::new(|| {
31                let mut mindelay = libc::timespec { tv_sec: 0, tv_nsec: 0 };
32                if unsafe { libc::clock_getres(libc::CLOCK_MONOTONIC, &mut mindelay) } == 0
33                {
34                    Duration::from_nanos(mindelay.tv_nsec as u64)
35                } else {
36                    Duration::from_millis(1)
37                }
38            });
39            *MIN_DELAY
40        }
41        // Arbitrary minimum sleep duration for retrying fork/spawn
42        const MIN_FORKSPAWN_SLEEP: Duration = Duration::from_nanos(1);
43        // Maximum duration of sleeping before giving up and returning an error
44        const MAX_FORKSPAWN_SLEEP: Duration = Duration::from_millis(1000);
45    }
46}
47
48////////////////////////////////////////////////////////////////////////////////
49// Command
50////////////////////////////////////////////////////////////////////////////////
51
52impl Command {
53    pub fn spawn(
54        &mut self,
55        default: Stdio,
56        needs_stdin: bool,
57    ) -> io::Result<(Process, StdioPipes)> {
58        const CLOEXEC_MSG_FOOTER: [u8; 4] = *b"NOEX";
59
60        let envp = self.capture_env();
61
62        if self.saw_nul() {
63            return Err(io::const_error!(
64                ErrorKind::InvalidInput,
65                "nul byte found in provided data",
66            ));
67        }
68
69        let (ours, theirs) = self.setup_io(default, needs_stdin)?;
70
71        if let Some(ret) = self.posix_spawn(&theirs, envp.as_ref())? {
72            return Ok((ret, ours));
73        }
74
75        #[cfg(target_os = "linux")]
76        let (input, output) = sys:🥅:Socket::new_pair(libc::AF_UNIX, libc::SOCK_SEQPACKET)?;
77
78        #[cfg(not(target_os = "linux"))]
79        let (input, output) = sys::pipe::anon_pipe()?;
80
81        // Whatever happens after the fork is almost for sure going to touch or
82        // look at the environment in one way or another (PATH in `execvp` or
83        // accessing the `environ` pointer ourselves). Make sure no other thread
84        // is accessing the environment when we do the fork itself.
85        //
86        // Note that as soon as we're done with the fork there's no need to hold
87        // a lock any more because the parent won't do anything and the child is
88        // in its own process. Thus the parent drops the lock guard immediately.
89        // The child calls `mem::forget` to leak the lock, which is crucial because
90        // releasing a lock is not async-signal-safe.
91        let env_lock = sys::env::env_read_lock();
92        let pid = unsafe { self.do_fork()? };
93
94        if pid == 0 {
95            crate::panic::always_abort();
96            mem::forget(env_lock); // avoid non-async-signal-safe unlocking
97            drop(input);
98            #[cfg(target_os = "linux")]
99            if self.get_create_pidfd() {
100                self.send_pidfd(&output);
101            }
102            let Err(err) = unsafe { self.do_exec(theirs, envp.as_ref()) };
103            let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
104            let errno = errno.to_be_bytes();
105            let bytes = [
106                errno[0],
107                errno[1],
108                errno[2],
109                errno[3],
110                CLOEXEC_MSG_FOOTER[0],
111                CLOEXEC_MSG_FOOTER[1],
112                CLOEXEC_MSG_FOOTER[2],
113                CLOEXEC_MSG_FOOTER[3],
114            ];
115            // pipe I/O up to PIPE_BUF bytes should be atomic, and then
116            // we want to be sure we *don't* run at_exit destructors as
117            // we're being torn down regardless
118            rtassert!(output.write(&bytes).is_ok());
119            unsafe { libc::_exit(1) }
120        }
121
122        drop(env_lock);
123        drop(output);
124
125        #[cfg(target_os = "linux")]
126        let pidfd = if self.get_create_pidfd() { self.recv_pidfd(&input) } else { -1 };
127
128        #[cfg(not(target_os = "linux"))]
129        let pidfd = -1;
130
131        // Safety: We obtained the pidfd (on Linux) using SOCK_SEQPACKET, so it's valid.
132        let mut p = unsafe { Process::new(pid, pidfd) };
133        let mut bytes = [0; 8];
134
135        // loop to handle EINTR
136        loop {
137            match input.read(&mut bytes) {
138                Ok(0) => return Ok((p, ours)),
139                Ok(8) => {
140                    let (errno, footer) = bytes.split_at(4);
141                    assert_eq!(
142                        CLOEXEC_MSG_FOOTER, footer,
143                        "Validation on the CLOEXEC pipe failed: {:?}",
144                        bytes
145                    );
146                    let errno = i32::from_be_bytes(errno.try_into().unwrap());
147                    assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
148                    return Err(Error::from_raw_os_error(errno));
149                }
150                Err(ref e) if e.is_interrupted() => {}
151                Err(e) => {
152                    assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
153                    panic!("the CLOEXEC pipe failed: {e:?}")
154                }
155                Ok(..) => {
156                    // pipe I/O up to PIPE_BUF bytes should be atomic
157                    // similarly SOCK_SEQPACKET messages should arrive whole
158                    assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
159                    panic!("short read on the CLOEXEC pipe")
160                }
161            }
162        }
163    }
164
165    // WatchOS and TVOS headers mark the `fork`/`exec*` functions with
166    // `__WATCHOS_PROHIBITED __TVOS_PROHIBITED`, and indicate that the
167    // `posix_spawn*` functions should be used instead. It isn't entirely clear
168    // what `PROHIBITED` means here (e.g. if calls to these functions are
169    // allowed to exist in dead code), but it sounds bad, so we go out of our
170    // way to avoid that all-together.
171    #[cfg(any(target_os = "tvos", target_os = "watchos"))]
172    const ERR_APPLE_TV_WATCH_NO_FORK_EXEC: Error = io::const_error!(
173        ErrorKind::Unsupported,
174        "`fork`+`exec`-based process spawning is not supported on this target",
175    );
176
177    #[cfg(any(target_os = "tvos", target_os = "watchos"))]
178    unsafe fn do_fork(&mut self) -> Result<pid_t, io::Error> {
179        return Err(Self::ERR_APPLE_TV_WATCH_NO_FORK_EXEC);
180    }
181
182    // Attempts to fork the process. If successful, returns Ok((0, -1))
183    // in the child, and Ok((child_pid, -1)) in the parent.
184    #[cfg(not(any(target_os = "watchos", target_os = "tvos", target_os = "nto")))]
185    unsafe fn do_fork(&mut self) -> Result<pid_t, io::Error> {
186        cvt(libc::fork())
187    }
188
189    // On QNX Neutrino, fork can fail with EBADF in case "another thread might have opened
190    // or closed a file descriptor while the fork() was occurring".
191    // Documentation says "... or try calling fork() again". This is what we do here.
192    // See also https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/f/fork.html
193    #[cfg(target_os = "nto")]
194    unsafe fn do_fork(&mut self) -> Result<pid_t, io::Error> {
195        use crate::sys::os::errno;
196
197        let mut delay = MIN_FORKSPAWN_SLEEP;
198
199        loop {
200            let r = libc::fork();
201            if r == -1 as libc::pid_t && errno() as libc::c_int == libc::EBADF {
202                if delay < get_clock_resolution() {
203                    // We cannot sleep this short (it would be longer).
204                    // Yield instead.
205                    thread::yield_now();
206                } else if delay < MAX_FORKSPAWN_SLEEP {
207                    thread::sleep(delay);
208                } else {
209                    return Err(io::const_error!(
210                        ErrorKind::WouldBlock,
211                        "forking returned EBADF too often",
212                    ));
213                }
214                delay *= 2;
215                continue;
216            } else {
217                return cvt(r);
218            }
219        }
220    }
221
222    pub fn exec(&mut self, default: Stdio) -> io::Error {
223        let envp = self.capture_env();
224
225        if self.saw_nul() {
226            return io::const_error!(ErrorKind::InvalidInput, "nul byte found in provided data");
227        }
228
229        match self.setup_io(default, true) {
230            Ok((_, theirs)) => {
231                unsafe {
232                    // Similar to when forking, we want to ensure that access to
233                    // the environment is synchronized, so make sure to grab the
234                    // environment lock before we try to exec.
235                    let _lock = sys::env::env_read_lock();
236
237                    let Err(e) = self.do_exec(theirs, envp.as_ref());
238                    e
239                }
240            }
241            Err(e) => e,
242        }
243    }
244
245    // And at this point we've reached a special time in the life of the
246    // child. The child must now be considered hamstrung and unable to
247    // do anything other than syscalls really. Consider the following
248    // scenario:
249    //
250    //      1. Thread A of process 1 grabs the malloc() mutex
251    //      2. Thread B of process 1 forks(), creating thread C
252    //      3. Thread C of process 2 then attempts to malloc()
253    //      4. The memory of process 2 is the same as the memory of
254    //         process 1, so the mutex is locked.
255    //
256    // This situation looks a lot like deadlock, right? It turns out
257    // that this is what pthread_atfork() takes care of, which is
258    // presumably implemented across platforms. The first thing that
259    // threads to *before* forking is to do things like grab the malloc
260    // mutex, and then after the fork they unlock it.
261    //
262    // Despite this information, libnative's spawn has been witnessed to
263    // deadlock on both macOS and FreeBSD. I'm not entirely sure why, but
264    // all collected backtraces point at malloc/free traffic in the
265    // child spawned process.
266    //
267    // For this reason, the block of code below should contain 0
268    // invocations of either malloc of free (or their related friends).
269    //
270    // As an example of not having malloc/free traffic, we don't close
271    // this file descriptor by dropping the FileDesc (which contains an
272    // allocation). Instead we just close it manually. This will never
273    // have the drop glue anyway because this code never returns (the
274    // child will either exec() or invoke libc::exit)
275    #[cfg(not(any(target_os = "tvos", target_os = "watchos")))]
276    unsafe fn do_exec(
277        &mut self,
278        stdio: ChildPipes,
279        maybe_envp: Option<&CStringArray>,
280    ) -> Result<!, io::Error> {
281        use crate::sys::{self, cvt_r};
282
283        if let Some(fd) = stdio.stdin.fd() {
284            cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?;
285        }
286        if let Some(fd) = stdio.stdout.fd() {
287            cvt_r(|| libc::dup2(fd, libc::STDOUT_FILENO))?;
288        }
289        if let Some(fd) = stdio.stderr.fd() {
290            cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?;
291        }
292
293        #[cfg(not(target_os = "l4re"))]
294        {
295            if let Some(_g) = self.get_groups() {
296                //FIXME: Redox kernel does not support setgroups yet
297                #[cfg(not(target_os = "redox"))]
298                cvt(libc::setgroups(_g.len().try_into().unwrap(), _g.as_ptr()))?;
299            }
300            if let Some(u) = self.get_gid() {
301                cvt(libc::setgid(u as gid_t))?;
302            }
303            if let Some(u) = self.get_uid() {
304                // When dropping privileges from root, the `setgroups` call
305                // will remove any extraneous groups. We only drop groups
306                // if we have CAP_SETGID and we weren't given an explicit
307                // set of groups. If we don't call this, then even though our
308                // uid has dropped, we may still have groups that enable us to
309                // do super-user things.
310                //FIXME: Redox kernel does not support setgroups yet
311                #[cfg(not(target_os = "redox"))]
312                if self.get_groups().is_none() {
313                    let res = cvt(libc::setgroups(0, crate::ptr::null()));
314                    if let Err(e) = res {
315                        // Here we ignore the case of not having CAP_SETGID.
316                        // An alternative would be to require CAP_SETGID (in
317                        // addition to CAP_SETUID) for setting the UID.
318                        if e.raw_os_error() != Some(libc::EPERM) {
319                            return Err(e.into());
320                        }
321                    }
322                }
323                cvt(libc::setuid(u as uid_t))?;
324            }
325        }
326        if let Some(cwd) = self.get_cwd() {
327            cvt(libc::chdir(cwd.as_ptr()))?;
328        }
329
330        if let Some(pgroup) = self.get_pgroup() {
331            cvt(libc::setpgid(0, pgroup))?;
332        }
333
334        // emscripten has no signal support.
335        #[cfg(not(target_os = "emscripten"))]
336        {
337            // Inherit the signal mask from the parent rather than resetting it (i.e. do not call
338            // pthread_sigmask).
339
340            // If -Zon-broken-pipe is used, don't reset SIGPIPE to SIG_DFL.
341            // If -Zon-broken-pipe is not used, reset SIGPIPE to SIG_DFL for backward compatibility.
342            //
343            // -Zon-broken-pipe is an opportunity to change the default here.
344            if !crate::sys::pal::on_broken_pipe_flag_used() {
345                #[cfg(target_os = "android")] // see issue #88585
346                {
347                    let mut action: libc::sigaction = mem::zeroed();
348                    action.sa_sigaction = libc::SIG_DFL;
349                    cvt(libc::sigaction(libc::SIGPIPE, &action, crate::ptr::null_mut()))?;
350                }
351                #[cfg(not(target_os = "android"))]
352                {
353                    let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL);
354                    if ret == libc::SIG_ERR {
355                        return Err(io::Error::last_os_error());
356                    }
357                }
358                #[cfg(target_os = "hurd")]
359                {
360                    let ret = sys::signal(libc::SIGLOST, libc::SIG_DFL);
361                    if ret == libc::SIG_ERR {
362                        return Err(io::Error::last_os_error());
363                    }
364                }
365            }
366        }
367
368        for callback in self.get_closures().iter_mut() {
369            callback()?;
370        }
371
372        // Although we're performing an exec here we may also return with an
373        // error from this function (without actually exec'ing) in which case we
374        // want to be sure to restore the global environment back to what it
375        // once was, ensuring that our temporary override, when free'd, doesn't
376        // corrupt our process's environment.
377        let mut _reset = None;
378        if let Some(envp) = maybe_envp {
379            struct Reset(*const *const libc::c_char);
380
381            impl Drop for Reset {
382                fn drop(&mut self) {
383                    unsafe {
384                        *sys::env::environ() = self.0;
385                    }
386                }
387            }
388
389            _reset = Some(Reset(*sys::env::environ()));
390            *sys::env::environ() = envp.as_ptr();
391        }
392
393        libc::execvp(self.get_program_cstr().as_ptr(), self.get_argv().as_ptr());
394        Err(io::Error::last_os_error())
395    }
396
397    #[cfg(any(target_os = "tvos", target_os = "watchos"))]
398    unsafe fn do_exec(
399        &mut self,
400        _stdio: ChildPipes,
401        _maybe_envp: Option<&CStringArray>,
402    ) -> Result<!, io::Error> {
403        return Err(Self::ERR_APPLE_TV_WATCH_NO_FORK_EXEC);
404    }
405
406    #[cfg(not(any(
407        target_os = "freebsd",
408        target_os = "illumos",
409        all(target_os = "linux", target_env = "gnu"),
410        all(target_os = "linux", target_env = "musl"),
411        target_os = "nto",
412        target_vendor = "apple",
413        target_os = "cygwin",
414    )))]
415    fn posix_spawn(
416        &mut self,
417        _: &ChildPipes,
418        _: Option<&CStringArray>,
419    ) -> io::Result<Option<Process>> {
420        Ok(None)
421    }
422
423    // Only support platforms for which posix_spawn() can return ENOENT
424    // directly.
425    #[cfg(any(
426        target_os = "freebsd",
427        target_os = "illumos",
428        all(target_os = "linux", target_env = "gnu"),
429        all(target_os = "linux", target_env = "musl"),
430        target_os = "nto",
431        target_vendor = "apple",
432        target_os = "cygwin",
433    ))]
434    fn posix_spawn(
435        &mut self,
436        stdio: &ChildPipes,
437        envp: Option<&CStringArray>,
438    ) -> io::Result<Option<Process>> {
439        #[cfg(target_os = "linux")]
440        use core::sync::atomic::{Atomic, AtomicU8, Ordering};
441
442        use crate::mem::MaybeUninit;
443        use crate::sys::{self, cvt_nz, on_broken_pipe_flag_used};
444
445        if self.get_gid().is_some()
446            || self.get_uid().is_some()
447            || (self.env_saw_path() && !self.program_is_path())
448            || !self.get_closures().is_empty()
449            || self.get_groups().is_some()
450        {
451            return Ok(None);
452        }
453
454        cfg_if::cfg_if! {
455            if #[cfg(target_os = "linux")] {
456                use crate::sys::weak::weak;
457
458                weak!(
459                    fn pidfd_spawnp(
460                        pidfd: *mut libc::c_int,
461                        path: *const libc::c_char,
462                        file_actions: *const libc::posix_spawn_file_actions_t,
463                        attrp: *const libc::posix_spawnattr_t,
464                        argv: *const *mut libc::c_char,
465                        envp: *const *mut libc::c_char,
466                    ) -> libc::c_int;
467                );
468
469                weak!(
470                    fn pidfd_getpid(pidfd: libc::c_int) -> libc::c_int;
471                );
472
473                static PIDFD_SUPPORTED: Atomic<u8> = AtomicU8::new(0);
474                const UNKNOWN: u8 = 0;
475                const SPAWN: u8 = 1;
476                // Obtaining a pidfd via the fork+exec path might work
477                const FORK_EXEC: u8 = 2;
478                // Neither pidfd_spawn nor fork/exec will get us a pidfd.
479                // Instead we'll just posix_spawn if the other preconditions are met.
480                const NO: u8 = 3;
481
482                if self.get_create_pidfd() {
483                    let mut support = PIDFD_SUPPORTED.load(Ordering::Relaxed);
484                    if support == FORK_EXEC {
485                        return Ok(None);
486                    }
487                    if support == UNKNOWN {
488                        support = NO;
489                        let our_pid = crate::process::id();
490                        let pidfd = cvt(unsafe { libc::syscall(libc::SYS_pidfd_open, our_pid, 0) } as c_int);
491                        match pidfd {
492                            Ok(pidfd) => {
493                                support = FORK_EXEC;
494                                if let Some(Ok(pid)) = pidfd_getpid.get().map(|f| cvt(unsafe { f(pidfd) } as i32)) {
495                                    if pidfd_spawnp.get().is_some() && pid as u32 == our_pid {
496                                        support = SPAWN
497                                    }
498                                }
499                                unsafe { libc::close(pidfd) };
500                            }
501                            Err(e) if e.raw_os_error() == Some(libc::EMFILE) => {
502                                // We're temporarily(?) out of file descriptors.  In this case obtaining a pidfd would also fail
503                                // Don't update the support flag so we can probe again later.
504                                return Err(e)
505                            }
506                            _ => {}
507                        }
508                        PIDFD_SUPPORTED.store(support, Ordering::Relaxed);
509                        if support == FORK_EXEC {
510                            return Ok(None);
511                        }
512                    }
513                    core::assert_matches::debug_assert_matches!(support, SPAWN | NO);
514                }
515            } else {
516                if self.get_create_pidfd() {
517                    unreachable!("only implemented on linux")
518                }
519            }
520        }
521
522        // Only glibc 2.24+ posix_spawn() supports returning ENOENT directly.
523        #[cfg(all(target_os = "linux", target_env = "gnu"))]
524        {
525            if let Some(version) = sys::os::glibc_version() {
526                if version < (2, 24) {
527                    return Ok(None);
528                }
529            } else {
530                return Ok(None);
531            }
532        }
533
534        // On QNX Neutrino, posix_spawnp can fail with EBADF in case "another thread might have opened
535        // or closed a file descriptor while the posix_spawn() was occurring".
536        // Documentation says "... or try calling posix_spawn() again". This is what we do here.
537        // See also http://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/p/posix_spawn.html
538        #[cfg(target_os = "nto")]
539        unsafe fn retrying_libc_posix_spawnp(
540            pid: *mut pid_t,
541            file: *const c_char,
542            file_actions: *const posix_spawn_file_actions_t,
543            attrp: *const posix_spawnattr_t,
544            argv: *const *mut c_char,
545            envp: *const *mut c_char,
546        ) -> io::Result<i32> {
547            let mut delay = MIN_FORKSPAWN_SLEEP;
548            loop {
549                match libc::posix_spawnp(pid, file, file_actions, attrp, argv, envp) {
550                    libc::EBADF => {
551                        if delay < get_clock_resolution() {
552                            // We cannot sleep this short (it would be longer).
553                            // Yield instead.
554                            thread::yield_now();
555                        } else if delay < MAX_FORKSPAWN_SLEEP {
556                            thread::sleep(delay);
557                        } else {
558                            return Err(io::const_error!(
559                                ErrorKind::WouldBlock,
560                                "posix_spawnp returned EBADF too often",
561                            ));
562                        }
563                        delay *= 2;
564                        continue;
565                    }
566                    r => {
567                        return Ok(r);
568                    }
569                }
570            }
571        }
572
573        type PosixSpawnAddChdirFn = unsafe extern "C" fn(
574            *mut libc::posix_spawn_file_actions_t,
575            *const libc::c_char,
576        ) -> libc::c_int;
577
578        /// Get the function pointer for adding a chdir action to a
579        /// `posix_spawn_file_actions_t`, if available, assuming a dynamic libc.
580        ///
581        /// Some platforms can set a new working directory for a spawned process in the
582        /// `posix_spawn` path. This function looks up the function pointer for adding
583        /// such an action to a `posix_spawn_file_actions_t` struct.
584        #[cfg(not(any(all(target_os = "linux", target_env = "musl"), target_os = "cygwin")))]
585        fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
586            use crate::sys::weak::weak;
587
588            // POSIX.1-2024 standardizes this function:
589            // https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_spawn_file_actions_addchdir.html.
590            // The _np version is more widely available, though, so try that first.
591
592            weak!(
593                fn posix_spawn_file_actions_addchdir_np(
594                    file_actions: *mut libc::posix_spawn_file_actions_t,
595                    path: *const libc::c_char,
596                ) -> libc::c_int;
597            );
598
599            weak!(
600                fn posix_spawn_file_actions_addchdir(
601                    file_actions: *mut libc::posix_spawn_file_actions_t,
602                    path: *const libc::c_char,
603                ) -> libc::c_int;
604            );
605
606            posix_spawn_file_actions_addchdir_np
607                .get()
608                .or_else(|| posix_spawn_file_actions_addchdir.get())
609        }
610
611        /// Get the function pointer for adding a chdir action to a
612        /// `posix_spawn_file_actions_t`, if available, on platforms where the function
613        /// is known to exist.
614        ///
615        /// Weak symbol lookup doesn't work with statically linked libcs, so in cases
616        /// where static linking is possible we need to either check for the presence
617        /// of the symbol at compile time or know about it upfront.
618        ///
619        /// Cygwin doesn't support weak symbol, so just link it.
620        #[cfg(any(all(target_os = "linux", target_env = "musl"), target_os = "cygwin"))]
621        fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
622            // Our minimum required musl supports this function, so we can just use it.
623            Some(libc::posix_spawn_file_actions_addchdir_np)
624        }
625
626        let addchdir = match self.get_cwd() {
627            Some(cwd) => {
628                if cfg!(target_vendor = "apple") {
629                    // There is a bug in macOS where a relative executable
630                    // path like "../myprogram" will cause `posix_spawn` to
631                    // successfully launch the program, but erroneously return
632                    // ENOENT when used with posix_spawn_file_actions_addchdir_np
633                    // which was introduced in macOS 10.15.
634                    if self.get_program_kind() == ProgramKind::Relative {
635                        return Ok(None);
636                    }
637                }
638                // Check for the availability of the posix_spawn addchdir
639                // function now. If it isn't available, bail and use the
640                // fork/exec path.
641                match get_posix_spawn_addchdir() {
642                    Some(f) => Some((f, cwd)),
643                    None => return Ok(None),
644                }
645            }
646            None => None,
647        };
648
649        let pgroup = self.get_pgroup();
650
651        struct PosixSpawnFileActions<'a>(&'a mut MaybeUninit<libc::posix_spawn_file_actions_t>);
652
653        impl Drop for PosixSpawnFileActions<'_> {
654            fn drop(&mut self) {
655                unsafe {
656                    libc::posix_spawn_file_actions_destroy(self.0.as_mut_ptr());
657                }
658            }
659        }
660
661        struct PosixSpawnattr<'a>(&'a mut MaybeUninit<libc::posix_spawnattr_t>);
662
663        impl Drop for PosixSpawnattr<'_> {
664            fn drop(&mut self) {
665                unsafe {
666                    libc::posix_spawnattr_destroy(self.0.as_mut_ptr());
667                }
668            }
669        }
670
671        unsafe {
672            let mut attrs = MaybeUninit::uninit();
673            cvt_nz(libc::posix_spawnattr_init(attrs.as_mut_ptr()))?;
674            let attrs = PosixSpawnattr(&mut attrs);
675
676            let mut flags = 0;
677
678            let mut file_actions = MaybeUninit::uninit();
679            cvt_nz(libc::posix_spawn_file_actions_init(file_actions.as_mut_ptr()))?;
680            let file_actions = PosixSpawnFileActions(&mut file_actions);
681
682            if let Some(fd) = stdio.stdin.fd() {
683                cvt_nz(libc::posix_spawn_file_actions_adddup2(
684                    file_actions.0.as_mut_ptr(),
685                    fd,
686                    libc::STDIN_FILENO,
687                ))?;
688            }
689            if let Some(fd) = stdio.stdout.fd() {
690                cvt_nz(libc::posix_spawn_file_actions_adddup2(
691                    file_actions.0.as_mut_ptr(),
692                    fd,
693                    libc::STDOUT_FILENO,
694                ))?;
695            }
696            if let Some(fd) = stdio.stderr.fd() {
697                cvt_nz(libc::posix_spawn_file_actions_adddup2(
698                    file_actions.0.as_mut_ptr(),
699                    fd,
700                    libc::STDERR_FILENO,
701                ))?;
702            }
703            if let Some((f, cwd)) = addchdir {
704                cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
705            }
706
707            if let Some(pgroup) = pgroup {
708                flags |= libc::POSIX_SPAWN_SETPGROUP;
709                cvt_nz(libc::posix_spawnattr_setpgroup(attrs.0.as_mut_ptr(), pgroup))?;
710            }
711
712            // Inherit the signal mask from this process rather than resetting it (i.e. do not call
713            // posix_spawnattr_setsigmask).
714
715            // If -Zon-broken-pipe is used, don't reset SIGPIPE to SIG_DFL.
716            // If -Zon-broken-pipe is not used, reset SIGPIPE to SIG_DFL for backward compatibility.
717            //
718            // -Zon-broken-pipe is an opportunity to change the default here.
719            if !on_broken_pipe_flag_used() {
720                let mut default_set = MaybeUninit::<libc::sigset_t>::uninit();
721                cvt(sigemptyset(default_set.as_mut_ptr()))?;
722                cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGPIPE))?;
723                #[cfg(target_os = "hurd")]
724                {
725                    cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGLOST))?;
726                }
727                cvt_nz(libc::posix_spawnattr_setsigdefault(
728                    attrs.0.as_mut_ptr(),
729                    default_set.as_ptr(),
730                ))?;
731                flags |= libc::POSIX_SPAWN_SETSIGDEF;
732            }
733
734            cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
735
736            // Make sure we synchronize access to the global `environ` resource
737            let _env_lock = sys::env::env_read_lock();
738            let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::env::environ() as *const _);
739
740            #[cfg(not(target_os = "nto"))]
741            let spawn_fn = libc::posix_spawnp;
742            #[cfg(target_os = "nto")]
743            let spawn_fn = retrying_libc_posix_spawnp;
744
745            #[cfg(target_os = "linux")]
746            if self.get_create_pidfd() && PIDFD_SUPPORTED.load(Ordering::Relaxed) == SPAWN {
747                let mut pidfd: libc::c_int = -1;
748                let spawn_res = pidfd_spawnp.get().unwrap()(
749                    &mut pidfd,
750                    self.get_program_cstr().as_ptr(),
751                    file_actions.0.as_ptr(),
752                    attrs.0.as_ptr(),
753                    self.get_argv().as_ptr() as *const _,
754                    envp as *const _,
755                );
756
757                let spawn_res = cvt_nz(spawn_res);
758                if let Err(ref e) = spawn_res
759                    && e.raw_os_error() == Some(libc::ENOSYS)
760                {
761                    PIDFD_SUPPORTED.store(FORK_EXEC, Ordering::Relaxed);
762                    return Ok(None);
763                }
764                spawn_res?;
765
766                let pid = match cvt(pidfd_getpid.get().unwrap()(pidfd)) {
767                    Ok(pid) => pid,
768                    Err(e) => {
769                        // The child has been spawned and we are holding its pidfd.
770                        // But we cannot obtain its pid even though pidfd_getpid support was verified earlier.
771                        // This might happen if libc can't open procfs because the file descriptor limit has been reached.
772                        libc::close(pidfd);
773                        return Err(Error::new(
774                            e.kind(),
775                            "pidfd_spawnp succeeded but the child's PID could not be obtained",
776                        ));
777                    }
778                };
779
780                return Ok(Some(Process::new(pid, pidfd)));
781            }
782
783            // Safety: -1 indicates we don't have a pidfd.
784            let mut p = Process::new(0, -1);
785
786            let spawn_res = spawn_fn(
787                &mut p.pid,
788                self.get_program_cstr().as_ptr(),
789                file_actions.0.as_ptr(),
790                attrs.0.as_ptr(),
791                self.get_argv().as_ptr() as *const _,
792                envp as *const _,
793            );
794
795            #[cfg(target_os = "nto")]
796            let spawn_res = spawn_res?;
797
798            cvt_nz(spawn_res)?;
799            Ok(Some(p))
800        }
801    }
802
803    #[cfg(target_os = "linux")]
804    fn send_pidfd(&self, sock: &crate::sys:🥅:Socket) {
805        use libc::{CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_SPACE, SCM_RIGHTS, SOL_SOCKET};
806
807        use crate::io::IoSlice;
808        use crate::os::fd::RawFd;
809        use crate::sys::cvt_r;
810
811        unsafe {
812            let child_pid = libc::getpid();
813            // pidfd_open sets CLOEXEC by default
814            let pidfd = libc::syscall(libc::SYS_pidfd_open, child_pid, 0);
815
816            let fds: [c_int; 1] = [pidfd as RawFd];
817
818            const SCM_MSG_LEN: usize = size_of::<[c_int; 1]>();
819
820            #[repr(C)]
821            union Cmsg {
822                buf: [u8; unsafe { CMSG_SPACE(SCM_MSG_LEN as u32) as usize }],
823                _align: libc::cmsghdr,
824            }
825
826            let mut cmsg: Cmsg = mem::zeroed();
827
828            // 0-length message to send through the socket so we can pass along the fd
829            let mut iov = [IoSlice::new(b"")];
830            let mut msg: libc::msghdr = mem::zeroed();
831
832            msg.msg_iov = (&raw mut iov) as *mut _;
833            msg.msg_iovlen = 1;
834
835            // only attach cmsg if we successfully acquired the pidfd
836            if pidfd >= 0 {
837                msg.msg_controllen = size_of_val(&cmsg.buf) as _;
838                msg.msg_control = (&raw mut cmsg.buf) as *mut _;
839
840                let hdr = CMSG_FIRSTHDR((&raw mut msg) as *mut _);
841                (*hdr).cmsg_level = SOL_SOCKET;
842                (*hdr).cmsg_type = SCM_RIGHTS;
843                (*hdr).cmsg_len = CMSG_LEN(SCM_MSG_LEN as _) as _;
844                let data = CMSG_DATA(hdr);
845                crate::ptr::copy_nonoverlapping(
846                    fds.as_ptr().cast::<u8>(),
847                    data as *mut _,
848                    SCM_MSG_LEN,
849                );
850            }
851
852            // we send the 0-length message even if we failed to acquire the pidfd
853            // so we get a consistent SEQPACKET order
854            match cvt_r(|| libc::sendmsg(sock.as_raw(), &msg, 0)) {
855                Ok(0) => {}
856                other => rtabort!("failed to communicate with parent process. {:?}", other),
857            }
858        }
859    }
860
861    #[cfg(target_os = "linux")]
862    fn recv_pidfd(&self, sock: &crate::sys:🥅:Socket) -> pid_t {
863        use libc::{CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_SPACE, SCM_RIGHTS, SOL_SOCKET};
864
865        use crate::io::IoSliceMut;
866        use crate::sys::cvt_r;
867
868        unsafe {
869            const SCM_MSG_LEN: usize = size_of::<[c_int; 1]>();
870
871            #[repr(C)]
872            union Cmsg {
873                _buf: [u8; unsafe { CMSG_SPACE(SCM_MSG_LEN as u32) as usize }],
874                _align: libc::cmsghdr,
875            }
876            let mut cmsg: Cmsg = mem::zeroed();
877            // 0-length read to get the fd
878            let mut iov = [IoSliceMut::new(&mut [])];
879
880            let mut msg: libc::msghdr = mem::zeroed();
881
882            msg.msg_iov = (&raw mut iov) as *mut _;
883            msg.msg_iovlen = 1;
884            msg.msg_controllen = size_of::<Cmsg>() as _;
885            msg.msg_control = (&raw mut cmsg) as *mut _;
886
887            match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, libc::MSG_CMSG_CLOEXEC)) {
888                Err(_) => return -1,
889                Ok(_) => {}
890            }
891
892            let hdr = CMSG_FIRSTHDR((&raw mut msg) as *mut _);
893            if hdr.is_null()
894                || (*hdr).cmsg_level != SOL_SOCKET
895                || (*hdr).cmsg_type != SCM_RIGHTS
896                || (*hdr).cmsg_len != CMSG_LEN(SCM_MSG_LEN as _) as _
897            {
898                return -1;
899            }
900            let data = CMSG_DATA(hdr);
901
902            let mut fds = [-1 as c_int];
903
904            crate::ptr::copy_nonoverlapping(
905                data as *const _,
906                fds.as_mut_ptr().cast::<u8>(),
907                SCM_MSG_LEN,
908            );
909
910            fds[0]
911        }
912    }
913}
914
915////////////////////////////////////////////////////////////////////////////////
916// Processes
917////////////////////////////////////////////////////////////////////////////////
918
919/// The unique ID of the process (this should never be negative).
920pub struct Process {
921    pid: pid_t,
922    status: Option<ExitStatus>,
923    // On Linux, stores the pidfd created for this child.
924    // This is None if the user did not request pidfd creation,
925    // or if the pidfd could not be created for some reason
926    // (e.g. the `pidfd_open` syscall was not available).
927    #[cfg(target_os = "linux")]
928    pidfd: Option<PidFd>,
929}
930
931impl Process {
932    #[cfg(target_os = "linux")]
933    /// # Safety
934    ///
935    /// `pidfd` must either be -1 (representing no file descriptor) or a valid, exclusively owned file
936    /// descriptor (See [I/O Safety]).
937    ///
938    /// [I/O Safety]: crate::io#io-safety
939    unsafe fn new(pid: pid_t, pidfd: pid_t) -> Self {
940        use crate::os::unix::io::FromRawFd;
941        use crate::sys_common::FromInner;
942        // Safety: If `pidfd` is nonnegative, we assume it's valid and otherwise unowned.
943        let pidfd = (pidfd >= 0).then(|| PidFd::from_inner(sys::fd::FileDesc::from_raw_fd(pidfd)));
944        Process { pid, status: None, pidfd }
945    }
946
947    #[cfg(not(target_os = "linux"))]
948    unsafe fn new(pid: pid_t, _pidfd: pid_t) -> Self {
949        Process { pid, status: None }
950    }
951
952    pub fn id(&self) -> u32 {
953        self.pid as u32
954    }
955
956    pub fn kill(&mut self) -> io::Result<()> {
957        // If we've already waited on this process then the pid can be recycled
958        // and used for another process, and we probably shouldn't be killing
959        // random processes, so return Ok because the process has exited already.
960        if self.status.is_some() {
961            return Ok(());
962        }
963        #[cfg(target_os = "linux")]
964        if let Some(pid_fd) = self.pidfd.as_ref() {
965            // pidfd_send_signal predates pidfd_open. so if we were able to get an fd then sending signals will work too
966            return pid_fd.kill();
967        }
968        cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
969    }
970
971    pub fn wait(&mut self) -> io::Result<ExitStatus> {
972        use crate::sys::cvt_r;
973        if let Some(status) = self.status {
974            return Ok(status);
975        }
976        #[cfg(target_os = "linux")]
977        if let Some(pid_fd) = self.pidfd.as_ref() {
978            let status = pid_fd.wait()?;
979            self.status = Some(status);
980            return Ok(status);
981        }
982        let mut status = 0 as c_int;
983        cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
984        self.status = Some(ExitStatus::new(status));
985        Ok(ExitStatus::new(status))
986    }
987
988    pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
989        if let Some(status) = self.status {
990            return Ok(Some(status));
991        }
992        #[cfg(target_os = "linux")]
993        if let Some(pid_fd) = self.pidfd.as_ref() {
994            let status = pid_fd.try_wait()?;
995            if let Some(status) = status {
996                self.status = Some(status)
997            }
998            return Ok(status);
999        }
1000        let mut status = 0 as c_int;
1001        let pid = cvt(unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) })?;
1002        if pid == 0 {
1003            Ok(None)
1004        } else {
1005            self.status = Some(ExitStatus::new(status));
1006            Ok(Some(ExitStatus::new(status)))
1007        }
1008    }
1009}
1010
1011/// Unix exit statuses
1012//
1013// This is not actually an "exit status" in Unix terminology.  Rather, it is a "wait status".
1014// See the discussion in comments and doc comments for `std::process::ExitStatus`.
1015#[derive(PartialEq, Eq, Clone, Copy, Default)]
1016pub struct ExitStatus(c_int);
1017
1018impl fmt::Debug for ExitStatus {
1019    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1020        f.debug_tuple("unix_wait_status").field(&self.0).finish()
1021    }
1022}
1023
1024impl ExitStatus {
1025    pub fn new(status: c_int) -> ExitStatus {
1026        ExitStatus(status)
1027    }
1028
1029    #[cfg(target_os = "linux")]
1030    pub fn from_waitid_siginfo(siginfo: libc::siginfo_t) -> ExitStatus {
1031        let status = unsafe { siginfo.si_status() };
1032
1033        match siginfo.si_code {
1034            libc::CLD_EXITED => ExitStatus((status & 0xff) << 8),
1035            libc::CLD_KILLED => ExitStatus(status),
1036            libc::CLD_DUMPED => ExitStatus(status | 0x80),
1037            libc::CLD_CONTINUED => ExitStatus(0xffff),
1038            libc::CLD_STOPPED | libc::CLD_TRAPPED => ExitStatus(((status & 0xff) << 8) | 0x7f),
1039            _ => unreachable!("waitid() should only return the above codes"),
1040        }
1041    }
1042
1043    fn exited(&self) -> bool {
1044        libc::WIFEXITED(self.0)
1045    }
1046
1047    pub fn exit_ok(&self) -> Result<(), ExitStatusError> {
1048        // This assumes that WIFEXITED(status) && WEXITSTATUS==0 corresponds to status==0. This is
1049        // true on all actual versions of Unix, is widely assumed, and is specified in SuS
1050        // https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html. If it is not
1051        // true for a platform pretending to be Unix, the tests (our doctests, and also
1052        // unix/tests.rs) will spot it. `ExitStatusError::code` assumes this too.
1053        match NonZero::try_from(self.0) {
1054            /* was nonzero */ Ok(failure) => Err(ExitStatusError(failure)),
1055            /* was zero, couldn't convert */ Err(_) => Ok(()),
1056        }
1057    }
1058
1059    pub fn code(&self) -> Option<i32> {
1060        self.exited().then(|| libc::WEXITSTATUS(self.0))
1061    }
1062
1063    pub fn signal(&self) -> Option<i32> {
1064        libc::WIFSIGNALED(self.0).then(|| libc::WTERMSIG(self.0))
1065    }
1066
1067    pub fn core_dumped(&self) -> bool {
1068        libc::WIFSIGNALED(self.0) && libc::WCOREDUMP(self.0)
1069    }
1070
1071    pub fn stopped_signal(&self) -> Option<i32> {
1072        libc::WIFSTOPPED(self.0).then(|| libc::WSTOPSIG(self.0))
1073    }
1074
1075    pub fn continued(&self) -> bool {
1076        libc::WIFCONTINUED(self.0)
1077    }
1078
1079    pub fn into_raw(&self) -> c_int {
1080        self.0
1081    }
1082}
1083
1084/// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it without copying.
1085impl From<c_int> for ExitStatus {
1086    fn from(a: c_int) -> ExitStatus {
1087        ExitStatus(a)
1088    }
1089}
1090
1091/// Converts a signal number to a readable, searchable name.
1092///
1093/// This string should be displayed right after the signal number.
1094/// If a signal is unrecognized, it returns the empty string, so that
1095/// you just get the number like "0". If it is recognized, you'll get
1096/// something like "9 (SIGKILL)".
1097fn signal_string(signal: i32) -> &'static str {
1098    match signal {
1099        libc::SIGHUP => " (SIGHUP)",
1100        libc::SIGINT => " (SIGINT)",
1101        libc::SIGQUIT => " (SIGQUIT)",
1102        libc::SIGILL => " (SIGILL)",
1103        libc::SIGTRAP => " (SIGTRAP)",
1104        libc::SIGABRT => " (SIGABRT)",
1105        #[cfg(not(target_os = "l4re"))]
1106        libc::SIGBUS => " (SIGBUS)",
1107        libc::SIGFPE => " (SIGFPE)",
1108        libc::SIGKILL => " (SIGKILL)",
1109        #[cfg(not(target_os = "l4re"))]
1110        libc::SIGUSR1 => " (SIGUSR1)",
1111        libc::SIGSEGV => " (SIGSEGV)",
1112        #[cfg(not(target_os = "l4re"))]
1113        libc::SIGUSR2 => " (SIGUSR2)",
1114        libc::SIGPIPE => " (SIGPIPE)",
1115        libc::SIGALRM => " (SIGALRM)",
1116        libc::SIGTERM => " (SIGTERM)",
1117        #[cfg(not(target_os = "l4re"))]
1118        libc::SIGCHLD => " (SIGCHLD)",
1119        #[cfg(not(target_os = "l4re"))]
1120        libc::SIGCONT => " (SIGCONT)",
1121        #[cfg(not(target_os = "l4re"))]
1122        libc::SIGSTOP => " (SIGSTOP)",
1123        #[cfg(not(target_os = "l4re"))]
1124        libc::SIGTSTP => " (SIGTSTP)",
1125        #[cfg(not(target_os = "l4re"))]
1126        libc::SIGTTIN => " (SIGTTIN)",
1127        #[cfg(not(target_os = "l4re"))]
1128        libc::SIGTTOU => " (SIGTTOU)",
1129        #[cfg(not(target_os = "l4re"))]
1130        libc::SIGURG => " (SIGURG)",
1131        #[cfg(not(target_os = "l4re"))]
1132        libc::SIGXCPU => " (SIGXCPU)",
1133        #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1134        libc::SIGXFSZ => " (SIGXFSZ)",
1135        #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1136        libc::SIGVTALRM => " (SIGVTALRM)",
1137        #[cfg(not(target_os = "l4re"))]
1138        libc::SIGPROF => " (SIGPROF)",
1139        #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1140        libc::SIGWINCH => " (SIGWINCH)",
1141        #[cfg(not(any(target_os = "haiku", target_os = "l4re")))]
1142        libc::SIGIO => " (SIGIO)",
1143        #[cfg(target_os = "haiku")]
1144        libc::SIGPOLL => " (SIGPOLL)",
1145        #[cfg(not(target_os = "l4re"))]
1146        libc::SIGSYS => " (SIGSYS)",
1147        // For information on Linux signals, run `man 7 signal`
1148        #[cfg(all(
1149            target_os = "linux",
1150            any(
1151                target_arch = "x86_64",
1152                target_arch = "x86",
1153                target_arch = "arm",
1154                target_arch = "aarch64"
1155            )
1156        ))]
1157        libc::SIGSTKFLT => " (SIGSTKFLT)",
1158        #[cfg(any(target_os = "linux", target_os = "nto", target_os = "cygwin"))]
1159        libc::SIGPWR => " (SIGPWR)",
1160        #[cfg(any(
1161            target_os = "freebsd",
1162            target_os = "netbsd",
1163            target_os = "openbsd",
1164            target_os = "dragonfly",
1165            target_os = "nto",
1166            target_vendor = "apple",
1167            target_os = "cygwin",
1168        ))]
1169        libc::SIGEMT => " (SIGEMT)",
1170        #[cfg(any(
1171            target_os = "freebsd",
1172            target_os = "netbsd",
1173            target_os = "openbsd",
1174            target_os = "dragonfly",
1175            target_vendor = "apple",
1176        ))]
1177        libc::SIGINFO => " (SIGINFO)",
1178        #[cfg(target_os = "hurd")]
1179        libc::SIGLOST => " (SIGLOST)",
1180        #[cfg(target_os = "freebsd")]
1181        libc::SIGTHR => " (SIGTHR)",
1182        #[cfg(target_os = "freebsd")]
1183        libc::SIGLIBRT => " (SIGLIBRT)",
1184        _ => "",
1185    }
1186}
1187
1188impl fmt::Display for ExitStatus {
1189    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1190        if let Some(code) = self.code() {
1191            write!(f, "exit status: {code}")
1192        } else if let Some(signal) = self.signal() {
1193            let signal_string = signal_string(signal);
1194            if self.core_dumped() {
1195                write!(f, "signal: {signal}{signal_string} (core dumped)")
1196            } else {
1197                write!(f, "signal: {signal}{signal_string}")
1198            }
1199        } else if let Some(signal) = self.stopped_signal() {
1200            let signal_string = signal_string(signal);
1201            write!(f, "stopped (not terminated) by signal: {signal}{signal_string}")
1202        } else if self.continued() {
1203            write!(f, "continued (WIFCONTINUED)")
1204        } else {
1205            write!(f, "unrecognised wait status: {} {:#x}", self.0, self.0)
1206        }
1207    }
1208}
1209
1210#[derive(PartialEq, Eq, Clone, Copy)]
1211pub struct ExitStatusError(NonZero<c_int>);
1212
1213impl Into<ExitStatus> for ExitStatusError {
1214    fn into(self) -> ExitStatus {
1215        ExitStatus(self.0.into())
1216    }
1217}
1218
1219impl fmt::Debug for ExitStatusError {
1220    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1221        f.debug_tuple("unix_wait_status").field(&self.0).finish()
1222    }
1223}
1224
1225impl ExitStatusError {
1226    pub fn code(self) -> Option<NonZero<i32>> {
1227        ExitStatus(self.0.into()).code().map(|st| st.try_into().unwrap())
1228    }
1229}
1230
1231#[cfg(target_os = "linux")]
1232mod linux_child_ext {
1233    use crate::io::ErrorKind;
1234    use crate::os::linux::process as os;
1235    use crate::sys::pal::linux::pidfd as imp;
1236    use crate::sys_common::FromInner;
1237    use crate::{io, mem};
1238
1239    #[unstable(feature = "linux_pidfd", issue = "82971")]
1240    impl crate::os::linux::process::ChildExt for crate::process::Child {
1241        fn pidfd(&self) -> io::Result<&os::PidFd> {
1242            self.handle
1243                .pidfd
1244                .as_ref()
1245                // SAFETY: The os type is a transparent wrapper, therefore we can transmute references
1246                .map(|fd| unsafe { mem::transmute::<&imp::PidFd, &os::PidFd>(fd) })
1247                .ok_or_else(|| io::const_error!(ErrorKind::Uncategorized, "no pidfd was created."))
1248        }
1249
1250        fn into_pidfd(mut self) -> Result<os::PidFd, Self> {
1251            self.handle
1252                .pidfd
1253                .take()
1254                .map(|fd| <os::PidFd as FromInner<imp::PidFd>>::from_inner(fd))
1255                .ok_or_else(|| self)
1256        }
1257    }
1258}
1259
1260#[cfg(test)]
1261mod tests;
1262
1263// See [`unsupported_wait_status::compare_with_linux`];
1264#[cfg(all(test, target_os = "linux"))]
1265#[path = "unsupported/wait_status.rs"]
1266mod unsupported_wait_status;