aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/process.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-08-25 16:58:38 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2022-08-26 04:05:02 +0200
commit532b3ae9b5cd8609237e04db768cc1f750d8631d (patch)
treef65253a6ce9970d1d93e6bb6c65758d6fa98373a /src/process.c
parentcmake: ignore unused parameters for now (diff)
downloadcimple-532b3ae9b5cd8609237e04db768cc1f750d8631d.tar.gz
cimple-532b3ae9b5cd8609237e04db768cc1f750d8631d.zip
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.
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c46
1 files changed, 46 insertions, 0 deletions
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 <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+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;
+}