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,4 +101,6 @@ int pyfcgi_monitor_parse_sock(const char*, int[3], pyfcgi_monitor_addr_t*);
101 101
 int pyfcgi_monitor_parse_inet_addr(const char*, int, pyfcgi_monitor_addr_t*,
102 102
 	int*);
103 103
 
104
+void pyfcgi_monitor_sighandler(int signum);
105
+
104 106
 #endif

+ 28
- 0
src/monitor.c View File

@@ -14,6 +14,16 @@ static void clean_exit(int status)
14 14
 pid_t pyfcgi_spawn_monitor()
15 15
 {
16 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 28
 	if(!PyFCGI_conf.mon_socket)
19 29
 	{
@@ -33,6 +43,17 @@ pid_t pyfcgi_spawn_monitor()
33 43
 	if(!res)
34 44
 	{ //child process
35 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 57
 		pyfcgi_monitor_loop();
37 58
 		pyfcgi_log(LOG_ALERT, "Monitor loop should never return but just did it...");
38 59
 		clean_exit(PYFCGI_FATAL);
@@ -331,3 +352,10 @@ free_err:
331 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