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 }
|
|
374 }
|
|
375
|
|
376
|
50
|
377 static void
|
514
|
378 ngx_start_cache_manager_processes(ngx_cycle_t *cycle, ngx_uint_t respawn)
|
16
|
379 {
|
514
|
380 ngx_uint_t i, manager, loader;
|
464
|
381 ngx_path_t **path;
|
|
382 ngx_channel_t ch;
|
16
|
383
|
514
|
384 manager = 0;
|
|
385 loader = 0;
|
|
386
|
464
|
387 path = ngx_cycle->pathes.elts;
|
514
|
388 for (i = 0; i < ngx_cycle->pathes.nelts; i++) {
|
|
389
|
|
390 if (path[i]->manager) {
|
|
391 manager = 1;
|
|
392 }
|
|
393
|
|
394 if (path[i]->loader) {
|
|
395 loader = 1;
|
464
|
396 }
|
|
397 }
|
|
398
|
514
|
399 if (manager == 0) {
|
|
400 return;
|
|
401 }
|
464
|
402
|
514
|
403 ngx_spawn_process(cycle, ngx_cache_manager_process_cycle,
|
|
404 &ngx_cache_manager_ctx, "cache manager process",
|
|
405 respawn ? NGX_PROCESS_JUST_RESPAWN : NGX_PROCESS_RESPAWN);
|
16
|
406
|
|
407 ch.command = NGX_CMD_OPEN_CHANNEL;
|
|
408 ch.pid = ngx_processes[ngx_process_slot].pid;
|
|
409 ch.slot = ngx_process_slot;
|
|
410 ch.fd = ngx_processes[ngx_process_slot].channel[0];
|
|
411
|
514
|
412 ngx_pass_open_channel(cycle, &ch);
|
|
413
|
|
414 if (loader == 0) {
|
|
415 return;
|
|
416 }
|
|
417
|
|
418 ngx_spawn_process(cycle, ngx_cache_manager_process_cycle,
|
|
419 &ngx_cache_loader_ctx, "cache loader process",
|
|
420 respawn ? NGX_PROCESS_JUST_SPAWN : NGX_PROCESS_NORESPAWN);
|
|
421
|
|
422 ch.command = NGX_CMD_OPEN_CHANNEL;
|
|
423 ch.pid = ngx_processes[ngx_process_slot].pid;
|
|
424 ch.slot = ngx_process_slot;
|
|
425 ch.fd = ngx_processes[ngx_process_slot].channel[0];
|
|
426
|
|
427 ngx_pass_open_channel(cycle, &ch);
|
|
428 }
|
|
429
|
|
430
|
|
431 static void
|
|
432 ngx_pass_open_channel(ngx_cycle_t *cycle, ngx_channel_t *ch)
|
|
433 {
|
|
434 ngx_int_t i;
|
|
435
|
16
|
436 for (i = 0; i < ngx_last_process; i++) {
|
|
437
|
|
438 if (i == ngx_process_slot
|
|
439 || ngx_processes[i].pid == -1
|
|
440 || ngx_processes[i].channel[0] == -1)
|
|
441 {
|
|
442 continue;
|
|
443 }
|
|
444
|
|
445 ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
446 "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
|
514
|
447 ch->slot, ch->pid, ch->fd,
|
16
|
448 i, ngx_processes[i].pid,
|
|
449 ngx_processes[i].channel[0]);
|
|
450
|
|
451 /* TODO: NGX_AGAIN */
|
|
452
|
|
453 ngx_write_channel(ngx_processes[i].channel[0],
|
514
|
454 ch, sizeof(ngx_channel_t), cycle->log);
|
16
|
455 }
|
|
456 }
|
|
457
|
|
458
|
50
|
459 static void
|
|
460 ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo)
|
0
|
461 {
|
|
462 ngx_int_t i;
|
|
463 ngx_err_t err;
|
|
464 ngx_channel_t ch;
|
|
465
|
342
|
466 #if (NGX_BROKEN_SCM_RIGHTS)
|
|
467
|
|
468 ch.command = 0;
|
|
469
|
|
470 #else
|
|
471
|
0
|
472 switch (signo) {
|
|
473
|
|
474 case ngx_signal_value(NGX_SHUTDOWN_SIGNAL):
|
|
475 ch.command = NGX_CMD_QUIT;
|
|
476 break;
|
|
477
|
|
478 case ngx_signal_value(NGX_TERMINATE_SIGNAL):
|
|
479 ch.command = NGX_CMD_TERMINATE;
|
|
480 break;
|
|
481
|
|
482 case ngx_signal_value(NGX_REOPEN_SIGNAL):
|
|
483 ch.command = NGX_CMD_REOPEN;
|
|
484 break;
|
|
485
|
|
486 default:
|
|
487 ch.command = 0;
|
|
488 }
|
|
489
|
342
|
490 #endif
|
|
491
|
0
|
492 ch.fd = -1;
|
|
493
|
|
494
|
|
495 for (i = 0; i < ngx_last_process; i++) {
|
|
496
|
|
497 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
498 "child: %d %P e:%d t:%d d:%d r:%d j:%d",
|
0
|
499 i,
|
|
500 ngx_processes[i].pid,
|
|
501 ngx_processes[i].exiting,
|
|
502 ngx_processes[i].exited,
|
|
503 ngx_processes[i].detached,
|
|
504 ngx_processes[i].respawn,
|
514
|
505 ngx_processes[i].just_spawn);
|
0
|
506
|
|
507 if (ngx_processes[i].detached || ngx_processes[i].pid == -1) {
|
|
508 continue;
|
|
509 }
|
|
510
|
514
|
511 if (ngx_processes[i].just_spawn) {
|
|
512 ngx_processes[i].just_spawn = 0;
|
0
|
513 continue;
|
|
514 }
|
|
515
|
|
516 if (ngx_processes[i].exiting
|
|
517 && signo == ngx_signal_value(NGX_SHUTDOWN_SIGNAL))
|
|
518 {
|
|
519 continue;
|
|
520 }
|
|
521
|
|
522 if (ch.command) {
|
|
523 if (ngx_write_channel(ngx_processes[i].channel[0],
|
290
|
524 &ch, sizeof(ngx_channel_t), cycle->log)
|
|
525 == NGX_OK)
|
0
|
526 {
|
|
527 if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
|
|
528 ngx_processes[i].exiting = 1;
|
|
529 }
|
|
530
|
|
531 continue;
|
|
532 }
|
|
533 }
|
|
534
|
|
535 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
536 "kill (%P, %d)" , ngx_processes[i].pid, signo);
|
0
|
537
|
|
538 if (kill(ngx_processes[i].pid, signo) == -1) {
|
|
539 err = ngx_errno;
|
|
540 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
518
|
541 "kill(%P, %d) failed", ngx_processes[i].pid, signo);
|
0
|
542
|
|
543 if (err == NGX_ESRCH) {
|
|
544 ngx_processes[i].exited = 1;
|
|
545 ngx_processes[i].exiting = 0;
|
|
546 ngx_reap = 1;
|
|
547 }
|
|
548
|
|
549 continue;
|
|
550 }
|
|
551
|
|
552 if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
|
|
553 ngx_processes[i].exiting = 1;
|
|
554 }
|
|
555 }
|
|
556 }
|
|
557
|
|
558
|
50
|
559 static ngx_uint_t
|
340
|
560 ngx_reap_children(ngx_cycle_t *cycle)
|
0
|
561 {
|
92
|
562 ngx_int_t i, n;
|
|
563 ngx_uint_t live;
|
|
564 ngx_channel_t ch;
|
|
565 ngx_core_conf_t *ccf;
|
0
|
566
|
|
567 ch.command = NGX_CMD_CLOSE_CHANNEL;
|
|
568 ch.fd = -1;
|
|
569
|
|
570 live = 0;
|
|
571 for (i = 0; i < ngx_last_process; i++) {
|
|
572
|
|
573 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
10
|
574 "child: %d %P e:%d t:%d d:%d r:%d j:%d",
|
0
|
575 i,
|
|
576 ngx_processes[i].pid,
|
|
577 ngx_processes[i].exiting,
|
|
578 ngx_processes[i].exited,
|
|
579 ngx_processes[i].detached,
|
|
580 ngx_processes[i].respawn,
|
514
|
581 ngx_processes[i].just_spawn);
|
0
|
582
|
|
583 if (ngx_processes[i].pid == -1) {
|
|
584 continue;
|
|
585 }
|
|
586
|
|
587 if (ngx_processes[i].exited) {
|
|
588
|
|
589 if (!ngx_processes[i].detached) {
|
|
590 ngx_close_channel(ngx_processes[i].channel, cycle->log);
|
|
591
|
|
592 ngx_processes[i].channel[0] = -1;
|
|
593 ngx_processes[i].channel[1] = -1;
|
|
594
|
|
595 ch.pid = ngx_processes[i].pid;
|
|
596 ch.slot = i;
|
|
597
|
|
598 for (n = 0; n < ngx_last_process; n++) {
|
|
599 if (ngx_processes[n].exited
|
|
600 || ngx_processes[n].pid == -1
|
|
601 || ngx_processes[n].channel[0] == -1)
|
|
602 {
|
|
603 continue;
|
|
604 }
|
|
605
|
|
606 ngx_log_debug3(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
607 "pass close channel s:%i pid:%P to:%P",
|
|
608 ch.slot, ch.pid, ngx_processes[n].pid);
|
0
|
609
|
|
610 /* TODO: NGX_AGAIN */
|
|
611
|
|
612 ngx_write_channel(ngx_processes[n].channel[0],
|
|
613 &ch, sizeof(ngx_channel_t), cycle->log);
|
|
614 }
|
|
615 }
|
|
616
|
|
617 if (ngx_processes[i].respawn
|
|
618 && !ngx_processes[i].exiting
|
|
619 && !ngx_terminate
|
|
620 && !ngx_quit)
|
|
621 {
|
|
622 if (ngx_spawn_process(cycle, ngx_processes[i].proc,
|
|
623 ngx_processes[i].data,
|
|
624 ngx_processes[i].name, i)
|
92
|
625 == NGX_INVALID_PID)
|
0
|
626 {
|
|
627 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
640
|
628 "could not respawn %s",
|
|
629 ngx_processes[i].name);
|
0
|
630 continue;
|
|
631 }
|
|
632
|
22
|
633
|
|
634 ch.command = NGX_CMD_OPEN_CHANNEL;
|
|
635 ch.pid = ngx_processes[ngx_process_slot].pid;
|
|
636 ch.slot = ngx_process_slot;
|
|
637 ch.fd = ngx_processes[ngx_process_slot].channel[0];
|
|
638
|
514
|
639 ngx_pass_open_channel(cycle, &ch);
|
22
|
640
|
0
|
641 live = 1;
|
|
642
|
|
643 continue;
|
|
644 }
|
|
645
|
|
646 if (ngx_processes[i].pid == ngx_new_binary) {
|
92
|
647
|
|
648 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
|
|
649 ngx_core_module);
|
|
650
|
|
651 if (ngx_rename_file((char *) ccf->oldpid.data,
|
|
652 (char *) ccf->pid.data)
|
|
653 != NGX_OK)
|
|
654 {
|
|
655 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
656 ngx_rename_file_n " %s back to %s failed "
|
|
657 "after the new binary process \"%s\" exited",
|
|
658 ccf->oldpid.data, ccf->pid.data, ngx_argv[0]);
|
|
659 }
|
|
660
|
0
|
661 ngx_new_binary = 0;
|
|
662 if (ngx_noaccepting) {
|
|
663 ngx_restart = 1;
|
|
664 ngx_noaccepting = 0;
|
|
665 }
|
|
666 }
|
|
667
|
|
668 if (i == ngx_last_process - 1) {
|
|
669 ngx_last_process--;
|
|
670
|
|
671 } else {
|
|
672 ngx_processes[i].pid = -1;
|
|
673 }
|
|
674
|
|
675 } else if (ngx_processes[i].exiting || !ngx_processes[i].detached) {
|
|
676 live = 1;
|
|
677 }
|
|
678 }
|
|
679
|
|
680 return live;
|
|
681 }
|
|
682
|
|
683
|
50
|
684 static void
|
122
|
685 ngx_master_process_exit(ngx_cycle_t *cycle)
|
0
|
686 {
|
120
|
687 ngx_uint_t i;
|
|
688
|
0
|
689 ngx_delete_pidfile(cycle);
|
|
690
|
26
|
691 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exit");
|
0
|
692
|
120
|
693 for (i = 0; ngx_modules[i]; i++) {
|
|
694 if (ngx_modules[i]->exit_master) {
|
|
695 ngx_modules[i]->exit_master(cycle);
|
|
696 }
|
|
697 }
|
|
698
|
538
|
699 ngx_close_listening_sockets(cycle);
|
|
700
|
54
|
701 /*
|
332
|
702 * Copy ngx_cycle->log related data to the special static exit cycle,
|
|
703 * log, and log file structures enough to allow a signal handler to log.
|
|
704 * The handler may be called when standard ngx_cycle->log allocated from
|
|
705 * ngx_cycle->pool is already destroyed.
|
54
|
706 */
|
|
707
|
332
|
708 ngx_exit_log_file.fd = ngx_cycle->log->file->fd;
|
|
709
|
|
710 ngx_exit_log = *ngx_cycle->log;
|
|
711 ngx_exit_log.file = &ngx_exit_log_file;
|
|
712
|
|
713 ngx_exit_cycle.log = &ngx_exit_log;
|
678
|
714 ngx_exit_cycle.files = ngx_cycle->files;
|
|
715 ngx_exit_cycle.files_n = ngx_cycle->files_n;
|
332
|
716 ngx_cycle = &ngx_exit_cycle;
|
|
717
|
0
|
718 ngx_destroy_pool(cycle->pool);
|
|
719
|
|
720 exit(0);
|
|
721 }
|
|
722
|
|
723
|
50
|
724 static void
|
|
725 ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
|
0
|
726 {
|
290
|
727 ngx_uint_t i;
|
|
728 ngx_connection_t *c;
|
18
|
729
|
552
|
730 ngx_process = NGX_PROCESS_WORKER;
|
|
731
|
22
|
732 ngx_worker_process_init(cycle, 1);
|
0
|
733
|
|
734 ngx_setproctitle("worker process");
|
|
735
|
|
736 #if (NGX_THREADS)
|
356
|
737 {
|
|
738 ngx_int_t n;
|
|
739 ngx_err_t err;
|
|
740 ngx_core_conf_t *ccf;
|
0
|
741
|
18
|
742 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
743
|
0
|
744 if (ngx_threads_n) {
|
290
|
745 if (ngx_init_threads(ngx_threads_n, ccf->thread_stack_size, cycle)
|
|
746 == NGX_ERROR)
|
0
|
747 {
|
|
748 /* fatal */
|
|
749 exit(2);
|
|
750 }
|
|
751
|
|
752 err = ngx_thread_key_create(&ngx_core_tls_key);
|
|
753 if (err != 0) {
|
|
754 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
755 ngx_thread_key_create_n " failed");
|
|
756 /* fatal */
|
|
757 exit(2);
|
|
758 }
|
|
759
|
|
760 for (n = 0; n < ngx_threads_n; n++) {
|
|
761
|
50
|
762 ngx_threads[n].cv = ngx_cond_init(cycle->log);
|
|
763
|
|
764 if (ngx_threads[n].cv == NULL) {
|
0
|
765 /* fatal */
|
|
766 exit(2);
|
|
767 }
|
|
768
|
|
769 if (ngx_create_thread((ngx_tid_t *) &ngx_threads[n].tid,
|
|
770 ngx_worker_thread_cycle,
|
290
|
771 (void *) &ngx_threads[n], cycle->log)
|
|
772 != 0)
|
0
|
773 {
|
|
774 /* fatal */
|
|
775 exit(2);
|
|
776 }
|
|
777 }
|
|
778 }
|
356
|
779 }
|
0
|
780 #endif
|
|
781
|
|
782 for ( ;; ) {
|
290
|
783
|
|
784 if (ngx_exiting) {
|
|
785
|
|
786 c = cycle->connections;
|
|
787
|
|
788 for (i = 0; i < cycle->connection_n; i++) {
|
|
789
|
|
790 /* THREAD: lock */
|
0
|
791
|
290
|
792 if (c[i].fd != -1 && c[i].idle) {
|
|
793 c[i].close = 1;
|
|
794 c[i].read->handler(c[i].read);
|
|
795 }
|
|
796 }
|
|
797
|
|
798 if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
|
|
799 {
|
|
800 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
|
|
801
|
|
802 ngx_worker_process_exit(cycle);
|
|
803 }
|
0
|
804 }
|
|
805
|
|
806 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
|
|
807
|
112
|
808 ngx_process_events_and_timers(cycle);
|
0
|
809
|
|
810 if (ngx_terminate) {
|
26
|
811 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
|
0
|
812
|
122
|
813 ngx_worker_process_exit(cycle);
|
0
|
814 }
|
|
815
|
|
816 if (ngx_quit) {
|
|
817 ngx_quit = 0;
|
26
|
818 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
|
0
|
819 "gracefully shutting down");
|
|
820 ngx_setproctitle("worker process is shutting down");
|
|
821
|
|
822 if (!ngx_exiting) {
|
|
823 ngx_close_listening_sockets(cycle);
|
|
824 ngx_exiting = 1;
|
|
825 }
|
|
826 }
|
|
827
|
|
828 if (ngx_reopen) {
|
|
829 ngx_reopen = 0;
|
26
|
830 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
|
0
|
831 ngx_reopen_files(cycle, -1);
|
|
832 }
|
|
833 }
|
|
834 }
|
|
835
|
|
836
|
50
|
837 static void
|
|
838 ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority)
|
16
|
839 {
|
278
|
840 sigset_t set;
|
|
841 ngx_int_t n;
|
|
842 ngx_uint_t i;
|
|
843 struct rlimit rlmt;
|
|
844 ngx_core_conf_t *ccf;
|
|
845 ngx_listening_t *ls;
|
16
|
846
|
278
|
847 if (ngx_set_environment(cycle, NULL) == NULL) {
|
|
848 /* fatal */
|
|
849 exit(2);
|
|
850 }
|
|
851
|
16
|
852 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
853
|
328
|
854 if (priority && ccf->priority != 0) {
|
|
855 if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1) {
|
|
856 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
857 "setpriority(%d) failed", ccf->priority);
|
22
|
858 }
|
328
|
859 }
|
22
|
860
|
328
|
861 if (ccf->rlimit_nofile != NGX_CONF_UNSET) {
|
|
862 rlmt.rlim_cur = (rlim_t) ccf->rlimit_nofile;
|
|
863 rlmt.rlim_max = (rlim_t) ccf->rlimit_nofile;
|
92
|
864
|
328
|
865 if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
|
|
866 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
867 "setrlimit(RLIMIT_NOFILE, %i) failed",
|
|
868 ccf->rlimit_nofile);
|
92
|
869 }
|
328
|
870 }
|
92
|
871
|
598
|
872 if (ccf->rlimit_core != NGX_CONF_UNSET) {
|
328
|
873 rlmt.rlim_cur = (rlim_t) ccf->rlimit_core;
|
|
874 rlmt.rlim_max = (rlim_t) ccf->rlimit_core;
|
138
|
875
|
328
|
876 if (setrlimit(RLIMIT_CORE, &rlmt) == -1) {
|
|
877 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
598
|
878 "setrlimit(RLIMIT_CORE, %O) failed",
|
328
|
879 ccf->rlimit_core);
|
138
|
880 }
|
328
|
881 }
|
138
|
882
|
92
|
883 #ifdef RLIMIT_SIGPENDING
|
328
|
884 if (ccf->rlimit_sigpending != NGX_CONF_UNSET) {
|
|
885 rlmt.rlim_cur = (rlim_t) ccf->rlimit_sigpending;
|
|
886 rlmt.rlim_max = (rlim_t) ccf->rlimit_sigpending;
|
92
|
887
|
328
|
888 if (setrlimit(RLIMIT_SIGPENDING, &rlmt) == -1) {
|
|
889 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
890 "setrlimit(RLIMIT_SIGPENDING, %i) failed",
|
|
891 ccf->rlimit_sigpending);
|
92
|
892 }
|
328
|
893 }
|
92
|
894 #endif
|
|
895
|
328
|
896 if (geteuid() == 0) {
|
16
|
897 if (setgid(ccf->group) == -1) {
|
|
898 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
899 "setgid(%d) failed", ccf->group);
|
|
900 /* fatal */
|
|
901 exit(2);
|
|
902 }
|
|
903
|
22
|
904 if (initgroups(ccf->username, ccf->group) == -1) {
|
|
905 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
906 "initgroups(%s, %d) failed",
|
|
907 ccf->username, ccf->group);
|
|
908 }
|
|
909
|
16
|
910 if (setuid(ccf->user) == -1) {
|
|
911 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
912 "setuid(%d) failed", ccf->user);
|
|
913 /* fatal */
|
|
914 exit(2);
|
|
915 }
|
|
916 }
|
|
917
|
126
|
918 if (cpu_affinity) {
|
668
|
919 ngx_setaffinity(cpu_affinity, cycle->log);
|
126
|
920 }
|
|
921
|
16
|
922 #if (NGX_HAVE_PR_SET_DUMPABLE)
|
|
923
|
|
924 /* allow coredump after setuid() in Linux 2.4.x */
|
|
925
|
|
926 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
|
|
927 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
928 "prctl(PR_SET_DUMPABLE) failed");
|
|
929 }
|
|
930
|
|
931 #endif
|
|
932
|
70
|
933 if (ccf->working_directory.len) {
|
72
|
934 if (chdir((char *) ccf->working_directory.data) == -1) {
|
70
|
935 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
936 "chdir(\"%s\") failed", ccf->working_directory.data);
|
|
937 /* fatal */
|
|
938 exit(2);
|
|
939 }
|
|
940 }
|
|
941
|
16
|
942 sigemptyset(&set);
|
|
943
|
|
944 if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
|
|
945 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
946 "sigprocmask() failed");
|
|
947 }
|
|
948
|
|
949 /*
|
|
950 * disable deleting previous events for the listening sockets because
|
|
951 * in the worker processes there are no events at all at this point
|
126
|
952 */
|
16
|
953 ls = cycle->listening.elts;
|
|
954 for (i = 0; i < cycle->listening.nelts; i++) {
|
92
|
955 ls[i].previous = NULL;
|
16
|
956 }
|
|
957
|
|
958 for (i = 0; ngx_modules[i]; i++) {
|
|
959 if (ngx_modules[i]->init_process) {
|
|
960 if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
|
|
961 /* fatal */
|
|
962 exit(2);
|
|
963 }
|
|
964 }
|
|
965 }
|
|
966
|
|
967 for (n = 0; n < ngx_last_process; n++) {
|
|
968
|
|
969 if (ngx_processes[n].pid == -1) {
|
|
970 continue;
|
|
971 }
|
|
972
|
|
973 if (n == ngx_process_slot) {
|
|
974 continue;
|
|
975 }
|
|
976
|
|
977 if (ngx_processes[n].channel[1] == -1) {
|
|
978 continue;
|
|
979 }
|
|
980
|
|
981 if (close(ngx_processes[n].channel[1]) == -1) {
|
|
982 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
983 "close() channel failed");
|
|
984 }
|
|
985 }
|
|
986
|
|
987 if (close(ngx_processes[ngx_process_slot].channel[0]) == -1) {
|
|
988 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
989 "close() channel failed");
|
|
990 }
|
|
991
|
|
992 #if 0
|
|
993 ngx_last_process = 0;
|
|
994 #endif
|
|
995
|
|
996 if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT,
|
290
|
997 ngx_channel_handler)
|
|
998 == NGX_ERROR)
|
16
|
999 {
|
|
1000 /* fatal */
|
|
1001 exit(2);
|
|
1002 }
|
|
1003 }
|
|
1004
|
|
1005
|
50
|
1006 static void
|
122
|
1007 ngx_worker_process_exit(ngx_cycle_t *cycle)
|
|
1008 {
|
|
1009 ngx_uint_t i;
|
|
1010 ngx_connection_t *c;
|
|
1011
|
|
1012 #if (NGX_THREADS)
|
|
1013 ngx_terminate = 1;
|
|
1014
|
|
1015 ngx_wakeup_worker_threads(cycle);
|
|
1016 #endif
|
|
1017
|
|
1018 for (i = 0; ngx_modules[i]; i++) {
|
|
1019 if (ngx_modules[i]->exit_process) {
|
|
1020 ngx_modules[i]->exit_process(cycle);
|
|
1021 }
|
|
1022 }
|
|
1023
|
366
|
1024 if (ngx_exiting) {
|
136
|
1025 c = cycle->connections;
|
|
1026 for (i = 0; i < cycle->connection_n; i++) {
|
|
1027 if (c[i].fd != -1
|
|
1028 && c[i].read
|
|
1029 && !c[i].read->accept
|
366
|
1030 && !c[i].read->channel
|
|
1031 && !c[i].read->resolver)
|
136
|
1032 {
|
|
1033 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
514
|
1034 "open socket #%d left in connection %ui",
|
|
1035 c[i].fd, i);
|
|
1036 ngx_debug_quit = 1;
|
136
|
1037 }
|
122
|
1038 }
|
|
1039
|
136
|
1040 if (ngx_debug_quit) {
|
514
|
1041 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "aborting");
|
136
|
1042 ngx_debug_point();
|
|
1043 }
|
122
|
1044 }
|
|
1045
|
|
1046 /*
|
332
|
1047 * Copy ngx_cycle->log related data to the special static exit cycle,
|
|
1048 * log, and log file structures enough to allow a signal handler to log.
|
|
1049 * The handler may be called when standard ngx_cycle->log allocated from
|
|
1050 * ngx_cycle->pool is already destroyed.
|
122
|
1051 */
|
|
1052
|
332
|
1053 ngx_exit_log_file.fd = ngx_cycle->log->file->fd;
|
|
1054
|
|
1055 ngx_exit_log = *ngx_cycle->log;
|
|
1056 ngx_exit_log.file = &ngx_exit_log_file;
|
|
1057
|
|
1058 ngx_exit_cycle.log = &ngx_exit_log;
|
678
|
1059 ngx_exit_cycle.files = ngx_cycle->files;
|
|
1060 ngx_exit_cycle.files_n = ngx_cycle->files_n;
|
332
|
1061 ngx_cycle = &ngx_exit_cycle;
|
|
1062
|
122
|
1063 ngx_destroy_pool(cycle->pool);
|
332
|
1064
|
|
1065 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "exit");
|
122
|
1066
|
|
1067 exit(0);
|
|
1068 }
|
|
1069
|
|
1070
|
|
1071 static void
|
50
|
1072 ngx_channel_handler(ngx_event_t *ev)
|
0
|
1073 {
|
|
1074 ngx_int_t n;
|
|
1075 ngx_channel_t ch;
|
|
1076 ngx_connection_t *c;
|
|
1077
|
16
|
1078 if (ev->timedout) {
|
|
1079 ev->timedout = 0;
|
|
1080 return;
|
|
1081 }
|
|
1082
|
0
|
1083 c = ev->data;
|
|
1084
|
|
1085 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel handler");
|
|
1086
|
378
|
1087 for ( ;; ) {
|
0
|
1088
|
378
|
1089 n = ngx_read_channel(c->fd, &ch, sizeof(ngx_channel_t), ev->log);
|
|
1090
|
|
1091 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel: %i", n);
|
0
|
1092
|
378
|
1093 if (n == NGX_ERROR) {
|
366
|
1094
|
378
|
1095 if (ngx_event_flags & NGX_USE_EPOLL_EVENT) {
|
|
1096 ngx_del_conn(c, 0);
|
|
1097 }
|
|
1098
|
|
1099 ngx_close_connection(c);
|
|
1100 return;
|
366
|
1101 }
|
|
1102
|
378
|
1103 if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {
|
|
1104 if (ngx_add_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
|
|
1105 return;
|
|
1106 }
|
|
1107 }
|
10
|
1108
|
378
|
1109 if (n == NGX_AGAIN) {
|
290
|
1110 return;
|
|
1111 }
|
378
|
1112
|
|
1113 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,
|
|
1114 "channel command: %d", ch.command);
|
290
|
1115
|
378
|
1116 switch (ch.command) {
|
0
|
1117
|
378
|
1118 case NGX_CMD_QUIT:
|
|
1119 ngx_quit = 1;
|
|
1120 break;
|
0
|
1121
|
378
|
1122 case NGX_CMD_TERMINATE:
|
|
1123 ngx_terminate = 1;
|
|
1124 break;
|
0
|
1125
|
378
|
1126 case NGX_CMD_REOPEN:
|
|
1127 ngx_reopen = 1;
|
|
1128 break;
|
0
|
1129
|
378
|
1130 case NGX_CMD_OPEN_CHANNEL:
|
0
|
1131
|
378
|
1132 ngx_log_debug3(NGX_LOG_DEBUG_CORE, ev->log, 0,
|
|
1133 "get channel s:%i pid:%P fd:%d",
|
|
1134 ch.slot, ch.pid, ch.fd);
|
0
|
1135
|
378
|
1136 ngx_processes[ch.slot].pid = ch.pid;
|
|
1137 ngx_processes[ch.slot].channel[0] = ch.fd;
|
|
1138 break;
|
0
|
1139
|
378
|
1140 case NGX_CMD_CLOSE_CHANNEL:
|
0
|
1141
|
378
|
1142 ngx_log_debug4(NGX_LOG_DEBUG_CORE, ev->log, 0,
|
|
1143 "close channel s:%i pid:%P our:%P fd:%d",
|
|
1144 ch.slot, ch.pid, ngx_processes[ch.slot].pid,
|
|
1145 ngx_processes[ch.slot].channel[0]);
|
0
|
1146
|
378
|
1147 if (close(ngx_processes[ch.slot].channel[0]) == -1) {
|
|
1148 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
|
|
1149 "close() channel failed");
|
|
1150 }
|
|
1151
|
|
1152 ngx_processes[ch.slot].channel[0] = -1;
|
|
1153 break;
|
0
|
1154 }
|
|
1155 }
|
|
1156 }
|
|
1157
|
|
1158
|
|
1159 #if (NGX_THREADS)
|
|
1160
|
50
|
1161 static void
|
|
1162 ngx_wakeup_worker_threads(ngx_cycle_t *cycle)
|
0
|
1163 {
|
|
1164 ngx_int_t i;
|
|
1165 ngx_uint_t live;
|
|
1166
|
|
1167 for ( ;; ) {
|
|
1168
|
|
1169 live = 0;
|
|
1170
|
|
1171 for (i = 0; i < ngx_threads_n; i++) {
|
|
1172 if (ngx_threads[i].state < NGX_THREAD_EXIT) {
|
18
|
1173 if (ngx_cond_signal(ngx_threads[i].cv) == NGX_ERROR) {
|
|
1174 ngx_threads[i].state = NGX_THREAD_DONE;
|
0
|
1175
|
|
1176 } else {
|
|
1177 live = 1;
|
|
1178 }
|
|
1179 }
|
|
1180
|
|
1181 if (ngx_threads[i].state == NGX_THREAD_EXIT) {
|
|
1182 ngx_thread_join(ngx_threads[i].tid, NULL);
|
|
1183 ngx_threads[i].state = NGX_THREAD_DONE;
|
|
1184 }
|
|
1185 }
|
|
1186
|
|
1187 if (live == 0) {
|
|
1188 ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
1189 "all worker threads are joined");
|
|
1190
|
|
1191 /* STUB */
|
|
1192 ngx_done_events(cycle);
|
|
1193 ngx_mutex_destroy(ngx_event_timer_mutex);
|
|
1194 ngx_mutex_destroy(ngx_posted_events_mutex);
|
|
1195
|
|
1196 return;
|
|
1197 }
|
|
1198
|
|
1199 ngx_sched_yield();
|
|
1200 }
|
|
1201 }
|
|
1202
|
|
1203
|
112
|
1204 static ngx_thread_value_t
|
50
|
1205 ngx_worker_thread_cycle(void *data)
|
0
|
1206 {
|
|
1207 ngx_thread_t *thr = data;
|
|
1208
|
|
1209 sigset_t set;
|
|
1210 ngx_err_t err;
|
|
1211 ngx_core_tls_t *tls;
|
|
1212 ngx_cycle_t *cycle;
|
|
1213
|
|
1214 cycle = (ngx_cycle_t *) ngx_cycle;
|
|
1215
|
|
1216 sigemptyset(&set);
|
|
1217 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
|
|
1218 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
|
|
1219 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
|
|
1220
|
|
1221 err = ngx_thread_sigmask(SIG_BLOCK, &set, NULL);
|
|
1222 if (err) {
|
|
1223 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
1224 ngx_thread_sigmask_n " failed");
|
112
|
1225 return (ngx_thread_value_t) 1;
|
0
|
1226 }
|
|
1227
|
|
1228 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
1229 "thread " NGX_TID_T_FMT " started", ngx_thread_self());
|
0
|
1230
|
|
1231 ngx_setthrtitle("worker thread");
|
|
1232
|
50
|
1233 tls = ngx_calloc(sizeof(ngx_core_tls_t), cycle->log);
|
|
1234 if (tls == NULL) {
|
112
|
1235 return (ngx_thread_value_t) 1;
|
0
|
1236 }
|
|
1237
|
|
1238 err = ngx_thread_set_tls(ngx_core_tls_key, tls);
|
|
1239 if (err != 0) {
|
|
1240 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
|
|
1241 ngx_thread_set_tls_n " failed");
|
112
|
1242 return (ngx_thread_value_t) 1;
|
0
|
1243 }
|
|
1244
|
112
|
1245 ngx_mutex_lock(ngx_posted_events_mutex);
|
0
|
1246
|
|
1247 for ( ;; ) {
|
|
1248 thr->state = NGX_THREAD_FREE;
|
|
1249
|
|
1250 if (ngx_cond_wait(thr->cv, ngx_posted_events_mutex) == NGX_ERROR) {
|
112
|
1251 return (ngx_thread_value_t) 1;
|
0
|
1252 }
|
|
1253
|
|
1254 if (ngx_terminate) {
|
|
1255 thr->state = NGX_THREAD_EXIT;
|
|
1256
|
|
1257 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
1258
|
|
1259 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
10
|
1260 "thread " NGX_TID_T_FMT " is done",
|
|
1261 ngx_thread_self());
|
0
|
1262
|
112
|
1263 return (ngx_thread_value_t) 0;
|
0
|
1264 }
|
|
1265
|
|
1266 thr->state = NGX_THREAD_BUSY;
|
|
1267
|
|
1268 if (ngx_event_thread_process_posted(cycle) == NGX_ERROR) {
|
112
|
1269 return (ngx_thread_value_t) 1;
|
0
|
1270 }
|
|
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_process_changes) {
|
|
1277 if (ngx_process_changes(cycle, 1) == NGX_ERROR) {
|
112
|
1278 return (ngx_thread_value_t) 1;
|
0
|
1279 }
|
|
1280 }
|
|
1281 }
|
|
1282 }
|
|
1283
|
|
1284 #endif
|
16
|
1285
|
|
1286
|
50
|
1287 static void
|
468
|
1288 ngx_cache_manager_process_cycle(ngx_cycle_t *cycle, void *data)
|
16
|
1289 {
|
514
|
1290 ngx_cache_manager_ctx_t *ctx = data;
|
|
1291
|
464
|
1292 void *ident[4];
|
|
1293 ngx_event_t ev;
|
|
1294
|
|
1295 cycle->connection_n = 512;
|
16
|
1296
|
552
|
1297 ngx_process = NGX_PROCESS_HELPER;
|
|
1298
|
22
|
1299 ngx_worker_process_init(cycle, 0);
|
16
|
1300
|
464
|
1301 ngx_close_listening_sockets(cycle);
|
16
|
1302
|
464
|
1303 ngx_memzero(&ev, sizeof(ngx_event_t));
|
514
|
1304 ev.handler = ctx->handler;
|
464
|
1305 ev.data = ident;
|
|
1306 ev.log = cycle->log;
|
|
1307 ident[3] = (void *) -1;
|
16
|
1308
|
464
|
1309 ngx_use_accept_mutex = 0;
|
|
1310
|
514
|
1311 ngx_setproctitle(ctx->name);
|
464
|
1312
|
514
|
1313 ngx_add_timer(&ev, ctx->delay);
|
16
|
1314
|
|
1315 for ( ;; ) {
|
|
1316
|
|
1317 if (ngx_terminate || ngx_quit) {
|
26
|
1318 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
|
16
|
1319 exit(0);
|
|
1320 }
|
|
1321
|
|
1322 if (ngx_reopen) {
|
|
1323 ngx_reopen = 0;
|
26
|
1324 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
|
16
|
1325 ngx_reopen_files(cycle, -1);
|
|
1326 }
|
|
1327
|
112
|
1328 ngx_process_events_and_timers(cycle);
|
16
|
1329 }
|
|
1330 }
|
50
|
1331
|
464
|
1332
|
|
1333 static void
|
468
|
1334 ngx_cache_manager_process_handler(ngx_event_t *ev)
|
464
|
1335 {
|
|
1336 time_t next, n;
|
|
1337 ngx_uint_t i;
|
|
1338 ngx_path_t **path;
|
|
1339
|
|
1340 next = 60 * 60;
|
|
1341
|
|
1342 path = ngx_cycle->pathes.elts;
|
|
1343 for (i = 0; i < ngx_cycle->pathes.nelts; i++) {
|
|
1344
|
468
|
1345 if (path[i]->manager) {
|
|
1346 n = path[i]->manager(path[i]->data);
|
464
|
1347
|
|
1348 next = (n <= next) ? n : next;
|
|
1349
|
566
|
1350 ngx_time_update();
|
464
|
1351 }
|
|
1352 }
|
|
1353
|
|
1354 if (next == 0) {
|
|
1355 next = 1;
|
|
1356 }
|
|
1357
|
|
1358 ngx_add_timer(ev, next * 1000);
|
|
1359 }
|
514
|
1360
|
|
1361
|
|
1362 static void
|
|
1363 ngx_cache_loader_process_handler(ngx_event_t *ev)
|
|
1364 {
|
|
1365 ngx_uint_t i;
|
|
1366 ngx_path_t **path;
|
|
1367 ngx_cycle_t *cycle;
|
|
1368
|
|
1369 cycle = (ngx_cycle_t *) ngx_cycle;
|
|
1370
|
|
1371 path = cycle->pathes.elts;
|
|
1372 for (i = 0; i < cycle->pathes.nelts; i++) {
|
|
1373
|
|
1374 if (ngx_terminate || ngx_quit) {
|
|
1375 break;
|
|
1376 }
|
|
1377
|
|
1378 if (path[i]->loader) {
|
|
1379 path[i]->loader(path[i]->data);
|
566
|
1380 ngx_time_update();
|
514
|
1381 }
|
|
1382 }
|
|
1383
|
|
1384 exit(0);
|
|
1385 }
|