1 //! Get system identification
2 use crate::{Errno, Result};
3 use libc::c_char;
4 use std::ffi::OsStr;
5 use std::mem;
6 use std::os::unix::ffi::OsStrExt;
7 
8 /// Describes the running system.  Return type of [`uname`].
9 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
10 #[repr(transparent)]
11 pub struct UtsName(libc::utsname);
12 
13 impl UtsName {
14     /// Name of the operating system implementation.
sysname(&self) -> &OsStr15     pub fn sysname(&self) -> &OsStr {
16         cast_and_trim(&self.0.sysname)
17     }
18 
19     /// Network name of this machine.
nodename(&self) -> &OsStr20     pub fn nodename(&self) -> &OsStr {
21         cast_and_trim(&self.0.nodename)
22     }
23 
24     /// Release level of the operating system.
release(&self) -> &OsStr25     pub fn release(&self) -> &OsStr {
26         cast_and_trim(&self.0.release)
27     }
28 
29     /// Version level of the operating system.
version(&self) -> &OsStr30     pub fn version(&self) -> &OsStr {
31         cast_and_trim(&self.0.version)
32     }
33 
34     /// Machine hardware platform.
machine(&self) -> &OsStr35     pub fn machine(&self) -> &OsStr {
36         cast_and_trim(&self.0.machine)
37     }
38 
39     /// NIS or YP domain name of this machine.
40     #[cfg(linux_android)]
domainname(&self) -> &OsStr41     pub fn domainname(&self) -> &OsStr {
42         cast_and_trim(&self.0.domainname)
43     }
44 }
45 
46 /// Get system identification
uname() -> Result<UtsName>47 pub fn uname() -> Result<UtsName> {
48     unsafe {
49         let mut ret = mem::MaybeUninit::zeroed();
50         Errno::result(libc::uname(ret.as_mut_ptr()))?;
51         Ok(UtsName(ret.assume_init()))
52     }
53 }
54 
cast_and_trim(slice: &[c_char]) -> &OsStr55 fn cast_and_trim(slice: &[c_char]) -> &OsStr {
56     let length = slice
57         .iter()
58         .position(|&byte| byte == 0)
59         .unwrap_or(slice.len());
60     let bytes =
61         unsafe { std::slice::from_raw_parts(slice.as_ptr().cast(), length) };
62 
63     OsStr::from_bytes(bytes)
64 }
65