capsule_runtime/host_warnings.rs
1//! Host-level warnings: stated once per launch, not once per process.
2//!
3//! A host-level warning is one whose condition and text depend on the host alone — the same for
4//! every process on the machine, whatever manifest it runs, such as `W-SEC-013`
5//! ([`crate::runtime::warn_on_userns_restriction_disabled_host_wide`]). A warning gated on, or
6//! naming, something in a session's own manifest is a fact about that session and is stated per
7//! session, outside this gate.
8//!
9//! The process the operator started prints each host-level warning once: a plain `mur run` while
10//! staging its session, and `mur run --roster` from the launcher before any member starts. Every
11//! `mur run` subprocess the runtime starts — each formation member
12//! ([`crate::formation_launch`]) and each delegated or plan-step sub-capsule
13//! ([`crate::child_launch`]) — is started with [`HOST_WARNINGS_REPORTED_ENV`] set to `1`, telling
14//! it that its launcher already did. `mur doctor` reports the host and is never gated.
15//!
16//! The gate silences stderr only. What a session records about the host — `session_start`'s
17//! `userns_grant` — is written whatever this variable holds.
18
19/// Set to `1` by the runtime in the environment of every `mur run` subprocess it starts. Any other
20/// value, or none, means this process prints the host-level warnings itself.
21pub const HOST_WARNINGS_REPORTED_ENV: &str = "MURMUR_HOST_WARNINGS_REPORTED";
22
23/// The value [`HOST_WARNINGS_REPORTED_ENV`] is set to, and the only one honoured.
24pub(crate) const HOST_WARNINGS_REPORTED: &str = "1";
25
26/// Whether this process's launcher already printed the host-level warnings, so this process must
27/// not: true only when [`HOST_WARNINGS_REPORTED_ENV`] is exactly `1`.
28pub fn host_warnings_reported() -> bool {
29 host_warnings_reported_in(std::env::var(HOST_WARNINGS_REPORTED_ENV).ok().as_deref())
30}
31
32/// [`host_warnings_reported`] over a given value of [`HOST_WARNINGS_REPORTED_ENV`]: true for
33/// exactly `Some("1")`. Unset, empty, `0`, `true` and every other value leave the warnings to this
34/// process.
35pub fn host_warnings_reported_in(value: Option<&str>) -> bool {
36 value == Some(HOST_WARNINGS_REPORTED)
37}
38
39/// The `(name, value)` pair the runtime sets in a `mur run` subprocess's environment.
40pub(crate) fn reported_env_pair() -> (String, String) {
41 (
42 HOST_WARNINGS_REPORTED_ENV.to_string(),
43 HOST_WARNINGS_REPORTED.to_string(),
44 )
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn host_warnings_are_reported_only_for_exactly_one() {
53 assert!(!host_warnings_reported_in(None));
54 assert!(!host_warnings_reported_in(Some("")));
55 assert!(!host_warnings_reported_in(Some("0")));
56 assert!(!host_warnings_reported_in(Some("true")));
57 assert!(!host_warnings_reported_in(Some(" 1")));
58 assert!(host_warnings_reported_in(Some("1")));
59 }
60
61 #[test]
62 fn host_warnings_env_pair_is_the_honoured_value() {
63 let (name, value) = reported_env_pair();
64 assert_eq!(name, "MURMUR_HOST_WARNINGS_REPORTED");
65 assert!(host_warnings_reported_in(Some(&value)));
66 }
67}