1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
mod process;
pub(crate) mod source;
pub use process::pid::Pid;
use source::termination::ChildTerminationSource;
use std::{
    cell::Cell,
    collections::BTreeMap,
    io::{self, BufRead, ErrorKind},
    mem,
    path::{Path, PathBuf},
    process::{Child, ChildStdin, Command, ExitStatus},
    rc::Rc,
};

use mio::{
    event::{self, Source},
    Events, Interest, Poll, Token,
};
use slab::Slab;

pub use self::source::childout::FdTag;
use self::source::{childout::ChildOut, SourceInstruction};

#[cfg(feature = "signals")]
use source::signal::{Signal, SignalSource};

/// A handle to a child process that was spawned with `Muxer`.
pub struct ChildInfo {
    pub pid: Pid,
    pub stdin: Option<ChildStdin>,
    prog_path: Rc<PathBuf>,
    exit_status: Rc<Cell<Option<ExitStatus>>>,
}

impl ChildInfo {
    pub fn program(&self) -> &Path {
        &self.prog_path
    }

    pub fn exit_status(&self) -> Option<ExitStatus> {
        self.exit_status.get()
    }
}

/// A user-facing event emitted by the `Muxer`
#[derive(Debug)]
pub enum Event<'a> {
    ChildTerminated {
        pid: Pid,
        prog_path: &'a Path,
        exit_status: ExitStatus,
    },
    ChildWrote {
        pid: Pid,
        prog_path: &'a Path,
        tag: FdTag,
        line: &'a str,
    },
    FdClosed {
        pid: Pid,
        prog_path: &'a Path,
        tag: FdTag,
    },
    #[cfg(feature = "signals")]
    SignalReceived { signal: Signal },
}

/// A process Muxer
pub struct Muxer {
    poll: Poll,
    events: Events,
    children: BTreeMap<Pid, MuxerChild>,
    fds: Slab<EventSource>,
    state: State,
    wait_buffer: Vec<(Pid, Rc<PathBuf>, ExitStatus)>,
    // We don't need this field, an index into "events" would do, but the Events
    // type only exposes an iterator over references
    pending_events: Vec<event::Event>,
}

impl Muxer {
    pub fn new() -> io::Result<Self> {
        let mut res = Self {
            poll: Poll::new()?,
            wait_buffer: Vec::new(),
            events: Events::with_capacity(1024),
            children: BTreeMap::new(),
            fds: Slab::new(),
            state: State::Awaiting,
            pending_events: Vec::new(),
        };

        let wait_source = ChildTerminationSource::new()?;
        res.register(EventSource::ChildTerminated(wait_source));
        #[cfg(feature = "signals")]
        {
            let signal_source = SignalSource::new()?;
            res.register(EventSource::ReceivedSignal(signal_source));
        }
        Ok(res)
    }

    pub fn pids(&self) -> impl Iterator<Item = &Pid> {
        self.children.keys()
    }

    pub fn spawn(&mut self, mut cmd: Command) -> io::Result<ChildInfo> {
        let prog_path = PathBuf::from(cmd.get_program());

        let mut child = cmd.spawn()?;
        let pid = Pid { inner: child.id() };
        let registry = self.poll.registry();
        let prog_path = Rc::new(prog_path);

        let child_info = ChildInfo {
            pid,
            stdin: child.stdin.take(),
            prog_path: prog_path.clone(),
            exit_status: Rc::new(Cell::new(None)),
        };

        if let Some(stdout) = child.stdout.take() {
            let prog_path = prog_path.clone();
            let mut stdout = ChildOut::from_pipe(stdout, pid, prog_path);
            let entry = self.fds.vacant_entry();
            registry.register(&mut stdout, Token(entry.key()), Interest::READABLE)?;
            entry.insert(EventSource::ReadableChild(stdout));
        }

        if let Some(stderr) = child.stderr.take() {
            let prog_path = prog_path.clone();
            let mut stderr = ChildOut::from_pipe(stderr, pid, prog_path);
            let entry = self.fds.vacant_entry();
            registry.register(&mut stderr, Token(entry.key()), Interest::READABLE)?;
            entry.insert(EventSource::ReadableChild(stderr));
        }

        let muxer_child = MuxerChild {
            child,
            prog_path: prog_path.clone(),
            exit_status: child_info.exit_status.clone(),
        };

        self.children.insert(pid, muxer_child);
        Ok(child_info)
    }

