Skip to content

Add argv[0] fallback for current_exe() on Linux when /proc is unavailable#153603

Open
Ecordonnier wants to merge 1 commit intorust-lang:mainfrom
Ecordonnier:eco/fix-current_exe
Open

Add argv[0] fallback for current_exe() on Linux when /proc is unavailable#153603
Ecordonnier wants to merge 1 commit intorust-lang:mainfrom
Ecordonnier:eco/fix-current_exe

Conversation

@Ecordonnier
Copy link

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.

Fixes #46090

@rustbot rustbot added O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 9, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 9, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates

@rustbot

This comment has been minimized.

@rust-log-analyzer

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>
@Ecordonnier Ecordonnier force-pushed the eco/fix-current_exe branch from 681b21f to 23dfde3 Compare March 9, 2026 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustc "unexpected panic"; no /proc/self/exe inside a debootstrap chroot

4 participants