agplatform 0.4.0

Platform providing env, fs, request and process infrastructure for production and testing
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! [internal]
//!
//! The Unix specific parts of [`ProcessImpl`](super::ProcessImpl).

use std::ffi::OsStr;
use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::os::fd::AsRawFd;
#[cfg(target_os = "linux")]
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::path::PathBuf;
use std::process::Child;
use std::process::Command;
use std::process::ExitStatus;
#[cfg(target_os = "linux")]
use std::sync::atomic::AtomicU32;
#[cfg(target_os = "linux")]
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::Instant;

use super::EXIT_GRACE;
use super::OutputStream;
use super::Outputs;
use super::millis_until;

#[cfg(target_os = "linux")]
type NFds = std::ffi::c_ulong;
#[cfg(not(target_os = "linux"))]
type NFds = std::ffi::c_uint;

#[cfg(target_os = "linux")]
const PIPE_BUF: usize = 4096;
#[cfg(not(target_os = "linux"))]
const PIPE_BUF: usize = 512;

/// The default capacity of a Linux pipe, read and written at once.
const BUFFER_SIZE: usize = 64 * 1024;
const ENOENT: i32 = 2;
#[cfg(target_os = "linux")]
const ESRCH: i32 = 3;
const F_GETFL: i32 = 3;
const F_SETFL: i32 = 4;
#[cfg(target_os = "linux")]
const O_NONBLOCK: i32 = 0o4000;
#[cfg(not(target_os = "linux"))]
const O_NONBLOCK: i32 = 0x4;
const POLLIN: i16 = 0x1;
const POLLOUT: i16 = 0x4;
#[cfg(target_os = "linux")]
const PR_SET_PDEATHSIG: i32 = 1;
const SIGKILL: i32 = 9;
#[cfg(target_os = "linux")]
const SYS_PIDFD_OPEN: std::ffi::c_long = 434;
const WAIT_INTERVAL_MS: u32 = 10;

/// The current process as the parent of the children configured with
/// [`configure`], read by them in [`die_with_parent`].
#[cfg(target_os = "linux")]
static PARENT: AtomicU32 = AtomicU32::new(0);

#[repr(C)]
struct PollFd {
    fd: i32,
    events: i16,
    revents: i16,
}

unsafe extern "C" {
    fn fcntl(fd: i32, command: i32, ...) -> i32;
    #[cfg(target_os = "linux")]
    fn getppid() -> i32;
    fn kill(pid: i32, signal: i32) -> i32;
    fn poll(fds: *mut PollFd, count: NFds, timeout: i32) -> i32;
    #[cfg(target_os = "linux")]
    fn prctl(option: i32, ...) -> i32;
    #[cfg(target_os = "linux")]
    fn syscall(number: std::ffi::c_long, ...) -> std::ffi::c_long;
}

/// Makes the child the leader of a new process group when it may need to
/// be killed with its descendants, see [`kill_group`]. On Linux such a
/// child is killed when the thread starting it exits, as it no longer gets
/// the signals sent to the process group of the current process.
pub(super) fn configure(command: &mut Command, group: bool) {
    if group {
        std::os::unix::process::CommandExt::process_group(command, 0);

        #[cfg(target_os = "linux")]
        {
            PARENT.store(std::process::id(), Ordering::Relaxed);
            // SAFETY: `die_with_parent` makes only async-signal-safe calls.
            unsafe { std::os::unix::process::CommandExt::pre_exec(command, die_with_parent) };
        }
    }
}

/// Makes the calling child killed when the thread that started it exits,
/// failing if the [`PARENT`] that did is gone already.
#[cfg(target_os = "linux")]
fn die_with_parent() -> std::io::Result<()> {
    // WHY: setting a valid signal cannot fail.
    // SAFETY: PR_SET_PDEATHSIG takes the signal as its only argument.
    unsafe { prctl(PR_SET_PDEATHSIG, SIGKILL as std::ffi::c_ulong) };

    // WHY: a parent dying before the signal is set orphans the child
    // without killing it, the child then has a new parent.
    // SAFETY: getppid has no preconditions and cannot fail.
    (unsafe { getppid() } as u32 == PARENT.load(Ordering::Relaxed))
        .then_some(())
        .ok_or(std::io::Error::from_raw_os_error(ESRCH))
}

