cosca 0.10.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! The state both the acceptor thread and the caller's threads act on, behind one lock.
//!
//! No thread holds the lock across a blocking call: every socket call made under it is nonblocking.

use std::io::{PipeReader, PipeWriter};
use std::os::fd::{AsFd, OwnedFd};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};

use rustix::event::{PollFd, PollFlags};
use rustix::io::Errno;

use super::outcome::{classify, AcceptorFailure, KillError, KillOutcome, LinkOutcome, NotStarted, NotStartedCause};
use super::probe::{DropReason, LinkEvent, Probe};
use super::sys::{self, Read};
use crate::elevation::shim::protocol::Command;

/// Whether a shim has been told to start the program. Moves only `Pending` to `Live` or `Pending`
/// to `Refused`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StartState {
    Pending,
    Live,
    Refused,
}

/// The length of a frame; see `protocol::decode_frame`.
const FRAME_LEN: usize = 5;

pub(super) struct Inner {
    start: StartState,
    /// Root peers that said hello.
    hello_seen: bool,
    /// The frame read so far.
    frame: [u8; FRAME_LEN],
    frame_len: usize,
    outcome: Option<LinkOutcome>,
    failure: Option<AcceptorFailure>,
    /// The socket path has been removed.
    unlinked: bool,
}

/// What [`Shared::observe`] saw.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Observed {
    pub(crate) start: StartState,
    pub(crate) outcome: Option<LinkOutcome>,
    pub(crate) acceptor_failure: Option<AcceptorFailure>,
}

pub(super) struct Shared {
    inner: Mutex<Inner>,
    /// The shim's connection, set once, by the `Pending` to `Live` transition. Closed only in
    /// teardown: a reader never closes it.
    pub(super) conn: OnceLock<UnixStream>,
    /// Written once, when the outcome is set, so a waiter that found nothing to read and then lost
    /// the frame to another reader still wakes: it polls this next to `conn`.
    pub(super) settled: (PipeReader, PipeWriter),
    /// Only for logs and tests: the socket is removed through `dir`, never by this path.
    pub(super) sock_path: PathBuf,
    /// The private directory, open: the socket is removed relative to it.
    dir: OwnedFd,
    /// The euid a shim must have: root, in production.
    pub(super) peer_euid: u32,
    pub(super) probe: Probe,
    /// Set in this process's memory before the stop byte is written. The acceptor stops only when it
    /// is set: a byte written by a fork copy, which cannot set the owner's flag, is consumed and
    /// ignored.
    stopping: AtomicBool,
}

impl Shared {
    pub(super) fn new(
        sock_path: PathBuf,
        dir: OwnedFd,
        peer_euid: u32,
        probe: Probe,
        settled: (PipeReader, PipeWriter),
    ) -> Self {
        Shared {
            inner: Mutex::new(Inner {
                start: StartState::Pending,
                hello_seen: false,
                frame: [0; FRAME_LEN],
                frame_len: 0,
                outcome: None,
                failure: None,
                unlinked: false,
            }),
            conn: OnceLock::new(),
            settled,
            sock_path,
            dir,
            peer_euid,
            probe,
            stopping: AtomicBool::new(false),
        }
    }

