Mercurial > hg > truffle
diff agent/src/os/linux/libproc_impl.c @ 14909:4ca6dc0799b6
Backout jdk9 merge
author | Gilles Duboscq <duboscq@ssw.jku.at> |
---|---|
date | Tue, 01 Apr 2014 13:57:07 +0200 |
parents | d7cb88bd7046 |
children |
line wrap: on
line diff
--- a/agent/src/os/linux/libproc_impl.c Tue Apr 01 14:09:03 2014 +0200 +++ b/agent/src/os/linux/libproc_impl.c Tue Apr 01 13:57:07 2014 +0200 @@ -29,51 +29,54 @@ #include <thread_db.h> #include "libproc_impl.h" +static const char* alt_root = NULL; +static int alt_root_len = -1; + #define SA_ALTROOT "SA_ALTROOT" +static void init_alt_root() { + if (alt_root_len == -1) { + alt_root = getenv(SA_ALTROOT); + if (alt_root) { + alt_root_len = strlen(alt_root); + } else { + alt_root_len = 0; + } + } +} + int pathmap_open(const char* name) { - static const char *alt_root = NULL; - static int alt_root_initialized = 0; + int fd; + char alt_path[PATH_MAX + 1]; - int fd; - char alt_path[PATH_MAX + 1], *alt_path_end; - const char *s; + init_alt_root(); - if (!alt_root_initialized) { - alt_root_initialized = -1; - alt_root = getenv(SA_ALTROOT); - } - - if (alt_root == NULL) { - return open(name, O_RDONLY); - } - - strcpy(alt_path, alt_root); - alt_path_end = alt_path + strlen(alt_path); + if (alt_root_len > 0) { + strcpy(alt_path, alt_root); + strcat(alt_path, name); + fd = open(alt_path, O_RDONLY); + if (fd >= 0) { + print_debug("path %s substituted for %s\n", alt_path, name); + return fd; + } - // Strip path items one by one and try to open file with alt_root prepended - s = name; - while (1) { - strcat(alt_path, s); - s += 1; - - fd = open(alt_path, O_RDONLY); - if (fd >= 0) { - print_debug("path %s substituted for %s\n", alt_path, name); - return fd; - } + if (strrchr(name, '/')) { + strcpy(alt_path, alt_root); + strcat(alt_path, strrchr(name, '/')); + fd = open(alt_path, O_RDONLY); + if (fd >= 0) { + print_debug("path %s substituted for %s\n", alt_path, name); + return fd; + } + } + } else { + fd = open(name, O_RDONLY); + if (fd >= 0) { + return fd; + } + } - // Linker always put full path to solib to process, so we can rely - // on presence of /. If slash is not present, it means, that SOlib doesn't - // physically exist (e.g. linux-gate.so) and we fail opening it anyway - if ((s = strchr(s, '/')) == NULL) { - break; - } - - *alt_path_end = 0; - } - - return -1; + return -1; } static bool _libsaproc_debug;