3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
13 static void ngx_destroy_cycle_pools(ngx_conf_t *conf);
14 static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2);
15 static void ngx_clean_old_cycles(ngx_event_t *ev);
18 volatile ngx_cycle_t *ngx_cycle;
19 ngx_array_t ngx_old_cycles;
21 static ngx_pool_t *ngx_temp_pool;
22 static ngx_event_t ngx_cleaner_event;
24 ngx_uint_t ngx_test_config;
27 ngx_tls_key_t ngx_core_tls_key;
32 static ngx_connection_t dumb;
35 #ifdef NGX_ERROR_LOG_PATH
36 static ngx_str_t error_log = ngx_string(NGX_ERROR_LOG_PATH);
38 static ngx_str_t error_log = ngx_null_string;
43 ngx_init_cycle(ngx_cycle_t *old_cycle)
53 ngx_cycle_t *cycle, **old;
54 ngx_shm_zone_t *shm_zone, *oshm_zone;
55 ngx_slab_pool_t *shpool;
56 ngx_list_part_t *part, *opart;
57 ngx_open_file_t *file;
58 ngx_listening_t *ls, *nls;
59 ngx_core_conf_t *ccf, *old_ccf;
60 ngx_core_module_t *module;
61 char hostname[NGX_MAXHOSTNAMELEN];
63 ngx_timezone_update();
65 /* force localtime update with a new timezone */
70 ngx_time_update(0, 0);
75 pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
81 cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));
83 ngx_destroy_pool(pool);
89 cycle->old_cycle = old_cycle;
90 cycle->root.len = sizeof(NGX_PREFIX) - 1;
91 cycle->root.data = (u_char *) NGX_PREFIX;
94 cycle->conf_file.len = old_cycle->conf_file.len;
95 cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
96 if (cycle->conf_file.data == NULL) {
97 ngx_destroy_pool(pool);
100 ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,
101 old_cycle->conf_file.len + 1);
104 cycle->conf_param.len = old_cycle->conf_param.len;
105 cycle->conf_param.data = ngx_pnalloc(pool, old_cycle->conf_param.len);
106 if (cycle->conf_param.data == NULL) {
107 ngx_destroy_pool(pool);
110 ngx_memcpy(cycle->conf_param.data, old_cycle->conf_param.data,
111 old_cycle->conf_param.len);
114 n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
116 cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));
117 if (cycle->pathes.elts == NULL) {
118 ngx_destroy_pool(pool);
122 cycle->pathes.nelts = 0;
123 cycle->pathes.size = sizeof(ngx_path_t *);
124 cycle->pathes.nalloc = n;
125 cycle->pathes.pool = pool;
128 if (old_cycle->open_files.part.nelts) {
129 n = old_cycle->open_files.part.nelts;
130 for (part = old_cycle->open_files.part.next; part; part = part->next) {
138 if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))
141 ngx_destroy_pool(pool);
146 if (old_cycle->shared_memory.part.nelts) {
147 n = old_cycle->shared_memory.part.nelts;
148 for (part = old_cycle->shared_memory.part.next; part; part = part->next)
157 if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))
160 ngx_destroy_pool(pool);
165 cycle->new_log = ngx_log_create_errlog(cycle, NULL);
166 if (cycle->new_log == NULL) {
167 ngx_destroy_pool(pool);
171 cycle->new_log->file->name = error_log;
174 n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;
176 cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));
177 if (cycle->listening.elts == NULL) {
178 ngx_destroy_pool(pool);
182 cycle->listening.nelts = 0;
183 cycle->listening.size = sizeof(ngx_listening_t);
184 cycle->listening.nalloc = n;
185 cycle->listening.pool = pool;
188 cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
189 if (cycle->conf_ctx == NULL) {
190 ngx_destroy_pool(pool);
195 if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {
196 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");
197 ngx_destroy_pool(pool);
201 /* on Linux gethostname() silently truncates name that does not fit */
203 hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';
204 cycle->hostname.len = ngx_strlen(hostname);
206 cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);
207 if (cycle->hostname.data == NULL) {
208 ngx_destroy_pool(pool);
212 ngx_memcpy(cycle->hostname.data, hostname, cycle->hostname.len);
215 for (i = 0; ngx_modules[i]; i++) {
216 if (ngx_modules[i]->type != NGX_CORE_MODULE) {
220 module = ngx_modules[i]->ctx;
222 if (module->create_conf) {
223 rv = module->create_conf(cycle);
224 if (rv == NGX_CONF_ERROR) {
225 ngx_destroy_pool(pool);
228 cycle->conf_ctx[ngx_modules[i]->index] = rv;
236 ngx_memzero(&conf, sizeof(ngx_conf_t));
237 /* STUB: init array ? */
238 conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));
239 if (conf.args == NULL) {
240 ngx_destroy_pool(pool);
244 conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
245 if (conf.temp_pool == NULL) {
246 ngx_destroy_pool(pool);
251 conf.ctx = cycle->conf_ctx;
255 conf.module_type = NGX_CORE_MODULE;
256 conf.cmd_type = NGX_MAIN_CONF;
259 log->log_level = NGX_LOG_DEBUG_ALL;
262 if (ngx_conf_param(&conf) != NGX_CONF_OK) {
263 ngx_destroy_cycle_pools(&conf);
267 if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
268 ngx_destroy_cycle_pools(&conf);
272 if (ngx_test_config) {
273 ngx_log_error(NGX_LOG_INFO, log, 0,
274 "the configuration file %s syntax is ok",
275 cycle->conf_file.data);
279 for (i = 0; ngx_modules[i]; i++) {
280 if (ngx_modules[i]->type != NGX_CORE_MODULE) {
284 module = ngx_modules[i]->ctx;
286 if (module->init_conf) {
287 if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
290 ngx_destroy_cycle_pools(&conf);
297 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
301 if (ngx_test_config) {
303 if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
307 } else if (!ngx_is_init_cycle(old_cycle)) {
310 * we do not create the pid file in the first ngx_init_cycle() call
311 * because we need to write the demonized process pid
314 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
316 if (ccf->pid.len != old_ccf->pid.len
317 || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0)
319 /* new pid file name */
321 if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
325 ngx_delete_pidfile(old_cycle);
332 if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {
337 if (ngx_create_pathes(cycle, ccf->user) != NGX_OK) {
342 /* open the new files */
344 part = &cycle->open_files.part;
347 for (i = 0; /* void */ ; i++) {
349 if (i >= part->nelts) {
350 if (part->next == NULL) {
358 if (file[i].name.data == NULL) {
362 file[i].fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR,
363 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
364 NGX_FILE_DEFAULT_ACCESS);
366 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
368 &file[i], file[i].fd, file[i].name.data);
370 if (file[i].fd == NGX_INVALID_FILE) {
371 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
372 ngx_open_file_n " \"%s\" failed",
378 if (ngx_file_append_mode(file[i].fd) != NGX_OK) {
379 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
380 ngx_file_append_mode_n " \"%s\" failed",
385 if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {
386 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
387 "fcntl(FD_CLOEXEC) \"%s\" failed",
394 cycle->log = cycle->new_log;
395 pool->log = cycle->new_log;
398 /* create shared memory */
400 part = &cycle->shared_memory.part;
401 shm_zone = part->elts;
403 for (i = 0; /* void */ ; i++) {
405 if (i >= part->nelts) {
406 if (part->next == NULL) {
410 shm_zone = part->elts;
414 if (shm_zone[i].shm.size == 0) {
415 ngx_log_error(NGX_LOG_EMERG, log, 0,
416 "zero size shared memory zone \"%V\"",
421 if (shm_zone[i].init == NULL) {
422 /* unused shared zone */
426 shm_zone[i].shm.log = cycle->log;
428 opart = &old_cycle->shared_memory.part;
429 oshm_zone = opart->elts;
431 for (n = 0; /* void */ ; n++) {
433 if (n >= opart->nelts) {
434 if (opart->next == NULL) {
438 oshm_zone = opart->elts;
442 if (shm_zone[i].name.len != oshm_zone[n].name.len) {
446 if (ngx_strncmp(shm_zone[i].name.data, oshm_zone[n].name.data,
447 shm_zone[i].name.len)
453 if (shm_zone[i].shm.size == oshm_zone[n].shm.size) {
454 shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
456 if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)
465 ngx_shm_free(&oshm_zone[n].shm);
470 if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {
474 shpool = (ngx_slab_pool_t *) shm_zone[i].shm.addr;
476 shpool->end = shm_zone[i].shm.addr + shm_zone[i].shm.size;
477 shpool->min_shift = 3;
479 #if (NGX_HAVE_ATOMIC_OPS)
485 lock_file = ngx_pnalloc(cycle->pool,
486 cycle->lock_file.len + shm_zone[i].name.len);
488 if (lock_file == NULL) {
492 (void) ngx_cpystrn(ngx_cpymem(lock_file, cycle->lock_file.data,
493 cycle->lock_file.len),
494 shm_zone[i].name.data, shm_zone[i].name.len + 1);
498 if (ngx_shmtx_create(&shpool->mutex, (void *) &shpool->lock, lock_file)
504 ngx_slab_init(shpool);
506 if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {
516 /* handle the listening sockets */
518 if (old_cycle->listening.nelts) {
519 ls = old_cycle->listening.elts;
520 for (i = 0; i < old_cycle->listening.nelts; i++) {
524 nls = cycle->listening.elts;
525 for (n = 0; n < cycle->listening.nelts; n++) {
527 for (i = 0; i < old_cycle->listening.nelts; i++) {
532 if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) == NGX_OK)
534 nls[n].fd = ls[i].fd;
535 nls[n].previous = &ls[i];
538 if (ls[n].backlog != nls[i].backlog) {
542 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
545 * FreeBSD, except the most recent versions,
546 * could not remove accept filter
548 nls[n].deferred_accept = ls[i].deferred_accept;
550 if (ls[i].accept_filter && nls[n].accept_filter) {
551 if (ngx_strcmp(ls[i].accept_filter,
552 nls[n].accept_filter)
555 nls[n].delete_deferred = 1;
556 nls[n].add_deferred = 1;
559 } else if (ls[i].accept_filter) {
560 nls[n].delete_deferred = 1;
562 } else if (nls[n].accept_filter) {
563 nls[n].add_deferred = 1;
567 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
569 if (ls[n].deferred_accept && !nls[n].deferred_accept) {
570 nls[n].delete_deferred = 1;
572 } else if (ls[i].deferred_accept != nls[n].deferred_accept)
574 nls[n].add_deferred = 1;
581 if (nls[n].fd == -1) {
587 ls = cycle->listening.elts;
588 for (i = 0; i < cycle->listening.nelts; i++) {
590 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
591 if (ls[i].accept_filter) {
592 ls[i].add_deferred = 1;
595 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
596 if (ls[i].deferred_accept) {
597 ls[i].add_deferred = 1;
603 if (ngx_open_listening_sockets(cycle) != NGX_OK) {
607 if (!ngx_test_config) {
608 ngx_configure_listening_socket(cycle);
612 /* commit the new cycle configuration */
616 if (!ngx_test_config && cycle->log->file->fd != STDERR_FILENO) {
618 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
619 "dup2: %p %d \"%s\"",
621 cycle->log->file->fd, cycle->log->file->name.data);
623 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
624 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
625 "dup2(STDERR) failed");
633 pool->log = cycle->log;
635 for (i = 0; ngx_modules[i]; i++) {
636 if (ngx_modules[i]->init_module) {
637 if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
645 /* close and delete stuff that lefts from an old cycle */
647 /* free the unnecessary shared memory */
649 opart = &old_cycle->shared_memory.part;
650 oshm_zone = opart->elts;
652 for (i = 0; /* void */ ; i++) {
654 if (i >= opart->nelts) {
655 if (opart->next == NULL) {
656 goto old_shm_zone_done;
659 oshm_zone = opart->elts;
663 part = &cycle->shared_memory.part;
664 shm_zone = part->elts;
666 for (n = 0; /* void */ ; n++) {
668 if (n >= part->nelts) {
669 if (part->next == NULL) {
673 shm_zone = part->elts;
677 if (oshm_zone[i].name.len == shm_zone[n].name.len
678 && ngx_strncmp(oshm_zone[i].name.data,
679 shm_zone[n].name.data,
680 oshm_zone[i].name.len)
687 ngx_shm_free(&oshm_zone[i].shm);
697 /* close the unnecessary listening sockets */
699 ls = old_cycle->listening.elts;
700 for (i = 0; i < old_cycle->listening.nelts; i++) {
705 if (ngx_close_socket(ls[i].fd) == -1) {
706 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
707 ngx_close_socket_n " listening socket on %V failed",
713 /* close the unnecessary open files */
715 part = &old_cycle->open_files.part;
718 for (i = 0; /* void */ ; i++) {
720 if (i >= part->nelts) {
721 if (part->next == NULL) {
729 if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) {
733 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
734 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
735 ngx_close_file_n " \"%s\" failed",
740 ngx_destroy_pool(conf.temp_pool);
742 if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {
745 * perl_destruct() frees environ if it is not the same as it was at
746 * perl_construct() time. So we have saved an previous cycle
747 * environment before ngx_conf_parse() where it will be changed.
753 ngx_destroy_pool(old_cycle->pool);
754 cycle->old_cycle = NULL;
762 if (ngx_temp_pool == NULL) {
763 ngx_temp_pool = ngx_create_pool(128, cycle->log);
764 if (ngx_temp_pool == NULL) {
765 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
766 "can not create ngx_temp_pool");
771 ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool,
772 n * sizeof(ngx_cycle_t *));
773 if (ngx_old_cycles.elts == NULL) {
776 ngx_old_cycles.nelts = 0;
777 ngx_old_cycles.size = sizeof(ngx_cycle_t *);
778 ngx_old_cycles.nalloc = n;
779 ngx_old_cycles.pool = ngx_temp_pool;
781 ngx_cleaner_event.handler = ngx_clean_old_cycles;
782 ngx_cleaner_event.log = cycle->log;
783 ngx_cleaner_event.data = &dumb;
784 dumb.fd = (ngx_socket_t) -1;
787 ngx_temp_pool->log = cycle->log;
789 old = ngx_array_push(&ngx_old_cycles);
795 if (!ngx_cleaner_event.timer_set) {
796 ngx_add_timer(&ngx_cleaner_event, 30000);
797 ngx_cleaner_event.timer_set = 1;
805 if (!ngx_is_init_cycle(old_cycle)) {
806 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
808 if (old_ccf->environment) {
809 environ = old_ccf->environment;
813 /* rollback the new cycle configuration */
815 part = &cycle->open_files.part;
818 for (i = 0; /* void */ ; i++) {
820 if (i >= part->nelts) {
821 if (part->next == NULL) {
829 if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) {
833 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
834 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
835 ngx_close_file_n " \"%s\" failed",
840 if (ngx_test_config) {
841 ngx_destroy_cycle_pools(&conf);
845 ls = cycle->listening.elts;
846 for (i = 0; i < cycle->listening.nelts; i++) {
847 if (ls[i].fd == -1 || !ls[i].open) {
851 if (ngx_close_socket(ls[i].fd) == -1) {
852 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
853 ngx_close_socket_n " %V failed",
858 ngx_destroy_cycle_pools(&conf);
865 ngx_destroy_cycle_pools(ngx_conf_t *conf)
867 ngx_destroy_pool(conf->temp_pool);
868 ngx_destroy_pool(conf->pool);
873 ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2)
875 struct sockaddr_in *sin1, *sin2;
879 if (sa1->sa_family != AF_INET || sa2->sa_family != AF_INET) {
883 sin1 = (struct sockaddr_in *) sa1;
884 sin2 = (struct sockaddr_in *) sa2;
886 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
890 if (sin1->sin_port != sin2->sin_port) {
901 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
906 u_char pid[NGX_INT64_LEN + 2];
908 ngx_memzero(&file, sizeof(ngx_file_t));
913 trunc = ngx_test_config ? 0 : NGX_FILE_TRUNCATE;
915 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
916 NGX_FILE_CREATE_OR_OPEN|trunc,
917 NGX_FILE_DEFAULT_ACCESS);
919 if (file.fd == NGX_INVALID_FILE) {
920 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
921 ngx_open_file_n " \"%s\" failed", file.name.data);
925 if (!ngx_test_config) {
926 len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;
928 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
933 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
934 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
935 ngx_close_file_n " \"%s\" failed", file.name.data);
943 ngx_delete_pidfile(ngx_cycle_t *cycle)
946 ngx_core_conf_t *ccf;
948 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
950 name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data;
952 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
953 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
954 ngx_delete_file_n " \"%s\" failed", name);
962 ngx_test_lockfile(u_char *file, ngx_log_t *log)
964 #if !(NGX_HAVE_ATOMIC_OPS)
967 fd = ngx_open_file(file, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
968 NGX_FILE_DEFAULT_ACCESS);
970 if (fd == NGX_INVALID_FILE) {
971 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
972 ngx_open_file_n " \"%s\" failed", file);
976 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
977 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
978 ngx_close_file_n " \"%s\" failed", file);
981 if (ngx_delete_file(file) == NGX_FILE_ERROR) {
982 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
983 ngx_delete_file_n " \"%s\" failed", file);
993 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
998 ngx_list_part_t *part;
999 ngx_open_file_t *file;
1001 part = &cycle->open_files.part;
1004 for (i = 0; /* void */ ; i++) {
1006 if (i >= part->nelts) {
1007 if (part->next == NULL) {
1015 if (file[i].name.data == NULL) {
1019 len = file[i].pos - file[i].buffer;
1021 if (file[i].buffer && len != 0) {
1023 n = ngx_write_fd(file[i].fd, file[i].buffer, len);
1025 if (n == NGX_FILE_ERROR) {
1026 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1027 ngx_write_fd_n " to \"%s\" failed",
1030 } else if (n != len) {
1031 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
1032 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
1033 file[i].name.data, n, len);
1036 file[i].pos = file[i].buffer;
1039 fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR,
1040 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
1041 NGX_FILE_DEFAULT_ACCESS);
1043 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
1044 "reopen file \"%s\", old:%d new:%d",
1045 file[i].name.data, file[i].fd, fd);
1047 if (fd == NGX_INVALID_FILE) {
1048 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1049 ngx_open_file_n " \"%s\" failed", file[i].name.data);
1054 if (ngx_file_append_mode(fd) == NGX_ERROR) {
1055 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1056 ngx_file_append_mode_n " \"%s\" failed",
1059 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1060 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1061 ngx_close_file_n " \"%s\" failed",
1068 if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
1071 if (ngx_file_info((const char *) file[i].name.data, &fi) == -1) {
1072 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1073 ngx_file_info_n " \"%s\" failed",
1076 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1077 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1078 ngx_close_file_n " \"%s\" failed",
1083 if (fi.st_uid != user) {
1084 if (chown((const char *) file[i].name.data, user, -1) == -1) {
1085 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1086 "chown(\"%s\", %d) failed",
1087 file[i].name.data, user);
1089 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1090 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1091 ngx_close_file_n " \"%s\" failed",
1097 if ((fi.st_mode & (S_IRUSR|S_IWUSR)) != (S_IRUSR|S_IWUSR)) {
1099 fi.st_mode |= (S_IRUSR|S_IWUSR);
1101 if (chmod((const char *) file[i].name.data, fi.st_mode) == -1) {
1102 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1103 "chmod() \"%s\" failed", file[i].name.data);
1105 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1106 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1107 ngx_close_file_n " \"%s\" failed",
1114 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
1115 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1116 "fcntl(FD_CLOEXEC) \"%s\" failed",
1119 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1120 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1121 ngx_close_file_n " \"%s\" failed",
1129 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
1130 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1131 ngx_close_file_n " \"%s\" failed",
1140 if (cycle->log->file->fd != STDERR_FILENO) {
1141 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
1142 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1143 "dup2(STDERR) failed");
1152 ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag)
1155 ngx_shm_zone_t *shm_zone;
1156 ngx_list_part_t *part;
1158 part = &cf->cycle->shared_memory.part;
1159 shm_zone = part->elts;
1161 for (i = 0; /* void */ ; i++) {
1163 if (i >= part->nelts) {
1164 if (part->next == NULL) {
1168 shm_zone = part->elts;
1172 if (name->len != shm_zone[i].name.len) {
1176 if (ngx_strncmp(name->data, shm_zone[i].name.data, name->len) != 0) {
1180 if (size && size != shm_zone[i].shm.size) {
1181 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1182 "the size %uz of shared memory zone \"%V\" "
1183 "conflicts with already declared size %uz",
1184 size, &shm_zone[i].name, shm_zone[i].shm.size);
1188 if (tag != shm_zone[i].tag) {
1189 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1190 "the shared memory zone \"%V\" is "
1191 "already declared for a different use",
1196 return &shm_zone[i];
1199 shm_zone = ngx_list_push(&cf->cycle->shared_memory);
1201 if (shm_zone == NULL) {
1205 shm_zone->data = NULL;
1206 shm_zone->shm.log = cf->cycle->log;
1207 shm_zone->shm.size = size;
1208 shm_zone->init = NULL;
1209 shm_zone->name = *name;
1210 shm_zone->tag = tag;
1217 ngx_clean_old_cycles(ngx_event_t *ev)
1219 ngx_uint_t i, n, found, live;
1221 ngx_cycle_t **cycle;
1223 log = ngx_cycle->log;
1224 ngx_temp_pool->log = log;
1226 ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");
1230 cycle = ngx_old_cycles.elts;
1231 for (i = 0; i < ngx_old_cycles.nelts; i++) {
1233 if (cycle[i] == NULL) {
1239 for (n = 0; n < cycle[i]->connection_n; n++) {
1240 if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) {
1243 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%d", n);
1254 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %d", i);
1256 ngx_destroy_pool(cycle[i]->pool);
1260 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %d", live);
1263 ngx_add_timer(ev, 30000);
1266 ngx_destroy_pool(ngx_temp_pool);
1267 ngx_temp_pool = NULL;
1268 ngx_old_cycles.nelts = 0;