Skip to content

Commit cb4519e

Browse files
committed
os current_exe using same approach as linux to get always the full absolute path
but in case of failure (e.g. prcfs not mounted) still using getexecname.
1 parent b289bb7 commit cb4519e

File tree

1 file changed

+17
-13
lines changed
  • library/std/src/sys/unix

1 file changed

+17
-13
lines changed

library/std/src/sys/unix/os.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -368,20 +368,24 @@ pub fn current_exe() -> io::Result<PathBuf> {
368368

369369
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
370370
pub fn current_exe() -> io::Result<PathBuf> {
371-
extern "C" {
372-
fn getexecname() -> *const c_char;
373-
}
374-
unsafe {
375-
let path = getexecname();
376-
if path.is_null() {
377-
Err(io::Error::last_os_error())
378-
} else {
379-
let filename = CStr::from_ptr(path).to_bytes();
380-
let path = PathBuf::from(<OsStr as OsStrExt>::from_bytes(filename));
371+
if let Ok(path) = crate::fs::read_link("/proc/self/path/a.out") {
372+
Ok(path)
373+
} else {
374+
extern "C" {
375+
fn getexecname() -> *const c_char;
376+
}
377+
unsafe {
378+
let path = getexecname();
379+
if path.is_null() {
380+
Err(io::Error::last_os_error())
381+
} else {
382+
let filename = CStr::from_ptr(path).to_bytes();
383+
let path = PathBuf::from(<OsStr as OsStrExt>::from_bytes(filename));
381384

382-
// Prepend a current working directory to the path if
383-
// it doesn't contain an absolute pathname.
384-
if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) }
385+
// Prepend a current working directory to the path if
386+
// it doesn't contain an absolute pathname.
387+
if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) }
388+
}
385389
}
386390
}
387391
}

0 commit comments

Comments
 (0)