Skip to content

Skip a corrupt utmpx entry rather than abandoning the whole read - #3643

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:utmpx-hardening
Aug 16, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:utmpx-hardening

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Aug 16, 2026 •

Copy link
Copy Markdown
Member

getSessions() treated a single entry failing its sanity check as a reason to discard the native read entirely and re-query every session through the who command:

// Sanity check. If errors, default to who command line
if (!isSessionValid(user, device, loginTime)) {
    return oshi.util.driver.unix.Who.queryWho();
}
whoList.add(new OSSession(user, device, loginTime, host));

So one bad record cost the whole result and a fork/exec. Now it costs that one session:

// The utmpx table is not reentrant. A session ending while this loop runs can hand
// back a partially written entry, so drop that one rather than abandoning a read
// whose other entries are fine.
if (isSessionValid(user, device, loginTime)) {
    whoList.add(new OSSession(user, device, loginTime, host));
}

with the who fallback reserved for a native read that yields nothing at all.

Why a corrupt entry is expected

The utmpx table is not reentrant, and synchronized only serialises OSHI's own readers — it says nothing about the rest of the system. A session ending while we iterate can legitimately hand back a partially written record. That makes a bad entry a normal consequence of reading a table another process is mutating, not evidence the read is unusable.

Treating it as fatal also had the perverse effect of making the rarer path the more expensive one: the response to a transient glitch was to spawn a process and re-read everything.

Scope

Applied identically to all eight implementations — JNA and FFM across Linux, macOS, FreeBSD and Solaris. Both Linux implementations already fell back only when the list came back empty (trying the systemd files first), so they needed only the skip half; the other six also gained the empty-list guard.

Testing

Full gate green: 1338 / 119 / 100 / 30 tests, checkstyle 0 violations, forbiddenapis, javadoc, and the error-prone/NullAway profile.

Only the macOS pair is exercisable on my hardware, so the Linux, FreeBSD and Solaris paths are compiled but untested on their platforms. The change is uniform across all eight, which is the main thing keeping that risk low.

What this does and does not fix

It makes the who fallback rare rather than routine, which shrinks exposure to ExecutingCommand on platforms where that path has looked fragile. It is not a proven fix for any specific crash — I have not confirmed which call inside the fallback was at fault, only that the fallback was being reached far more often than it should have been.

Summary by CodeRabbit

  • Bug Fixes

    • Improved session detection across Linux, macOS, FreeBSD, and Solaris when system records are corrupt or partially written.
    • Invalid records are now skipped so valid sessions remain available.
    • The legacy who fallback is used only when no sessions are successfully read or a native read error occurs.
  • Documentation

    • Added release notes for version 7.5.0.

getSessions() treated a single entry failing its sanity check as a
reason to discard the native read entirely and re-query every session
through the who command. One bad record therefore cost the whole result
and a fork/exec, on all eight implementations: JNA and FFM across Linux,
macOS, FreeBSD and Solaris.

The utmpx table is not reentrant, and synchronized only serialises
OSHI's own readers. A session ending while the loop runs can hand back a
partially written entry, so a corrupt record is an expected consequence
of reading a table another process is mutating, not a sign the read is
unusable. Skip that entry and keep the rest.

The who fallback now applies only when the native read yields nothing at
all, which makes it rare rather than routine. Both Linux implementations
already had that shape, falling back through the systemd files first;
they needed only the skip.

Compiles and passes the gate; the macOS pair is the only path
exercisable here, so the other six are untested on their platforms.
@coderabbitai

coderabbitai Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e8b15cc0-dbd1-4bc6-b06f-040bac9b8f21

📥 Commits

Reviewing files that changed from the base of the PR and between a97bd07 and 4716eef.

📒 Files selected for processing (9)
  • CHANGELOG.md
  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • oshi-core/src/main/java/oshi/driver/mac/Who.java
  • oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java
  • oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java

Included review availability: Your plan includes up to 8 reviews per rolling hour; 7 remain after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout. (10)
  • GitHub Check: Test JDK 25, FreeBSD
  • GitHub Check: Test JDK 25, OpenBSD
  • GitHub Check: Test JDK 25, OpenIndiana
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Error Prone
  • GitHub Check: Analyze (java)
🧰 Additional context used
📓 Path-based instructions (3)
**/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

