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