/// Returns a descriptor of `child` that becomes readable when it exits,
/// `None` when unsupported.
#[cfg(target_os = "linux")]
fn exit_fd(child: &Child) -> Option<OwnedFd> {
    // SAFETY: pidfd_open takes the pid and no flags and returns a new
    // descriptor, which is close-on-exec, or -1.
    let fd = unsafe { syscall(SYS_PIDFD_OPEN, child.id() as std::ffi::c_long, 0) };

    // SAFETY: `fd` is a new descriptor owned by nothing else.
    (fd >= 0).then(|| unsafe { OwnedFd::from_raw_fd(fd as i32) })
}

/// Returns `None`, the exit of `child` is polled instead.
#[cfg(not(target_os = "linux"))]
fn exit_fd(_child: &Child) -> Option<OwnedFd> {
    None
}

/// Returns the first executable `program` in the directories of `path`,
/// relative and empty (POSIX) ones resolved against `dir`.
///
/// # Errors
///
/// Returns the error of std not finding a program if there is none.
pub(super) fn find_program(program: &Path, path: &OsStr, dir: &Path) -> std::io::Result<PathBuf> {
    std::env::split_paths(path)
        .map(|entry| dir.join(entry).join(program))
        .find(|candidate| {
            std::fs::metadata(candidate).is_ok_and(|metadata| {
                metadata.is_file() && metadata.permissions().mode() & 0o111 != 0
            })
        })
        .ok_or(std::io::Error::from_raw_os_error(ENOENT))
}

/// Writes `input` to the standard input of `child` and reads its standard
/// output and error into `outputs` on the current thread until they close,
/// then waits for it to exit. Once it exited they are read at most for
/// [`EXIT_GRACE`]. Past `deadline`, or once the sink of `outputs` fails, it
/// is killed with its descendants and `None` is returned as the status.
///
/// # Errors
///
/// Returns the first failure to use the pipes, once the child exited, as
/// its output would be incomplete. The pipes are closed on a failure, all
/// of them if they cannot be polled, rather than retried.
pub(super) fn wait_with_output(
    child: &mut Child,
    mut input: &[u8],
    deadline: Option<Instant>,
    outputs: &mut Outputs,
) -> std::io::Result<Option<ExitStatus>> {
    let mut stdin = child.stdin.take().map(OwnedFd::from).map(File::from);
    let mut stdout = child.stdout.take().map(OwnedFd::from).map(File::from);
    let mut stderr = child.stderr.take().map(OwnedFd::from).map(File::from);
    let mut exit = exit_fd(child);
    let mut grace_end = None;
    let mut buffer = vec![0; BUFFER_SIZE];
    let mut failure = None;
    // WHY: once poll reports a pipe writable, writing at most PIPE_BUF bytes
    // does not block, a non-blocking pipe takes as much as it can.
    let max_write = stdin
        .as_ref()
        .and_then(|stdin| set_nonblocking(stdin).ok())
        .map_or(PIPE_BUF, |()| BUFFER_SIZE);

    let kill = loop {
        if input.is_empty() {
            stdin = None;
        }

        if stdin.is_none() && stdout.is_none() && stderr.is_none() {
            break false;
        }

        let timeout = match grace_end.or(deadline).map(millis_until) {
            Some(0) => break grace_end.is_none(),
            Some(millis) => millis as i32,
            None => -1,
        };
        let mut fds = [
            poll_fd(&stdin, POLLOUT),
            poll_fd(&stdout, POLLIN),
            poll_fd(&stderr, POLLIN),
            poll_fd(&exit, POLLIN),
        ];

        // WHY: without an exit descriptor the exit is checked at least every
        // WAIT_INTERVAL_MS.
        let polled = poll_pipes(
            &mut fds,
            [&mut stdin, &mut stdout, &mut stderr],
            exit.as_ref().map_or(interval(timeout), |_| timeout),
        );
        failure = failure.or(polled.err());

        if grace_end.is_none()
            && (exit.is_none() || fds[3].revents != 0)
            && child.try_wait().ok().flatten().is_some()
        {
            grace_end = Some(Instant::now() + EXIT_GRACE);
            exit = None;
        }

        if fds[0].revents != 0 {
            failure = failure.or(write_chunk(&mut stdin, &mut input, max_write).err());
        }

        if fds[1].revents != 0 {
            failure =
                failure
                    .or(read_chunk(&mut stdout, &mut buffer, outputs, OutputStream::Stdout).err());
        }

        if fds[2].revents != 0 {
            failure =
                failure
                    .or(read_chunk(&mut stderr, &mut buffer, outputs, OutputStream::Stderr).err());
        }

        if outputs.failed() {
            break true;
        }
    };

    let status = if kill {
        kill_and_wait(child)
    } else {
        wait_timeout(child, deadline)
    };

    status.and_then(|status| failure.map_or(Ok(status), Err))
}

