tawasuyu

tawasuyu/brahman

Rust 326TOML 123JSON 17Markdown 144606 Sutichaykuna

⎇ d3649bfd1a80153652c2a028315369bb4b184024 crates/shared/ente-incarnate/src/child.rs Rust · 47 siq'ikuna · 1.4 KB Ñawpa willay Pi imata llamkarqan ch'uya

shell c22d2480b · sergio ·

1//! Helpers que corren EN el hijo post-clone, antes de execve.
2//!
3//! Reglas inviolables (la clausura de clone(2) corre en stack nuevo, COW):
4//!   - sólo syscalls async-signal-safe
5//!   - no `println!`/`tracing!`/cualquier I/O del runtime
6//!   - no allocator (vec/box/string)
7//!   - no Drop con efectos
8//!   - capturar sólo Copy o datos pre-construidos
9
10use brahman_card::ResourceLimits;
11
12/// SAFETY: invocada en el hijo post-clone, sólo libc.
13pub unsafe fn apply_rlimits(rl: &ResourceLimits) {
14    if let Some(mem) = rl.mem_bytes {
15        let lim = libc::rlimit {
16            rlim_cur: mem,
17            rlim_max: mem,
18        };
19        libc::setrlimit(libc::RLIMIT_AS, &lim);
20    }
21    if let Some(np) = rl.nproc {
22        let lim = libc::rlimit {
23            rlim_cur: np as u64,
24            rlim_max: np as u64,
25        };
26        libc::setrlimit(libc::RLIMIT_NPROC, &lim);
27    }
28    if let Some(nf) = rl.nofile {
29        let lim = libc::rlimit {
30            rlim_cur: nf as u64,
31            rlim_max: nf as u64,
32        };
33        libc::setrlimit(libc::RLIMIT_NOFILE, &lim);
34    }
35}
36
37/// SAFETY: idem. `MS_PRIVATE | MS_REC` sobre `/` para que mounts del hijo
38/// no se filtren al host. Trampa típica al delegar mount ns.
39pub unsafe fn make_root_private() {
40    libc::mount(
41        std::ptr::null(),
42        b"/\0".as_ptr() as *const _,
43        std::ptr::null(),
44        libc::MS_PRIVATE | libc::MS_REC,
45        std::ptr::null(),
46    );
47}