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 #[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 #[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 #[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 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 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 #[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 #[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 #[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 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(chroot) = self.get_chroot() {
327 #[cfg(not(target_os = "fuchsia"))]
328 cvt(libc::chroot(chroot.as_ptr()))?;
329 #[cfg(target_os = "fuchsia")]
330 return Err(io::const_error!(
331 io::ErrorKind::Unsupported,
332 "chroot not supported by fuchsia"
333 ));
334 }
335 if let Some(cwd) = self.get_cwd() {
336 cvt(libc::chdir(cwd.as_ptr()))?;
337 }
338
339 if let Some(pgroup) = self.get_pgroup() {
340 cvt(libc::setpgid(0, pgroup))?;
341 }
342
343 if self.get_setsid() {
344 cvt(libc::setsid())?;
345 }
346
347 #[cfg(not(target_os = "emscripten"))]
349 {
350 if !crate::sys::pal::on_broken_pipe_flag_used() {
358 #[cfg(target_os = "android")] {
360 let mut action: libc::sigaction = mem::zeroed();
361 action.sa_sigaction = libc::SIG_DFL;
362 cvt(libc::sigaction(libc::SIGPIPE, &action, crate::ptr::null_mut()))?;
363 }
364 #[cfg(not(target_os = "android"))]
365 {
366 let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL);
367 if ret == libc::SIG_ERR {
368 return Err(io::Error::last_os_error());
369 }
370 }
371 #[cfg(target_os = "hurd")]
372 {
373 let ret = sys::signal(libc::SIGLOST, libc::SIG_DFL);
374 if ret == libc::SIG_ERR {
375 return Err(io::Error::last_os_error());
376 }
377 }
378 }
379 }
380
381 for callback in self.get_closures().iter_mut() {
382 callback()?;
383 }
384
385 let mut _reset = None;
391 if let Some(envp) = maybe_envp {
392 struct Reset(*const *const libc::c_char);
393
394 impl Drop for Reset {
395 fn drop(&mut self) {
396 unsafe {
397 *sys::env::environ() = self.0;
398 }
399 }
400 }
401
402 _reset = Some(Reset(*sys::env::environ()));
403 *sys::env::environ() = envp.as_ptr();
404 }
405
406 libc::execvp(self.get_program_cstr().as_ptr(), self.get_argv().as_ptr());
407 Err(io::Error::last_os_error())
408 }
409
410 #[cfg(any(target_os = "tvos", target_os = "watchos"))]
411 unsafe fn do_exec(
412 &mut self,
413 _stdio: ChildPipes,
414 _maybe_envp: Option<&CStringArray>,
415 ) -> Result<!, io::Error> {
416 return Err(Self::ERR_APPLE_TV_WATCH_NO_FORK_EXEC);
417 }
418
419 #[cfg(not(any(
420 target_os = "freebsd",
421 target_os = "illumos",
422 all(target_os = "linux", target_env = "gnu"),
423 all(target_os = "linux", target_env = "musl"),
424 target_os = "nto",
425 target_vendor = "apple",
426 target_os = "cygwin",
427 )))]
428 fn posix_spawn(
429 &mut self,
430 _: &ChildPipes,
431 _: Option<&CStringArray>,
432 ) -> io::Result<Option<Process>> {
433 Ok(None)
434 }
435
436 #[cfg(any(
439 target_os = "freebsd",
440 target_os = "illumos",
441 all(target_os = "linux", target_env = "gnu"),
442 all(target_os = "linux", target_env = "musl"),
443 target_os = "nto",
444 target_vendor = "apple",
445 target_os = "cygwin",
446 ))]
447 fn posix_spawn(
448 &mut self,
449 stdio: &ChildPipes,
450 envp: Option<&CStringArray>,
451 ) -> io::Result<Option<Process>> {
452 #[cfg(target_os = "linux")]
453 use core::sync::atomic::{Atomic, AtomicU8, Ordering};
454
455 use crate::mem::MaybeUninit;
456 use crate::sys::{self, cvt_nz, on_broken_pipe_flag_used};
457
458 if self.get_gid().is_some()
459 || self.get_uid().is_some()
460 || (self.env_saw_path() && !self.program_is_path())
461 || !self.get_closures().is_empty()
462 || self.get_groups().is_some()
463 || self.get_chroot().is_some()
464 {
465 return Ok(None);
466 }
467
468 cfg_if::cfg_if! {
469 if #[cfg(target_os = "linux")] {
470 use crate::sys::weak::weak;
471
472 weak!(
473 fn pidfd_spawnp(
474 pidfd: *mut libc::c_int,
475 path: *const libc::c_char,
476 file_actions: *const libc::posix_spawn_file_actions_t,
477 attrp: *const libc::posix_spawnattr_t,
478 argv: *const *mut libc::c_char,
479 envp: *const *mut libc::c_char,
480 ) -> libc::c_int;
481 );
482
483 weak!(
484 fn pidfd_getpid(pidfd: libc::c_int) -> libc::c_int;
485 );
486
487 static PIDFD_SUPPORTED: Atomic<u8> = AtomicU8::new(0);
488 const UNKNOWN: u8 = 0;
489 const SPAWN: u8 = 1;
490 const FORK_EXEC: u8 = 2;
492 const NO: u8 = 3;
495
496 if self.get_create_pidfd() {
497 let mut support = PIDFD_SUPPORTED.load(Ordering::Relaxed);
498 if support == FORK_EXEC {
499 return Ok(None);
500 }
501 if support == UNKNOWN {
502 support = NO;
503 let our_pid = crate::process::id();
504 let pidfd = cvt(unsafe { libc::syscall(libc::SYS_pidfd_open, our_pid, 0) } as c_int);
505 match pidfd {
506 Ok(pidfd) => {
507 support = FORK_EXEC;
508 if let Some(Ok(pid)) = pidfd_getpid.get().map(|f| cvt(unsafe { f(pidfd) } as i32)) {
509 if pidfd_spawnp.get().is_some() && pid as u32 == our_pid {
510 support = SPAWN
511 }
512 }
513 unsafe { libc::close(pidfd) };
514 }
515 Err(e) if e.raw_os_error() == Some(libc::EMFILE) => {
516 return Err(e)
519 }
520 _ => {}
521 }
522 PIDFD_SUPPORTED.store(support, Ordering::Relaxed);
523 if support == FORK_EXEC {
524 return Ok(None);
525 }
526 }
527 core::assert_matches::debug_assert_matches!(support, SPAWN | NO);
528 }
529 } else {
530 if self.get_create_pidfd() {
531 unreachable!("only implemented on linux")
532 }
533 }
534 }
535
536 #[cfg(all(target_os = "linux", target_env = "gnu"))]
538 {
539 if let Some(version) = sys::os::glibc_version() {
540 if version < (2, 24) {
541 return Ok(None);
542 }
543 } else {
544 return Ok(None);
545 }
546 }
547
548 #[cfg(target_os = "nto")]
553 unsafe fn retrying_libc_posix_spawnp(
554 pid: *mut pid_t,
555 file: *const c_char,
556 file_actions: *const posix_spawn_file_actions_t,
557 attrp: *const posix_spawnattr_t,
558 argv: *const *mut c_char,
559 envp: *const *mut c_char,
560 ) -> io::Result<i32> {
561 let mut delay = MIN_FORKSPAWN_SLEEP;
562 loop {
563 match libc::posix_spawnp(pid, file, file_actions, attrp, argv, envp) {
564 libc::EBADF => {
565 if delay < get_clock_resolution() {
566 thread::yield_now();
569 } else if delay < MAX_FORKSPAWN_SLEEP {
570 thread::sleep(delay);
571 } else {
572 return Err(io::const_error!(
573 ErrorKind::WouldBlock,
574 "posix_spawnp returned EBADF too often",
575 ));
576 }
577 delay *= 2;
578 continue;
579 }
580 r => {
581 return Ok(r);
582 }
583 }
584 }
585 }
586
587 type PosixSpawnAddChdirFn = unsafe extern "C" fn(
588 *mut libc::posix_spawn_file_actions_t,
589 *const libc::c_char,
590 ) -> libc::c_int;
591
592 #[cfg(not(any(all(target_os = "linux", target_env = "musl"), target_os = "cygwin")))]
599 fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
600 use crate::sys::weak::weak;
601
602 weak!(
607 fn posix_spawn_file_actions_addchdir_np(
608 file_actions: *mut libc::posix_spawn_file_actions_t,
609 path: *const libc::c_char,
610 ) -> libc::c_int;
611 );
612
613 weak!(
614 fn posix_spawn_file_actions_addchdir(
615 file_actions: *mut libc::posix_spawn_file_actions_t,
616 path: *const libc::c_char,
617 ) -> libc::c_int;
618 );
619
620 posix_spawn_file_actions_addchdir_np
621 .get()
622 .or_else(|| posix_spawn_file_actions_addchdir.get())
623 }
624
625 #[cfg(any(all(target_os = "linux", target_env = "musl"), target_os = "cygwin"))]
635 fn get_posix_spawn_addchdir() -> Option<PosixSpawnAddChdirFn> {
636 Some(libc::posix_spawn_file_actions_addchdir_np)
638 }
639
640 let addchdir = match self.get_cwd() {
641 Some(cwd) => {
642 if cfg!(target_vendor = "apple") {
643 if self.get_program_kind() == ProgramKind::Relative {
649 return Ok(None);
650 }
651 }
652 match get_posix_spawn_addchdir() {
656 Some(f) => Some((f, cwd)),
657 None => return Ok(None),
658 }
659 }
660 None => None,
661 };
662
663 let pgroup = self.get_pgroup();
664
665 struct PosixSpawnFileActions<'a>(&'a mut MaybeUninit<libc::posix_spawn_file_actions_t>);
666
667 impl Drop for PosixSpawnFileActions<'_> {
668 fn drop(&mut self) {
669 unsafe {
670 libc::posix_spawn_file_actions_destroy(self.0.as_mut_ptr());
671 }
672 }
673 }
674
675 struct PosixSpawnattr<'a>(&'a mut MaybeUninit<libc::posix_spawnattr_t>);
676
677 impl Drop for PosixSpawnattr<'_> {
678 fn drop(&mut self) {
679 unsafe {
680 libc::posix_spawnattr_destroy(self.0.as_mut_ptr());
681 }
682 }
683 }
684
685 unsafe {
686 let mut attrs = MaybeUninit::uninit();
687 cvt_nz(libc::posix_spawnattr_init(attrs.as_mut_ptr()))?;
688 let attrs = PosixSpawnattr(&mut attrs);
689
690 let mut flags = 0;
691
692 let mut file_actions = MaybeUninit::uninit();
693 cvt_nz(libc::posix_spawn_file_actions_init(file_actions.as_mut_ptr()))?;
694 let file_actions = PosixSpawnFileActions(&mut file_actions);
695
696 if let Some(fd) = stdio.stdin.fd() {
697 cvt_nz(libc::posix_spawn_file_actions_adddup2(
698 file_actions.0.as_mut_ptr(),
699 fd,
700 libc::STDIN_FILENO,
701 ))?;
702 }
703 if let Some(fd) = stdio.stdout.fd() {
704 cvt_nz(libc::posix_spawn_file_actions_adddup2(
705 file_actions.0.as_mut_ptr(),
706 fd,
707 libc::STDOUT_FILENO,
708 ))?;
709 }
710 if let Some(fd) = stdio.stderr.fd() {
711 cvt_nz(libc::posix_spawn_file_actions_adddup2(
712 file_actions.0.as_mut_ptr(),
713 fd,
714 libc::STDERR_FILENO,
715 ))?;
716 }
717 if let Some((f, cwd)) = addchdir {
718 cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
719 }
720
721 if let Some(pgroup) = pgroup {
722 flags |= libc::POSIX_SPAWN_SETPGROUP;
723 cvt_nz(libc::posix_spawnattr_setpgroup(attrs.0.as_mut_ptr(), pgroup))?;
724 }
725
726 if !on_broken_pipe_flag_used() {
734 let mut default_set = MaybeUninit::<libc::sigset_t>::uninit();
735 cvt(sigemptyset(default_set.as_mut_ptr()))?;
736 cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGPIPE))?;
737 #[cfg(target_os = "hurd")]
738 {
739 cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGLOST))?;
740 }
741 cvt_nz(libc::posix_spawnattr_setsigdefault(
742 attrs.0.as_mut_ptr(),
743 default_set.as_ptr(),
744 ))?;
745 flags |= libc::POSIX_SPAWN_SETSIGDEF;
746 }
747
748 if self.get_setsid() {
749 cfg_if::cfg_if! {
750 if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
751 flags |= libc::POSIX_SPAWN_SETSID;
752 } else {
753 return Ok(None);
754 }
755 }
756 }
757
758 cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
759
760 let _env_lock = sys::env::env_read_lock();
762 let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::env::environ() as *const _);
763
764 #[cfg(not(target_os = "nto"))]
765 let spawn_fn = libc::posix_spawnp;
766 #[cfg(target_os = "nto")]
767 let spawn_fn = retrying_libc_posix_spawnp;
768
769 #[cfg(target_os = "linux")]
770 if self.get_create_pidfd() && PIDFD_SUPPORTED.load(Ordering::Relaxed) == SPAWN {
771 let mut pidfd: libc::c_int = -1;
772 let spawn_res = pidfd_spawnp.get().unwrap()(
773 &mut pidfd,
774 self.get_program_cstr().as_ptr(),
775 file_actions.0.as_ptr(),
776 attrs.0.as_ptr(),
777 self.get_argv().as_ptr() as *const _,
778 envp as *const _,
779 );
780
781 let spawn_res = cvt_nz(spawn_res);
782 if let Err(ref e) = spawn_res
783 && e.raw_os_error() == Some(libc::ENOSYS)
784 {
785 PIDFD_SUPPORTED.store(FORK_EXEC, Ordering::Relaxed);
786 return Ok(None);
787 }
788 spawn_res?;
789
790 let pid = match cvt(pidfd_getpid.get().unwrap()(pidfd)) {
791 Ok(pid) => pid,
792 Err(e) => {
793 libc::close(pidfd);
797 return Err(Error::new(
798 e.kind(),
799 "pidfd_spawnp succeeded but the child's PID could not be obtained",
800 ));
801 }
802 };
803
804 return Ok(Some(Process::new(pid, pidfd)));
805 }
806
807 let mut p = Process::new(0, -1);
809
810 let spawn_res = spawn_fn(
811 &mut p.pid,
812 self.get_program_cstr().as_ptr(),
813 file_actions.0.as_ptr(),
814 attrs.0.as_ptr(),
815 self.get_argv().as_ptr() as *const _,
816 envp as *const _,
817 );
818
819 #[cfg(target_os = "nto")]
820 let spawn_res = spawn_res?;
821
822 cvt_nz(spawn_res)?;
823 Ok(Some(p))
824 }
825 }
826
827 #[cfg(target_os = "linux")]
828 fn send_pidfd(&self, sock: &crate::sys::net::Socket) {
829 use libc::{CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_SPACE, SCM_RIGHTS, SOL_SOCKET};
830
831 use crate::io::IoSlice;
832 use crate::os::fd::RawFd;
833 use crate::sys::cvt_r;
834
835 unsafe {
836 let child_pid = libc::getpid();
837 let pidfd = libc::syscall(libc::SYS_pidfd_open, child_pid, 0);
839
840 let fds: [c_int; 1] = [pidfd as RawFd];
841
842 const SCM_MSG_LEN: usize = size_of::<[c_int; 1]>();
843
844 #[repr(C)]
845 union Cmsg {
846 buf: [u8; unsafe { CMSG_SPACE(SCM_MSG_LEN as u32) as usize }],
847 _align: libc::cmsghdr,
848 }
849
850 let mut cmsg: Cmsg = mem::zeroed();
851
852 let mut iov = [IoSlice::new(b"")];
854 let mut msg: libc::msghdr = mem::zeroed();
855
856 msg.msg_iov = (&raw mut iov) as *mut _;
857 msg.msg_iovlen = 1;
858
859 if pidfd >= 0 {
861 msg.msg_controllen = size_of_val(&cmsg.buf) as _;
862 msg.msg_control = (&raw mut cmsg.buf) as *mut _;
863
864 let hdr = CMSG_FIRSTHDR((&raw mut msg) as *mut _);
865 (*hdr).cmsg_level = SOL_SOCKET;
866 (*hdr).cmsg_type = SCM_RIGHTS;
867 (*hdr).cmsg_len = CMSG_LEN(SCM_MSG_LEN as _) as _;
868 let data = CMSG_DATA(hdr);
869 crate::ptr::copy_nonoverlapping(
870 fds.as_ptr().cast::<u8>(),
871 data as *mut _,
872 SCM_MSG_LEN,
873 );
874 }
875
876 match cvt_r(|| libc::sendmsg(sock.as_raw(), &msg, 0)) {
879 Ok(0) => {}
880 other => rtabort!("failed to communicate with parent process. {:?}", other),
881 }
882 }
883 }
884
885 #[cfg(target_os = "linux")]
886 fn recv_pidfd(&self, sock: &crate::sys::net::Socket) -> pid_t {
887 use libc::{CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_SPACE, SCM_RIGHTS, SOL_SOCKET};
888
889 use crate::io::IoSliceMut;
890 use crate::sys::cvt_r;
891
892 unsafe {
893 const SCM_MSG_LEN: usize = size_of::<[c_int; 1]>();
894
895 #[repr(C)]
896 union Cmsg {
897 _buf: [u8; unsafe { CMSG_SPACE(SCM_MSG_LEN as u32) as usize }],
898 _align: libc::cmsghdr,
899 }
900 let mut cmsg: Cmsg = mem::zeroed();
901 let mut iov = [IoSliceMut::new(&mut [])];
903
904 let mut msg: libc::msghdr = mem::zeroed();
905
906 msg.msg_iov = (&raw mut iov) as *mut _;
907 msg.msg_iovlen = 1;
908 msg.msg_controllen = size_of::<Cmsg>() as _;
909 msg.msg_control = (&raw mut cmsg) as *mut _;
910
911 match cvt_r(|| libc::recvmsg(sock.as_raw(), &mut msg, libc::MSG_CMSG_CLOEXEC)) {
912 Err(_) => return -1,
913 Ok(_) => {}
914 }
915
916 let hdr = CMSG_FIRSTHDR((&raw mut msg) as *mut _);
917 if hdr.is_null()
918 || (*hdr).cmsg_level != SOL_SOCKET
919 || (*hdr).cmsg_type != SCM_RIGHTS
920 || (*hdr).cmsg_len != CMSG_LEN(SCM_MSG_LEN as _) as _
921 {
922 return -1;
923 }
924 let data = CMSG_DATA(hdr);
925
926 let mut fds = [-1 as c_int];
927
928 crate::ptr::copy_nonoverlapping(
929 data as *const _,
930 fds.as_mut_ptr().cast::<u8>(),
931 SCM_MSG_LEN,
932 );
933
934 fds[0]
935 }
936 }
937}
938
939pub struct Process {
945 pid: pid_t,
946 status: Option<ExitStatus>,
947 #[cfg(target_os = "linux")]
952 pidfd: Option<PidFd>,
953}
954
955impl Process {
956 #[cfg(target_os = "linux")]
957 unsafe fn new(pid: pid_t, pidfd: pid_t) -> Self {
964 use crate::os::unix::io::FromRawFd;
965 use crate::sys_common::FromInner;
966 let pidfd = (pidfd >= 0).then(|| PidFd::from_inner(sys::fd::FileDesc::from_raw_fd(pidfd)));
968 Process { pid, status: None, pidfd }
969 }
970
971 #[cfg(not(target_os = "linux"))]
972 unsafe fn new(pid: pid_t, _pidfd: pid_t) -> Self {
973 Process { pid, status: None }
974 }
975
976 pub fn id(&self) -> u32 {
977 self.pid as u32
978 }
979
980 pub fn kill(&self) -> io::Result<()> {
981 self.send_signal(libc::SIGKILL)
982 }
983
984 pub(crate) fn send_signal(&self, signal: i32) -> io::Result<()> {
985 if self.status.is_some() {
989 return Ok(());
990 }
991 #[cfg(target_os = "linux")]
992 if let Some(pid_fd) = self.pidfd.as_ref() {
993 return pid_fd.send_signal(signal);
995 }
996 cvt(unsafe { libc::kill(self.pid, signal) }).map(drop)
997 }
998
999 pub fn wait(&mut self) -> io::Result<ExitStatus> {
1000 use crate::sys::cvt_r;
1001 if let Some(status) = self.status {
1002 return Ok(status);
1003 }
1004 #[cfg(target_os = "linux")]
1005 if let Some(pid_fd) = self.pidfd.as_ref() {
1006 let status = pid_fd.wait()?;
1007 self.status = Some(status);
1008 return Ok(status);
1009 }
1010 let mut status = 0 as c_int;
1011 cvt_r(|| unsafe { libc::waitpid(self.pid, &mut status, 0) })?;
1012 self.status = Some(ExitStatus::new(status));
1013 Ok(ExitStatus::new(status))
1014 }
1015
1016 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
1017 if let Some(status) = self.status {
1018 return Ok(Some(status));
1019 }
1020 #[cfg(target_os = "linux")]
1021 if let Some(pid_fd) = self.pidfd.as_ref() {
1022 let status = pid_fd.try_wait()?;
1023 if let Some(status) = status {
1024 self.status = Some(status)
1025 }
1026 return Ok(status);
1027 }
1028 let mut status = 0 as c_int;
1029 let pid = cvt(unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) })?;
1030 if pid == 0 {
1031 Ok(None)
1032 } else {
1033 self.status = Some(ExitStatus::new(status));
1034 Ok(Some(ExitStatus::new(status)))
1035 }
1036 }
1037}
1038
1039#[derive(PartialEq, Eq, Clone, Copy, Default)]
1044pub struct ExitStatus(c_int);
1045
1046impl fmt::Debug for ExitStatus {
1047 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1048 f.debug_tuple("unix_wait_status").field(&self.0).finish()
1049 }
1050}
1051
1052impl ExitStatus {
1053 pub fn new(status: c_int) -> ExitStatus {
1054 ExitStatus(status)
1055 }
1056
1057 #[cfg(target_os = "linux")]
1058 pub fn from_waitid_siginfo(siginfo: libc::siginfo_t) -> ExitStatus {
1059 let status = unsafe { siginfo.si_status() };
1060
1061 match siginfo.si_code {
1062 libc::CLD_EXITED => ExitStatus((status & 0xff) << 8),
1063 libc::CLD_KILLED => ExitStatus(status),
1064 libc::CLD_DUMPED => ExitStatus(status | 0x80),
1065 libc::CLD_CONTINUED => ExitStatus(0xffff),
1066 libc::CLD_STOPPED | libc::CLD_TRAPPED => ExitStatus(((status & 0xff) << 8) | 0x7f),
1067 _ => unreachable!("waitid() should only return the above codes"),
1068 }
1069 }
1070
1071 fn exited(&self) -> bool {
1072 libc::WIFEXITED(self.0)
1073 }
1074
1075 pub fn exit_ok(&self) -> Result<(), ExitStatusError> {
1076 match NonZero::try_from(self.0) {
1082 Ok(failure) => Err(ExitStatusError(failure)),
1083 Err(_) => Ok(()),
1084 }
1085 }
1086
1087 pub fn code(&self) -> Option<i32> {
1088 self.exited().then(|| libc::WEXITSTATUS(self.0))
1089 }
1090
1091 pub fn signal(&self) -> Option<i32> {
1092 libc::WIFSIGNALED(self.0).then(|| libc::WTERMSIG(self.0))
1093 }
1094
1095 pub fn core_dumped(&self) -> bool {
1096 libc::WIFSIGNALED(self.0) && libc::WCOREDUMP(self.0)
1097 }
1098
1099 pub fn stopped_signal(&self) -> Option<i32> {
1100 libc::WIFSTOPPED(self.0).then(|| libc::WSTOPSIG(self.0))
1101 }
1102
1103 pub fn continued(&self) -> bool {
1104 libc::WIFCONTINUED(self.0)
1105 }
1106
1107 pub fn into_raw(&self) -> c_int {
1108 self.0
1109 }
1110}
1111
1112impl From<c_int> for ExitStatus {
1114 fn from(a: c_int) -> ExitStatus {
1115 ExitStatus(a)
1116 }
1117}
1118
1119fn signal_string(signal: i32) -> &'static str {
1126 match signal {
1127 libc::SIGHUP => " (SIGHUP)",
1128 libc::SIGINT => " (SIGINT)",
1129 libc::SIGQUIT => " (SIGQUIT)",
1130 libc::SIGILL => " (SIGILL)",
1131 libc::SIGTRAP => " (SIGTRAP)",
1132 libc::SIGABRT => " (SIGABRT)",
1133 #[cfg(not(target_os = "l4re"))]
1134 libc::SIGBUS => " (SIGBUS)",
1135 libc::SIGFPE => " (SIGFPE)",
1136 libc::SIGKILL => " (SIGKILL)",
1137 #[cfg(not(target_os = "l4re"))]
1138 libc::SIGUSR1 => " (SIGUSR1)",
1139 libc::SIGSEGV => " (SIGSEGV)",
1140 #[cfg(not(target_os = "l4re"))]
1141 libc::SIGUSR2 => " (SIGUSR2)",
1142 libc::SIGPIPE => " (SIGPIPE)",
1143 libc::SIGALRM => " (SIGALRM)",
1144 libc::SIGTERM => " (SIGTERM)",
1145 #[cfg(not(target_os = "l4re"))]
1146 libc::SIGCHLD => " (SIGCHLD)",
1147 #[cfg(not(target_os = "l4re"))]
1148 libc::SIGCONT => " (SIGCONT)",
1149 #[cfg(not(target_os = "l4re"))]
1150 libc::SIGSTOP => " (SIGSTOP)",
1151 #[cfg(not(target_os = "l4re"))]
1152 libc::SIGTSTP => " (SIGTSTP)",
1153 #[cfg(not(target_os = "l4re"))]
1154 libc::SIGTTIN => " (SIGTTIN)",
1155 #[cfg(not(target_os = "l4re"))]
1156 libc::SIGTTOU => " (SIGTTOU)",
1157 #[cfg(not(target_os = "l4re"))]
1158 libc::SIGURG => " (SIGURG)",
1159 #[cfg(not(target_os = "l4re"))]
1160 libc::SIGXCPU => " (SIGXCPU)",
1161 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1162 libc::SIGXFSZ => " (SIGXFSZ)",
1163 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1164 libc::SIGVTALRM => " (SIGVTALRM)",
1165 #[cfg(not(target_os = "l4re"))]
1166 libc::SIGPROF => " (SIGPROF)",
1167 #[cfg(not(any(target_os = "l4re", target_os = "rtems")))]
1168 libc::SIGWINCH => " (SIGWINCH)",
1169 #[cfg(not(any(target_os = "haiku", target_os = "l4re")))]
1170 libc::SIGIO => " (SIGIO)",
1171 #[cfg(target_os = "haiku")]
1172 libc::SIGPOLL => " (SIGPOLL)",
1173 #[cfg(not(target_os = "l4re"))]
1174 libc::SIGSYS => " (SIGSYS)",
1175 #[cfg(all(
1177 target_os = "linux",
1178 any(
1179 target_arch = "x86_64",
1180 target_arch = "x86",
1181 target_arch = "arm",
1182 target_arch = "aarch64"
1183 )
1184 ))]
1185 libc::SIGSTKFLT => " (SIGSTKFLT)",
1186 #[cfg(any(target_os = "linux", target_os = "nto", target_os = "cygwin"))]
1187 libc::SIGPWR => " (SIGPWR)",
1188 #[cfg(any(
1189 target_os = "freebsd",
1190 target_os = "netbsd",
1191 target_os = "openbsd",
1192 target_os = "dragonfly",
1193 target_os = "nto",
1194 target_vendor = "apple",
1195 target_os = "cygwin",
1196 ))]
1197 libc::SIGEMT => " (SIGEMT)",
1198 #[cfg(any(
1199 target_os = "freebsd",
1200 target_os = "netbsd",
1201 target_os = "openbsd",
1202 target_os = "dragonfly",
1203 target_vendor = "apple",
1204 ))]
1205 libc::SIGINFO => " (SIGINFO)",
1206 #[cfg(target_os = "hurd")]
1207 libc::SIGLOST => " (SIGLOST)",
1208 #[cfg(target_os = "freebsd")]
1209 libc::SIGTHR => " (SIGTHR)",
1210 #[cfg(target_os = "freebsd")]
1211 libc::SIGLIBRT => " (SIGLIBRT)",
1212 _ => "",
1213 }
1214}
1215
1216impl fmt::Display for ExitStatus {
1217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1218 if let Some(code) = self.code() {
1219 write!(f, "exit status: {code}")
1220 } else if let Some(signal) = self.signal() {
1221 let signal_string = signal_string(signal);
1222 if self.core_dumped() {
1223 write!(f, "signal: {signal}{signal_string} (core dumped)")
1224 } else {
1225 write!(f, "signal: {signal}{signal_string}")
1226 }
1227 } else if let Some(signal) = self.stopped_signal() {
1228 let signal_string = signal_string(signal);
1229 write!(f, "stopped (not terminated) by signal: {signal}{signal_string}")
1230 } else if self.continued() {
1231 write!(f, "continued (WIFCONTINUED)")
1232 } else {
1233 write!(f, "unrecognised wait status: {} {:#x}", self.0, self.0)
1234 }
1235 }
1236}
1237
1238#[derive(PartialEq, Eq, Clone, Copy)]
1239pub struct ExitStatusError(NonZero<c_int>);
1240
1241impl Into<ExitStatus> for ExitStatusError {
1242 fn into(self) -> ExitStatus {
1243 ExitStatus(self.0.into())
1244 }
1245}
1246
1247impl fmt::Debug for ExitStatusError {
1248 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1249 f.debug_tuple("unix_wait_status").field(&self.0).finish()
1250 }
1251}
1252
1253impl ExitStatusError {
1254 pub fn code(self) -> Option<NonZero<i32>> {
1255 ExitStatus(self.0.into()).code().map(|st| st.try_into().unwrap())
1256 }
1257}
1258
1259#[cfg(target_os = "linux")]
1260mod linux_child_ext {
1261 use crate::io::ErrorKind;
1262 use crate::os::linux::process as os;
1263 use crate::sys::pal::linux::pidfd as imp;
1264 use crate::sys_common::FromInner;
1265 use crate::{io, mem};
1266
1267 #[unstable(feature = "linux_pidfd", issue = "82971")]
1268 impl crate::os::linux::process::ChildExt for crate::process::Child {
1269 fn pidfd(&self) -> io::Result<&os::PidFd> {
1270 self.handle
1271 .pidfd
1272 .as_ref()
1273 .map(|fd| unsafe { mem::transmute::<&imp::PidFd, &os::PidFd>(fd) })
1275 .ok_or_else(|| io::const_error!(ErrorKind::Uncategorized, "no pidfd was created."))
1276 }
1277
1278 fn into_pidfd(mut self) -> Result<os::PidFd, Self> {
1279 self.handle
1280 .pidfd
1281 .take()
1282 .map(|fd| <os::PidFd as FromInner<imp::PidFd>>::from_inner(fd))
1283 .ok_or_else(|| self)
1284 }
1285 }
1286}
1287
1288#[cfg(test)]
1289mod tests;
1290
1291#[cfg(all(test, target_os = "linux"))]
1293#[path = "unsupported/wait_status.rs"]
1294mod unsupported_wait_status;