execve(): find executable first in COKERNEL_PATH and then in PATH if executable name is not absolute
This commit is contained in:
committed by
Balazs Gerofi bgerofi@riken.jp
parent
e5d3407d8e
commit
f1e39f5fb2
@@ -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 *PATH = getenv("PATH");
|
char *token, *string, *tofree;
|
||||||
fprintf(stderr, "PATH: %s\n", PATH);
|
char *PATH = getenv("COKERNEL_PATH");
|
||||||
|
if (!PATH) {
|
||||||
|
PATH = getenv("PATH");
|
||||||
|
}
|
||||||
|
|
||||||
/* Open command for reading. */
|
__dprintf("PATH: %s\n", PATH);
|
||||||
sprintf(path, "/usr/bin/which %s", filename);
|
|
||||||
fp = popen(path, "r");
|
/* strsep() modifies string! */
|
||||||
if (fp == NULL) {
|
tofree = string = strdup(PATH);
|
||||||
fprintf(stderr, "execve(): failed to run which\n" );
|
if (string == NULL) {
|
||||||
|
printf("error: copying PATH, not enough memory?\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;
|
||||||
}
|
}
|
||||||
|
|
||||||
__dprintf("execve: filename: %s\n", filename);
|
if (!found) {
|
||||||
__dprintf("execve: LD_LIBRARY_PATH: %s\n", getenv("LD_LIBRARY_PATH") ? getenv("LD_LIBRARY_PATH") : "(empty)");
|
fprintf(stderr,
|
||||||
|
"execve(): error finding file %s\n", path);
|
||||||
|
goto return_execve1;
|
||||||
|
}
|
||||||
|
|
||||||
|
__dprintf("execve(): path to binary: %s\n", path);
|
||||||
|
|
||||||
if (load_elf_desc(path, &desc) != 0) {
|
if (load_elf_desc(path, &desc) != 0) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
|
|||||||
Reference in New Issue
Block a user