Repository navigation
Read the process executable path from the kernel on FreeBSD and DragonFly - #3810
Conversation
…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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
) * 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.
OSProcess.getPath()on FreeBSD and DragonFly BSD returned thepscommcolumn, which is only the command name (init,java). It now returns the executable's full path from theKERN_PROC_PATHNAMEsysctl.BsdOSProcessgains aqueryExecutablePath()hook (empty by default) thatqueryPath()prefers over thepscolumn. OpenBSD (no equivalent sysctl) and NetBSD (ownargv[0]handling) are unchanged.<sys/sysctl.h>on the platforms; the constants live on the shared bases.dhclient.comm's 19 characters. A script reports its interpreter (sh,/bin/sh);commalready did.Verification
getProcesses()~115-135 ms.dhclientand exiting processes lack a full path.BsdOSProcessTestcases for the kernel path and the fallback. Full gate with-Paggregate-coveragegreen on macOS.Summary by CodeRabbit