Skip to content

Commit 12c44e5

Browse files
committedSep 26, 2012
The assumption that /proc/self/exe always exists is incorrect.
For example, under a Linux chroot, /proc/ might not be mounted. Therefor, we test if this file exist. If it is the case, use it (the current behavior). Otherwise, we fall back to the detection used by *BSD. The issue has been reported initially on the Debian bug tracker: http://bugs.debian.org/674588 llvm-svn: 164676
1 parent 2b425e1 commit 12c44e5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed
 

‎llvm/lib/Support/Unix/Path.inc

+13-4
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ Path::GetCurrentDirectory() {
261261
}
262262

263263
#if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
264-
defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__)
264+
defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \
265+
defined(__linux__) || defined(__CYGWIN__)
265266
static int
266267
test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
267268
const char *dir, const char *bin)
@@ -337,9 +338,17 @@ Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
337338
return Path(exe_path);
338339
#elif defined(__linux__) || defined(__CYGWIN__)
339340
char exe_path[MAXPATHLEN];
340-
ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
341-
if (len >= 0)
342-
return Path(StringRef(exe_path, len));
341+
StringRef aPath("/proc/self/exe");
342+
if (sys::fs::exists(aPath)) {
343+
// /proc is not always mounted under Linux (chroot for example).
344+
ssize_t len = readlink(aPath.str().c_str(), exe_path, sizeof(exe_path));
345+
if (len >= 0)
346+
return Path(StringRef(exe_path, len));
347+
} else {
348+
// Fall back to the classical detection.
349+
if (getprogpath(exe_path, argv0) != NULL)
350+
return Path(exe_path);
351+
}
343352
#elif defined(HAVE_DLFCN_H)
344353
// Use dladdr to get executable path if available.
345354
Dl_info DLInfo;

0 commit comments

Comments
 (0)
Please sign in to comment.