From 532b3ae9b5cd8609237e04db768cc1f750d8631d Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 25 Aug 2022 16:58:38 +0200 Subject: add some more code This adds a basic "worker" program. You can now do something like ./server & ./worker & ./client ci_run URL REV and the server should pass a message to worker, after which it should clone the repository at URL, checkout REV, and try to run the CI script. It's extremely unfinished: I need to sort out the graceful shutdown, how the server manages workers, etc. --- src/process.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/process.c (limited to 'src/process.c') diff --git a/src/process.c b/src/process.c new file mode 100644 index 0000000..9d95037 --- /dev/null +++ b/src/process.c @@ -0,0 +1,46 @@ +#include "process.h" +#include "log.h" + +#include +#include +#include + +static int wait_for_child(pid_t pid, int *ec) +{ + int status; + + pid_t ret = waitpid(pid, &status, 0); + if (ret < 0) { + print_errno("waitpid"); + return ret; + } + + if (WIFEXITED(status)) + *ec = WEXITSTATUS(status); + else + *ec = -1; + + return 0; +} + +int proc_spawn(const char *args[], int *ec) +{ + int ret = 0; + + pid_t child_pid = fork(); + if (child_pid < 0) { + print_errno("fork"); + return child_pid; + } + + if (child_pid) + return wait_for_child(child_pid, ec); + + ret = execv(args[0], (char *const *)args); + if (ret < 0) { + print_errno("execv"); + exit(ret); + } + + return 0; +} -- cgit v1.3