execve(): find executable first in COKERNEL_PATH and then in PATH if executable name is not absolute

This commit is contained in:
bgerofi@riken.jp
2014-07-28 22:09:54 +09:00
committed by Balazs Gerofi bgerofi@riken.jp
parent e5d3407d8e
commit f1e39f5fb2

View File

@@ -1221,8 +1221,8 @@ int main_loop(int fd, int cpu, pthread_mutex_t *lock)
int ret = -1; int ret = -1;
struct program_load_desc *desc; struct program_load_desc *desc;
struct remote_transfer trans; struct remote_transfer trans;
FILE *fp; int error;
int status; int found = 0;
char path[2048]; char path[2048];
char *filename; char *filename;
@@ -1232,37 +1232,59 @@ int main_loop(int fd, int cpu, pthread_mutex_t *lock)
filename = (char *)w.sr.args[1]; filename = (char *)w.sr.args[1];
/* Is filename without path? */ /* Is filename without path? */
if (0 && strncmp(filename, "/", 1) if (strncmp(filename, "/", 1)
//&& strncmp(filename, ".", 1) && strncmp(filename, ".", 1)) {
) {
char *token, *string, *tofree;
char *PATH = getenv("COKERNEL_PATH");
if (!PATH) {
PATH = getenv("PATH");
}
__dprintf("PATH: %s\n", PATH);
char *PATH = getenv("PATH"); /* strsep() modifies string! */
fprintf(stderr, "PATH: %s\n", PATH); tofree = string = strdup(PATH);
if (string == NULL) {
/* Open command for reading. */ printf("error: copying PATH, not enough memory?\n");
sprintf(path, "/usr/bin/which %s", filename);
fp = popen(path, "r");
if (fp == NULL) {
fprintf(stderr, "execve(): failed to run which\n" );
goto return_execve1; goto return_execve1;
} }
/* Read the output a line at a time - output it. */ while ((token = strsep(&string, ":")) != NULL) {
if (fgets(path, sizeof(path)-1, fp) == NULL) {
fprintf(stderr, "execve(): failed to read which\n" ); error = snprintf(path, sizeof(path),
pclose(fp); "%s/%s", token, filename);
goto return_execve1; if (error < 0 || error >= sizeof(path)) {
fprintf(stderr, "execve(): array too small?\n");
continue;
}
error = access(path, X_OK);
if (!error) {
found = 1;
break;
}
} }
/* close */ free(tofree);
pclose(fp);
} }
else { else {
sprintf(path, "%s", filename); error = snprintf(path, sizeof(path), "%s", filename);
if (error < 0 || error >= sizeof(path)) {
fprintf(stderr, "execve(): array too small?\n");
goto return_execve1;
}
found = 1;
}
if (!found) {
fprintf(stderr,
"execve(): error finding file %s\n", path);
goto return_execve1;
} }
__dprintf("execve: filename: %s\n", filename); __dprintf("execve(): path to binary: %s\n", path);
__dprintf("execve: LD_LIBRARY_PATH: %s\n", getenv("LD_LIBRARY_PATH") ? getenv("LD_LIBRARY_PATH") : "(empty)");
if (load_elf_desc(path, &desc) != 0) { if (load_elf_desc(path, &desc) != 0) {
fprintf(stderr, fprintf(stderr,