aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dbx.h7
-rw-r--r--src/main.c9
-rw-r--r--src/proc.c74
3 files changed, 90 insertions, 0 deletions
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 <[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
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 <[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;
+}