| 1 | This crate provides a safe interface for reading and writing information to the kernel using the sysctl interface. |
| 2 | |
| 3 | [](https://cirrus-ci.com/github/johalun/sysctl-rs/master) |
| 4 | |
| 5 | [](https://crates.io/crates/sysctl) |
| 6 | |
| 7 | |
| 8 | *FreeBSD, Linux, macOS and iOS are supported.* |
| 9 | *Contributions for improvements and other platforms are welcome.* |
| 10 | |
| 11 | ### Documentation |
| 12 | |
| 13 | Documentation is available on [docs.rs](https://docs.rs/sysctl) |
| 14 | |
| 15 | ### Usage |
| 16 | |
| 17 | Add to `Cargo.toml` |
| 18 | |
| 19 | ```toml |
| 20 | [dependencies] |
| 21 | sysctl = "*" |
| 22 | ``` |
| 23 | |
| 24 | ### macOS/iOS |
| 25 | |
| 26 | * Due to limitations in the sysctl(3) API, many of the methods of |
| 27 | the `Ctl` take a mutable reference to `self` on macOS/iOS. |
| 28 | * Sysctl descriptions are not available on macOS/iOS and Linux. |
| 29 | * Some tests failures are ignored, as the respective sysctls do not |
| 30 | exist on macos. |
| 31 | |
| 32 | ### Example |
| 33 | |
| 34 | sysctl comes with several examples, see the examples folder: |
| 35 | |
| 36 | * `value.rs`: shows how to get a sysctl value |
| 37 | * `value_as.rs`: parsing values as structures |
| 38 | * `value_string.rs`: parsing values as string. Use this for cross platform compatibility since all sysctls are strings on Linux. |
| 39 | * `value_oid_as.rs`: getting a sysctl from OID constants from the `libc` crate. |
| 40 | * `set_value.rs`: shows how to set a sysctl value |
| 41 | * `struct.rs`: reading data into a struct |
| 42 | * `temperature.rs`: parsing temperatures |
| 43 | * `iterate.rs`: showcases iteration over the sysctl tree |
| 44 | |
| 45 | Run with: |
| 46 | |
| 47 | ```sh |
| 48 | $ cargo run --example iterate |
| 49 | ``` |
| 50 | |
| 51 | Or to use in your program: |
| 52 | |
| 53 | ```rust |
| 54 | extern crate sysctl; |
| 55 | use sysctl::Sysctl; |
| 56 | |
| 57 | fn main() { |
| 58 | let ctl = sysctl::Ctl::new("kern.osrevision").unwrap(); |
| 59 | println!("Description: {}", ctl.description().unwrap()); |
| 60 | println!("Value: {}", ctl.value_string().unwrap()); |
| 61 | } |
| 62 | ``` |
| 63 |