Skip to content

Commit

Permalink
Merge pull request uutils#6406 from jadijadi/users-openbsd
Browse files Browse the repository at this point in the history
users: support OpenBSD using utmp
  • Loading branch information
cakebaker committed Jun 29, 2024
2 parents 69b603b + 576341b commit f451714
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 90 deletions.
34 changes: 30 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ time = { version = "0.3.36" }
unicode-segmentation = "1.11.0"
unicode-width = "0.1.12"
utf-8 = "0.7.6"
utmp-classic = "0.1.6"
walkdir = "2.5"
winapi-util = "0.1.8"
windows-sys = { version = "0.48.0", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions src/uu/users/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ path = "src/users.rs"
clap = { workspace = true }
uucore = { workspace = true, features = ["utmpx"] }

[target.'cfg(target_os = "openbsd")'.dependencies]
utmp-classic = { workspace = true }

[[bin]]
name = "users"
path = "src/main.rs"
14 changes: 0 additions & 14 deletions src/uu/users/src/platform/mod.rs

This file was deleted.

17 changes: 0 additions & 17 deletions src/uu/users/src/platform/openbsd.rs

This file was deleted.

53 changes: 0 additions & 53 deletions src/uu/users/src/platform/unix.rs

This file was deleted.

82 changes: 80 additions & 2 deletions src/uu/users/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,97 @@

// spell-checker:ignore (paths) wtmp

use std::ffi::OsString;
use std::path::Path;

use clap::builder::ValueParser;
use clap::{crate_version, Arg, Command};
use uucore::error::UResult;
use uucore::{format_usage, help_about, help_usage};

mod platform;
#[cfg(target_os = "openbsd")]
use utmp_classic::{parse_from_path, UtmpEntry};
#[cfg(not(target_os = "openbsd"))]
use uucore::utmpx::{self, Utmpx};

const ABOUT: &str = help_about!("users.md");
const USAGE: &str = help_usage!("users.md");

#[cfg(target_os = "openbsd")]
const OPENBSD_UTMP_FILE: &str = "/var/run/utmp";

static ARG_FILES: &str = "files";

fn get_long_usage() -> String {
#[cfg(not(target_os = "openbsd"))]
let default_path: &str = utmpx::DEFAULT_FILE;
#[cfg(target_os = "openbsd")]
let default_path: &str = OPENBSD_UTMP_FILE;
format!(
"Output who is currently logged in according to FILE.
If FILE is not specified, use {}. /var/log/wtmp as FILE is common.",
default_path
)
}

#[uucore::main]
use platform::uumain;
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app()
.after_help(get_long_usage())
.try_get_matches_from(args)?;

let files: Vec<&Path> = matches
.get_many::<OsString>(ARG_FILES)
.map(|v| v.map(AsRef::as_ref).collect())
.unwrap_or_default();

let mut users: Vec<String>;

// OpenBSD uses the Unix version 1 UTMP, all other Unixes use the newer UTMPX
#[cfg(target_os = "openbsd")]
{
let filename = if files.is_empty() {
Path::new(OPENBSD_UTMP_FILE)
} else {
files[0]
};
let entries = parse_from_path(&filename).unwrap_or(Vec::new());
users = Vec::new();
for entry in entries {
if let UtmpEntry::UTMP {
line: _,
user,
host: _,
time: _,
} = entry
{
if !user.is_empty() {
users.push(user);
}
}
}
};
#[cfg(not(target_os = "openbsd"))]
{
let filename = if files.is_empty() {
utmpx::DEFAULT_FILE.as_ref()
} else {
files[0]
};

users = Utmpx::iter_all_records_from(filename)
.filter(Utmpx::is_user_process)
.map(|ut| ut.user())
.collect::<Vec<_>>();
};

if !users.is_empty() {
users.sort();
println!("{}", users.join(" "));
}

Ok(())
}

pub fn uu_app() -> Command {
Command::new(uucore::util_name())
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ fn test_users_check_name() {

new_ucmd!().succeeds().stdout_is(&expected);
}

#[test]
#[cfg(target_os = "openbsd")]
fn test_users_check_name_openbsd() {
new_ucmd!()
.args(&["openbsd_utmp"])
.run()
.stdout_contains("test");
}
Binary file added tests/fixtures/users/openbsd_utmp
Binary file not shown.

0 comments on commit f451714

Please sign in to comment.