Browse Source

Add sighandler for monitor server

Yann Weber 4 years ago
parent
commit
9e0d3f34f5
2 changed files with 30 additions and 0 deletions
  1. 2
    0
      include/monitor.h
  2. 28
    0
      src/monitor.c

+ 2
- 0
include/monitor.h View File

101
 int pyfcgi_monitor_parse_inet_addr(const char*, int, pyfcgi_monitor_addr_t*,
101
 int pyfcgi_monitor_parse_inet_addr(const char*, int, pyfcgi_monitor_addr_t*,
102
 	int*);
102
 	int*);
103
 
103
 
104
+void pyfcgi_monitor_sighandler(int signum);
105
+
104
 #endif
106
 #endif

+ 28
- 0
src/monitor.c View File

14
 pid_t pyfcgi_spawn_monitor()
14
 pid_t pyfcgi_spawn_monitor()
15
 {
15
 {
16
 	pid_t res;
16
 	pid_t res;
17
+	struct sigaction act;
18
+	const int signums[] = {SIGINT, SIGTERM, SIGALRM, 0};
19
+	const int *signum;
20
+	int err;
21
+
22
+	signum = signums;
23
+	act.sa_handler = pyfcgi_monitor_sighandler;
24
+	sigemptyset(&act.sa_mask);
25
+	act.sa_flags = 0;
26
+	act.sa_restorer = NULL;
17
 
27
 
18
 	if(!PyFCGI_conf.mon_socket)
28
 	if(!PyFCGI_conf.mon_socket)
19
 	{
29
 	{
33
 	if(!res)
43
 	if(!res)
34
 	{ //child process
44
 	{ //child process
35
 		pyfcgi_logger_set_ident("StatServ");
45
 		pyfcgi_logger_set_ident("StatServ");
46
+		while(*signum)
47
+		{
48
+			if(sigaction(*signum, &act, NULL))
49
+			{
50
+				err = errno;
51
+				pyfcgi_log(LOG_ERR, "Unable to run sigaction for %s : %s",
52
+					strsignal(*signum), strerror(err));
53
+				exit(PYFCGI_FATAL);
54
+			}
55
+			signum++;
56
+		}
36
 		pyfcgi_monitor_loop();
57
 		pyfcgi_monitor_loop();
37
 		pyfcgi_log(LOG_ALERT, "Monitor loop should never return but just did it...");
58
 		pyfcgi_log(LOG_ALERT, "Monitor loop should never return but just did it...");
38
 		clean_exit(PYFCGI_FATAL);
59
 		clean_exit(PYFCGI_FATAL);
331
 	return -1;
352
 	return -1;
332
 }
353
 }
333
 
354
 
355
+void pyfcgi_monitor_sighandler(int signum)
356
+{
357
+	pyfcgi_log(LOG_NOTICE, "Received signal %s, exiting",
358
+		strsignal(signum));
359
+	clean_exit(0);
360
+}
361
+

Loading…
Cancel
Save