sys_lib/host.rs
1//! The machine and its environment.
2//!
3//! ```
4//! use sys_lib as sys;
5//!
6//! println!("running on {}", sys::host::name()?);
7//! # Ok::<(), sys_lib::Error>(())
8//! ```
9
10use crate::Result;
11
12/// The host name. A live reading: DHCP and `hostnamectl` change it while
13/// processes run. A name that is not UTF-8 is converted lossily, and one
14/// longer than 255 bytes is truncated (E-95).
15///
16/// The host name is an identifying value: keep it out of output that leaves
17/// the machine unless that is intended.
18///
19/// # Errors
20///
21/// `NotFound` or `PermissionDenied` when a sandbox hides it; `Os` when the
22/// system call fails.
23pub fn name() -> Result<String> {
24 crate::sys::host_name()
25}
26
27#[cfg(test)]
28mod tests {
29 #[test]
30 fn test_host_name_is_not_empty() {
31 let name = super::name().unwrap();
32 assert!(!name.is_empty() && name.len() <= 255);
33 }
34}