diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-09-03 10:59:17 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-09-03 10:59:17 +0200 |
| commit | 8b51676a32d9e03e248b14dbfc9e74ee3c8ff54f (patch) | |
| tree | a21899f25b6a1cda5ac9d83a26c1387921ca7dff | |
| parent | ee2c3e1294272f3da77abb3db1928e85b2e17dc4 (diff) | |
Add Utility for Spawning Processes
| -rw-r--r-- | src/dbx.h | 7 | ||||
| -rw-r--r-- | src/main.c | 9 | ||||
| -rw-r--r-- | src/proc.c | 74 |
3 files changed, 90 insertions, 0 deletions
@@ -1,6 +1,10 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> + #ifndef _DBX_H_ #define _DBX_H_ +#include <stdbool.h> #include <stdint.h> struct dbx_options { @@ -15,4 +19,7 @@ struct dbx_options { #define COUNTOF(x) ((sizeof(x)) / (sizeof(*x))) +int dbx_proc_run(char *const command[]); +bool dbx_proc_exec(char *const command[]); + #endif @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> + #include <assert.h> #include <errno.h> #include <stdarg.h> @@ -94,6 +97,12 @@ int dummy_run(struct dbx_options *options) { printf("- name: %s\n", options->name); 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); + return EXIT_SUCCESS; } diff --git a/src/proc.c b/src/proc.c new file mode 100644 index 0000000..9e57aa5 --- /dev/null +++ b/src/proc.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]> + +#include <assert.h> +#include <fcntl.h> +#include <spawn.h> +#include <stdio.h> +#include <string.h> +#include <sys/wait.h> +#include <unistd.h> + +#include "dbx.h" + +extern char **environ; + +int dbx_proc_run(char *const command[]) { + assert(command != NULL && command[0] != NULL); + + posix_spawn_file_actions_t file_actions; + if (posix_spawn_file_actions_init(&file_actions) != 0) { + perror("spawn_file_actions_init"); + return -1; + } + + int exit_code = -1; + if (posix_spawn_file_actions_addclose(&file_actions, STDIN_FILENO) != 0) { + perror("posix_spawn_file_actions_addclose(stdin)"); + 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)"); + 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)"); + goto cleanup; + } + + pid_t pid; + if (posix_spawnp(&pid, command[0], &file_actions, NULL, command, environ) != + 0) { + perror("posix_spawnp"); + goto cleanup; + } + + int status; + if (waitpid(pid, &status, 0) != pid) { + perror("waitpid"); + goto cleanup; + } + + if (WIFEXITED(status)) { + exit_code = WEXITSTATUS(status); + } else if (WIFSIGNALED(status)) { + exit_code = 128 + WTERMSIG(status); + } + +cleanup: + if (posix_spawn_file_actions_destroy(&file_actions) != 0) { + perror("posix_spawn_file_actions_destroy"); + } + return exit_code; +} + +bool dbx_proc_exec(char *const command[]) { + assert(command != NULL && command[0] != NULL); + execvp(command[0], command); + + // execvp only ever returns if there was an error. + perror("execvp"); + return false; +} |
