]> git.proxmox.com Git - cargo.git/blob - vendor/os_info/src/freebsd/mod.rs
New upstream version 0.63.1
[cargo.git] / vendor / os_info / src / freebsd / mod.rs
1 use std::process::Command;
2 use std::str;
3
4 use log::{error, trace};
5
6 use crate::{bitness, uname::uname, Info, Type, Version};
7
8 pub fn current_platform() -> Info {
9 trace!("freebsd::current_platform is called");
10
11 let version = uname()
12 .map(Version::from_string)
13 .unwrap_or_else(|| Version::Unknown);
14
15 let info = Info {
16 os_type: get_os(),
17 version,
18 bitness: bitness::get(),
19 ..Default::default()
20 };
21
22 trace!("Returning {:?}", info);
23 info
24 }
25
26 fn get_os() -> Type {
27 let os = Command::new("uname")
28 .arg("-s")
29 .output()
30 .expect("Failed to get OS");
31
32 match str::from_utf8(&os.stdout) {
33 Ok("FreeBSD\n") => {
34 let check_hardening = Command::new("sysctl")
35 .arg("hardening.version")
36 .output()
37 .expect("Failed to check if is hardened");
38 match str::from_utf8(&check_hardening.stderr) {
39 Ok("0\n") => Type::HardenedBSD,
40 Ok(_) => Type::FreeBSD,
41 Err(_) => Type::FreeBSD,
42 }
43 }
44 Ok("MidnightBSD\n") => Type::MidnightBSD,
45 Ok(_) => Type::Unknown,
46 Err(_) => Type::Unknown,
47 }
48 }
49
50 #[cfg(test)]
51 mod tests {
52 use super::*;
53 use pretty_assertions::assert_eq;
54
55 #[test]
56 fn os_type() {
57 let version = current_platform();
58 assert_eq!(Type::FreeBSD, version.os_type());
59 }
60 }