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