]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Flock: seems like machines being actually created
authorMaria Matejka <mq@ucw.cz>
Sun, 22 Sep 2024 20:02:25 +0000 (22:02 +0200)
committerMaria Matejka <mq@ucw.cz>
Sun, 23 Feb 2025 18:07:35 +0000 (19:07 +0100)
flock/container.c
flock/flock.h

index 9159bac3369b287e72489acc1eb0f8529be96cd4..1b6b5a83e3bc783384ba6b568553a566f1ede72c 100644 (file)
@@ -1,10 +1,13 @@
+#include "flock/flock.h"
+
 #include "lib/birdlib.h"
 #include "lib/cbor.h"
 #include "lib/io-loop.h"
 
-#include "flock/flock.h"
-
+#include <sched.h>
 #include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 static struct container_config {
   const char *hostname;
@@ -18,7 +21,7 @@ static void
 container_mainloop(int fd)
 {
   log(L_INFO "container mainloop with fd %d", fd);
-  /* TODO cleanup the loops from the forked process */
+  /* TODO unshare, fork and send info */
   while (1)
   {
     pause();
@@ -42,7 +45,40 @@ container_start(void)
 
   pid_t pid = fork();
   if (pid < 0)
-    die("Failed to fork container: %m");
+    die("Failed to fork container (parent): %m");
+
+  if (pid)
+  {
+    log(L_INFO "Forked container parent pid %d", pid);
+    container_counter++;
+    int status;
+    pid_t pp = waitpid(pid, &status, 0);
+
+    if (pp < 0)
+      die("Failed to waitpid %d: %m");
+
+    if (pp != pid)
+      die("Waited pid %d instead of %d, wtf", pp, pid);
+
+    const char *coreinfo = WCOREDUMP(status) ? " (core dumped)" : "";
+
+    if (WIFEXITED(status))
+      log(L_INFO "Process %d ended with status %d%s", pp, WEXITSTATUS(status), coreinfo);
+    else if (WIFSIGNALED(status))
+      log(L_INFO "Process %d exited by signal %d (%s)%s", pp, WTERMSIG(status), strsignal(WTERMSIG(status)), coreinfo);
+    else
+      log(L_ERR "Process %d exited with a strange status %d", pp, status);
+
+    return;
+  }
+
+  e = unshare(CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWTIME | CLONE_NEWNET);
+  if (e < 0)
+    die("Failed to unshare container: %m");
+
+  pid = fork();
+  if (pid < 0)
+    die("Failed to fork container (child): %m");
 
   if (!pid)
   {
@@ -53,8 +89,6 @@ container_start(void)
     bug("container_mainloop has returned");
   }
 
-  container_counter -= 2;
-
   close(fds[1]);
 
   byte outbuf[128];
@@ -84,8 +118,7 @@ container_start(void)
   if (e < 0)
     log(L_ERR "Failed to send socket: %m");
 
-  close(fds[0]);
-  rfree(lp);
+  exit(0);
 }
 
 /* The Parent */
@@ -440,8 +473,12 @@ hcf_parse(byte *buf, int size)
       {
        /* Code to run at the end of the mapping */
        case 0: /* toplevel item ended */
-         ctx->major_state = ~0ULL;
+         /* Reinit the parser */
+         ctx->type = 0xff;
+         ctx->major_state = 0;
+         ctx->stack_countdown[0] = 1;
          ctx->bytes_consumed = 0;
+
          if (size > pos + 1)
            hcf_parse(buf + pos + 1, size - pos - 1);
          return;
index 4c84f7055b2007fe2c9d6d285828e2a319f0b2d8..a58ee86f7de950659742d85500a4b1d366cf4ddc 100644 (file)
@@ -1,4 +1,6 @@
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
 
 #ifndef INCLUDE_FLOCK_H
 #define INCLUDE_FLOCK_H