diff options
| -rw-r--r-- | src/dbx.h | 6 | ||||
| -rw-r--r-- | src/main.c | 37 | ||||
| -rw-r--r-- | src/proc.c | 105 |
3 files changed, 117 insertions, 31 deletions
@@ -4,7 +4,9 @@ #ifndef _DBX_H_ #define _DBX_H_ +#include <limits.h> #include <stdbool.h> +#include <stddef.h> #include <stdint.h> struct dbx_options { @@ -19,6 +21,10 @@ struct dbx_options { #define COUNTOF(x) ((sizeof(x)) / (sizeof(*x))) +void dbx_printerr(const char *format, ...); +void dbx_perror(const char *s, int e); + +bool dbx_proc_find(const char *name, char path[PATH_MAX]); int dbx_proc_run(char *const command[]); bool dbx_proc_exec(char *const command[]); @@ -36,16 +36,21 @@ static void usage(FILE *output) { progname); } -static int usage_error(const char *format, ...) { +void dbx_printerr(const char *format, ...) { va_list ap; va_start(ap, format); fprintf(stderr, "%s: ", progname); vfprintf(stderr, format, ap); fprintf(stderr, "\n"); va_end(ap); - return EX_USAGE; } +void dbx_perror(const char *s, int e) { + dbx_printerr("%s: %s", s, strerror(e)); +} + +#define USAGE_ERROR(...) (dbx_printerr(__VA_ARGS__), EX_USAGE) + static bool parse_port(const char *str, uint16_t *port) { char *end = NULL; errno = 0; @@ -98,10 +103,16 @@ int dummy_run(struct dbx_options *options) { printf("- image: %s\n", options->image); printf("- ports: %d:%d\n", options->ports.container, options->ports.host); - char *const ls[] = {"ls", "-la", ".", NULL}; - int exit_code = dbx_proc_run(ls); - printf("- ls: %d\n", exit_code); - dbx_proc_exec(ls); + char container[PATH_MAX]; + if (!dbx_proc_find("container", container)) { + fprintf(stderr, "dbx: can't find `container`.\n"); + return EXIT_FAILURE; + } + + char *const cmd[] = {container, "--version", NULL}; + int exit_code = dbx_proc_run(cmd); + printf("- cmd: %s:%d\n", container, exit_code); + dbx_proc_exec(cmd); return EXIT_SUCCESS; } @@ -176,7 +187,7 @@ int main(int argc, char **argv) { } } if (command == NULL) { - return usage_error("unknown command '%s'", options.command); + return USAGE_ERROR("unknown command '%s'", options.command); } char optstr[32] = ":hn:"; @@ -199,14 +210,14 @@ int main(int argc, char **argv) { break; case 'p': if (!parse_port(optarg, &options.ports.host)) { - return usage_error("invalid port '%s'", optarg); + return USAGE_ERROR("invalid port '%s'", optarg); } break; case ':': - return usage_error("missing value for -%c", optopt); + return USAGE_ERROR("missing value for -%c", optopt); case '?': default: - return usage_error("unknown option -%c", optopt); + return USAGE_ERROR("unknown option -%c", optopt); } } @@ -216,15 +227,15 @@ int main(int argc, char **argv) { const char *value = args[i]; struct argument *arg = command->args ? &command->args[i] : NULL; if (arg == NULL || arg->parse == NULL) { - return usage_error("unexpected argument '%s'", value); + return USAGE_ERROR("unexpected argument '%s'", value); } if (!arg->parse(&options, value)) { - return usage_error("invalid %s value '%s'", arg->name, value); + return USAGE_ERROR("invalid %s value '%s'", arg->name, value); } } struct argument *endarg = command->args ? &command->args[nargs] : NULL; if (endarg != NULL && endarg->name != NULL) { - return usage_error("missing argument %s", endarg->name); + return USAGE_ERROR("missing argument %s", endarg->name); } return command->run(&options); @@ -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; } |
