sysctl/lib.rs
1// lib.rs
2
3//! A simplified interface to the `sysctl` system call.
4//!
5//! # Example: Get value
6//! ```
7//! # use sysctl::Sysctl;
8//! #[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "visionos", target_os = "freebsd"))]
9//! const CTLNAME: &str = "kern.ostype";
10//! #[cfg(any(target_os = "linux", target_os = "android"))]
11//! const CTLNAME: &str = "kernel.ostype";
12//!
13//! let ctl = sysctl::Ctl::new(CTLNAME).unwrap();
14//! let desc = ctl.description().unwrap();
15//! println!("Description: {}", desc);
16//! let val = ctl.value().unwrap();
17//! println!("Value: {}", val);
18//! // On Linux all sysctls are String type. Use the following for
19//! // cross-platform compatibility:
20//! let str_val = ctl.value_string().unwrap();
21//! println!("String value: {}", str_val);
22//! ```
23//!
24//! # Example: Get value as struct
25//! ```
26//! // Not available on Linux
27//! # use sysctl::Sysctl;
28//! #[derive(Debug, Default)]
29//! #[repr(C)]
30//! struct ClockInfo {
31//! hz: libc::c_int, /* clock frequency */
32//! tick: libc::c_int, /* micro-seconds per hz tick */
33//! spare: libc::c_int,
34//! stathz: libc::c_int, /* statistics clock frequency */
35//! profhz: libc::c_int, /* profiling clock frequency */
36//! }
37//! # #[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "visionos", target_os = "freebsd"))]
38//! let val: Box<ClockInfo> = sysctl::Ctl::new("kern.clockrate").unwrap().value_as().unwrap();
39//! # #[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "visionos", target_os = "freebsd"))]
40//! println!("{:?}", val);
41//! ```
42
43#[macro_use]
44extern crate bitflags;
45extern crate byteorder;
46extern crate enum_as_inner;
47extern crate libc;
48extern crate thiserror;
49
50#[cfg(any(target_os = "android", target_os = "linux"))]
51extern crate walkdir;
52
53#[cfg(any(target_os = "android", target_os = "linux"))]
54#[path = "linux/mod.rs"]
55mod sys;
56
57#[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "visionos", target_os = "freebsd"))]
58#[path = "unix/mod.rs"]
59mod sys;
60
61mod consts;
62mod ctl_error;
63mod ctl_flags;
64mod ctl_info;
65mod ctl_type;
66mod ctl_value;
67#[cfg(target_os = "freebsd")]
68mod temperature;
69mod traits;
70
71pub use consts::*;
72pub use ctl_error::*;
73pub use ctl_flags::*;
74pub use ctl_info::*;
75pub use ctl_type::*;
76pub use ctl_value::*;
77pub use sys::ctl::*;
78pub use sys::ctl_iter::*;
79#[cfg(target_os = "freebsd")]
80pub use temperature::Temperature;
81pub use traits::Sysctl;