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 crate::io::{self, Error, ErrorKind};
14use crate::num::NonZero;
15use crate::sys::cvt;
16#[cfg(target_os = "linux")]
17use crate::sys::pal::unix::linux::pidfd::PidFd;
18use crate::sys::process::process_common::*;
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::os::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::os::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::os::environ() = self.0;
390 }
391 }
392 }
393
394 _reset = Some(Reset(*sys::os::environ()));
395 *sys::os::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 all(target_os = "linux", target_env = "gnu"),
414 all(target_os = "linux", target_env = "musl"),
415 target_os = "nto",
416 target_vendor = "apple",
417 )))]
418 fn posix_spawn(
419 &mut self,
420 _: &ChildPipes,
421 _: Option<&CStringArray>,
422 ) -> io::Result<Option<Process>> {
423 Ok(None)
424 }
425
426 #[cfg(any(
429 target_os = "freebsd",
430 all(target_os = "linux", target_env = "gnu"),
431 all(target_os = "linux", target_env = "musl"),
432 target_os = "nto",
433 target_vendor = "apple",
434 ))]
435 fn posix_spawn(
436 &mut self,
437 stdio: &ChildPipes,
438 envp: Option<&CStringArray>,
439 ) -> io::Result<Option<Process>> {
440 #[cfg(target_os = "linux")]
441 use core::sync::atomic::{AtomicU8, Ordering};
442
443 use crate::mem::MaybeUninit;
444 use crate::sys::{self, cvt_nz, on_broken_pipe_flag_used};
445
446 if self.get_gid().is_some()
447 || self.get_uid().is_some()
448 || (self.env_saw_path() && !self.program_is_path())
449 || !self.get_closures().is_empty()
450 || self.get_groups().is_some()
451 {
452 return Ok(None);
453 }
454
455 cfg_if::cfg_if! {
456 if #[cfg(target_os = "linux")] {
457 use crate::sys::weak::weak;
458
459 weak! {
460 fn pidfd_spawnp(
461 *mut libc::c_int,
462 *const libc::c_char,
463 *const libc::posix_spawn_file_actions_t,
464 *const libc::posix_spawnattr_t,
465 *const *mut libc::c_char,
466 *const *mut libc::c_char
467 ) -> libc::c_int
468 }
469
470 weak! { fn pidfd_getpid(libc::c_int) -> libc::c_int }
471
472 static PIDFD_SUPPORTED: AtomicU8 = AtomicU8::new(0);
473 const UNKNOWN: u8 = 0;
474 const SPAWN: u8 = 1;
475 const FORK_EXEC: u8 = 2;
477 const NO: u8 = 3;
480
481 if self.get_create_pidfd() {
482 let mut support = PIDFD_SUPPORTED.load(Ordering::Relaxed);
483 if support == FORK_EXEC {
484 return Ok(None);
485 }
486 if support == UNKNOWN {
487 support = NO;
488 let our_pid = crate::process::id();
489 let pidfd = cvt(unsafe { libc::syscall(libc::SYS_pidfd_open, our_pid, 0) } as c_int);
490 match pidfd {
491 Ok(pidfd) => {
492 support = FORK_EXEC;
493 if let Some(Ok(pid)) = pidfd_getpid.get().map(|f| cvt(unsafe { f(pidfd) } as i32)) {
494 if pidfd_spawnp.get().is_some() && pid as u32 == our_pid {
495 support = SPAWN
496 }
497 }
498 unsafe { libc::close(pidfd) };
499 }
500 Err(e) if e.raw_os_error() == Some(libc::EMFILE) => {
501 return Err(e)
504 }
505 _ => {}
506 }
507 PIDFD_SUPPORTED.store(support, Ordering::Relaxed);
508 if support == FORK_EXEC {
509 return Ok(None);
510 }
511 }
512 core::assert_matches::debug_assert_matches!(support, SPAWN | NO);
513 }
514 } else {
515 if self.get_create_pidfd() {
516 unreachable!("only implemented on linux")
517 }
518 }
519 }
520
521 #[cfg(all(target_os = "linux", target_env = "gnu"))]
523 {
524 if let Some(version) = sys::os::glibc_version() {
525 if version < (2, 24) {
526 return Ok(None);
527 }
528 } else {
529 return Ok(None);
530 }
531 }
532
533 #[cfg(target_os = "nto")]
538 unsafe fn retrying_libc_posix_spawnp(
539 pid: *mut pid_t,
540 file: *const c_char,
541 file_actions: *const posix_spawn_file_actions_t,
542 attrp: *const posix_spawnattr_t,
543 argv: *const *mut c_char,
544 envp: *const *mut c_char,
545 ) -> io::Result<i32> {
546 let mut delay = MIN_FORKSPAWN_SLEEP;
547 loop {
548 match libc::posix_spawnp(pid, file, file_actions, attrp, argv, envp) {
549 libc::EBADF => {
550 if delay < get_clock_resolution() {
551 thread::yield_now();
554 } else if delay < MAX_FORKSPAWN_SLEEP {
555 thread::sleep(delay);
556 } else {
557 return Err(io::const_error!(
558 ErrorKind::WouldBlock,
559 "posix_spawnp returned EBADF too often",
560 ));
561 }
562 delay *= 2;
563 continue;
564 }
565 r => {
566 return Ok(r);
567 }
568 }
569 }
570 }
571
572 type PosixSpawnAddChdirFn = unsafe extern "C" fn(
573 *mut libc::posix_spawn_file_actions_t,
574 *const libc::c_char,
575 ) -> libc::c_int;
576
577 #[cfg(not(all(target_os = "linux", target_env = "musl")))]
584 fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
585 use crate::sys::weak::weak;
586
587 weak! {
588 fn posix_spawn_file_actions_addchdir_np(
589 *mut libc::posix_spawn_file_actions_t,
590 *const libc::c_char
591 ) -> libc::c_int
592 }
593
594 posix_spawn_file_actions_addchdir_np.get()
595 }
596
597 #[cfg(all(target_os = "linux", target_env = "musl"))]
605 fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
606 Some(libc::posix_spawn_file_actions_addchdir_np)
608 }
609
610 let addchdir = match self.get_cwd() {
611 Some(cwd) => {
612 if cfg!(target_vendor = "apple") {
613 if self.get_program_kind() == ProgramKind::Relative {
619 return Ok(None);
620 }
621 }
622 match get_posix_spawn_addchdir() {
626 Some(f) => Some((f, cwd)),
627 None => return Ok(None),
628 }
629 }
630 None => None,
631 };
632
633 let pgroup = self.get_pgroup();
634
635 struct PosixSpawnFileActions<'a>(&'a mut MaybeUninit<libc::posix_spawn_file_actions_t>);
636
637 impl Drop for PosixSpawnFileActions<'_> {
638 fn drop(&mut self) {
639 unsafe {
640 libc::posix_spawn_file_actions_destroy(self.0.as_mut_ptr());
641 }
642 }
643 }
644
645 struct PosixSpawnattr<'a>(&'a mut MaybeUninit<libc::posix_spawnattr_t>);
646
647 impl Drop for PosixSpawnattr<'_> {
648 fn drop(&mut self) {
649 unsafe {
650 libc::posix_spawnattr_destroy(self.0.as_mut_ptr());
651 }
652 }
653 }
654
655 unsafe {
656 let mut attrs = MaybeUninit::uninit();
657 cvt_nz(libc::posix_spawnattr_init(attrs.as_mut_ptr()))?;
658 let attrs = PosixSpawnattr(&mut attrs);
659
660 let mut flags = 0;
661
662 let mut file_actions = MaybeUninit::uninit();
663 cvt_nz(libc::posix_spawn_file_actions_init(file_actions.as_mut_ptr()))?;
664 let file_actions = PosixSpawnFileActions(&mut file_actions);
665
666 if let Some(fd) = stdio.stdin.fd() {
667 cvt_nz(libc::posix_spawn_file_actions_adddup2(
668 file_actions.0.as_mut_ptr(),
669 fd,
670 libc::STDIN_FILENO,
671 ))?;
672 }
673 if let Some(fd) = stdio.stdout.fd() {
674 cvt_nz(libc::posix_spawn_file_actions_adddup2(
675 file_actions.0.as_mut_ptr(),
676 fd,
677 libc::STDOUT_FILENO,
678 ))?;
679 }
680 if let Some(fd) = stdio.stderr.fd() {
681 cvt_nz(libc::posix_spawn_file_actions_adddup2(
682 file_actions.0.as_mut_ptr(),
683 fd,
684 libc::STDERR_FILENO,
685 ))?;
686 }
687 if let Some((f, cwd)) = addchdir {
688 cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
689 }
690
691 if let Some(pgroup) = pgroup {
692 flags |= libc::POSIX_SPAWN_SETPGROUP;
693 cvt_nz(libc::posix_spawnattr_setpgroup(attrs.0.as_mut_ptr(), pgroup))?;
694 }
695
696 if !on_broken_pipe_flag_used() {
704 let mut default_set = MaybeUninit::<libc::sigset_t>::uninit();
705 cvt(sigemptyset(default_set.as_mut_ptr()))?;
706 cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGPIPE))?;
707 #[cfg(target_os = "hurd")]
708 {
709 cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGLOST))?;
710 }
711 cvt_nz(libc::posix_spawnattr_setsigdefault(
712 attrs.0.as_mut_ptr(),
713 default_set.as_ptr(),
714 ))?;
715 flags |= libc::POSIX_SPAWN_SETSIGDEF;
716 }
717
718 cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
719
720 let _env_lock = sys::os::env_read_lock();
722 let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::os::environ() as *const _);
723
724 #[cfg(not(target_os = "nto"))]
725 let spawn_fn = libc::posix_spawnp;
726 #[cfg(target_os = "nto")]
727 let spawn_fn = retrying_libc_posix_spawnp;
728
729 #[cfg(target_os = "linux")]
730 if self.get_create_pidfd() && PIDFD_SUPPORTED.load(Ordering::Relaxed) == SPAWN {
731 let mut pidfd: libc::c_int = -1;
732 let spawn_res = pidfd_spawnp.get().unwrap()(
733 &mut pidfd,
734 self.get_program_cstr().as_ptr(),
735 file_actions.0.as_ptr(),
736 attrs.0.as_ptr(),
737 self.get_argv().as_ptr() as *const _,
738 envp as *const _,
739 );
740
741 let spawn_res = cvt_nz(spawn_res);
742 if let Err(ref e) = spawn_res
743 && e.raw_os_error() == Some(libc::ENOSYS)
744 {
745 PIDFD_SUPPORTED.store(FORK_EXEC, Ordering::Relaxed);
746 return Ok(None);
747 }
748 spawn_res?;
749
750 let pid = match cvt(pidfd_getpid.get().unwrap()(pidfd)) {
751 Ok(pid) => pid,
752 Err(e) => {
753 libc::close(pidfd);
757 return Err(Error::new(
758 e.kind(),
759 "pidfd_spawnp succeeded but the child's PID could not be obtained",
760 ));
761 }
762 };
763
764 return Ok(Some(Process::new(pid, pidfd)));
765 }
766
767 let mut p = Process::new(0, -1);
769
770 let spawn_res = spawn_fn(
771 &mut p.pid,
772 self.get_program_cstr().as_ptr(),
773 file_actions.0.as_ptr(),
774 attrs.0.as_ptr(),
775 self.get_argv().as_ptr() as *const _,
776 envp as *const _,
777 );
778
779 #[cfg(target_os = "nto")]
780 let spawn_res = spawn_res?;
781
782 cvt_nz(spawn_res)?;
783 Ok(Some(p))
784 }
785 }
786
787 #[cfg(target_os = "linux")]
788 fn send_pidfd(&self, sock: &crate::sys::net::Socket) {
789 use libc::{CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_SPACE, SCM_RIGHTS, SOL_SOCKET};
790
791 use crate::io::IoSlice;
792 use crate::os::fd::RawFd;
793 use crate::sys::cvt_r;
794
795 unsafe {
796 let child_pid = libc::getpid();
797 let pidfd = libc::syscall(libc::SYS_pidfd_open, child_pid, 0);
799
800 let fds: [c_int; 1] = [pidfd as RawFd];
801
802 const SCM_MSG_LEN: usize = mem::size_of::<[c_int; 1]>();
803
804 #[repr(C)]
805 union Cmsg {
806 buf: [u8; unsafe { CMSG_SPACE(SCM_MSG_LEN as u32) as usize }],
807 _align: libc::cmsghdr,
808 }
809
810 let mut cmsg: Cmsg = mem::zeroed();
811
812 let mut iov = [IoSlice::new(b"")];
814 let mut msg: libc::msghdr = mem::zeroed();
815
816 msg.msg_iov = (&raw mut iov) as *mut _;
817 msg.msg_iovlen = 1;
818
819 if pidfd >= 0 {
821 msg.msg_controllen = mem::size_of_val(&cmsg.buf) as _;
822 msg.msg_control = (&raw mut cmsg.buf) as *mut _;
823
824 let hdr = CMSG_FIRSTHDR((&raw mut msg) as *mut _);
825 (*hdr).cmsg_level = SOL_SOCKET;
826 (*hdr).cmsg_type = SCM_RIGHTS;
827 (*hdr).cmsg_len = CMSG_LEN(SCM_MSG_LEN as _) as _;
828 let data = CMSG_DATA(hdr);
829 crate::ptr::copy_nonoverlapping(
830 fds.as_ptr().cast::<u8>(),
831 data as *mut _,
832 SCM_MSG_LEN,
833 );
834 }
835
836 match cvt_r(|| libc::sendmsg(sock.as_raw(), &msg, 0)) {
839 Ok(0) => {}
840 other => rtabort!("failed to communicate with parent process. {:?}", other),
841 }
842 }
843 }
844
845 #[cfg(target_os = "linux")]
846 fn recv_pidfd(&self, sock: &crate::sys::net::Socket) -> pid_t {
847 use libc::{CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_SPACE, SCM_RIGHTS, SOL_SOCKET};
848
849 use crate::io::IoSliceMut;
850 use crate::sys::cvt_r;
851
852 unsafe {
853 const SCM_MSG_LEN: usize = mem::size_of::<[c_int; 1]>();
854
855 #[repr(C)]
856 union Cmsg {
857 _buf: [u8; unsafe { CMSG_SPACE(SCM_MSG_LEN as u32) as usize }],
858 _align: libc::cmsghdr,
859 }
860 let mut cmsg: Cmsg = mem::zeroed();
861 let mut iov = [IoSliceMut::new(&mut [])];
863
864 let mut msg: libc::msghdr = mem::zeroed();
865
866 msg.msg_iov = (&raw mut iov) as *mut _;
867 msg.msg_iovlen = 1;
868 msg.msg_controllen = mem::size_of::<Cmsg>() as _;
869 msg.msg_control = (&raw mut cmsg) as *mut _;
870
871 match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, libc::MSG_CMSG_CLOEXEC)) {
872 Err(_) => return -1,
873 Ok(_) => {}
874 }
875
876 let hdr = CMSG_FIRSTHDR((&raw mut msg) as *mut _);
877 if hdr.is_null()
878 || (*hdr).cmsg_level != SOL_SOCKET
879 || (*hdr).cmsg_type != SCM_RIGHTS
880 || (*hdr).cmsg_len != CMSG_LEN(SCM_MSG_LEN as _) as _
881 {
882 return -1;
883 }
884 let data = CMSG_DATA(hdr);
885
886 let mut fds = [-1 as c_int];
887
888 crate::ptr::copy_nonoverlapping(
889 data as *const _,
890 fds.as_mut_ptr().cast::<u8>(),
891 SCM_MSG_LEN,
892 );
893
894 fds[0]
895 }
896 }
897}
898
899pub struct Process {
905 pid: pid_t,
906 status: Option<ExitStatus>,
907 #[cfg(target_os = "linux")]
912 pidfd: Option<PidFd>,
913}
914
915impl Process {
916 #[cfg(target_os = "linux")]
917 unsafe fn new(pid: pid_t, pidfd: pid_t) -> Self {
924 use crate::os::unix::io::FromRawFd;
925 use crate::sys_common::FromInner;
926 let pidfd = (pidfd >= 0).then(|| PidFd::from_inner(sys::fd::FileDesc::from_raw_fd(pidfd)));
928 Process { pid, status: None, pidfd }
929 }
930
931 #[cfg(not(target_os = "linux"))]
932 unsafe fn new(pid: pid_t, _pidfd: pid_t) -> Self {
933 Process { pid, status: None }
934 }
935
936 pub fn id(&self) -> u32 {
937 self.pid as u32
938 }
939
940 pub fn kill(&mut self) -> io::Result<()> {
941 if self.status.is_some() {
945 return Ok(());
946 }
947 #[cfg(target_os = "linux")]
948 if let Some(pid_fd) = self.pidfd.as_ref() {
949 return pid_fd.kill();
951 }
952 cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)
953 }
954
955 pub fn wait(&mut self) -> io::Result<ExitStatus> {
956 use crate::sys::cvt_r;
957 if let Some(status) = self.status {
958 return Ok(status);
959 }
960 #[cfg(target_os = "linux")]
961 if let Some(pid_fd) = self.pidfd.as_ref() {
962 let status = pid_fd.wait()?;
963 self.status = Some(status);
964 return Ok(status);
965 }
966 let mut status = 0 as c_int;
967 cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
968 self.status = Some(ExitStatus::new(status));
969 Ok(ExitStatus::new(status))
970 }
971
972 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
973 if let Some(status) = self.status {
974 return Ok(Some(status));
975 }
976 #[cfg(target_os = "linux")]
977 if let Some(pid_fd) = self.pidfd.as_ref() {
978 let status = pid_fd.try_wait()?;
979 if let Some(status) = status {
980 self.status = Some(status)
981 }
982 return Ok(status);
983 }
984 let mut status = 0 as c_int;
985 let pid = cvt(unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) })?;
986 if pid == 0 {
987 Ok(None)
988 } else {
989 self.status = Some(ExitStatus::new(status));
990 Ok(Some(ExitStatus::new(status)))
991 }
992 }
993}
994
995#[derive(PartialEq, Eq, Clone, Copy, Default)]
1000pub struct ExitStatus(c_int);
1001
1002impl fmt::Debug for ExitStatus {
1003 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1004 f.debug_tuple("unix_wait_status").field(&self.0).finish()
1005 }
1006}
1007
1008impl ExitStatus {
1009 pub fn new(status: c_int) -> ExitStatus {
1010 ExitStatus(status)
1011 }
1012
1013 #[cfg(target_os = "linux")]
1014 pub fn from_waitid_siginfo(siginfo: libc::siginfo_t) -> ExitStatus {
1015 let status = unsafe { siginfo.si_status() };
1016
1017 match siginfo.si_code {
1018 libc::CLD_EXITED => ExitStatus((status & 0xff) << 8),
1019 libc::CLD_KILLED => ExitStatus(status),
1020 libc::CLD_DUMPED => ExitStatus(status | 0x80),
1021 libc::CLD_CONTINUED => ExitStatus(0xffff),
1022 libc::CLD_STOPPED | libc::CLD_TRAPPED => ExitStatus(((status & 0xff) << 8) | 0x7f),
1023 _ => unreachable!("waitid() should only return the above codes"),
1024 }
1025 }
1026
1027 fn exited(&self) -> bool {
1028 libc::WIFEXITED(self.0)
1029 }
1030
1031 pub fn exit_ok(&self) -> Result<(), ExitStatusError> {
1032 match NonZero::try_from(self.0) {
1038 Ok(failure) => Err(ExitStatusError(failure)),
1039 Err(_) => Ok(()),
1040 }
1041 }
1042
1043 pub fn code(&self) -> Option<i32> {
1044 self.exited().then(|| libc::WEXITSTATUS(self.0))
1045 }
1046
1047 pub fn signal(&self) -> Option<i32> {
1048 libc::WIFSIGNALED(self.0).then(|| libc::WTERMSIG(self.0))
1049 }
1050
1051 pub fn core_dumped(&self) -> bool {
1052 libc::WIFSIGNALED(self.0) && libc::WCOREDUMP(self.0)
1053 }
1054
1055 pub fn stopped_signal(&self) -> Option<i32> {
1056 libc::WIFSTOPPED(self.0).then(|| libc::WSTOPSIG(self.0))
1057 }
1058
1059 pub fn continued(&self) -> bool {
1060 libc::WIFCONTINUED(self.0)
1061 }
1062
1063 pub fn into_raw(&self) -> c_int {
1064 self.0
1065 }
1066}
1067
1068impl From<c_int> for ExitStatus {
1070 fn from(a: c_int) -> ExitStatus {
1071 ExitStatus(a)
1072 }
1073}
1074
1075fn signal_string(signal: i32) -> &'static str {
1082 match signal {
1083 libc::SIGHUP => " (SIGHUP)",
1084 libc::SIGINT => " (SIGINT)",
1085 libc::SIGQUIT => " (SIGQUIT)",
1086 libc::SIGILL => " (SIGILL)",
1087 libc::SIGTRAP => " (SIGTRAP)",
1088 libc::SIGABRT => " (SIGABRT)",
1089 #[cfg(not(target_os = "l4re"))]
1090 libc::SIGBUS => " (SIGBUS)",
1091 libc::SIGFPE => " (SIGFPE)",
1092 libc::SIGKILL => " (SIGKILL)",
1093 #[cfg(not(target_os = "l4re"))]
1094 libc::SIGUSR1 => " (SIGUSR1)",
1095 libc::SIGSEGV => " (SIGSEGV)",
1096 #[cfg(not(target_os = "l4re"))]
1097 libc::SIGUSR2 => " (SIGUSR2)",
1098 libc::SIGPIPE => " (SIGPIPE)",
1099 libc::SIGALRM => " (SIGALRM)",
1100 libc::SIGTERM => " (SIGTERM)",
1101 #[cfg(not(target_os = "l4re"))]
1102 libc::SIGCHLD => " (SIGCHLD)",
1103 #[cfg(not(target_os = "l4re"))]
1104 libc::SIGCONT => " (SIGCONT)",
1105 #[cfg(not(target_os = "l4re"))]
1106 libc::SIGSTOP => " (SIGSTOP)",
1107 #[cfg(not(target_os = "l4re"))]
1108 libc::SIGTSTP => " (SIGTSTP)",
1109 #[cfg(not(target_os = "l4re"))]
1110 libc::SIGTTIN => " (SIGTTIN)",
1111 #[cfg(not(target_os = "l4re"))]
1112 libc::SIGTTOU => " (SIGTTOU)",
1113 #[cfg(not(target_os = "l4re"))]
1114 libc::SIGURG => " (SIGURG)",
1115 #[cfg(not(target_os = "l4re"))]
1116 libc::SIGXCPU => " (SIGXCPU)",
1117 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1118 libc::SIGXFSZ => " (SIGXFSZ)",
1119 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1120 libc::SIGVTALRM => " (SIGVTALRM)",
1121 #[cfg(not(target_os = "l4re"))]
1122 libc::SIGPROF => " (SIGPROF)",
1123 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1124 libc::SIGWINCH => " (SIGWINCH)",
1125 #[cfg(not(any(target_os = "haiku", target_os = "l4re")))]
1126 libc::SIGIO => " (SIGIO)",
1127 #[cfg(target_os = "haiku")]
1128 libc::SIGPOLL => " (SIGPOLL)",
1129 #[cfg(not(target_os = "l4re"))]
1130 libc::SIGSYS => " (SIGSYS)",
1131 #[cfg(all(
1133 target_os = "linux",
1134 any(
1135 target_arch = "x86_64",
1136 target_arch = "x86",
1137 target_arch = "arm",
1138 target_arch = "aarch64"
1139 )
1140 ))]
1141 libc::SIGSTKFLT => " (SIGSTKFLT)",
1142 #[cfg(any(target_os = "linux", target_os = "nto"))]
1143 libc::SIGPWR => " (SIGPWR)",
1144 #[cfg(any(
1145 target_os = "freebsd",
1146 target_os = "netbsd",
1147 target_os = "openbsd",
1148 target_os = "dragonfly",
1149 target_os = "nto",
1150 target_vendor = "apple",
1151 ))]
1152 libc::SIGEMT => " (SIGEMT)",
1153 #[cfg(any(
1154 target_os = "freebsd",
1155 target_os = "netbsd",
1156 target_os = "openbsd",
1157 target_os = "dragonfly",
1158 target_vendor = "apple",
1159 ))]
1160 libc::SIGINFO => " (SIGINFO)",
1161 #[cfg(target_os = "hurd")]
1162 libc::SIGLOST => " (SIGLOST)",
1163 #[cfg(target_os = "freebsd")]
1164 libc::SIGTHR => " (SIGTHR)",
1165 #[cfg(target_os = "freebsd")]
1166 libc::SIGLIBRT => " (SIGLIBRT)",
1167 _ => "",
1168 }
1169}
1170
1171impl fmt::Display for ExitStatus {
1172 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1173 if let Some(code) = self.code() {
1174 write!(f, "exit status: {code}")
1175 } else if let Some(signal) = self.signal() {
1176 let signal_string = signal_string(signal);
1177 if self.core_dumped() {
1178 write!(f, "signal: {signal}{signal_string} (core dumped)")
1179 } else {
1180 write!(f, "signal: {signal}{signal_string}")
1181 }
1182 } else if let Some(signal) = self.stopped_signal() {
1183 let signal_string = signal_string(signal);
1184 write!(f, "stopped (not terminated) by signal: {signal}{signal_string}")
1185 } else if self.continued() {
1186 write!(f, "continued (WIFCONTINUED)")
1187 } else {
1188 write!(f, "unrecognised wait status: {} {:#x}", self.0, self.0)
1189 }
1190 }
1191}
1192
1193#[derive(PartialEq, Eq, Clone, Copy)]
1194pub struct ExitStatusError(NonZero<c_int>);
1195
1196impl Into<ExitStatus> for ExitStatusError {
1197 fn into(self) -> ExitStatus {
1198 ExitStatus(self.0.into())
1199 }
1200}
1201
1202impl fmt::Debug for ExitStatusError {
1203 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1204 f.debug_tuple("unix_wait_status").field(&self.0).finish()
1205 }
1206}
1207
1208impl ExitStatusError {
1209 pub fn code(self) -> Option<NonZero<i32>> {
1210 ExitStatus(self.0.into()).code().map(|st| st.try_into().unwrap())
1211 }
1212}
1213
1214#[cfg(target_os = "linux")]
1215mod linux_child_ext {
1216
1217 use crate::os::linux::process as os;
1218 use crate::sys::pal::unix::ErrorKind;
1219 use crate::sys::pal::unix::linux::pidfd as imp;
1220 use crate::sys_common::FromInner;
1221 use crate::{io, mem};
1222
1223 #[unstable(feature = "linux_pidfd", issue = "82971")]
1224 impl crate::os::linux::process::ChildExt for crate::process::Child {
1225 fn pidfd(&self) -> io::Result<&os::PidFd> {
1226 self.handle
1227 .pidfd
1228 .as_ref()
1229 .map(|fd| unsafe { mem::transmute::<&imp::PidFd, &os::PidFd>(fd) })
1231 .ok_or_else(|| io::Error::new(ErrorKind::Uncategorized, "No pidfd was created."))
1232 }
1233
1234 fn into_pidfd(mut self) -> Result<os::PidFd, Self> {
1235 self.handle
1236 .pidfd
1237 .take()
1238 .map(|fd| <os::PidFd as FromInner<imp::PidFd>>::from_inner(fd))
1239 .ok_or_else(|| self)
1240 }
1241 }
1242}
1243
1244#[cfg(test)]
1245#[path = "process_unix/tests.rs"]
1246mod tests;
1247
1248#[cfg(all(test, target_os = "linux"))]
1250#[path = "process_unsupported/wait_status.rs"]
1251mod process_unsupported_wait_status;