OXYGENHUB
# 8aa45fa6ece5 oxygenflow / crates / flow / src / tier.rs
1.8 KiB · Rust · last changed Windows runner: helpers start from the running binary; microVM says Linux only 10 days ago source blame history raw
3554afef Andy McCall ↶ 1
//! What the sandbox tiers share, on every system a runner runs on: where a
2
//! job's disks are kept, how a helper says a job never started, reading a
3
//! job's output by lines, and this binary's hash for receipts.
4
5
use std::io::{BufRead, BufReader, Read};
6
use std::path::{Path, PathBuf};
7
use std::sync::{mpsc, OnceLock};
8
9
/// Where the `n`th `ephemeral://disk` mount of a job keeps its files.
10
pub fn disk_dir(scratch: &Path, n: usize) -> PathBuf {
11
    scratch.join(format!("mount-{n}"))
12
}
13
14
/// How the helper says the job never started, on its stderr.
15
pub(crate) const FAILED: &str = "\u{1e}F ";
16
17
pub(crate) fn lines(r: impl Read, stdout: bool, tx: mpsc::Sender<(bool, Vec<u8>)>) {
18
    let mut r = BufReader::new(r);
19
    let mut buf = Vec::new();
20
    loop {
21
        buf.clear();
22
        // A long line is cut at 16 KiB, as flow-init cuts it.
23
        match (&mut r).take(16 * 1024).read_until(b'\n', &mut buf) {
24
            Ok(0) | Err(_) => break,
25
            Ok(_) => {
26
                if buf.last() == Some(&b'\n') {
27
                    buf.pop();
28
                }
29
                if tx.send((stdout, buf.clone())).is_err() {
30
                    break;
31
                }
32
            }
33
        }
34
    }
35
}
36
e6949d87 Andy McCall ↶ 37
/// This binary, to start helpers from and hash: `/proc/self/exe` on Linux
38
/// and Android (the running file, even after an update replaced its name),
39
/// the path it was started from elsewhere (oxygenflow#83).
40
pub fn self_exe() -> PathBuf {
41
    if cfg!(target_os = "linux") || cfg!(target_os = "android") {
42
        PathBuf::from("/proc/self/exe")
43
    } else {
44
        std::env::current_exe().unwrap_or_default()
45
    }
46
}
47
3554afef Andy McCall ↶ 48
/// This binary's hash, which receipts name as the sandbox.
49
pub(crate) fn self_hash() -> &'static str {
50
    static HASH: OnceLock<String> = OnceLock::new();
e6949d87 Andy McCall ↶ 51
    HASH.get_or_init(|| crate::home::hash_file(&self_exe()).unwrap_or_else(|_| "unknown".into()))
3554afef Andy McCall ↶ 52
}