]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Flock: filedescriptor cleanups in hypervisor
authorMaria Matejka <mq@ucw.cz>
Wed, 2 Oct 2024 12:48:26 +0000 (14:48 +0200)
committerMaria Matejka <mq@ucw.cz>
Sun, 23 Feb 2025 18:07:35 +0000 (19:07 +0100)
flock/container.c
flock/flock.c
flock/flock.h
flock/hypervisor.c

index 9e8aaa2deea2c95245c892cce8383cdcd67e0a10..597e6c42af9b7b7c2f5f9a87f105fd4bfd31d56f 100644 (file)
@@ -471,7 +471,7 @@ container_start(void)
 
   /* create socketpair before forking to do communication */
   int fds[2];
-  int e = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
+  int e = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds);
   if (e < 0)
     die("Failed to create internal socketpair: %m");
 
@@ -1161,7 +1161,7 @@ hypervisor_container_fork(void)
   int fds[2], e;
 
   /* create socketpair before forking to do communication */
-  e = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
+  e = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds);
   if (e < 0)
     die("Failed to create internal socketpair: %m");
 
@@ -1196,6 +1196,7 @@ hypervisor_container_fork(void)
 
   /* noreturn child side */
   close(fds[0]);
+  hexp_cleanup_after_fork();
   container_forker_fd = fds[1];
 
   this_thread_id |= 0xf000;
index 5c062642703a9df72998c1e7c2dd39a666643a31..a5a129c05fbd5752880156067176748efe1d13bd 100644 (file)
@@ -285,15 +285,15 @@ main(int argc, char **argv, char **argh UNUSED)
    * let's spawn a child to do external communication before unsharing */
   hypervisor_exposed_fork();
 
-  /* We also need to prepare all the hypervisor-init stuff */
-  hypervisor_control_socket();
-
-  /* And now finally we can go for unsharing the networks */
+  /* And now we can unshare the networks */
   SYSCALL(unshare, CLONE_NEWNET);
 
-  /* Before resuming, we also need to fork the container forker */
+  /* Before running in multiple threads, we also need to fork the container forker */
   hypervisor_container_fork();
 
+  /* Control socket needs to exist */
+  hypervisor_control_socket();
+
   /* Set signal handlers as this process is init in its PID namespace */
   signal(SIGTERM, hypervisor_poweroff_sighandler);
   signal(SIGINT, hypervisor_poweroff_sighandler);
index 81c9ef3fd2c5edcb08c2f3710a528056240f8688..1abb6c58cbd7bfd74cfa69b14e327d32172966a1 100644 (file)
@@ -50,6 +50,8 @@ void hypervisor_container_request(sock *s, const char *name, const char *basedir
 void hypervisor_container_shutdown(sock *s, const char *name);
 int container_ctl_fd(const char *name);
 
+void hexp_cleanup_after_fork(void);
+
 extern event reboot_event, poweroff_event;
 extern event_list shutdown_event_list;
 
index 8717c0390c102c491e28db00b81464c5bd4ac30b..50fff418deb2e2b2fdf0322e7c4f174f3fabaaaa 100644 (file)
@@ -356,7 +356,7 @@ hypervisor_exposed_fork(void)
   int fds[2], e;
 
   /* create socketpair before forking to do communication */
-  e = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
+  e = socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds);
   if (e < 0)
     die("Failed to create internal socketpair: %m");
 
@@ -402,6 +402,14 @@ hypervisor_exposed_fork(void)
   birdloop_minimalist_main();
 }
 
+void
+hexp_cleanup_after_fork(void)
+{
+  birdloop_enter(he.loop);
+  rp_free(he.p);
+  birdloop_leave(he.loop);
+  birdloop_free(he.loop);
+}
 
 /**
  * Hypervisor's mapping between external ports and names