|
@@ -24,7 +24,6 @@ void init_context()
|
24
|
24
|
pyfcgi_semid = 0;
|
25
|
25
|
}
|
26
|
26
|
|
27
|
|
-
|
28
|
27
|
int responder_loop()
|
29
|
28
|
{
|
30
|
29
|
unsigned int n_wrk, wanted_n, n;
|
|
@@ -35,6 +34,19 @@ int responder_loop()
|
35
|
34
|
struct sembuf sop;
|
36
|
35
|
struct timespec timeout;
|
37
|
36
|
short idle;
|
|
37
|
+ struct sigaction act;
|
|
38
|
+
|
|
39
|
+ act.sa_handler = pool_sighandler;
|
|
40
|
+ sigemptyset(&act.sa_mask);
|
|
41
|
+ sigaddset(&act.sa_mask, SIGTERM);
|
|
42
|
+ act.sa_flags = 0;
|
|
43
|
+ act.sa_restorer = NULL;
|
|
44
|
+
|
|
45
|
+ if(sigaction(SIGINT, &act, NULL))
|
|
46
|
+ {
|
|
47
|
+ perror("Sigaction error for pool process");
|
|
48
|
+ exit(PYFCGI_FATAL);
|
|
49
|
+ }
|
38
|
50
|
|
39
|
51
|
sop.sem_num = 0;
|
40
|
52
|
sop.sem_op = 0;
|
|
@@ -48,6 +60,8 @@ int responder_loop()
|
48
|
60
|
|
49
|
61
|
init_context();
|
50
|
62
|
|
|
63
|
+ PyFCGI_conf.context.wrk_pids = &wrk_pids;
|
|
64
|
+ PyFCGI_conf.context.n_wrk = 0;
|
51
|
65
|
wrk_pids = malloc(sizeof(int) * PyFCGI_conf.max_wrk);
|
52
|
66
|
if(!wrk_pids)
|
53
|
67
|
{
|
|
@@ -67,6 +81,7 @@ int responder_loop()
|
67
|
81
|
for(n_wrk=0; n_wrk < wanted_n; n_wrk++)
|
68
|
82
|
{
|
69
|
83
|
wrk_pids[n_wrk] = spawn(n_wrk, semid);
|
|
84
|
+ PyFCGI_conf.context.n_wrk = n_wrk;
|
70
|
85
|
}
|
71
|
86
|
//Wait at least for a process to be ready
|
72
|
87
|
while(!semtimedop(semid, &sop, 1, &timeout));
|
|
@@ -75,6 +90,7 @@ int responder_loop()
|
75
|
90
|
// spawn new one if needed, etc.
|
76
|
91
|
while(1)
|
77
|
92
|
{
|
|
93
|
+ PyFCGI_conf.context.n_wrk = n_wrk;
|
78
|
94
|
if( (ret = waitpid(0, &status, WNOHANG)) )
|
79
|
95
|
{
|
80
|
96
|
if(ret < 0)
|
|
@@ -200,11 +216,12 @@ pyfcgi_log( LOG_DEBUG, "GC want %d have %d", wanted_n, n_wrk);
|
200
|
216
|
}
|
201
|
217
|
|
202
|
218
|
//Debug wait & exit
|
203
|
|
- for(n_wrk=0; n_wrk != PyFCGI_conf.min_wrk; n_wrk++)
|
|
219
|
+ for(; n_wrk != 0; n_wrk--)
|
204
|
220
|
{
|
205
|
221
|
waitpid(wrk_pids[n_wrk], &status, 0);
|
206
|
222
|
pyfcgi_log(LOG_DEBUG, "Child %d stopped with status %d",
|
207
|
223
|
wrk_pids[n_wrk], status);
|
|
224
|
+ PyFCGI_conf.context.n_wrk = n_wrk;
|
208
|
225
|
}
|
209
|
226
|
//printf("Content-Type: text/html\r\n\r\nHello world !\n");
|
210
|
227
|
pyfcgi_log(LOG_INFO,"Child workers stoped, stopping responder");
|
|
@@ -215,9 +232,17 @@ pid_t spawn(int wrk_id, int semid)
|
215
|
232
|
{
|
216
|
233
|
pid_t res;
|
217
|
234
|
struct timespec timeout;
|
|
235
|
+ struct sigaction act;
|
|
236
|
+
|
218
|
237
|
timeout.tv_sec = 0;
|
219
|
238
|
timeout.tv_nsec = 100000000;
|
220
|
|
-
|
|
239
|
+
|
|
240
|
+ act.sa_handler = worker_sighandler;
|
|
241
|
+ sigemptyset(&act.sa_mask);
|
|
242
|
+ sigaddset(&act.sa_mask, SIGTERM);
|
|
243
|
+ act.sa_flags = 0;
|
|
244
|
+ act.sa_restorer = NULL;
|
|
245
|
+
|
221
|
246
|
|
222
|
247
|
res = fork();
|
223
|
248
|
if(res == -1)
|
|
@@ -229,6 +254,12 @@ pid_t spawn(int wrk_id, int semid)
|
229
|
254
|
else if(!res)
|
230
|
255
|
{
|
231
|
256
|
// Child process
|
|
257
|
+ if(sigaction(SIGINT, &act, NULL))
|
|
258
|
+ {
|
|
259
|
+ perror("Sigaction error for pool process");
|
|
260
|
+ exit(PYFCGI_FATAL);
|
|
261
|
+ }
|
|
262
|
+
|
232
|
263
|
if(PyFCGI_conf.pep333)
|
233
|
264
|
{
|
234
|
265
|
exit(work333(wrk_id, semid));
|
|
@@ -276,8 +307,38 @@ void clean_exit(int status)
|
276
|
307
|
{
|
277
|
308
|
if(pyfcgi_semid && semctl(pyfcgi_semid, 0, IPC_RMID) == -1)
|
278
|
309
|
{
|
|
310
|
+ /*silentely fails (when exiting from sighandler)
|
279
|
311
|
pyfcgi_log( LOG_CRIT,
|
280
|
|
- "Unable to delete semaphore before exiting.");
|
|
312
|
+ "Unable to delete semaphore before exiting : %s",
|
|
313
|
+ strerror(errno));
|
|
314
|
+ */
|
281
|
315
|
}
|
282
|
316
|
exit(status);
|
283
|
317
|
}
|
|
318
|
+
|
|
319
|
+void pool_sighandler(int signum)
|
|
320
|
+{
|
|
321
|
+ unsigned int i;
|
|
322
|
+ struct timespec req;
|
|
323
|
+ req.tv_sec = 0;
|
|
324
|
+ req.tv_nsec = 200000000; //0.2s
|
|
325
|
+ if(PyFCGI_conf.context.n_wrk < 1) { clean_exit(0); }
|
|
326
|
+
|
|
327
|
+ for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
|
|
328
|
+ {
|
|
329
|
+ pyfcgi_log(LOG_INFO, "Sending SIGTERM to child #%d (pid %d)",
|
|
330
|
+ i,(*PyFCGI_conf.context.wrk_pids)[i]);
|
|
331
|
+ kill((*PyFCGI_conf.context.wrk_pids)[i], SIGTERM);
|
|
332
|
+ nanosleep(&req, NULL); //waiting 0.2s
|
|
333
|
+ }
|
|
334
|
+ for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
|
|
335
|
+ {
|
|
336
|
+ if(kill((*PyFCGI_conf.context.wrk_pids)[i], SIGCONT))
|
|
337
|
+ {
|
|
338
|
+ pyfcgi_log(LOG_INFO, "Sending SIGKILL to child %d", i);
|
|
339
|
+ kill((*PyFCGI_conf.context.wrk_pids)[i], SIGKILL);
|
|
340
|
+ }
|
|
341
|
+
|
|
342
|
+ }
|
|
343
|
+ clean_exit(0);
|
|
344
|
+}
|