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

users: support OpenBSD using utmp #6406

Merged
merged 1 commit into from
Jun 29, 2024
Merged

Conversation

jadijadi
Copy link
Contributor

OpenBSD uses the original utmp file format so the general users does not work there. By using the utmp-classic lib, we cna process the current users on the system from the /var/run/utmp and show the list.

fixes #5665

@jadijadi
Copy link
Contributor Author

A few points:

  1. Not sure if we have to commit the Cargo.lock
  2. Based on my observation the native users on OpenBSD shows each name only once. (so jadi root regardless of different logins by jadi) This is different than the behaviour on Linux machines (that is jadi jadi jadi root root if 5 logins are there). Not sure which path we should go. I kept the Linux style for consistency in our tool but I'm open to keeping the OpenBSD version like the native one.
  3. if this will be approved, I can kind of quickly work other similar tools (say who) using the same utmp-classic parser.

@cakebaker
Copy link
Contributor

Can you please add zerocopy to the skip list in deny.toml or, as you are the author of utmp-classic, upgrade its zerocopy dependency?

new_ucmd!()
.args(&["basic_openbsd_utmp"])
.run()
.stdout_contains("jadi");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what means jadi ? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its my user name. no specific meaning. If not appropriate to use it, I can add a new user to my OpenBSD (say test_user), login and use that utmp file instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, please :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let _matches = uu_app().try_get_matches_from(args)?;
let matches = uu_app()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bunch of code is duplicated from the unix.rs
i would prefer that we have a single file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. they are different only in small parts. Will try to use the same file and merge this into that using a os_name check when reading data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done... tried to do it clean but please do not hesitate to comment & improve.

@@ -16,6 +16,7 @@ path = "src/users.rs"

