-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsignal.c
66 lines (54 loc) · 1.43 KB
/
signal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include "board.h"
#include "config.h"
static void sigterm_handler(const int signum) {
if (signum == SIGTERM) {
printf("ubnt-fan-speed: Shutting down...\n");
g_board.set_fan_mode(FAN_MODE_AUTO);
abort();
}
}
static void sigterm_init(void) {
struct sigaction new_action;
struct sigaction old_action;
new_action.sa_handler = sigterm_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGTERM, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN) {
sigaction(SIGTERM, &new_action, NULL);
}
}
static void sighup_handler(const int signum) {
char buffer[256] = {0};
if (signum == SIGHUP) {
int read = readlink("/proc/self/exe", buffer, 256);
if (read == -1) {
// check errno
return;
}
char* newargv[] = { buffer, "--config", (char*)g_config.path, NULL };
printf("Restarting...\n");
execv(buffer, newargv);
printf("Error %d %s!\n", errno, buffer);
}
}
static void sighup_init(void) {
struct sigaction new_action;
struct sigaction old_action;
new_action.sa_handler = sighup_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = SA_NODEFER;
sigaction(SIGHUP, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN) {
sigaction(SIGHUP, &new_action, NULL);
}
}
void signal_init(void) {
sigterm_init();
sighup_init();
}