    fn register(&mut self, mut evsrc: EventSource) {
        let entry = self.fds.vacant_entry();
        evsrc
            .register(self.poll.registry(), Token(entry.key()), Interest::READABLE)
            .unwrap();
        entry.insert(evsrc);
    }

    fn reregister(&mut self, mut evsrc: EventSource) {
        let entry = self.fds.vacant_entry();
        evsrc
            .reregister(self.poll.registry(), Token(entry.key()), Interest::READABLE)
            .unwrap();
        entry.insert(evsrc);
    }

    fn deregister(&mut self, mut evsrc: EventSource) {
        evsrc.deregister(self.poll.registry()).unwrap();
    }

    pub fn pump<R, F>(&mut self, mut func: F) -> R
    where
        F: FnMut(Event) -> Option<R>,
    {
        let mut state = mem::replace(&mut self.state, State::Awaiting);
        let (state, event) = loop {
            match state {
                State::Awaiting => match self.pending_events.pop() {
                    None => {
                        // fill our events buffer
                        loop {
                            match self.poll.poll(&mut self.events, None) {
                                Ok(()) => break,
                                Err(e) => match e.kind() {
                                    // if our poll is interrupted by a
                                    // system call then retry
                                    ErrorKind::Interrupted => continue,
                                    _ => panic!("Unexpected error during poll: {e}"),
                                },
                            }
                        }
                        // Since events buffer is opaque, we cannot suspend our iteration through it
                        // easily. So, we copy the contents to a vector that we can pop from.
                        self.pending_events.extend(self.events.iter().cloned());

                        self.events.clear();
                    }
                    // We have some event to handle. In these cases we
                    // potentially have many events to handle before
                    // reregistering the handle, but pump doesn't assume we are
                    // prepared to handle them all before returning, so we
                    // transition the state from Awaiting to a resource specific
                    // state representing draining all pending events of some
                    // type before reregistering the underlying fd.
                    Some(ev) => match self.fds.remove(ev.token().0) {
                        EventSource::ChildTerminated(mut w) => {
                            match w.handle_event(&mut self.children, &mut self.wait_buffer) {
                                SourceInstruction::Reregister => {
                                    self.reregister(EventSource::ChildTerminated(w));
                                    state = State::DrainingChildTerminated;
                                }
                                SourceInstruction::Deregister => {
                                    self.deregister(EventSource::ChildTerminated(w));
                                }
                            }
                        }
                        EventSource::ReadableChild(child_out) => {
                            state = State::DrainingChildOut(child_out);
                        }
                        #[cfg(feature = "signals")]
                        EventSource::ReceivedSignal(signal_source) => {
                            state = State::DrainingSignals(signal_source);
                        }
                    },
                },
                State::DrainingChildTerminated => match self.wait_buffer.pop() {
                    None => state = State::Awaiting,
                    Some((pid, prog_path, exit_status)) => {
                        let event = Event::ChildTerminated {
                            pid,
                            prog_path: &prog_path,
                            exit_status,
                        };
                        match func(event) {
                            None => state = State::DrainingChildTerminated,
                            Some(r) => {
                                break (State::DrainingChildTerminated, r);
                            }
                        }
                    }
                },
                State::DrainingChildOut(mut child_out) => {
                    let fd = &mut child_out.fd;
                    let buf: &mut String = &mut child_out.buf;
                    match fd.read_line(buf) {
                        Ok(0) => {
                            // The fd was closed; we must deregister the fd and
                            // return to the awaiting state.
                            child_out
                                .fd
                                .get_mut()
                                .deregister(self.poll.registry())
                                .unwrap();
                            let event = Event::FdClosed {
                                pid: child_out.pid,
                                tag: child_out.tag,
                                prog_path: &child_out.prog_path,
                            };
                            match func(event) {
                                None => state = State::Awaiting,
                                Some(r) => {
                                    break (State::Awaiting, r);
                                }
                            }
                        }
                        Ok(_) => {
                            let event = Event::ChildWrote {
                                pid: child_out.pid,
                                tag: child_out.tag,
                                prog_path: &child_out.prog_path,
                                line: buf,
                            };
                            let ores = func(event);
                            buf.clear();
                            match ores {
                                None => state = State::DrainingChildOut(child_out),
                                Some(r) => {
                                    break (State::DrainingChildOut(child_out), r);
                                }
                            }
                        }
                        // maybe we want to break in the future if we start
                        // listening for SIGALRM
                        Err(e) if e.kind() == ErrorKind::Interrupted => {
                            state = State::DrainingChildOut(child_out)
                        }
                        Err(e) if e.kind() == ErrorKind::WouldBlock => {
                            self.reregister(EventSource::ReadableChild(child_out));
                            state = State::Awaiting;
                        }
                        Err(e) => panic!("Unexpected error when reading child output: {e}"),
                    }
                }
                #[cfg(feature = "signals")]
                State::DrainingSignals(mut signal_source) => match signal_source.next() {
                    EventStream::Emit(signal) => {
                        let event = Event::SignalReceived { signal };
                        match func(event) {
                            Some(r) => {
                                break (State::DrainingSignals(signal_source), r);
                            }
                            None => state = State::DrainingSignals(signal_source),
                        }
                    }
                    EventStream::Drained(source_instruction) => {
                        let event_source = EventSource::ReceivedSignal(signal_source);
                        match source_instruction {
                            SourceInstruction::Reregister => self.reregister(event_source),
                            SourceInstruction::Deregister => self.deregister(event_source),
                        }
                        state = State::Awaiting;
                    }
                },
            }
        };
        self.state = state;
        event
    }
}

