diff options
| author | David Carlier <devnexen@gmail.com> | 2021-07-30 17:28:44 +0100 |
|---|---|---|
| committer | David Carlier <devnexen@gmail.com> | 2021-08-02 09:13:30 +0100 |
| commit | cb4519e59c75b848e808a28f2046c8bc2f91b943 (patch) | |
| tree | 3b2e94ea3895028a445c70f9ee8f72cc6a4b1cce | |
| parent | b289bb7fdfcb6f54d825927ab9b5722cabc2a140 (diff) | |
| download | rust-cb4519e59c75b848e808a28f2046c8bc2f91b943.tar.gz rust-cb4519e59c75b848e808a28f2046c8bc2f91b943.zip | |
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.
| -rw-r--r-- | library/std/src/sys/unix/os.rs | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs index cc0802ed709..14f0747bedc 100644 --- a/library/std/src/sys/unix/os.rs +++ b/library/std/src/sys/unix/os.rs @@ -368,20 +368,24 @@ pub fn current_exe() -> io::Result<PathBuf> { #[cfg(any(target_os = "solaris", target_os = "illumos"))] pub fn current_exe() -> io::Result<PathBuf> { - extern "C" { - fn getexecname() -> *const c_char; - } - unsafe { - let path = getexecname(); - if path.is_null() { - Err(io::Error::last_os_error()) - } else { - let filename = CStr::from_ptr(path).to_bytes(); - let path = PathBuf::from(<OsStr as OsStrExt>::from_bytes(filename)); + if let Ok(path) = crate::fs::read_link("/proc/self/path/a.out") { + Ok(path) + } else { + extern "C" { + fn getexecname() -> *const c_char; + } + unsafe { + let path = getexecname(); + if path.is_null() { + Err(io::Error::last_os_error()) + } else { + let filename = CStr::from_ptr(path).to_bytes(); + let path = PathBuf::from(<OsStr as OsStrExt>::from_bytes(filename)); - // Prepend a current working directory to the path if - // it doesn't contain an absolute pathname. - if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) } + // Prepend a current working directory to the path if + // it doesn't contain an absolute pathname. + if filename[0] == b'/' { Ok(path) } else { getcwd().map(|cwd| cwd.join(path)) } + } } } } |
