Elffuss BraidHub

utopia-ia/elffuss-braid public

verify: without /proc, start the watchdog from braid's name in PATH

On Linux without /proc, neither /proc/self/exe nor the path braid was
started from can be found, and verify could not start its watchdog. It
now falls back on the name braid was started by: a path when it holds a
slash, otherwise the first executable of that name in PATH. The handshake
turns away anything that is not this braid's watchdog.
Changeset003ac7192c00fab5945b32cf311c3eea00d53caefdec5ec973a04a9177e566d2
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 16:40 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 16:40 +02:00
Parentsb092a334ad0e

Browse the files at this changeset

1 file changed

modified crates/cli/src/verify.rs +55 −2
@@ -683,7 +683,9 @@
683683
684684 /// The braid command to start the watchdog with: on Linux this very binary,
685685 /// through `/proc/self/exe`, even if its file was replaced or removed since
686-/// it started; elsewhere the file it was started from.
686+/// it started; elsewhere the file it was started from; and where neither
687+/// can be found, as on Linux without `/proc`, the name it was started by,
688+/// looked up in `PATH`. The handshake turns away any other program.
687689 #[cfg(unix)]
688690 fn own_exe() -> Result<PathBuf> {
689691 #[cfg(target_os = "linux")]
@@ -693,11 +695,36 @@
693695 return Ok(this);
694696 }
695697 }
696- std::env::current_exe().context(
698+ if let Ok(path) = std::env::current_exe() {
699+ return Ok(path);
700+ }
701+ let name = std::env::args_os().next().unwrap_or_default();
702+ started_as(&name, std::env::var_os("PATH").as_deref()).context(
697703 "cannot find the braid command to start verify's watchdog, so the tests did not run",
698704 )
699705 }
700706
707+/// The file that a command started as `name` runs: `name` itself when it
708+/// holds a `/`, or else the first executable file of that name in the
709+/// directories of `path`, as a shell finds it.
710+#[cfg(unix)]
711+fn started_as(name: &std::ffi::OsStr, path: Option<&std::ffi::OsStr>) -> Option<PathBuf> {
712+ use std::os::unix::ffi::OsStrExt;
713+ use std::os::unix::fs::PermissionsExt;
714+ if name.is_empty() {
715+ return None;
716+ }
717+ if name.as_bytes().contains(&b'/') {
718+ return Some(PathBuf::from(name));
719+ }
720+ std::env::split_paths(path?)
721+ .map(|dir| dir.join(name))
722+ .find(|candidate| {
723+ fs::metadata(candidate)
724+ .is_ok_and(|meta| meta.is_file() && meta.permissions().mode() & 0o111 != 0)
725+ })
726+}
727+
701728 /// The watchdog of one test run in `dir`, as [`Watchdog`] starts it.
702729 pub(crate) fn watchdog(dir: &Path) -> Result<ExitCode> {
703730 {
@@ -1411,6 +1438,32 @@
14111438 assert_eq!(found, expected);
14121439 }
14131440
1441+ #[cfg(unix)]
1442+ #[test]
1443+ fn without_its_own_file_braid_is_found_as_a_shell_finds_it() {
1444+ use std::os::unix::fs::PermissionsExt;
1445+ let temp = tempfile::tempdir().unwrap();
1446+ let bin = temp.path().join("bin");
1447+ fs::create_dir(&bin).unwrap();
1448+ let program = bin.join("braid-like");
1449+ fs::write(&program, "#!/bin/sh\n").unwrap();
1450+ fs::set_permissions(&program, fs::Permissions::from_mode(0o755)).unwrap();
1451+ let plain = bin.join("not-a-program");
1452+ fs::write(&plain, "").unwrap();
1453+ let path = std::env::join_paths([temp.path().join("missing"), bin.clone()]).unwrap();
1454+ let found = |name: &str| started_as(name.as_ref(), Some(path.as_os_str()));
1455+ assert_eq!(found("braid-like"), Some(program.clone()));
1456+ assert_eq!(found("not-a-program"), None);
1457+ assert_eq!(found("absent"), None);
1458+ assert_eq!(found(""), None);
1459+ // A name with a slash is a path already.
1460+ assert_eq!(
1461+ found("./target/braid"),
1462+ Some(PathBuf::from("./target/braid"))
1463+ );
1464+ assert_eq!(started_as("braid-like".as_ref(), None), None);
1465+ }
1466+
14141467 /// A stand-in for the watchdog: `script`, run by `sh`.
14151468 #[cfg(unix)]
14161469 fn fake_watchdog(script: &str) -> Command {