bpf_sys/
uname.rs

1// Copyright 2019 Authors of Red Sift
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use std::os::raw::c_char;
9use std::ffi::CStr;
10use std::mem;
11use std::str::from_utf8_unchecked;
12use std::str::FromStr;
13use std::fs;
14
15#[allow(clippy::result_unit_err)]
16pub fn uname() -> Result<::libc::utsname, ()> {
17    let mut uname = unsafe { mem::zeroed() };
18    let res = unsafe { ::libc::uname(&mut uname) };
19    if res < 0 {
20        Err(())
21    } else {
22        Ok(uname)
23    }
24}
25
26#[inline]
27pub fn get_kernel_internal_version() -> Option<u32> {
28    let version = if let Ok(version) = fs::read_to_string("/proc/version_signature") {
29        parse_version_signature(&version.trim())?
30    } else {
31        to_str(&uname().ok()?.release).into()
32    };
33
34    parse_version(&version).map(|(major, minor, patch)| {
35        major << 16 | minor << 8 | patch
36    })
37}
38
39#[allow(clippy::result_unit_err)]
40#[inline]
41pub fn get_fqdn() -> Result<String, ()> {
42    let uname = uname()?;
43    let mut hostname = to_str(&uname.nodename).to_string();
44    let domainname = to_str(&uname.domainname);
45    if domainname != "(none)" {
46        hostname.push('.');
47        hostname.push_str(domainname);
48    }
49
50    Ok(hostname)
51}
52
53#[inline]
54pub fn to_str(bytes: &[c_char]) -> &str {
55    unsafe { from_utf8_unchecked(CStr::from_ptr(bytes.as_ptr()).to_bytes()) }
56}
57
58fn parse_version_signature(signature: &str) -> Option<String> {
59    let parts: Vec<_> = signature.split(' ').collect();
60    if parts.len() != 3 {
61        return None;
62    }
63
64    parts.last().map(|v| <&str>::clone(v).into())
65}
66
67fn parse_version(version: &str) -> Option<(u32, u32, u32)> {
68    if let Some(version) = version.splitn(2, '-').next() {
69        if let Some(version) = version.splitn(2, '+').next() {
70            let parts: Vec<_> = version.splitn(3, '.').filter_map(|v| u32::from_str(v).ok()).collect();
71            if parts.len() != 3 {
72                return None;
73            }
74            return Some((parts[0], parts[1], parts[2]))
75        }
76    }
77
78    None
79}
80
81#[cfg(test)]
82mod test {
83    use super::*;
84
85    #[test]
86    fn test_parse_version() {
87        assert_eq!(parse_version("4.15.18"), Some((4, 15, 18)));
88        assert_eq!(parse_version("4.15.1-generic"), Some((4, 15, 1)));
89        assert_eq!(parse_version("4.15.1-generic-foo"), Some((4, 15, 1)));
90        assert_eq!(parse_version("4.14.138+"), Some((4, 14, 138)));
91        assert_eq!(parse_version("4.3.2.1"), None);
92        assert_eq!(parse_version("4.2.foo"), None);
93        assert_eq!(parse_version("4.2."), None);
94        assert_eq!(parse_version("4.2"), None);
95        assert_eq!(parse_version("foo"), None);
96        assert_eq!(parse_version(""), None);
97    }
98
99    #[test]
100    fn test_parse_version_signature() {
101        assert_eq!(parse_version_signature("Ubuntu 4.15.0-55.60-generic 4.15.18"), Some("4.15.18".into()));
102        assert_eq!(parse_version_signature("Ubuntu 4.15.0-55.60-generic 4.15.18 foo"), None);
103        assert_eq!(parse_version_signature("Ubuntu 4.15.0-55.60-generic"), None);
104    }
105}