0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_channel.h>
|
|
11
|
|
12
|
|
13 static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n,
|
50
|
14 ngx_int_t type);
|
16
|
15 static void ngx_start_garbage_collector(ngx_cycle_t *cycle, ngx_int_t type);
|
0
|
16 static void ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo);
|
340
|
17 static ngx_uint_t ngx_reap_children(ngx_cycle_t *cycle);
|
122
|
18 static void ngx_master_process_exit(ngx_cycle_t *cycle);
|
0
|
19 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
|
22
|
20 static void ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority);
|
122
|
21 static void ngx_worker_process_exit(ngx_cycle_t *cycle);
|
0
|
22 static void ngx_channel_handler(ngx_event_t *ev);
|
|
23 #if (NGX_THREADS)
|
|
24 static void ngx_wakeup_worker_threads(ngx_cycle_t *cycle);
|
112
|
25 static ngx_thread_value_t ngx_worker_thread_cycle(void *data);
|
0
|
26 #endif
|
50
|
27 #if 0
|
16
|
28 static void ngx_garbage_collector_cycle(ngx_cycle_t *cycle, void *data);
|
50
|
29 #endif
|
0
|
30
|
|
31
|
|
32 ngx_uint_t ngx_process;
|
|
33 ngx_pid_t ngx_pid;
|
|
34 ngx_uint_t ngx_threaded;
|
|
35
|
|
36 sig_atomic_t ngx_reap;
|
|
37 sig_atomic_t ngx_sigio;
|
|
38 sig_atomic_t ngx_terminate;
|
|
39 sig_atomic_t ngx_quit;
|
26
|
40 sig_atomic_t ngx_debug_quit;
|
0
|
41 ngx_uint_t ngx_exiting;
|
|
42 sig_atomic_t ngx_reconfigure;
|
|
43 sig_atomic_t ngx_reopen;
|
|
44
|
|
45 sig_atomic_t ngx_change_binary;
|
|
46 ngx_pid_t ngx_new_binary;
|
|
47 ngx_uint_t ngx_inherited;
|
|
48 ngx_uint_t ngx_daemonized;
|
|
49
|
|
50 sig_atomic_t ngx_noaccept;
|
|
51 ngx_uint_t ngx_noaccepting;
|
|
52 ngx_uint_t ngx_restart;
|
|
53
|
|
54
|
|
55 #if (NGX_THREADS)
|
|
56 volatile ngx_thread_t ngx_threads[NGX_MAX_THREADS];
|
|
57 ngx_int_t ngx_threads_n;
|
|
58 #endif
|
|
59
|
|
60
|
126
|
61 u_long cpu_affinity;
|
|
62 static u_char master_process[] = "master process";
|
0
|
63
|
|
64
|
332
|
65 static ngx_cycle_t ngx_exit_cycle;
|
|
66 static ngx_log_t ngx_exit_log;
|
|
67 static ngx_open_file_t ngx_exit_log_file;
|
|
68
|
|
69
|
50
|
70 void
|
|
71 ngx_master_process_cycle(ngx_cycle_t *cycle)
|
0
|
72 {
|
|
73 char *title;
|
|
74 u_char *p;
|
|
75 size_t size;
|
|
76 ngx_int_t i;
|
80
|
77 ngx_uint_t n;
|
0
|
78 sigset_t set;
|
|
79 struct itimerval itv;
|
|
80 ngx_uint_t live;
|
|
81 ngx_msec_t delay;
|
80
|
82 ngx_listening_t *ls;
|
0
|
83 ngx_core_conf_t *ccf;
|
|
84
|
|
85 sigemptyset(&set);
|
|
86 sigaddset(&set, SIGCHLD);
|
|
87 sigaddset(&set, SIGALRM);
|
|
88 sigaddset(&set, SIGIO);
|
|
89 sigaddset(&set, SIGINT);
|
|
90 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
|
|
91 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
|
|
92 sigaddset(&set, ngx_signal_value(NGX_NOACCEPT_SIGNAL));
|
|
93 sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL));
|
|
94 sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
|
|
95 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
|
|
96
|
|
97 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
|
|
98 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
99 "sigprocmask() failed");
|
|
100 }
|
|
101
|
|
102 sigemptyset(&set);
|
|
103
|
|
104
|
|
105 size = sizeof(master_process);
|
|
106
|
2
|
107 for (i = 0; i < ngx_argc; i++) {
|
|
108 size += ngx_strlen(ngx_argv[i]) + 1;
|
0
|
109 }
|
|
110
|
|
111 title = ngx_palloc(cycle->pool, size);
|
|
112
|
|
113 p = ngx_cpymem(title, master_process, sizeof(master_process) - 1);
|
2
|
114 for (i = 0; i < ngx_argc; i++) {
|
0
|
115 *p++ = ' ';
|
2
|
116 p = ngx_cpystrn(p, (u_char *) ngx_argv[i], size);
|
0
|
117 }
|
|
118
|
|
119 ngx_setproctitle(title);
|
|
120
|
|
121
|
|
122 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
123
|
|
124 ngx_start_worker_processes(cycle, ccf->worker_processes,
|
|
125 NGX_PROCESS_RESPAWN);
|
16
|
126 ngx_start_garbage_collector(cycle, NGX_PROCESS_RESPAWN);
|
0
|
127
|
|
128 ngx_new_binary = 0;
|
|
129 delay = 0;
|
|
130 live = 1;
|
|
131
|
|
132 for ( ;; ) {
|
|
133 if (delay) {
|
|
134 delay *= 2;
|
|
135
|
|
136 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
137 "temination cycle: %d", delay);
|
|
138
|
|
139 itv.it_interval.tv_sec = 0;
|
|
140 itv.it_interval.tv_usec = 0;
|
|
141 itv.it_value.tv_sec = delay / 1000;
|
|
142 itv.it_value.tv_usec = (delay % 1000 ) * 1000;
|
|
143
|
|
144 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
|
|
145 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
146 "setitimer() failed");
|
|
147 }
|
|
148 }
|
|
149
|
|
150 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "sigsuspend");
|
|
151
|
|
152 sigsuspend(&set);
|
|
153
|
112
|
154 ngx_time_update(0, 0);
|
0
|
155
|
|
156 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "wake up");
|
|
157
|
|
158 if (ngx_reap) {
|
|
159 ngx_reap = 0;
|
340
|
160 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");
|
0
|
161
|
340
|
162 live = ngx_reap_children(cycle);
|
0
|
163 }
|
|
164
|
|
165 if (!live && (ngx_terminate || ngx_quit)) {
|
122
|
166 ngx_master_process_exit(cycle);
|
0
|
167 }
|
|
168
|
|
169 if (ngx_terminate) {
|
|
170 if (delay == 0) {
|
|
171 delay = 50;
|
|
172 }
|
|
173
|
|
174 if (delay > 1000) {
|
|
175 ngx_signal_worker_processes(cycle, SIGKILL);
|
|
176 } else {
|
|
177 ngx_signal_worker_processes(cycle,
|
|
178 ngx_signal_value(NGX_TERMINATE_SIGNAL));
|
|
179 }
|
|
180
|
|
181 continue;
|
|
182 }
|
|
183
|
|
184 if (ngx_quit) {
|
|
185 ngx_signal_worker_processes(cycle,
|
|
186 ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
|
80
|
187
|
|
188 ls = cycle->listening.elts;
|
|
189 for (n = 0; n < cycle->listening.nelts; n++) {
|
|
190 if (ngx_close_socket(ls[n].fd) == -1) {
|
|
191 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
|
|
192 ngx_close_socket_n " %V failed",
|
|
193 &ls[n].addr_text);
|
|
194 }
|
|
195 }
|
|
196 cycle->listening.nelts = 0;
|
|
197
|
0
|
198 continue;
|
|
199 }
|
|
200
|
|
201 if (ngx_reconfigure) {
|
|
202 ngx_reconfigure = 0;
|
|
203
|
|
204 if (ngx_new_binary) {
|
|
205 ngx_start_worker_processes(cycle, ccf->worker_processes,
|
|
206 NGX_PROCESS_RESPAWN);
|
16
|
207 ngx_start_garbage_collector(cycle, NGX_PROCESS_RESPAWN);
|
0
|
208 ngx_noaccepting = 0;
|
|
209
|
|
210 continue;
|
|
211 }
|
|
212
|
26
|
213 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");
|
0
|
214
|
|
215 cycle = ngx_init_cycle(cycle);
|
|
216 if (cycle == NULL) {
|
|
217 cycle = (ngx_cycle_t *) ngx_cycle;
|
|
218 continue;
|
|
219 }
|
|
220
|
|
221 ngx_cycle = cycle;
|
|
222 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
|
|
223 ngx_core_module);
|
|
224 ngx_start_worker_processes(cycle, ccf->worker_processes,
|
|
225 NGX_PROCESS_JUST_RESPAWN);
|
16
|
226 ngx_start_garbage_collector(cycle, NGX_PROCESS_JUST_RESPAWN);
|
0
|
227 live = 1;
|
|
228 ngx_signal_worker_processes(cycle,
|
|
229 ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
|
|
230 }
|
|
231
|
|
232 if (ngx_restart) {
|
|
233 ngx_restart = 0;
|
|
234 ngx_start_worker_processes(cycle, ccf->worker_processes,
|
|
235 NGX_PROCESS_RESPAWN);
|
16
|
236 ngx_start_garbage_collector(cycle, NGX_PROCESS_RESPAWN);
|
0
|
237 live = 1;
|
|
238 }
|
|
239
|
|
240 if (ngx_reopen) {
|
|
241 ngx_reopen = 0;
|
26
|
242 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
|
0
|
243 ngx_reopen_files(cycle, ccf->user);
|
|
244 ngx_signal_worker_processes(cycle,
|
|
245 ngx_signal_value(NGX_REOPEN_SIGNAL));
|
|
246 }
|
|
247
|
|
248 if (ngx_change_binary) {
|
|
249 ngx_change_binary = 0;
|
26
|
250 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "changing binary");
|
2
|
251 ngx_new_binary = ngx_exec_new_binary(cycle, ngx_argv);
|
0
|
252 }
|
|
253
|
|
254 if (ngx_noaccept) {
|
|
255 ngx_noaccept = 0;
|
|
256 ngx_noaccepting = 1;
|
|
257 ngx_signal_worker_processes(cycle,
|
|
258 ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
|
|
259 }
|
|
260 }
|
|
261 }
|
|
262
|
|
263
|
50
|
264 void
|
|
265 ngx_single_process_cycle(ngx_cycle_t *cycle)
|
0
|
266 {
|
|
267 ngx_uint_t i;
|
|
268
|
|
269 ngx_init_temp_number();
|
|
270
|
|
271 for (i = 0; ngx_modules[i]; i++) {
|
|
272 if (ngx_modules[i]->init_process) {
|
|
273 if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
|
|
274 /* fatal */
|
|
275 exit(2);
|
|
276 }
|
|
277 }
|
|
278 }
|
|
279
|
|
280 for ( ;; ) {
|
|
281 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
|
|
282
|
112
|
283 ngx_process_events_and_timers(cycle);
|
0
|
284
|
|
285 if (ngx_terminate || ngx_quit) {
|
120
|
286
|
|
287 for (i = 0; ngx_modules[i]; i++) {
|
|
288 if (ngx_modules[i]->exit_process) {
|
|
289 ngx_modules[i]->exit_process(cycle);
|
|
290 }
|
|
291 }
|
|
292
|
122
|
293 ngx_master_process_exit(cycle);
|
0
|
294 }
|
|
295
|
|
296 if (ngx_reconfigure) {
|
|
297 ngx_reconfigure = 0;
|
26
|
298 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");
|
0
|
299
|
|
300 cycle = ngx_init_cycle(cycle);
|
|
301 if (cycle == NULL) {
|
|
302 cycle = (ngx_cycle_t *) ngx_cycle;
|
|
303 continue;
|
|
304 }
|
|
305
|
|
306 ngx_cycle = cycle;
|
|
307 }
|
|
308
|
|
309 if (ngx_reopen) {
|
|
310 ngx_reopen = 0;
|
26
|
311 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
|
0
|
312 ngx_reopen_files(cycle, (ngx_uid_t) -1);
|
|
313 }
|
|
314 }
|
|
315 }
|
|
316
|
|
317
|
50
|
318 static void
|
|
319 ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n, ngx_int_t type)
|
0
|
320 {
|
126
|
321 ngx_int_t i, s;
|
106
|
322 ngx_channel_t ch;
|
0
|
323
|
26
|
324 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "start worker processes");
|
0
|
325
|
|
326 ch.command = NGX_CMD_OPEN_CHANNEL;
|
|
327
|
126
|
328 for (i = 0; i < n; i++) {
|
|
329
|
|
330 cpu_affinity = ngx_get_cpu_affinity(i);
|
|
331
|
0
|
332 ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
|
|
333 "worker process", type);
|
|
334
|
|
335 ch.pid = ngx_processes[ngx_process_slot].pid;
|
|
336 ch.slot = ngx_process_slot;
|
|
337 ch.fd = ngx_processes[ngx_process_slot].channel[0];
|
|
338
|
126
|
339 for (s = 0; s < ngx_last_process; s++) {
|
0
|
340
|
126
|
341 if (s == ngx_process_slot
|
|
342 || ngx_processes[s].pid == -1
|
|
343 || ngx_processes[s].channel[0] == -1)
|
0
|
344 {
|
|
345 continue;
|
|
346 }
|
|
347
|
|
348 ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
349 "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
|
|
350 ch.slot, ch.pid, ch.fd,
|
126
|
351 s, ngx_processes[s].pid,
|
|
352 ngx_processes[s].channel[0]);
|
0
|
353
|
|
354 /* TODO: NGX_AGAIN */
|
|
355
|
126
|
356 ngx_write_channel(ngx_processes[s].channel[0],
|
0
|
357 &ch, sizeof(ngx_channel_t), cycle->log);
|
|
358 }
|
|
359 }
|
|
360 }
|
|
361
|
|
362
|
50
|
363 static void
|
|
364 ngx_start_garbage_collector(ngx_cycle_t *cycle, ngx_int_t type)
|
16
|
365 {
|
50
|
366 #if 0
|
|
367 ngx_int_t i;
|
|
368 ngx_channel_t ch;
|
16
|
369
|
26
|
370 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "start garbage collector");
|
16
|
371
|
|
372 ch.command = NGX_CMD_OPEN_CHANNEL;
|
|
373
|
|
374 ngx_spawn_process(cycle, ngx_garbage_collector_cycle, NULL,
|
|
375 "garbage collector", type);
|
|
376
|
|
377 ch.pid = ngx_processes[ngx_process_slot].pid;
|
|
378 ch.slot = ngx_process_slot;
|
|
379 ch.fd = ngx_processes[ngx_process_slot].channel[0];
|
|
380
|
|
381 for (i = 0; i < ngx_last_process; i++) {
|
|
382
|
|
383 if (i == ngx_process_slot
|
|
384 || ngx_processes[i].pid == -1
|
|
385 || ngx_processes[i].channel[0] == -1)
|
|
386 {
|
|
387 continue;
|
|
388 }
|
|
389
|
|
390 ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
391 "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
|
|
392 ch.slot, ch.pid, ch.fd,
|
|
393 i, ngx_processes[i].pid,
|
|
394 ngx_processes[i].channel[0]);
|
|
395
|
|
396 /* TODO: NGX_AGAIN */
|
|
397
|
|
398 ngx_write_channel(ngx_processes[i].channel[0],
|
|
399 &ch, sizeof(ngx_channel_t), cycle->log);
|
|
400 }
|
50
|
401 #endif
|
16
|
402 }
|
|
403
|
|
404
|
50
|
405 static void
|
|
406 ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo)
|
0
|
407 {
|
|
408 ngx_int_t i;
|
|
409 ngx_err_t err;
|
|
410 ngx_channel_t ch;
|
|
411
|
342
|
412 #if (NGX_BROKEN_SCM_RIGHTS)
|
|
413
|
|
414 ch.command = 0;
|
|
415
|
|
416 #else
|
|
417
|
0
|
418 switch (signo) {
|
|
419
|
|
420 case ngx_signal_value(NGX_SHUTDOWN_SIGNAL):
|
|
421 ch.command = NGX_CMD_QUIT;
|
|
422 break;
|
|
423
|
|
424 case ngx_signal_value(NGX_TERMINATE_SIGNAL):
|
|
425 ch.command = NGX_CMD_TERMINATE;
|
|
426 break;
|
|
427
|
|
428 case ngx_signal_value(NGX_REOPEN_SIGNAL):
|
|
429 ch.command = NGX_CMD_REOPEN;
|
|
430 break;
|
|
431
|
|
432 default:
|
|
433 ch.command = 0;
|
|
434 }
|
|
435
|
342
|
436 #endif
|
|
437
|
0
|
438 ch.fd = -1;
|
|
439
|
|
440
|
|
441 for (i = 0; i < ngx_last_process; i++) {
|
|
442
|
|
443 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
444 "child: %d %P e:%d t:%d d:%d r:%d j:%d",
|
0
|
445 i,
|
|
446 ngx_processes[i].pid,
|
|
447 ngx_processes[i].exiting,
|
|
448 ngx_processes[i].exited,
|
|
449 ngx_processes[i].detached,
|
|
450 ngx_processes[i].respawn,
|
|
451 ngx_processes[i].just_respawn);
|
|
452
|
|
453 if (ngx_processes[i].detached || ngx_processes[i].pid == -1) {
|
|
454 continue;
|
|
455 }
|
|
456
|
|
457 if (ngx_processes[i].just_respawn) {
|
|
458 ngx_processes[i].just_respawn = 0;
|
|
459 continue;
|
|
460 }
|
|
461
|
|
462 if (ngx_processes[i].exiting
|
|
463 && signo == ngx_signal_value(NGX_SHUTDOWN_SIGNAL))
|
|
464 {
|
|
465 continue;
|
|
466 }
|
|
467
|
|
468 if (ch.command) {
|
|
469 if (ngx_write_channel(ngx_processes[i].channel[0],
|
290
|
470 &ch, sizeof(ngx_channel_t), cycle->log)
|
|
471 == NGX_OK)
|
0
|
472 {
|
|
473 if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
|
|
474 ngx_processes[i].exiting = 1;
|
|
475 }
|
|
476
|
|
477 continue;
|
|
478 }
|
|
479 }
|
|
480
|
|
481 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
482 "kill (%P, %d)" , ngx_processes[i].pid, signo);
|
0
|
483
|
|
484 if (kill(ngx_processes[i].pid, signo) == -1) {
|
|
485 err = ngx_errno;
|
|
486 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
10
|
487 "kill(%P, %d) failed",
|
0
|
488 ngx_processes[i].pid, signo);
|
|
489
|
|
490 if (err == NGX_ESRCH) {
|
|
491 ngx_processes[i].exited = 1;
|
|
492 ngx_processes[i].exiting = 0;
|
|
493 ngx_reap = 1;
|
|
494 }
|
|
495
|
|
496 continue;
|
|
497 }
|
|
498
|
|
499 if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
|
|
500 ngx_processes[i].exiting = 1;
|
|
501 }
|
|
502 }
|
|
503 }
|
|
504
|
|
505
|
50
|
506 static ngx_uint_t
|
340
|
507 ngx_reap_children(ngx_cycle_t *cycle)
|
0
|
508 {
|
92
|
509 ngx_int_t i, n;
|
|
510 ngx_uint_t live;
|
|
511 ngx_channel_t ch;
|
|
512 ngx_core_conf_t *ccf;
|
0
|
513
|
|
514 ch.command = NGX_CMD_CLOSE_CHANNEL;
|
|
515 ch.fd = -1;
|
|
516
|
|
517 live = 0;
|
|
518 for (i = 0; i < ngx_last_process; i++) {
|
|
519
|
|
520 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
521 "child: %d %P e:%d t:%d d:%d r:%d j:%d",
|
0
|
522 i,
|
|
523 ngx_processes[i].pid,
|
|
524 ngx_processes[i].exiting,
|
|
525 ngx_processes[i].exited,
|
|
526 ngx_processes[i].detached,
|
|
527 ngx_processes[i].respawn,
|
|
528 ngx_processes[i].just_respawn);
|
|
529
|
|
530 if (ngx_processes[i].pid == -1) {
|
|
531 continue;
|
|
532 }
|
|
533
|
|
534 if (ngx_processes[i].exited) {
|
|
535
|
|
536 if (!ngx_processes[i].detached) {
|
|
537 ngx_close_channel(ngx_processes[i].channel, cycle->log);
|
|
538
|
|
539 ngx_processes[i].channel[0] = -1;
|
|
540 ngx_processes[i].channel[1] = -1;
|
|
541
|
|
542 ch.pid = ngx_processes[i].pid;
|
|
543 ch.slot = i;
|
|
544
|
|
545 for (n = 0; n < ngx_last_process; n++) {
|
|
546 if (ngx_processes[n].exited
|
|
547 || ngx_processes[n].pid == -1
|
|
548 || ngx_processes[n].channel[0] == -1)
|
|
549 {
|
|
550 continue;
|
|
551 }
|
|
552
|
|
553 ngx_log_debug3(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
554 "pass close channel s:%i pid:%P to:%P",
|
|
555 ch.slot, ch.pid, ngx_processes[n].pid);
|
0
|
556
|
|
557 /* TODO: NGX_AGAIN */
|
|
558
|
|
559 ngx_write_channel(ngx_processes[n].channel[0],
|
|
560 &ch, sizeof(ngx_channel_t), cycle->log);
|
|
561 }
|
|
562 }
|
|
563
|
|
564 if (ngx_processes[i].respawn
|
|
565 && !ngx_processes[i].exiting
|
|
566 && !ngx_terminate
|
|
567 && !ngx_quit)
|
|
568 {
|
|
569 if (ngx_spawn_process(cycle, ngx_processes[i].proc,
|
|
570 ngx_processes[i].data,
|
|
571 ngx_processes[i].name, i)
|
92
|
572 == NGX_INVALID_PID)
|
0
|
573 {
|
|
574 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
575 "can not respawn %s", ngx_processes[i].name);
|
|
576 continue;
|
|
577 }
|
|
578
|
22
|
579
|
|
580 ch.command = NGX_CMD_OPEN_CHANNEL;
|
|
581 ch.pid = ngx_processes[ngx_process_slot].pid;
|
|
582 ch.slot = ngx_process_slot;
|
|
583 ch.fd = ngx_processes[ngx_process_slot].channel[0];
|
|
584
|
|
585 for (n = 0; n < ngx_last_process; n++) {
|
|
586
|
|
587 if (n == ngx_process_slot
|
|
588 || ngx_processes[n].pid == -1
|
|
589 || ngx_processes[n].channel[0] == -1)
|
|
590 {
|
|
591 continue;
|
|
592 }
|
|
593
|
|
594 ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
595 "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
|
|
596 ch.slot, ch.pid, ch.fd,
|
|
597 n, ngx_processes[n].pid,
|
|
598 ngx_processes[n].channel[0]);
|
|
599
|
|
600 /* TODO: NGX_AGAIN */
|
|
601
|
|
602 ngx_write_channel(ngx_processes[n].channel[0],
|
|
603 &ch, sizeof(ngx_channel_t), cycle->log);
|
|
604 }
|
|
605
|
0
|
606 live = 1;
|
|
607
|
|
608 continue;
|
|
609 }
|
|
610
|
|
611 if (ngx_processes[i].pid == ngx_new_binary) {
|
92
|
612
|
|
613 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
|
|
614 ngx_core_module);
|
|
615
|
|
616 if (ngx_rename_file((char *) ccf->oldpid.data,
|
|
617 (char *) ccf->pid.data)
|
|
618 != NGX_OK)
|
|
619 {
|
|
620 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
621 ngx_rename_file_n " %s back to %s failed "
|
|
622 "after the new binary process \"%s\" exited",
|
|
623 ccf->oldpid.data, ccf->pid.data, ngx_argv[0]);
|
|
624 }
|
|
625
|
0
|
626 ngx_new_binary = 0;
|
|
627 if (ngx_noaccepting) {
|
|
628 ngx_restart = 1;
|
|
629 ngx_noaccepting = 0;
|
|
630 }
|
|
631 }
|
|
632
|
|
633 if (i == ngx_last_process - 1) {
|
|
634 ngx_last_process--;
|
|
635
|
|
636 } else {
|
|
637 ngx_processes[i].pid = -1;
|
|
638 }
|
|
639
|
|
640 } else if (ngx_processes[i].exiting || !ngx_processes[i].detached) {
|
|
641 live = 1;
|
|
642 }
|
|
643 }
|
|
644
|
|
645 return live;
|
|
646 }
|
|
647
|
|
648
|
50
|
649 static void
|
122
|
650 ngx_master_process_exit(ngx_cycle_t *cycle)
|
0
|
651 {
|
120
|
652 ngx_uint_t i;
|
|
653
|
0
|
654 ngx_delete_pidfile(cycle);
|
|
655
|
26
|
656 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exit");
|
0
|
657
|
120
|
658 for (i = 0; ngx_modules[i]; i++) {
|
|
659 if (ngx_modules[i]->exit_master) {
|
|
660 ngx_modules[i]->exit_master(cycle);
|
|
661 }
|
|
662 }
|
|
663
|
54
|
664 /*
|
332
|
665 * Copy ngx_cycle->log related data to the special static exit cycle,
|
|
666 * log, and log file structures enough to allow a signal handler to log.
|
|
667 * The handler may be called when standard ngx_cycle->log allocated from
|
|
668 * ngx_cycle->pool is already destroyed.
|
54
|
669 */
|
|
670
|
332
|
671 ngx_exit_log_file.fd = ngx_cycle->log->file->fd;
|
|
672
|
|
673 ngx_exit_log = *ngx_cycle->log;
|
|
674 ngx_exit_log.file = &ngx_exit_log_file;
|
|
675
|
|
676 ngx_exit_cycle.log = &ngx_exit_log;
|
|
677 ngx_cycle = &ngx_exit_cycle;
|
|
678
|
0
|
679 ngx_destroy_pool(cycle->pool);
|
|
680
|
|
681 exit(0);
|
|
682 }
|
|
683
|
|
684
|
50
|
685 static void
|
|
686 ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
|
0
|
687 {
|
290
|
688 ngx_uint_t i;
|
|
689 ngx_connection_t *c;
|
50
|
690 #if (NGX_THREADS)
|
18
|
691 ngx_int_t n;
|
|
692 ngx_err_t err;
|
|
693 ngx_core_conf_t *ccf;
|
50
|
694 #endif
|
18
|
695
|
22
|
696 ngx_worker_process_init(cycle, 1);
|
0
|
697
|
|
698 ngx_setproctitle("worker process");
|
|
699
|
|
700 #if (NGX_THREADS)
|
|
701
|
18
|
702 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
703
|
0
|
704 if (ngx_threads_n) {
|
290
|
705 if (ngx_init_threads(ngx_threads_n, ccf->thread_stack_size, cycle)
|
|
706 == NGX_ERROR)
|
0
|
707 {
|
|
708 /* fatal */
|
|
709 exit(2);
|
|
710 }
|
|
711
|
|
712 err = ngx_thread_key_create(&ngx_core_tls_key);
|
|
713 if (err != 0) {
|
|
714 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
715 ngx_thread_key_create_n " failed");
|
|
716 /* fatal */
|
|
717 exit(2);
|
|
718 }
|
|
719
|
|
720 for (n = 0; n < ngx_threads_n; n++) {
|
|
721
|
50
|
722 ngx_threads[n].cv = ngx_cond_init(cycle->log);
|
|
723
|
|
724 if (ngx_threads[n].cv == NULL) {
|
0
|
725 /* fatal */
|
|
726 exit(2);
|
|
727 }
|
|
728
|
|
729 if (ngx_create_thread((ngx_tid_t *) &ngx_threads[n].tid,
|
|
730 ngx_worker_thread_cycle,
|
290
|
731 (void *) &ngx_threads[n], cycle->log)
|
|
732 != 0)
|
0
|
733 {
|
|
734 /* fatal */
|
|
735 exit(2);
|
|
736 }
|
|
737 }
|
|
738 }
|
|
739
|
|
740 #endif
|
|
741
|
|
742 for ( ;; ) {
|
290
|
743
|
|
744 if (ngx_exiting) {
|
|
745
|
|
746 c = cycle->connections;
|
|
747
|
|
748 for (i = 0; i < cycle->connection_n; i++) {
|
|
749
|
|
750 /* THREAD: lock */
|
0
|
751
|
290
|
752 if (c[i].fd != -1 && c[i].idle) {
|
|
753 c[i].close = 1;
|
|
754 c[i].read->handler(c[i].read);
|
|
755 }
|
|
756 }
|
|
757
|
|
758 if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
|
|
759 {
|
|
760 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
|
|
761
|
|
762 ngx_worker_process_exit(cycle);
|
|
763 }
|
0
|
764 }
|
|
765
|
|
766 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
|
|
767
|
112
|
768 ngx_process_events_and_timers(cycle);
|
0
|
769
|
|
770 if (ngx_terminate) {
|
26
|
771 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
|
0
|
772
|
122
|
773 ngx_worker_process_exit(cycle);
|
0
|
774 }
|
|
775
|
|
776 if (ngx_quit) {
|
|
777 ngx_quit = 0;
|
26
|
778 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
|
0
|
779 "gracefully shutting down");
|
|
780 ngx_setproctitle("worker process is shutting down");
|
|
781
|
|
782 if (!ngx_exiting) {
|
|
783 ngx_close_listening_sockets(cycle);
|
|
784 ngx_exiting = 1;
|
|
785 }
|
|
786 }
|
|
787
|
|
788 if (ngx_reopen) {
|
|
789 ngx_reopen = 0;
|
26
|
790 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
|
0
|
791 ngx_reopen_files(cycle, -1);
|
|
792 }
|
|
793 }
|
|
794 }
|
|
795
|
|
796
|
50
|
797 static void
|
|
798 ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority)
|
16
|
799 {
|
278
|
800 sigset_t set;
|
|
801 ngx_int_t n;
|
|
802 ngx_uint_t i;
|
|
803 struct rlimit rlmt;
|
|
804 ngx_core_conf_t *ccf;
|
|
805 ngx_listening_t *ls;
|
16
|
806
|
|
807 ngx_process = NGX_PROCESS_WORKER;
|
|
808
|
278
|
809 if (ngx_set_environment(cycle, NULL) == NULL) {
|
|
810 /* fatal */
|
|
811 exit(2);
|
|
812 }
|
|
813
|
16
|
814 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
815
|
328
|
816 if (priority && ccf->priority != 0) {
|
|
817 if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1) {
|
|
818 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
819 "setpriority(%d) failed", ccf->priority);
|
22
|
820 }
|
328
|
821 }
|
22
|
822
|
328
|
823 if (ccf->rlimit_nofile != NGX_CONF_UNSET) {
|
|
824 rlmt.rlim_cur = (rlim_t) ccf->rlimit_nofile;
|
|
825 rlmt.rlim_max = (rlim_t) ccf->rlimit_nofile;
|
92
|
826
|
328
|
827 if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
|
|
828 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
829 "setrlimit(RLIMIT_NOFILE, %i) failed",
|
|
830 ccf->rlimit_nofile);
|
92
|
831 }
|
328
|
832 }
|
92
|
833
|
328
|
834 if (ccf->rlimit_core != NGX_CONF_UNSET_SIZE) {
|
|
835 rlmt.rlim_cur = (rlim_t) ccf->rlimit_core;
|
|
836 rlmt.rlim_max = (rlim_t) ccf->rlimit_core;
|
138
|
837
|
328
|
838 if (setrlimit(RLIMIT_CORE, &rlmt) == -1) {
|
|
839 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
840 "setrlimit(RLIMIT_CORE, %i) failed",
|
|
841 ccf->rlimit_core);
|
138
|
842 }
|
328
|
843 }
|
138
|
844
|
92
|
845 #ifdef RLIMIT_SIGPENDING
|
328
|
846 if (ccf->rlimit_sigpending != NGX_CONF_UNSET) {
|
|
847 rlmt.rlim_cur = (rlim_t) ccf->rlimit_sigpending;
|
|
848 rlmt.rlim_max = (rlim_t) ccf->rlimit_sigpending;
|
92
|
849
|
328
|
850 if (setrlimit(RLIMIT_SIGPENDING, &rlmt) == -1) {
|
|
851 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
852 "setrlimit(RLIMIT_SIGPENDING, %i) failed",
|
|
853 ccf->rlimit_sigpending);
|
92
|
854 }
|
328
|
855 }
|
92
|
856 #endif
|
|
857
|
328
|
858 if (geteuid() == 0) {
|
16
|
859 if (setgid(ccf->group) == -1) {
|
|
860 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
861 "setgid(%d) failed", ccf->group);
|
|
862 /* fatal */
|
|
863 exit(2);
|
|
864 }
|
|
865
|
22
|
866 if (initgroups(ccf->username, ccf->group) == -1) {
|
|
867 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
868 "initgroups(%s, %d) failed",
|
|
869 ccf->username, ccf->group);
|
|
870 }
|
|
871
|
16
|
872 if (setuid(ccf->user) == -1) {
|
|
873 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
874 "setuid(%d) failed", ccf->user);
|
|
875 /* fatal */
|
|
876 exit(2);
|
|
877 }
|
|
878 }
|
|
879
|
126
|
880 #if (NGX_HAVE_SCHED_SETAFFINITY)
|
|
881
|
|
882 if (cpu_affinity) {
|
|
883 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
|
|
884 "sched_setaffinity(0x%08Xl)", cpu_affinity);
|
|
885
|
|
886 if (sched_setaffinity(0, 32, (cpu_set_t *) &cpu_affinity) == -1) {
|
|
887 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
888 "sched_setaffinity(0x%08Xl) failed", cpu_affinity);
|
|
889 }
|
|
890 }
|
|
891
|
|
892 #endif
|
|
893
|
16
|
894 #if (NGX_HAVE_PR_SET_DUMPABLE)
|
|
895
|
|
896 /* allow coredump after setuid() in Linux 2.4.x */
|
|
897
|
|
898 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
|
|
899 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
900 "prctl(PR_SET_DUMPABLE) failed");
|
|
901 }
|
|
902
|
|
903 #endif
|
|
904
|
70
|
905 if (ccf->working_directory.len) {
|
72
|
906 if (chdir((char *) ccf->working_directory.data) == -1) {
|
70
|
907 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
908 "chdir(\"%s\") failed", ccf->working_directory.data);
|
|
909 /* fatal */
|
|
910 exit(2);
|
|
911 }
|
|
912 }
|
|
913
|
16
|
914 sigemptyset(&set);
|
|
915
|
|
916 if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
|
|
917 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
918 "sigprocmask() failed");
|
|
919 }
|
|
920
|
|
921 ngx_init_temp_number();
|
|
922
|
|
923 /*
|
|
924 * disable deleting previous events for the listening sockets because
|
|
925 * in the worker processes there are no events at all at this point
|
126
|
926 */
|
16
|
927 ls = cycle->listening.elts;
|
|
928 for (i = 0; i < cycle->listening.nelts; i++) {
|
92
|
929 ls[i].previous = NULL;
|
16
|
930 }
|
|
931
|
|
932 for (i = 0; ngx_modules[i]; i++) {
|
|
933 if (ngx_modules[i]->init_process) {
|
|
934 if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
|
|
935 /* fatal */
|
|
936 exit(2);
|
|
937 }
|
|
938 }
|
|
939 }
|
|
940
|
|
941 for (n = 0; n < ngx_last_process; n++) {
|
|
942
|
|
943 if (ngx_processes[n].pid == -1) {
|
|
944 continue;
|
|
945 }
|
|
946
|
|
947 if (n == ngx_process_slot) {
|
|
948 continue;
|
|
949 }
|
|
950
|
|
951 if (ngx_processes[n].channel[1] == -1) {
|
|
952 continue;
|
|
953 }
|
|
954
|
|
955 if (close(ngx_processes[n].channel[1]) == -1) {
|
|
956 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
957 "close() channel failed");
|
|
958 }
|
|
959 }
|
|
960
|
|
961 if (close(ngx_processes[ngx_process_slot].channel[0]) == -1) {
|
|
962 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
963 "close() channel failed");
|
|
964 }
|
|
965
|
|
966 #if 0
|
|
967 ngx_last_process = 0;
|
|
968 #endif
|
|
969
|
|
970 if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT,
|
290
|
971 ngx_channel_handler)
|
|
972 == NGX_ERROR)
|
16
|
973 {
|
|
974 /* fatal */
|
|
975 exit(2);
|
|
976 }
|
|
977 }
|
|
978
|
|
979
|
50
|
980 static void
|
122
|
981 ngx_worker_process_exit(ngx_cycle_t *cycle)
|
|
982 {
|
|
983 ngx_uint_t i;
|
|
984 ngx_connection_t *c;
|
|
985
|
|
986 #if (NGX_THREADS)
|
|
987 ngx_terminate = 1;
|
|
988
|
|
989 ngx_wakeup_worker_threads(cycle);
|
|
990 #endif
|
|
991
|
|
992 for (i = 0; ngx_modules[i]; i++) {
|
|
993 if (ngx_modules[i]->exit_process) {
|
|
994 ngx_modules[i]->exit_process(cycle);
|
|
995 }
|
|
996 }
|
|
997
|
136
|
998 if (ngx_quit) {
|
|
999 c = cycle->connections;
|
|
1000 for (i = 0; i < cycle->connection_n; i++) {
|
|
1001 if (c[i].fd != -1
|
|
1002 && c[i].read
|
|
1003 && !c[i].read->accept
|
|
1004 && !c[i].read->channel)
|
|
1005 {
|
|
1006 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
1007 "open socket #%d left in %ui connection, "
|
|
1008 "aborting",
|
|
1009 c[i].fd, i);
|
|
1010 ngx_debug_point();
|
|
1011 }
|
122
|
1012 }
|
|
1013
|
136
|
1014 if (ngx_debug_quit) {
|
|
1015 ngx_debug_point();
|
|
1016 }
|
122
|
1017 }
|
|
1018
|
|
1019 /*
|
332
|
1020 * Copy ngx_cycle->log related data to the special static exit cycle,
|
|
1021 * log, and log file structures enough to allow a signal handler to log.
|
|
1022 * The handler may be called when standard ngx_cycle->log allocated from
|
|
1023 * ngx_cycle->pool is already destroyed.
|
122
|
1024 */
|
|
1025
|
332
|
1026 ngx_exit_log_file.fd = ngx_cycle->log->file->fd;
|
|
1027
|
|
1028 ngx_exit_log = *ngx_cycle->log;
|
|
1029 ngx_exit_log.file = &ngx_exit_log_file;
|
|
1030
|
|
1031 ngx_exit_cycle.log = &ngx_exit_log;
|
|
1032 ngx_cycle = &ngx_exit_cycle;
|
|
1033
|
122
|
1034 ngx_destroy_pool(cycle->pool);
|
332
|
1035
|
|
1036 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "exit");
|
122
|
1037
|
|
1038 exit(0);
|
|
1039 }
|
|
1040
|
|
1041
|
|
1042 static void
|
50
|
1043 ngx_channel_handler(ngx_event_t *ev)
|
0
|
1044 {
|
|
1045 ngx_int_t n;
|
|
1046 ngx_channel_t ch;
|
|
1047 ngx_connection_t *c;
|
|
1048
|
16
|
1049 if (ev->timedout) {
|
|
1050 ev->timedout = 0;
|
|
1051 return;
|
|
1052 }
|
|
1053
|
0
|
1054 c = ev->data;
|
|
1055
|
|
1056 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel handler");
|
|
1057
|
|
1058 n = ngx_read_channel(c->fd, &ch, sizeof(ngx_channel_t), ev->log);
|
|
1059
|
10
|
1060 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel: %i", n);
|
0
|
1061
|
10
|
1062 if (n == NGX_ERROR) {
|
342
|
1063 ngx_close_connection(c);
|
10
|
1064 return;
|
|
1065 }
|
|
1066
|
290
|
1067 if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {
|
|
1068 if (ngx_add_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
|
|
1069 return;
|
|
1070 }
|
|
1071 }
|
|
1072
|
10
|
1073 if (n == NGX_AGAIN) {
|
0
|
1074 return;
|
|
1075 }
|
|
1076
|
|
1077 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,
|
|
1078 "channel command: %d", ch.command);
|
|
1079
|
|
1080 switch (ch.command) {
|
|
1081
|
|
1082 case NGX_CMD_QUIT:
|
|
1083 ngx_quit = 1;
|
|
1084 break;
|
|
1085
|
|
1086 case NGX_CMD_TERMINATE:
|
|
1087 ngx_terminate = 1;
|
|
1088 break;
|
|
1089
|
|
1090 case NGX_CMD_REOPEN:
|
|
1091 ngx_reopen = 1;
|
|
1092 break;
|
|
1093
|
|
1094 case NGX_CMD_OPEN_CHANNEL:
|
|
1095
|
|
1096 ngx_log_debug3(NGX_LOG_DEBUG_CORE, ev->log, 0,
|
10
|
1097 "get channel s:%i pid:%P fd:%d", ch.slot, ch.pid, ch.fd);
|
0
|
1098
|
|
1099 ngx_processes[ch.slot].pid = ch.pid;
|
|
1100 ngx_processes[ch.slot].channel[0] = ch.fd;
|
|
1101 break;
|
|
1102
|
|
1103 case NGX_CMD_CLOSE_CHANNEL:
|
|
1104
|
|
1105 ngx_log_debug4(NGX_LOG_DEBUG_CORE, ev->log, 0,
|
10
|
1106 "close channel s:%i pid:%P our:%P fd:%d",
|
0
|
1107 ch.slot, ch.pid, ngx_processes[ch.slot].pid,
|
|
1108 ngx_processes[ch.slot].channel[0]);
|
|
1109
|
|
1110 if (close(ngx_processes[ch.slot].channel[0]) == -1) {
|
16
|
1111 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
|
|
1112 "close() channel failed");
|
0
|
1113 }
|
|
1114
|
|
1115 ngx_processes[ch.slot].channel[0] = -1;
|
|
1116 break;
|
|
1117 }
|
|
1118 }
|
|
1119
|
|
1120
|
|
1121 #if (NGX_THREADS)
|
|
1122
|
50
|
1123 static void
|
|
1124 ngx_wakeup_worker_threads(ngx_cycle_t *cycle)
|
0
|
1125 {
|
|
1126 ngx_int_t i;
|
|
1127 ngx_uint_t live;
|
|
1128
|
|
1129 for ( ;; ) {
|
|
1130
|
|
1131 live = 0;
|
|
1132
|
|
1133 for (i = 0; i < ngx_threads_n; i++) {
|
|
1134 if (ngx_threads[i].state < NGX_THREAD_EXIT) {
|
18
|
1135 if (ngx_cond_signal(ngx_threads[i].cv) == NGX_ERROR) {
|
|
1136 ngx_threads[i].state = NGX_THREAD_DONE;
|
0
|
1137
|
|
1138 } else {
|
|
1139 live = 1;
|
|
1140 }
|
|
1141 }
|
|
1142
|
|
1143 if (ngx_threads[i].state == NGX_THREAD_EXIT) {
|
|
1144 ngx_thread_join(ngx_threads[i].tid, NULL);
|
|
1145 ngx_threads[i].state = NGX_THREAD_DONE;
|
|
1146 }
|
|
1147 }
|
|
1148
|
|
1149 if (live == 0) {
|
|
1150 ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
1151 "all worker threads are joined");
|
|
1152
|
|
1153 /* STUB */
|
|
1154 ngx_done_events(cycle);
|
|
1155 ngx_mutex_destroy(ngx_event_timer_mutex);
|
|
1156 ngx_mutex_destroy(ngx_posted_events_mutex);
|
|
1157
|
|
1158 return;
|
|
1159 }
|
|
1160
|
|
1161 ngx_sched_yield();
|
|
1162 }
|
|
1163 }
|
|
1164
|
|
1165
|
112
|
1166 static ngx_thread_value_t
|
50
|
1167 ngx_worker_thread_cycle(void *data)
|
0
|
1168 {
|
|
1169 ngx_thread_t *thr = data;
|
|
1170
|
|
1171 sigset_t set;
|
|
1172 ngx_err_t err;
|
|
1173 ngx_core_tls_t *tls;
|
|
1174 ngx_cycle_t *cycle;
|
|
1175
|
|
1176 cycle = (ngx_cycle_t *) ngx_cycle;
|
|
1177
|
|
1178 sigemptyset(&set);
|
|
1179 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
|
|
1180 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
|
|
1181 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
|
|
1182
|
|
1183 err = ngx_thread_sigmask(SIG_BLOCK, &set, NULL);
|
|
1184 if (err) {
|
|
1185 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
1186 ngx_thread_sigmask_n " failed");
|
112
|
1187 return (ngx_thread_value_t) 1;
|
0
|
1188 }
|
|
1189
|
|
1190 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
1191 "thread " NGX_TID_T_FMT " started", ngx_thread_self());
|
0
|
1192
|
|
1193 ngx_setthrtitle("worker thread");
|
|
1194
|
50
|
1195 tls = ngx_calloc(sizeof(ngx_core_tls_t), cycle->log);
|
|
1196 if (tls == NULL) {
|
112
|
1197 return (ngx_thread_value_t) 1;
|
0
|
1198 }
|
|
1199
|
|
1200 err = ngx_thread_set_tls(ngx_core_tls_key, tls);
|
|
1201 if (err != 0) {
|
|
1202 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
1203 ngx_thread_set_tls_n " failed");
|
112
|
1204 return (ngx_thread_value_t) 1;
|
0
|
1205 }
|
|
1206
|
112
|
1207 ngx_mutex_lock(ngx_posted_events_mutex);
|
0
|
1208
|
|
1209 for ( ;; ) {
|
|
1210 thr->state = NGX_THREAD_FREE;
|
|
1211
|
|
1212 if (ngx_cond_wait(thr->cv, ngx_posted_events_mutex) == NGX_ERROR) {
|
112
|
1213 return (ngx_thread_value_t) 1;
|
0
|
1214 }
|
|
1215
|
|
1216 if (ngx_terminate) {
|
|
1217 thr->state = NGX_THREAD_EXIT;
|
|
1218
|
|
1219 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
1220
|
|
1221 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
1222 "thread " NGX_TID_T_FMT " is done",
|
|
1223 ngx_thread_self());
|
0
|
1224
|
112
|
1225 return (ngx_thread_value_t) 0;
|
0
|
1226 }
|
|
1227
|
|
1228 thr->state = NGX_THREAD_BUSY;
|
|
1229
|
|
1230 if (ngx_event_thread_process_posted(cycle) == NGX_ERROR) {
|
112
|
1231 return (ngx_thread_value_t) 1;
|
0
|
1232 }
|
|
1233
|
|
1234 if (ngx_event_thread_process_posted(cycle) == NGX_ERROR) {
|
112
|
1235 return (ngx_thread_value_t) 1;
|
0
|
1236 }
|
|
1237
|
|
1238 if (ngx_process_changes) {
|
|
1239 if (ngx_process_changes(cycle, 1) == NGX_ERROR) {
|
112
|
1240 return (ngx_thread_value_t) 1;
|
0
|
1241 }
|
|
1242 }
|
|
1243 }
|
|
1244 }
|
|
1245
|
|
1246 #endif
|
16
|
1247
|
|
1248
|
50
|
1249 #if 0
|
|
1250
|
|
1251 static void
|
|
1252 ngx_garbage_collector_cycle(ngx_cycle_t *cycle, void *data)
|
16
|
1253 {
|
|
1254 ngx_uint_t i;
|
|
1255 ngx_gc_t ctx;
|
|
1256 ngx_path_t **path;
|
|
1257 ngx_event_t *ev;
|
|
1258
|
22
|
1259 ngx_worker_process_init(cycle, 0);
|
16
|
1260
|
92
|
1261 ev = &cycle->read_events0[ngx_channel];
|
16
|
1262
|
|
1263 ngx_accept_mutex = NULL;
|
|
1264
|
|
1265 ngx_setproctitle("garbage collector");
|
|
1266
|
|
1267 #if 0
|
|
1268 ngx_add_timer(ev, 60 * 1000);
|
|
1269 #endif
|
|
1270
|
|
1271 for ( ;; ) {
|
|
1272
|
|
1273 if (ngx_terminate || ngx_quit) {
|
26
|
1274 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
|
16
|
1275 exit(0);
|
|
1276 }
|
|
1277
|
|
1278 if (ngx_reopen) {
|
|
1279 ngx_reopen = 0;
|
26
|
1280 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
|
16
|
1281 ngx_reopen_files(cycle, -1);
|
|
1282 }
|
|
1283
|
|
1284 path = cycle->pathes.elts;
|
|
1285 for (i = 0; i < cycle->pathes.nelts; i++) {
|
|
1286 ctx.path = path[i];
|
|
1287 ctx.log = cycle->log;
|
28
|
1288 ctx.handler = path[i]->cleaner;
|
16
|
1289
|
|
1290 ngx_collect_garbage(&ctx, &path[i]->name, 0);
|
|
1291 }
|
|
1292
|
|
1293 ngx_add_timer(ev, 60 * 60 * 1000);
|
|
1294
|
112
|
1295 ngx_process_events_and_timers(cycle);
|
16
|
1296 }
|
|
1297 }
|
50
|
1298
|
|
1299 #endif
|