aboutsummaryrefslogtreecommitdiff
path: root/src/proc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/proc.c')
-rw-r--r--src/proc.c105
1 files changed, 87 insertions, 18 deletions
diff --git a/src/proc.c b/src/proc.c
index 9e57aa5..718e140 100644
--- a/src/proc.c
+++ b/src/proc.c
@@ -3,9 +3,13 @@
#include <assert.h>
#include <fcntl.h>
+#include <paths.h>
#include <spawn.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include <sys/errno.h>
+#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -13,41 +17,103 @@
extern char **environ;
+static const char *get_path(void) {
+ const char *path = getenv("PATH");
+ if (path == NULL || path[0] == '\0') {
+ // In case `PATH` is not specified, use the default search path.
+ return _PATH_DEFPATH;
+ }
+ return path;
+}
+
+static const char *next_path_segment(char **path) {
+ char *segment = *path;
+ if (segment != NULL) {
+ *path = strchr(segment, ':');
+ if (*path != NULL) {
+ *((*path)++) = '\0';
+ }
+
+ if (segment[0] == '\0') {
+ // POSIX defines an empty PATH segment to mean "current directory"
+ segment = ".";
+ }
+ }
+
+ return segment;
+}
+
+bool dbx_proc_find(const char *name, char file[PATH_MAX]) {
+ assert(name != NULL && name[0] != '\0' && strchr(name, '/') == NULL &&
+ file != NULL);
+
+ char *path = strdup(get_path());
+ if (path == NULL) {
+ dbx_perror("strdup", ENOMEM);
+ return false;
+ }
+
+ char *pos = path;
+ bool found = false;
+ for (const char *dir = next_path_segment(&pos); dir != NULL;
+ dir = next_path_segment(&pos)) {
+ char candidate[PATH_MAX];
+ int n = snprintf(candidate, sizeof(candidate), "%s/%s", dir, name);
+ if (n < 0 || n >= (int)sizeof(candidate)) {
+ continue;
+ }
+
+ struct stat st;
+ if (realpath(candidate, file) != NULL && access(file, X_OK) == 0 &&
+ stat(file, &st) == 0 && S_ISREG(st.st_mode)) {
+ found = true;
+ break;
+ }
+ }
+
+ free(path);
+ return found;
+}
+
int dbx_proc_run(char *const command[]) {
assert(command != NULL && command[0] != NULL);
+ int result;
posix_spawn_file_actions_t file_actions;
- if (posix_spawn_file_actions_init(&file_actions) != 0) {
- perror("spawn_file_actions_init");
+ if ((result = posix_spawn_file_actions_init(&file_actions)) != 0) {
+ dbx_perror("spawn_file_actions_init", result);
return -1;
}
int exit_code = -1;
- if (posix_spawn_file_actions_addclose(&file_actions, STDIN_FILENO) != 0) {
- perror("posix_spawn_file_actions_addclose(stdin)");
+ result = posix_spawn_file_actions_addclose(&file_actions, STDIN_FILENO);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_addclose(stdin)", result);
goto cleanup;
}
- if (posix_spawn_file_actions_addopen(&file_actions, STDOUT_FILENO,
- "/dev/null", O_WRONLY, 0) != 0) {
- perror("posix_spawn_file_actions_addopen(stdout)");
+ result = posix_spawn_file_actions_addopen(&file_actions, STDOUT_FILENO,
+ _PATH_DEVNULL, O_WRONLY, 0);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_addopen(stdout)", result);
goto cleanup;
}
- if (posix_spawn_file_actions_addopen(&file_actions, STDERR_FILENO,
- "/dev/null", O_WRONLY, 0) != 0) {
- perror("posix_spawn_file_actions_addopen(stderr)");
+ result = posix_spawn_file_actions_addopen(&file_actions, STDERR_FILENO,
+ _PATH_DEVNULL, O_WRONLY, 0);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_addopen(stderr)", result);
goto cleanup;
}
pid_t pid;
- if (posix_spawnp(&pid, command[0], &file_actions, NULL, command, environ) !=
- 0) {
- perror("posix_spawnp");
+ result = posix_spawn(&pid, command[0], &file_actions, NULL, command, environ);
+ if (result != 0) {
+ dbx_perror("posix_spawn", result);
goto cleanup;
}
int status;
if (waitpid(pid, &status, 0) != pid) {
- perror("waitpid");
+ dbx_perror("waitpid", errno);
goto cleanup;
}
@@ -58,17 +124,20 @@ int dbx_proc_run(char *const command[]) {
}
cleanup:
- if (posix_spawn_file_actions_destroy(&file_actions) != 0) {
- perror("posix_spawn_file_actions_destroy");
+ result = posix_spawn_file_actions_destroy(&file_actions);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_destroy", result);
+ // Don't change the exit code on cleanup failure: issues cleaning up do not
+ // change the status of the child process's execution.
}
return exit_code;
}
bool dbx_proc_exec(char *const command[]) {
assert(command != NULL && command[0] != NULL);
- execvp(command[0], command);
+ execv(command[0], command);
// execvp only ever returns if there was an error.
- perror("execvp");
+ dbx_perror("execv", errno);
return false;
}