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