From 2f9ff2e757341c6639cc0e4f435eb58e617456eb Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sat, 14 Jun 2025 22:47:54 +0200 Subject: [PATCH] add cmd_run_async() --- gebs.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/gebs.h b/gebs.h index 4563b24..9a28efc 100644 --- a/gebs.h +++ b/gebs.h @@ -338,6 +338,10 @@ typedef Gebs_NString_List Gebs_Cmd; int gebs_cmd_run_sync_alloc(Gebs_Allocator *alloc, Gebs_Cmd *cmd); int gebs_cmd_fd_run_sync_alloc(Gebs_Allocator *alloc, int fd, Gebs_Cmd *cmd); +#define gebs_cmd_run_async(cmd) gebs_cmd_run_async_alloc(&gebs_default_allocator, (cmd)) + +pid_t gebs_cmd_run_async_alloc(Gebs_Allocator *alloc, Gebs_Cmd *cmd); + #define GEBS_CMD(...) \ ({ \ char *__args[] = { __VA_ARGS__ }; \ @@ -644,6 +648,35 @@ int gebs_cmd_fd_run_sync_collect_alloc(Gebs_Allocator *alloc, int fd, Gebs_Cmd * } } +pid_t gebs_cmd_run_async_alloc(Gebs_Allocator *alloc, Gebs_Cmd *cmd) +{ + Gebs_String_Builder sb = {0}; + defer { gebs_sb_free_alloc(alloc, &sb); } + gebs_nsl_join_alloc(alloc, cmd, &sb, " "); + gebs_sb_finish_alloc(alloc, &sb); + GEBS_LOGI("cmd `%s`\n", sb.items); + + // Clone the list + Gebs_Cmd new_cmd = {0}; + defer { gebs_cmd_free_alloc(alloc, &new_cmd); } + for (size_t i = 0; i < cmd->count; i++) { + gebs_cmd_append_alloc(alloc, &new_cmd, cmd->items[i]); + } + gebs_cmd_append_alloc((alloc), &new_cmd, nil); + + pid_t pid = vfork(); + switch (pid) { + case -1: + GEBS_LOGE("Could not vfork\n"); + return -1; + case 0: + execvp(new_cmd.items[0], (char * const *)new_cmd.items); + exit(EXIT_FAILURE); + default: + return pid; + } +} + // ---------------------------------------------------------------------------- // Allocators // ---------------------------------------------------------------------------- @@ -958,6 +991,8 @@ void gebs_rebuild_self1_alloc(Gebs_Allocator *alloc, int argc, char ** argv, #define cmd_fd_run_sync_alloc gebs_cmd_fd_run_sync_alloc #define cmd_fd_run_alloc gebs_cmd_fd_run_alloc #define cmd_fd_run gebs_cmd_fd_run +#define cmd_run_async_alloc gebs_cmd_run_async_alloc +#define cmd_run_async gebs_cmd_run_async #define needs_rebuild gebs_needs_rebuild #define needs_rebuild_many gebs_needs_rebuild_many