libdd_telemetry/
info.rs

1// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4pub mod os {
5    #[cfg(unix)]
6    use std::ffi::CStr;
7
8    // TODO: this function will call API's (fargate, k8s, etc) in the future to get to real host API
9    pub fn real_hostname() -> anyhow::Result<String> {
10        Ok(sys_info::hostname()?)
11    }
12
13    pub const fn os_name() -> &'static str {
14        std::env::consts::OS
15    }
16
17    pub fn os_version() -> anyhow::Result<String> {
18        sys_info::os_release().map_err(|e| e.into())
19    }
20
21    pub fn os_type() -> Option<String> {
22        sys_info::os_type().ok()
23    }
24
25    pub fn os_release() -> Option<String> {
26        sys_info::os_release().ok()
27    }
28
29    /// Get string similar to `uname -a`'s output
30    ///
31    /// # Safety
32    ///   Unsafe because of FFI, libc's uname only fails if struct utsname is
33    ///   malformed, considering we `zeroed` it, it virtually cannot be
34    ///   malformed. All in all pretty safe
35    #[cfg(unix)]
36    pub unsafe fn uname() -> Option<String> {
37        let mut n = std::mem::zeroed();
38        match libc::uname(&mut n) {
39            0 => Some(
40                CStr::from_ptr(n.version.as_ptr())
41                    .to_string_lossy()
42                    .into_owned(),
43            ),
44            _ => None,
45        }
46    }
47}