3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
10 #include <ngx_channel.h>
13 static void ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n,
15 static void ngx_start_garbage_collector(ngx_cycle_t *cycle, ngx_int_t type);
16 static void ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo);
17 static ngx_uint_t ngx_reap_children(ngx_cycle_t *cycle);
18 static void ngx_master_process_exit(ngx_cycle_t *cycle);
19 static void ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data);
20 static void ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority);
21 static void ngx_worker_process_exit(ngx_cycle_t *cycle);
22 static void ngx_channel_handler(ngx_event_t *ev);
24 static void ngx_wakeup_worker_threads(ngx_cycle_t *cycle);
25 static ngx_thread_value_t ngx_worker_thread_cycle(void *data);
28 static void ngx_garbage_collector_cycle(ngx_cycle_t *cycle, void *data);
32 ngx_uint_t ngx_process;
34 ngx_uint_t ngx_threaded;
36 sig_atomic_t ngx_reap;
37 sig_atomic_t ngx_sigio;
38 sig_atomic_t ngx_terminate;
39 sig_atomic_t ngx_quit;
40 sig_atomic_t ngx_debug_quit;
41 ngx_uint_t ngx_exiting;
42 sig_atomic_t ngx_reconfigure;
43 sig_atomic_t ngx_reopen;
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;
50 sig_atomic_t ngx_noaccept;
51 ngx_uint_t ngx_noaccepting;
52 ngx_uint_t ngx_restart;
56 volatile ngx_thread_t ngx_threads[NGX_MAX_THREADS];
57 ngx_int_t ngx_threads_n;
62 static u_char master_process[] = "master process";
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;
71 ngx_master_process_cycle(ngx_cycle_t *cycle)
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));
97 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
98 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
99 "sigprocmask() failed");
105 size = sizeof(master_process);
107 for (i = 0; i < ngx_argc; i++) {
108 size += ngx_strlen(ngx_argv[i]) + 1;
111 title = ngx_pnalloc(cycle->pool, size);
113 p = ngx_cpymem(title, master_process, sizeof(master_process) - 1);
114 for (i = 0; i < ngx_argc; i++) {
116 p = ngx_cpystrn(p, (u_char *) ngx_argv[i], size);
119 ngx_setproctitle(title);
122 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
124 ngx_start_worker_processes(cycle, ccf->worker_processes,
125 NGX_PROCESS_RESPAWN);
126 ngx_start_garbage_collector(cycle, NGX_PROCESS_RESPAWN);
136 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
137 "temination cycle: %d", delay);
139 itv.it_interval.tv_sec = 0;
140 itv.it_interval.tv_usec = 0;
141 itv.it_value.tv_sec = delay / 1000;
142 itv.it_value.tv_usec = (delay % 1000 ) * 1000;
144 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
145 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
146 "setitimer() failed");
150 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "sigsuspend");
154 ngx_time_update(0, 0);
156 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "wake up");
160 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "reap children");
162 live = ngx_reap_children(cycle);
165 if (!live && (ngx_terminate || ngx_quit)) {
166 ngx_master_process_exit(cycle);
175 ngx_signal_worker_processes(cycle, SIGKILL);
177 ngx_signal_worker_processes(cycle,
178 ngx_signal_value(NGX_TERMINATE_SIGNAL));
185 ngx_signal_worker_processes(cycle,
186 ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
188 ls = cycle->listening.elts;
189 for (n = 0; n < cycle->listening.nelts; n++) {
190 if (ngx_close_socket(ls[n].fd) == -1) {
191 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
192 ngx_close_socket_n " %V failed",
196 cycle->listening.nelts = 0;
201 if (ngx_reconfigure) {
204 if (ngx_new_binary) {
205 ngx_start_worker_processes(cycle, ccf->worker_processes,
206 NGX_PROCESS_RESPAWN);
207 ngx_start_garbage_collector(cycle, NGX_PROCESS_RESPAWN);
213 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");
215 cycle = ngx_init_cycle(cycle);
217 cycle = (ngx_cycle_t *) ngx_cycle;
222 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
224 ngx_start_worker_processes(cycle, ccf->worker_processes,
225 NGX_PROCESS_JUST_RESPAWN);
226 ngx_start_garbage_collector(cycle, NGX_PROCESS_JUST_RESPAWN);
228 ngx_signal_worker_processes(cycle,
229 ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
234 ngx_start_worker_processes(cycle, ccf->worker_processes,
235 NGX_PROCESS_RESPAWN);
236 ngx_start_garbage_collector(cycle, NGX_PROCESS_RESPAWN);
242 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
243 ngx_reopen_files(cycle, ccf->user);
244 ngx_signal_worker_processes(cycle,
245 ngx_signal_value(NGX_REOPEN_SIGNAL));
248 if (ngx_change_binary) {
249 ngx_change_binary = 0;
250 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "changing binary");
251 ngx_new_binary = ngx_exec_new_binary(cycle, ngx_argv);
257 ngx_signal_worker_processes(cycle,
258 ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
265 ngx_single_process_cycle(ngx_cycle_t *cycle)
269 ngx_init_temp_number();
271 for (i = 0; ngx_modules[i]; i++) {
272 if (ngx_modules[i]->init_process) {
273 if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
281 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
283 ngx_process_events_and_timers(cycle);
285 if (ngx_terminate || ngx_quit) {
287 for (i = 0; ngx_modules[i]; i++) {
288 if (ngx_modules[i]->exit_process) {
289 ngx_modules[i]->exit_process(cycle);
293 ngx_master_process_exit(cycle);
296 if (ngx_reconfigure) {
298 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");
300 cycle = ngx_init_cycle(cycle);
302 cycle = (ngx_cycle_t *) ngx_cycle;
311 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
312 ngx_reopen_files(cycle, (ngx_uid_t) -1);
319 ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t n, ngx_int_t type)
324 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "start worker processes");
326 ch.command = NGX_CMD_OPEN_CHANNEL;
328 for (i = 0; i < n; i++) {
330 cpu_affinity = ngx_get_cpu_affinity(i);
332 ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
333 "worker process", type);
335 ch.pid = ngx_processes[ngx_process_slot].pid;
336 ch.slot = ngx_process_slot;
337 ch.fd = ngx_processes[ngx_process_slot].channel[0];
339 for (s = 0; s < ngx_last_process; s++) {
341 if (s == ngx_process_slot
342 || ngx_processes[s].pid == -1
343 || ngx_processes[s].channel[0] == -1)
348 ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
349 "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
350 ch.slot, ch.pid, ch.fd,
351 s, ngx_processes[s].pid,
352 ngx_processes[s].channel[0]);
354 /* TODO: NGX_AGAIN */
356 ngx_write_channel(ngx_processes[s].channel[0],
357 &ch, sizeof(ngx_channel_t), cycle->log);
364 ngx_start_garbage_collector(ngx_cycle_t *cycle, ngx_int_t type)
370 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "start garbage collector");
372 ch.command = NGX_CMD_OPEN_CHANNEL;
374 ngx_spawn_process(cycle, ngx_garbage_collector_cycle, NULL,
375 "garbage collector", type);
377 ch.pid = ngx_processes[ngx_process_slot].pid;
378 ch.slot = ngx_process_slot;
379 ch.fd = ngx_processes[ngx_process_slot].channel[0];
381 for (i = 0; i < ngx_last_process; i++) {
383 if (i == ngx_process_slot
384 || ngx_processes[i].pid == -1
385 || ngx_processes[i].channel[0] == -1)
390 ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
391 "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
392 ch.slot, ch.pid, ch.fd,
393 i, ngx_processes[i].pid,
394 ngx_processes[i].channel[0]);
396 /* TODO: NGX_AGAIN */
398 ngx_write_channel(ngx_processes[i].channel[0],
399 &ch, sizeof(ngx_channel_t), cycle->log);
406 ngx_signal_worker_processes(ngx_cycle_t *cycle, int signo)
412 #if (NGX_BROKEN_SCM_RIGHTS)
420 case ngx_signal_value(NGX_SHUTDOWN_SIGNAL):
421 ch.command = NGX_CMD_QUIT;
424 case ngx_signal_value(NGX_TERMINATE_SIGNAL):
425 ch.command = NGX_CMD_TERMINATE;
428 case ngx_signal_value(NGX_REOPEN_SIGNAL):
429 ch.command = NGX_CMD_REOPEN;
441 for (i = 0; i < ngx_last_process; i++) {
443 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
444 "child: %d %P e:%d t:%d d:%d r:%d j:%d",
446 ngx_processes[i].pid,
447 ngx_processes[i].exiting,
448 ngx_processes[i].exited,
449 ngx_processes[i].detached,
450 ngx_processes[i].respawn,
451 ngx_processes[i].just_respawn);
453 if (ngx_processes[i].detached || ngx_processes[i].pid == -1) {
457 if (ngx_processes[i].just_respawn) {
458 ngx_processes[i].just_respawn = 0;
462 if (ngx_processes[i].exiting
463 && signo == ngx_signal_value(NGX_SHUTDOWN_SIGNAL))
469 if (ngx_write_channel(ngx_processes[i].channel[0],
470 &ch, sizeof(ngx_channel_t), cycle->log)
473 if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
474 ngx_processes[i].exiting = 1;
481 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
482 "kill (%P, %d)" , ngx_processes[i].pid, signo);
484 if (kill(ngx_processes[i].pid, signo) == -1) {
486 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
487 "kill(%P, %d) failed",
488 ngx_processes[i].pid, signo);
490 if (err == NGX_ESRCH) {
491 ngx_processes[i].exited = 1;
492 ngx_processes[i].exiting = 0;
499 if (signo != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
500 ngx_processes[i].exiting = 1;
507 ngx_reap_children(ngx_cycle_t *cycle)
512 ngx_core_conf_t *ccf;
514 ch.command = NGX_CMD_CLOSE_CHANNEL;
518 for (i = 0; i < ngx_last_process; i++) {
520 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
521 "child: %d %P e:%d t:%d d:%d r:%d j:%d",
523 ngx_processes[i].pid,
524 ngx_processes[i].exiting,
525 ngx_processes[i].exited,
526 ngx_processes[i].detached,
527 ngx_processes[i].respawn,
528 ngx_processes[i].just_respawn);
530 if (ngx_processes[i].pid == -1) {
534 if (ngx_processes[i].exited) {
536 if (!ngx_processes[i].detached) {
537 ngx_close_channel(ngx_processes[i].channel, cycle->log);
539 ngx_processes[i].channel[0] = -1;
540 ngx_processes[i].channel[1] = -1;
542 ch.pid = ngx_processes[i].pid;
545 for (n = 0; n < ngx_last_process; n++) {
546 if (ngx_processes[n].exited
547 || ngx_processes[n].pid == -1
548 || ngx_processes[n].channel[0] == -1)
553 ngx_log_debug3(NGX_LOG_DEBUG_CORE, cycle->log, 0,
554 "pass close channel s:%i pid:%P to:%P",
555 ch.slot, ch.pid, ngx_processes[n].pid);
557 /* TODO: NGX_AGAIN */
559 ngx_write_channel(ngx_processes[n].channel[0],
560 &ch, sizeof(ngx_channel_t), cycle->log);
564 if (ngx_processes[i].respawn
565 && !ngx_processes[i].exiting
569 if (ngx_spawn_process(cycle, ngx_processes[i].proc,
570 ngx_processes[i].data,
571 ngx_processes[i].name, i)
574 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
575 "can not respawn %s", ngx_processes[i].name);
580 ch.command = NGX_CMD_OPEN_CHANNEL;
581 ch.pid = ngx_processes[ngx_process_slot].pid;
582 ch.slot = ngx_process_slot;
583 ch.fd = ngx_processes[ngx_process_slot].channel[0];
585 for (n = 0; n < ngx_last_process; n++) {
587 if (n == ngx_process_slot
588 || ngx_processes[n].pid == -1
589 || ngx_processes[n].channel[0] == -1)
594 ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0,
595 "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
596 ch.slot, ch.pid, ch.fd,
597 n, ngx_processes[n].pid,
598 ngx_processes[n].channel[0]);
600 /* TODO: NGX_AGAIN */
602 ngx_write_channel(ngx_processes[n].channel[0],
603 &ch, sizeof(ngx_channel_t), cycle->log);
611 if (ngx_processes[i].pid == ngx_new_binary) {
613 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
616 if (ngx_rename_file((char *) ccf->oldpid.data,
617 (char *) ccf->pid.data)
620 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
621 ngx_rename_file_n " %s back to %s failed "
622 "after the new binary process \"%s\" exited",
623 ccf->oldpid.data, ccf->pid.data, ngx_argv[0]);
627 if (ngx_noaccepting) {
633 if (i == ngx_last_process - 1) {
637 ngx_processes[i].pid = -1;
640 } else if (ngx_processes[i].exiting || !ngx_processes[i].detached) {
650 ngx_master_process_exit(ngx_cycle_t *cycle)
654 ngx_delete_pidfile(cycle);
656 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exit");
658 for (i = 0; ngx_modules[i]; i++) {
659 if (ngx_modules[i]->exit_master) {
660 ngx_modules[i]->exit_master(cycle);
665 * Copy ngx_cycle->log related data to the special static exit cycle,
666 * log, and log file structures enough to allow a signal handler to log.
667 * The handler may be called when standard ngx_cycle->log allocated from
668 * ngx_cycle->pool is already destroyed.
671 ngx_exit_log_file.fd = ngx_cycle->log->file->fd;
673 ngx_exit_log = *ngx_cycle->log;
674 ngx_exit_log.file = &ngx_exit_log_file;
676 ngx_exit_cycle.log = &ngx_exit_log;
677 ngx_cycle = &ngx_exit_cycle;
679 ngx_destroy_pool(cycle->pool);
686 ngx_worker_process_cycle(ngx_cycle_t *cycle, void *data)
691 ngx_worker_process_init(cycle, 1);
693 ngx_setproctitle("worker process");
699 ngx_core_conf_t *ccf;
701 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
704 if (ngx_init_threads(ngx_threads_n, ccf->thread_stack_size, cycle)
711 err = ngx_thread_key_create(&ngx_core_tls_key);
713 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
714 ngx_thread_key_create_n " failed");
719 for (n = 0; n < ngx_threads_n; n++) {
721 ngx_threads[n].cv = ngx_cond_init(cycle->log);
723 if (ngx_threads[n].cv == NULL) {
728 if (ngx_create_thread((ngx_tid_t *) &ngx_threads[n].tid,
729 ngx_worker_thread_cycle,
730 (void *) &ngx_threads[n], cycle->log)
745 c = cycle->connections;
747 for (i = 0; i < cycle->connection_n; i++) {
751 if (c[i].fd != -1 && c[i].idle) {
753 c[i].read->handler(c[i].read);
757 if (ngx_event_timer_rbtree.root == ngx_event_timer_rbtree.sentinel)
759 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
761 ngx_worker_process_exit(cycle);
765 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle");
767 ngx_process_events_and_timers(cycle);
770 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
772 ngx_worker_process_exit(cycle);
777 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
778 "gracefully shutting down");
779 ngx_setproctitle("worker process is shutting down");
782 ngx_close_listening_sockets(cycle);
789 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
790 ngx_reopen_files(cycle, -1);
797 ngx_worker_process_init(ngx_cycle_t *cycle, ngx_uint_t priority)
803 ngx_core_conf_t *ccf;
806 ngx_process = NGX_PROCESS_WORKER;
808 if (ngx_set_environment(cycle, NULL) == NULL) {
813 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
815 if (priority && ccf->priority != 0) {
816 if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1) {
817 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
818 "setpriority(%d) failed", ccf->priority);
822 if (ccf->rlimit_nofile != NGX_CONF_UNSET) {
823 rlmt.rlim_cur = (rlim_t) ccf->rlimit_nofile;
824 rlmt.rlim_max = (rlim_t) ccf->rlimit_nofile;
826 if (setrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
827 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
828 "setrlimit(RLIMIT_NOFILE, %i) failed",
833 if (ccf->rlimit_core != NGX_CONF_UNSET_SIZE) {
834 rlmt.rlim_cur = (rlim_t) ccf->rlimit_core;
835 rlmt.rlim_max = (rlim_t) ccf->rlimit_core;
837 if (setrlimit(RLIMIT_CORE, &rlmt) == -1) {
838 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
839 "setrlimit(RLIMIT_CORE, %i) failed",
844 #ifdef RLIMIT_SIGPENDING
845 if (ccf->rlimit_sigpending != NGX_CONF_UNSET) {
846 rlmt.rlim_cur = (rlim_t) ccf->rlimit_sigpending;
847 rlmt.rlim_max = (rlim_t) ccf->rlimit_sigpending;
849 if (setrlimit(RLIMIT_SIGPENDING, &rlmt) == -1) {
850 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
851 "setrlimit(RLIMIT_SIGPENDING, %i) failed",
852 ccf->rlimit_sigpending);
857 if (geteuid() == 0) {
858 if (setgid(ccf->group) == -1) {
859 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
860 "setgid(%d) failed", ccf->group);
865 if (initgroups(ccf->username, ccf->group) == -1) {
866 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
867 "initgroups(%s, %d) failed",
868 ccf->username, ccf->group);
871 if (setuid(ccf->user) == -1) {
872 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
873 "setuid(%d) failed", ccf->user);
879 #if (NGX_HAVE_SCHED_SETAFFINITY)
882 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
883 "sched_setaffinity(0x%08Xl)", cpu_affinity);
885 if (sched_setaffinity(0, 32, (cpu_set_t *) &cpu_affinity) == -1) {
886 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
887 "sched_setaffinity(0x%08Xl) failed", cpu_affinity);
893 #if (NGX_HAVE_PR_SET_DUMPABLE)
895 /* allow coredump after setuid() in Linux 2.4.x */
897 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
898 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
899 "prctl(PR_SET_DUMPABLE) failed");
904 if (ccf->working_directory.len) {
905 if (chdir((char *) ccf->working_directory.data) == -1) {
906 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
907 "chdir(\"%s\") failed", ccf->working_directory.data);
915 if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
916 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
917 "sigprocmask() failed");
920 ngx_init_temp_number();
923 * disable deleting previous events for the listening sockets because
924 * in the worker processes there are no events at all at this point
926 ls = cycle->listening.elts;
927 for (i = 0; i < cycle->listening.nelts; i++) {
928 ls[i].previous = NULL;
931 for (i = 0; ngx_modules[i]; i++) {
932 if (ngx_modules[i]->init_process) {
933 if (ngx_modules[i]->init_process(cycle) == NGX_ERROR) {
940 for (n = 0; n < ngx_last_process; n++) {
942 if (ngx_processes[n].pid == -1) {
946 if (n == ngx_process_slot) {
950 if (ngx_processes[n].channel[1] == -1) {
954 if (close(ngx_processes[n].channel[1]) == -1) {
955 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
956 "close() channel failed");
960 if (close(ngx_processes[ngx_process_slot].channel[0]) == -1) {
961 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
962 "close() channel failed");
966 ngx_last_process = 0;
969 if (ngx_add_channel_event(cycle, ngx_channel, NGX_READ_EVENT,
980 ngx_worker_process_exit(ngx_cycle_t *cycle)
988 ngx_wakeup_worker_threads(cycle);
991 for (i = 0; ngx_modules[i]; i++) {
992 if (ngx_modules[i]->exit_process) {
993 ngx_modules[i]->exit_process(cycle);
998 c = cycle->connections;
999 for (i = 0; i < cycle->connection_n; i++) {
1002 && !c[i].read->accept
1003 && !c[i].read->channel
1004 && !c[i].read->resolver)
1006 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
1007 "open socket #%d left in connection %ui%s",
1008 c[i].fd, i, ngx_debug_quit ? ", aborting" : "");
1013 if (ngx_debug_quit) {
1019 * Copy ngx_cycle->log related data to the special static exit cycle,
1020 * log, and log file structures enough to allow a signal handler to log.
1021 * The handler may be called when standard ngx_cycle->log allocated from
1022 * ngx_cycle->pool is already destroyed.
1025 ngx_exit_log_file.fd = ngx_cycle->log->file->fd;
1027 ngx_exit_log = *ngx_cycle->log;
1028 ngx_exit_log.file = &ngx_exit_log_file;
1030 ngx_exit_cycle.log = &ngx_exit_log;
1031 ngx_cycle = &ngx_exit_cycle;
1033 ngx_destroy_pool(cycle->pool);
1035 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "exit");
1042 ngx_channel_handler(ngx_event_t *ev)
1046 ngx_connection_t *c;
1055 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel handler");
1059 n = ngx_read_channel(c->fd, &ch, sizeof(ngx_channel_t), ev->log);
1061 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0, "channel: %i", n);
1063 if (n == NGX_ERROR) {
1065 if (ngx_event_flags & NGX_USE_EPOLL_EVENT) {
1069 ngx_close_connection(c);
1073 if (ngx_event_flags & NGX_USE_EVENTPORT_EVENT) {
1074 if (ngx_add_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
1079 if (n == NGX_AGAIN) {
1083 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ev->log, 0,
1084 "channel command: %d", ch.command);
1086 switch (ch.command) {
1092 case NGX_CMD_TERMINATE:
1096 case NGX_CMD_REOPEN:
1100 case NGX_CMD_OPEN_CHANNEL:
1102 ngx_log_debug3(NGX_LOG_DEBUG_CORE, ev->log, 0,
1103 "get channel s:%i pid:%P fd:%d",
1104 ch.slot, ch.pid, ch.fd);
1106 ngx_processes[ch.slot].pid = ch.pid;
1107 ngx_processes[ch.slot].channel[0] = ch.fd;
1110 case NGX_CMD_CLOSE_CHANNEL:
1112 ngx_log_debug4(NGX_LOG_DEBUG_CORE, ev->log, 0,
1113 "close channel s:%i pid:%P our:%P fd:%d",
1114 ch.slot, ch.pid, ngx_processes[ch.slot].pid,
1115 ngx_processes[ch.slot].channel[0]);
1117 if (close(ngx_processes[ch.slot].channel[0]) == -1) {
1118 ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
1119 "close() channel failed");
1122 ngx_processes[ch.slot].channel[0] = -1;
1132 ngx_wakeup_worker_threads(ngx_cycle_t *cycle)
1141 for (i = 0; i < ngx_threads_n; i++) {
1142 if (ngx_threads[i].state < NGX_THREAD_EXIT) {
1143 if (ngx_cond_signal(ngx_threads[i].cv) == NGX_ERROR) {
1144 ngx_threads[i].state = NGX_THREAD_DONE;
1151 if (ngx_threads[i].state == NGX_THREAD_EXIT) {
1152 ngx_thread_join(ngx_threads[i].tid, NULL);
1153 ngx_threads[i].state = NGX_THREAD_DONE;
1158 ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0,
1159 "all worker threads are joined");
1162 ngx_done_events(cycle);
1163 ngx_mutex_destroy(ngx_event_timer_mutex);
1164 ngx_mutex_destroy(ngx_posted_events_mutex);
1174 static ngx_thread_value_t
1175 ngx_worker_thread_cycle(void *data)
1177 ngx_thread_t *thr = data;
1181 ngx_core_tls_t *tls;
1184 cycle = (ngx_cycle_t *) ngx_cycle;
1187 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
1188 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
1189 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
1191 err = ngx_thread_sigmask(SIG_BLOCK, &set, NULL);
1193 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
1194 ngx_thread_sigmask_n " failed");
1195 return (ngx_thread_value_t) 1;
1198 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
1199 "thread " NGX_TID_T_FMT " started", ngx_thread_self());
1201 ngx_setthrtitle("worker thread");
1203 tls = ngx_calloc(sizeof(ngx_core_tls_t), cycle->log);
1205 return (ngx_thread_value_t) 1;
1208 err = ngx_thread_set_tls(ngx_core_tls_key, tls);
1210 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
1211 ngx_thread_set_tls_n " failed");
1212 return (ngx_thread_value_t) 1;
1215 ngx_mutex_lock(ngx_posted_events_mutex);
1218 thr->state = NGX_THREAD_FREE;
1220 if (ngx_cond_wait(thr->cv, ngx_posted_events_mutex) == NGX_ERROR) {
1221 return (ngx_thread_value_t) 1;
1224 if (ngx_terminate) {
1225 thr->state = NGX_THREAD_EXIT;
1227 ngx_mutex_unlock(ngx_posted_events_mutex);
1229 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
1230 "thread " NGX_TID_T_FMT " is done",
1233 return (ngx_thread_value_t) 0;
1236 thr->state = NGX_THREAD_BUSY;
1238 if (ngx_event_thread_process_posted(cycle) == NGX_ERROR) {
1239 return (ngx_thread_value_t) 1;
1242 if (ngx_event_thread_process_posted(cycle) == NGX_ERROR) {
1243 return (ngx_thread_value_t) 1;
1246 if (ngx_process_changes) {
1247 if (ngx_process_changes(cycle, 1) == NGX_ERROR) {
1248 return (ngx_thread_value_t) 1;
1260 ngx_garbage_collector_cycle(ngx_cycle_t *cycle, void *data)
1267 ngx_worker_process_init(cycle, 0);
1269 ev = &cycle->read_events0[ngx_channel];
1271 ngx_accept_mutex = NULL;
1273 ngx_setproctitle("garbage collector");
1276 ngx_add_timer(ev, 60 * 1000);
1281 if (ngx_terminate || ngx_quit) {
1282 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
1288 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
1289 ngx_reopen_files(cycle, -1);
1292 path = cycle->pathes.elts;
1293 for (i = 0; i < cycle->pathes.nelts; i++) {
1295 ctx.log = cycle->log;
1296 ctx.handler = path[i]->cleaner;
1298 ngx_collect_garbage(&ctx, &path[i]->name, 0);
1301 ngx_add_timer(ev, 60 * 60 * 1000);
1303 ngx_process_events_and_timers(cycle);