1use std::ffi::CString;
13use std::mem::MaybeUninit;
14use std::os::unix::io::RawFd;
15use std::ptr;
16
17use crate::CoreError;
18use crate::error::{posix_ret, syscall_ret};
19use crate::reactor::Fd;
20use crate::signal::SignalRuntime;
21use libc::{
22 O_CLOEXEC, O_NONBLOCK, WEXITSTATUS, WIFEXITED, WIFSIGNALED, WTERMSIG, c_char, pid_t, pipe2,
23 waitpid,
24};
25
26unsafe extern "C" {
27 pub(crate) static mut environ: *mut *mut libc::c_char;
28}
29
30pub(crate) const POSIX_SPAWN_SETPGROUP: i32 = 2;
31pub(crate) const POSIX_SPAWN_SETSIGDEF: i32 = 4;
32pub(crate) const POSIX_SPAWN_SETSIGMASK: i32 = 8;
33
34unsafe extern "C" {
35 pub(crate) fn posix_spawn(
36 pid: *mut libc::pid_t,
37 path: *const libc::c_char,
38 file_actions: *const libc::posix_spawn_file_actions_t,
39 attrp: *const libc::posix_spawnattr_t,
40 argv: *const *mut libc::c_char,
41 envp: *const *mut libc::c_char,
42 ) -> libc::c_int;
43
44 pub(crate) fn posix_spawn_file_actions_addclose(
45 file_actions: *mut libc::posix_spawn_file_actions_t,
46 fd: libc::c_int,
47 ) -> libc::c_int;
48
49 pub(crate) fn posix_spawn_file_actions_adddup2(
50 file_actions: *mut libc::posix_spawn_file_actions_t,
51 fd: libc::c_int,
52 newfd: libc::c_int,
53 ) -> libc::c_int;
54
55 pub(crate) fn posix_spawn_file_actions_destroy(
56 file_actions: *mut libc::posix_spawn_file_actions_t,
57 ) -> libc::c_int;
58
59 pub(crate) fn posix_spawn_file_actions_init(
60 file_actions: *mut libc::posix_spawn_file_actions_t,
61 ) -> libc::c_int;
62
63 pub(crate) fn posix_spawnattr_destroy(attr: *mut libc::posix_spawnattr_t) -> libc::c_int;
64
65 pub(crate) fn posix_spawnattr_init(attr: *mut libc::posix_spawnattr_t) -> libc::c_int;
66
67 pub(crate) fn posix_spawnattr_setflags(
68 attr: *mut libc::posix_spawnattr_t,
69 flags: libc::c_short,
70 ) -> libc::c_int;
71
72 pub(crate) fn posix_spawnattr_setpgroup(
73 attr: *mut libc::posix_spawnattr_t,
74 pgroup: libc::pid_t,
75 ) -> libc::c_int;
76
77 pub(crate) fn posix_spawnattr_setsigdefault(
78 attr: *mut libc::posix_spawnattr_t,
79 sigdefault: *const libc::sigset_t,
80 ) -> libc::c_int;
81
82 pub(crate) fn posix_spawnattr_setsigmask(
83 attr: *mut libc::posix_spawnattr_t,
84 sigmask: *const libc::sigset_t,
85 ) -> libc::c_int;
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
90pub enum CancelPolicy {
91 #[default]
93 None,
94 Graceful,
96 Kill,
98}
99
100#[derive(Debug, Clone, Copy, Default)]
102pub struct ProcessGroup {
103 pub leader: Option<pid_t>,
105 pub isolated: bool,
107}
108
109impl ProcessGroup {
110 pub fn new(leader: Option<pid_t>, isolated: bool) -> Self {
112 Self { leader, isolated }
113 }
114}
115
116#[inline(always)]
117fn errno() -> i32 {
118 std::io::Error::last_os_error().raw_os_error().unwrap_or(0)
119}
120
121#[inline(always)]
124fn make_pipe() -> Result<(Fd, Fd), CoreError> {
125 let mut fds = [0; 2];
126 let r = unsafe { pipe2(fds.as_mut_ptr(), O_CLOEXEC | O_NONBLOCK) };
127 syscall_ret(r, "pipe2")?;
128 Ok((Fd::new(fds[0], "pipe2")?, Fd::new(fds[1], "pipe2")?))
129}
130
131fn make_cloexec_pipe() -> Result<(RawFd, RawFd), CoreError> {
132 let mut fds = [0; 2];
133 let r = unsafe { pipe2(fds.as_mut_ptr(), O_CLOEXEC) };
134 syscall_ret(r, "pipe2")?;
135 Ok((fds[0], fds[1]))
136}
137
138#[repr(u8)]
139#[derive(Clone, Copy)]
140enum ChildSetupOp {
141 DupStdin = 1,
142 DupStdout = 2,
143 DupStderr = 3,
144 Setsid = 4,
145 Chdir = 5,
146 Setpgid = 6,
147 SignalMask = 7,
148 Execve = 8,
149}
150
151impl ChildSetupOp {
152 fn as_str(self) -> &'static str {
153 match self {
154 Self::DupStdin => "fork child dup2 stdin",
155 Self::DupStdout => "fork child dup2 stdout",
156 Self::DupStderr => "fork child dup2 stderr",
157 Self::Setsid => "fork child setsid",
158 Self::Chdir => "fork child chdir",
159 Self::Setpgid => "fork child setpgid",
160 Self::SignalMask => "fork child signal setup",
161 Self::Execve => "fork child execve",
162 }
163 }
164
165 fn from_u8(value: u8) -> Self {
166 match value {
167 1 => Self::DupStdin,
168 2 => Self::DupStdout,
169 3 => Self::DupStderr,
170 4 => Self::Setsid,
171 5 => Self::Chdir,
172 6 => Self::Setpgid,
173 7 => Self::SignalMask,
174 _ => Self::Execve,
175 }
176 }
177}
178
179unsafe fn report_child_setup_error(fd: RawFd, op: ChildSetupOp, code: i32) -> ! {
180 let mut msg = [0u8; 5];
181 msg[..4].copy_from_slice(&code.to_ne_bytes());
182 msg[4] = op as u8;
183 let mut written = 0;
184 while written < msg.len() {
185 let n = unsafe {
186 libc::write(
187 fd,
188 msg[written..].as_ptr().cast::<libc::c_void>(),
189 msg.len() - written,
190 )
191 };
192 if n <= 0 {
193 break;
194 }
195 written += n as usize;
196 }
197 unsafe {
198 libc::_exit(127);
199 }
200}
201
202fn read_child_setup_error(fd: RawFd) -> Result<Option<CoreError>, CoreError> {
203 let mut msg = [0u8; 5];
204 let mut read_len = 0;
205 loop {
206 let n = unsafe {
207 libc::read(
208 fd,
209 msg[read_len..].as_mut_ptr().cast::<libc::c_void>(),
210 msg.len() - read_len,
211 )
212 };
213 if n == 0 {
214 return Ok(None);
215 }
216 if n < 0 {
217 let code = errno();
218 if code == libc::EINTR {
219 continue;
220 }
221 return Err(CoreError::sys(code, "read fork child setup error"));
222 }
223 read_len += n as usize;
224 if read_len == msg.len() {
225 let code = i32::from_ne_bytes([msg[0], msg[1], msg[2], msg[3]]);
226 return Ok(Some(CoreError::sys(
227 code,
228 ChildSetupOp::from_u8(msg[4]).as_str(),
229 )));
230 }
231 }
232}
233
234struct Pipes {
235 stdin_r: Option<Fd>,
236 stdin_w: Option<Fd>,
237 stdout_r: Option<Fd>,
238 stdout_w: Option<Fd>,
239 stderr_r: Option<Fd>,
240 stderr_w: Option<Fd>,
241}
242
243impl Pipes {
244 fn new(in_buf: Option<&[u8]>, out: bool, err: bool) -> Result<Self, CoreError> {
245 let (stdin_r, stdin_w) = if in_buf.is_some() {
246 let (r, w) = make_pipe()?;
247 (Some(r), Some(w))
248 } else {
249 (None, None)
250 };
251
252 let (stdout_r, stdout_w) = if out {
253 let (r, w) = make_pipe()?;
254 (Some(r), Some(w))
255 } else {
256 (None, None)
257 };
258
259 let (stderr_r, stderr_w) = if err {
260 let (r, w) = make_pipe()?;
261 (Some(r), Some(w))
262 } else {
263 (None, None)
264 };
265
266 Ok(Self {
267 stdin_r,
268 stdin_w,
269 stdout_r,
270 stdout_w,
271 stderr_r,
272 stderr_w,
273 })
274 }
275
276 #[inline(always)]
277 fn close_all(&mut self) {
278 self.stdin_r.take();
279 self.stdin_w.take();
280 self.stdout_r.take();
281 self.stdout_w.take();
282 self.stderr_r.take();
283 self.stderr_w.take();
284 }
285}
286
287#[derive(Debug, PartialEq, Eq)]
289pub enum ExitStatus {
290 Exited(i32),
292 Signaled(i32),
294}
295
296#[derive(Debug, Clone, Copy, PartialEq, Eq)]
298pub enum SpawnBackend {
299 PosixSpawn,
301 Fork,
306}
307
308#[derive(Debug, Clone, PartialEq, Eq, Default)]
310pub enum SpawnFdPolicy {
311 #[default]
313 CloexecOnly,
314 CloseFrom3,
317 Allowlist(Vec<RawFd>),
324}
325
326#[derive(Clone)]
328enum ExecArgv {
329 Dynamic(Vec<CString>),
331}
332
333#[derive(Clone)]
335struct ExecContext {
336 argv: ExecArgv,
337 envp: Option<Vec<CString>>,
338 cwd: Option<CString>,
339}
340
341impl ExecContext {
342 fn new(
344 argv: Vec<String>,
345 env: Option<Vec<String>>,
346 cwd: Option<String>,
347 ) -> Result<Self, CoreError> {
348 if argv.is_empty() {
349 return Err(CoreError::sys(libc::EINVAL, "exec argv empty"));
350 }
351
352 let c_argv: Vec<CString> = argv
353 .into_iter()
354 .map(|s| {
355 CString::new(s).map_err(|_| CoreError::sys(libc::EINVAL, "exec argv contains nul"))
356 })
357 .collect::<Result<_, _>>()?;
358
359 let c_envp = match env {
360 Some(vars) => Some(
361 vars.into_iter()
362 .map(|s| {
363 CString::new(s)
364 .map_err(|_| CoreError::sys(libc::EINVAL, "exec env contains nul"))
365 })
366 .collect::<Result<Vec<_>, _>>()?,
367 ),
368 None => None,
369 };
370
371 let c_cwd = match cwd {
372 Some(c) => Some(
373 CString::new(c)
374 .map_err(|_| CoreError::sys(libc::EINVAL, "exec cwd contains nul"))?,
375 ),
376 None => None,
377 };
378
379 Ok(Self {
380 argv: ExecArgv::Dynamic(c_argv),
381 envp: c_envp,
382 cwd: c_cwd,
383 })
384 }
385
386 fn get_argv_ptrs(&self) -> Vec<*mut libc::c_char> {
388 let mut ptrs = Vec::new();
389 match &self.argv {
390 ExecArgv::Dynamic(v) => {
391 for s in v {
392 ptrs.push(s.as_ptr() as *mut libc::c_char);
393 }
394 }
395 }
396 ptrs.push(ptr::null_mut());
397 ptrs
398 }
399
400 fn get_envp_ptrs(&self) -> Option<Vec<*mut libc::c_char>> {
402 self.envp.as_ref().map(|envp| {
403 let mut ptrs = Vec::new();
404 for s in envp {
405 ptrs.push(s.as_ptr() as *mut libc::c_char);
406 }
407 ptrs.push(ptr::null_mut());
408 ptrs
409 })
410 }
411}
412
413#[inline(always)]
414fn decode_status(status: i32) -> ExitStatus {
415 if WIFEXITED(status) {
416 ExitStatus::Exited(WEXITSTATUS(status))
417 } else if WIFSIGNALED(status) {
418 ExitStatus::Signaled(WTERMSIG(status))
419 } else {
420 ExitStatus::Exited(-1)
421 }
422}
423
424pub struct Process {
432 pid: pid_t,
433}
434
435impl Process {
436 pub fn new(pid: pid_t) -> Self {
438 Self { pid }
439 }
440
441 pub fn pid(&self) -> pid_t {
443 self.pid
444 }
445
446 pub fn wait_step(&self) -> Result<Option<ExitStatus>, CoreError> {
452 loop {
453 let mut status = 0;
454 let r = unsafe { waitpid(self.pid, &mut status, libc::WNOHANG) };
455 if r == 0 {
456 return Ok(None);
457 }
458 if r < 0 {
459 let e = errno();
460 if e == libc::EINTR {
461 continue;
462 }
463 return Err(CoreError::sys(e, "waitpid_step"));
464 }
465 return Ok(Some(decode_status(status)));
466 }
467 }
468
469 pub fn wait_blocking(&self) -> Result<ExitStatus, CoreError> {
474 loop {
475 let mut status = 0;
476 let r = unsafe { waitpid(self.pid, &mut status, 0) };
477 if r < 0 {
478 let e = errno();
479 if e == libc::EINTR {
480 continue;
481 }
482 return Err(CoreError::sys(e, "waitpid_blocking"));
483 }
484 return Ok(decode_status(status));
485 }
486 }
487
488 pub fn kill(&self, sig: i32) -> Result<(), CoreError> {
495 let r = unsafe { libc::kill(self.pid, sig) };
496 if r < 0 {
497 let e = errno();
498 if e == libc::ESRCH {
499 return Ok(());
500 }
501 syscall_ret(-1, "kill")?;
502 }
503 Ok(())
504 }
505
506 pub fn kill_pgroup(&self, sig: i32) -> Result<(), CoreError> {
511 let r = unsafe { libc::kill(-self.pid, sig) };
512 if r < 0 {
513 let e = errno();
514 if e == libc::ESRCH {
515 return Ok(());
516 }
517 syscall_ret(-1, "kill_pgroup")?;
518 }
519 Ok(())
520 }
521}
522
523#[derive(Clone)]
525pub struct SpawnOptions {
526 ctx: ExecContext,
527 stdin: Option<Box<[u8]>>,
528 capture_stdout: bool,
529 capture_stderr: bool,
530 wait: bool,
531 pgroup: ProcessGroup,
532 max_output: usize,
533 timeout_ms: Option<u32>,
534 kill_grace_ms: u32,
535 cancel: CancelPolicy,
536 backend: SpawnBackend,
537 fd_policy: SpawnFdPolicy,
538 early_exit: Option<fn(&[u8]) -> bool>,
539}
540
541impl SpawnOptions {
542 pub fn builder(argv: Vec<String>, backend: SpawnBackend) -> SpawnOptionsBuilder {
544 SpawnOptionsBuilder::new(argv, backend)
545 }
546
547 pub fn run(self) -> Result<Output, CoreError> {
549 spawn(self)
550 }
551}
552
553#[derive(Clone)]
555pub struct SpawnOptionsBuilder {
556 argv: Vec<String>,
557 env: Option<Vec<String>>,
558 cwd: Option<String>,
559 stdin: Option<Box<[u8]>>,
560 capture_stdout: bool,
561 capture_stderr: bool,
562 wait: bool,
563 pgroup: ProcessGroup,
564 max_output: usize,
565 timeout_ms: Option<u32>,
566 kill_grace_ms: u32,
567 cancel: CancelPolicy,
568 backend: SpawnBackend,
569 fd_policy: SpawnFdPolicy,
570 early_exit: Option<fn(&[u8]) -> bool>,
571}
572
573impl SpawnOptionsBuilder {
574 pub fn new(argv: Vec<String>, backend: SpawnBackend) -> Self {
576 Self {
577 argv,
578 env: None,
579 cwd: None,
580 stdin: None,
581 capture_stdout: false,
582 capture_stderr: false,
583 wait: true,
584 pgroup: ProcessGroup::default(),
585 max_output: 1024 * 1024,
586 timeout_ms: None,
587 kill_grace_ms: 2000,
588 cancel: CancelPolicy::Kill,
589 backend,
590 fd_policy: SpawnFdPolicy::default(),
591 early_exit: None,
592 }
593 }
594
595 pub fn env(mut self, env: Vec<String>) -> Self {
597 self.env = Some(env);
598 self
599 }
600
601 pub fn cwd(mut self, cwd: String) -> Self {
603 self.cwd = Some(cwd);
604 self
605 }
606
607 pub fn stdin(mut self, data: impl Into<Box<[u8]>>) -> Self {
609 self.stdin = Some(data.into());
610 self
611 }
612
613 pub fn capture_stdout(mut self) -> Self {
615 self.capture_stdout = true;
616 self
617 }
618
619 pub fn capture_stderr(mut self) -> Self {
621 self.capture_stderr = true;
622 self
623 }
624
625 pub fn wait(mut self, wait: bool) -> Self {
627 self.wait = wait;
628 self
629 }
630
631 pub fn pgroup(mut self, pgroup: ProcessGroup) -> Self {
633 self.pgroup = pgroup;
634 self
635 }
636
637 pub fn max_output(mut self, max: usize) -> Self {
642 self.max_output = max;
643 self
644 }
645
646 pub fn timeout_ms(mut self, ms: u32) -> Self {
648 self.timeout_ms = Some(ms);
649 self
650 }
651
652 pub fn kill_grace_ms(mut self, ms: u32) -> Self {
654 self.kill_grace_ms = ms;
655 self
656 }
657
658 pub fn cancel(mut self, policy: CancelPolicy) -> Self {
660 self.cancel = policy;
661 self
662 }
663
664 pub fn fd_policy(mut self, policy: SpawnFdPolicy) -> Self {
666 self.fd_policy = policy;
667 self
668 }
669
670 pub fn early_exit(mut self, callback: fn(&[u8]) -> bool) -> Self {
672 self.early_exit = Some(callback);
673 self
674 }
675
676 pub fn build(self) -> Result<SpawnOptions, CoreError> {
678 let ctx = ExecContext::new(self.argv, self.env, self.cwd)?;
679 Ok(SpawnOptions {
680 ctx,
681 stdin: self.stdin,
682 capture_stdout: self.capture_stdout,
683 capture_stderr: self.capture_stderr,
684 wait: self.wait,
685 pgroup: self.pgroup,
686 max_output: self.max_output,
687 timeout_ms: self.timeout_ms,
688 kill_grace_ms: self.kill_grace_ms,
689 cancel: self.cancel,
690 backend: self.backend,
691 fd_policy: self.fd_policy,
692 early_exit: self.early_exit,
693 })
694 }
695}
696
697#[derive(Debug)]
699pub struct Output {
700 pub pid: pid_t,
702 pub status: Option<ExitStatus>,
704 pub stdout: Vec<u8>,
706 pub stderr: Vec<u8>,
708 pub timed_out: bool,
710 pub stdout_early_exited: bool,
712}
713
714fn validate_backend(opts: &SpawnOptions) -> Result<(), CoreError> {
715 validate_fd_policy(&opts.fd_policy)?;
716 match opts.backend {
717 SpawnBackend::PosixSpawn => {
718 if opts.ctx.cwd.is_some() {
719 return Err(CoreError::sys(libc::EINVAL, "posix_spawn cwd unsupported"));
720 }
721 if opts.pgroup.isolated {
722 return Err(CoreError::sys(
723 libc::EINVAL,
724 "posix_spawn setsid unsupported",
725 ));
726 }
727 if opts.fd_policy != SpawnFdPolicy::CloexecOnly {
728 return Err(CoreError::sys(
729 libc::EINVAL,
730 "posix_spawn fd policy unsupported",
731 ));
732 }
733 Ok(())
734 }
735 SpawnBackend::Fork => Ok(()),
736 }
737}
738
739fn validate_fd_policy(policy: &SpawnFdPolicy) -> Result<(), CoreError> {
740 if let SpawnFdPolicy::Allowlist(fds) = policy {
741 let mut seen = Vec::with_capacity(fds.len());
742 for &fd in fds {
743 if fd < 0 {
744 return Err(CoreError::sys(libc::EINVAL, "spawn fd allowlist invalid"));
745 }
746 let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
747 if flags < 0 {
748 return Err(CoreError::sys(errno(), "spawn fd allowlist fcntl(F_GETFD)"));
749 }
750 if seen.contains(&fd) {
751 return Err(CoreError::sys(libc::EINVAL, "spawn fd allowlist duplicate"));
752 }
753 seen.push(fd);
754 }
755 }
756 Ok(())
757}
758
759use crate::io::DrainState;
760
761pub type SpawnDrain = DrainState<fn(&[u8]) -> bool>;
763
764pub struct RunningProcess {
771 pub process: Process,
773 drain: SpawnDrain,
774}
775
776impl RunningProcess {
777 pub fn register_with_reactor(&mut self, reactor: &mut Reactor) -> Result<(), CoreError> {
783 self.drain.register_with_reactor(reactor)
784 }
785
786 pub fn handle_reactor_event(
792 &mut self,
793 reactor: &mut Reactor,
794 event: &crate::reactor::Event,
795 ) -> Result<(), CoreError> {
796 if self.drain.stdout_matches(event.token) {
797 if event.readable || event.hangup {
798 self.drain.handle_stdout_ready(reactor)?;
799 } else if event.error {
800 self.drain.drop_stdout(reactor)?;
801 }
802 } else if self.drain.stderr_matches(event.token) {
803 if event.readable || event.hangup {
804 self.drain.handle_stderr_ready(reactor)?;
805 } else if event.error {
806 self.drain.drop_stderr(reactor)?;
807 }
808 } else if self.drain.stdin_matches(event.token) {
809 if event.writable {
810 self.drain.handle_stdin_writable(reactor)?;
811 } else if event.error || event.hangup {
812 self.drain.drop_stdin(reactor)?;
813 }
814 }
815 Ok(())
816 }
817
818 pub fn io_done(&self) -> bool {
820 self.drain.is_done()
821 }
822
823 pub fn into_output_parts(self) -> (Vec<u8>, Vec<u8>) {
825 self.drain.into_parts()
826 }
827}
828
829use crate::reactor::Reactor;
830
831pub fn spawn_start(opts: SpawnOptions) -> Result<RunningProcess, CoreError> {
846 if !opts.wait && (opts.stdin.is_some() || opts.capture_stdout || opts.capture_stderr) {
847 return Err(CoreError::sys(
848 libc::EINVAL,
849 "background I/O capture not supported (wait must be true)",
850 ));
851 }
852
853 validate_backend(&opts)?;
854
855 let (pid, drain) = match opts.backend {
856 SpawnBackend::PosixSpawn => spawn_posix_internal(opts)?,
857 SpawnBackend::Fork => spawn_fork_internal(opts)?,
858 };
859
860 Ok(RunningProcess {
861 process: Process::new(pid),
862 drain,
863 })
864}
865
866pub fn spawn(opts: SpawnOptions) -> Result<Output, CoreError> {
875 let wait = opts.wait;
876 let timeout_ms = opts.timeout_ms;
877 let kill_grace_ms = opts.kill_grace_ms;
878 let cancel = opts.cancel;
879 let pgroup = opts.pgroup;
880
881 let mut reactor = Reactor::new()?;
882 let running = spawn_start(opts)?;
883
884 let pid = running.process.pid();
885 let mut drain = running.drain;
886
887 drain.register_with_reactor(&mut reactor)?;
888
889 if !wait {
890 let (stdout, stderr) = drain.into_parts();
891 return Ok(Output {
892 pid,
893 status: None,
894 stdout,
895 stderr,
896 timed_out: false,
897 stdout_early_exited: false,
898 });
899 }
900
901 wait_loop(
902 pid,
903 drain,
904 reactor,
905 timeout_ms,
906 kill_grace_ms,
907 cancel,
908 pgroup,
909 )
910}
911
912fn spawn_posix_internal(opts: SpawnOptions) -> Result<(pid_t, SpawnDrain), CoreError> {
913 let mut pipes = Pipes::new(
914 opts.stdin.as_deref(),
915 opts.capture_stdout,
916 opts.capture_stderr,
917 )?;
918
919 let exe_ptr = match &opts.ctx.argv {
920 ExecArgv::Dynamic(v) => v[0].as_ptr(),
921 };
922
923 let argv = opts.ctx.get_argv_ptrs();
924 let envp = opts.ctx.get_envp_ptrs();
925
926 let actions = MaybeUninit::zeroed();
927 let mut actions = unsafe { actions.assume_init() };
928 if let Err(e) = posix_ret(
929 unsafe { posix_spawn_file_actions_init(&mut actions) },
930 "file_actions_init",
931 ) {
932 pipes.close_all();
933 return Err(e);
934 }
935
936 struct Actions(*mut libc::posix_spawn_file_actions_t);
937 impl Drop for Actions {
938 fn drop(&mut self) {
939 unsafe {
940 posix_spawn_file_actions_destroy(self.0);
941 }
942 }
943 }
944 let _guard = Actions(&mut actions);
945
946 if let (Some(r), Some(w)) = (&pipes.stdin_r, &pipes.stdin_w) {
947 if let Err(e) = posix_ret(
948 unsafe { posix_spawn_file_actions_adddup2(&mut actions, r.raw(), 0) },
949 "dup2 stdin",
950 ) {
951 pipes.close_all();
952 return Err(e);
953 }
954 if let Err(e) = posix_ret(
955 unsafe { posix_spawn_file_actions_addclose(&mut actions, r.raw()) },
956 "close stdin pipe",
957 ) {
958 pipes.close_all();
959 return Err(e);
960 }
961 if let Err(e) = posix_ret(
962 unsafe { posix_spawn_file_actions_addclose(&mut actions, w.raw()) },
963 "close stdin write pipe",
964 ) {
965 pipes.close_all();
966 return Err(e);
967 }
968 }
969
970 if let (Some(r), Some(w)) = (&pipes.stdout_r, &pipes.stdout_w) {
971 if let Err(e) = posix_ret(
972 unsafe { posix_spawn_file_actions_adddup2(&mut actions, w.raw(), 1) },
973 "dup2 stdout",
974 ) {
975 pipes.close_all();
976 return Err(e);
977 }
978 if let Err(e) = posix_ret(
979 unsafe { posix_spawn_file_actions_addclose(&mut actions, w.raw()) },
980 "close stdout pipe",
981 ) {
982 pipes.close_all();
983 return Err(e);
984 }
985 if let Err(e) = posix_ret(
986 unsafe { posix_spawn_file_actions_addclose(&mut actions, r.raw()) },
987 "close stdout read pipe",
988 ) {
989 pipes.close_all();
990 return Err(e);
991 }
992 }
993
994 if let (Some(r), Some(w)) = (&pipes.stderr_r, &pipes.stderr_w) {
995 if let Err(e) = posix_ret(
996 unsafe { posix_spawn_file_actions_adddup2(&mut actions, w.raw(), 2) },
997 "dup2 stderr",
998 ) {
999 pipes.close_all();
1000 return Err(e);
1001 }
1002 if let Err(e) = posix_ret(
1003 unsafe { posix_spawn_file_actions_addclose(&mut actions, w.raw()) },
1004 "close stderr pipe",
1005 ) {
1006 pipes.close_all();
1007 return Err(e);
1008 }
1009 if let Err(e) = posix_ret(
1010 unsafe { posix_spawn_file_actions_addclose(&mut actions, r.raw()) },
1011 "close stderr read pipe",
1012 ) {
1013 pipes.close_all();
1014 return Err(e);
1015 }
1016 }
1017
1018 let attr = MaybeUninit::zeroed();
1019 let mut attr = unsafe { attr.assume_init() };
1020 if let Err(e) = posix_ret(unsafe { posix_spawnattr_init(&mut attr) }, "attr_init") {
1021 pipes.close_all();
1022 return Err(e);
1023 }
1024
1025 struct Attr(*mut libc::posix_spawnattr_t);
1026 impl Drop for Attr {
1027 fn drop(&mut self) {
1028 unsafe {
1029 posix_spawnattr_destroy(self.0);
1030 }
1031 }
1032 }
1033 let _attr = Attr(&mut attr);
1034
1035 let mut flags = 0;
1036
1037 if let Some(pg) = opts.pgroup.leader {
1038 flags |= POSIX_SPAWN_SETPGROUP;
1039 if let Err(e) = posix_ret(
1040 unsafe { posix_spawnattr_setpgroup(&mut attr, pg) },
1041 "setpgroup",
1042 ) {
1043 pipes.close_all();
1044 return Err(e);
1045 }
1046 }
1047
1048 flags |= POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF;
1049
1050 if let Err(e) = posix_ret(
1051 unsafe { posix_spawnattr_setflags(&mut attr, flags as _) },
1052 "setflags",
1053 ) {
1054 pipes.close_all();
1055 return Err(e);
1056 }
1057
1058 let empty_mask = SignalRuntime::empty_set();
1059 let def = SignalRuntime::set_with(&[libc::SIGPIPE])?;
1060
1061 if let Err(e) = posix_ret(
1062 unsafe { posix_spawnattr_setsigmask(&mut attr, &empty_mask) },
1063 "setsigmask",
1064 ) {
1065 pipes.close_all();
1066 return Err(e);
1067 }
1068 if let Err(e) = posix_ret(
1069 unsafe { posix_spawnattr_setsigdefault(&mut attr, &def) },
1070 "setsigdefault",
1071 ) {
1072 pipes.close_all();
1073 return Err(e);
1074 }
1075
1076 let mut pid: pid_t = 0;
1077
1078 let envp_ptr = envp.as_ref().map_or_else(
1079 || unsafe { environ as *const *mut c_char },
1080 |e: &Vec<*mut c_char>| e.as_ptr(),
1081 );
1082
1083 if let Err(e) = posix_ret(
1084 unsafe { posix_spawn(&mut pid, exe_ptr, &actions, &attr, argv.as_ptr(), envp_ptr) },
1085 "posix_spawn",
1086 ) {
1087 pipes.close_all();
1088 return Err(e);
1089 }
1090
1091 drop(pipes.stdin_r.take());
1092 drop(pipes.stdout_w.take());
1093 drop(pipes.stderr_w.take());
1094
1095 let drain = crate::io::DrainState::new(
1096 pipes.stdin_w.take().filter(|_| opts.stdin.is_some()),
1097 opts.stdin,
1098 pipes.stdout_r.take(),
1099 pipes.stderr_r.take(),
1100 opts.max_output,
1101 opts.early_exit,
1102 )?;
1103
1104 Ok((pid, drain))
1105}
1106
1107fn collect_required_pipe_fds(pipes: &Pipes) -> Vec<RawFd> {
1108 let mut fds = Vec::new();
1109 if let Some(fd) = &pipes.stdin_r {
1110 fds.push(fd.raw());
1111 }
1112 if let Some(fd) = &pipes.stdin_w {
1113 fds.push(fd.raw());
1114 }
1115 if let Some(fd) = &pipes.stdout_r {
1116 fds.push(fd.raw());
1117 }
1118 if let Some(fd) = &pipes.stdout_w {
1119 fds.push(fd.raw());
1120 }
1121 if let Some(fd) = &pipes.stderr_r {
1122 fds.push(fd.raw());
1123 }
1124 if let Some(fd) = &pipes.stderr_w {
1125 fds.push(fd.raw());
1126 }
1127 fds
1128}
1129
1130fn collect_open_fds_for_child_policy(policy: &SpawnFdPolicy) -> Result<Vec<RawFd>, CoreError> {
1131 match policy {
1132 SpawnFdPolicy::CloexecOnly => Ok(Vec::new()),
1133 SpawnFdPolicy::CloseFrom3 | SpawnFdPolicy::Allowlist(_) => {
1134 let dir_fd = unsafe {
1135 libc::open(
1136 c"/proc/self/fd".as_ptr(),
1137 libc::O_RDONLY | libc::O_DIRECTORY | libc::O_CLOEXEC,
1138 )
1139 };
1140 if dir_fd < 0 {
1141 return Err(CoreError::sys(errno(), "open /proc/self/fd"));
1142 }
1143
1144 let dir = unsafe { libc::fdopendir(dir_fd) };
1145 if dir.is_null() {
1146 let code = errno();
1147 unsafe {
1148 libc::close(dir_fd);
1149 }
1150 return Err(CoreError::sys(code, "fdopendir /proc/self/fd"));
1151 }
1152
1153 let mut open_fds = Vec::new();
1154 loop {
1155 let entry = unsafe { libc::readdir(dir) };
1156 if entry.is_null() {
1157 break;
1158 }
1159 let name = unsafe { std::ffi::CStr::from_ptr((*entry).d_name.as_ptr()) };
1160 if let Ok(s) = name.to_str()
1161 && let Ok(fd) = s.parse::<RawFd>()
1162 && fd != dir_fd
1163 {
1164 open_fds.push(fd);
1165 }
1166 }
1167 unsafe {
1168 libc::closedir(dir);
1169 }
1170 Ok(open_fds)
1171 }
1172 }
1173}
1174
1175fn close_child_fds_for_policy(policy: &SpawnFdPolicy, required_fds: &[RawFd], open_fds: &[RawFd]) {
1176 match policy {
1177 SpawnFdPolicy::CloexecOnly => {}
1178 SpawnFdPolicy::CloseFrom3 | SpawnFdPolicy::Allowlist(_) => {
1179 for &fd in open_fds {
1180 if fd > 2
1181 && !required_fds.contains(&fd)
1182 && !matches!(policy, SpawnFdPolicy::Allowlist(allowlist) if allowlist.contains(&fd))
1183 {
1184 unsafe {
1185 libc::close(fd);
1186 }
1187 }
1188 }
1189 }
1190 }
1191}
1192
1193fn spawn_fork_internal(opts: SpawnOptions) -> Result<(pid_t, SpawnDrain), CoreError> {
1194 let mut pipes = Pipes::new(
1195 opts.stdin.as_deref(),
1196 opts.capture_stdout,
1197 opts.capture_stderr,
1198 )?;
1199
1200 let exe_ptr = match &opts.ctx.argv {
1201 ExecArgv::Dynamic(v) => v[0].as_ptr(),
1202 };
1203
1204 let argv = opts.ctx.get_argv_ptrs();
1205 let envp = opts.ctx.get_envp_ptrs();
1206 let cwd_cstr = &opts.ctx.cwd;
1207 let (child_error_r, child_error_w) = make_cloexec_pipe()?;
1208 let mut required_fds = collect_required_pipe_fds(&pipes);
1209 required_fds.push(child_error_w);
1210 let open_fds = collect_open_fds_for_child_policy(&opts.fd_policy)?;
1211
1212 let pid = unsafe { libc::fork() };
1213
1214 if pid < 0 {
1215 unsafe {
1216 libc::close(child_error_r);
1217 libc::close(child_error_w);
1218 }
1219 pipes.close_all();
1220 syscall_ret(-1, "fork")?;
1221 }
1222
1223 if pid == 0 {
1224 unsafe {
1226 libc::close(child_error_r);
1227 }
1228
1229 if let (Some(r), Some(_)) = (&pipes.stdin_r, &pipes.stdin_w) {
1231 unsafe {
1232 if libc::dup2(r.raw(), 0) < 0 {
1233 report_child_setup_error(child_error_w, ChildSetupOp::DupStdin, errno());
1234 }
1235 }
1236 }
1237
1238 if let (Some(_), Some(w)) = (&pipes.stdout_r, &pipes.stdout_w) {
1240 unsafe {
1241 if libc::dup2(w.raw(), 1) < 0 {
1242 report_child_setup_error(child_error_w, ChildSetupOp::DupStdout, errno());
1243 }
1244 }
1245 }
1246
1247 if let (Some(_), Some(w)) = (&pipes.stderr_r, &pipes.stderr_w) {
1249 unsafe {
1250 if libc::dup2(w.raw(), 2) < 0 {
1251 report_child_setup_error(child_error_w, ChildSetupOp::DupStderr, errno());
1252 }
1253 }
1254 }
1255
1256 pipes.close_all();
1258
1259 close_child_fds_for_policy(&opts.fd_policy, &required_fds, &open_fds);
1260
1261 if opts.pgroup.isolated {
1263 unsafe {
1265 if libc::setsid() < 0 {
1266 report_child_setup_error(child_error_w, ChildSetupOp::Setsid, errno());
1267 }
1268 }
1269 }
1270
1271 if let Some(cwd) = cwd_cstr {
1273 unsafe {
1275 if libc::chdir(cwd.as_ptr()) != 0 {
1276 report_child_setup_error(child_error_w, ChildSetupOp::Chdir, errno());
1277 }
1278 }
1279 }
1280
1281 if let Some(pg) = opts.pgroup.leader {
1283 unsafe {
1285 if libc::setpgid(0, pg) < 0 {
1286 report_child_setup_error(child_error_w, ChildSetupOp::Setpgid, errno());
1287 }
1288 }
1289 }
1290
1291 let envp_ptr = envp.as_ref().map_or_else(
1292 || unsafe { environ as *const *mut c_char },
1293 |e: &Vec<*mut c_char>| e.as_ptr(),
1294 );
1295
1296 if let Err(err) = SignalRuntime::unblock_all() {
1299 unsafe {
1300 report_child_setup_error(
1301 child_error_w,
1302 ChildSetupOp::SignalMask,
1303 err.raw_os_error().unwrap_or(libc::EIO),
1304 );
1305 }
1306 }
1307 if let Err(err) = SignalRuntime::reset_default(libc::SIGPIPE) {
1308 unsafe {
1309 report_child_setup_error(
1310 child_error_w,
1311 ChildSetupOp::SignalMask,
1312 err.raw_os_error().unwrap_or(libc::EIO),
1313 );
1314 }
1315 }
1316
1317 unsafe {
1320 libc::execve(
1321 exe_ptr,
1322 argv.as_ptr() as *const *const _,
1323 envp_ptr as *const *const _,
1324 );
1325 report_child_setup_error(child_error_w, ChildSetupOp::Execve, errno());
1326 }
1327 }
1328
1329 unsafe {
1331 libc::close(child_error_w);
1332 }
1333 match read_child_setup_error(child_error_r) {
1334 Ok(Some(err)) => {
1335 unsafe {
1336 libc::close(child_error_r);
1337 let mut status = 0;
1338 let _ = libc::waitpid(pid, &mut status, 0);
1339 }
1340 pipes.close_all();
1341 return Err(err);
1342 }
1343 Ok(None) => {}
1344 Err(err) => {
1345 unsafe {
1346 libc::close(child_error_r);
1347 }
1348 pipes.close_all();
1349 return Err(err);
1350 }
1351 }
1352 unsafe {
1353 libc::close(child_error_r);
1354 }
1355 drop(pipes.stdin_r.take());
1356 drop(pipes.stdout_w.take());
1357 drop(pipes.stderr_w.take());
1358
1359 let drain = crate::io::DrainState::new(
1360 pipes.stdin_w.take().filter(|_| opts.stdin.is_some()),
1361 opts.stdin,
1362 pipes.stdout_r.take(),
1363 pipes.stderr_r.take(),
1364 opts.max_output,
1365 opts.early_exit,
1366 )?;
1367
1368 Ok((pid, drain))
1369}
1370
1371enum KillState {
1372 None,
1373 TermSent,
1374 KillSent,
1375}
1376
1377fn wait_loop(
1378 pid: pid_t,
1379 mut drain: crate::io::DrainState<fn(&[u8]) -> bool>,
1380 mut reactor: Reactor,
1381 timeout_ms: Option<u32>,
1382 kill_grace_ms: u32,
1383 cancel: CancelPolicy,
1384 pgroup: ProcessGroup,
1385) -> Result<Output, CoreError> {
1386 let process = Process::new(pid);
1387 let mut status_raw = process.wait_step()?;
1388 let mut state = KillState::None;
1389 let mut timed_out = false;
1390
1391 let start_time = std::time::Instant::now();
1392 let deadline = timeout_ms.map(|t| std::time::Duration::from_millis(t as u64));
1393
1394 loop {
1395 let mut poll_timeout = -1;
1396
1397 if let Some(dl) = deadline {
1398 let elapsed = start_time.elapsed();
1399 if elapsed >= dl {
1400 timed_out = true;
1401 let elapsed_over = (elapsed - dl).as_millis();
1402
1403 let target_is_group = pgroup.isolated || pgroup.leader.is_some();
1404
1405 match state {
1406 KillState::None => {
1407 if cancel == CancelPolicy::Graceful {
1408 let r = if target_is_group {
1409 process.kill_pgroup(libc::SIGTERM)
1410 } else {
1411 process.kill(libc::SIGTERM)
1412 };
1413 if r.is_err() {
1414 state = KillState::KillSent; } else {
1416 state = KillState::TermSent;
1417 }
1418 } else if cancel == CancelPolicy::Kill {
1419 let _ = if target_is_group {
1420 process.kill_pgroup(libc::SIGKILL)
1421 } else {
1422 process.kill(libc::SIGKILL)
1423 };
1424 state = KillState::KillSent;
1425 } else {
1426 }
1428 }
1429 KillState::TermSent if elapsed_over > kill_grace_ms as u128 => {
1430 let _ = if target_is_group {
1431 process.kill_pgroup(libc::SIGKILL)
1432 } else {
1433 process.kill(libc::SIGKILL)
1434 };
1435 state = KillState::KillSent;
1436 }
1437 _ => {}
1438 }
1439 poll_timeout = 100; } else {
1441 let remaining = dl - elapsed;
1442 poll_timeout = remaining.as_millis().min(i32::MAX as u128) as i32;
1443 }
1444 }
1445
1446 if status_raw.is_none()
1447 && let Some(s) = process.wait_step()?
1448 {
1449 status_raw = Some(s);
1450 }
1451
1452 if drain.is_done() {
1453 let s = match status_raw {
1454 Some(s) => s,
1455 None => process.wait_blocking()?,
1456 };
1457
1458 for slot in drain.take_all_slots() {
1459 reactor.del(&slot.fd)?;
1460 }
1461 let (stdout, stderr, output_limit_exceeded, stdout_early_exited) =
1462 drain.into_parts_with_state();
1463 if output_limit_exceeded {
1464 return Err(CoreError::sys(libc::EOVERFLOW, "spawn output limit"));
1465 }
1466 return Ok(Output {
1467 pid,
1468 status: Some(s),
1469 stdout,
1470 stderr,
1471 timed_out,
1472 stdout_early_exited,
1473 });
1474 }
1475
1476 let timeout = poll_timeout;
1477
1478 let mut events = Vec::new();
1479 let nevents = reactor.wait(&mut events, 64, timeout)?;
1480
1481 for ev in events.iter().take(nevents) {
1482 if drain.stdout_matches(ev.token) {
1483 if ev.readable || ev.hangup {
1484 drain.handle_stdout_ready(&mut reactor)?;
1485 } else if ev.error {
1486 drain.drop_stdout(&mut reactor)?;
1487 }
1488 } else if drain.stderr_matches(ev.token) {
1489 if ev.readable || ev.hangup {
1490 drain.handle_stderr_ready(&mut reactor)?;
1491 } else if ev.error {
1492 drain.drop_stderr(&mut reactor)?;
1493 }
1494 } else if drain.stdin_matches(ev.token) {
1495 if ev.writable {
1496 drain.handle_stdin_writable(&mut reactor)?;
1497 } else if ev.error || ev.hangup {
1498 drain.drop_stdin(&mut reactor)?;
1499 }
1500 }
1501 }
1502 }
1503}