From d8b8b7b729ddcc6ff805272df8ba330ffa8bcf01 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Tue, 6 Aug 2024 18:38:42 +0200 Subject: [PATCH] feat: inject pipeline env and dont use system --- include/pipeline.h | 5 ++++- include/util.h | 2 ++ scid.service | 2 +- src/main.c | 35 +++++++++++++++++++++++++++++------ src/notify.c | 3 +++ src/util.c | 8 ++++++++ 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/include/pipeline.h b/include/pipeline.h index ef9b83d..576b68b 100644 --- a/include/pipeline.h +++ b/include/pipeline.h @@ -13,7 +13,10 @@ typedef optional_type(pipeline_conf*) optional_pipeline_conf; typedef struct { const struct inotify_event* event; - const char* command; + char* name; + char* url; + char* trigger; + char* command; } pipeline_event; // create a new pipeline_conf struct instance based on a configuration line. diff --git a/include/util.h b/include/util.h index b143e8c..80b5c5f 100644 --- a/include/util.h +++ b/include/util.h @@ -22,4 +22,6 @@ char* trim(const char* const str); typedef void(*line_handler)(const char*); void per_line(const char* file, line_handler handler); +char* join(const char* a, const char* b); + #endif diff --git a/scid.service b/scid.service index e2177cd..c262b07 100644 --- a/scid.service +++ b/scid.service @@ -4,7 +4,7 @@ AssertPathExists=/etc/sci/pipelines.conf [Service] Type=exec -ExecStart=/usr/local/bin/sci +ExecStart=/usr/local/bin/sci -f /etc/sci/pipelines.conf [Install] WantedBy=multi-user.target diff --git a/src/main.c b/src/main.c index 25744e6..af081f4 100644 --- a/src/main.c +++ b/src/main.c @@ -6,16 +6,39 @@ #include "util.h" #include #include - -void executor(void* data) { - const char* command = data; - system(command); -} +#include +#include threadpool* pool = NULL; +void executor(void* data) { + const pipeline_event* const e = data; + pid_t pid; + char* name = join("SCI_PIPELINE_NAME=", e->name); + char* url = join("SCI_PIPELINE_URL=", e->url); + char* trigger = join("SCI_PIPELINE_TRIGGER=", e->trigger); + char *envp[] = { + name, + url, + trigger, + NULL + }; + char* argv[] = { "/bin/sh", "-c", e->command, NULL }; + if(posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, envp) != 0) { + perror("posix_spawn"); + return; + } + int status; + waitpid(pid, &status, 0); + if(WIFEXITED(status)) + log_trace("pipeline exited with status %d", WEXITSTATUS(status)); + free(name); + free(url); + free(trigger); +} + void on_event(pipeline_event* const e) { - if(!threadpool_add_work(pool, executor, (void*)e->command)) + if(!threadpool_add_work(pool, executor, (void*)e)) log_error("could not add work to the threadpool"); } diff --git a/src/notify.c b/src/notify.c index 158c597..377ce82 100644 --- a/src/notify.c +++ b/src/notify.c @@ -24,6 +24,9 @@ void listen_for_changes(const pipeline_conf* config, notify_callback callback) { struct inotify_event* e = (struct inotify_event*)&buffer[i]; pipeline_event ev; ev.event = e; + ev.name = config->name; + ev.url = config->url; + ev.trigger = config->trigger; ev.command = config->command; callback(&ev); i += EV_SIZE + e->len; diff --git a/src/util.c b/src/util.c index bfb7de7..3565a56 100644 --- a/src/util.c +++ b/src/util.c @@ -36,3 +36,11 @@ void per_line(const char* file, line_handler handler) { free(line); fclose(stream); } + +char* join(const char* a, const char* b) { + size_t alen = strlen(a); + size_t blen = strlen(b); + char* result = malloc(alen + blen + 1); + sprintf(result, "%s%s", a, b); + return result; +}