#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <limits.h> // PATH_MAX
#ifndef EM_X86_64
#define EM_X86_64 62 // elf.h doesn't define this on some older systems
const char *default_platform;
const char *cp;
char *toolfile;
- char launcher_name[PATH_MAX+1];
+ const char *launcher_name;
char* new_line;
char** new_env;
/* Figure out the name of this executable (viz, the launcher), so
we can tell stage2. stage2 will use the name for recursive
invocations of valgrind on child processes. */
- memset(launcher_name, 0, PATH_MAX+1);
- r = readlink("/proc/self/exe", launcher_name, PATH_MAX);
- if (r == -1) {
- /* If /proc/self/exe can't be followed, don't give up. Instead
- continue with an empty string for VALGRIND_LAUNCHER. In the
- sys_execve wrapper, this is tested, and if found to be empty,
- fail the execve. */
- fprintf(stderr, "valgrind: warning (non-fatal): "
- "readlink(\"/proc/self/exe\") failed.\n");
- fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
- "will not work.\n");
+ unsigned bufsiz = 0;
+ char *buf = NULL;
+
+ while (42) {
+ bufsiz += 500;
+ buf = realloc(buf, bufsiz);
+ if (buf == NULL)
+ barf("realloc of buf failed.");
+ r = readlink("/proc/self/exe", buf, bufsiz);
+ if (r == -1) {
+ /* If /proc/self/exe can't be followed, don't give up. Instead
+ continue with an empty string for VALGRIND_LAUNCHER. In the
+ sys_execve wrapper, this is tested, and if found to be empty,
+ fail the execve. */
+ fprintf(stderr, "valgrind: warning (non-fatal): "
+ "readlink(\"/proc/self/exe\") failed.\n");
+ fprintf(stderr, "valgrind: continuing, however --trace-children=yes "
+ "will not work.\n");
+ launcher_name = "";
+ break;
+ }
+ if (r == bufsiz) continue; // buffer to small; retry
+
+ assert(r < bufsiz); // paranoia
+
+ buf[r] = '\0';
+ launcher_name = buf;
+ break;
}
/* tediously augment the env: VALGRIND_LAUNCHER=launcher_name */