]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/process/process_unix.rs
Rollup merge of #93613 - crlf0710:rename_to_async_iter, r=yaahc
[rust.git] / library / std / src / sys / unix / process / process_unix.rs
1 use crate::convert::{TryFrom, TryInto};
2 use crate::fmt;
3 use crate::io::{self, Error, ErrorKind};
4 use crate::mem;
5 use crate::num::NonZeroI32;
6 use crate::os::raw::NonZero_c_int;
7 use crate::ptr;
8 use crate::sys;
9 use crate::sys::cvt;
10 use crate::sys::process::process_common::*;
11
12 #[cfg(target_os = "linux")]
13 use crate::os::linux::process::PidFd;
14
15 #[cfg(target_os = "linux")]
16 use crate::sys::weak::raw_syscall;
17
18 #[cfg(any(
19     target_os = "macos",
20     target_os = "freebsd",
21     all(target_os = "linux", target_env = "gnu"),
22     all(target_os = "linux", target_env = "musl"),
23 ))]
24 use crate::sys::weak::weak;
25
26 #[cfg(target_os = "vxworks")]
27 use libc::RTP_ID as pid_t;
28
29 #[cfg(not(target_os = "vxworks"))]
30 use libc::{c_int, gid_t, pid_t, uid_t};
31
32 ////////////////////////////////////////////////////////////////////////////////
33 // Command
34 ////////////////////////////////////////////////////////////////////////////////
35
36 impl Command {
37     pub fn spawn(
38         &mut self,
39         default: Stdio,
40         needs_stdin: bool,
41     ) -> io::Result<(Process, StdioPipes)> {
42         const CLOEXEC_MSG_FOOTER: [u8; 4] = *b"NOEX";
43
44         let envp = self.capture_env();
45
46         if self.saw_nul() {
47             return Err(io::const_io_error!(
48                 ErrorKind::InvalidInput,
49                 "nul byte found in provided data",
50             ));
51         }
52
53         let (ours, theirs) = self.setup_io(default, needs_stdin)?;
54
55         if let Some(ret) = self.posix_spawn(&theirs, envp.as_ref())? {
56             return Ok((ret, ours));
57         }
58
59         let (input, output) = sys::pipe::anon_pipe()?;
60
61         // Whatever happens after the fork is almost for sure going to touch or
62         // look at the environment in one way or another (PATH in `execvp` or
63         // accessing the `environ` pointer ourselves). Make sure no other thread
64         // is accessing the environment when we do the fork itself.
65         //
66         // Note that as soon as we're done with the fork there's no need to hold
67         // a lock any more because the parent won't do anything and the child is
68         // in its own process. Thus the parent drops the lock guard while the child
69         // forgets it to avoid unlocking it on a new thread, which would be invalid.
70         let env_lock = sys::os::env_read_lock();
71         let (pid, pidfd) = unsafe { self.do_fork()? };
72
73         if pid == 0 {
74             crate::panic::always_abort();
75             mem::forget(env_lock);
76             drop(input);
77             let Err(err) = unsafe { self.do_exec(theirs, envp.as_ref()) };
78             let errno = err.raw_os_error().unwrap_or(libc::EINVAL) as u32;
79             let errno = errno.to_be_bytes();
80             let bytes = [
81                 errno[0],
82                 errno[1],
83                 errno[2],
84                 errno[3],
85                 CLOEXEC_MSG_FOOTER[0],
86                 CLOEXEC_MSG_FOOTER[1],
87                 CLOEXEC_MSG_FOOTER[2],
88                 CLOEXEC_MSG_FOOTER[3],
89             ];
90             // pipe I/O up to PIPE_BUF bytes should be atomic, and then
91             // we want to be sure we *don't* run at_exit destructors as
92             // we're being torn down regardless
93             rtassert!(output.write(&bytes).is_ok());
94             unsafe { libc::_exit(1) }
95         }
96
97         drop(env_lock);
98         drop(output);
99
100         // Safety: We obtained the pidfd from calling `clone3` with
101         // `CLONE_PIDFD` so it's valid an otherwise unowned.
102         let mut p = unsafe { Process::new(pid, pidfd) };
103         let mut bytes = [0; 8];
104
105         // loop to handle EINTR
106         loop {
107             match input.read(&mut bytes) {
108                 Ok(0) => return Ok((p, ours)),
109                 Ok(8) => {
110                     let (errno, footer) = bytes.split_at(4);
111                     assert_eq!(
112                         CLOEXEC_MSG_FOOTER, footer,
113                         "Validation on the CLOEXEC pipe failed: {:?}",
114                         bytes
115                     );
116                     let errno = i32::from_be_bytes(errno.try_into().unwrap());
117                     assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
118                     return Err(Error::from_raw_os_error(errno));
119                 }
120                 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
121                 Err(e) => {
122                     assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
123                     panic!("the CLOEXEC pipe failed: {:?}", e)
124                 }
125                 Ok(..) => {
126                     // pipe I/O up to PIPE_BUF bytes should be atomic
127                     assert!(p.wait().is_ok(), "wait() should either return Ok or panic");
128                     panic!("short read on the CLOEXEC pipe")
129                 }
130             }
131         }
132     }
133
134     // Attempts to fork the process. If successful, returns Ok((0, -1))
135     // in the child, and Ok((child_pid, -1)) in the parent.
136     #[cfg(not(target_os = "linux"))]
137     unsafe fn do_fork(&mut self) -> Result<(pid_t, pid_t), io::Error> {
138         cvt(libc::fork()).map(|res| (res, -1))
139     }
140
141     // Attempts to fork the process. If successful, returns Ok((0, -1))
142     // in the child, and Ok((child_pid, child_pidfd)) in the parent.
143     #[cfg(target_os = "linux")]
144     unsafe fn do_fork(&mut self) -> Result<(pid_t, pid_t), io::Error> {
145         use crate::sync::atomic::{AtomicBool, Ordering};
146
147         static HAS_CLONE3: AtomicBool = AtomicBool::new(true);
148         const CLONE_PIDFD: u64 = 0x00001000;
149
150         #[repr(C)]
151         struct clone_args {
152             flags: u64,
153             pidfd: u64,
154             child_tid: u64,
155             parent_tid: u64,
156             exit_signal: u64,
157             stack: u64,
158             stack_size: u64,
159             tls: u64,
160             set_tid: u64,
161             set_tid_size: u64,
162             cgroup: u64,
163         }
164
165         raw_syscall! {
166             fn clone3(cl_args: *mut clone_args, len: libc::size_t) -> libc::c_long
167         }
168
169         // Bypassing libc for `clone3` can make further libc calls unsafe,
170         // so we use it sparingly for now. See #89522 for details.
171         // Some tools (e.g. sandboxing tools) may also expect `fork`
172         // rather than `clone3`.
173         let want_clone3_pidfd = self.get_create_pidfd();
174
175         // If we fail to create a pidfd for any reason, this will
176         // stay as -1, which indicates an error.
177         let mut pidfd: pid_t = -1;
178
179         // Attempt to use the `clone3` syscall, which supports more arguments
180         // (in particular, the ability to create a pidfd). If this fails,
181         // we will fall through this block to a call to `fork()`
182         if want_clone3_pidfd && HAS_CLONE3.load(Ordering::Relaxed) {
183             let mut args = clone_args {
184                 flags: CLONE_PIDFD,
185                 pidfd: &mut pidfd as *mut pid_t as u64,
186                 child_tid: 0,
187                 parent_tid: 0,
188                 exit_signal: libc::SIGCHLD as u64,
189                 stack: 0,
190                 stack_size: 0,
191                 tls: 0,
192                 set_tid: 0,
193                 set_tid_size: 0,
194                 cgroup: 0,
195             };
196
197             let args_ptr = &mut args as *mut clone_args;
198             let args_size = crate::mem::size_of::<clone_args>();
199
200             let res = cvt(clone3(args_ptr, args_size));
201             match res {
202                 Ok(n) => return Ok((n as pid_t, pidfd)),
203                 Err(e) => match e.raw_os_error() {
204                     // Multiple threads can race to execute this store,
205                     // but that's fine - that just means that multiple threads
206                     // will have tried and failed to execute the same syscall,
207                     // with no other side effects.
208                     Some(libc::ENOSYS) => HAS_CLONE3.store(false, Ordering::Relaxed),
209                     // Fallback to fork if `EPERM` is returned. (e.g. blocked by seccomp)
210                     Some(libc::EPERM) => {}
211                     _ => return Err(e),
212                 },
213             }
214         }
215
216         // Generally, we just call `fork`. If we get here after wanting `clone3`,
217         // then the syscall does not exist or we do not have permission to call it.
218         cvt(libc::fork()).map(|res| (res, pidfd))
219     }
220
221     pub fn exec(&mut self, default: Stdio) -> io::Error {
222         let envp = self.capture_env();
223
224         if self.saw_nul() {
225             return io::const_io_error!(ErrorKind::InvalidInput, "nul byte found in provided data",);
226         }
227
228         match self.setup_io(default, true) {
229             Ok((_, theirs)) => {
230                 unsafe {
231                     // Similar to when forking, we want to ensure that access to
232                     // the environment is synchronized, so make sure to grab the
233                     // environment lock before we try to exec.
234                     let _lock = sys::os::env_read_lock();
235
236                     let Err(e) = self.do_exec(theirs, envp.as_ref());
237                     e
238                 }
239             }
240             Err(e) => e,
241         }
242     }
243
244     // And at this point we've reached a special time in the life of the
245     // child. The child must now be considered hamstrung and unable to
246     // do anything other than syscalls really. Consider the following
247     // scenario:
248     //
249     //      1. Thread A of process 1 grabs the malloc() mutex
250     //      2. Thread B of process 1 forks(), creating thread C
251     //      3. Thread C of process 2 then attempts to malloc()
252     //      4. The memory of process 2 is the same as the memory of
253     //         process 1, so the mutex is locked.
254     //
255     // This situation looks a lot like deadlock, right? It turns out
256     // that this is what pthread_atfork() takes care of, which is
257     // presumably implemented across platforms. The first thing that
258     // threads to *before* forking is to do things like grab the malloc
259     // mutex, and then after the fork they unlock it.
260     //
261     // Despite this information, libnative's spawn has been witnessed to
262     // deadlock on both macOS and FreeBSD. I'm not entirely sure why, but
263     // all collected backtraces point at malloc/free traffic in the
264     // child spawned process.
265     //
266     // For this reason, the block of code below should contain 0
267     // invocations of either malloc of free (or their related friends).
268     //
269     // As an example of not having malloc/free traffic, we don't close
270     // this file descriptor by dropping the FileDesc (which contains an
271     // allocation). Instead we just close it manually. This will never
272     // have the drop glue anyway because this code never returns (the
273     // child will either exec() or invoke libc::exit)
274     unsafe fn do_exec(
275         &mut self,
276         stdio: ChildPipes,
277         maybe_envp: Option<&CStringArray>,
278     ) -> Result<!, io::Error> {
279         use crate::sys::{self, cvt_r};
280
281         if let Some(fd) = stdio.stdin.fd() {
282             cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?;
283         }
284         if let Some(fd) = stdio.stdout.fd() {
285             cvt_r(|| libc::dup2(fd, libc::STDOUT_FILENO))?;
286         }
287         if let Some(fd) = stdio.stderr.fd() {
288             cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?;
289         }
290
291         #[cfg(not(target_os = "l4re"))]
292         {
293             if let Some(_g) = self.get_groups() {
294                 //FIXME: Redox kernel does not support setgroups yet
295                 #[cfg(not(target_os = "redox"))]
296                 cvt(libc::setgroups(_g.len().try_into().unwrap(), _g.as_ptr()))?;
297             }
298             if let Some(u) = self.get_gid() {
299                 cvt(libc::setgid(u as gid_t))?;
300             }
301             if let Some(u) = self.get_uid() {
302                 // When dropping privileges from root, the `setgroups` call
303                 // will remove any extraneous groups. We only drop groups
304                 // if the current uid is 0 and we weren't given an explicit
305                 // set of groups. If we don't call this, then even though our
306                 // uid has dropped, we may still have groups that enable us to
307                 // do super-user things.
308                 //FIXME: Redox kernel does not support setgroups yet
309                 #[cfg(not(target_os = "redox"))]
310                 if libc::getuid() == 0 && self.get_groups().is_none() {
311                     cvt(libc::setgroups(0, ptr::null()))?;
312                 }
313                 cvt(libc::setuid(u as uid_t))?;
314             }
315         }
316         if let Some(ref cwd) = *self.get_cwd() {
317             cvt(libc::chdir(cwd.as_ptr()))?;
318         }
319
320         // emscripten has no signal support.
321         #[cfg(not(target_os = "emscripten"))]
322         {
323             use crate::mem::MaybeUninit;
324             // Reset signal handling so the child process starts in a
325             // standardized state. libstd ignores SIGPIPE, and signal-handling
326             // libraries often set a mask. Child processes inherit ignored
327             // signals and the signal mask from their parent, but most
328             // UNIX programs do not reset these things on their own, so we
329             // need to clean things up now to avoid confusing the program
330             // we're about to run.
331             let mut set = MaybeUninit::<libc::sigset_t>::uninit();
332             cvt(sigemptyset(set.as_mut_ptr()))?;
333             cvt(libc::pthread_sigmask(libc::SIG_SETMASK, set.as_ptr(), ptr::null_mut()))?;
334
335             #[cfg(target_os = "android")] // see issue #88585
336             {
337                 let mut action: libc::sigaction = mem::zeroed();
338                 action.sa_sigaction = libc::SIG_DFL;
339                 cvt(libc::sigaction(libc::SIGPIPE, &action, ptr::null_mut()))?;
340             }
341             #[cfg(not(target_os = "android"))]
342             {
343                 let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL);
344                 if ret == libc::SIG_ERR {
345                     return Err(io::Error::last_os_error());
346                 }
347             }
348         }
349
350         for callback in self.get_closures().iter_mut() {
351             callback()?;
352         }
353
354         // Although we're performing an exec here we may also return with an
355         // error from this function (without actually exec'ing) in which case we
356         // want to be sure to restore the global environment back to what it
357         // once was, ensuring that our temporary override, when free'd, doesn't
358         // corrupt our process's environment.
359         let mut _reset = None;
360         if let Some(envp) = maybe_envp {
361             struct Reset(*const *const libc::c_char);
362
363             impl Drop for Reset {
364                 fn drop(&mut self) {
365                     unsafe {
366                         *sys::os::environ() = self.0;
367                     }
368                 }
369             }
370
371             _reset = Some(Reset(*sys::os::environ()));
372             *sys::os::environ() = envp.as_ptr();
373         }
374
375         libc::execvp(self.get_program_cstr().as_ptr(), self.get_argv().as_ptr());
376         Err(io::Error::last_os_error())
377     }
378
379     #[cfg(not(any(
380         target_os = "macos",
381         target_os = "freebsd",
382         all(target_os = "linux", target_env = "gnu"),
383         all(target_os = "linux", target_env = "musl"),
384     )))]
385     fn posix_spawn(
386         &mut self,
387         _: &ChildPipes,
388         _: Option<&CStringArray>,
389     ) -> io::Result<Option<Process>> {
390         Ok(None)
391     }
392
393     // Only support platforms for which posix_spawn() can return ENOENT
394     // directly.
395     #[cfg(any(
396         target_os = "macos",
397         target_os = "freebsd",
398         all(target_os = "linux", target_env = "gnu"),
399         all(target_os = "linux", target_env = "musl"),
400     ))]
401     fn posix_spawn(
402         &mut self,
403         stdio: &ChildPipes,
404         envp: Option<&CStringArray>,
405     ) -> io::Result<Option<Process>> {
406         use crate::mem::MaybeUninit;
407         use crate::sys::{self, cvt_nz};
408
409         if self.get_gid().is_some()
410             || self.get_uid().is_some()
411             || (self.env_saw_path() && !self.program_is_path())
412             || !self.get_closures().is_empty()
413             || self.get_groups().is_some()
414             || self.get_create_pidfd()
415         {
416             return Ok(None);
417         }
418
419         // Only glibc 2.24+ posix_spawn() supports returning ENOENT directly.
420         #[cfg(all(target_os = "linux", target_env = "gnu"))]
421         {
422             if let Some(version) = sys::os::glibc_version() {
423                 if version < (2, 24) {
424                     return Ok(None);
425                 }
426             } else {
427                 return Ok(None);
428             }
429         }
430
431         // Solaris, glibc 2.29+, and musl 1.24+ can set a new working directory,
432         // and maybe others will gain this non-POSIX function too. We'll check
433         // for this weak symbol as soon as it's needed, so we can return early
434         // otherwise to do a manual chdir before exec.
435         weak! {
436             fn posix_spawn_file_actions_addchdir_np(
437                 *mut libc::posix_spawn_file_actions_t,
438                 *const libc::c_char
439             ) -> libc::c_int
440         }
441         let addchdir = match self.get_cwd() {
442             Some(cwd) => {
443                 if cfg!(target_os = "macos") {
444                     // There is a bug in macOS where a relative executable
445                     // path like "../myprogram" will cause `posix_spawn` to
446                     // successfully launch the program, but erroneously return
447                     // ENOENT when used with posix_spawn_file_actions_addchdir_np
448                     // which was introduced in macOS 10.15.
449                     return Ok(None);
450                 }
451                 match posix_spawn_file_actions_addchdir_np.get() {
452                     Some(f) => Some((f, cwd)),
453                     None => return Ok(None),
454                 }
455             }
456             None => None,
457         };
458
459         // Safety: -1 indicates we don't have a pidfd.
460         let mut p = unsafe { Process::new(0, -1) };
461
462         struct PosixSpawnFileActions<'a>(&'a mut MaybeUninit<libc::posix_spawn_file_actions_t>);
463
464         impl Drop for PosixSpawnFileActions<'_> {
465             fn drop(&mut self) {
466                 unsafe {
467                     libc::posix_spawn_file_actions_destroy(self.0.as_mut_ptr());
468                 }
469             }
470         }
471
472         struct PosixSpawnattr<'a>(&'a mut MaybeUninit<libc::posix_spawnattr_t>);
473
474         impl Drop for PosixSpawnattr<'_> {
475             fn drop(&mut self) {
476                 unsafe {
477                     libc::posix_spawnattr_destroy(self.0.as_mut_ptr());
478                 }
479             }
480         }
481
482         unsafe {
483             let mut attrs = MaybeUninit::uninit();
484             cvt_nz(libc::posix_spawnattr_init(attrs.as_mut_ptr()))?;
485             let attrs = PosixSpawnattr(&mut attrs);
486
487             let mut file_actions = MaybeUninit::uninit();
488             cvt_nz(libc::posix_spawn_file_actions_init(file_actions.as_mut_ptr()))?;
489             let file_actions = PosixSpawnFileActions(&mut file_actions);
490
491             if let Some(fd) = stdio.stdin.fd() {
492                 cvt_nz(libc::posix_spawn_file_actions_adddup2(
493                     file_actions.0.as_mut_ptr(),
494                     fd,
495                     libc::STDIN_FILENO,
496                 ))?;
497             }
498             if let Some(fd) = stdio.stdout.fd() {
499                 cvt_nz(libc::posix_spawn_file_actions_adddup2(
500                     file_actions.0.as_mut_ptr(),
501                     fd,
502                     libc::STDOUT_FILENO,
503                 ))?;
504             }
505             if let Some(fd) = stdio.stderr.fd() {
506                 cvt_nz(libc::posix_spawn_file_actions_adddup2(
507                     file_actions.0.as_mut_ptr(),
508                     fd,
509                     libc::STDERR_FILENO,
510                 ))?;
511             }
512             if let Some((f, cwd)) = addchdir {
513                 cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
514             }
515
516             let mut set = MaybeUninit::<libc::sigset_t>::uninit();
517             cvt(sigemptyset(set.as_mut_ptr()))?;
518             cvt_nz(libc::posix_spawnattr_setsigmask(attrs.0.as_mut_ptr(), set.as_ptr()))?;
519             cvt(sigaddset(set.as_mut_ptr(), libc::SIGPIPE))?;
520             cvt_nz(libc::posix_spawnattr_setsigdefault(attrs.0.as_mut_ptr(), set.as_ptr()))?;
521
522             let flags = libc::POSIX_SPAWN_SETSIGDEF | libc::POSIX_SPAWN_SETSIGMASK;
523             cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
524
525             // Make sure we synchronize access to the global `environ` resource
526             let _env_lock = sys::os::env_read_lock();
527             let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::os::environ() as *const _);
528             cvt_nz(libc::posix_spawnp(
529                 &mut p.pid,
530                 self.get_program_cstr().as_ptr(),
531                 file_actions.0.as_ptr(),
532                 attrs.0.as_ptr(),
533                 self.get_argv().as_ptr() as *const _,
534                 envp as *const _,
535             ))?;
536             Ok(Some(p))
537         }
538     }
539 }
540
541 ////////////////////////////////////////////////////////////////////////////////
542 // Processes
543 ////////////////////////////////////////////////////////////////////////////////
544
545 /// The unique ID of the process (this should never be negative).
546 pub struct Process {
547     pid: pid_t,
548     status: Option<ExitStatus>,
549     // On Linux, stores the pidfd created for this child.
550     // This is None if the user did not request pidfd creation,
551     // or if the pidfd could not be created for some reason
552     // (e.g. the `clone3` syscall was not available).
553     #[cfg(target_os = "linux")]
554     pidfd: Option<PidFd>,
555 }
556
557 impl Process {
558     #[cfg(target_os = "linux")]
559     unsafe fn new(pid: pid_t, pidfd: pid_t) -> Self {
560         use crate::os::unix::io::FromRawFd;
561         use crate::sys_common::FromInner;
562         // Safety: If `pidfd` is nonnegative, we assume it's valid and otherwise unowned.
563         let pidfd = (pidfd >= 0).then(|| PidFd::from_inner(sys::fd::FileDesc::from_raw_fd(pidfd)));
564         Process { pid, status: None, pidfd }
565     }
566
567     #[cfg(not(target_os = "linux"))]
568     unsafe fn new(pid: pid_t, _pidfd: pid_t) -> Self {
569         Process { pid, status: None }
570     }
571
572     pub fn id(&self) -> u32 {
573         self.pid as u32
574     }
575
576     pub fn kill(&mut self) -> io::Result<()> {
577         // If we've already waited on this process then the pid can be recycled
578         // and used for another process, and we probably shouldn't be killing
579         // random processes, so just return an error.
580         if self.status.is_some() {
581             Err(io::const_io_error!(
582                 ErrorKind::InvalidInput,
583                 "invalid argument: can't kill an exited process",
584             ))
585         } else {
586             cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
587         }
588     }
589
590     pub fn wait(&mut self) -> io::Result<ExitStatus> {
591         use crate::sys::cvt_r;
592         if let Some(status) = self.status {
593             return Ok(status);
594         }
595         let mut status = 0 as c_int;
596         cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
597         self.status = Some(ExitStatus::new(status));
598         Ok(ExitStatus::new(status))
599     }
600
601     pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
602         if let Some(status) = self.status {
603             return Ok(Some(status));
604         }
605         let mut status = 0 as c_int;
606         let pid = cvt(unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) })?;
607         if pid == 0 {
608             Ok(None)
609         } else {
610             self.status = Some(ExitStatus::new(status));
611             Ok(Some(ExitStatus::new(status)))
612         }
613     }
614 }
615
616 /// Unix exit statuses
617 //
618 // This is not actually an "exit status" in Unix terminology.  Rather, it is a "wait status".
619 // See the discussion in comments and doc comments for `std::process::ExitStatus`.
620 #[derive(PartialEq, Eq, Clone, Copy)]
621 pub struct ExitStatus(c_int);
622
623 impl fmt::Debug for ExitStatus {
624     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
625         f.debug_tuple("unix_wait_status").field(&self.0).finish()
626     }
627 }
628
629 impl ExitStatus {
630     pub fn new(status: c_int) -> ExitStatus {
631         ExitStatus(status)
632     }
633
634     fn exited(&self) -> bool {
635         libc::WIFEXITED(self.0)
636     }
637
638     pub fn exit_ok(&self) -> Result<(), ExitStatusError> {
639         // This assumes that WIFEXITED(status) && WEXITSTATUS==0 corresponds to status==0.  This is
640         // true on all actual versions of Unix, is widely assumed, and is specified in SuS
641         // https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html .  If it is not
642         // true for a platform pretending to be Unix, the tests (our doctests, and also
643         // procsss_unix/tests.rs) will spot it.  `ExitStatusError::code` assumes this too.
644         match NonZero_c_int::try_from(self.0) {
645             /* was nonzero */ Ok(failure) => Err(ExitStatusError(failure)),
646             /* was zero, couldn't convert */ Err(_) => Ok(()),
647         }
648     }
649
650     pub fn code(&self) -> Option<i32> {
651         if self.exited() { Some(libc::WEXITSTATUS(self.0)) } else { None }
652     }
653
654     pub fn signal(&self) -> Option<i32> {
655         if libc::WIFSIGNALED(self.0) { Some(libc::WTERMSIG(self.0)) } else { None }
656     }
657
658     pub fn core_dumped(&self) -> bool {
659         libc::WIFSIGNALED(self.0) && libc::WCOREDUMP(self.0)
660     }
661
662     pub fn stopped_signal(&self) -> Option<i32> {
663         if libc::WIFSTOPPED(self.0) { Some(libc::WSTOPSIG(self.0)) } else { None }
664     }
665
666     pub fn continued(&self) -> bool {
667         libc::WIFCONTINUED(self.0)
668     }
669
670     pub fn into_raw(&self) -> c_int {
671         self.0
672     }
673 }
674
675 /// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it without copying.
676 impl From<c_int> for ExitStatus {
677     fn from(a: c_int) -> ExitStatus {
678         ExitStatus(a)
679     }
680 }
681
682 impl fmt::Display for ExitStatus {
683     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
684         if let Some(code) = self.code() {
685             write!(f, "exit status: {}", code)
686         } else if let Some(signal) = self.signal() {
687             if self.core_dumped() {
688                 write!(f, "signal: {} (core dumped)", signal)
689             } else {
690                 write!(f, "signal: {}", signal)
691             }
692         } else if let Some(signal) = self.stopped_signal() {
693             write!(f, "stopped (not terminated) by signal: {}", signal)
694         } else if self.continued() {
695             write!(f, "continued (WIFCONTINUED)")
696         } else {
697             write!(f, "unrecognised wait status: {} {:#x}", self.0, self.0)
698         }
699     }
700 }
701
702 #[derive(PartialEq, Eq, Clone, Copy)]
703 pub struct ExitStatusError(NonZero_c_int);
704
705 impl Into<ExitStatus> for ExitStatusError {
706     fn into(self) -> ExitStatus {
707         ExitStatus(self.0.into())
708     }
709 }
710
711 impl fmt::Debug for ExitStatusError {
712     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
713         f.debug_tuple("unix_wait_status").field(&self.0).finish()
714     }
715 }
716
717 impl ExitStatusError {
718     pub fn code(self) -> Option<NonZeroI32> {
719         ExitStatus(self.0.into()).code().map(|st| st.try_into().unwrap())
720     }
721 }
722
723 #[cfg(target_os = "linux")]
724 #[unstable(feature = "linux_pidfd", issue = "82971")]
725 impl crate::os::linux::process::ChildExt for crate::process::Child {
726     fn pidfd(&self) -> io::Result<&PidFd> {
727         self.handle
728             .pidfd
729             .as_ref()
730             .ok_or_else(|| Error::new(ErrorKind::Other, "No pidfd was created."))
731     }
732
733     fn take_pidfd(&mut self) -> io::Result<PidFd> {
734         self.handle
735             .pidfd
736             .take()
737             .ok_or_else(|| Error::new(ErrorKind::Other, "No pidfd was created."))
738     }
739 }
740
741 #[cfg(test)]
742 #[path = "process_unix/tests.rs"]
743 mod tests;