Commit 23dfde3
committed
Add argv[0] fallback for current_exe() on Linux when /proc is unavailable
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>1 parent 98e7077 commit 23dfde3
1 file changed
+56
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
264 | 264 | | |
265 | 265 | | |
266 | 266 | | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
271 | 323 | | |
272 | 324 | | |
273 | 325 | | |
| |||
0 commit comments