/// Returns the signal that terminated the process of `status`, if any.
pub(super) fn exit_signal(status: &ExitStatus) -> Option<i32> {
    std::os::unix::process::ExitStatusExt::signal(status)
}

/// Kills the process group led by the child `pid` configured with
/// [`configure`].
pub(super) fn kill_group(pid: u32) {
    // SAFETY: a negative pid signals the whole process group.
    let _ = unsafe { kill(-(pid as i32), SIGKILL) };
}

/// Returns the poll `timeout` in milliseconds limited to
/// [`WAIT_INTERVAL_MS`], which an infinite (negative) one is as well.
fn interval(timeout: i32) -> i32 {
    (timeout as u32).min(WAIT_INTERVAL_MS) as i32
}

/// Kills `child` with the process group it leads, if any.
fn kill_and_wait(child: &mut Child) -> std::io::Result<Option<ExitStatus>> {
    kill_group(child.id());
    let _ = child.kill();
    child.wait().map(|_| None)
}

fn poll_fd<T: AsRawFd>(pipe: &Option<T>, events: i16) -> PollFd {
    PollFd {
        fd: pipe.as_ref().map_or(-1, AsRawFd::as_raw_fd),
        events,
        revents: 0,
    }
}

/// Polls the descriptors `fds` of the `pipes` (and the exit of a child)
/// for at most `timeout` milliseconds, negative for no limit, closing the
/// `pipes` if they cannot be polled. An interrupted poll is left to be
/// retried.
fn poll_pipes(
    fds: &mut [PollFd],
    pipes: [&mut Option<File>; 3],
    timeout: i32,
) -> std::io::Result<()> {
    // SAFETY: `fds` is a slice of pollfd alive for the duration of the call.
    // Closed pipes have a negative fd, which poll ignores.
    let result = unsafe { poll(fds.as_mut_ptr(), fds.len() as NFds, timeout) };
    let error = std::io::Error::last_os_error();

    (result >= 0 || is_transient(&error))
        .then_some(())
        .ok_or(error)
        .inspect_err(|_| pipes.into_iter().for_each(|pipe| *pipe = None))
}

/// Returns `true` for an `error` of an operation that can be retried.
fn is_transient(error: &std::io::Error) -> bool {
    matches!(
        error.kind(),
        std::io::ErrorKind::Interrupted | std::io::ErrorKind::WouldBlock
    )
}

/// Reads what is available from `pipe` into `outputs` as its `stream`,
/// closing it at its end or when it fails.
fn read_chunk(
    pipe: &mut Option<File>,
    buffer: &mut [u8],
    outputs: &mut Outputs,
    stream: OutputStream,
) -> std::io::Result<()> {
    match pipe.as_mut().map(|pipe| pipe.read(buffer)) {
        Some(Ok(read @ 1..)) => {
            outputs.push(stream, &buffer[..read]);
            Ok(())
        }
        Some(Err(error)) if is_transient(&error) => Ok(()),
        Some(Err(error)) => {
            *pipe = None;
            Err(error)
        }
        _ => {
            *pipe = None;
            Ok(())
        }
    }
}

/// Makes writing to `file` take what it can without blocking.
fn set_nonblocking(file: &File) -> std::io::Result<()> {
    let fd = file.as_raw_fd();
    // SAFETY: F_GETFL and F_SETFL get and set the status flags of the open
    // `fd`, returning -1 on failure.
    let set = unsafe {
        let flags = fcntl(fd, F_GETFL);
        flags >= 0 && fcntl(fd, F_SETFL, flags | O_NONBLOCK) >= 0
    };

    set.then_some(()).ok_or_else(std::io::Error::last_os_error)
}

