| 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 = "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 = "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 = "freebsd"))] |
| 40 | //! println!("{:?}", val); |
| 41 | //! ``` |
| 42 | |
| 43 | #[macro_use] |
| 44 | extern crate bitflags; |
| 45 | extern crate byteorder; |
| 46 | extern crate enum_as_inner; |
| 47 | extern crate libc; |
| 48 | extern crate thiserror; |
| 49 | |
| 50 | #[cfg(any(target_os = "android", target_os = "linux"))] |
| 51 | extern crate walkdir; |
| 52 | |
| 53 | #[cfg(any(target_os = "android", target_os = "linux"))] |
| 54 | #[path = "linux/mod.rs"] |
| 55 | mod sys; |
| 56 | |
| 57 | #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] |
| 58 | #[path = "unix/mod.rs"] |
| 59 | mod sys; |
| 60 | |
| 61 | mod consts; |
| 62 | mod ctl_error; |
| 63 | mod ctl_flags; |
| 64 | mod ctl_info; |
| 65 | mod ctl_type; |
| 66 | mod ctl_value; |
| 67 | #[cfg(target_os = "freebsd")] |
| 68 | mod temperature; |
| 69 | mod traits; |
| 70 | |
| 71 | pub use consts::*; |
| 72 | pub use ctl_error::*; |
| 73 | pub use ctl_flags::*; |
| 74 | pub use ctl_info::*; |
| 75 | pub use ctl_type::*; |
| 76 | pub use ctl_value::*; |
| 77 | pub use sys::ctl::*; |
| 78 | pub use sys::ctl_iter::*; |
| 79 | #[cfg(target_os = "freebsd")] |
| 80 | pub use temperature::Temperature; |
| 81 | pub use traits::Sysctl; |