Browse Source

Fix #28 add a watchdog for the stats server

Yann Weber 4 years ago
parent
commit
26f05d869b
2 changed files with 23 additions and 0 deletions
  1. 1
    0
      include/monitor.h
  2. 22
    0
      src/monitor.c

+ 1
- 0
include/monitor.h View File

114
 	int*);
114
 	int*);
115
 
115
 
116
 void pyfcgi_monitor_sighandler(int signum);
116
 void pyfcgi_monitor_sighandler(int signum);
117
+void pyfcgi_monitor_timeout(int signum);
117
 
118
 
118
 #endif
119
 #endif

+ 22
- 0
src/monitor.c View File

4
 
4
 
5
 static void clean_exit(int status)
5
 static void clean_exit(int status)
6
 {
6
 {
7
+	pyfcgi_wd_stop();
7
 	if(pyfcgi_mon.sockserv)
8
 	if(pyfcgi_mon.sockserv)
8
 	{
9
 	{
9
 		if(pyfcgi_mon.sockcli)
10
 		if(pyfcgi_mon.sockcli)
38
 {
39
 {
39
 	pid_t res;
40
 	pid_t res;
40
 	struct sigaction act, actdrop;
41
 	struct sigaction act, actdrop;
42
+	struct timespec wd_delay;
41
 	int err;
43
 	int err;
42
 
44
 
45
+	wd_delay.tv_sec = 0;
46
+	wd_delay.tv_nsec = 250000000; //0.25s
47
+
43
 	act.sa_handler = pyfcgi_monitor_sighandler;
48
 	act.sa_handler = pyfcgi_monitor_sighandler;
44
 	sigemptyset(&act.sa_mask);
49
 	sigemptyset(&act.sa_mask);
45
 	act.sa_flags = 0;
50
 	act.sa_flags = 0;
85
 			pyfcgi_log(LOG_WARNING, "Unable to restore ALARM sigaction : %s",
90
 			pyfcgi_log(LOG_WARNING, "Unable to restore ALARM sigaction : %s",
86
 				strerror(errno));
91
 				strerror(errno));
87
 		}
92
 		}
93
+
94
+		if(pyfcgi_wd_init(pyfcgi_monitor_timeout, &wd_delay) < 0)
95
+		{
96
+			pyfcgi_log(LOG_ALERT,
97
+				"Unable to start watchdog, sleep 1s then exiting...");
98
+			sleep(1);
99
+			exit(PYFCGI_FATAL);
100
+		}
101
+
88
 		pyfcgi_monitor_loop();
102
 		pyfcgi_monitor_loop();
89
 		pyfcgi_log(LOG_ALERT, "Monitor loop should never return but just did it...");
103
 		pyfcgi_log(LOG_ALERT, "Monitor loop should never return but just did it...");
90
 		clean_exit(PYFCGI_FATAL);
104
 		clean_exit(PYFCGI_FATAL);
208
 	{
222
 	{
209
 		sockcli = pyfcgi_mon.sockcli = accept(pyfcgi_mon.sockserv,
223
 		sockcli = pyfcgi_mon.sockcli = accept(pyfcgi_mon.sockserv,
210
 			(struct sockaddr*)&cliaddr, &addrlen);
224
 			(struct sockaddr*)&cliaddr, &addrlen);
225
+		pyfcgi_wd_arm(); //watchdog
211
 		if(sockcli < 0)
226
 		if(sockcli < 0)
212
 		{
227
 		{
213
 			err = errno;
228
 			err = errno;
243
 		}
258
 		}
244
 		shutdown(sockcli, SHUT_RDWR);
259
 		shutdown(sockcli, SHUT_RDWR);
245
 		close(sockcli);
260
 		close(sockcli);
261
+		pyfcgi_wd_pause(); //watchdog
246
 		pyfcgi_mon.sockcli = 0;
262
 		pyfcgi_mon.sockcli = 0;
247
 		
263
 		
248
 		addrlen = sizeof(cliaddr);
264
 		addrlen = sizeof(cliaddr);
451
 	clean_exit(0);
467
 	clean_exit(0);
452
 }
468
 }
453
 
469
 
470
+void pyfcgi_monitor_timeout(int signum)
471
+{
472
+	pyfcgi_log(LOG_ALERT, "Monitor server timeout, killed by watchdog");
473
+	clean_exit(PYFCGI_TIMEOUT);
474
+}
475
+

Loading…
Cancel
Save