1#[cfg(test)]
5mod tests;
6
7use std::cell::RefCell;
8use std::ffi::OsStr;
9use std::io::{ErrorKind, PipeReader, PipeWriter, Read, Write};
10use std::os::fd::AsFd;
11use std::os::unix::process::CommandExt;
12use std::panic::{self, AssertUnwindSafe, Location};
13use std::process::{Child, Command, ExitStatus, Stdio};
14use std::rc::{Rc, Weak};
15use std::sync::Once;
16use std::time::Instant;
17
18use calloop::timer::TimeoutAction;
19use calloop::{Interest, RegistrationToken};
20use crabshell_reactive::{ReadSignal, Signal, untrack};
21use crabshell_ui::Cx;
22use rustix::process::{Pid, PidfdFlags, Signal as Kill, kill_process_group, pidfd_open};
23
24use crate::lines::{Lines, decode, panic_message};
25use crate::{Shared, activation, spawn};
26
27pub(crate) const MAX_OUTPUT: usize = 1024 * 1024;
30
31pub(crate) const MAX_INPUT: usize = 4 * 1024 * 1024;
34
35pub(crate) struct Processes {
37 pub(crate) shared: Rc<Shared>,
38 live: RefCell<Vec<Weak<Inner>>>,
40}
41
42impl Processes {
43 pub(crate) fn new(shared: Rc<Shared>) -> Processes {
44 Processes {
45 shared,
46 live: RefCell::default(),
47 }
48 }
49}
50
51pub(crate) fn stop_all(cx: &Cx) {
55 let Some(processes) = cx.service::<Processes>() else {
56 return;
57 };
58 let live: Vec<Rc<Inner>> = processes
59 .live
60 .borrow()
61 .iter()
62 .filter_map(Weak::upgrade)
63 .collect();
64 for inner in live {
65 inner.stop();
66 }
67}
68
69pub trait IntoCommand {
73 fn into_command(self) -> Option<Command>;
75}
76
77impl IntoCommand for Command {
78 fn into_command(self) -> Option<Command> {
79 Some(self)
80 }
81}
82
83impl<S: AsRef<OsStr>> IntoCommand for Vec<S> {
84 fn into_command(self) -> Option<Command> {
85 let mut words = self.into_iter();
86 let mut command = Command::new(words.next()?);
87 command.args(words);
88 Some(command)
89 }
90}
91
92impl<S: AsRef<OsStr>, const N: usize> IntoCommand for [S; N] {
93 fn into_command(self) -> Option<Command> {
94 Vec::from(self).into_command()
95 }
96}
97
98#[derive(Clone)]
107pub struct Process {
108 inner: Rc<Inner>,
109}
110
111struct Inner {
112 processes: Option<Rc<Shared>>,
114 running: Signal<bool>,
116 status: Signal<Option<ExitStatus>>,
117 stdout: Signal<String>,
118 stderr: Signal<String>,
119 restarts: Signal<u64>,
122 state: RefCell<State>,
123}
124
125#[derive(Default)]
126struct State {
127 pending: Option<Command>,
130 starter: Option<RegistrationToken>,
131 run: Option<Run>,
132 keep_stdin: bool,
133 input: Vec<u8>,
135 closing: bool,
138 ran: bool,
141 on_line: Option<OnLine>,
142 at: Option<(&'static str, &'static Location<'static>)>,
144 lines: Lines,
148 later: bool,
150 stopped: bool,
152}
153
154type OnLine = Rc<dyn Fn(&str)>;
156
157struct Run {
159 child: Child,
160 program: String,
161 exit: Option<RegistrationToken>,
162 out: Output,
163 err: Output,
164 stdin: Option<PipeWriter>,
165 outbox: Vec<u8>,
166 writer: Option<RegistrationToken>,
167}
168
169#[derive(Default)]
171struct Output {
172 stream: Option<(PipeReader, RegistrationToken)>,
173 partial: Vec<u8>,
175 kept: usize,
177 capped: bool,
178}
179
180#[derive(Clone, Copy, PartialEq)]
181enum Which {
182 Out,
183 Err,
184}
185
186#[track_caller]
188pub(crate) fn process<C: IntoCommand>(cx: &Cx, command: impl Fn() -> C + 'static) -> Process {
189 let at = Location::caller();
190 let service = cx.service::<Processes>();
191 let processes = service
192 .as_ref()
193 .map(|processes| Rc::clone(&processes.shared));
194 if processes.is_none() {
195 static LOGGED: Once = Once::new();
196 LOGGED.call_once(|| {
197 tracing::warn!("cx.process() needs the services the shell provides; nothing runs");
198 });
199 }
200 let inner = Rc::new(Inner {
201 processes,
202 running: cx.signal(true),
205 status: cx.signal(None),
206 stdout: cx.signal(String::new()),
207 stderr: cx.signal(String::new()),
208 restarts: cx.signal(0),
209 state: RefCell::default(),
210 });
211 cx.owner().signal(StopOnDrop(Rc::clone(&inner)));
213 if let Some(service) = service {
214 let mut live = service.live.borrow_mut();
215 live.retain(|inner| inner.strong_count() > 0);
216 live.push(Rc::downgrade(&inner));
217 }
218 let weak = Rc::downgrade(&inner);
219 let restarts = inner.restarts;
220 cx.effect(move || {
224 restarts.get();
225 let Some(inner) = weak.upgrade().filter(|inner| !inner.state.borrow().stopped) else {
226 return;
227 };
228 let built = panic::catch_unwind(AssertUnwindSafe(|| command().into_command()));
229 match built {
230 Ok(next) => untrack(|| inner.start(next)),
231 Err(payload) => {
232 let message = panic_message(payload.as_ref());
233 tracing::error!("cx.process (called at {at}) panicked building its command: {message}");
234 untrack(|| inner.cancel());
235 }
236 }
237 });
238 Process { inner }
239}
240
241pub(crate) fn exec_detached(cx: &Cx, command: impl IntoCommand) {
243 let Some(processes) = cx.service::<Processes>() else {
244 tracing::warn!("cx.exec_detached() needs the services the shell provides; nothing runs");
245 return;
246 };
247 let Some(mut command) = command.into_command() else {
248 return tracing::warn!("cx.exec_detached() got an empty command; nothing runs");
249 };
250 let shared = Rc::clone(&processes.shared);
251 activation::with_token(&processes.shared, None, move |token| {
252 if let Some(token) = &token {
253 activation::set_env(&mut command, token);
254 }
255 let _ = spawn::spawn(&shared, &mut command);
257 });
258}
259
260struct StopOnDrop(Rc<Inner>);
262
263impl Drop for StopOnDrop {
264 fn drop(&mut self) {
265 self.0.stop();
266 }
267}
268
269impl Process {
270 pub fn running(&self) -> ReadSignal<bool> {
273 self.inner.running.read_only()
274 }
275
276 pub fn exit_status(&self) -> ReadSignal<Option<ExitStatus>> {
279 self.inner.status.read_only()
280 }
281
282 pub fn stdout(&self) -> ReadSignal<String> {
286 self.inner.stdout.read_only()
287 }
288
289 pub fn stderr(&self) -> ReadSignal<String> {
291 self.inner.stderr.read_only()
292 }
293
294 #[track_caller]
302 pub fn on_line(self, on_line: impl Fn(&str) + 'static) -> Process {
303 self.handle("on_line", "\n", Rc::new(on_line))
304 }
305
306 #[track_caller]
315 pub fn on_split(self, marker: &str, on_split: impl Fn(&str) + 'static) -> Process {
316 self.handle("on_split", marker, Rc::new(on_split))
317 }
318
319 #[track_caller]
320 fn handle(self, name: &'static str, marker: &str, handler: OnLine) -> Process {
321 let mut state = self.inner.state.borrow_mut();
322 state.on_line = Some(handler);
323 state.at = Some((name, Location::caller()));
324 state.lines.set_marker(marker);
325 let waiting = state.lines.waiting();
326 drop(state);
327 if waiting {
328 self.inner.deliver_later();
329 }
330 self
331 }
332
333 pub fn with_stdin(self) -> Process {
338 self.inner.state.borrow_mut().keep_stdin = true;
339 self
340 }
341
342 pub fn write(&self, data: impl AsRef<[u8]>) {
348 self.inner.write(data.as_ref());
349 }
350
351 pub fn close_stdin(&self) {
354 {
355 let mut state = self.inner.state.borrow_mut();
356 if state.run.is_none() && state.ran {
358 return;
359 }
360 state.closing = true;
361 }
362 self.inner.flush();
363 }
364
365 pub fn kill(&self) {
369 if let Some(run) = &self.inner.state.borrow().run {
370 return terminate(&run.child);
371 }
372 self.inner.cancel();
373 }
374
375 pub fn restart(&self) {
379 self.inner.state.borrow_mut().stopped = false;
380 self.inner.restarts.try_update(|count| *count += 1);
381 }
382
383 pub fn stopped(self) -> Process {
391 self.inner.state.borrow_mut().stopped = true;
392 if self.inner.state.borrow().run.is_none() {
393 self.inner.cancel();
394 }
395 self
396 }
397}
398
399fn terminate(child: &Child) {
401 if let Err(error) = kill_process_group(Pid::from_child(child), Kill::TERM) {
403 tracing::debug!(%error, "couldn't stop a program; it may have ended");
404 }
405}
406
407impl Inner {
408 fn shared(&self) -> Option<&Rc<Shared>> {
409 self.processes.as_ref()
410 }
411
412 fn event_loop(&self) -> Option<Box<dyn crate::EventLoop>> {
413 self.shared()?.event_loop()
414 }
415
416 fn start(self: &Rc<Self>, command: Option<Command>) {
419 self.stop_run();
420 let Some(command) = command else {
421 tracing::warn!("cx.process() got an empty command; nothing runs");
422 set(self.running, false);
423 return;
424 };
425 let Some(event_loop) = self.event_loop() else {
426 return;
427 };
428 let mut state = self.state.borrow_mut();
429 state.pending = Some(command);
430 state.ran = false;
431 if state.starter.is_none() {
432 let inner = Rc::clone(self);
433 state.starter = event_loop.insert_timer(
434 Instant::now(),
435 Box::new(move || {
436 inner.spawn_pending();
437 TimeoutAction::Drop
438 }),
439 );
440 }
441 drop(state);
442 set(self.running, true);
443 set(self.status, None);
444 set(self.stdout, String::new());
445 set(self.stderr, String::new());
446 }
447
448 fn spawn_pending(self: &Rc<Self>) {
449 let Some(event_loop) = self.event_loop() else {
450 return;
451 };
452 let (command, keep_stdin, input) = {
453 let mut state = self.state.borrow_mut();
454 state.starter = None;
455 let input = std::mem::take(&mut state.input);
456 (state.pending.take(), state.keep_stdin, input)
457 };
458 let Some(mut command) = command else {
459 return;
460 };
461 let program = command.get_program().to_string_lossy().into_owned();
462 let spawned = spawn_piped(&mut command, keep_stdin);
463 drop(command);
466 let (child, out, err, stdin) = match spawned {
467 Ok(spawned) => spawned,
468 Err(error) => {
469 tracing::warn!(program, %error, "couldn't start the program");
470 let mut state = self.state.borrow_mut();
471 state.ran = true;
472 state.closing = false;
473 drop(state);
474 set(self.running, false);
475 return;
476 }
477 };
478 let mut run = Run {
479 child,
480 program,
481 exit: None,
482 out: Output::default(),
483 err: Output::default(),
484 stdin,
485 outbox: input,
486 writer: None,
487 };
488 self.state.borrow_mut().lines.reset();
489 for (which, stream) in [(Which::Out, out), (Which::Err, err)] {
490 let Ok(fd) = stream.as_fd().try_clone_to_owned() else {
491 continue;
492 };
493 let inner = Rc::clone(self);
494 let read = Box::new(move || inner.on_output(which));
495 if let Some(token) = event_loop.insert_fd(fd, Interest::READ, read) {
496 run.output(which).stream = Some((stream, token));
497 }
498 }
499 match pidfd_open(Pid::from_child(&run.child), PidfdFlags::NONBLOCK) {
500 Ok(fd) => {
501 let inner = Rc::clone(self);
502 let ended = Box::new(move || inner.on_exit());
503 run.exit = event_loop.insert_fd(fd, Interest::READ, ended);
504 }
505 Err(error) => tracing::warn!(%error, "couldn't follow a program to its end"),
506 }
507 self.state.borrow_mut().run = Some(run);
508 self.flush();
510 }
511
512 fn on_output(&self, which: Which) {
514 let mut chunk = [0; 64 * 1024];
515 let mut state = self.state.borrow_mut();
516 let State { run, lines, .. } = &mut *state;
517 let Some(run) = run.as_mut() else {
518 return;
519 };
520 let Some((stream, _)) = run.output(which).stream.as_mut() else {
521 return;
522 };
523 let read = match stream.read(&mut chunk) {
524 Ok(read) => read,
525 Err(error) if matches!(error.kind(), ErrorKind::WouldBlock | ErrorKind::Interrupted) => {
526 return;
527 }
528 Err(_) => 0,
529 };
530 let text = run.take(which, &chunk[..read]);
531 if which == Which::Out {
532 lines.append(&chunk[..read]);
533 }
534 let program = run.program.clone();
535 if read == 0 {
536 let token = run.output(which).stream.take().map(|(_, token)| token);
538 if let (Some(token), Some(event_loop)) = (token, self.event_loop()) {
539 event_loop.remove(token);
540 }
541 }
542 drop(state);
543 self.show(which, text);
544 self.deliver(false, &program);
545 }
546
547 fn on_exit(&self) {
550 let mut state = self.state.borrow_mut();
551 let Some(run) = state.run.as_mut() else {
552 return;
553 };
554 let status = match run.child.try_wait() {
555 Ok(Some(status)) => Some(status),
556 Ok(None) => return,
557 Err(error) => {
559 tracing::warn!(%error, program = run.program, "lost track of the program");
560 None
561 }
562 };
563 let mut run = state.run.take().expect("invariant: matched just above");
564 state.closing = false;
566 state.ran = true;
567 drop(state);
568 let mut chunk = [0; 64 * 1024];
573 for which in [Which::Out, Which::Err] {
574 let mut budget = MAX_OUTPUT;
575 while let Some((stream, _)) = run.output(which).stream.as_mut()
576 && budget > 0
577 {
578 let Ok(read @ 1..) = stream.read(&mut chunk) else {
579 break;
580 };
581 budget = budget.saturating_sub(read);
582 let text = run.take(which, &chunk[..read]);
583 if which == Which::Out {
584 self.state.borrow_mut().lines.append(&chunk[..read]);
585 }
586 self.show(which, text);
587 self.deliver(false, &run.program);
588 }
589 let text = run.take(which, &[]);
590 self.show(which, text);
591 }
592 self.deliver(true, &run.program);
593 self.remove_sources(&mut run);
594 set(self.status, status);
595 set(self.running, false);
596 }
597
598 fn show(&self, which: Which, text: String) {
600 if text.is_empty() {
601 return;
602 }
603 let signal = match which {
604 Which::Out => self.stdout,
605 Which::Err => self.stderr,
606 };
607 signal.try_update(|all| all.push_str(&text));
608 }
609
610 fn deliver_later(self: &Rc<Self>) {
613 let Some(event_loop) = self.event_loop() else {
614 return;
615 };
616 if std::mem::replace(&mut self.state.borrow_mut().later, true) {
617 return;
618 }
619 let inner = Rc::clone(self);
620 let later = move || {
621 inner.state.borrow_mut().later = false;
622 let program = inner
623 .state
624 .borrow()
625 .run
626 .as_ref()
627 .map(|run| run.program.clone());
628 if let Some(program) = program {
629 inner.deliver(false, &program);
630 }
631 TimeoutAction::Drop
632 };
633 event_loop.insert_timer(Instant::now(), Box::new(later));
634 }
635
636 fn deliver(&self, done: bool, program: &str) {
640 loop {
641 let (line, on_line, at) = {
642 let mut state = self.state.borrow_mut();
643 let line = match state.lines.next(&program) {
644 Some(line) => line,
645 None if done => match state.lines.rest(&program) {
646 Some(line) => line,
647 None => return,
648 },
649 None => return,
650 };
651 (line, state.on_line.clone(), state.at)
652 };
653 let Some(on_line) = on_line else {
654 continue;
655 };
656 if let Err(payload) = panic::catch_unwind(AssertUnwindSafe(|| on_line(&line))) {
657 let (name, at) = at.map_or(("on_line", String::new()), |(name, at)| {
658 (name, format!(" (called at {at})"))
659 });
660 let message = panic_message(payload.as_ref());
661 tracing::error!("process {program} {name}{at} panicked: {message}");
662 let mut state = self.state.borrow_mut();
663 if state
665 .on_line
666 .as_ref()
667 .is_some_and(|now| Rc::ptr_eq(now, &on_line))
668 {
669 state.on_line = None;
670 }
671 }
672 }
673 }
674
675 fn write(self: &Rc<Self>, data: &[u8]) {
676 let mut state = self.state.borrow_mut();
677 if state.closing {
678 return tracing::warn!("the program's input is closed, so a write was dropped");
679 }
680 let waiting = state.keep_stdin && state.run.is_none() && !state.ran;
682 match state.run.as_mut() {
683 Some(run) if run.stdin.is_some() => run.outbox.extend_from_slice(data),
684 _ if waiting => {
685 if state.input.len() + data.len() > MAX_INPUT {
686 return tracing::warn!("over 4 MiB written before the program started; dropped");
687 }
688 return state.input.extend_from_slice(data);
689 }
690 _ => return tracing::warn!("the program takes no input now, so a write was dropped"),
691 }
692 drop(state);
693 self.flush();
694 }
695
696 fn flush(self: &Rc<Self>) {
699 let Some(event_loop) = self.event_loop() else {
700 return;
701 };
702 let mut state = self.state.borrow_mut();
703 let closing = state.closing;
704 let Some(run) = state.run.as_mut() else {
705 return;
706 };
707 let Some(stdin) = run.stdin.as_mut() else {
708 return;
709 };
710 let mut broken = false;
711 while !run.outbox.is_empty() {
712 match stdin.write(&run.outbox) {
713 Ok(written) => {
714 run.outbox.drain(..written);
715 }
716 Err(error) if error.kind() == ErrorKind::Interrupted => {}
717 Err(error) if error.kind() == ErrorKind::WouldBlock => break,
718 Err(_) => {
719 broken = true;
720 break;
721 }
722 }
723 }
724 if run.outbox.len() > MAX_INPUT {
725 let program = &run.program;
726 tracing::warn!(
727 program,
728 "the program reads nothing it is sent; closing its input"
729 );
730 broken = true;
731 }
732 if broken {
733 drop(state);
734 return self.drop_stdin();
735 }
736 if run.outbox.is_empty() {
737 if let Some(writer) = run.writer.take() {
738 event_loop.remove(writer);
739 }
740 if closing {
741 run.stdin = None;
743 }
744 } else if run.writer.is_none()
745 && let Ok(fd) = stdin.as_fd().try_clone_to_owned()
746 {
747 let inner = Rc::clone(self);
748 run.writer = event_loop.insert_fd(fd, Interest::WRITE, Box::new(move || inner.flush()));
749 }
750 }
751
752 fn drop_stdin(&self) {
754 let writer = {
755 let mut state = self.state.borrow_mut();
756 state.input.clear();
757 let Some(run) = state.run.as_mut() else {
758 return;
759 };
760 run.stdin = None;
761 run.outbox.clear();
762 run.writer.take()
763 };
764 if let (Some(writer), Some(event_loop)) = (writer, self.event_loop()) {
765 event_loop.remove(writer);
766 }
767 }
768
769 fn stop_run(&self) {
772 let run = {
773 let mut state = self.state.borrow_mut();
774 let run = state.run.take();
775 if run.is_some() {
777 state.closing = false;
778 }
779 run
780 };
781 let Some(mut run) = run else {
782 return;
783 };
784 self.state.borrow_mut().lines.reset();
786 terminate(&run.child);
787 self.remove_sources(&mut run);
788 if let Some(shared) = &self.processes {
789 spawn::reap(shared, run.child, run.program);
790 }
791 }
792
793 fn stop(&self) {
795 let (starter, on_line) = {
796 let mut state = self.state.borrow_mut();
797 state.pending = None;
798 (state.starter.take(), state.on_line.take())
799 };
800 drop(on_line);
803 if let (Some(starter), Some(event_loop)) = (starter, self.event_loop()) {
804 event_loop.remove(starter);
805 }
806 self.stop_run();
807 }
808
809 fn cancel(&self) {
812 let starter = {
813 let mut state = self.state.borrow_mut();
814 state.pending = None;
815 state.ran = true;
816 state.closing = false;
817 state.starter.take()
818 };
819 if let (Some(starter), Some(event_loop)) = (starter, self.event_loop()) {
820 event_loop.remove(starter);
821 }
822 self.stop_run();
823 let ran = self.state.borrow().run.is_none();
824 if ran {
825 set(self.running, false);
826 }
827 }
828
829 fn remove_sources(&self, run: &mut Run) {
830 let tokens = [
831 run.exit.take(),
832 run.writer.take(),
833 run.out.stream.take().map(|(_, token)| token),
834 run.err.stream.take().map(|(_, token)| token),
835 ];
836 run.stdin = None;
837 if let Some(event_loop) = self.event_loop() {
838 for token in tokens.into_iter().flatten() {
839 event_loop.remove(token);
840 }
841 }
842 }
843}
844
845#[allow(
849 clippy::type_complexity,
850 reason = "the four parts of one spawn, used once"
851)]
852pub(crate) fn spawn_piped(
853 command: &mut Command,
854 keep_stdin: bool,
855) -> std::io::Result<(Child, PipeReader, PipeReader, Option<PipeWriter>)> {
856 let (out, child_out) = std::io::pipe()?;
857 let (err, child_err) = std::io::pipe()?;
858 rustix::io::ioctl_fionbio(&out, true)?;
859 rustix::io::ioctl_fionbio(&err, true)?;
860 let (stdin, child_in) = if keep_stdin {
861 let (theirs, ours) = std::io::pipe()?;
862 rustix::io::ioctl_fionbio(&ours, true)?;
863 (Some(ours), Stdio::from(theirs))
864 } else {
865 (None, Stdio::null())
866 };
867 spawn::untune(command);
868 let child = command
869 .stdout(child_out)
870 .stderr(child_err)
871 .stdin(child_in)
872 .process_group(0)
873 .spawn()?;
874 Ok((child, out, err, stdin))
875}
876
877impl Run {
878 fn output(&mut self, which: Which) -> &mut Output {
879 match which {
880 Which::Out => &mut self.out,
881 Which::Err => &mut self.err,
882 }
883 }
884
885 fn take(&mut self, which: Which, bytes: &[u8]) -> String {
888 let program = self.program.clone();
889 let output = self.output(which);
890 output.partial.extend_from_slice(bytes);
891 let mut text = if bytes.is_empty() {
892 String::from_utf8_lossy(&std::mem::take(&mut output.partial)).into_owned()
893 } else {
894 decode(&mut output.partial)
895 };
896 if output.capped {
898 text.clear();
899 }
900 let room = MAX_OUTPUT.saturating_sub(output.kept);
901 if text.len() > room {
902 if !std::mem::replace(&mut output.capped, true) {
903 tracing::warn!(program, "a program printed over 1 MiB; the rest isn't kept");
904 }
905 let mut end = room;
906 while !text.is_char_boundary(end) {
907 end -= 1;
908 }
909 text.truncate(end);
910 }
911 output.kept += text.len();
912 text
913 }
914}
915
916fn set<T: PartialEq + 'static>(signal: Signal<T>, value: T) {
919 if untrack(|| signal.try_with(|old| *old != value)) == Some(true) {
920 signal.try_set(value);
921 }
922}