diff --git a/Makefile b/Makefile index 49f94d8..a21b7dc 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ CFLAGS += -D_POSIX_C_SOURCE=2 CFLAGS += -D_GNU_SOURCE CFLAGS += -Wall -Werror -std=c23 -g CFLAGS += -Iinclude -CFLAGS += -lpthread +CFLAGS += -lpthread -luuid .PHONY: all clean diff --git a/src/main.c b/src/main.c index af081f4..f61569a 100644 --- a/src/main.c +++ b/src/main.c @@ -4,9 +4,11 @@ #include "pipeline.h" #include "threadpool.h" #include "util.h" +#include +#include #include #include -#include +#include #include threadpool* pool = NULL; @@ -24,14 +26,31 @@ void executor(void* data) { NULL }; char* argv[] = { "/bin/sh", "-c", e->command, NULL }; - if(posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, envp) != 0) { + uuid_t uuid; + uuid_generate(uuid); + char* pipeline_id = malloc(32); + uuid_unparse_lower(uuid, pipeline_id); + posix_spawn_file_actions_t actions; + posix_spawn_file_actions_init(&actions); + char* output_file = join(pipeline_id, ".log"); + int fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd == -1) { + perror("open"); + return; + } + posix_spawn_file_actions_adddup2(&actions, fd, STDOUT_FILENO); + posix_spawn_file_actions_adddup2(&actions, fd, STDERR_FILENO); + if(posix_spawn(&pid, "/bin/sh", &actions, NULL, argv, envp) != 0) { perror("posix_spawn"); return; } + log_trace("{%s} spawned (%s)", pipeline_id, e->name); int status; waitpid(pid, &status, 0); if(WIFEXITED(status)) - log_trace("pipeline exited with status %d", WEXITSTATUS(status)); + log_trace("{%s} exited with status %d", pipeline_id, WEXITSTATUS(status)); + free(pipeline_id); + free(output_file); free(name); free(url); free(trigger);