diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-09-07 21:03:32 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-09-07 21:03:32 +0200 |
| commit | cd6d183fc8cd96cfb14de1ee0800d2613297012d (patch) | |
| tree | aff2e5f1efb7852ede4959d1efc922d032695947 | |
| parent | aa2c6a351167e44dd285d72d5a77627f3b0e78de (diff) | |
Start Refactoring Sub-Process Library
We stopped in the middle, as I realized that I am spending WAY to
much time just to do basic things in C instead of actually focusing
on implementing the tool.
| -rw-r--r-- | src/dbx.h | 12 | ||||
| -rw-r--r-- | src/engine_container.c | 73 | ||||
| -rw-r--r-- | src/main.c | 5 | ||||
| -rw-r--r-- | src/proc.c | 214 |
4 files changed, 169 insertions, 135 deletions
@@ -6,6 +6,7 @@ #include <limits.h> #include <stdbool.h> +#include <stddef.h> #include <stdint.h> #define COUNTOF(x) ((sizeof(x)) / (sizeof(*x))) @@ -26,11 +27,12 @@ enum dbx_proc_fds { DBXFD_STDERR = 4, DBXFD_ALL = 7, }; -bool dbx_proc_find(const char *name, char path[PATH_MAX]); -int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds); -int dbx_proc_output(const char *const command[], char *out, int outlen); -bool dbx_proc_exec(const char *const command[]); -#define DBX_CMD(...) ((const char *const[]){__VA_ARGS__, NULL}) +int dbx_proc_find2(const char *name, char path[PATH_MAX]); +int dbx_proc_run2(const char *const command[], enum dbx_proc_fds fds, + int *exit_code); +int dbx_proc_output2(const char *const command[], int *exit_code, char **out, + size_t *outlen); +int dbx_proc_exec2(const char *const command[]); bool dbx_ssh_init(const char *name, char ssh_pubkey[PATH_MAX]); bool dbx_ssh_config(const char *name, const char *hostname, uint16_t port); diff --git a/src/engine_container.c b/src/engine_container.c index da704a8..89d3706 100644 --- a/src/engine_container.c +++ b/src/engine_container.c @@ -11,18 +11,26 @@ struct dbx_engine { }; struct dbx_engine *dbx_engine_init(void) { - struct dbx_engine *engine = calloc(1, sizeof(struct dbx_engine)); + struct dbx_engine *engine = malloc(sizeof(struct dbx_engine)); if (engine == NULL) { dbx_perror("calloc", errno); return NULL; } - if (!dbx_proc_find("container", engine->container)) { + int result = dbx_proc_find("container", engine->container); + if (result) { + dbx_perror("unable to find `container` executable", result); goto error; } - if (dbx_proc_run(DBX_CMD(engine->container, "system", "status"), - DBXFD_NONE) != 0) { + int status_exit; + const char *const status[] = {"system", "status", NULL}; + result = dbx_proc_run(status, DBXFD_NONE, &status_exit); + if (result) { + dbx_perror("unable to check `container` status", result); + goto error; + } + if (status_exit != 0) { dbx_printerr("container engine must started with `container system start`"); goto error; } @@ -34,41 +42,6 @@ error: return NULL; } -#define PUBKEY_MAX 256 - -static int read_pubkey(const char *file, char key[PUBKEY_MAX]) { - FILE *f = fopen(file, "rb"); - if (!f) { - dbx_perror("fopen(ssh-pubkey)", errno); - return -1; - } - - int total = 0; - while (total < PUBKEY_MAX) { - size_t n = fread(key + total, 1, PUBKEY_MAX - total, f); - if (n == 0) { - if (!feof(f)) { - dbx_printerr("failed to read %s", file); - total = -1; - } - break; - } - total += n; - } - - if (--total >= 0 && total < PUBKEY_MAX && key[total] == '\n') { - key[total] = '\0'; - } else { - total = -1; - } - - if (fclose(f) != 0) { - dbx_perror("fclose(ssh-pubkey)", errno); - total = -1; - }; - return total; -} - bool dbx_engine_create(struct dbx_engine *engine, const char *image, const char *name, const char *ssh_pubkey, uint16_t sshd_port) { @@ -76,28 +49,40 @@ bool dbx_engine_create(struct dbx_engine *engine, const char *image, // on the host. We can therefore ignore the `sshd_port` setting. (void)sshd_port; - if (dbx_proc_run(DBX_CMD(engine->container, "machine", "create", "--name", - name, "--no-boot", "--home-mount=none", image), - DBXFD_STDOUT | DBXFD_STDERR) != 0) { + int create_exit; + const char *const status[] = { + engine->container, "machine", "create", "--name", name, + "--no-boot", "--home-mount=none", image, NULL}; + int result = dbx_proc_run(status, DBXFD_STDOUT | DBXFD_STDERR, &status_exit); + if (result) { + dbx_perror("unable to create `container`", result); + return false; + } + if (create_exit != 0) { + dbx_printerr("container creation failed"); return false; } // Unfortunately, there is no way to access files from the container machine // while it is stopped; instead boot it and run a small script that can // authorize our SSH key. - char key[PUBKEY_MAX]; - if (read_pubkey(ssh_pubkey, key) < 0) { + char *key; + result = dbx_readfile(ssh_pubkey, &key); + if (result) { + dbx_perror("unable to read SSH public key", result); return false; } char script[1024]; int n = snprintf(script, sizeof(script), "echo '%s' > /etc/ssh/authorized_keys", key); if (n < 0 || n >= (int)sizeof(script)) { + dbx_printerr("invalid SSH public key format"); return false; } // Note that `container machine run` _requires_ `stdin` for some reason (I // assume it is to attach to the VM process), make sure to not close it. + if (dbx_proc_run(DBX_CMD(engine->container, "machine", "run", "--name", name, "--root", script), DBXFD_ALL) != 0) { @@ -95,8 +95,9 @@ int dummy_run(struct dbx_engine *engine, struct dbx_options const *options) { printf("- ports: %d:%d\n", options->ports.container, options->ports.host); char container[PATH_MAX]; - if (!dbx_proc_find("container", container)) { - fprintf(stderr, "dbx: can't find `container`.\n"); + int result = dbx_proc_find("container", container); + if (result) { + dbx_perror("Can't find `container`", result); return EXIT_FAILURE; } @@ -44,14 +44,15 @@ static const char *next_path_segment(char **path) { return segment; } -bool dbx_proc_find(const char *name, char file[PATH_MAX]) { - assert(name != NULL && name[0] != '\0' && strchr(name, '/') == NULL && - file != NULL); +int dbx_proc_find(const char *name, char file[PATH_MAX]) { + if (name == NULL || name[0] == '\0' || strchr(name, '/') != NULL || + file == NULL) { + return EINVAL; + } char *path = strdup(get_path()); if (path == NULL) { - dbx_perror("strdup", ENOMEM); - return false; + return ENOMEM; } char *pos = path; @@ -59,8 +60,7 @@ bool dbx_proc_find(const char *name, char file[PATH_MAX]) { 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)) { + if (dbx_formatpath(candidate, "%s/%s", dir, name)) { continue; } @@ -77,11 +77,6 @@ bool dbx_proc_find(const char *name, char file[PATH_MAX]) { } static char **dup_argv(const char *const command[]) { - if (command == NULL || command[0] == NULL) { - dbx_perror("dup_argv", EINVAL); - return NULL; - } - size_t count = 0; size_t size = 0; for (; command[count] != NULL; count++) { @@ -92,7 +87,6 @@ static char **dup_argv(const char *const command[]) { // for the extra NULL pointer at the end of the array. char **argv = malloc((count + 1) * sizeof(char *) + size); if (argv == NULL) { - dbx_perror("malloc", ENOMEM); return NULL; } @@ -106,18 +100,22 @@ static char **dup_argv(const char *const command[]) { return argv; } -int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds) { - int exit_code = -1; +int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds, + int *exit_code) { + if (command == NULL || command[0] == NULL || exit_code == NULL) { + return EINVAL; + } + int result = 0; char **argv = dup_argv(command); if (argv == NULL) { + result = ENOMEM; goto exit; } posix_spawn_file_actions_t file_actions; - int result = posix_spawn_file_actions_init(&file_actions); + result = posix_spawn_file_actions_init(&file_actions); if (result != 0) { - dbx_perror("spawn_file_actions_init", result); goto cleanup_argv; } @@ -125,7 +123,6 @@ int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds) { result = posix_spawn_file_actions_addopen(&file_actions, STDIN_FILENO, _PATH_DEVNULL, O_RDONLY, 0); if (result != 0) { - dbx_perror("posix_spawn_file_actions_addopen(stdin)", result); goto cleanup_fa; } } @@ -134,7 +131,6 @@ int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds) { 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_fa; } } @@ -143,7 +139,6 @@ int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds) { 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_fa; } } @@ -151,7 +146,6 @@ int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds) { pid_t pid; result = posix_spawnp(&pid, argv[0], &file_actions, NULL, argv, environ); if (result != 0) { - dbx_perror("posix_spawnp", result); goto cleanup_fa; } @@ -161,113 +155,156 @@ int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds) { continue; } - dbx_perror("waitpid", errno); + result = errno; goto cleanup_fa; } if (WIFEXITED(status)) { - exit_code = WEXITSTATUS(status); + *exit_code = WEXITSTATUS(status); } else if (WIFSIGNALED(status)) { - exit_code = 128 + WTERMSIG(status); + *exit_code = 128 + WTERMSIG(status); + } else { + *exit_code = -1; } -cleanup_fa: - result = posix_spawn_file_actions_destroy(&file_actions); - if (result != 0) { - dbx_perror("posix_spawn_file_actions_destroy", result); +cleanup_fa:; + int destroy_result = posix_spawn_file_actions_destroy(&file_actions); + if (result == 0) { + result = destroy_result; } cleanup_argv: free(argv); exit: - return exit_code; + return result; +} + +static int read_all(int fd, char **out, size_t *outlen) { + size_t cap = 4096; + size_t len = 0; + char *buf = malloc(cap); + if (buf == NULL) { + return ENOMEM; + } + + for (;;) { + if (len == cap) { + size_t new_cap = cap * 2; + if (new_cap >= SSIZE_MAX || new_cap <= cap) { + free(buf); + return ENOMEM; + } + + char *new_buf = realloc(buf, new_cap); + if (new_buf == NULL) { + free(buf); + return ENOMEM; + } + + buf = new_buf; + cap = new_cap; + } + + ssize_t n = read(fd, buf + len, cap - len); + if (n == 0) { + break; + } else if (n < 0) { + if (errno == EINTR) { + continue; + } + + int result = errno; + free(buf); + return result; + } + + len += (size_t)n; + } + + char *final = realloc(buf, len + 1); + if (!final) { + free(buf); + return ENOMEM; + } + final[len] = '\0'; + + *out = final; + *outlen = len; + return 0; } -int dbx_proc_output(const char *const command[], char *out, int outlen) { - if (out == NULL || outlen < 1) { - return -1; +int dbx_proc_output(const char *const command[], int *exit_code, char **out, + size_t *outlen) { + if (command == NULL || command[0] == NULL || exit_code == NULL || + out == NULL || outlen == NULL) { + return EINVAL; } - size_t total = 0; - int exit_code = -1; + int result = 0; + char *buf = NULL; char **argv = dup_argv(command); if (argv == NULL) { + result = ENOMEM; goto exit; } int pipe_fds[2]; if (pipe(pipe_fds) != 0) { - dbx_perror("pipe", errno); + result = errno; goto cleanup_argv; } int read_fd = pipe_fds[0]; int write_fd = pipe_fds[1]; if (write_fd == STDOUT_FILENO) { - dbx_printerr("unexpected pipe file descriptor values"); + result = EINVAL; goto cleanup_fds; } posix_spawn_file_actions_t file_actions; - int result = posix_spawn_file_actions_init(&file_actions); + result = posix_spawn_file_actions_init(&file_actions); if (result != 0) { - dbx_perror("spawn_file_actions_init", result); goto cleanup_fds; } result = posix_spawn_file_actions_addclose(&file_actions, read_fd); if (result != 0) { - dbx_perror("posix_spawn_file_actions_addclose(read_fd)", result); goto cleanup_fa; } result = posix_spawn_file_actions_adddup2(&file_actions, write_fd, STDOUT_FILENO); if (result != 0) { - dbx_perror("posix_spawn_file_actions_adddup2(stdout)", result); goto cleanup_fa; } result = posix_spawn_file_actions_addclose(&file_actions, write_fd); if (result != 0) { - dbx_perror("posix_spawn_file_actions_addclose(write_fd)", result); goto cleanup_fa; } pid_t pid; result = posix_spawnp(&pid, command[0], &file_actions, NULL, argv, environ); if (result != 0) { - dbx_perror("posix_spawnp", result); goto cleanup_fa; } if (close(write_fd) != 0) { - dbx_perror("close", errno); + result = errno; } write_fd = -1; - - size_t limit = outlen - 1; - while (total < limit) { - ssize_t n = read(read_fd, out + total, limit - total); - if (n == 0) { - break; - } else if (n < 0) { - if (errno == EINTR) { - continue; - } - dbx_perror("read", errno); - result = errno; - goto cleanup_pid; - } - total += (size_t)n; + if (result != 0) { + goto cleanup_pid; } - out[total] = '\0'; + + size_t buflen; + result = read_all(read_fd, &buf, &buflen); cleanup_pid: - if (close(read_fd) != 0) { - dbx_perror("close", errno); + if (close(read_fd) != 0 && result == 0) { + result = errno; } read_fd = -1; @@ -277,51 +314,60 @@ cleanup_pid: continue; } - dbx_perror("waitpid", errno); + if (result == 0) { + result = errno; + } goto cleanup_fa; } +cleanup_fa:; + int destroy_result = posix_spawn_file_actions_destroy(&file_actions); if (result == 0) { - if (WIFEXITED(status)) { - exit_code = WEXITSTATUS(status); - } else if (WIFSIGNALED(status)) { - exit_code = 128 + WTERMSIG(status); - } - } - -cleanup_fa: - result = posix_spawn_file_actions_destroy(&file_actions); - if (result != 0) { - dbx_perror("posix_spawn_file_actions_destroy", result); + result = destroy_result; } cleanup_fds: - if (write_fd >= 0 && close(write_fd) != 0) { - dbx_perror("close", errno); + if (write_fd >= 0 && close(write_fd) != 0 && result == 0) { + result = errno; } - if (read_fd >= 0 && close(read_fd) != 0) { - dbx_perror("close", errno); + if (read_fd >= 0 && close(read_fd) != 0 && result == 0) { + result = errno; } cleanup_argv: free(argv); exit: - out[total] = '\0'; - return exit_code; + if (result == 0) { + if (WIFEXITED(status)) { + *exit_code = WEXITSTATUS(status); + } else if (WIFSIGNALED(status)) { + *exit_code = 128 + WTERMSIG(status); + } else { + *exit_code = -1; + } + *out = buf; + *outlen = buflen; + } else { + free(buf); + } + + return result; } -bool dbx_proc_exec(const char *const command[]) { +int dbx_proc_exec(const char *const command[]) { + if (command == NULL || command[0] == NULL) { + return EINVAL; + } + char **argv = dup_argv(command); if (argv == NULL) { - return false; + return ENOMEM; } execvp(argv[0], argv); // execvp only ever returns if there was an error. - dbx_perror("execv", errno); - free(argv); - return false; + return errno; } |
