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 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 const MIN_FORKSPAWN_SLEEP: Duration = Duration::from_nanos(1);
43 const MAX_FORKSPAWN_SLEEP: Duration = Duration::from_millis(1000);
45 }
46}
47
48impl 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::net::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 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); 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 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 let mut p = unsafe { Process::new(pid, pidfd) };
133 let mut bytes = [0; 8];
134
135 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 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 pub fn output(&mut self) -> io::Result<(ExitStatus, Vec<u8>, Vec<u8>)> {
166 let (proc, pipes) = self.spawn(Stdio::MakePipe, false)?;
167 crate::sys_common::process::wait_with_output(proc, pipes)
168 }
169
170 #[cfg(any(target_os = "tvos", target_os = "watchos"))]
177 const ERR_APPLE_TV_WATCH_NO_FORK_EXEC: Error = io::const_error!(
178 ErrorKind::Unsupported,
179 "`fork`+`exec`-based process spawning is not supported on this target",
180 );
181
182 #[cfg(any(target_os = "tvos", target_os = "watchos"))]
183 unsafe fn do_fork(&mut self) -> Result<pid_t, io::Error> {
184 return Err(Self::ERR_APPLE_TV_WATCH_NO_FORK_EXEC);
185 }
186
187 #[cfg(not(any(target_os = "watchos", target_os = "tvos", target_os = "nto")))]
190 unsafe fn do_fork(&mut self) -> Result<pid_t, io::Error> {
191 cvt(libc::fork())
192 }
193
194 #[cfg(target_os = "nto")]
199 unsafe fn do_fork(&mut self) -> Result<pid_t, io::Error> {
200 use crate::sys::os::errno;
201
202 let mut delay = MIN_FORKSPAWN_SLEEP;
203
204 loop {
205 let r = libc::fork();
206 if r == -1 as libc::pid_t && errno() as libc::c_int == libc::EBADF {
207 if delay < get_clock_resolution() {
208 thread::yield_now();
211 } else if delay < MAX_FORKSPAWN_SLEEP {
212 thread::sleep(delay);
213 } else {
214 return Err(io::const_error!(
215 ErrorKind::WouldBlock,
216 "forking returned EBADF too often",
217 ));
218 }
219 delay *= 2;
220 continue;
221 } else {
222 return cvt(r);
223 }
224 }
225 }
226
227 pub fn exec(&mut self, default: Stdio) -> io::Error {
228 let envp = self.capture_env();
229
230 if self.saw_nul() {
231 return io::const_error!(ErrorKind::InvalidInput, "nul byte found in provided data");
232 }
233
234 match self.setup_io(default, true) {
235 Ok((_, theirs)) => {
236 unsafe {
237 let _lock = sys::env::env_read_lock();
241
242 let Err(e) = self.do_exec(theirs, envp.as_ref());
243 e
244 }
245 }
246 Err(e) => e,
247 }
248 }
249
250 #[cfg(not(any(target_os = "tvos", target_os = "watchos")))]
281 unsafe fn do_exec(
282 &mut self,
283 stdio: ChildPipes,
284 maybe_envp: Option<&CStringArray>,
285 ) -> Result<!, io::Error> {
286 use crate::sys::{self, cvt_r};
287
288 if let Some(fd) = stdio.stdin.fd() {
289 cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?;
290 }
291 if let Some(fd) = stdio.stdout.fd() {
292 cvt_r(|| libc::dup2(fd, libc::STDOUT_FILENO))?;
293 }
294 if let Some(fd) = stdio.stderr.fd() {
295 cvt_r(|| libc::dup2(fd, libc::STDERR_FILENO))?;
296 }
297
298 #[cfg(not(target_os = "l4re"))]
299 {
300 if let Some(_g) = self.get_groups() {
301 #[cfg(not(target_os = "redox"))]
303 cvt(libc::setgroups(_g.len().try_into().unwrap(), _g.as_ptr()))?;
304 }
305 if let Some(u) = self.get_gid() {
306 cvt(libc::setgid(u as gid_t))?;
307 }
308 if let Some(u) = self.get_uid() {
309 #[cfg(not(target_os = "redox"))]
317 if self.get_groups().is_none() {
318 let res = cvt(libc::setgroups(0, crate::ptr::null()));
319 if let Err(e) = res {
320 if e.raw_os_error() != Some(libc::EPERM) {
324 return Err(e.into());
325 }
326 }
327 }
328 cvt(libc::setuid(u as uid_t))?;
329 }
330 }
331 if let Some(cwd) = self.get_cwd() {
332 cvt(libc::chdir(cwd.as_ptr()))?;
333 }
334
335 if let Some(pgroup) = self.get_pgroup() {
336 cvt(libc::setpgid(0, pgroup))?;
337 }
338
339 #[cfg(not(target_os = "emscripten"))]
341 {
342 if !crate::sys::pal::on_broken_pipe_flag_used() {
350 #[cfg(target_os = "android")] {
352 let mut action: libc::sigaction = mem::zeroed();
353 action.sa_sigaction = libc::SIG_DFL;
354 cvt(libc::sigaction(libc::SIGPIPE, &action, crate::ptr::null_mut()))?;
355 }
356 #[cfg(not(target_os = "android"))]
357 {
358 let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL);
359 if ret == libc::SIG_ERR {
360 return Err(io::Error::last_os_error());
361 }
362 }
363 #[cfg(target_os = "hurd")]
364 {
365 let ret = sys::signal(libc::SIGLOST, libc::SIG_DFL);
366 if ret == libc::SIG_ERR {
367 return Err(io::Error::last_os_error());
368 }
369 }
370 }
371 }
372
373 for callback in self.get_closures().iter_mut() {
374 callback()?;
375 }
376
377 let mut _reset = None;
383 if let Some(envp) = maybe_envp {
384 struct Reset(*const *const libc::c_char);
385
386 impl Drop for Reset {
387 fn drop(&mut self) {
388 unsafe {
389 *sys::env::environ() = self.0;
390 }
391 }
392 }
393
394 _reset = Some(Reset(*sys::env::environ()));
395 *sys::env::environ() = envp.as_ptr();
396 }
397
398 libc::execvp(self.get_program_cstr().as_ptr(), self.get_argv().as_ptr());
399 Err(io::Error::last_os_error())
400 }
401
402 #[cfg(any(target_os = "tvos", target_os = "watchos"))]
403 unsafe fn do_exec(
404 &mut self,
405 _stdio: ChildPipes,
406 _maybe_envp: Option<&CStringArray>,
407 ) -> Result<!, io::Error> {
408 return Err(Self::ERR_APPLE_TV_WATCH_NO_FORK_EXEC);
409 }
410
411 #[cfg(not(any(
412 target_os = "freebsd",
413 target_os = "illumos",
414 all(target_os = "linux", target_env = "gnu"),
415 all(target_os = "linux", target_env = "musl"),
416 target_os = "nto",
417 target_vendor = "apple",
418 target_os = "cygwin",
419 )))]
420 fn posix_spawn(
421 &mut self,
422 _: &ChildPipes,
423 _: Option<&CStringArray>,
424 ) -> io::Result<Option<Process>> {
425 Ok(None)
426 }
427
428 #[cfg(any(
431 target_os = "freebsd",
432 target_os = "illumos",
433 all(target_os = "linux", target_env = "gnu"),
434 all(target_os = "linux", target_env = "musl"),
435 target_os = "nto",
436 target_vendor = "apple",
437 target_os = "cygwin",
438 ))]
439 fn posix_spawn(
440 &mut self,
441 stdio: &ChildPipes,
442 envp: Option<&CStringArray>,
443 ) -> io::Result<Option<Process>> {
444 #[cfg(target_os = "linux")]
445 use core::sync::atomic::{Atomic, AtomicU8, Ordering};
446
447 use crate::mem::MaybeUninit;
448 use crate::sys::{self, cvt_nz, on_broken_pipe_flag_used};
449
450 if self.get_gid().is_some()
451 || self.get_uid().is_some()
452 || (self.env_saw_path() && !self.program_is_path())
453 || !self.get_closures().is_empty()
454 || self.get_groups().is_some()
455 {
456 return Ok(None);
457 }
458
459 cfg_if::cfg_if! {
460 if #[cfg(target_os = "linux")] {
461 use crate::sys::weak::weak;
462
463 weak!(
464 fn pidfd_spawnp(
465 pidfd: *mut libc::c_int,
466 path: *const libc::c_char,
467 file_actions: *const libc::posix_spawn_file_actions_t,
468 attrp: *const libc::posix_spawnattr_t,
469 argv: *const *mut libc::c_char,
470 envp: *const *mut libc::c_char,
471 ) -> libc::c_int;
472 );
473
474 weak!(
475 fn pidfd_getpid(pidfd: libc::c_int) -> libc::c_int;
476 );
477
478 static PIDFD_SUPPORTED: Atomic<u8> = AtomicU8::new(0);
479 const UNKNOWN: u8 = 0;
480 const SPAWN: u8 = 1;
481 const FORK_EXEC: u8 = 2;
483 const NO: u8 = 3;
486
487 if self.get_create_pidfd() {
488 let mut support = PIDFD_SUPPORTED.load(Ordering::Relaxed);
489 if support == FORK_EXEC {
490 return Ok(None);
491 }
492 if support == UNKNOWN {
493 support = NO;
494 let our_pid = crate::process::id();
495 let pidfd = cvt(unsafe { libc::syscall(libc::SYS_pidfd_open, our_pid, 0) } as c_int);
496 match pidfd {
497 Ok(pidfd) => {
498 support = FORK_EXEC;
499 if let Some(Ok(pid)) = pidfd_getpid.get().map(|f| cvt(unsafe { f(pidfd) } as i32)) {
500 if pidfd_spawnp.get().is_some() && pid as u32 == our_pid {
501 support = SPAWN
502 }
503 }
504 unsafe { libc::close(pidfd) };
505 }
506 Err(e) if e.raw_os_error() == Some(libc::EMFILE) => {
507 return Err(e)
510 }
511 _ => {}
512 }
513 PIDFD_SUPPORTED.store(support, Ordering::Relaxed);
514 if support == FORK_EXEC {
515 return Ok(None);
516 }
517 }
518 core::assert_matches::debug_assert_matches!(support, SPAWN | NO);
519 }
520 } else {
521 if self.get_create_pidfd() {
522 unreachable!("only implemented on linux")
523 }
524 }
525 }
526
527 #[cfg(all(target_os = "linux", target_env = "gnu"))]
529 {
530 if let Some(version) = sys::os::glibc_version() {
531 if version < (2, 24) {
532 return Ok(None);
533 }
534 } else {
535 return Ok(None);
536 }
537 }
538
539 #[cfg(target_os = "nto")]
544 unsafe fn retrying_libc_posix_spawnp(
545 pid: *mut pid_t,
546 file: *const c_char,
547 file_actions: *const posix_spawn_file_actions_t,
548 attrp: *const posix_spawnattr_t,
549 argv: *const *mut c_char,
550 envp: *const *mut c_char,
551 ) -> io::Result<i32> {
552 let mut delay = MIN_FORKSPAWN_SLEEP;
553 loop {
554 match libc::posix_spawnp(pid, file, file_actions, attrp, argv, envp) {
555 libc::EBADF => {
556 if delay < get_clock_resolution() {
557 thread::yield_now();
560 } else if delay < MAX_FORKSPAWN_SLEEP {
561 thread::sleep(delay);
562 } else {
563 return Err(io::const_error!(
564 ErrorKind::WouldBlock,
565 "posix_spawnp returned EBADF too often",
566 ));
567 }
568 delay *= 2;
569 continue;
570 }
571 r => {
572 return Ok(r);
573 }
574 }
575 }
576 }
577
578 type PosixSpawnAddChdirFn = unsafe extern "C" fn(
579 *mut libc::posix_spawn_file_actions_t,
580 *const libc::c_char,
581 ) -> libc::c_int;
582
583 #[cfg(not(any(all(target_os = "linux", target_env = "musl"), target_os = "cygwin")))]
590 fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
591 use crate::sys::weak::weak;
592
593 weak!(
598 fn posix_spawn_file_actions_addchdir_np(
599 file_actions: *mut libc::posix_spawn_file_actions_t,
600 path: *const libc::c_char,
601 ) -> libc::c_int;
602 );
603
604 weak!(
605 fn posix_spawn_file_actions_addchdir(
606 file_actions: *mut libc::posix_spawn_file_actions_t,
607 path: *const libc::c_char,
608 ) -> libc::c_int;
609 );
610
611 posix_spawn_file_actions_addchdir_np
612 .get()
613 .or_else(|| posix_spawn_file_actions_addchdir.get())
614 }
615
616 #[cfg(any(all(target_os = "linux", target_env = "musl"), target_os = "cygwin"))]
626 fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
627 Some(libc::posix_spawn_file_actions_addchdir_np)
629 }
630
631 let addchdir = match self.get_cwd() {
632 Some(cwd) => {
633 if cfg!(target_vendor = "apple") {
634 if self.get_program_kind() == ProgramKind::Relative {
640 return Ok(None);
641 }
642 }
643 match get_posix_spawn_addchdir() {
647 Some(f) => Some((f, cwd)),
648 None => return Ok(None),
649 }
650 }
651 None => None,
652 };
653
654 let pgroup = self.get_pgroup();
655
656 struct PosixSpawnFileActions<'a>(&'a mut MaybeUninit<libc::posix_spawn_file_actions_t>);
657
658 impl Drop for PosixSpawnFileActions<'_> {
659 fn drop(&mut self) {
660 unsafe {
661 libc::posix_spawn_file_actions_destroy(self.0.as_mut_ptr());
662 }
663 }
664 }
665
666 struct PosixSpawnattr<'a>(&'a mut MaybeUninit<libc::posix_spawnattr_t>);
667
668 impl Drop for PosixSpawnattr<'_> {
669 fn drop(&mut self) {
670 unsafe {
671 libc::posix_spawnattr_destroy(self.0.as_mut_ptr());
672 }
673 }
674 }
675
676 unsafe {
677 let mut attrs = MaybeUninit::uninit();
678 cvt_nz(libc::posix_spawnattr_init(attrs.as_mut_ptr()))?;
679 let attrs = PosixSpawnattr(&mut attrs);
680
681 let mut flags = 0;
682
683 let mut file_actions = MaybeUninit::uninit();
684 cvt_nz(libc::posix_spawn_file_actions_init(file_actions.as_mut_ptr()))?;
685 let file_actions = PosixSpawnFileActions(&mut file_actions);
686
687 if let Some(fd) = stdio.stdin.fd() {
688 cvt_nz(libc::posix_spawn_file_actions_adddup2(
689 file_actions.0.as_mut_ptr(),
690 fd,
691 libc::STDIN_FILENO,
692 ))?;
693 }
694 if let Some(fd) = stdio.stdout.fd() {
695 cvt_nz(libc::posix_spawn_file_actions_adddup2(
696 file_actions.0.as_mut_ptr(),
697 fd,
698 libc::STDOUT_FILENO,
699 ))?;
700 }
701 if let Some(fd) = stdio.stderr.fd() {
702 cvt_nz(libc::posix_spawn_file_actions_adddup2(
703 file_actions.0.as_mut_ptr(),
704 fd,
705 libc::STDERR_FILENO,
706 ))?;
707 }
708 if let Some((f, cwd)) = addchdir {
709 cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
710 }
711
712 if let Some(pgroup) = pgroup {
713 flags |= libc::POSIX_SPAWN_SETPGROUP;
714 cvt_nz(libc::posix_spawnattr_setpgroup(attrs.0.as_mut_ptr(), pgroup))?;
715 }
716
717 if !on_broken_pipe_flag_used() {
725 let mut default_set = MaybeUninit::<libc::sigset_t>::uninit();
726 cvt(sigemptyset(default_set.as_mut_ptr()))?;
727 cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGPIPE))?;
728 #[cfg(target_os = "hurd")]
729 {
730 cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGLOST))?;
731 }
732 cvt_nz(libc::posix_spawnattr_setsigdefault(
733 attrs.0.as_mut_ptr(),
734 default_set.as_ptr(),
735 ))?;
736 flags |= libc::POSIX_SPAWN_SETSIGDEF;
737 }
738
739 cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
740
741 let _env_lock = sys::env::env_read_lock();
743 let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::env::environ() as *const _);
744
745 #[cfg(not(target_os = "nto"))]
746 let spawn_fn = libc::posix_spawnp;
747 #[cfg(target_os = "nto")]
748 let spawn_fn = retrying_libc_posix_spawnp;
749
750 #[cfg(target_os = "linux")]
751 if self.get_create_pidfd() && PIDFD_SUPPORTED.load(Ordering::Relaxed) == SPAWN {
752 let mut pidfd: libc::c_int = -1;
753 let spawn_res = pidfd_spawnp.get().unwrap()(
754 &mut pidfd,
755 self.get_program_cstr().as_ptr(),
756 file_actions.0.as_ptr(),
757 attrs.0.as_ptr(),
758 self.get_argv().as_ptr() as *const _,
759 envp as *const _,
760 );
761
762 let spawn_res = cvt_nz(spawn_res);
763 if let Err(ref e) = spawn_res
764 && e.raw_os_error() == Some(libc::ENOSYS)
765 {
766 PIDFD_SUPPORTED.store(FORK_EXEC, Ordering::Relaxed);
767 return Ok(None);
768 }
769 spawn_res?;
770
771 let pid = match cvt(pidfd_getpid.get().unwrap()(pidfd)) {
772 Ok(pid) => pid,
773 Err(e) => {
774 libc::close(pidfd);
778 return Err(Error::new(
779 e.kind(),
780 "pidfd_spawnp succeeded but the child's PID could not be obtained",
781 ));
782 }
783 };
784
785 return Ok(Some(Process::new(pid, pidfd)));
786 }
787
788 let mut p = Process::new(0, -1);
790
791 let spawn_res = spawn_fn(
792 &mut p.pid,
793 self.get_program_cstr().as_ptr(),
794 file_actions.0.as_ptr(),
795 attrs.0.as_ptr(),
796 self.get_argv().as_ptr() as *const _,
797 envp as *const _,
798 );
799
800 #[cfg(target_os = "nto")]
801 let spawn_res = spawn_res?;
802
803 cvt_nz(spawn_res)?;
804 Ok(Some(p))
805 }
806 }
807
808 #[cfg(target_os = "linux")]
809 fn send_pidfd(&self, sock: &crate::sys::net::Socket) {
810 use libc::{CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_SPACE, SCM_RIGHTS, SOL_SOCKET};
811
812 use crate::io::IoSlice;
813 use crate::os::fd::RawFd;
814 use crate::sys::cvt_r;
815
816 unsafe {
817 let child_pid = libc::getpid();
818 let pidfd = libc::syscall(libc::SYS_pidfd_open, child_pid, 0);
820
821 let fds: [c_int; 1] = [pidfd as RawFd];
822
823 const SCM_MSG_LEN: usize = size_of::<[c_int; 1]>();
824
825 #[repr(C)]
826 union Cmsg {
827 buf: [u8; unsafe { CMSG_SPACE(SCM_MSG_LEN as u32) as usize }],
828 _align: libc::cmsghdr,
829 }
830
831 let mut cmsg: Cmsg = mem::zeroed();
832
833 let mut iov = [IoSlice::new(b"")];
835 let mut msg: libc::msghdr = mem::zeroed();
836
837 msg.msg_iov = (&raw mut iov) as *mut _;
838 msg.msg_iovlen = 1;
839
840 if pidfd >= 0 {
842 msg.msg_controllen = size_of_val(&cmsg.buf) as _;
843 msg.msg_control = (&raw mut cmsg.buf) as *mut _;
844
845 let hdr = CMSG_FIRSTHDR((&raw mut msg) as *mut _);
846 (*hdr).cmsg_level = SOL_SOCKET;
847 (*hdr).cmsg_type = SCM_RIGHTS;
848 (*hdr).cmsg_len = CMSG_LEN(SCM_MSG_LEN as _) as _;
849 let data = CMSG_DATA(hdr);
850 crate::ptr::copy_nonoverlapping(
851 fds.as_ptr().cast::<u8>(),
852 data as *mut _,
853 SCM_MSG_LEN,
854 );
855 }
856
857 match cvt_r(|| libc::sendmsg(sock.as_raw(), &msg, 0)) {
860 Ok(0) => {}
861 other => rtabort!("failed to communicate with parent process. {:?}", other),
862 }
863 }
864 }
865
866 #[cfg(target_os = "linux")]
867 fn recv_pidfd(&self, sock: &crate::sys::net::Socket) -> pid_t {
868 use libc::{CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_SPACE, SCM_RIGHTS, SOL_SOCKET};
869
870 use crate::io::IoSliceMut;
871 use crate::sys::cvt_r;
872
873 unsafe {
874 const SCM_MSG_LEN: usize = size_of::<[c_int; 1]>();
875
876 #[repr(C)]
877 union Cmsg {
878 _buf: [u8; unsafe { CMSG_SPACE(SCM_MSG_LEN as u32) as usize }],
879 _align: libc::cmsghdr,
880 }
881 let mut cmsg: Cmsg = mem::zeroed();
882 let mut iov = [IoSliceMut::new(&mut [])];
884
885 let mut msg: libc::msghdr = mem::zeroed();
886
887 msg.msg_iov = (&raw mut iov) as *mut _;
888 msg.msg_iovlen = 1;
889 msg.msg_controllen = size_of::<Cmsg>() as _;
890 msg.msg_control = (&raw mut cmsg) as *mut _;
891
892 match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, libc::MSG_CMSG_CLOEXEC)) {
893 Err(_) => return -1,
894 Ok(_) => {}
895 }
896
897 let hdr = CMSG_FIRSTHDR((&raw mut msg) as *mut _);
898 if hdr.is_null()
899 || (*hdr).cmsg_level != SOL_SOCKET
900 || (*hdr).cmsg_type != SCM_RIGHTS
901 || (*hdr).cmsg_len != CMSG_LEN(SCM_MSG_LEN as _) as _
902 {
903 return -1;
904 }
905 let data = CMSG_DATA(hdr);
906
907 let mut fds = [-1 as c_int];
908
909 crate::ptr::copy_nonoverlapping(
910 data as *const _,
911 fds.as_mut_ptr().cast::<u8>(),
912 SCM_MSG_LEN,
913 );
914
915 fds[0]
916 }
917 }
918}
919
920pub struct Process {
926 pid: pid_t,
927 status: Option<ExitStatus>,
928 #[cfg(target_os = "linux")]
933 pidfd: Option<PidFd>,
934}
935
936impl Process {
937 #[cfg(target_os = "linux")]
938 unsafe fn new(pid: pid_t, pidfd: pid_t) -> Self {
945 use crate::os::unix::io::FromRawFd;
946 use crate::sys_common::FromInner;
947 let pidfd = (pidfd >= 0).then(|| PidFd::from_inner(sys::fd::FileDesc::from_raw_fd(pidfd)));
949 Process { pid, status: None, pidfd }
950 }
951
952 #[cfg(not(target_os = "linux"))]
953 unsafe fn new(pid: pid_t, _pidfd: pid_t) -> Self {
954 Process { pid, status: None }
955 }
956
957 pub fn id(&self) -> u32 {
958 self.pid as u32
959 }
960
961 pub fn kill(&mut self) -> io::Result<()> {
962 if self.status.is_some() {
966 return Ok(());
967 }
968 #[cfg(target_os = "linux")]
969 if let Some(pid_fd) = self.pidfd.as_ref() {
970 return pid_fd.kill();
972 }
973 cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
974 }
975
976 pub fn wait(&mut self) -> io::Result<ExitStatus> {
977 use crate::sys::cvt_r;
978 if let Some(status) = self.status {
979 return Ok(status);
980 }
981 #[cfg(target_os = "linux")]
982 if let Some(pid_fd) = self.pidfd.as_ref() {
983 let status = pid_fd.wait()?;
984 self.status = Some(status);
985 return Ok(status);
986 }
987 let mut status = 0 as c_int;
988 cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
989 self.status = Some(ExitStatus::new(status));
990 Ok(ExitStatus::new(status))
991 }
992
993 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
994 if let Some(status) = self.status {
995 return Ok(Some(status));
996 }
997 #[cfg(target_os = "linux")]
998 if let Some(pid_fd) = self.pidfd.as_ref() {
999 let status = pid_fd.try_wait()?;
1000 if let Some(status) = status {
1001 self.status = Some(status)
1002 }
1003 return Ok(status);
1004 }
1005 let mut status = 0 as c_int;
1006 let pid = cvt(unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) })?;
1007 if pid == 0 {
1008 Ok(None)
1009 } else {
1010 self.status = Some(ExitStatus::new(status));
1011 Ok(Some(ExitStatus::new(status)))
1012 }
1013 }
1014}
1015
1016#[derive(PartialEq, Eq, Clone, Copy, Default)]
1021pub struct ExitStatus(c_int);
1022
1023impl fmt::Debug for ExitStatus {
1024 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1025 f.debug_tuple("unix_wait_status").field(&self.0).finish()
1026 }
1027}
1028
1029impl ExitStatus {
1030 pub fn new(status: c_int) -> ExitStatus {
1031 ExitStatus(status)
1032 }
1033
1034 #[cfg(target_os = "linux")]
1035 pub fn from_waitid_siginfo(siginfo: libc::siginfo_t) -> ExitStatus {
1036 let status = unsafe { siginfo.si_status() };
1037
1038 match siginfo.si_code {
1039 libc::CLD_EXITED => ExitStatus((status & 0xff) << 8),
1040 libc::CLD_KILLED => ExitStatus(status),
1041 libc::CLD_DUMPED => ExitStatus(status | 0x80),
1042 libc::CLD_CONTINUED => ExitStatus(0xffff),
1043 libc::CLD_STOPPED | libc::CLD_TRAPPED => ExitStatus(((status & 0xff) << 8) | 0x7f),
1044 _ => unreachable!("waitid() should only return the above codes"),
1045 }
1046 }
1047
1048 fn exited(&self) -> bool {
1049 libc::WIFEXITED(self.0)
1050 }
1051
1052 pub fn exit_ok(&self) -> Result<(), ExitStatusError> {
1053 match NonZero::try_from(self.0) {
1059 Ok(failure) => Err(ExitStatusError(failure)),
1060 Err(_) => Ok(()),
1061 }
1062 }
1063
1064 pub fn code(&self) -> Option<i32> {
1065 self.exited().then(|| libc::WEXITSTATUS(self.0))
1066 }
1067
1068 pub fn signal(&self) -> Option<i32> {
1069 libc::WIFSIGNALED(self.0).then(|| libc::WTERMSIG(self.0))
1070 }
1071
1072 pub fn core_dumped(&self) -> bool {
1073 libc::WIFSIGNALED(self.0) && libc::WCOREDUMP(self.0)
1074 }
1075
1076 pub fn stopped_signal(&self) -> Option<i32> {
1077 libc::WIFSTOPPED(self.0).then(|| libc::WSTOPSIG(self.0))
1078 }
1079
1080 pub fn continued(&self) -> bool {
1081 libc::WIFCONTINUED(self.0)
1082 }
1083
1084 pub fn into_raw(&self) -> c_int {
1085 self.0
1086 }
1087}
1088
1089impl From<c_int> for ExitStatus {
1091 fn from(a: c_int) -> ExitStatus {
1092 ExitStatus(a)
1093 }
1094}
1095
1096fn signal_string(signal: i32) -> &'static str {
1103 match signal {
1104 libc::SIGHUP => " (SIGHUP)",
1105 libc::SIGINT => " (SIGINT)",
1106 libc::SIGQUIT => " (SIGQUIT)",
1107 libc::SIGILL => " (SIGILL)",
1108 libc::SIGTRAP => " (SIGTRAP)",
1109 libc::SIGABRT => " (SIGABRT)",
1110 #[cfg(not(target_os = "l4re"))]
1111 libc::SIGBUS => " (SIGBUS)",
1112 libc::SIGFPE => " (SIGFPE)",
1113 libc::SIGKILL => " (SIGKILL)",
1114 #[cfg(not(target_os = "l4re"))]
1115 libc::SIGUSR1 => " (SIGUSR1)",
1116 libc::SIGSEGV => " (SIGSEGV)",
1117 #[cfg(not(target_os = "l4re"))]
1118 libc::SIGUSR2 => " (SIGUSR2)",
1119 libc::SIGPIPE => " (SIGPIPE)",
1120 libc::SIGALRM => " (SIGALRM)",
1121 libc::SIGTERM => " (SIGTERM)",
1122 #[cfg(not(target_os = "l4re"))]
1123 libc::SIGCHLD => " (SIGCHLD)",
1124 #[cfg(not(target_os = "l4re"))]
1125 libc::SIGCONT => " (SIGCONT)",
1126 #[cfg(not(target_os = "l4re"))]
1127 libc::SIGSTOP => " (SIGSTOP)",
1128 #[cfg(not(target_os = "l4re"))]
1129 libc::SIGTSTP => " (SIGTSTP)",
1130 #[cfg(not(target_os = "l4re"))]
1131 libc::SIGTTIN => " (SIGTTIN)",
1132 #[cfg(not(target_os = "l4re"))]
1133 libc::SIGTTOU => " (SIGTTOU)",
1134 #[cfg(not(target_os = "l4re"))]
1135 libc::SIGURG => " (SIGURG)",
1136 #[cfg(not(target_os = "l4re"))]
1137 libc::SIGXCPU => " (SIGXCPU)",
1138 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1139 libc::SIGXFSZ => " (SIGXFSZ)",
1140 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1141 libc::SIGVTALRM => " (SIGVTALRM)",
1142 #[cfg(not(target_os = "l4re"))]
1143 libc::SIGPROF => " (SIGPROF)",
1144 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1145 libc::SIGWINCH => " (SIGWINCH)",
1146 #[cfg(not(any(target_os = "haiku", target_os = "l4re")))]
1147 libc::SIGIO => " (SIGIO)",
1148 #[cfg(target_os = "haiku")]
1149 libc::SIGPOLL => " (SIGPOLL)",
1150 #[cfg(not(target_os = "l4re"))]
1151 libc::SIGSYS => " (SIGSYS)",
1152 #[cfg(all(
1154 target_os = "linux",
1155 any(
1156 target_arch = "x86_64",
1157 target_arch = "x86",
1158 target_arch = "arm",
1159 target_arch = "aarch64"
1160 )
1161 ))]
1162 libc::SIGSTKFLT => " (SIGSTKFLT)",
1163 #[cfg(any(target_os = "linux", target_os = "nto", target_os = "cygwin"))]
1164 libc::SIGPWR => " (SIGPWR)",
1165 #[cfg(any(
1166 target_os = "freebsd",
1167 target_os = "netbsd",
1168 target_os = "openbsd",
1169 target_os = "dragonfly",
1170 target_os = "nto",
1171 target_vendor = "apple",
1172 target_os = "cygwin",
1173 ))]
1174 libc::SIGEMT => " (SIGEMT)",
1175 #[cfg(any(
1176 target_os = "freebsd",
1177 target_os = "netbsd",
1178 target_os = "openbsd",
1179 target_os = "dragonfly",
1180 target_vendor = "apple",
1181 ))]
1182 libc::SIGINFO => " (SIGINFO)",
1183 #[cfg(target_os = "hurd")]
1184 libc::SIGLOST => " (SIGLOST)",
1185 #[cfg(target_os = "freebsd")]
1186 libc::SIGTHR => " (SIGTHR)",
1187 #[cfg(target_os = "freebsd")]
1188 libc::SIGLIBRT => " (SIGLIBRT)",
1189 _ => "",
1190 }
1191}
1192
1193impl fmt::Display for ExitStatus {
1194 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1195 if let Some(code) = self.code() {
1196 write!(f, "exit status: {code}")
1197 } else if let Some(signal) = self.signal() {
1198 let signal_string = signal_string(signal);
1199 if self.core_dumped() {
1200 write!(f, "signal: {signal}{signal_string} (core dumped)")
1201 } else {
1202 write!(f, "signal: {signal}{signal_string}")
1203 }
1204 } else if let Some(signal) = self.stopped_signal() {
1205 let signal_string = signal_string(signal);
1206 write!(f, "stopped (not terminated) by signal: {signal}{signal_string}")
1207 } else if self.continued() {
1208 write!(f, "continued (WIFCONTINUED)")
1209 } else {
1210 write!(f, "unrecognised wait status: {} {:#x}", self.0, self.0)
1211 }
1212 }
1213}
1214
1215#[derive(PartialEq, Eq, Clone, Copy)]
1216pub struct ExitStatusError(NonZero<c_int>);
1217
1218impl Into<ExitStatus> for ExitStatusError {
1219 fn into(self) -> ExitStatus {
1220 ExitStatus(self.0.into())
1221 }
1222}
1223
1224impl fmt::Debug for ExitStatusError {
1225 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1226 f.debug_tuple("unix_wait_status").field(&self.0).finish()
1227 }
1228}
1229
1230impl ExitStatusError {
1231 pub fn code(self) -> Option<NonZero<i32>> {
1232 ExitStatus(self.0.into()).code().map(|st| st.try_into().unwrap())
1233 }
1234}
1235
1236#[cfg(target_os = "linux")]
1237mod linux_child_ext {
1238 use crate::io::ErrorKind;
1239 use crate::os::linux::process as os;
1240 use crate::sys::pal::linux::pidfd as imp;
1241 use crate::sys_common::FromInner;
1242 use crate::{io, mem};
1243
1244 #[unstable(feature = "linux_pidfd", issue = "82971")]
1245 impl crate::os::linux::process::ChildExt for crate::process::Child {
1246 fn pidfd(&self) -> io::Result<&os::PidFd> {
1247 self.handle
1248 .pidfd
1249 .as_ref()
1250 .map(|fd| unsafe { mem::transmute::<&imp::PidFd, &os::PidFd>(fd) })
1252 .ok_or_else(|| io::const_error!(ErrorKind::Uncategorized, "no pidfd was created."))
1253 }
1254
1255 fn into_pidfd(mut self) -> Result<os::PidFd, Self> {
1256 self.handle
1257 .pidfd
1258 .take()
1259 .map(|fd| <os::PidFd as FromInner<imp::PidFd>>::from_inner(fd))
1260 .ok_or_else(|| self)
1261 }
1262 }
1263}
1264
1265#[cfg(test)]
1266mod tests;
1267
1268#[cfg(all(test, target_os = "linux"))]
1270#[path = "unsupported/wait_status.rs"]
1271mod unsupported_wait_status;