From 8b51676a32d9e03e248b14dbfc9e74ee3c8ff54f Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Thu, 3 Sep 2026 10:59:17 +0200 Subject: Add Utility for Spawning Processes --- src/dbx.h | 7 ++++++ src/main.c | 9 ++++++++ src/proc.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/proc.c diff --git a/src/dbx.h b/src/dbx.h index 869d1d6..7673d5b 100644 --- a/src/dbx.h +++ b/src/dbx.h @@ -1,6 +1,10 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello + #ifndef _DBX_H_ #define _DBX_H_ +#include #include 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 diff --git a/src/main.c b/src/main.c index 201e644..73e3028 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello + #include #include #include @@ -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 + +#include +#include +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3