Skip to main content

crabshell_services/
process.rs

1//! Programs a config runs, as Quickshell's `Process`: what they print,
2//! whole or line by line, and how they ended.
3
4#[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
27/// The most of each output kept in [`Process::stdout`] and
28/// [`Process::stderr`]. Lines still reach `on_line` past it.
29pub(crate) const MAX_OUTPUT: usize = 1024 * 1024;
30
31/// The most bytes left to write to a program's input before it is closed,
32/// so a program that reads nothing can't grow the shell without end.
33pub(crate) const MAX_INPUT: usize = 4 * 1024 * 1024;
34
35/// What `cx.process` and `cx.exec_detached` need from the shell: the loop.
36pub(crate) struct Processes {
37  pub(crate) shared: Rc<Shared>,
38  /// Every `cx.process`, so the shell can stop them all before it exits.
39  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
51/// Stops every program `cx.process` started, as the shell does before it
52/// exits: `exit` skips the drops that would, and each program has a
53/// process group of its own, which nothing else signals.
54pub(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
69/// What `cx.process` and `cx.exec_detached` run: a [`Command`], or the
70/// program and its arguments as a list, such as `["sleep", "30"]`, as
71/// Quickshell's `command` is.
72pub trait IntoCommand {
73  /// The command, or `None` for an empty list.
74  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/// A program the shell runs, from `cx.process(command)`. A clone is cheap
99/// and shares it.
100///
101/// It starts on the loop's next turn, so `on_line` and `with_stdin` set
102/// right after `cx.process` apply to it. It runs in a process group of its
103/// own, and that group gets `SIGTERM` when the context that started it
104/// goes or the command starts again. Nothing polls: the shell wakes when
105/// the program prints, takes input or ends.
106#[derive(Clone)]
107pub struct Process {
108  inner: Rc<Inner>,
109}
110
111struct Inner {
112  /// `None` when the shell provided no services.
113  processes: Option<Rc<Shared>>,
114  /// These live under the starting context's owner.
115  running: Signal<bool>,
116  status: Signal<Option<ExitStatus>>,
117  stdout: Signal<String>,
118  stderr: Signal<String>,
119  /// Read by the effect that builds the command, so bumping it starts
120  /// the command again.
121  restarts: Signal<u64>,
122  state: RefCell<State>,
123}
124
125#[derive(Default)]
126struct State {
127  /// The command to start at the loop's next turn, and the timer that
128  /// will.
129  pending: Option<Command>,
130  starter: Option<RegistrationToken>,
131  run: Option<Run>,
132  keep_stdin: bool,
133  /// Written before the program started, for its input.
134  input: Vec<u8>,
135  /// The config closed the input: it closes once what was written is
136  /// sent.
137  closing: bool,
138  /// A run ended, failed to start or was killed before it started, and
139  /// nothing is to start: writes and closes have no run to wait for.
140  ran: bool,
141  on_line: Option<OnLine>,
142  /// Which method set `on_line` and where, for the log if it panics.
143  at: Option<(&'static str, &'static Location<'static>)>,
144  /// What the run printed on its standard output and wasn't passed on
145  /// yet. Split at a newline until a handler says otherwise, so one set
146  /// late still gets the start of its first line.
147  lines: Lines,
148  /// A next-turn delivery is on its way, and passes on all that waits.
149  later: bool,
150  /// From [`Process::stopped`]: nothing starts until a restart.
151  stopped: bool,
152}
153
154/// What a config does with each line.
155type OnLine = Rc<dyn Fn(&str)>;
156
157/// The program running now.
158struct 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/// One of a program's outputs, read as it comes.
170#[derive(Default)]
171struct Output {
172  stream: Option<(PipeReader, RegistrationToken)>,
173  /// The start of a character a read cut off.
174  partial: Vec<u8>,
175  /// How much is in the signal, and whether the cap was logged.
176  kept: usize,
177  capped: bool,
178}
179
180#[derive(Clone, Copy, PartialEq)]
181enum Which {
182  Out,
183  Err,
184}
185
186/// `cx.process(command)`.
187#[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 from the start: the effect below starts it at the next
203    // flush.
204    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  // Dropped with the starting context's owner, which stops the program.
212  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  // The command is the config's: built in an effect, so it is built again
221  // when a signal it read changes. A panic stops the run before and is
222  // logged; the next change tries again.
223  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
241/// `cx.exec_detached(command)`.
242pub(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    // Logged there.
256    let _ = spawn::spawn(&shared, &mut command);
257  });
258}
259
260/// Stops the program when the starting context's owner drops it.
261struct StopOnDrop(Rc<Inner>);
262
263impl Drop for StopOnDrop {
264  fn drop(&mut self) {
265    self.0.stop();
266  }
267}
268
269impl Process {
270  /// `true` from `cx.process` or a restart until the program ends or
271  /// can't start.
272  pub fn running(&self) -> ReadSignal<bool> {
273    self.inner.running.read_only()
274  }
275
276  /// How the last run ended: `None` while it runs, or if it couldn't
277  /// start.
278  pub fn exit_status(&self) -> ReadSignal<Option<ExitStatus>> {
279    self.inner.status.read_only()
280  }
281
282  /// Everything it printed on its standard output this run, up to 1 MiB,
283  /// decoded as UTF-8 with bad bytes replaced. Complete once
284  /// [`exit_status`](Self::exit_status) is set.
285  pub fn stdout(&self) -> ReadSignal<String> {
286    self.inner.stdout.read_only()
287  }
288
289  /// Like [`stdout`](Self::stdout), for its standard error.
290  pub fn stderr(&self) -> ReadSignal<String> {
291    self.inner.stderr.read_only()
292  }
293
294  /// Calls `on_line` with each line of its standard output, without its
295  /// `\n`, decoded as UTF-8 with bad bytes replaced, as it comes. What is
296  /// left when it ends counts as a last line. A line over 1 MiB is
297  /// dropped and logged.
298  ///
299  /// It runs on the main thread. If it panics, that is logged with the
300  /// program and where `on_line` was called, and it isn't called again.
301  #[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  /// Like [`on_line`](Self::on_line), with lines that end at `marker`
307  /// instead of `\n`, such as `"\0"` for `find -print0`, as Quickshell's
308  /// `SplitParser`. An empty `marker` passes on each read as it comes, up
309  /// to 64 KiB, keeping a character the read cut off for the next.
310  ///
311  /// It replaces `on_line`, and everything not passed on yet is split at
312  /// the new marker: set from a handler, from the line after the one it
313  /// was given; set elsewhere, on the loop's next turn.
314  #[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  /// Gives the program an input that [`write`](Self::write) feeds, open
334  /// until [`close_stdin`](Self::close_stdin). Without it the program's
335  /// input is empty. It applies from the next start, so call it right
336  /// after `cx.process`.
337  pub fn with_stdin(self) -> Process {
338    self.inner.state.borrow_mut().keep_stdin = true;
339    self
340  }
341
342  /// Sends `data` to the program's input, as it takes it; written before
343  /// the program started, it waits for the start. Without
344  /// [`with_stdin`](Self::with_stdin), or once the input is closed, `data`
345  /// is dropped and that is logged. If more than 4 MiB wait because the
346  /// program reads nothing, its input is closed.
347  pub fn write(&self, data: impl AsRef<[u8]>) {
348    self.inner.write(data.as_ref());
349  }
350
351  /// Closes the program's input once what was written is sent, so it
352  /// reads the end of it.
353  pub fn close_stdin(&self) {
354    {
355      let mut state = self.inner.state.borrow_mut();
356      // Nothing runs or is to start: the next run's input starts open.
357      if state.run.is_none() && state.ran {
358        return;
359      }
360      state.closing = true;
361    }
362    self.inner.flush();
363  }
364
365  /// Sends `SIGTERM` to the program's process group. Its exit status
366  /// comes when it ends.
367  /// Before it started, it doesn't start.
368  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  /// Stops the program if it runs and starts the command again, as when
376  /// a signal the command read changed. A [`stopped`](Self::stopped) one
377  /// starts.
378  pub fn restart(&self) {
379    self.inner.state.borrow_mut().stopped = false;
380    self.inner.restarts.try_update(|count| *count += 1);
381  }
382
383  /// Doesn't start the program until [`restart`](Self::restart), as
384  /// Quickshell's `Process` with `running: false`, so a program run on
385  /// demand, such as a button's, is one process restarted per click.
386  /// Until then the command isn't built, a signal it reads starts
387  /// nothing, [`running`](Self::running) is `false`, and writes are
388  /// dropped. Call it right after `cx.process`; later, it stops a run
389  /// about to start, but not one already running.
390  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
399/// Sends `SIGTERM` to `child`'s process group.
400fn terminate(child: &Child) {
401  // The group is the child's own, from `process_group(0)`.
402  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  /// Stops the program running now, if one is, and starts `command` at
417  /// the loop's next turn. An empty command is logged and runs nothing.
418  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    // Beside the old shell of a reload, its own run of the program is still
453    // up; a second one could be refused or double what it does.
454    if let Some(shared) = self.shared()
455      && !shared.alone.get()
456    {
457      self.state.borrow_mut().starter = None;
458      let inner = Rc::downgrade(self);
459      shared.when_alone(move || {
460        if let Some(inner) = inner.upgrade() {
461          inner.spawn_pending();
462        }
463      });
464      return;
465    }
466    let (command, keep_stdin, input) = {
467      let mut state = self.state.borrow_mut();
468      state.starter = None;
469      let input = std::mem::take(&mut state.input);
470      (state.pending.take(), state.keep_stdin, input)
471    };
472    let Some(mut command) = command else {
473      return;
474    };
475    let program = command.get_program().to_string_lossy().into_owned();
476    let spawned = spawn_piped(&mut command, keep_stdin);
477    // The command holds the program's ends of the pipes. They must close
478    // here, or its outputs would never end.
479    drop(command);
480    let (child, out, err, stdin) = match spawned {
481      Ok(spawned) => spawned,
482      Err(error) => {
483        tracing::warn!(program, %error, "couldn't start the program");
484        let mut state = self.state.borrow_mut();
485        state.ran = true;
486        state.closing = false;
487        drop(state);
488        set(self.running, false);
489        return;
490      }
491    };
492    let mut run = Run {
493      child,
494      program,
495      exit: None,
496      out: Output::default(),
497      err: Output::default(),
498      stdin,
499      outbox: input,
500      writer: None,
501    };
502    self.state.borrow_mut().lines.reset();
503    for (which, stream) in [(Which::Out, out), (Which::Err, err)] {
504      let Ok(fd) = stream.as_fd().try_clone_to_owned() else {
505        continue;
506      };
507      let inner = Rc::clone(self);
508      let read = Box::new(move || inner.on_output(which));
509      if let Some(token) = event_loop.insert_fd(fd, Interest::READ, read) {
510        run.output(which).stream = Some((stream, token));
511      }
512    }
513    match pidfd_open(Pid::from_child(&run.child), PidfdFlags::NONBLOCK) {
514      Ok(fd) => {
515        let inner = Rc::clone(self);
516        let ended = Box::new(move || inner.on_exit());
517        run.exit = event_loop.insert_fd(fd, Interest::READ, ended);
518      }
519      Err(error) => tracing::warn!(%error, "couldn't follow a program to its end"),
520    }
521    self.state.borrow_mut().run = Some(run);
522    // Whatever was written before it started.
523    self.flush();
524  }
525
526  /// Reads what the program printed on one output.
527  fn on_output(&self, which: Which) {
528    let mut chunk = [0; 64 * 1024];
529    let mut state = self.state.borrow_mut();
530    let State { run, lines, .. } = &mut *state;
531    let Some(run) = run.as_mut() else {
532      return;
533    };
534    let Some((stream, _)) = run.output(which).stream.as_mut() else {
535      return;
536    };
537    let read = match stream.read(&mut chunk) {
538      Ok(read) => read,
539      Err(error) if matches!(error.kind(), ErrorKind::WouldBlock | ErrorKind::Interrupted) => {
540        return;
541      }
542      Err(_) => 0,
543    };
544    let text = run.take(which, &chunk[..read]);
545    if which == Which::Out {
546      lines.append(&chunk[..read]);
547    }
548    let program = run.program.clone();
549    if read == 0 {
550      // It closed this output; its exit is still to come.
551      let token = run.output(which).stream.take().map(|(_, token)| token);
552      if let (Some(token), Some(event_loop)) = (token, self.event_loop()) {
553        event_loop.remove(token);
554      }
555    }
556    drop(state);
557    self.show(which, text);
558    self.deliver(false, &program);
559  }
560
561  /// The program ended: reads what is left of its outputs, then shows how
562  /// it ended.
563  fn on_exit(&self) {
564    let mut state = self.state.borrow_mut();
565    let Some(run) = state.run.as_mut() else {
566      return;
567    };
568    let status = match run.child.try_wait() {
569      Ok(Some(status)) => Some(status),
570      Ok(None) => return,
571      // Reaped behind the shell's back: its end is all there is to know.
572      Err(error) => {
573        tracing::warn!(%error, program = run.program, "lost track of the program");
574        None
575      }
576    };
577    let mut run = state.run.take().expect("invariant: matched just above");
578    // The next run's input starts open.
579    state.closing = false;
580    state.ran = true;
581    drop(state);
582    // What it printed before it ended is in the sockets now. A program it
583    // started may hold them open, so read only what is there.
584    // A program it started that keeps printing would keep this going, so
585    // it stops after what the signal keeps.
586    let mut chunk = [0; 64 * 1024];
587    for which in [Which::Out, Which::Err] {
588      let mut budget = MAX_OUTPUT;
589      while let Some((stream, _)) = run.output(which).stream.as_mut()
590        && budget > 0
591      {
592        let Ok(read @ 1..) = stream.read(&mut chunk) else {
593          break;
594        };
595        budget = budget.saturating_sub(read);
596        let text = run.take(which, &chunk[..read]);
597        if which == Which::Out {
598          self.state.borrow_mut().lines.append(&chunk[..read]);
599        }
600        self.show(which, text);
601        self.deliver(false, &run.program);
602      }
603      let text = run.take(which, &[]);
604      self.show(which, text);
605    }
606    self.deliver(true, &run.program);
607    self.remove_sources(&mut run);
608    set(self.status, status);
609    set(self.running, false);
610  }
611
612  /// Adds `text` to the output's signal.
613  fn show(&self, which: Which, text: String) {
614    if text.is_empty() {
615      return;
616    }
617    let signal = match which {
618      Which::Out => self.stdout,
619      Which::Err => self.stderr,
620    };
621    signal.try_update(|all| all.push_str(&text));
622  }
623
624  /// Passes on what waits at the loop's next turn, as a marker set outside
625  /// a handler asks.
626  fn deliver_later(self: &Rc<Self>) {
627    let Some(event_loop) = self.event_loop() else {
628      return;
629    };
630    if std::mem::replace(&mut self.state.borrow_mut().later, true) {
631      return;
632    }
633    let inner = Rc::clone(self);
634    let later = move || {
635      inner.state.borrow_mut().later = false;
636      let program = inner
637        .state
638        .borrow()
639        .run
640        .as_ref()
641        .map(|run| run.program.clone());
642      if let Some(program) = program {
643        inner.deliver(false, &program);
644      }
645      TimeoutAction::Drop
646    };
647    event_loop.insert_timer(Instant::now(), Box::new(later));
648  }
649
650  /// Passes each whole line waiting to the handler set at the time, one at
651  /// a time with nothing borrowed, so one that sets another marker has the
652  /// rest split at it. Once the program is `done`, what is left goes last.
653  fn deliver(&self, done: bool, program: &str) {
654    loop {
655      let (line, on_line, at) = {
656        let mut state = self.state.borrow_mut();
657        let line = match state.lines.next(&program) {
658          Some(line) => line,
659          None if done => match state.lines.rest(&program) {
660            Some(line) => line,
661            None => return,
662          },
663          None => return,
664        };
665        (line, state.on_line.clone(), state.at)
666      };
667      let Some(on_line) = on_line else {
668        continue;
669      };
670      if let Err(payload) = panic::catch_unwind(AssertUnwindSafe(|| on_line(&line))) {
671        let (name, at) = at.map_or(("on_line", String::new()), |(name, at)| {
672          (name, format!(" (called at {at})"))
673        });
674        let message = panic_message(payload.as_ref());
675        tracing::error!("process {program} {name}{at} panicked: {message}");
676        let mut state = self.state.borrow_mut();
677        // Unless it set a new one meanwhile.
678        if state
679          .on_line
680          .as_ref()
681          .is_some_and(|now| Rc::ptr_eq(now, &on_line))
682        {
683          state.on_line = None;
684        }
685      }
686    }
687  }
688
689  fn write(self: &Rc<Self>, data: &[u8]) {
690    let mut state = self.state.borrow_mut();
691    if state.closing {
692      return tracing::warn!("the program's input is closed, so a write was dropped");
693    }
694    // Not started yet, so kept for the start.
695    let waiting = state.keep_stdin && state.run.is_none() && !state.ran;
696    match state.run.as_mut() {
697      Some(run) if run.stdin.is_some() => run.outbox.extend_from_slice(data),
698      _ if waiting => {
699        if state.input.len() + data.len() > MAX_INPUT {
700          return tracing::warn!("over 4 MiB written before the program started; dropped");
701        }
702        return state.input.extend_from_slice(data);
703      }
704      _ => return tracing::warn!("the program takes no input now, so a write was dropped"),
705    }
706    drop(state);
707    self.flush();
708  }
709
710  /// Writes what the program's input takes, and waits for room for the
711  /// rest.
712  fn flush(self: &Rc<Self>) {
713    let Some(event_loop) = self.event_loop() else {
714      return;
715    };
716    let mut state = self.state.borrow_mut();
717    let closing = state.closing;
718    let Some(run) = state.run.as_mut() else {
719      return;
720    };
721    let Some(stdin) = run.stdin.as_mut() else {
722      return;
723    };
724    let mut broken = false;
725    while !run.outbox.is_empty() {
726      match stdin.write(&run.outbox) {
727        Ok(written) => {
728          run.outbox.drain(..written);
729        }
730        Err(error) if error.kind() == ErrorKind::Interrupted => {}
731        Err(error) if error.kind() == ErrorKind::WouldBlock => break,
732        Err(_) => {
733          broken = true;
734          break;
735        }
736      }
737    }
738    if run.outbox.len() > MAX_INPUT {
739      let program = &run.program;
740      tracing::warn!(
741        program,
742        "the program reads nothing it is sent; closing its input"
743      );
744      broken = true;
745    }
746    if broken {
747      drop(state);
748      return self.drop_stdin();
749    }
750    if run.outbox.is_empty() {
751      if let Some(writer) = run.writer.take() {
752        event_loop.remove(writer);
753      }
754      if closing {
755        // All sent: the program reads the end.
756        run.stdin = None;
757      }
758    } else if run.writer.is_none()
759      && let Ok(fd) = stdin.as_fd().try_clone_to_owned()
760    {
761      let inner = Rc::clone(self);
762      run.writer = event_loop.insert_fd(fd, Interest::WRITE, Box::new(move || inner.flush()));
763    }
764  }
765
766  /// Closes the program's input now, dropping what wasn't sent.
767  fn drop_stdin(&self) {
768    let writer = {
769      let mut state = self.state.borrow_mut();
770      state.input.clear();
771      let Some(run) = state.run.as_mut() else {
772        return;
773      };
774      run.stdin = None;
775      run.outbox.clear();
776      run.writer.take()
777    };
778    if let (Some(writer), Some(event_loop)) = (writer, self.event_loop()) {
779      event_loop.remove(writer);
780    }
781  }
782
783  /// Stops the program running now, if one is: `SIGTERM` to its group,
784  /// its outputs closed, and it is left to be reaped.
785  fn stop_run(&self) {
786    let run = {
787      let mut state = self.state.borrow_mut();
788      let run = state.run.take();
789      // The next run's input starts open.
790      if run.is_some() {
791        state.closing = false;
792      }
793      run
794    };
795    let Some(mut run) = run else {
796      return;
797    };
798    // Its lines aren't passed on after it.
799    self.state.borrow_mut().lines.reset();
800    terminate(&run.child);
801    self.remove_sources(&mut run);
802    if let Some(shared) = &self.processes {
803      spawn::reap(shared, run.child, run.program);
804    }
805  }
806
807  /// Stops everything, for good, as the starting context went.
808  fn stop(&self) {
809    let (starter, on_line) = {
810      let mut state = self.state.borrow_mut();
811      state.pending = None;
812      (state.starter.take(), state.on_line.take())
813    };
814    // Dropped outside the borrow: it may hold this process, and its drop
815    // may use it.
816    drop(on_line);
817    if let (Some(starter), Some(event_loop)) = (starter, self.event_loop()) {
818      event_loop.remove(starter);
819    }
820    self.stop_run();
821  }
822
823  /// Stops the run and drops a start still to come, as when the command
824  /// panicked or the config killed it before it started.
825  fn cancel(&self) {
826    let starter = {
827      let mut state = self.state.borrow_mut();
828      state.pending = None;
829      state.ran = true;
830      state.closing = false;
831      state.starter.take()
832    };
833    if let (Some(starter), Some(event_loop)) = (starter, self.event_loop()) {
834      event_loop.remove(starter);
835    }
836    self.stop_run();
837    let ran = self.state.borrow().run.is_none();
838    if ran {
839      set(self.running, false);
840    }
841  }
842
843  fn remove_sources(&self, run: &mut Run) {
844    let tokens = [
845      run.exit.take(),
846      run.writer.take(),
847      run.out.stream.take().map(|(_, token)| token),
848      run.err.stream.take().map(|(_, token)| token),
849    ];
850    run.stdin = None;
851    if let Some(event_loop) = self.event_loop() {
852      for token in tokens.into_iter().flatten() {
853        event_loop.remove(token);
854      }
855    }
856  }
857}
858
859/// The program's outputs, and its input if wanted, through pipes, whose
860/// ends here are non-blocking. Pipes, not socket pairs: a program can
861/// open `/dev/stdout` on a pipe.
862#[allow(
863  clippy::type_complexity,
864  reason = "the four parts of one spawn, used once"
865)]
866pub(crate) fn spawn_piped(
867  command: &mut Command,
868  keep_stdin: bool,
869) -> std::io::Result<(Child, PipeReader, PipeReader, Option<PipeWriter>)> {
870  let (out, child_out) = std::io::pipe()?;
871  let (err, child_err) = std::io::pipe()?;
872  rustix::io::ioctl_fionbio(&out, true)?;
873  rustix::io::ioctl_fionbio(&err, true)?;
874  let (stdin, child_in) = if keep_stdin {
875    let (theirs, ours) = std::io::pipe()?;
876    rustix::io::ioctl_fionbio(&ours, true)?;
877    (Some(ours), Stdio::from(theirs))
878  } else {
879    (None, Stdio::null())
880  };
881  spawn::untune(command);
882  let child = command
883    .stdout(child_out)
884    .stderr(child_err)
885    .stdin(child_in)
886    .process_group(0)
887    .spawn()?;
888  Ok((child, out, err, stdin))
889}
890
891impl Run {
892  fn output(&mut self, which: Which) -> &mut Output {
893    match which {
894      Which::Out => &mut self.out,
895      Which::Err => &mut self.err,
896    }
897  }
898
899  /// `bytes` read from one output: the text to add to its signal. Empty
900  /// `bytes` means the output ended, which lets a cut-off character go.
901  fn take(&mut self, which: Which, bytes: &[u8]) -> String {
902    let program = self.program.clone();
903    let output = self.output(which);
904    output.partial.extend_from_slice(bytes);
905    let mut text = if bytes.is_empty() {
906      String::from_utf8_lossy(&std::mem::take(&mut output.partial)).into_owned()
907    } else {
908      decode(&mut output.partial)
909    };
910    // Once a character didn't fit, nothing after it is kept either.
911    if output.capped {
912      text.clear();
913    }
914    let room = MAX_OUTPUT.saturating_sub(output.kept);
915    if text.len() > room {
916      if !std::mem::replace(&mut output.capped, true) {
917        tracing::warn!(program, "a program printed over 1 MiB; the rest isn't kept");
918      }
919      let mut end = room;
920      while !text.is_char_boundary(end) {
921        end -= 1;
922      }
923      text.truncate(end);
924    }
925    output.kept += text.len();
926    text
927  }
928}
929
930/// Sets `signal` unless it holds `value` or is gone. The read is
931/// untracked, so an effect that starts a process doesn't run again for it.
932fn set<T: PartialEq + 'static>(signal: Signal<T>, value: T) {
933  if untrack(|| signal.try_with(|old| *old != value)) == Some(true) {
934    signal.try_set(value);
935  }
936}