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;
397 if (cycle->log->log_level == 0) {
398 cycle->log->log_level = NGX_LOG_ERR;
402 /* create shared memory */
404 part = &cycle->shared_memory.part;
405 shm_zone = part->elts;
407 for (i = 0; /* void */ ; i++) {
409 if (i >= part->nelts) {
410 if (part->next == NULL) {
414 shm_zone = part->elts;
418 if (shm_zone[i].shm.size == 0) {
419 ngx_log_error(NGX_LOG_EMERG, log, 0,
420 "zero size shared memory zone \"%V\"",
425 if (shm_zone[i].init == NULL) {
426 /* unused shared zone */
430 shm_zone[i].shm.log = cycle->log;
432 opart = &old_cycle->shared_memory.part;
433 oshm_zone = opart->elts;
435 for (n = 0; /* void */ ; n++) {
437 if (n >= opart->nelts) {
438 if (opart->next == NULL) {
442 oshm_zone = opart->elts;
446 if (shm_zone[i].name.len != oshm_zone[n].name.len) {
450 if (ngx_strncmp(shm_zone[i].name.data, oshm_zone[n].name.data,
451 shm_zone[i].name.len)
457 if (shm_zone[i].shm.size == oshm_zone[n].shm.size) {
458 shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
460 if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)
469 ngx_shm_free(&oshm_zone[n].shm);
474 if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {
478 shpool = (ngx_slab_pool_t *) shm_zone[i].shm.addr;
480 shpool->end = shm_zone[i].shm.addr + shm_zone[i].shm.size;
481 shpool->min_shift = 3;
483 #if (NGX_HAVE_ATOMIC_OPS)
489 lock_file = ngx_pnalloc(cycle->pool,
490 cycle->lock_file.len + shm_zone[i].name.len);
492 if (lock_file == NULL) {
496 (void) ngx_cpystrn(ngx_cpymem(lock_file, cycle->lock_file.data,
497 cycle->lock_file.len),
498 shm_zone[i].name.data, shm_zone[i].name.len + 1);
502 if (ngx_shmtx_create(&shpool->mutex, (void *) &shpool->lock, lock_file)
508 ngx_slab_init(shpool);
510 if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {
520 /* handle the listening sockets */
522 if (old_cycle->listening.nelts) {
523 ls = old_cycle->listening.elts;
524 for (i = 0; i < old_cycle->listening.nelts; i++) {
528 nls = cycle->listening.elts;
529 for (n = 0; n < cycle->listening.nelts; n++) {
531 for (i = 0; i < old_cycle->listening.nelts; i++) {
536 if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) == NGX_OK)
538 nls[n].fd = ls[i].fd;
539 nls[n].previous = &ls[i];
542 if (ls[n].backlog != nls[i].backlog) {
546 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
549 * FreeBSD, except the most recent versions,
550 * could not remove accept filter
552 nls[n].deferred_accept = ls[i].deferred_accept;
554 if (ls[i].accept_filter && nls[n].accept_filter) {
555 if (ngx_strcmp(ls[i].accept_filter,
556 nls[n].accept_filter)
559 nls[n].delete_deferred = 1;
560 nls[n].add_deferred = 1;
563 } else if (ls[i].accept_filter) {
564 nls[n].delete_deferred = 1;
566 } else if (nls[n].accept_filter) {
567 nls[n].add_deferred = 1;
571 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
573 if (ls[n].deferred_accept && !nls[n].deferred_accept) {
574 nls[n].delete_deferred = 1;
576 } else if (ls[i].deferred_accept != nls[n].deferred_accept)
578 nls[n].add_deferred = 1;
585 if (nls[n].fd == -1) {
591 ls = cycle->listening.elts;
592 for (i = 0; i < cycle->listening.nelts; i++) {
594 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
595 if (ls[i].accept_filter) {
596 ls[i].add_deferred = 1;
599 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
600 if (ls[i].deferred_accept) {
601 ls[i].add_deferred = 1;
607 if (ngx_open_listening_sockets(cycle) != NGX_OK) {
611 if (!ngx_test_config) {
612 ngx_configure_listening_socket(cycle);
616 /* commit the new cycle configuration */
620 if (!ngx_test_config && cycle->log->file->fd != STDERR_FILENO) {
622 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
623 "dup2: %p %d \"%s\"",
625 cycle->log->file->fd, cycle->log->file->name.data);
627 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
628 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
629 "dup2(STDERR) failed");
637 pool->log = cycle->log;
639 for (i = 0; ngx_modules[i]; i++) {
640 if (ngx_modules[i]->init_module) {
641 if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
649 /* close and delete stuff that lefts from an old cycle */
651 /* free the unnecessary shared memory */
653 opart = &old_cycle->shared_memory.part;
654 oshm_zone = opart->elts;
656 for (i = 0; /* void */ ; i++) {
658 if (i >= opart->nelts) {
659 if (opart->next == NULL) {
660 goto old_shm_zone_done;
663 oshm_zone = opart->elts;
667 part = &cycle->shared_memory.part;
668 shm_zone = part->elts;
670 for (n = 0; /* void */ ; n++) {
672 if (n >= part->nelts) {
673 if (part->next == NULL) {
677 shm_zone = part->elts;
681 if (oshm_zone[i].name.len == shm_zone[n].name.len
682 && ngx_strncmp(oshm_zone[i].name.data,
683 shm_zone[n].name.data,
684 oshm_zone[i].name.len)
691 ngx_shm_free(&oshm_zone[i].shm);
701 /* close the unnecessary listening sockets */
703 ls = old_cycle->listening.elts;
704 for (i = 0; i < old_cycle->listening.nelts; i++) {
709 if (ngx_close_socket(ls[i].fd) == -1) {
710 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
711 ngx_close_socket_n " listening socket on %V failed",
717 /* close the unnecessary open files */
719 part = &old_cycle->open_files.part;
722 for (i = 0; /* void */ ; i++) {
724 if (i >= part->nelts) {
725 if (part->next == NULL) {
733 if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) {
737 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
738 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
739 ngx_close_file_n " \"%s\" failed",
744 ngx_destroy_pool(conf.temp_pool);
746 if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {
749 * perl_destruct() frees environ if it is not the same as it was at
750 * perl_construct() time. So we have saved an previous cycle
751 * environment before ngx_conf_parse() where it will be changed.
757 ngx_destroy_pool(old_cycle->pool);
758 cycle->old_cycle = NULL;
766 if (ngx_temp_pool == NULL) {
767 ngx_temp_pool = ngx_create_pool(128, cycle->log);
768 if (ngx_temp_pool == NULL) {
769 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
770 "can not create ngx_temp_pool");
775 ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool,
776 n * sizeof(ngx_cycle_t *));
777 if (ngx_old_cycles.elts == NULL) {
780 ngx_old_cycles.nelts = 0;
781 ngx_old_cycles.size = sizeof(ngx_cycle_t *);
782 ngx_old_cycles.nalloc = n;
783 ngx_old_cycles.pool = ngx_temp_pool;
785 ngx_cleaner_event.handler = ngx_clean_old_cycles;
786 ngx_cleaner_event.log = cycle->log;
787 ngx_cleaner_event.data = &dumb;
788 dumb.fd = (ngx_socket_t) -1;
791 ngx_temp_pool->log = cycle->log;
793 old = ngx_array_push(&ngx_old_cycles);
799 if (!ngx_cleaner_event.timer_set) {
800 ngx_add_timer(&ngx_cleaner_event, 30000);
801 ngx_cleaner_event.timer_set = 1;
809 if (!ngx_is_init_cycle(old_cycle)) {
810 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
812 if (old_ccf->environment) {
813 environ = old_ccf->environment;
817 /* rollback the new cycle configuration */
819 part = &cycle->open_files.part;
822 for (i = 0; /* void */ ; i++) {
824 if (i >= part->nelts) {
825 if (part->next == NULL) {
833 if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) {
837 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
838 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
839 ngx_close_file_n " \"%s\" failed",
844 if (ngx_test_config) {
845 ngx_destroy_cycle_pools(&conf);
849 ls = cycle->listening.elts;
850 for (i = 0; i < cycle->listening.nelts; i++) {
851 if (ls[i].fd == -1 || !ls[i].open) {
855 if (ngx_close_socket(ls[i].fd) == -1) {
856 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
857 ngx_close_socket_n " %V failed",
862 ngx_destroy_cycle_pools(&conf);
869 ngx_destroy_cycle_pools(ngx_conf_t *conf)
871 ngx_destroy_pool(conf->temp_pool);
872 ngx_destroy_pool(conf->pool);
877 ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2)
879 struct sockaddr_in *sin1, *sin2;
881 struct sockaddr_in6 *sin61, *sin62;
884 if (sa1->sa_family != sa2->sa_family) {
888 switch (sa1->sa_family) {
892 sin61 = (struct sockaddr_in6 *) sa1;
893 sin62 = (struct sockaddr_in6 *) sa2;
895 if (sin61->sin6_port != sin61->sin6_port) {
899 if (ngx_memcmp(&sin61->sin6_addr, &sin62->sin6_addr, 16) != 0) {
906 default: /* AF_INET */
908 sin1 = (struct sockaddr_in *) sa1;
909 sin2 = (struct sockaddr_in *) sa2;
911 if (sin1->sin_port != sin2->sin_port) {
915 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
929 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
934 u_char pid[NGX_INT64_LEN + 2];
936 ngx_memzero(&file, sizeof(ngx_file_t));
941 trunc = ngx_test_config ? 0 : NGX_FILE_TRUNCATE;
943 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
944 NGX_FILE_CREATE_OR_OPEN|trunc,
945 NGX_FILE_DEFAULT_ACCESS);
947 if (file.fd == NGX_INVALID_FILE) {
948 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
949 ngx_open_file_n " \"%s\" failed", file.name.data);
953 if (!ngx_test_config) {
954 len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;
956 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
961 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
962 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
963 ngx_close_file_n " \"%s\" failed", file.name.data);
971 ngx_delete_pidfile(ngx_cycle_t *cycle)
974 ngx_core_conf_t *ccf;
976 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
978 name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data;
980 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
981 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
982 ngx_delete_file_n " \"%s\" failed", name);
990 ngx_test_lockfile(u_char *file, ngx_log_t *log)
992 #if !(NGX_HAVE_ATOMIC_OPS)
995 fd = ngx_open_file(file, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
996 NGX_FILE_DEFAULT_ACCESS);
998 if (fd == NGX_INVALID_FILE) {
999 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
1000 ngx_open_file_n " \"%s\" failed", file);
1004 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1005 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1006 ngx_close_file_n " \"%s\" failed", file);
1009 if (ngx_delete_file(file) == NGX_FILE_ERROR) {
1010 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1011 ngx_delete_file_n " \"%s\" failed", file);
1021 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
1026 ngx_list_part_t *part;
1027 ngx_open_file_t *file;
1029 part = &cycle->open_files.part;
1032 for (i = 0; /* void */ ; i++) {
1034 if (i >= part->nelts) {
1035 if (part->next == NULL) {
1043 if (file[i].name.data == NULL) {
1047 len = file[i].pos - file[i].buffer;
1049 if (file[i].buffer && len != 0) {
1051 n = ngx_write_fd(file[i].fd, file[i].buffer, len);
1053 if (n == NGX_FILE_ERROR) {
1054 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1055 ngx_write_fd_n " to \"%s\" failed",
1058 } else if (n != len) {
1059 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
1060 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
1061 file[i].name.data, n, len);
1064 file[i].pos = file[i].buffer;
1067 fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR,
1068 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
1069 NGX_FILE_DEFAULT_ACCESS);
1071 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
1072 "reopen file \"%s\", old:%d new:%d",
1073 file[i].name.data, file[i].fd, fd);
1075 if (fd == NGX_INVALID_FILE) {
1076 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1077 ngx_open_file_n " \"%s\" failed", file[i].name.data);
1082 if (ngx_file_append_mode(fd) == NGX_ERROR) {
1083 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1084 ngx_file_append_mode_n " \"%s\" failed",
1087 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1088 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1089 ngx_close_file_n " \"%s\" failed",
1096 if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
1099 if (ngx_file_info((const char *) file[i].name.data, &fi) == -1) {
1100 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1101 ngx_file_info_n " \"%s\" failed",
1104 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1105 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1106 ngx_close_file_n " \"%s\" failed",
1111 if (fi.st_uid != user) {
1112 if (chown((const char *) file[i].name.data, user, -1) == -1) {
1113 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1114 "chown(\"%s\", %d) failed",
1115 file[i].name.data, user);
1117 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1118 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1119 ngx_close_file_n " \"%s\" failed",
1125 if ((fi.st_mode & (S_IRUSR|S_IWUSR)) != (S_IRUSR|S_IWUSR)) {
1127 fi.st_mode |= (S_IRUSR|S_IWUSR);
1129 if (chmod((const char *) file[i].name.data, fi.st_mode) == -1) {
1130 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1131 "chmod() \"%s\" failed", file[i].name.data);
1133 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1134 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1135 ngx_close_file_n " \"%s\" failed",
1142 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
1143 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1144 "fcntl(FD_CLOEXEC) \"%s\" failed",
1147 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1148 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1149 ngx_close_file_n " \"%s\" failed",
1157 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
1158 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1159 ngx_close_file_n " \"%s\" failed",
1168 if (cycle->log->file->fd != STDERR_FILENO) {
1169 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
1170 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1171 "dup2(STDERR) failed");
1180 ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag)
1183 ngx_shm_zone_t *shm_zone;
1184 ngx_list_part_t *part;
1186 part = &cf->cycle->shared_memory.part;
1187 shm_zone = part->elts;
1189 for (i = 0; /* void */ ; i++) {
1191 if (i >= part->nelts) {
1192 if (part->next == NULL) {
1196 shm_zone = part->elts;
1200 if (name->len != shm_zone[i].name.len) {
1204 if (ngx_strncmp(name->data, shm_zone[i].name.data, name->len) != 0) {
1208 if (size && size != shm_zone[i].shm.size) {
1209 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1210 "the size %uz of shared memory zone \"%V\" "
1211 "conflicts with already declared size %uz",
1212 size, &shm_zone[i].name, shm_zone[i].shm.size);
1216 if (tag != shm_zone[i].tag) {
1217 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1218 "the shared memory zone \"%V\" is "
1219 "already declared for a different use",
1224 return &shm_zone[i];
1227 shm_zone = ngx_list_push(&cf->cycle->shared_memory);
1229 if (shm_zone == NULL) {
1233 shm_zone->data = NULL;
1234 shm_zone->shm.log = cf->cycle->log;
1235 shm_zone->shm.size = size;
1236 shm_zone->init = NULL;
1237 shm_zone->name = *name;
1238 shm_zone->tag = tag;
1245 ngx_clean_old_cycles(ngx_event_t *ev)
1247 ngx_uint_t i, n, found, live;
1249 ngx_cycle_t **cycle;
1251 log = ngx_cycle->log;
1252 ngx_temp_pool->log = log;
1254 ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");
1258 cycle = ngx_old_cycles.elts;
1259 for (i = 0; i < ngx_old_cycles.nelts; i++) {
1261 if (cycle[i] == NULL) {
1267 for (n = 0; n < cycle[i]->connection_n; n++) {
1268 if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) {
1271 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%d", n);
1282 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %d", i);
1284 ngx_destroy_pool(cycle[i]->pool);
1288 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %d", live);
1291 ngx_add_timer(ev, 30000);
1294 ngx_destroy_pool(ngx_temp_pool);
1295 ngx_temp_pool = NULL;
1296 ngx_old_cycles.nelts = 0;