I patched runit-2.1.2 (ALTLinux build) to handle SIGPWR on systems which 
support it.
SIGPWR cause immediate system shutdown just like SIGCONT with executable 
/etc/runit/stopit present in system.
It works with LXD for me.

mx
---
diff -Naur --new-file a/runit-2.1.2/man/runit.8 b/runit-2.1.2/man/runit.8
--- a/runit-2.1.2/man/runit.8	2020-03-13 23:11:44.080623026 +0300
+++ b/runit-2.1.2/man/runit.8	2020-03-13 23:21:48.188070643 +0300
@@ -75,6 +75,12 @@
 .B runit
 is told to shutdown the system.
 .P
+If
+.B runit
+receives a PWR signal,
+.B runit
+is told to start shutdown process immediately.
+.P
 if
 .B runit
 receives an INT signal, a ctrl-alt-del keyboard request is triggered.
diff -Naur --new-file a/runit-2.1.2/src/Makefile b/runit-2.1.2/src/Makefile
--- a/runit-2.1.2/src/Makefile	2020-03-13 23:11:44.099622788 +0300
+++ b/runit-2.1.2/src/Makefile	2020-03-13 23:13:44.983111536 +0300
@@ -226,6 +226,9 @@
 hasmkffo.h: choose compile hasmkffo.h1 hasmkffo.h2 load trymkffo.c
 	./choose cl trymkffo hasmkffo.h1 hasmkffo.h2 > hasmkffo.h
 
+hassgpwr.h: choose compile hassgpwr.h1 hassgpwr.h2 load trysgpwr.c
+	./choose cl trysgpwr hassgpwr.h1 hassgpwr.h2 > hassgpwr.h
+
 hassgact.h: choose compile hassgact.h1 hassgact.h2 load trysgact.c
 	./choose cl trysgact hassgact.h1 hassgact.h2 > hassgact.h
 
@@ -311,7 +314,7 @@
 sgetopt.o: buffer.h compile sgetopt.c sgetopt.h subgetopt.h
 	./compile sgetopt.c
 
-sig.o: compile sig.c sig.h
+sig.o: compile sig.c sig.h hassgpwr.h
 	./compile sig.c
 
 sig_block.o: compile hassgprm.h sig.h sig_block.c
@@ -373,12 +376,13 @@
 
 sysdeps: compile direntry.h hasflock.h hasmkffo.h hassgact.h \
 hassgprm.h hasshsgr.h haswaitp.h iopause.h load select.h systype \
-uint64.h reboot_system.h uw_tmp.h socket.lib
+uint64.h reboot_system.h uw_tmp.h socket.lib hassgpwr.h
 	rm -f sysdeps
 	cat systype compile load socket.lib >>sysdeps
 	grep sysdep direntry.h >>sysdeps
 	grep sysdep haswaitp.h >>sysdeps
 	grep sysdep hassgact.h >>sysdeps
+	grep sysdep hassgpwr.h >>sysdeps
 	grep sysdep hassgprm.h >>sysdeps
 	grep sysdep select.h >>sysdeps
 	grep sysdep uint64.h >>sysdeps
diff -Naur --new-file a/runit-2.1.2/src/TARGETS b/runit-2.1.2/src/TARGETS
--- a/runit-2.1.2/src/TARGETS	2018-10-18 12:34:41.000000000 +0300
+++ b/runit-2.1.2/src/TARGETS	2020-03-13 23:13:44.985111511 +0300
@@ -68,6 +68,7 @@
 hasmkffo.h
 hassgact.h
 hassgprm.h
+hassgpwr.h
 hasshsgr.h
 haswaitp.h
 iopause.h
diff -Naur --new-file a/runit-2.1.2/src/hassgpwr.h1 b/runit-2.1.2/src/hassgpwr.h1
--- a/runit-2.1.2/src/hassgpwr.h1	1970-01-01 03:00:00.000000000 +0300
+++ b/runit-2.1.2/src/hassgpwr.h1	2020-03-13 23:13:44.986111499 +0300
@@ -0,0 +1,3 @@
+/* Public domain. */
+
+/* sysdep: -sigpwr */
diff -Naur --new-file a/runit-2.1.2/src/hassgpwr.h2 b/runit-2.1.2/src/hassgpwr.h2
--- a/runit-2.1.2/src/hassgpwr.h2	1970-01-01 03:00:00.000000000 +0300
+++ b/runit-2.1.2/src/hassgpwr.h2	2020-03-13 23:13:44.988111474 +0300
@@ -0,0 +1,4 @@
+/* Public domain. */
+
+/* sysdep: +sigpwr */
+#define HASSIGPWR 1
diff -Naur --new-file a/runit-2.1.2/src/runit.c b/runit-2.1.2/src/runit.c
--- a/runit-2.1.2/src/runit.c	2020-03-13 23:11:44.050623401 +0300
+++ b/runit-2.1.2/src/runit.c	2020-03-13 23:13:44.990111449 +0300
@@ -5,6 +5,7 @@
 #include <signal.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include "hassgpwr.h"
 #include "runit.h"
 #include "sig.h"
 #include "strerr.h"
@@ -30,6 +31,7 @@
 int selfpipe[2];
 int sigc =0;
 int sigi =0;
+int sigp =0;
 
 void sig_cont_handler (void) {
   sigc++;
@@ -39,6 +41,10 @@
   sigi++;
   write(selfpipe[1], "", 1);
 }
