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