use std::{mem::MaybeUninit, process::ExitCode};
use nix::errno::Errno;
use serde_json::json;
#[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! {
use lexopt::prelude::*;
syd::set_sigpipe_dfl()?;
let mut parser = lexopt::Parser::from_env();
#[expect(clippy::never_loop)]
while let Some(arg) = parser.next()? {
match arg {
Short('h') => {
help();
return Ok(ExitCode::SUCCESS);
}
_ => return Err(arg.unexpected().into()),
}
}
let mut info = MaybeUninit::<libc::sysinfo>::uninit();
Errno::result(unsafe { libc::sysinfo(info.as_mut_ptr()) })?;
let info = unsafe { info.assume_init() };
#[expect(clippy::disallowed_methods)]
let info = json!({
"uptime": info.uptime,
"loads": info.loads,
"totalram": info.totalram,
"freeram": info.freeram,
"sharedram": info.sharedram,
"bufferram": info.bufferram,
"totalswap": info.totalswap,
"freeswap": info.freeswap,
"procs": info.procs,
"totalhigh": info.totalhigh,
"freehigh": info.freehigh,
"mem_unit": info.mem_unit,
});
#[expect(clippy::disallowed_methods)]
let info = serde_json::to_string_pretty(&info).unwrap();
println!("{info}");
Ok(ExitCode::SUCCESS)
}
fn help() {
println!("Usage: syd-info [-h]");
println!("Print system information.");
}