Skip to content

Commit 03e8444

Browse files
committed
utmpx: fall back to systemd-logind on Linux when traditional utmp is empty
On Linux, keep preferring the traditional utmp/utmpx file when it is usable. When the default utmp file is missing or empty (e.g. recent Ubuntu where systemd no longer populates /var/run/utmp), try the systemd-logind backend instead, and fall back to the traditional utmp iterator when libsystemd cannot be loaded, a required symbol is missing, or systemd-logind initialization fails. libsystemd.so.0 is loaded dynamically via libloading (enabled through uucore's `utmpx` feature, Linux only), so the resulting binary has no NEEDED entry for libsystemd and remains usable on systems without it. Additional changes: - Remove the `feat_systemd_logind` opt-in feature and its propagation through `users`, `who`, `pinky`, and `uptime`. - Read the boot time (`who -b` / `uptime -s`) from the kernel's /proc/stat `btime` instead of the /var/lib/systemd/random-seed mtime. - Keep the existing traditional utmp/utmpx behavior on non-Linux. Related to #3219.
1 parent 697df98 commit 03e8444

13 files changed

Lines changed: 317 additions & 126 deletions

File tree

‎.github/workflows/openbsd.yml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,9 @@ jobs:
221221
cargo test --features '${{ matrix.job.features }}' || FAULT=1
222222
echo "::endgroup::"
223223
fi
224-
# There is no systemd-logind on OpenBSD, so test all features except feat_systemd_logind
225224
if (test -z "\$FAULT"); then
226225
echo "::group::cargo test (uucore)"
227-
UUCORE_FEATURES=\$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "uucore") | .features | keys | .[]' | grep -v "feat_systemd_logind" | paste -s -d "," -)
226+
UUCORE_FEATURES=\$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name == "uucore") | .features | keys | .[]' | paste -s -d "," -)
228227
cargo test --features "\$UUCORE_FEATURES" -p uucore || FAULT=1
229228
echo "::endgroup::"
230229
fi

‎.vscode/cspell.dictionaries/workspace.wordlist.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ doctest
55
# * crates
66
aho-corasick
77
blake2b_simd
8+
libloading
89
rustix
910

1011
# * uutils project

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ uudoc = [
4141
## Optional feature for stdbuf
4242
# "feat_external_libstdbuf" == use an external libstdbuf.so for stdbuf instead of embedding it
4343
feat_external_libstdbuf = ["stdbuf/feat_external_libstdbuf"]
44-
# "feat_systemd_logind" == enable feat_systemd_logind support for utmpx replacement
45-
feat_systemd_logind = [
46-
"pinky/feat_systemd_logind",
47-
"uptime/feat_systemd_logind",
48-
"users/feat_systemd_logind",
49-
"uucore/feat_systemd_logind",
50-
"who/feat_systemd_logind",
51-
]
5244
# "feat_acl" == enable support for ACLs (access control lists; by using`--features feat_acl`)
5345
# NOTE:
5446
# * On linux, the posix-acl/acl-sys crate requires `libacl` headers and shared library to be accessible in the C toolchain at compile time.
@@ -413,6 +405,7 @@ itoa = "1.0.15"
413405
jiff = "0.2.18"
414406
jiff-icu = "0.2.2"
415407
libc = "0.2.186"
408+
libloading = "0.8.9"
416409
lscolors = { version = "0.21.0", default-features = false, features = [
417410
"gnu_legacy",
418411
] }

‎src/uu/pinky/Cargo.toml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ readme.workspace = true
1616
[lints]
1717
workspace = true
1818

19-
[features]
20-
feat_systemd_logind = ["uucore/feat_systemd_logind"]
21-
2219
[lib]
2320
path = "src/pinky.rs"
2421
doctest = false

‎src/uu/uptime/Cargo.toml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ readme.workspace = true
1616
[lints]
1717
workspace = true
1818

19-
[features]
20-
feat_systemd_logind = ["uucore/feat_systemd_logind"]
21-
2219
[lib]
2320
path = "src/uptime.rs"
2421
test = false

‎src/uu/users/Cargo.toml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ readme.workspace = true
1616
[lints]
1717
workspace = true
1818

19-
[features]
20-
feat_systemd_logind = ["uucore/feat_systemd_logind"]
21-
2219
[lib]
2320
path = "src/users.rs"
2421
test = false

‎src/uu/who/Cargo.toml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ readme.workspace = true
1717
[lints]
1818
workspace = true
1919

20-
[features]
21-
feat_systemd_logind = ["uucore/feat_systemd_logind"]
22-
2320
[lib]
2421
path = "src/who.rs"
2522
test = false

‎src/uucore/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ walkdir = { workspace = true, optional = true }
112112
xattr = { workspace = true, optional = true }
113113

114114
[target.'cfg(target_os = "linux")'.dependencies]
115+
libloading = { workspace = true, optional = true }
115116
procfs = { workspace = true, optional = true }
116117

117118
[target.'cfg(target_os = "windows")'.dependencies]
@@ -144,7 +145,6 @@ fsext = ["libc", "windows-sys", "bstr"]
144145
fsxattr = ["xattr", "itertools", "libc"]
145146
hardware = []
146147
lines = []
147-
feat_systemd_logind = ["utmpx", "libc"]
148148
format = [
149149
"bigdecimal",
150150
"extendedbigdecimal",
@@ -201,7 +201,7 @@ sum = [
201201
]
202202
update-control = ["parser"]
203203
utf8 = []
204-
utmpx = ["time", "time/macros", "libc", "dns-lookup"]
204+
utmpx = ["time", "time/macros", "libc", "dns-lookup", "dep:libloading"]
205205
version-cmp = []
206206
wide = []
207207
tty = []

‎src/uucore/src/lib/features.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub mod selinux;
9292
pub mod signals;
9393
#[cfg(all(feature = "smack", target_os = "linux"))]
9494
pub mod smack;
95-
#[cfg(feature = "feat_systemd_logind")]
95+
#[cfg(all(target_os = "linux", feature = "utmpx"))]
9696
pub mod systemd_logind;
9797
#[cfg(all(
9898
unix,

0 commit comments

Comments
 (0)