+void sig_pwr_handler (void) {
+  sigp++;
+  write(selfpipe[1], "", 1);
+}
 void sig_child_handler (void) { write(selfpipe[1], "", 1); }
 
 void sync_if_needed() {
@@ -73,6 +79,11 @@
   sig_catch(sig_int, sig_int_handler);
   sig_block(sig_pipe);
   sig_block(sig_term);
+#ifdef HASSIGPWR
+  sig_block(sig_pwr);
+  sig_catch(sig_pwr, sig_pwr_handler);
+#endif
+
 
   /* console */
   if ((ttyfd =open_write("/dev/console")) != -1) {
@@ -136,6 +147,10 @@
       sig_uncatch(sig_int);
       sig_unblock(sig_pipe);
       sig_unblock(sig_term);
+#ifdef HASSIGPWR
+      sig_unblock(sig_pwr);
+      sig_ignore(sig_pwr);
+#endif
             
       strerr_warn3(INFO, "enter stage: ", stage[st], 0);
       execve(*prog, (char *const *)prog, envp);
@@ -150,6 +165,9 @@
       sig_unblock(sig_child);
       sig_unblock(sig_cont);
       sig_unblock(sig_int);
+#ifdef HASSIGPWR
+      sig_unblock(sig_pwr);
+#endif
 #ifdef IOPAUSE_POLL
       poll(&x, 1, 14000);
 #else
@@ -161,6 +179,9 @@
       sig_block(sig_cont);
       sig_block(sig_child);
       sig_block(sig_int);
+#ifdef HASSIGPWR
+      sig_block(sig_pwr);
+#endif
       
       while (read(selfpipe[0], &ch, 1) == 1) {}
       while ((child =wait_nohang(&wstat)) > 0)
@@ -211,7 +232,7 @@
       }
 
       /* sig? */
-      if (!sigc  && !sigi) {
+      if (!sigc  && !sigi && !sigp) {
 #ifdef DEBUG
         strerr_warn2(WARNING, "poll: ", &strerr_sys);
 #endif
@@ -219,7 +240,7 @@
       }
       if (st != 1) {
         strerr_warn2(WARNING, "signals only work in stage 2.", 0);
-        sigc =sigi =0;
+        sigc =sigi =sigp =0;
         continue;
       }
       if (sigi && (stat(CTRLALTDEL, &s) != -1) && (s.st_mode & S_IXUSR)) {
@@ -244,7 +265,7 @@
         sigi =0;
         sigc++;
       }
-      if (sigc && (stat(STOPIT, &s) != -1) && (s.st_mode & S_IXUSR)) {
+      if (sigp || (sigc && (stat(STOPIT, &s) != -1) && (s.st_mode & S_IXUSR))) {
         int i;
         /* unlink(STOPIT); */
         chmod(STOPIT, 0);
@@ -280,13 +301,13 @@
           if (wait_pid(&wstat, pid) == -1)
             strerr_warn2(WARNING, "wait_pid: ", &strerr_sys);
         }
-        sigc =0;
+        sigc =sigp =0;
         strerr_warn3(INFO, "leave stage: ", stage[st], 0);
 
         /* enter stage 3 */
         break;
       }
-      sigc =sigi =0;
+      sigc =sigi =sigp =0;
 #ifdef DEBUG
       strerr_warn2(WARNING, "no request.", 0);
 #endif
diff -Naur --new-file a/runit-2.1.2/src/sig.c b/runit-2.1.2/src/sig.c
--- a/runit-2.1.2/src/sig.c	2018-10-18 12:34:41.000000000 +0300
+++ b/runit-2.1.2/src/sig.c	2020-03-13 23:13:44.991111436 +0300
@@ -1,6 +1,7 @@
 /* Public domain. */
 
 #include <signal.h>
+#include "hassgpwr.h"
 #include "sig.h"
 
 int sig_alarm = SIGALRM;
@@ -11,5 +12,9 @@
 int sig_pipe = SIGPIPE;
 int sig_term = SIGTERM;
 
+#ifdef HASSIGPWR
+int sig_pwr = SIGPWR;
+#endif
+
 void (*sig_defaulthandler)() = SIG_DFL;
 void (*sig_ignorehandler)() = SIG_IGN;
diff -Naur --new-file a/runit-2.1.2/src/sig.h b/runit-2.1.2/src/sig.h
--- a/runit-2.1.2/src/sig.h	2018-10-18 12:34:41.000000000 +0300
+++ b/runit-2.1.2/src/sig.h	2020-03-13 23:13:44.993111411 +0300
@@ -11,6 +11,10 @@
 extern int sig_pipe;
 extern int sig_term;
 
+#ifdef HASSIGPWR
+extern int sig_pwr;
+#endif
+
 extern void (*sig_defaulthandler)();
 extern void (*sig_ignorehandler)();
 
diff -Naur --new-file a/runit-2.1.2/src/trysgpwr.c b/runit-2.1.2/src/trysgpwr.c
--- a/runit-2.1.2/src/trysgpwr.c	1970-01-01 03:00:00.000000000 +0300
+++ b/runit-2.1.2/src/trysgpwr.c	2020-03-13 23:13:44.994111399 +0300
@@ -0,0 +1,8 @@
+/* Public domain. */
+
+#include <signal.h>
+
+main()
+{
+    int sg = SIGPWR;
+}

Reply via email to