Skip to content

Read the process executable path from the kernel on FreeBSD and DragonFly - #3810

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:bsd-process-path
Oct 15, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:bsd-process-path

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

OSProcess.getPath() on FreeBSD and DragonFly BSD returned the ps comm column, which is only the command name (init, java). It now returns the executable's full path from the KERN_PROC_PATHNAME sysctl.

  • BsdOSProcess gains a queryExecutablePath() hook (empty by default) that queryPath() prefers over the ps column. OpenBSD (no equivalent sysctl) and NetBSD (own argv[0] handling) are unchanged.
  • FreeBSD and DragonFly JNA and FFM implement it. The subtype is 12 on FreeBSD and 9 on DragonFly, both checked against <sys/sysctl.h> on the platforms; the constants live on the shared bases.
  • Fallback to the command name, as before, for processes with no executable: kernel processes, a FreeBSD process whose binary was deleted (DragonFly returns the stale path), and DragonFly's privilege-separated dhclient.
  • The name is still the path's last element, so it is no longer truncated at comm's 19 characters. A script reports its interpreter (sh, /bin/sh); comm already did.
  • One extra sysctl per process during enumeration; no process spawn.

Verification

  • FreeBSD 16-CURRENT (cfarm430): 69 processes, JNA and FFM identical; only PIDs 0-16 and 458 (kernel) and processes that exited mid-listing lack a full path. getProcesses() ~115-135 ms.
  • DragonFly 6.4 (cfarm152): 46 processes, JNA and FFM identical; only dhclient and exiting processes lack a full path.
  • New BsdOSProcessTest cases for the kernel path and the fallback. Full gate with -Paggregate-coverage green on macOS.

Summary by CodeRabbit

  • New Features
    • On FreeBSD and DragonFly BSD, process paths now report the full executable path when available, with the command name used as a fallback.

…nFly

OSProcess.getPath() on FreeBSD and DragonFly BSD returned the ps comm
column, which holds only the command name. BsdOSProcess gains a
queryExecutablePath() hook, empty by default, that queryPath() prefers
over the ps column; the FreeBSD and DragonFly JNA and FFM processes
implement it with the KERN_PROC_PATHNAME sysctl (subtype 12 on FreeBSD,
9 on DragonFly, both checked against the platform headers).

Processes with no executable fall back to the command name as before:
kernel processes, a FreeBSD process whose binary was deleted, and on
DragonFly the privilege-separated dhclient. The name is still the path's
last element, so it is no longer cut at comm's 19 characters; a script
reports its interpreter for both, as comm already did.

OpenBSD has no equivalent sysctl, and NetBSD keeps its argv[0] handling.

Verified on FreeBSD 16 (cfarm430) and DragonFly 6.4 (cfarm152): every
process identical between JNA and FFM, only kernel and exiting processes
without a full path.
@coderabbitai

coderabbitai Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 067c413c-b943-4ca9-b4c5-043a64eb2b8d
📥 Commits

Reviewing files that changed from the base of the PR and between 649ef8f and 9eee47e.

📒 Files selected for processing: 9
  • CHANGELOG.md
  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java
  • oshi-common/src/test/java/oshi/software/common/os/unix/bsd/BsdOSProcessTest.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.java
  • oshi-core/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessJNA.java
  • oshi-core/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessJNA.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

⏰ Context from checks skipped due to timeout: 10
  • GitHub Check: Test JDK 25, FreeBSD
  • GitHub Check: Test JDK 25, OpenIndiana
  • GitHub Check: Test JDK 25, NetBSD
  • GitHub Check: Test JDK 25, OpenBSD
  • GitHub Check: Error Prone
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Analyze (java)
🧰 Additional context used
📚 Code guidelines: 2
AGENTS.md — auto-discovered
CONTRIBUTING.md — auto-discovered
📓 Path-based instructions: 3
Source excerpt: **Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`): `oshi-core-ffm` may not reference `com.sun.jna.**`.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/unix/dragonflybsd/DragonFlyBsdOSProcessFFM.java
Source excerpt: **Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`): `oshi-common` may not reference `com.sun.jna.**`, `java.lang.foreign.**`, or any `System.load`/`loadLibrary`.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/dragonflybsd/DragonFlyBsdOSProcess.java
  • oshi-common/src/test/java/oshi/software/common/os/unix/bsd/BsdOSProcessTest.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/freebsd/FreeBsdOSProcess.java
