Skip to main content

rns_server/
self_exec.rs

1use std::path::PathBuf;
2
3pub fn resolve_self_exec() -> Result<PathBuf, String> {
4    let proc_self = PathBuf::from("/proc/self/exe");
5    if proc_self.exists() {
6        return Ok(proc_self);
7    }
8
9    std::env::current_exe().map_err(|err| format!("failed to resolve current executable: {}", err))
10}
11
12pub fn self_exec_display() -> &'static str {
13    "/proc/self/exe"
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19
20    #[test]
21    fn self_exec_prefers_proc_self_exe_or_falls_back_to_existing_path() {
22        let resolved = resolve_self_exec().unwrap();
23        assert!(
24            resolved == PathBuf::from("/proc/self/exe") || resolved.exists(),
25            "unexpected self exec path: {}",
26            resolved.display()
27        );
28    }
29}