Created
January 18, 2024 07:37
-
-
Save chadaustin/ee1a20e0522c10b65cb4006496d1fb7c to your computer and use it in GitHub Desktop.
Script to detect whether mosh-server is a parent process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rust-script | |
//! ```cargo | |
//! [dependencies] | |
//! anyhow = "1" | |
//! libc = "0.2" | |
//! procfs = { version = "0.16", default-features = false } | |
//! ``` | |
use procfs::process::Process; | |
use std::ffi::OsStr; | |
const INIT_PID: libc::pid_t = 1; | |
#[cfg(target_os = "linux")] | |
fn main() -> anyhow::Result<std::process::ExitCode> { | |
let mosh_server_name: &OsStr = OsStr::new("mosh-server"); | |
let my_ppid = unsafe { libc::getppid() }; | |
let mut p = Process::new(my_ppid)?; | |
while p.pid != INIT_PID { | |
if p.exe()?.file_name() == Some(mosh_server_name) { | |
return Ok(0.into()); | |
} | |
p = Process::new(p.status()?.ppid)?; | |
} | |
return Ok(1.into()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment