Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created December 21, 2023 21:08
Show Gist options
  • Save chadaustin/73e59498f25eba7d4501920e7db29d34 to your computer and use it in GitHub Desktop.
Save chadaustin/73e59498f25eba7d4501920e7db29d34 to your computer and use it in GitHub Desktop.
#!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! anyhow = "1"
//! libc = "0.2"
//! procfs = "0.16"
//! ```
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