[PATCH] Implement stage 4 (execve handoff)

From: Ryan Finnie <ryan_at_finnie.org>
Date: Tue, 29 Jul 2014 12:01:36 -0700

This patch adds stage 4, an optional stage which is run after stage 3.
If /etc/runit/4 is found and executable, runit will execve() into it,
giving it control of PID 1. Finnix (http://www.finnix.org/) uses runit
as its init system, and uses this patch as a way to pivot root back into
a ramdisk upon shutdown, so it may cleanly umount the union mount which
houses runit.

Signed-off-by: Ryan Finnie <ryan_at_finnie.org>
---
diff -ruN a/runit-2.1.1/src/runit.c b/runit-2.1.1/src/runit.c
--- a/runit-2.1.1/src/runit.c	2009-10-04 13:44:02.000000000 -0700
+++ b/runit-2.1.1/src/runit.c	2013-10-10 16:03:21.738259966 -0700
_at_@ -22,10 +22,11 @@
 #define WARNING "- runit: warning: "
 #define FATAL "- runit: fatal: "
 
-const char * const stage[3] ={
+const char * const stage[4] ={
   "/etc/runit/1",
   "/etc/runit/2",
-  "/etc/runit/3" };
+  "/etc/runit/3",
+  "/etc/runit/4" };
 
 int selfpipe[2];
 int sigc =0;
_at_@ -94,7 +95,17 @@
                ": booting.", 0);
 
   /* runit */
-  for (st =0; st < 3; st++) {
+  for (st =0; st < 4; st++) {
+    /* stage 4 gets control of pid 1 */
+    if (st == 3) {
+      if ((stat(stage[st], &s) != -1) && (s.st_mode & S_IXUSR)) {
+        prog[0] =stage[st];
+        prog[1] =0;
+        strerr_warn3(INFO, "enter stage: ", stage[st], 0);
+        execve(*prog, (char *const *)prog, envp);
+        strerr_die4sys(0, FATAL, "unable to exec: ", stage[st], ": ");
+      }
+    }
     /* if (st == 2) logwtmp("~", "reboot", ""); */
     while ((pid =fork()) == -1) {
       strerr_warn4(FATAL, "unable to fork for \"", stage[st], "\" pausing: ",



Received on Tue Jul 29 2014 - 19:01:36 UTC

This archive was generated by hypermail 2.3.0 : Sun May 09 2021 - 19:44:18 UTC