0bb881e4435bc06881598140c5aaa5abcbf54589
[project/procd.git] / signal.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <sys/reboot.h>
16 #include <sys/types.h>
17
18 #include <unistd.h>
19
20 #include "procd.h"
21
22 static int preinit;
23
24 static void do_reboot(void)
25 {
26         LOG("reboot\n");
27         fflush(stderr);
28         sync();
29         reboot(RB_AUTOBOOT);
30         while (1)
31         ;
32 }
33
34 static void signal_shutdown(int signal, siginfo_t *siginfo, void *data)
35 {
36         int event = 0;
37         char *msg = NULL;
38
39         if (preinit)
40                 do_reboot();
41
42         switch(signal) {
43         case SIGTERM:
44                 event = RB_AUTOBOOT;
45                 msg = "reboot";
46                 break;
47         case SIGUSR1:
48         case SIGUSR2:
49                 event = RB_POWER_OFF;
50                 msg = "poweroff";
51                 break;
52         }
53
54         DEBUG(1, "Triggering %s\n", msg);
55         if (event)
56                 procd_shutdown(event);
57 }
58
59 struct sigaction sa_shutdown = {
60         .sa_sigaction = signal_shutdown,
61         .sa_flags = SA_SIGINFO
62 };
63
64 static void signal_crash(int signal, siginfo_t *siginfo, void *data)
65 {
66         ERROR("Rebooting as procd has crashed\n");
67         do_reboot();
68 }
69
70 struct sigaction sa_crash = {
71         .sa_sigaction = signal_crash,
72         .sa_flags = SA_SIGINFO
73 };
74
75 static void signal_dummy(int signal, siginfo_t *siginfo, void *data)
76 {
77         ERROR("Got unexpected signal %d\n", signal);
78 }
79
80 struct sigaction sa_dummy = {
81         .sa_sigaction = signal_dummy,
82         .sa_flags = SA_SIGINFO
83 };
84
85 void procd_signal(void)
86 {
87         signal(SIGPIPE, SIG_IGN);
88         if (getpid() != 1)
89                 return;
90         sigaction(SIGTERM, &sa_shutdown, NULL);
91         sigaction(SIGUSR1, &sa_shutdown, NULL);
92         sigaction(SIGUSR2, &sa_shutdown, NULL);
93         sigaction(SIGSEGV, &sa_crash, NULL);
94         sigaction(SIGBUS, &sa_crash, NULL);
95         sigaction(SIGHUP, &sa_dummy, NULL);
96         sigaction(SIGKILL, &sa_dummy, NULL);
97         sigaction(SIGSTOP, &sa_dummy, NULL);
98 }
99
100 void procd_signal_preinit(void)
101 {
102         preinit = 1;
103         sigaction(SIGTERM, &sa_shutdown, NULL);
104         sigaction(SIGUSR1, &sa_shutdown, NULL);
105         sigaction(SIGUSR2, &sa_shutdown, NULL);
106 }