service: make instance_update() void and unconditionally replace config on update
[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 void do_reboot(void)
23 {
24 LOG("reboot\n");
25 fflush(stderr);
26 sync();
27 sleep(2);
28 reboot(RB_AUTOBOOT);
29 while (1)
30 ;
31 }
32
33 static void signal_shutdown(int signal, siginfo_t *siginfo, void *data)
34 {
35 int event = 0;
36 char *msg = NULL;
37
38 #ifndef DISABLE_INIT
39 switch(signal) {
40 case SIGINT:
41 case SIGTERM:
42 event = RB_AUTOBOOT;
43 msg = "reboot";
44 break;
45 case SIGUSR1:
46 case SIGUSR2:
47 event = RB_POWER_OFF;
48 msg = "poweroff";
49 break;
50 }
51 #endif
52
53 DEBUG(1, "Triggering %s\n", msg);
54 if (event)
55 procd_shutdown(event);
56 }
57
58 struct sigaction sa_shutdown = {
59 .sa_sigaction = signal_shutdown,
60 .sa_flags = SA_SIGINFO
61 };
62
63 static void signal_crash(int signal, siginfo_t *siginfo, void *data)
64 {
65 ERROR("Rebooting as procd has crashed\n");
66 do_reboot();
67 }
68
69 struct sigaction sa_crash = {
70 .sa_sigaction = signal_crash,
71 .sa_flags = SA_SIGINFO
72 };
73
74 static void signal_dummy(int signal, siginfo_t *siginfo, void *data)
75 {
76 ERROR("Got unexpected signal %d\n", signal);
77 }
78
79 struct sigaction sa_dummy = {
80 .sa_sigaction = signal_dummy,
81 .sa_flags = SA_SIGINFO
82 };
83
84 void procd_signal(void)
85 {
86 signal(SIGPIPE, SIG_IGN);
87 if (getpid() != 1)
88 return;
89 sigaction(SIGTERM, &sa_shutdown, NULL);
90 sigaction(SIGINT, &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 #ifndef DISABLE_INIT
99 reboot(RB_DISABLE_CAD);
100 #endif
101 }