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