Crate sysctl

source ·
Expand description

A simplified interface to the sysctl system call.

§Example: Get value

#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "visionos", target_os = "freebsd"))]
const CTLNAME: &str = "kern.ostype";
#[cfg(any(target_os = "linux", target_os = "android"))]
const CTLNAME: &str = "kernel.ostype";

let ctl = sysctl::Ctl::new(CTLNAME).unwrap();
let desc = ctl.description().unwrap();
println!("Description: {}", desc);
let val = ctl.value().unwrap();
println!("Value: {}", val);
// On Linux all sysctls are String type. Use the following for
// cross-platform compatibility:
let str_val = ctl.value_string().unwrap();
println!("String value: {}", str_val);

§Example: Get value as struct

// Not available on Linux
#[derive(Debug, Default)]
#[repr(C)]
struct ClockInfo {
    hz: libc::c_int, /* clock frequency */
    tick: libc::c_int, /* micro-seconds per hz tick */
    spare: libc::c_int,
    stathz: libc::c_int, /* statistics clock frequency */
    profhz: libc::c_int, /* profiling clock frequency */
}
let val: Box<ClockInfo> = sysctl::Ctl::new("kern.clockrate").unwrap().value_as().unwrap();
println!("{:?}", val);

Structs§

Enums§

  • This struct represents a system control.
  • An Enum that represents a sysctl’s type information.
  • An Enum that holds all values returned by sysctl calls. Extract inner value with accessors like as_int().

Constants§

Traits§