Kill processes on shutdown
[project/procd.git] / state.c
diff --git a/state.c b/state.c
index 9f3033be83299e3a5f760b7e510aad2a9eae8fde..b9416341440d873b999dadbda8e6b43a0b9a3477 100644 (file)
--- a/state.c
+++ b/state.c
@@ -15,6 +15,8 @@
 #include <sys/reboot.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <signal.h>
 
 #include "procd.h"
 #include "syslog.h"
@@ -37,6 +39,7 @@ static int reboot_event;
 
 static void state_enter(void)
 {
+       char ubus_cmd[] = "/sbin/ubusd";
 
        switch (state) {
        case STATE_EARLY:
@@ -54,7 +57,7 @@ static void state_enter(void)
 
                LOG("- init -\n");
                service_init();
-               service_start_early("ubus", "/sbin/ubusd");
+               service_start_early("ubus", ubus_cmd);
 
                procd_inittab();
                procd_inittab_run("respawn");
@@ -74,6 +77,14 @@ static void state_enter(void)
                break;
 
        case STATE_HALT:
+               LOG("- SIGTERM processes -\n");
+               kill(-1, SIGTERM);
+               sync();
+               sleep(1);
+               LOG("- SIGKILL processes -\n");
+               kill(-1, SIGKILL);
+               sync();
+               sleep(1);
                LOG("- reboot -\n");
                reboot(reboot_event);
                break;
@@ -93,6 +104,8 @@ void procd_state_next(void)
 
 void procd_shutdown(int event)
 {
+       if (state >= STATE_SHUTDOWN)
+               return;
        DEBUG(2, "Shutting down system with event %x\n", event);
        reboot_event = event;
        state = STATE_SHUTDOWN;