Repository navigation
Use sysctl KERN_PROC_PATHNAME for the executable path on the BSDs - #231
Open
hjmjohnson wants to merge 1 commit into
Open
hjmjohnson wants to merge 1 commit into
hjmjohnson wants to merge 1 commit into
Conversation
hjmjohnson
marked this pull request as ready for review
August 22, 2026 13:20
GetCurrentProcessFileName() read /proc/curproc/file on FreeBSD, DragonFly and OpenBSD. procfs is not mounted by default on modern FreeBSD, and OpenBSD has no equivalent at all, so the readlink fails and the function returns nullptr on a default install. Ask the kernel instead. FreeBSD and DragonFly expose the path as CTL_KERN.KERN_PROC.KERN_PROC_PATHNAME; NetBSD spells the same thing CTL_KERN.KERN_PROC_ARGS.KERN_PROC_PATHNAME. The procfs readlink is kept as a fallback for systems that do mount it, so nothing that works today regresses. The block is guarded on KERN_PROC_PATHNAME rather than on a platform name. It is a real macro in <sys/sysctl.h>, so the preprocessor can test for it directly. OpenBSD, which defines neither the macro nor an equivalent, keeps the fallback and is no worse off than before. Also terminate the readlink results. readlink does not NUL-terminate, and returns the buffer size when it truncates. The buffers here are static and therefore zero-initialized, so in practice the strings are terminated and this is hardening rather than a live bug, but the truncation case would leave the buffer unterminated, and passing sizeof(path) - 1 with an explicit terminator removes the question. The commented-out NetBSD block is dropped now that NetBSD is handled. Verified: the BSD block compiles clean at -Wall -Wextra -Werror against the FreeBSD 15.1, NetBSD 11.0, and OpenBSD 7.9 headers (clang 21 cross), and the whole of gdcmSystem.cxx compiles against FreeBSD's headers and libc++. FreeBSD selects the sysctl path and OpenBSD the fallback. It has not been executed on a BSD. glibc, musl, i386, and macOS builds show the same test failures as master. Assisted-by: Claude Code: cross-platform verification and patch drafting
hjmjohnson
force-pushed
the
fix/bsd-exe-path
branch
from
October 26, 2026 17:02
0889e6c to
28fcdbf
Compare
This was referenced Oct 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GetCurrentProcessFileName()reads/proc/curproc/fileon FreeBSD, DragonFly, and OpenBSD. procfs is not mounted by default on modern FreeBSD, and OpenBSD has no procfs at all, so on a default install the readlink fails and the function returnsnullptr. This asks the kernel throughsysctlinstead, keeping the procfs read as a fallback.The BSD branch has been compiled against each BSD's real headers but not executed on a BSD host. A BSD reviewer confirming the returned path would close that gap.
The change
FreeBSD and DragonFly expose the executable path as
CTL_KERN.KERN_PROC.KERN_PROC_PATHNAME; NetBSD spells itCTL_KERN.KERN_PROC_ARGS.KERN_PROC_PATHNAME:readlinkstays as a fallback, so a system that mounts procfs behaves as it does today.KERN_PROC_PATHNAMEitself, a macro in<sys/sysctl.h>. The outer platform list remains, because<sys/sysctl.h>is only included on the BSDs.KERN_PROC_PATHNAME(confirmed in its 7.9 headers), so it keeps the fallback and is no worse off than before.The commented-out NetBSD block is dropped now that NetBSD is handled.
readlink termination: hardening, not a live bug
readlinkdoes not NUL-terminate and returns the buffer size when it truncates. Both call sites now passsizeof(path) - 1and terminate explicitly. The buffers arestatic, hence zero-initialized, so today this only matters for a link target at or beyondPATH_MAX.Verification
gdcmSystem.cxxcompiles with 0 warnings at-Wall -Wextra; thesysctlpath is selected (KERN_PROC_PATHNAME= 12)-Wall -Wextra -Werror;KERN_PROC_ARGS= 48,KERN_PROC_PATHNAME= 5-Werror; noKERN_PROC_PATHNAME, fallback onlyThe NetBSD and OpenBSD sets carry no C++ standard library headers, so for those two the BSD block was extracted verbatim from
gdcmSystem.cxxinto a translation unit that needs only C headers.Independent of #228 and #230. Touches the same file as #229 but a different function; the two merge cleanly.