**/src/main/java/**/*.java: Before writing any parsing helper, check whether ParseUtil already has one.
Do not use fully-qualified class names in
code or in javadoc — including inside {@link ...}. Import the type and link the simple name.
Every public and protected type and method needs javadoc.
New files get a current-year-only
header (Copyright 2026 The OSHI Project Contributors / SPDX-License-Identifier: MIT)
new classes are
expected to declare one
Do not reach for @Nullable to make a null return legal: the convention is a sentinel, not a
null

System calls are expensive, and callers poll. Values that are constant (CPU model,
serial number) or expensive-but-slow-changing are wrapped in
oshi.util.Memoizer.memoize(supplier, ...)

Files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/mac/Who.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
oshi-core-ffm/src/main/java/**/*.java

📄 CodeRabbit inference engine (AGENTS.md)

oshi-core-ffm may not reference com.sun.jna.**.

Files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
**/*.{java,xml,md}

📄 CodeRabbit inference engine (AGENTS.md)

Do not commit generated output, target/, or IDE files.

Files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/mac/Who.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • CHANGELOG.md
  • oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
🧠 Learnings (7)
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/mac/Who.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
📚 Learning: 2026-07-30T00:35:22.638Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3547
File: oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdFirmware.java:82-82
Timestamp: 2026-07-30T00:35:22.638Z
Learning: In OSHI, treat changes as “API-breaking” only when they affect classes (or relevant API surface) annotated with `PublicAPI`. Public classes/members that are not annotated with `PublicAPI` should be reviewed as internal implementation details, even if they are public (e.g., classes like `oshi.hardware.common.platform.unix.BsdFirmware`). Also, respect intentional visibility decisions used for tests—e.g., methods like `BsdFirmware.parseDmesg(List<String>)` may be intentionally package-private to support tests within the same package; avoid widening visibility or altering signatures unless the related `PublicAPI` contract requires it.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/mac/Who.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
📚 Learning: 2026-08-02T23:56:23.866Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3565
File: oshi-core-ffm/src/main/java/oshi/ffm/platform/unix/PosixLibcFunctions.java:51-56
Timestamp: 2026-08-02T23:56:23.866Z
Learning: For Java FFM bindings in the oshi-core-ffm module, assume a 64-bit JVM and 64-bit operating system. Do not flag 32-bit native ABI or memory-layout compatibility concerns, consistent with the project's JDK 25 support policy, unless that policy changes.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
📚 Learning: 2026-08-03T00:46:14.953Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3566
File: oshi-common/src/main/java/oshi/software/common/os/unix/openbsd/OpenBsdNetworkParams.java:18-22
Timestamp: 2026-08-03T00:46:14.953Z
Learning: In the OSHI Java codebase, do not require duplicate Javadoc on public methods annotated with Override when the inherited Javadoc from the parent declaration adequately documents the method. The general public-member Javadoc requirement should not flag these overrides solely because they lack redundant documentation.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/mac/Who.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
📚 Learning: 2026-08-15T14:25:48.170Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3625
File: oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxNetworkIF.java:65-69
Timestamp: 2026-08-15T14:25:48.170Z
Learning: In OSHI Java code, treat `oshi.util.Util.isBlank(String)` as equivalent to `value == null || value.isEmpty()`: it does not consider whitespace-only strings blank. Do not report a behavior change when code replaces `!Util.isBlank(value)` with the equivalent `value != null && !value.isEmpty()` condition.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/mac/Who.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
📚 Learning: 2026-08-15T15:43:29.296Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3628
File: oshi-core-ffm/src/main/java/oshi/driver/windows/registry/HkeyUserDataFFM.java:108-108
Timestamp: 2026-08-15T15:43:29.296Z
Learning: In the OSHI Java codebase, use the sentinel-over-null convention at public API boundaries. Private helpers may return an explicitly JSpecify-annotated nullable value when the caller handles the missing result clearly and locally; callers must check for null before accessing the result. For example, methods such as lookupAccountBySid(String) may return String Nullable [] when queryUserSessions() performs the required null check.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/mac/Who.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
📚 Learning: 2026-08-15T15:59:20.875Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3629
File: oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsPowerSourceFFM.java:75-75
Timestamp: 2026-08-15T15:59:20.875Z
Learning: For OSHI backend twin classes whose filenames end in JNA.java or FFM.java, do not flag missing Javadoc on public forwarding constructors when config/checkstyle-suppressions.xml exempts them. Treat the documented abstract parent constructor as the contract location and avoid requiring duplicate Javadoc for the forwarded parameters.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java
  • oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java
🔇 Additional comments (9)
oshi-core-ffm/src/main/java/oshi/driver/linux/WhoFFM.java (1)

71-75: LGTM!

oshi-core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java (1)

66-70: LGTM!

Also applies to: 80-83

oshi-core-ffm/src/main/java/oshi/driver/unix/freebsd/WhoFFM.java (1)

56-60: LGTM!

Also applies to: 70-73

oshi-core-ffm/src/main/java/oshi/driver/unix/solaris/WhoFFM.java (1)

56-60: LGTM!

Also applies to: 70-73

oshi-core/src/main/java/oshi/driver/linux/WhoJNA.java (1)

78-82: LGTM!

oshi-core/src/main/java/oshi/driver/mac/Who.java (1)

50-65: LGTM!

oshi-core/src/main/java/oshi/driver/unix/freebsd/Who.java (1)

51-66: LGTM!

oshi-core/src/main/java/oshi/driver/unix/solaris/Who.java (1)

51-66: LGTM!

CHANGELOG.md (1)

62-62: LGTM!


📝 Walkthrough

Walkthrough

The utmpx readers now skip corrupt or partially written records and continue scanning. The who fallback runs only when native processing returns no sessions. The change applies to FFM and JNA implementations on Linux, macOS, FreeBSD, and Solaris.

Changes

utmpx session enumeration

Layer / File(s) Summary
Invalid-record handling and fallback policy
oshi-core-ffm/src/main/java/oshi/driver/{linux,mac,unix/freebsd,unix/solaris}/WhoFFM.java, oshi-core/src/main/java/oshi/driver/{linux/WhoJNA,mac,unix/freebsd,unix/solaris}/Who.java, CHANGELOG.md
The readers skip invalid utmpx records, retain valid sessions, and use the who fallback only when the native result is empty. The changelog records the behavior in version 7.5.0.

Estimated code review effort: 2 (Simple) | ~15 minutes

Merge Risk: ⚪ Minimal · up to 4716e

The change skips malformed session records while preserving valid sessions and limits fallback execution to empty native reads; no actionable merge-blocking risk remains after normal checks and review.

Poem

I hop through records, one by one,
I keep the valid sessions in the sun.
Bad entries tumble from my tray,
While who waits for an empty day.
Four platforms bloom, and scans run true.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: skipping corrupt utmpx entries instead of abandoning the entire read.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 33.33333% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.00%. Comparing base (a97bd07) to head (4716eef).

Files with missing lines Patch % Lines
...core-ffm/src/main/java/oshi/driver/mac/WhoFFM.java 25.00% 1 Missing and 2 partials ⚠️
...src/main/java/oshi/driver/unix/freebsd/WhoFFM.java 25.00% 2 Missing and 1 partial ⚠️
oshi-core/src/main/java/oshi/driver/mac/Who.java 25.00% 1 Missing and 2 partials ⚠️
...re/src/main/java/oshi/driver/unix/freebsd/Who.java 25.00% 2 Missing and 1 partial ⚠️
...src/main/java/oshi/driver/unix/solaris/WhoFFM.java 50.00% 1 Missing and 1 partial ⚠️
...re/src/main/java/oshi/driver/unix/solaris/Who.java 50.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3643      +/-   ##
============================================
- Coverage     80.01%   80.00%   -0.02%     
- Complexity     3902     4262     +360     
============================================
  Files           698      698              
  Lines         24348    24354       +6     
  Branches       4027     4033       +6     
============================================
+ Hits          19482    19484       +2     
- Misses         2723     2728       +5     
+ Partials       2143     2142       -1     
Flag Coverage Δ
freebsd 33.66% <8.33%> (-0.01%) ⬇️
linux 37.78% <0.00%> (-0.03%) ⬇️
macos 38.64% <8.33%> (-0.05%) ⬇️
openbsd 32.84% <0.00%> (-0.02%) ⬇️
solaris 34.37% <16.66%> (+0.04%) ⬆️
windows 45.35% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit 327fcf5 into oshi:master Aug 16, 2026
18 checks passed
@dbwiddis
dbwiddis deleted the utmpx-hardening branch August 16, 2026 23:39
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.

1 participant