use super::Root;
use crate::{
Error, Result,
host::{OpenFiles, Session},
parse::{procfs, tz, utmp},
};
pub(crate) const MAX_NAME: usize = 255;
pub(crate) fn name(root: &Root) -> Result<String> {
root.with(
"/proc/sys/kernel/hostname",
"host name",
"/proc/sys/kernel/hostname",
|d| crate::parse::lossy(crate::parse::trim(d), MAX_NAME),
)
}
pub(crate) fn open_files(root: &Root) -> Result<OpenFiles> {
let (open, max) = root
.with(
"/proc/sys/fs/file-nr",
"open files",
"/proc/sys/fs/file-nr",
procfs::file_nr,
)?
.ok_or(Error::Malformed {
what: "open files",
source: "/proc/sys/fs/file-nr",
line: None,
})?;
Ok(OpenFiles {
open: Some(open),
max: Some(max),
})
}
pub(crate) fn zone_name(root: &Root, env: Option<&str>) -> Option<String> {
if let Some(name) = env.and_then(tz::from_env) {
return Some(name.to_owned());
}
let link = root.link_target("/etc/localtime");
if let Some(name) = link.as_deref().and_then(tz::from_link) {
return Some(name.to_owned());
}
root.with("/etc/timezone", "time zone", "/etc/timezone", |d| {
tz::from_file(d).map(str::to_owned)
})
.ok()
.flatten()
}
const fn layout() -> utmp::Layout {
if cfg!(target_arch = "x86_64") {
utmp::COMPAT32
} else {
utmp::NATIVE64
}
}
pub(crate) fn sessions(root: &Root) -> Result<Vec<Session>> {
let limit = utmp::MAX_RECORDS * 400;
for path in ["/run/utmp", "/var/run/utmp"] {
match root.read_prefix(path, limit, "sessions", path) {
Ok(data) => {
return Ok(utmp::parse(&data, layout())
.into_iter()
.map(|e| Session {
user: e.user,
terminal: e.terminal,
remote: e.remote,
since: e.since,
})
.collect());
}
Err(Error::NotFound { .. }) => {}
Err(e) => return Err(e),
}
}
Ok(Vec::new())
}
#[cfg(test)]
mod tests {
use super::{super::fixtures, *};
#[test]
fn test_linux_host_open_files_from_a_fixture() {
let f = open_files(&fixtures::root("wsl2")).unwrap();
assert!(f.open.unwrap() > 0 && f.open <= f.max);
assert!(
open_files(&fixtures::root("no-such-fixture"))
.unwrap_err()
.is_unavailable()
);
}
#[test]
fn test_linux_host_name_from_fixtures() {
for f in fixtures::ALL {
assert_eq!(name(&fixtures::root(f)).unwrap(), "fixture-host", "{f}");
}
}
}