Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of Illumos support #305

Merged
merged 17 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Right now, the following operating system types can be returned:
- Fedora
- FreeBSD
- HardenedBSD
- Illumos
- Linux
- Linux Mint
- macOS (Mac OS X or OS X)
Expand Down
2 changes: 2 additions & 0 deletions cspell-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ endeavouros
freebsd
hardenedbsd
hbsd
illumos
isainfo
libntdll
linuxmint
macos
Expand Down
10 changes: 10 additions & 0 deletions os_info/src/bitness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt::{self, Display, Formatter};
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "illumos",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
Expand Down Expand Up @@ -78,6 +79,15 @@ pub fn get() -> Bitness {
}
}

#[cfg(target_os = "illumos")]
pub fn get() -> Bitness {
match &Command::new("isainfo").arg("-b").output() {
Ok(Output { stdout, .. }) if stdout == b"64\n" => Bitness::X64,
Ok(Output { stdout, .. }) if stdout == b"32\n" => Bitness::X32,
_ => Bitness::Unknown,
}
}

#[cfg(all(
test,
any(
Expand Down
67 changes: 67 additions & 0 deletions os_info/src/illumos/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::process::Command;
use std::str;

use log::{error, trace};

use crate::{bitness, uname::uname, Info, Type, Version};

pub fn current_platform() -> Info {
trace!("illumos::current_platform is called");

let version = get_version()
.map(Version::from_string)
.unwrap_or_else(|| Version::Unknown);

let info = Info {
os_type: get_os(),
version,
bitness: bitness::get(),
..Default::default()
};

trace!("Returning {:?}", info);
info
}

fn get_version() -> Option<String> {
Command::new("uname")
.arg("-v")
.output()
.map_err(|e| {
error!("Failed to invoke 'uname': {:?}", e);
})
.ok()
.and_then(|out| {
if out.status.success() {
Some(String::from_utf8_lossy(&out.stdout).trim_end().to_owned())
} else {
log::error!("'uname' invocation error: {:?}", out);
None
}
})
}

fn get_os() -> Type {
let os = Command::new("uname")
.arg("-o")
.output()
.expect("Failed to get OS");

match str::from_utf8(&os.stdout) {
Ok("illumos\n") => Type::Illumos,
Ok(_) => Type::Unknown,
Err(_) => Type::Unknown,
}
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn os_type() {
let version = current_platform();
assert_eq!(Type::Illumos, version.os_type());
}
}
6 changes: 6 additions & 0 deletions os_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ mod imp;
#[path = "freebsd/mod.rs"]
mod imp;

#[cfg(target_os = "illumos")]
#[path = "illumos/mod.rs"]
mod imp;

#[cfg(target_os = "linux")]
#[path = "linux/mod.rs"]
mod imp;
Expand Down Expand Up @@ -55,6 +59,7 @@ mod imp;
target_os = "dragonfly",
target_os = "emscripten",
target_os = "freebsd",
target_os = "illumos",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
Expand All @@ -73,6 +78,7 @@ mod os_type;
#[cfg(any(
target_os = "dragonfly",
target_os = "freebsd",
target_os = "illumos",
target_os = "netbsd",
target_os = "openbsd"
))]
Expand Down
3 changes: 3 additions & 0 deletions os_info/src/os_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub enum Type {
/// Linux based operating system (<https://en.wikipedia.org/wiki/Linux>).
HardenedBSD,
/// HardenedBSD (https://hardenedbsd.org/)
Illumos,
/// Illumos (https://en.wikipedia.org/wiki/Illumos)
Linux,
/// Mac OS X/OS X/macOS (<https://en.wikipedia.org/wiki/MacOS>).
Macos,
Expand Down Expand Up @@ -87,6 +89,7 @@ impl Display for Type {
Type::Amazon => write!(f, "Amazon Linux AMI"),
Type::Arch => write!(f, "Arch Linux"),
Type::DragonFly => write!(f, "DragonFly BSD"),
Type::Illumos => write!(f, "illumos"),
Type::Macos => write!(f, "Mac OS"),
Type::MidnightBSD => write!(f, "Midnight BSD"),
Type::Mint => write!(f, "Linux Mint"),
Expand Down