Add argv[0] fallback for current_exe() on Linux when /proc is unavailable#153603
Add argv[0] fallback for current_exe() on Linux when /proc is unavailable#153603Ecordonnier wants to merge 1 commit intorust-lang:mainfrom
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
3f3743c to
681b21f
Compare
This comment has been minimized.
This comment has been minimized.
…able When /proc/self/exe is not accessible (e.g., in containers with masked /proc, chroot environments, or systemd services with ProtectProc=invisible), current_exe() will now fall back to parsing argv[0] and searching PATH. This brings the Linux implementation in line with existing Rust stdlib patterns on Fuchsia, Solaris/illumos, and AIX, which also use argv[0] when direct kernel APIs are unavailable. Fallback strategy: 1. Try /proc/self/exe (fast path, existing behavior) 2. On NotFound error, retrieve argv[0] from env::args() 3. If argv[0] is absolute, use it directly 4. If argv[0] contains '/', resolve it as relative path against getcwd() 5. Otherwise, search PATH for the executable (checking execute permissions) 6. Return NotFound error if executable not found in PATH This handles all common invocation patterns: - Absolute path: `/usr/bin/program` - Relative path: `./program` or `../bin/program` - PATH lookup: `program` (searches directories in PATH) This maintains backward compatibility: behavior is unchanged when /proc is accessible. Signed-off-by: Etienne Cordonnier <ecordonnier@snap.com>
681b21f to
23dfde3
Compare
|
Nominating for libs-api to discuss whether we want this or not. #152269 has some good discussion that is worth reading for that discussion, in particular #152269 (comment). |
Please also consider #46090 in the discussion. The comments in #152269 support the opinion that the corner-case of |
|
☔ The latest upstream changes (presumably #154255) made this pull request unmergeable. Please resolve the merge conflicts. |
When /proc/self/exe is not accessible (e.g., in containers with masked /proc, chroot environments, or systemd services with ProtectProc=invisible), current_exe() will now fall back to parsing argv[0] and searching PATH.
This brings the Linux implementation in line with existing Rust stdlib patterns on Fuchsia, Solaris/illumos, and AIX, which also use argv[0] when direct kernel APIs are unavailable.
Fallback strategy:
This handles all common invocation patterns:
/usr/bin/program./programor../bin/programprogram(searches directories in PATH)This maintains backward compatibility: behavior is unchanged when /proc is accessible.
Fixes #46090