use std::{
io::{stdout, Write},
os::unix::ffi::OsStrExt,
process::ExitCode,
};
use nix::{libc::pid_t, unistd::Pid};
use syd::{fd::open_static_proc, proc::proc_tty};
#[cfg(all(
not(coverage),
not(feature = "prof"),
not(target_os = "android"),
not(target_arch = "riscv64"),
target_page_size_4k,
target_pointer_width = "64"
))]
#[global_allocator]
static GLOBAL: hardened_malloc::HardenedMalloc = hardened_malloc::HardenedMalloc;
#[cfg(feature = "prof")]
#[global_allocator]
static GLOBAL: tcmalloc::TCMalloc = tcmalloc::TCMalloc;
syd::main! {
syd::set_sigpipe_dfl()?;
open_static_proc()?;
let pid = match std::env::args().nth(1).map(|arg| arg.parse::<pid_t>()) {
Some(Ok(pid)) => Pid::from_raw(pid),
None => Pid::this(),
Some(Err(_)) => {
help();
return Ok(ExitCode::SUCCESS);
}
};
match proc_tty(pid) {
Ok(path) => {
let path = path.as_os_str().as_bytes();
stdout().write_all(path)?;
}
Err(errno) => {
eprintln!("syd-tty: {errno}");
return Ok(ExitCode::from(errno as u8));
}
}
Ok(ExitCode::SUCCESS)
}
fn help() {
println!("Usage: syd-tty [PID]");
println!("Print the controlling terminal of the given process.");
}