Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created January 18, 2024 07:37
Show Gist options
  • Save chadaustin/ee1a20e0522c10b65cb4006496d1fb7c to your computer and use it in GitHub Desktop.
Save chadaustin/ee1a20e0522c10b65cb4006496d1fb7c to your computer and use it in GitHub Desktop.
Script to detect whether mosh-server is a parent process
#!/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