enum EventSource {
    ReadableChild(ChildOut),
    ChildTerminated(ChildTerminationSource),
    #[cfg(feature = "signals")]
    ReceivedSignal(SignalSource),
}

impl Source for EventSource {
    fn register(
        &mut self,
        registry: &mio::Registry,
        token: Token,
        interests: Interest,
    ) -> io::Result<()> {
        match self {
            EventSource::ReadableChild(x) => x.register(registry, token, interests),
            EventSource::ChildTerminated(x) => x.register(registry, token, interests),
            #[cfg(feature = "signals")]
            EventSource::ReceivedSignal(x) => x.register(registry, token, interests),
        }
    }

    fn reregister(
        &mut self,
        registry: &mio::Registry,
        token: Token,
        interests: Interest,
    ) -> io::Result<()> {
        match self {
            EventSource::ReadableChild(x) => x.reregister(registry, token, interests),
            EventSource::ChildTerminated(x) => x.reregister(registry, token, interests),
            #[cfg(feature = "signals")]
            EventSource::ReceivedSignal(x) => x.reregister(registry, token, interests),
        }
    }

    fn deregister(&mut self, registry: &mio::Registry) -> io::Result<()> {
        match self {
            EventSource::ReadableChild(x) => x.deregister(registry),
            EventSource::ChildTerminated(x) => x.deregister(registry),
            #[cfg(feature = "signals")]
            EventSource::ReceivedSignal(x) => x.deregister(registry),
        }
    }
}

#[derive(Debug)]
enum State {
    Awaiting,
    DrainingChildOut(ChildOut),
    DrainingChildTerminated,
    #[cfg(feature = "signals")]
    DrainingSignals(SignalSource),
}

pub struct MuxerChild {
    child: Child,
    prog_path: Rc<PathBuf>,
    exit_status: Rc<Cell<Option<ExitStatus>>>,
}