Source excerpt: The Changelog lets users know what's changed.

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Files:

  • CHANGELOG.md
📝 Walkthrough

FreeBSD and DragonFly BSD process path queries now return the kernel-reported executable path when available. If no path is available, they use the process command name.

Changes: BSD executable path lookup

Layer / File(s) Summary
Common path behavior and tests
oshi-common/src/main/java/oshi/software/common/os/unix/bsd/BsdOSProcess.java, oshi-common/src/test/java/oshi/software/common/os/unix/bsd/BsdOSProcessTest.java, CHANGELOG.md
BsdOSProcess prefers a nonempty executable path and otherwise falls back to the ucomm or comm column. Tests cover both outcomes.
FreeBSD and DragonFly BSD sysctl queries
oshi-common/src/main/java/oshi/software/common/os/unix/{freebsd,dragonflybsd}/*, oshi-core/src/main/java/oshi/software/os/unix/{freebsd,dragonflybsd}/*, oshi-core-ffm/src/main/java/oshi/software/os/unix/{freebsd,dragonflybsd}/*
FreeBSD and DragonFly BSD add queryExecutablePath() implementations for FFM and JNA. Each implementation queries kern.proc.pathname and returns an empty string when the query fails.

Priority: ⬇️ Low

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

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 9eee4

No actionable issue has been established for the new executable-path lookup; the change is mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 21 functions across 8 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 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: reading process executable paths from the kernel on FreeBSD and DragonFly.
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.
Full details: Docstring Coverage

Explanation

Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 21 functions across 8 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks the kernel trail,
For paths that hide beyond the name.
If none comes back, the command will do,
FreeBSD and DragonFly join the game.
Then off I hop, with paths in view!

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

@dbwiddis
dbwiddis merged commit 5801857 into oshi:master Oct 15, 2026
17 of 18 checks passed
@dbwiddis
dbwiddis deleted the bsd-process-path branch October 15, 2026 02:07
dbwiddis added a commit that referenced this pull request Oct 28, 2026
)

* Query only the current process's executable path on DragonFly BSD

DragonFly's KERN_PROC_PATHNAME handler checks only the target's
p_textnch before calling cache_fullpath(p, ...), which dereferences
p->p_fd unchecked. exit1() frees p_fd well before it drops p_textnch,
releasing p_token in between, so querying a process that is exiting
panics the kernel. #3810 made that query for every ps row, in a
parallel stream; on the shared cfarm152 it took the box down on
2026-10-15 and 2026-10-27, both times during the oshi-core tests.

DragonFlyBsdOSProcess now gates the query in oshi-common: a final
queryExecutablePath() asks the kernel only when the process is the
current one, which cannot be exiting, and the JNA and FFM twins supply
queryOwnExecutablePath(). Other processes fall back to ps's ucomm
column, as before #3810. FreeBSD's handler takes the proc lock and
reads p_textvp, and is unaffected.

* Keep DragonFly process names past ucomm's 16 characters

Other processes fell back to ucomm, which the kernel cuts to MAXCOMLEN,
so getName() truncated long names and getServices() could report a
running rc.d service as stopped. DragonFlyBsdOSProcess now overrides
queryPath() to take argv[0] from the command column ps already
returns, which needs no kernel query. A process can rewrite argv[0], so
it is used only when its last element equals ucomm, or extends a ucomm
of exactly MAXCOMLEN characters; otherwise the result is ucomm.

parseArgv0() moves from NetBsdOSProcess to BsdOSProcess so both use it,
and its test moves with it. OSProcess.getPath() documents the
DragonFly behavior.

* Read DragonFly argv[0] from procfs and reject process titles

ps's command column separates arguments with unescaped spaces, so a
path such as "/opt/java tools/java" split at the space and still
matched ucomm. argv[0] now comes from the NUL-separated
/proc/<pid>/cmdline through queryArguments(), which procfs serves from
data the kernel already holds; without procfs the result is ucomm.

A setproctitle() title such as "postgres_exporter: running" also
extends a truncated ucomm, so namesProgram() rejects a colon or
whitespace after the prefix.

parseArgv0() and its test return to NetBSD, its only user.
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