    /// Recovers a poisoned lock so a panic cannot wedge teardown.
    pub(super) fn lock(&self) -> MutexGuard<'_, Inner> {
        self.inner.lock().unwrap_or_else(PoisonError::into_inner)
    }

    fn unlink(&self, inner: &mut Inner) {
        if std::mem::replace(&mut inner.unlinked, true) {
            return;
        }
        match sys::unlink_socket(self.dir.as_fd(), super::SOCKET_NAME) {
            Ok(()) => {}
            Err(Errno::NOENT) => {
                log::debug!("the socket {} was already gone", self.sock_path.display());
            }
            Err(e) => log::warn!(
                "cannot remove the socket {}: {report}",
                self.sock_path.display(),
                report = crate::error::Report(&e)
            ),
        }
    }

    pub(super) fn request_stop(&self) {
        self.stopping.store(true, Ordering::SeqCst);
    }

    pub(super) fn is_stopping(&self) -> bool {
        self.stopping.load(Ordering::SeqCst)
    }

    /// `Pending` to `Refused` in this process's memory only: no path is removed, and the lock is only
    /// tried, since a fork copy may hold a snapshot of it locked. Returns whether it took effect.
    pub(super) fn refuse_pending_in_memory(&self) -> bool {
        let Ok(mut inner) = self.inner.try_lock() else {
            return false;
        };
        if inner.start == StartState::Pending {
            inner.start = StartState::Refused;
        }
        true
    }

    /// `Pending` to `Refused`, removing the path in the same critical section.
    pub(super) fn refuse_pending(&self, inner: &mut Inner) {
        if inner.start == StartState::Pending {
            inner.start = StartState::Refused;
            self.unlink(inner);
        }
    }

    /// The acceptor cannot go on: refuses the start unless a shim already has `A`.
    pub(super) fn fail_closed(&self, failure: AcceptorFailure) {
        let mut inner = self.lock();
        match inner.start {
            StartState::Pending | StartState::Refused => {
                log::error!(
                    "the acceptor for {} failed ({failure}); the start is refused",
                    self.sock_path.display()
                );
                inner.failure.get_or_insert(failure);
                self.refuse_pending(&mut inner);
            }
            StartState::Live => {
                log::warn!(
                    "the acceptor for {} stopped ({failure}); the running shim is unaffected",
                    self.sock_path.display()
                );
                self.unlink(&mut inner);
            }
        }
    }

    /// A root peer said hello: answers it under the lock.
    pub(super) fn answer_hello(&self, conn: UnixStream) {
        let mut inner = self.lock();
        let further = std::mem::replace(&mut inner.hello_seen, true);
        let path = self.sock_path.display();
        if inner.start == StartState::Pending {
            match sys::send_answer(conn.as_fd(), Command::Allow.encode()) {
                Ok(()) => {
                    inner.start = StartState::Live;
                    let set = self.conn.set(conn);
                    debug_assert!(set.is_ok(), "the connection is set once, at the first Live");
                    self.unlink(&mut inner);
                    self.probe.event(|| LinkEvent::Answered(Command::Allow));
                }
                Err(e) => {
                    log::debug!(
                        "cannot answer the shim at {path}: {report}; the start is refused",
                        report = crate::error::Report(&e)
                    );
                    self.refuse_pending(&mut inner);
                    self.probe.event(|| LinkEvent::Dropped(DropReason::AnswerFailed));
                }
            }
            return;
        }
        if further {
            log::warn!("a further root peer at {path} said hello; answered N");
        } else {
            log::debug!("a root peer at {path} said hello after the start was refused; answered N");
        }
        self.deny(&conn);
    }

    /// Answers `N`; a failed send is only logged.
    pub(super) fn deny(&self, conn: &UnixStream) {
        if let Err(e) = sys::send_answer(conn.as_fd(), Command::Deny.encode()) {
            log::debug!(
                "cannot answer N at {}: {report}",
                self.sock_path.display(),
                report = crate::error::Report(&e)
            );
        }
        self.probe.event(|| LinkEvent::Answered(Command::Deny));
    }

    fn set_outcome(&self, inner: &mut Inner, outcome: LinkOutcome) {
        debug_assert!(inner.outcome.is_none(), "the outcome is set once");
        let path = self.sock_path.display();
        match outcome {
            LinkOutcome::Exited(_) => {}
            LinkOutcome::NotStarted(n) => log::debug!("the program at {path} never ran: {n:?}"),
            LinkOutcome::SupervisionLost(ws) => {
                log::warn!("supervision of the program at {path} was lost; the shim killed it (status {ws:#x})");
            }
            LinkOutcome::StatusLost => {
                log::warn!("the program at {path} has exited and its status is lost");
            }
            LinkOutcome::ShimLost => {
                log::warn!("the shim at {path} was lost; the program may still be running");
            }
        }
        inner.outcome = Some(outcome);
        // Written under the lock: a waiter that saw no outcome is guaranteed to see this byte when it
        // polls. The write end is nonblocking, and a full pipe is already readable.
        let written = match self.probe.settled_write_error() {
            Some(injected) => Err(injected),
            None => sys::write_byte(self.settled.1.as_fd()),
        };
        if let Err(e) = written {
            if e != Errno::AGAIN {
                debug_assert!(false, "writing the settled byte failed: {}", crate::error::Report(&e));
                log::warn!(
                    "cannot signal the outcome of the shim at {}: {report}",
                    self.sock_path.display(),
                    report = crate::error::Report(&e)
                );
            }
        }
    }

    /// Reads what the connection has into the frame buffer, without blocking, until the frame
    /// settles the outcome or nothing more is there. The caller has seen `Live`.
    fn read_frame(&self, inner: &mut Inner) {
        let conn = self.conn.get().expect("Live has a connection");
        while inner.outcome.is_none() {
            let len = inner.frame_len;
            let (settled, more) = match sys::read_some(conn.as_fd(), &mut inner.frame[len..]) {
                Read::Bytes(n) => {
                    inner.frame_len += n;
                    self.probe.event(|| LinkEvent::Read(n, std::thread::current().id()));
                    (classify(&inner.frame[..inner.frame_len], false), true)
                }
                Read::Eof => (classify(&inner.frame[..len], true), false),
                Read::Empty => (None, false),
                Read::Failed(e) => {
                    log::warn!(
                        "reading from the shim at {}: {report}",
                        self.sock_path.display(),
                        report = crate::error::Report(&e)
                    );
                    (Some(LinkOutcome::ShimLost), false)
                }
            };
            if let Some(outcome) = settled {
                self.set_outcome(inner, outcome);
            } else if !more {
                break;
            }
        }
    }

    /// The settled outcome, if any. Refuses a pending start; `None` means `Live` with no complete
    /// frame yet.
    pub(super) fn settle(&self) -> Option<LinkOutcome> {
        let mut inner = self.lock();
        if inner.outcome.is_none() {
            self.refuse_pending(&mut inner);
            if inner.start == StartState::Refused {
                let cause = inner
                    .failure
                    .map_or(NotStartedCause::Withheld, NotStartedCause::AcceptorFailed);
                let shim_connected = inner.hello_seen;
                self.set_outcome(
                    &mut inner,
                    LinkOutcome::NotStarted(NotStarted { shim_connected, cause }),
                );
            } else {
                self.read_frame(&mut inner);
            }
        }
        inner.outcome
    }

    /// Blocks until the outcome is settled. Waiters block on readiness outside the lock, then read
    /// under it. A failure to wait is returned and settles nothing: the shim may be fine.
    pub(super) fn wait(&self) -> Result<LinkOutcome, Errno> {
        loop {
            if let Some(outcome) = self.settle() {
                return Ok(outcome);
            }
            let conn = self.conn.get().expect("Live has a connection");
            self.probe.event(|| LinkEvent::Parked(std::thread::current().id()));
            self.probe.waiter_gate();
            // Poll `settled` too: another reader may take the frame and leave `conn` silent.
            let armed = |fds| {
                self.probe.event(|| LinkEvent::Polling {
                    fds,
                    settled_readable: crate::wait::any_ready_now(&mut [PollFd::new(&self.settled.0, PollFlags::IN)])
                        .expect("a look at the settled pipe"),
                });
            };
            let waited = match self.probe.wait_poll_error() {
                Some(injected) => Err(injected),
                None => sys::wait_for_frame_or_outcome(conn.as_fd(), &self.settled.0, armed),
            };
            if let Err(e) = waited {
                log::warn!(
                    "waiting for the shim at {}: {report}",
                    self.sock_path.display(),
                    report = crate::error::Report(&e)
                );
                return Err(e);
            }
        }
    }

    /// The state and outcome, after one nonblocking read when `Live`; moves nothing.
    pub(super) fn observe(&self) -> Observed {
        let mut inner = self.lock();
        if inner.start == StartState::Live && inner.outcome.is_none() {
            self.read_frame(&mut inner);
        }
        Observed {
            start: inner.start,
            outcome: inner.outcome,
            acceptor_failure: inner.failure,
        }
    }

    /// Writes `byte` to the shim's connection, whatever it is: the end-to-end tests send what
    /// `terminate` and `detach` will (`T`, `D`), a ping, and bytes no one should send.
    #[cfg(test)]
    pub(super) fn send_control(&self, byte: u8) -> Result<(), Errno> {
        let inner = self.lock();
        assert_eq!(inner.start, StartState::Live, "a control byte needs a live shim");
        sys::send_byte(self.conn.get().expect("Live has a connection").as_fd(), byte)
    }

    /// The descriptor of the shim's connection, once there is one.
    #[cfg(test)]
    pub(super) fn connection_fd(&self) -> Option<std::os::fd::RawFd> {
        use std::os::fd::AsRawFd;
        self.conn.get().map(|c| c.as_raw_fd())
    }

    pub(super) fn kill(&self) -> Result<KillOutcome, KillError> {
        let mut inner = self.lock();
        if inner.start != StartState::Live {
            self.refuse_pending(&mut inner);
            return Ok(KillOutcome::RefusedStart);
        }
        match inner.outcome {
            Some(LinkOutcome::ShimLost) => return Err(KillError::ShimLost),
            Some(_) => return Ok(KillOutcome::AlreadyEnded),
            None => {}
        }
        let conn = self.conn.get().expect("Live has a connection");
        let sent = match self.probe.send_error() {
            Some(injected) => Err(injected),
            None => sys::send_byte(conn.as_fd(), Command::Kill.encode()),
        };
        match sent {
            Ok(()) => Ok(KillOutcome::Delivered),
            Err(Errno::PIPE | Errno::CONNRESET | Errno::NOTCONN) => {
                // The shim is gone. Whether the program is depends on what it said first.
                self.read_frame(&mut inner);
                match inner.outcome {
                    Some(outcome) if outcome.program_is_gone() => Ok(KillOutcome::AlreadyEnded),
                    _ => Err(KillError::ShimLost),
                }
            }
            Err(Errno::AGAIN | Errno::NOBUFS) => Err(KillError::Unkillable),
            Err(e) => {
                log::warn!(
                    "sending K to the shim at {}: {report}",
                    self.sock_path.display(),
                    report = crate::error::Report(&e)
                );
                Err(KillError::Io(e.into()))
            }
        }
    }
}