static void unblock_sigchld(const sigset_t *);
static void sigchld_handler(int signr UNUSED);
static bool is_member(int x, const int *array, size_t);
-static bool find_in_path(const char *name);
/* Initializes the process subsystem (if it is not already initialized). Calls
* exit() if initialization fails.
struct process **pp)
{
sigset_t oldsigs;
+ char *binary;
pid_t pid;
*pp = NULL;
/* execvp() will search PATH too, but the error in that case is more
* obscure, since it is only reported post-fork. */
- if (!find_in_path(argv[0])) {
+ binary = process_search_path(argv[0]);
+ if (!binary) {
VLOG_ERR("%s not found in PATH", argv[0]);
return ENOENT;
}
+ free(binary);
block_sigchld(&oldsigs);
pid = fork();
poll_fd_wait(fds[0], POLLIN);
}
}
+
+char *
+process_search_path(const char *name)
+{
+ char *save_ptr = NULL;
+ char *path, *dir;
+ struct stat s;
+
+ if (strchr(name, '/') || !getenv("PATH")) {
+ return stat(name, &s) == 0 ? xstrdup(name) : NULL;
+ }
+
+ path = xstrdup(getenv("PATH"));
+ for (dir = strtok_r(path, ":", &save_ptr); dir;
+ dir = strtok_r(NULL, ":", &save_ptr)) {
+ char *file = xasprintf("%s/%s", dir, name);
+ if (stat(file, &s) == 0) {
+ free(path);
+ return file;
+ }
+ free(file);
+ }
+ free(path);
+ return NULL;
+}
\f
static void
sigchld_handler(int signr UNUSED)
ofp_fatal(errno, "sigprocmask");
}
}
-
-static bool
-find_in_path(const char *name)
-{
- char *save_ptr = NULL;
- char *path, *dir;
- struct stat s;
-
- if (strchr(name, '/') || !getenv("PATH")) {
- return stat(name, &s) == 0;
- }
-
- path = xstrdup(getenv("PATH"));
- for (dir = strtok_r(path, ":", &save_ptr); dir;
- dir = strtok_r(NULL, ":", &save_ptr)) {
- char *file = xasprintf("%s/%s", dir, name);
- if (stat(file, &s) == 0) {
- free(file);
- free(path);
- return true;
- }
- free(file);
- }
- free(path);
- return false;
-}