16 static void ngx_start_cache_manager_processes(
ngx_cycle_t *cycle,
19 static void ngx_signal_worker_processes(
ngx_cycle_t *cycle,
int signo);
21 static void ngx_master_process_exit(
ngx_cycle_t *cycle);
22 static void ngx_worker_process_cycle(
ngx_cycle_t *cycle,
void *data);
24 static void ngx_worker_process_exit(
ngx_cycle_t *cycle);
27 static void ngx_wakeup_worker_threads(
ngx_cycle_t *cycle);
28 static ngx_thread_value_t ngx_worker_thread_cycle(
void *data);
30 static void ngx_cache_manager_process_cycle(
ngx_cycle_t *cycle,
void *data);
31 static void ngx_cache_manager_process_handler(
ngx_event_t *ev);
32 static void ngx_cache_loader_process_handler(
ngx_event_t *ev);
60 volatile ngx_thread_t ngx_threads[NGX_MAX_THREADS];
65 static u_char master_process[] =
"master process";
69 ngx_cache_manager_process_handler,
"cache manager process", 0
73 ngx_cache_loader_process_handler,
"cache loader process", 60000
98 sigaddset(&
set, SIGCHLD);
99 sigaddset(&
set, SIGALRM);
100 sigaddset(&
set, SIGIO);
101 sigaddset(&
set, SIGINT);
109 if (sigprocmask(SIG_BLOCK, &
set, NULL) == -1) {
111 "sigprocmask() failed");
117 size =
sizeof(master_process);
125 p =
ngx_cpymem(title, master_process,
sizeof(master_process) - 1);
138 ngx_start_cache_manager_processes(cycle, 0);
154 "termination cycle: %d", delay);
156 itv.it_interval.tv_sec = 0;
157 itv.it_interval.tv_usec = 0;
158 itv.it_value.tv_sec = delay / 1000;
159 itv.it_value.tv_usec = (delay % 1000 ) * 1000;
161 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
163 "setitimer() failed");
174 "wake up, sigio %i", sigio);
180 live = ngx_reap_children(cycle);
184 ngx_master_process_exit(cycle);
200 ngx_signal_worker_processes(cycle, SIGKILL);
202 ngx_signal_worker_processes(cycle,
210 ngx_signal_worker_processes(cycle,
232 ngx_start_cache_manager_processes(cycle, 0);
251 ngx_start_cache_manager_processes(cycle, 1);
257 ngx_signal_worker_processes(cycle,
265 ngx_start_cache_manager_processes(cycle, 0);
273 ngx_signal_worker_processes(cycle,
286 ngx_signal_worker_processes(cycle,
325 ngx_master_process_exit(cycle);
360 for (i = 0; i < n; i++) {
363 (
void *) (intptr_t) i,
"worker process", type);
369 ngx_pass_open_channel(cycle, &ch);
387 if (path[i]->manager) {
391 if (path[i]->loader) {
401 &ngx_cache_manager_ctx,
"cache manager process",
409 ngx_pass_open_channel(cycle, &ch);
416 &ngx_cache_loader_ctx,
"cache loader process",
424 ngx_pass_open_channel(cycle, &ch);
443 "pass channel s:%d pid:%P fd:%d to s:%i pid:%P fd:%d",
457 ngx_signal_worker_processes(
ngx_cycle_t *cycle,
int signo)
463 #if (NGX_BROKEN_SCM_RIGHTS)
495 "child: %d %P e:%d t:%d d:%d r:%d j:%d",
571 "child: %d %P e:%d t:%d d:%d r:%d j:%d",
604 "pass close channel s:%i pid:%P to:%P",
625 "could not respawn %s",
636 ngx_pass_open_channel(cycle, &ch);
654 "after the new binary process \"%s\" exited",
665 if (i == ngx_last_process - 1) {
708 ngx_exit_log.
file = &ngx_exit_log_file;
710 ngx_exit_cycle.
log = &ngx_exit_log;
722 ngx_worker_process_cycle(
ngx_cycle_t *cycle,
void *data)
731 ngx_worker_process_init(cycle, worker);
759 for (n = 0; n < ngx_threads_n; n++) {
763 if (ngx_threads[n].cv == NULL) {
769 ngx_worker_thread_cycle,
770 (
void *) &ngx_threads[n], cycle->
log)
791 if (c[i].fd != -1 && c[i].idle) {
801 ngx_worker_process_exit(cycle);
812 ngx_worker_process_exit(cycle);
818 "gracefully shutting down");
840 uint64_t cpu_affinity;
854 if (worker >= 0 && ccf->priority != 0) {
855 if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1) {
857 "setpriority(%d) failed", ccf->priority);
862 rlmt.rlim_cur = (rlim_t) ccf->rlimit_nofile;
863 rlmt.rlim_max = (rlim_t) ccf->rlimit_nofile;
865 if (setrlimit(RLIMIT_NOFILE, &
rlmt) == -1) {
867 "setrlimit(RLIMIT_NOFILE, %i) failed",
873 rlmt.rlim_cur = (rlim_t) ccf->rlimit_core;
874 rlmt.rlim_max = (rlim_t) ccf->rlimit_core;
876 if (setrlimit(RLIMIT_CORE, &
rlmt) == -1) {
878 "setrlimit(RLIMIT_CORE, %O) failed",
883 #ifdef RLIMIT_SIGPENDING
885 rlmt.rlim_cur = (rlim_t) ccf->rlimit_sigpending;
886 rlmt.rlim_max = (rlim_t) ccf->rlimit_sigpending;
888 if (setrlimit(RLIMIT_SIGPENDING, &
rlmt) == -1) {
890 "setrlimit(RLIMIT_SIGPENDING, %i) failed",
891 ccf->rlimit_sigpending);
896 if (geteuid() == 0) {
897 if (setgid(ccf->group) == -1) {
899 "setgid(%d) failed", ccf->group);
904 if (initgroups(ccf->username, ccf->group) == -1) {
906 "initgroups(%s, %d) failed",
907 ccf->username, ccf->group);
910 if (setuid(ccf->user) == -1) {
912 "setuid(%d) failed", ccf->user);
926 #if (NGX_HAVE_PR_SET_DUMPABLE)
930 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
932 "prctl(PR_SET_DUMPABLE) failed");
937 if (ccf->working_directory.len) {
938 if (chdir((
char *) ccf->working_directory.data) == -1) {
940 "chdir(\"%s\") failed", ccf->working_directory.data);
948 if (sigprocmask(SIG_SETMASK, &
set, NULL) == -1) {
950 "sigprocmask() failed");
987 "close() channel failed");
993 "close() channel failed");
997 ngx_last_process = 0;
1001 ngx_channel_handler)
1019 ngx_wakeup_worker_threads(cycle);
1033 && !c[i].read->accept
1038 "open socket #%d left in connection %ui",
1060 ngx_exit_log.
file = &ngx_exit_log_file;
1062 ngx_exit_cycle.
log = &ngx_exit_log;
1118 "channel command: %d", ch.
command);
1137 "get channel s:%i pid:%P fd:%d",
1147 "close channel s:%i pid:%P our:%P fd:%d",
1153 "close() channel failed");
1175 for (i = 0; i < ngx_threads_n; i++) {
1176 if (ngx_threads[i].state < NGX_THREAD_EXIT) {
1178 ngx_threads[
i].state = NGX_THREAD_DONE;
1185 if (ngx_threads[i].state == NGX_THREAD_EXIT) {
1187 ngx_threads[
i].state = NGX_THREAD_DONE;
1193 "all worker threads are joined");
1208 static ngx_thread_value_t
1209 ngx_worker_thread_cycle(
void *data)
1211 ngx_thread_t *thr = data;
1229 return (ngx_thread_value_t) 1;
1239 return (ngx_thread_value_t) 1;
1246 return (ngx_thread_value_t) 1;
1252 thr->state = NGX_THREAD_FREE;
1255 return (ngx_thread_value_t) 1;
1259 thr->state = NGX_THREAD_EXIT;
1267 return (ngx_thread_value_t) 0;
1270 thr->state = NGX_THREAD_BUSY;
1272 if (ngx_event_thread_process_posted(cycle) ==
NGX_ERROR) {
1273 return (ngx_thread_value_t) 1;
1276 if (ngx_event_thread_process_posted(cycle) ==
NGX_ERROR) {
1277 return (ngx_thread_value_t) 1;
1282 return (ngx_thread_value_t) 1;
1292 ngx_cache_manager_process_cycle(
ngx_cycle_t *cycle,
void *data)
1310 ngx_worker_process_init(cycle, -1);
1316 ident[3] = (
void *) -1;
1343 ngx_cache_manager_process_handler(
ngx_event_t *ev)
1354 if (path[i]->manager) {
1355 n = path[
i]->
manager(path[i]->data);
1357 next = (n <= next) ? n : next;
1387 if (path[i]->loader) {
1388 path[
i]->
loader(path[i]->data);