/// Waits for `child` to exit at most until `deadline`, otherwise kills it
/// with its descendants and returns `None`.
fn wait_timeout(
    child: &mut Child,
    deadline: Option<Instant>,
) -> std::io::Result<Option<ExitStatus>> {
    let Some(deadline) = deadline else {
        return child.wait().map(Some);
    };

    loop {
        match child.try_wait() {
            Ok(None) => match millis_until(deadline) {
                0 => return kill_and_wait(child),
                millis => {
                    std::thread::sleep(Duration::from_millis(millis.min(WAIT_INTERVAL_MS).into()))
                }
            },
            result => return result,
        }
    }
}

/// Writes the next chunk of `input` to `stdin`, at most `max` bytes, and
/// leaves the rest in `input`, none once `stdin` is closed by the child or
/// fails.
fn write_chunk(stdin: &mut Option<File>, input: &mut &[u8], max: usize) -> std::io::Result<()> {
    match stdin
        .as_mut()
        .map(|stdin| stdin.write(&input[..input.len().min(max)]))
    {
        Some(Ok(written)) => {
            *input = &input[written..];
            Ok(())
        }
        Some(Err(error)) if is_transient(&error) => Ok(()),
        Some(Err(error)) if error.kind() != std::io::ErrorKind::BrokenPipe => {
            *input = &[];
            Err(error)
        }
        _ => {
            *input = &[];
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::TestDir;

    #[cfg(target_os = "linux")]
    mod die_with_parent {
        use super::*;

        #[test]
        fn die_with_parent_orphaned_should_fail() {
            // GIVEN the current process as the parent, which is not its own
            PARENT.store(std::process::id(), Ordering::Relaxed);

            // WHEN setting the parent death signal of the current process
            let result = die_with_parent();
            // WHY: the signal set by `die_with_parent` is reset for the test thread.
            unsafe { prctl(PR_SET_PDEATHSIG, 0 as std::ffi::c_ulong) };

            // THEN the parent is found to be gone
            assert_eq!(result.unwrap_err().raw_os_error(), Some(ESRCH));
        }
    }

    mod find_program {
        use super::*;

        fn write(path: &Path, mode: u32) {
            std::fs::write(path, "").unwrap();
            std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).unwrap();
        }

        #[test]
        fn find_program_should_return_first_executable_match() {
            // GIVEN a not executable file, a directory and two executables
            // of the name in four directories
            let dir = TestDir::new();
            for name in ["file", "dir", "first", "second"] {
                std::fs::create_dir(dir.path().join(name)).unwrap();
            }
            write(&dir.path().join("file/tool"), 0o644);
            std::fs::create_dir(dir.path().join("dir/tool")).unwrap();
            write(&dir.path().join("first/tool"), 0o755);
            write(&dir.path().join("second/tool"), 0o755);

            // WHEN finding it in all of them, some relative to the directory
            let found = find_program(
                Path::new("tool"),
                OsStr::new(&format!("file:{}/dir:first:second", dir.path().display())),
                dir.path(),
            );

            // THEN the first executable is found
            assert_eq!(found.unwrap(), dir.path().join("first/tool"));
        }

        #[test]
        fn find_program_empty_entry_should_search_dir() {
            let dir = TestDir::new();
            write(&dir.path().join("tool"), 0o700);

            let found = find_program(
                Path::new("tool"),
                OsStr::new("/agplatform_missing:"),
                dir.path(),
            );

            assert_eq!(found.unwrap(), dir.path().join("tool"));
        }

        #[test]
        fn find_program_missing_should_return_not_found() {
            let dir = TestDir::new();

            let error = find_program(Path::new("tool"), OsStr::new("bin"), dir.path()).unwrap_err();

            assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
            assert_eq!(error.raw_os_error(), Some(ENOENT));
        }
    }

    mod interval {
        use super::*;

        #[test]
        fn interval_should_limit_timeout_to_wait_interval() {
            for (timeout, expected) in [(-1, 10), (0, 0), (5, 5), (10_000, 10)] {
                assert_eq!(interval(timeout), expected, "timeout: {timeout}");
            }
        }
    }

    /// Calls `f` with the outputs of a run capturing them.
    fn with_outputs<T>(f: impl FnOnce(&mut Outputs) -> T) -> T {
        let command = Command::new("sh");
        let run = super::super::CommandRun::new(&command);
        f(&mut Outputs::new(&super::super::StdObserver, &run, None))
    }

    /// Returns the ends of a new pipe, the writing one non-blocking.
    fn nonblocking_pipe() -> (File, File) {
        let (reader, writer) = std::io::pipe().unwrap();
        let writer = File::from(OwnedFd::from(writer));
        set_nonblocking(&writer).unwrap();
        (File::from(OwnedFd::from(reader)), writer)
    }

    mod poll_pipes {
        use super::*;

        #[test]
        fn poll_pipes_failing_should_close_pipes() {
            // GIVEN more descriptors than a process may have open
            let mut fds: Vec<PollFd> = (0..1 << 21)
                .map(|_| PollFd {
                    fd: -1,
                    events: 0,
                    revents: 0,
                })
                .collect();
            let mut pipes = [
                Some(File::open("Cargo.toml").unwrap()),
                Some(File::open("Cargo.toml").unwrap()),
                None,
            ];
            let [stdin, stdout, stderr] = &mut pipes;

            // WHEN polling them
            let result = poll_pipes(&mut fds, [stdin, stdout, stderr], 0);

            // THEN it fails and the pipes are closed
            assert_eq!(result.unwrap_err().kind(), std::io::ErrorKind::InvalidInput);
            assert!(pipes.iter().all(Option::is_none));
        }
    }

    mod read_chunk {
        use super::*;

        #[test]
        fn read_chunk_with_nothing_to_read_yet_should_keep_pipe() {
            let (reader, _writer) = nonblocking_pipe();
            set_nonblocking(&reader).unwrap();
            let mut pipe = Some(reader);

            let result = with_outputs(|outputs| {
                read_chunk(&mut pipe, &mut [0; 8], outputs, OutputStream::Stdout)
            });

            assert!(result.is_ok());
            assert!(pipe.is_some());
        }

        #[test]
        fn read_chunk_failing_should_close_pipe_and_fail() {
            let mut pipe = Some(File::open(".").unwrap());

            let result = with_outputs(|outputs| {
                read_chunk(&mut pipe, &mut [0; 8], outputs, OutputStream::Stdout)
            });

            assert_eq!(result.unwrap_err().kind(), std::io::ErrorKind::IsADirectory);
            assert!(pipe.is_none());
        }
    }

    mod write_chunk {
        use super::*;

        #[test]
        fn write_chunk_to_full_pipe_should_keep_input() {
            // GIVEN a full pipe
            let (_reader, mut writer) = nonblocking_pipe();
            while writer.write(&[0; PIPE_BUF]).is_ok() {}
            let mut stdin = Some(writer);
            let mut input: &[u8] = b"input";

            // WHEN writing more to it
            let result = write_chunk(&mut stdin, &mut input, BUFFER_SIZE);

            // THEN the input is left to be written
            assert!(result.is_ok());
            assert_eq!(input, b"input");
        }

        #[test]
        fn write_chunk_to_closed_pipe_should_drop_input() {
            let (reader, writer) = nonblocking_pipe();
            drop(reader);
            let mut input: &[u8] = b"input";

            let result = write_chunk(&mut Some(writer), &mut input, BUFFER_SIZE);

            assert!(result.is_ok());
            assert!(input.is_empty());
        }

        #[test]
        fn write_chunk_failing_should_drop_input_and_fail() {
            let mut input: &[u8] = b"input";

            let result = write_chunk(
                &mut Some(File::open("Cargo.toml").unwrap()),
                &mut input,
                BUFFER_SIZE,
            );

            assert!(result.is_err());
            assert!(input.is_empty());
        }
    }

    mod wait_timeout {
        use super::*;

        #[test]
        fn wait_timeout_exceeding_deadline_should_kill_child() {
            // GIVEN a running child leading its process group
            let mut command = Command::new("sleep");
            command.arg("10");
            configure(&mut command, true);
            let mut child = command.spawn().unwrap();
            let start = Instant::now();

            // WHEN its deadline passes
            let status = wait_timeout(&mut child, Some(start + Duration::from_millis(100)));

            // THEN it is killed right away
            assert_eq!(status.unwrap(), None);
            assert!(start.elapsed() < Duration::from_secs(5));
        }
    }
}