aboutsummaryrefslogtreecommitdiff
path: root/src/proc.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-09-06 10:55:07 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-09-06 10:55:07 +0200
commit5f48411161d243627ff3b521250c74d40d776bc3 (patch)
tree8f1fa43a8c39c2af666668cc8d57b83819345d37 /src/proc.c
parent8165883a153e4bc2d50fa3710e47b76d9f79bb15 (diff)
Implemented Devbox Creation
We are now able to create devboxes :tada:. It supports the post install script, and added support for custom SSH configuration options (through `~/.config/dbx/$NAME.ssh_config` file).
Diffstat (limited to 'src/proc.c')
-rw-r--r--src/proc.c229
1 files changed, 200 insertions, 29 deletions
diff --git a/src/proc.c b/src/proc.c
index 5c6fe83..59aa9a7 100644
--- a/src/proc.c
+++ b/src/proc.c
@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
+#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -75,55 +76,93 @@ bool dbx_proc_find(const char *name, char file[PATH_MAX]) {
return found;
}
-int dbx_proc_run(const char *const command[], enum dbx_proc_output out) {
- assert(command != NULL && command[0] != NULL);
+static char **dup_argv(const char *const command[]) {
+ if (command == NULL || command[0] == NULL) {
+ dbx_perror("dup_argv", EINVAL);
+ return NULL;
+ }
- int result;
- posix_spawn_file_actions_t file_actions;
- if ((result = posix_spawn_file_actions_init(&file_actions)) != 0) {
- dbx_perror("spawn_file_actions_init", result);
- return -1;
+ size_t count = 0;
+ size_t size = 0;
+ for (; command[count] != NULL; count++) {
+ size += strlen(command[count]) + 1;
+ }
+
+ // Allocate a single buffer for the `argv` array and its data. Include space
+ // 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;
}
+ char *buf = (char *)(argv + count + 1);
+ for (size_t i = 0; i < count; i++) {
+ argv[i] = buf;
+ buf = stpcpy(buf, command[i]) + 1;
+ }
+
+ argv[count] = NULL;
+ return argv;
+}
+
+int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds) {
int exit_code = -1;
- result = posix_spawn_file_actions_addclose(&file_actions, STDIN_FILENO);
+
+ char **argv = dup_argv(command);
+ if (argv == NULL) {
+ goto exit;
+ }
+
+ posix_spawn_file_actions_t file_actions;
+ int result = posix_spawn_file_actions_init(&file_actions);
if (result != 0) {
- dbx_perror("posix_spawn_file_actions_addclose(stdin)", result);
- goto cleanup;
+ dbx_perror("spawn_file_actions_init", result);
+ goto cleanup_argv;
}
- if ((out & DBXP_STDOUT) == 0) {
+ if ((fds & DBXFD_STDIN) == 0) {
+ 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;
+ }
+ }
+
+ if ((fds & DBXFD_STDOUT) == 0) {
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;
+ goto cleanup_fa;
}
}
- if ((out & DBXP_STDERR) == 0) {
+ if ((fds & DBXFD_STDERR) == 0) {
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;
+ goto cleanup_fa;
}
}
- // POSIX guarantees that the pointer is not modified, so the cast is safe.
- char *const *argv = (char *const *)command;
-
pid_t pid;
- result = posix_spawnp(&pid, command[0], &file_actions, NULL, argv, environ);
+ result = posix_spawnp(&pid, argv[0], &file_actions, NULL, argv, environ);
if (result != 0) {
dbx_perror("posix_spawnp", result);
- goto cleanup;
+ goto cleanup_fa;
}
int status;
- if (waitpid(pid, &status, 0) != pid) {
+ while (waitpid(pid, &status, 0) != pid) {
+ if (errno == EINTR) {
+ continue;
+ }
+
dbx_perror("waitpid", errno);
- goto cleanup;
+ goto cleanup_fa;
}
if (WIFEXITED(status)) {
@@ -132,25 +171,157 @@ int dbx_proc_run(const char *const command[], enum dbx_proc_output out) {
exit_code = 128 + WTERMSIG(status);
}
-cleanup:
+cleanup_fa:
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.
}
+
+cleanup_argv:
+ free(argv);
+
+exit:
return exit_code;
}
-bool dbx_proc_exec(const char *const command[]) {
- assert(command != NULL && command[0] != NULL);
+int dbx_proc_output(const char *const command[], char *out, int outlen) {
+ if (out == NULL || outlen < 1) {
+ return -1;
+ }
- // POSIX guarantees that the pointer is not modified, so the cast is safe.
- char *const *argv = (char *const *)command;
+ size_t total = 0;
+ int exit_code = -1;
+
+ char **argv = dup_argv(command);
+ if (argv == NULL) {
+ goto exit;
+ }
- execvp(command[0], argv);
+ int pipe_fds[2];
+ if (pipe(pipe_fds) != 0) {
+ dbx_perror("pipe", 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");
+ goto cleanup_fds;
+ }
+
+ posix_spawn_file_actions_t file_actions;
+ int 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);
+ }
+ 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;
+ }
+ out[total] = '\0';
+
+cleanup_pid:
+ if (close(read_fd) != 0) {
+ dbx_perror("close", errno);
+ }
+ read_fd = -1;
+
+ int status;
+ while (waitpid(pid, &status, 0) != pid) {
+ if (errno == EINTR) {
+ continue;
+ }
+
+ dbx_perror("waitpid", errno);
+ goto cleanup_fa;
+ }
+
+ 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);
+ }
+
+cleanup_fds:
+ if (write_fd >= 0 && close(write_fd) != 0) {
+ dbx_perror("close", errno);
+ }
+ if (read_fd >= 0 && close(read_fd) != 0) {
+ dbx_perror("close", errno);
+ }
+
+cleanup_argv:
+ free(argv);
+
+exit:
+ out[total] = '\0';
+ return exit_code;
+}
+
+bool dbx_proc_exec(const char *const command[]) {
+ char **argv = dup_argv(command);
+ if (argv == NULL) {
+ return false;
+ }
+
+ execvp(argv[0], argv);
// execvp only ever returns if there was an error.
dbx_perror("execv", errno);
+
+ free(argv);
return false;
}