From 1b6a182cf1e400b63ae8692e3183beabe541ea6c Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 17 May 2017 15:14:30 +0200 Subject: [PATCH] Improve the error management when /proc is not mounted This PR does two things: * Triggers an error on GNU/Linux & Android when /proc/self/exe doesn't exist * Handle the error properly --- src/librustc/session/filesearch.rs | 11 ++++++++--- src/libstd/sys/unix/os.rs | 7 ++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/librustc/session/filesearch.rs b/src/librustc/session/filesearch.rs index 82c2425aead..47b988a21b4 100644 --- a/src/librustc/session/filesearch.rs +++ b/src/librustc/session/filesearch.rs @@ -159,9 +159,14 @@ pub fn get_or_default_sysroot() -> PathBuf { }) } - match canonicalize(env::current_exe().ok()) { - Some(mut p) => { p.pop(); p.pop(); p } - None => bug!("can't determine value for sysroot") + match env::current_exe() { + Ok(exe) => { + match canonicalize(Some(exe)) { + Some(mut p) => { p.pop(); p.pop(); return p; }, + None => bug!("can't determine value for sysroot") + } + } + Err(ref e) => panic!(format!("failed to get current_exe: {}", e)) } } diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index 36928696c40..8e41fd009be 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -253,7 +253,12 @@ pub fn current_exe() -> io::Result { #[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))] pub fn current_exe() -> io::Result { - ::fs::read_link("/proc/self/exe") + let selfexe = PathBuf::from("/proc/self/exe"); + if selfexe.exists() { + ::fs::read_link(selfexe) + } else { + Err(io::Error::new(io::ErrorKind::Other, "no /proc/self/exe available. Is /proc mounted?")) + } } #[cfg(any(target_os = "macos", target_os = "ios"))]