[dependencies]
clap = { workspace = true }
utmp-classic = {version = "0.1.5"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please define it in /Cargo.toml and use workspace = true
and make it openbsd only

@jadijadi
Copy link
Contributor Author

Can you please add zerocopy to the skip list in deny.toml or, as you are the author of utmp-classic, upgrade its zerocopy dependency?

will work on it. The last time I tested the utmp-classic did not worked using the latest zeroconf. Obviously its cleaner to fix the code to work with the latest version. will work on that.

@jadijadi
Copy link
Contributor Author

Can you please add zerocopy to the skip list in deny.toml or, as you are the author of utmp-classic, upgrade its zerocopy dependency?

done. upgraded to the latest zerocopy

Copy link
Contributor

@lcheylus lcheylus May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add spell-checker exception for testuser => prevent error in CI job "Style/spelling (ubuntu-latest, feat_os_unix)" : tests/by-util/test_users.rs:41:27 - Unknown word (testuser)

spell-checker:ignore (words) testuser

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edited the sample utmp file to make it cleaner. Now it includes two logins for test, one for user and one for root. cleaner for tests and no need for spell-check ignore I imagine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK with the last version of your tests/fixtures/users/openbsd_utmp file:

  • test tests/by-util/test_users.rs::test_users_check_name_openbsd is OK to check user test
  • no need to add an exception for spell-check.

@lcheylus
Copy link
Contributor

@jadijadi Congrats for your work to add support for OpenBSD on coreutils project. I will use your utmp-classic crate to fix uptime command on OpenBSD (see issue # #5664) (WIP in my own fork).

Some comments - review for this PR

  1. Please, amend your commit message / title of this PR : "added to the openbsd" is not very understandable. I propose the message "users: support OpenBSD using utmp"

  2. I merged your code in a local branch based on upstream/main :

  • Build OK on OpenBSD current/amd64 with Rust 1.77.2 :
$ cargo build --no-default-features --features users

$ ./target/debug/coreutils users -V
users 0.0.26
$ ./target/debug/coreutils users -h
Print the user names of users currently logged in to the current host.

Usage: ./target/debug/coreutils users [FILE]
(...)
  • Unit tests OK with cargo test
$ cargo test --no-default-features --features users
(...)
test test_users::test_invalid_arg ... ok
test test_users::test_users_check_name_openbsd ... ok
test test_users::test_users_no_arg ... ok

test result: ok. 29 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.14s
  • Tests on OpenBSD vs GNU coreutils (install package coreutils-9.4p0 via pkg_add) :

With 1 logged user:

$ ./target/debug/coreutils users
fox
$ gusers
fox

With 3 logged users (user fox logged twice + root):

$ ./target/debug/coreutils users
fox fox root
$ gusers
fox fox root

=> same results for commands users with Rust uutils/coreutils and GNU coreutils :)

NB : with OpenBSD native command users, duplicate entries are removed in output (with 3 logged users: users => fox root). But we must keep the same output than GNU coreutils users command.

@jadijadi
Copy link
Contributor Author

@jadijadi Congrats for your work to add support for OpenBSD on coreutils project. I will use your utmp-classic crate to fix uptime command on OpenBSD (see issue # #5664) (WIP in my own fork).

Great and thanks for the warm words. I'm also working on other utmp related tools on OpenBSD. Will send PRs after this one is finalized.

  1. Please, amend your commit message / title of this PR : "added to the openbsd" is not very understandable. I propose the message "users: support OpenBSD using utmp"

done. thanks for the hint.

  1. I merged your code in a local branch based on upstream/main :
  • Build OK on OpenBSD current/amd64 with Rust 1.77.2 :
    ...
    With 3 logged users (user fox logged twice + root):
$ ./target/debug/coreutils users
fox fox root
$ gusers
fox fox root

=> same results for commands users with Rust uutils/coreutils and GNU coreutils :)

NB : with OpenBSD native command users, duplicate entries are removed in output (with 3 logged users: users => fox root). But we must keep the same output than GNU coreutils users command.

As you mentioned, I kept the GNU compatibility and showing all logged in users and not removing the duplicate user names.

@lcheylus
Copy link
Contributor

lcheylus commented May 21, 2024

Great and thanks for the warm words. I'm also working on other utmp related tools on OpenBSD. Will send PRs after this one is finalized.

I have a working implementation for uptime tool on OpenBSD using your utmp-classic crate. I will submit it as soon as this PR is merged.

$ ./target/debug/coreutils uptime -s
2024-05-21 12:36:04

$ ./target/debug/coreutils uptime && guptime
21:04:05 up   8:28, 1 user,  load average: 0.37, 0.48, 0.39
21:04:05  up   8:28,  1 user,  load average: 0.37, 0.48, 0.39

For who unsupported on OpenBSD, I haven't found solution to implement all features equivalent to GNU coreutils version. Some ("print dead processes") need infos from UTMPX, not available in UTMP file.

  1. Please, amend your commit message / title of this PR : "added to the openbsd" is not very understandable. I propose the message "users: support OpenBSD using utmp"

done. thanks for the hint.

Thanks, please edit also the title of this PR ("users: support OpenBSD using utmp")

@jadijadi jadijadi changed the title users: added to the openbsd users: support OpenBSD using utmp May 22, 2024
@lcheylus
Copy link
Contributor

@sylvestre @cakebaker Please, could you do a final review of this PR and merge it if OK?

I'm working on a WIP to support uptime on OpenBSD and also use utmp-classic crate added in this PR.

Comment on lines 59 to 62
let entries = match parse_from_path(&filename) {
Ok(data) => data,
Err(_) => Vec::new(),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is more idiomatic to use unwrap_or instead of a match.

Suggested change
let entries = match parse_from_path(&filename) {
Ok(data) => data,
Err(_) => Vec::new(),
};
let entries = parse_from_path(&filename).unwrap_or(Vec::new());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as suggested.

#[cfg(not(target_os = "openbsd"))]
let default_path: &str = utmpx::DEFAULT_FILE;
#[cfg(target_os = "openbsd")]
let default_path: &str = "/var/log/wtmp";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a mismatch with the default path used in uumain: /var/run/utmp (my guess is that this is the correct path). It might make sense to define an OpenBSD specific const with the default path and then use this const in both places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cakebaker , fixed this based on your comment

Copy link
Contributor

@cakebaker cakebaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the exception of the two details it looks good from my point of view.

OpenBSD uses the original utmp file format so the general `users` does not
work there. By using the `utmp-classic` lib, we cna process the current users
on the system from the /var/run/utmp and show the list.

fixes uutils#5665
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/inotify-dir-recreate (passes in this run but fails in the 'main' branch)

@cakebaker cakebaker merged commit f451714 into uutils:main Jun 29, 2024
67 of 68 checks passed
@cakebaker
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

users: support on OpenBSD
4 participants