Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
| 9 | #include <ngx_event.h> |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 10 | |
| 11 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 12 | static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log); |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 13 | static void ngx_destroy_cycle_pools(ngx_conf_t *conf); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 14 | static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 15 | static void ngx_clean_old_cycles(ngx_event_t *ev); |
| 16 | |
| 17 | |
| 18 | volatile ngx_cycle_t *ngx_cycle; |
| 19 | ngx_array_t ngx_old_cycles; |
| 20 | |
| 21 | static ngx_pool_t *ngx_temp_pool; |
| 22 | static ngx_event_t ngx_cleaner_event; |
| 23 | |
Igor Sysoev | 630ad0c | 2004-04-16 05:14:16 +0000 | [diff] [blame] | 24 | ngx_uint_t ngx_test_config; |
| 25 | |
Igor Sysoev | 2b97993 | 2004-07-07 15:01:00 +0000 | [diff] [blame] | 26 | #if (NGX_THREADS) |
| 27 | ngx_tls_key_t ngx_core_tls_key; |
| 28 | #endif |
| 29 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 30 | |
| 31 | /* STUB NAME */ |
| 32 | static ngx_connection_t dumb; |
| 33 | /* STUB */ |
| 34 | |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 35 | #ifdef NGX_ERROR_LOG_PATH |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 36 | static ngx_str_t error_log = ngx_string(NGX_ERROR_LOG_PATH); |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 37 | #else |
| 38 | static ngx_str_t error_log = ngx_null_string; |
| 39 | #endif |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 40 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 41 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 42 | ngx_cycle_t * |
| 43 | ngx_init_cycle(ngx_cycle_t *old_cycle) |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 44 | { |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 45 | void *rv; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 46 | u_char *lock_file; |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 47 | ngx_uint_t i, n; |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 48 | ngx_log_t *log; |
| 49 | ngx_conf_t conf; |
| 50 | ngx_pool_t *pool; |
| 51 | ngx_cycle_t *cycle, **old; |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 52 | ngx_shm_zone_t *shm_zone, *oshm_zone; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 53 | ngx_slab_pool_t *shpool; |
| 54 | ngx_list_part_t *part, *opart; |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 55 | ngx_open_file_t *file; |
| 56 | ngx_listening_t *ls, *nls; |
| 57 | ngx_core_conf_t *ccf; |
| 58 | ngx_core_module_t *module; |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 59 | #if !(WIN32) |
| 60 | ngx_core_conf_t *old_ccf; |
| 61 | #endif |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 62 | |
| 63 | log = old_cycle->log; |
| 64 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 65 | pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log); |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 66 | if (pool == NULL) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 69 | pool->log = log; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 70 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 71 | cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)); |
| 72 | if (cycle == NULL) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 73 | ngx_destroy_pool(pool); |
| 74 | return NULL; |
| 75 | } |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 76 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 77 | cycle->pool = pool; |
Igor Sysoev | cccc552 | 2004-04-14 20:34:05 +0000 | [diff] [blame] | 78 | cycle->log = log; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 79 | cycle->old_cycle = old_cycle; |
Igor Sysoev | 090849d | 2004-05-18 20:28:54 +0000 | [diff] [blame] | 80 | cycle->root.len = sizeof(NGX_PREFIX) - 1; |
| 81 | cycle->root.data = (u_char *) NGX_PREFIX; |
Igor Sysoev | ab517d5 | 2004-05-18 15:29:08 +0000 | [diff] [blame] | 82 | |
| 83 | |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 84 | cycle->conf_file.len = old_cycle->conf_file.len; |
| 85 | cycle->conf_file.data = ngx_palloc(pool, old_cycle->conf_file.len + 1); |
| 86 | if (cycle->conf_file.data == NULL) { |
| 87 | ngx_destroy_pool(pool); |
| 88 | return NULL; |
| 89 | } |
| 90 | ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data, |
| 91 | old_cycle->conf_file.len + 1); |
| 92 | |
| 93 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 94 | n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10; |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 95 | |
| 96 | cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *)); |
| 97 | if (cycle->pathes.elts == NULL) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 98 | ngx_destroy_pool(pool); |
| 99 | return NULL; |
| 100 | } |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 101 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 102 | cycle->pathes.nelts = 0; |
| 103 | cycle->pathes.size = sizeof(ngx_path_t *); |
| 104 | cycle->pathes.nalloc = n; |
| 105 | cycle->pathes.pool = pool; |
| 106 | |
| 107 | |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 108 | if (old_cycle->open_files.part.nelts) { |
| 109 | n = old_cycle->open_files.part.nelts; |
| 110 | for (part = old_cycle->open_files.part.next; part; part = part->next) { |
| 111 | n += part->nelts; |
| 112 | } |
| 113 | |
| 114 | } else { |
| 115 | n = 20; |
| 116 | } |
| 117 | |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 118 | if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t)) |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 119 | == NGX_ERROR) |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 120 | { |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 121 | ngx_destroy_pool(pool); |
| 122 | return NULL; |
| 123 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 124 | |
| 125 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 126 | if (old_cycle->shared_memory.part.nelts) { |
| 127 | n = old_cycle->shared_memory.part.nelts; |
| 128 | for (part = old_cycle->shared_memory.part.next; part; part = part->next) |
| 129 | { |
| 130 | n += part->nelts; |
| 131 | } |
| 132 | |
| 133 | } else { |
| 134 | n = 1; |
| 135 | } |
| 136 | |
| 137 | if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t)) |
| 138 | == NGX_ERROR) |
| 139 | { |
| 140 | ngx_destroy_pool(pool); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 145 | cycle->new_log = ngx_log_create_errlog(cycle, NULL); |
| 146 | if (cycle->new_log == NULL) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 147 | ngx_destroy_pool(pool); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 151 | cycle->new_log->file->name = error_log; |
| 152 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 153 | |
| 154 | n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10; |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 155 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 156 | cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t)); |
| 157 | if (cycle->listening.elts == NULL) { |
| 158 | ngx_destroy_pool(pool); |
| 159 | return NULL; |
| 160 | } |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 161 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 162 | cycle->listening.nelts = 0; |
| 163 | cycle->listening.size = sizeof(ngx_listening_t); |
| 164 | cycle->listening.nalloc = n; |
| 165 | cycle->listening.pool = pool; |
| 166 | |
| 167 | |
| 168 | cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *)); |
| 169 | if (cycle->conf_ctx == NULL) { |
| 170 | ngx_destroy_pool(pool); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 175 | for (i = 0; ngx_modules[i]; i++) { |
| 176 | if (ngx_modules[i]->type != NGX_CORE_MODULE) { |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | module = ngx_modules[i]->ctx; |
| 181 | |
| 182 | if (module->create_conf) { |
| 183 | rv = module->create_conf(cycle); |
| 184 | if (rv == NGX_CONF_ERROR) { |
| 185 | ngx_destroy_pool(pool); |
| 186 | return NULL; |
| 187 | } |
| 188 | cycle->conf_ctx[ngx_modules[i]->index] = rv; |
| 189 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
| 193 | ngx_memzero(&conf, sizeof(ngx_conf_t)); |
| 194 | /* STUB: init array ? */ |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 195 | conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t)); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 196 | if (conf.args == NULL) { |
| 197 | ngx_destroy_pool(pool); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
Igor Sysoev | 305a9d8 | 2005-12-26 17:07:48 +0000 | [diff] [blame] | 201 | conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log); |
| 202 | if (conf.temp_pool == NULL) { |
| 203 | ngx_destroy_pool(pool); |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 207 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 208 | conf.ctx = cycle->conf_ctx; |
| 209 | conf.cycle = cycle; |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 210 | conf.pool = pool; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 211 | conf.log = log; |
| 212 | conf.module_type = NGX_CORE_MODULE; |
| 213 | conf.cmd_type = NGX_MAIN_CONF; |
| 214 | |
Igor Sysoev | 50cc0c9 | 2006-10-02 08:50:03 +0000 | [diff] [blame] | 215 | #if 0 |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 216 | log->log_level = NGX_LOG_DEBUG_ALL; |
| 217 | #endif |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 218 | |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 219 | if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) { |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 220 | ngx_destroy_cycle_pools(&conf); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 224 | if (ngx_test_config) { |
| 225 | ngx_log_error(NGX_LOG_INFO, log, 0, |
| 226 | "the configuration file %s syntax is ok", |
| 227 | cycle->conf_file.data); |
| 228 | } |
| 229 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 230 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 231 | for (i = 0; ngx_modules[i]; i++) { |
| 232 | if (ngx_modules[i]->type != NGX_CORE_MODULE) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 233 | continue; |
| 234 | } |
| 235 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 236 | module = ngx_modules[i]->ctx; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 237 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 238 | if (module->init_conf) { |
| 239 | if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index]) |
Igor Sysoev | 305a9d8 | 2005-12-26 17:07:48 +0000 | [diff] [blame] | 240 | == NGX_CONF_ERROR) |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 241 | { |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 242 | ngx_destroy_cycle_pools(&conf); |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 245 | } |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 249 | ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 250 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 251 | #if !(NGX_WIN32) |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 252 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 253 | if (ngx_test_config) { |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 254 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 255 | if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) { |
| 256 | goto failed; |
| 257 | } |
Igor Sysoev | d43bee8 | 2004-11-20 19:52:20 +0000 | [diff] [blame] | 258 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 259 | } else if (!ngx_is_init_cycle(old_cycle)) { |
| 260 | |
| 261 | /* |
| 262 | * we do not create the pid file in the first ngx_init_cycle() call |
| 263 | * because we need to write the demonized process pid |
| 264 | */ |
| 265 | |
| 266 | old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx, |
| 267 | ngx_core_module); |
| 268 | if (ccf->pid.len != old_ccf->pid.len |
| 269 | || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0) |
| 270 | { |
| 271 | /* new pid file name */ |
| 272 | |
| 273 | if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) { |
| 274 | goto failed; |
| 275 | } |
| 276 | |
| 277 | ngx_delete_pidfile(old_cycle); |
Igor Sysoev | d43bee8 | 2004-11-20 19:52:20 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 281 | #endif |
Igor Sysoev | d43bee8 | 2004-11-20 19:52:20 +0000 | [diff] [blame] | 282 | |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 283 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 284 | if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) { |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 285 | goto failed; |
| 286 | } |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 287 | |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 288 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 289 | if (ngx_create_pathes(cycle, ccf->user) != NGX_OK) { |
| 290 | goto failed; |
| 291 | } |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 292 | |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 293 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 294 | /* open the new files */ |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 295 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 296 | part = &cycle->open_files.part; |
| 297 | file = part->elts; |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 298 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 299 | for (i = 0; /* void */ ; i++) { |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 300 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 301 | if (i >= part->nelts) { |
| 302 | if (part->next == NULL) { |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 303 | break; |
| 304 | } |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 305 | part = part->next; |
| 306 | file = part->elts; |
| 307 | i = 0; |
| 308 | } |
| 309 | |
| 310 | if (file[i].name.data == NULL) { |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | file[i].fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR, |
| 315 | NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); |
| 316 | |
| 317 | ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0, |
| 318 | "log: %p %d \"%s\"", |
| 319 | &file[i], file[i].fd, file[i].name.data); |
| 320 | |
| 321 | if (file[i].fd == NGX_INVALID_FILE) { |
| 322 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 323 | ngx_open_file_n " \"%s\" failed", |
| 324 | file[i].name.data); |
| 325 | goto failed; |
| 326 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 327 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 328 | #if (NGX_WIN32) |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 329 | if (ngx_file_append_mode(file[i].fd) != NGX_OK) { |
| 330 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 331 | ngx_file_append_mode_n " \"%s\" failed", |
| 332 | file[i].name.data); |
| 333 | goto failed; |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 334 | } |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 335 | #else |
| 336 | if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) { |
| 337 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 338 | "fcntl(FD_CLOEXEC) \"%s\" failed", |
| 339 | file[i].name.data); |
| 340 | goto failed; |
| 341 | } |
| 342 | #endif |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 345 | cycle->log = cycle->new_log; |
| 346 | pool->log = cycle->new_log; |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 347 | |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 348 | if (cycle->log->log_level == 0) { |
| 349 | cycle->log->log_level = NGX_LOG_ERR; |
| 350 | } |
| 351 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 352 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 353 | /* create shared memory */ |
| 354 | |
| 355 | part = &cycle->shared_memory.part; |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 356 | shm_zone = part->elts; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 357 | |
| 358 | for (i = 0; /* void */ ; i++) { |
| 359 | |
| 360 | if (i >= part->nelts) { |
| 361 | if (part->next == NULL) { |
| 362 | break; |
| 363 | } |
| 364 | part = part->next; |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 365 | shm_zone = part->elts; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 366 | i = 0; |
| 367 | } |
| 368 | |
Igor Sysoev | 14110b3 | 2007-01-06 18:52:06 +0000 | [diff] [blame] | 369 | if (shm_zone[i].shm.size == 0) { |
| 370 | ngx_log_error(NGX_LOG_EMERG, log, 0, |
| 371 | "zero size shared memory zone \"%V\"", |
| 372 | &shm_zone[i].name); |
| 373 | goto failed; |
| 374 | } |
| 375 | |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 376 | shm_zone[i].shm.log = cycle->log; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 377 | |
| 378 | opart = &old_cycle->shared_memory.part; |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 379 | oshm_zone = opart->elts; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 380 | |
| 381 | for (n = 0; /* void */ ; n++) { |
| 382 | |
| 383 | if (n >= opart->nelts) { |
| 384 | if (opart->next == NULL) { |
| 385 | break; |
| 386 | } |
| 387 | opart = opart->next; |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 388 | oshm_zone = opart->elts; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 389 | n = 0; |
| 390 | } |
| 391 | |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 392 | if (shm_zone[i].name.len != oshm_zone[n].name.len) { |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 393 | continue; |
| 394 | } |
| 395 | |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 396 | if (ngx_strncmp(shm_zone[i].name.data, oshm_zone[n].name.data, |
| 397 | shm_zone[i].name.len) |
| 398 | != 0) |
| 399 | { |
| 400 | continue; |
| 401 | } |
| 402 | |
| 403 | if (shm_zone[i].shm.size == oshm_zone[n].shm.size) { |
| 404 | shm_zone[i].shm.addr = oshm_zone[n].shm.addr; |
Igor Sysoev | 4e77a2b | 2007-01-09 15:59:20 +0000 | [diff] [blame] | 405 | |
| 406 | if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data) |
| 407 | != NGX_OK) |
| 408 | { |
| 409 | goto failed; |
| 410 | } |
| 411 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 412 | goto found; |
| 413 | } |
| 414 | |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 415 | ngx_shm_free(&oshm_zone[n].shm); |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 416 | |
| 417 | break; |
| 418 | } |
| 419 | |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 420 | if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) { |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 421 | goto failed; |
| 422 | } |
| 423 | |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 424 | shpool = (ngx_slab_pool_t *) shm_zone[i].shm.addr; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 425 | |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 426 | shpool->end = shm_zone[i].shm.addr + shm_zone[i].shm.size; |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 427 | shpool->min_shift = 3; |
| 428 | |
| 429 | #if (NGX_HAVE_ATOMIC_OPS) |
| 430 | |
| 431 | lock_file = NULL; |
| 432 | |
| 433 | #else |
| 434 | |
| 435 | lock_file = ngx_palloc(cycle->pool, |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 436 | cycle->lock_file.len + shm_zone[i].name.len); |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 437 | |
| 438 | if (lock_file == NULL) { |
| 439 | goto failed; |
| 440 | } |
| 441 | |
| 442 | (void) ngx_cpystrn(ngx_cpymem(lock_file, cycle->lock_file.data, |
| 443 | cycle->lock_file.len), |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 444 | shm_zone[i].name.data, shm_zone[i].name.len + 1); |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 445 | |
| 446 | #endif |
| 447 | |
| 448 | if (ngx_shmtx_create(&shpool->mutex, (void *) &shpool->lock, lock_file) |
| 449 | != NGX_OK) |
| 450 | { |
| 451 | goto failed; |
| 452 | } |
| 453 | |
| 454 | ngx_slab_init(shpool); |
| 455 | |
Igor Sysoev | 4e77a2b | 2007-01-09 15:59:20 +0000 | [diff] [blame] | 456 | if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) { |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 457 | goto failed; |
| 458 | } |
| 459 | |
Igor Sysoev | 67cd336 | 2006-11-20 08:51:45 +0000 | [diff] [blame] | 460 | found: |
| 461 | |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 466 | /* handle the listening sockets */ |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 467 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 468 | if (old_cycle->listening.nelts) { |
| 469 | ls = old_cycle->listening.elts; |
| 470 | for (i = 0; i < old_cycle->listening.nelts; i++) { |
| 471 | ls[i].remain = 0; |
| 472 | } |
| 473 | |
| 474 | nls = cycle->listening.elts; |
| 475 | for (n = 0; n < cycle->listening.nelts; n++) { |
| 476 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 477 | for (i = 0; i < old_cycle->listening.nelts; i++) { |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 478 | if (ls[i].ignore) { |
| 479 | continue; |
| 480 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 481 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 482 | if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) == NGX_OK) |
| 483 | { |
| 484 | nls[n].fd = ls[i].fd; |
| 485 | nls[n].previous = &ls[i]; |
| 486 | ls[i].remain = 1; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 487 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 488 | if (ls[n].backlog != nls[i].backlog) { |
| 489 | nls[n].listen = 1; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 492 | #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) |
| 493 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 494 | /* |
| 495 | * FreeBSD, except the most recent versions, |
| 496 | * could not remove accept filter |
| 497 | */ |
| 498 | nls[n].deferred_accept = ls[i].deferred_accept; |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 499 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 500 | if (ls[i].accept_filter && nls[n].accept_filter) { |
| 501 | if (ngx_strcmp(ls[i].accept_filter, |
| 502 | nls[n].accept_filter) |
| 503 | != 0) |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 504 | { |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 505 | nls[n].delete_deferred = 1; |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 506 | nls[n].add_deferred = 1; |
| 507 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 508 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 509 | } else if (ls[i].accept_filter) { |
| 510 | nls[n].delete_deferred = 1; |
| 511 | |
| 512 | } else if (nls[n].accept_filter) { |
| 513 | nls[n].add_deferred = 1; |
| 514 | } |
| 515 | #endif |
| 516 | |
| 517 | #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) |
| 518 | |
| 519 | if (ls[n].deferred_accept && !nls[n].deferred_accept) { |
| 520 | nls[n].delete_deferred = 1; |
| 521 | |
| 522 | } else if (ls[i].deferred_accept != nls[n].deferred_accept) |
| 523 | { |
| 524 | nls[n].add_deferred = 1; |
| 525 | } |
| 526 | #endif |
| 527 | break; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 531 | if (nls[n].fd == -1) { |
| 532 | nls[n].open = 1; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 536 | } else { |
| 537 | ls = cycle->listening.elts; |
| 538 | for (i = 0; i < cycle->listening.nelts; i++) { |
| 539 | ls[i].open = 1; |
| 540 | #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) |
| 541 | if (ls[i].accept_filter) { |
| 542 | ls[i].add_deferred = 1; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 543 | } |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 544 | #endif |
| 545 | #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) |
| 546 | if (ls[i].deferred_accept) { |
| 547 | ls[i].add_deferred = 1; |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 548 | } |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 549 | #endif |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 553 | if (ngx_open_listening_sockets(cycle) != NGX_OK) { |
| 554 | goto failed; |
| 555 | } |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 556 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 557 | if (!ngx_test_config) { |
| 558 | ngx_configure_listening_socket(cycle); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 561 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 562 | /* commit the new cycle configuration */ |
| 563 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 564 | #if !(NGX_WIN32) |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 565 | |
Igor Sysoev | aad1b89 | 2004-10-03 20:02:06 +0000 | [diff] [blame] | 566 | if (!ngx_test_config && cycle->log->file->fd != STDERR_FILENO) { |
| 567 | |
| 568 | ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 569 | "dup2: %p %d \"%s\"", |
Igor Sysoev | aad1b89 | 2004-10-03 20:02:06 +0000 | [diff] [blame] | 570 | cycle->log->file, |
| 571 | cycle->log->file->fd, cycle->log->file->name.data); |
| 572 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 573 | if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) { |
Igor Sysoev | aad1b89 | 2004-10-03 20:02:06 +0000 | [diff] [blame] | 574 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 575 | "dup2(STDERR) failed"); |
| 576 | /* fatal */ |
| 577 | exit(1); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | #endif |
| 582 | |
| 583 | pool->log = cycle->log; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 584 | |
| 585 | for (i = 0; ngx_modules[i]; i++) { |
| 586 | if (ngx_modules[i]->init_module) { |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 587 | if (ngx_modules[i]->init_module(cycle) != NGX_OK) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 588 | /* fatal */ |
| 589 | exit(1); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
Igor Sysoev | d43bee8 | 2004-11-20 19:52:20 +0000 | [diff] [blame] | 594 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 595 | /* close and delete stuff that lefts from an old cycle */ |
| 596 | |
Igor Sysoev | a269c82 | 2007-01-09 16:00:07 +0000 | [diff] [blame^] | 597 | /* close the unnecessary listening sockets */ |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 598 | |
| 599 | ls = old_cycle->listening.elts; |
| 600 | for (i = 0; i < old_cycle->listening.nelts; i++) { |
| 601 | if (ls[i].remain) { |
| 602 | continue; |
| 603 | } |
| 604 | |
| 605 | if (ngx_close_socket(ls[i].fd) == -1) { |
| 606 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
Igor Sysoev | 31eb8c0 | 2005-09-23 11:02:22 +0000 | [diff] [blame] | 607 | ngx_close_socket_n " listening socket on %V failed", |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 608 | &ls[i].addr_text); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | |
Igor Sysoev | a269c82 | 2007-01-09 16:00:07 +0000 | [diff] [blame^] | 613 | /* close the unnecessary open files */ |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 614 | |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 615 | part = &old_cycle->open_files.part; |
| 616 | file = part->elts; |
| 617 | |
| 618 | for (i = 0; /* void */ ; i++) { |
| 619 | |
| 620 | if (i >= part->nelts) { |
| 621 | if (part->next == NULL) { |
| 622 | break; |
| 623 | } |
| 624 | part = part->next; |
| 625 | file = part->elts; |
| 626 | i = 0; |
| 627 | } |
| 628 | |
Igor Sysoev | aad1b89 | 2004-10-03 20:02:06 +0000 | [diff] [blame] | 629 | if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 630 | continue; |
| 631 | } |
| 632 | |
| 633 | if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { |
| 634 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 635 | ngx_close_file_n " \"%s\" failed", |
| 636 | file[i].name.data); |
| 637 | } |
| 638 | } |
| 639 | |
Igor Sysoev | 305a9d8 | 2005-12-26 17:07:48 +0000 | [diff] [blame] | 640 | ngx_destroy_pool(conf.temp_pool); |
| 641 | |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 642 | if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) { |
| 643 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 644 | ngx_destroy_pool(old_cycle->pool); |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 645 | cycle->old_cycle = NULL; |
| 646 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 647 | return cycle; |
| 648 | } |
| 649 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 650 | |
| 651 | if (ngx_temp_pool == NULL) { |
| 652 | ngx_temp_pool = ngx_create_pool(128, cycle->log); |
| 653 | if (ngx_temp_pool == NULL) { |
| 654 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| 655 | "can not create ngx_temp_pool"); |
| 656 | exit(1); |
| 657 | } |
| 658 | |
| 659 | n = 10; |
| 660 | ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool, |
| 661 | n * sizeof(ngx_cycle_t *)); |
| 662 | if (ngx_old_cycles.elts == NULL) { |
| 663 | exit(1); |
| 664 | } |
| 665 | ngx_old_cycles.nelts = 0; |
| 666 | ngx_old_cycles.size = sizeof(ngx_cycle_t *); |
| 667 | ngx_old_cycles.nalloc = n; |
| 668 | ngx_old_cycles.pool = ngx_temp_pool; |
| 669 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 670 | ngx_cleaner_event.handler = ngx_clean_old_cycles; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 671 | ngx_cleaner_event.log = cycle->log; |
| 672 | ngx_cleaner_event.data = &dumb; |
| 673 | dumb.fd = (ngx_socket_t) -1; |
| 674 | } |
| 675 | |
| 676 | ngx_temp_pool->log = cycle->log; |
| 677 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 678 | old = ngx_array_push(&ngx_old_cycles); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 679 | if (old == NULL) { |
| 680 | exit(1); |
| 681 | } |
| 682 | *old = old_cycle; |
| 683 | |
| 684 | if (!ngx_cleaner_event.timer_set) { |
| 685 | ngx_add_timer(&ngx_cleaner_event, 30000); |
| 686 | ngx_cleaner_event.timer_set = 1; |
| 687 | } |
| 688 | |
| 689 | return cycle; |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 690 | |
| 691 | |
| 692 | failed: |
| 693 | |
| 694 | /* rollback the new cycle configuration */ |
| 695 | |
| 696 | part = &cycle->open_files.part; |
| 697 | file = part->elts; |
| 698 | |
| 699 | for (i = 0; /* void */ ; i++) { |
| 700 | |
| 701 | if (i >= part->nelts) { |
| 702 | if (part->next == NULL) { |
| 703 | break; |
| 704 | } |
| 705 | part = part->next; |
| 706 | file = part->elts; |
| 707 | i = 0; |
| 708 | } |
| 709 | |
| 710 | if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) { |
| 711 | continue; |
| 712 | } |
| 713 | |
| 714 | if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { |
| 715 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 716 | ngx_close_file_n " \"%s\" failed", |
| 717 | file[i].name.data); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | if (ngx_test_config) { |
| 722 | ngx_destroy_cycle_pools(&conf); |
| 723 | return NULL; |
| 724 | } |
| 725 | |
| 726 | ls = cycle->listening.elts; |
| 727 | for (i = 0; i < cycle->listening.nelts; i++) { |
| 728 | if (ls[i].fd == -1 || !ls[i].open) { |
| 729 | continue; |
| 730 | } |
| 731 | |
| 732 | if (ngx_close_socket(ls[i].fd) == -1) { |
| 733 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
| 734 | ngx_close_socket_n " %V failed", |
| 735 | &ls[i].addr_text); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | ngx_destroy_cycle_pools(&conf); |
| 740 | |
| 741 | return NULL; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 745 | static void |
| 746 | ngx_destroy_cycle_pools(ngx_conf_t *conf) |
| 747 | { |
Igor Sysoev | 9e58019 | 2006-02-01 18:22:15 +0000 | [diff] [blame] | 748 | ngx_destroy_pool(conf->temp_pool); |
| 749 | ngx_destroy_pool(conf->pool); |
| 750 | } |
| 751 | |
| 752 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 753 | static ngx_int_t |
| 754 | ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2) |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 755 | { |
| 756 | struct sockaddr_in *sin1, *sin2; |
| 757 | |
| 758 | /* AF_INET only */ |
| 759 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 760 | if (sa1->sa_family != AF_INET || sa2->sa_family != AF_INET) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 761 | return NGX_DECLINED; |
| 762 | } |
| 763 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 764 | sin1 = (struct sockaddr_in *) sa1; |
| 765 | sin2 = (struct sockaddr_in *) sa2; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 766 | |
| 767 | if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) { |
| 768 | return NGX_DECLINED; |
| 769 | } |
| 770 | |
| 771 | if (sin1->sin_port != sin2->sin_port) { |
| 772 | return NGX_DECLINED; |
| 773 | } |
| 774 | |
| 775 | return NGX_OK; |
| 776 | } |
| 777 | |
| 778 | |
| 779 | #if !(NGX_WIN32) |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 780 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 781 | ngx_int_t |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 782 | ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log) |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 783 | { |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 784 | size_t len; |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 785 | ngx_uint_t trunc; |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 786 | ngx_file_t file; |
Igor Sysoev | 9ba14ac | 2006-12-22 20:30:26 +0000 | [diff] [blame] | 787 | u_char pid[NGX_INT64_LEN + 2]; |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 788 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 789 | ngx_memzero(&file, sizeof(ngx_file_t)); |
Igor Sysoev | 31eb8c0 | 2005-09-23 11:02:22 +0000 | [diff] [blame] | 790 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 791 | file.name = *name; |
| 792 | file.log = log; |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 793 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 794 | trunc = ngx_test_config ? 0 : NGX_FILE_TRUNCATE; |
Igor Sysoev | 090849d | 2004-05-18 20:28:54 +0000 | [diff] [blame] | 795 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 796 | file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR, |
Igor Sysoev | 090849d | 2004-05-18 20:28:54 +0000 | [diff] [blame] | 797 | NGX_FILE_CREATE_OR_OPEN|trunc); |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 798 | |
| 799 | if (file.fd == NGX_INVALID_FILE) { |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 800 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 801 | ngx_open_file_n " \"%s\" failed", file.name.data); |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 802 | return NGX_ERROR; |
| 803 | } |
| 804 | |
Igor Sysoev | 090849d | 2004-05-18 20:28:54 +0000 | [diff] [blame] | 805 | if (!ngx_test_config) { |
Igor Sysoev | 9ba14ac | 2006-12-22 20:30:26 +0000 | [diff] [blame] | 806 | len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 807 | |
Igor Sysoev | 090849d | 2004-05-18 20:28:54 +0000 | [diff] [blame] | 808 | if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) { |
| 809 | return NGX_ERROR; |
| 810 | } |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 813 | if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 814 | ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 815 | ngx_close_file_n " \"%s\" failed", file.name.data); |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | return NGX_OK; |
| 819 | } |
| 820 | |
| 821 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 822 | void |
| 823 | ngx_delete_pidfile(ngx_cycle_t *cycle) |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 824 | { |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 825 | u_char *name; |
| 826 | ngx_core_conf_t *ccf; |
| 827 | |
| 828 | ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
| 829 | |
Igor Sysoev | 31eb8c0 | 2005-09-23 11:02:22 +0000 | [diff] [blame] | 830 | name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data; |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 831 | |
| 832 | if (ngx_delete_file(name) == NGX_FILE_ERROR) { |
| 833 | ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
| 834 | ngx_delete_file_n " \"%s\" failed", name); |
| 835 | } |
| 836 | } |
| 837 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 838 | #endif |
| 839 | |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 840 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 841 | static ngx_int_t |
| 842 | ngx_test_lockfile(u_char *file, ngx_log_t *log) |
| 843 | { |
| 844 | #if !(NGX_HAVE_ATOMIC_OPS) |
| 845 | ngx_fd_t fd; |
| 846 | |
| 847 | fd = ngx_open_file(file, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN); |
| 848 | |
| 849 | if (fd == NGX_INVALID_FILE) { |
| 850 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 851 | ngx_open_file_n " \"%s\" failed", file); |
| 852 | return NGX_ERROR; |
| 853 | } |
| 854 | |
| 855 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 856 | ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
| 857 | ngx_close_file_n " \"%s\" failed", file); |
| 858 | } |
| 859 | |
| 860 | if (ngx_delete_file(file) == NGX_FILE_ERROR) { |
| 861 | ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
| 862 | ngx_delete_file_n " \"%s\" failed", file); |
| 863 | } |
| 864 | |
| 865 | #endif |
| 866 | |
| 867 | return NGX_OK; |
| 868 | } |
| 869 | |
| 870 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 871 | void |
| 872 | ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user) |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 873 | { |
| 874 | ngx_fd_t fd; |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 875 | ngx_uint_t i; |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 876 | ngx_list_part_t *part; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 877 | ngx_open_file_t *file; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 878 | #if !(NGX_WIN32) |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 879 | ngx_file_info_t fi; |
| 880 | #endif |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 881 | |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 882 | part = &cycle->open_files.part; |
| 883 | file = part->elts; |
| 884 | |
| 885 | for (i = 0; /* void */ ; i++) { |
| 886 | |
| 887 | if (i >= part->nelts) { |
| 888 | if (part->next == NULL) { |
| 889 | break; |
| 890 | } |
| 891 | part = part->next; |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 892 | file = part->elts; |
Igor Sysoev | b9e3441 | 2004-09-03 15:50:30 +0000 | [diff] [blame] | 893 | i = 0; |
| 894 | } |
| 895 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 896 | if (file[i].name.data == NULL) { |
| 897 | continue; |
| 898 | } |
| 899 | |
Igor Sysoev | 697d1ae | 2005-10-27 15:46:13 +0000 | [diff] [blame] | 900 | if (file[i].buffer && file[i].pos - file[i].buffer != 0) { |
| 901 | ngx_write_fd(file[i].fd, file[i].buffer, |
| 902 | file[i].pos - file[i].buffer); |
| 903 | file[i].pos = file[i].buffer; |
| 904 | } |
| 905 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 906 | fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR, |
| 907 | NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); |
| 908 | |
| 909 | ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
| 910 | "reopen file \"%s\", old:%d new:%d", |
| 911 | file[i].name.data, file[i].fd, fd); |
| 912 | |
| 913 | if (fd == NGX_INVALID_FILE) { |
| 914 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 915 | ngx_open_file_n " \"%s\" failed", file[i].name.data); |
| 916 | continue; |
| 917 | } |
| 918 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 919 | #if (NGX_WIN32) |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 920 | if (ngx_file_append_mode(fd) == NGX_ERROR) { |
| 921 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 922 | ngx_file_append_mode_n " \"%s\" failed", |
| 923 | file[i].name.data); |
| 924 | |
| 925 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 926 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 927 | ngx_close_file_n " \"%s\" failed", |
| 928 | file[i].name.data); |
| 929 | } |
| 930 | |
| 931 | continue; |
| 932 | } |
Igor Sysoev | 0911f77 | 2004-01-14 18:19:42 +0000 | [diff] [blame] | 933 | #else |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame] | 934 | if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) { |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 935 | |
| 936 | if (ngx_file_info((const char *) file[i].name.data, &fi) == -1) { |
| 937 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 938 | ngx_file_info_n " \"%s\" failed", |
| 939 | file[i].name.data); |
| 940 | |
| 941 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 942 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 943 | ngx_close_file_n " \"%s\" failed", |
| 944 | file[i].name.data); |
| 945 | } |
| 946 | } |
| 947 | |
Igor Sysoev | d43bee8 | 2004-11-20 19:52:20 +0000 | [diff] [blame] | 948 | if (fi.st_uid != user) { |
| 949 | if (chown((const char *) file[i].name.data, user, -1) == -1) { |
| 950 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 951 | "chown(\"%s\", %d) failed", |
| 952 | file[i].name.data, user); |
| 953 | |
| 954 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 955 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 956 | ngx_close_file_n " \"%s\" failed", |
| 957 | file[i].name.data); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 962 | if ((fi.st_mode & (S_IRUSR|S_IWUSR)) != (S_IRUSR|S_IWUSR)) { |
| 963 | |
| 964 | fi.st_mode |= (S_IRUSR|S_IWUSR); |
| 965 | |
| 966 | if (chmod((const char *) file[i].name.data, fi.st_mode) == -1) { |
| 967 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
Igor Sysoev | d43bee8 | 2004-11-20 19:52:20 +0000 | [diff] [blame] | 968 | "chmod() \"%s\" failed", file[i].name.data); |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 969 | |
| 970 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 971 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 972 | ngx_close_file_n " \"%s\" failed", |
| 973 | file[i].name.data); |
| 974 | } |
| 975 | } |
| 976 | } |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Igor Sysoev | 0911f77 | 2004-01-14 18:19:42 +0000 | [diff] [blame] | 979 | if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { |
| 980 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 981 | "fcntl(FD_CLOEXEC) \"%s\" failed", |
| 982 | file[i].name.data); |
| 983 | |
| 984 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 985 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 986 | ngx_close_file_n " \"%s\" failed", |
| 987 | file[i].name.data); |
| 988 | } |
| 989 | |
| 990 | continue; |
| 991 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 992 | #endif |
| 993 | |
| 994 | if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { |
| 995 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 996 | ngx_close_file_n " \"%s\" failed", |
| 997 | file[i].name.data); |
| 998 | } |
| 999 | |
| 1000 | file[i].fd = fd; |
| 1001 | } |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 1002 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1003 | #if !(NGX_WIN32) |
Igor Sysoev | a893eab | 2004-03-11 21:34:52 +0000 | [diff] [blame] | 1004 | |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 1005 | if (cycle->log->file->fd != STDERR_FILENO) { |
| 1006 | if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) { |
| 1007 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 1008 | "dup2(STDERR) failed"); |
| 1009 | } |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 1010 | } |
Igor Sysoev | a893eab | 2004-03-11 21:34:52 +0000 | [diff] [blame] | 1011 | |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 1012 | #endif |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 1016 | ngx_shm_zone_t * |
| 1017 | ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag) |
| 1018 | { |
| 1019 | ngx_uint_t i; |
| 1020 | ngx_shm_zone_t *shm_zone; |
| 1021 | ngx_list_part_t *part; |
| 1022 | |
| 1023 | part = &cf->cycle->shared_memory.part; |
| 1024 | shm_zone = part->elts; |
| 1025 | |
| 1026 | for (i = 0; /* void */ ; i++) { |
| 1027 | |
| 1028 | if (i >= part->nelts) { |
| 1029 | if (part->next == NULL) { |
| 1030 | break; |
| 1031 | } |
| 1032 | part = part->next; |
| 1033 | shm_zone = part->elts; |
| 1034 | i = 0; |
| 1035 | } |
| 1036 | |
| 1037 | if (name->len != shm_zone[i].name.len) { |
| 1038 | continue; |
| 1039 | } |
| 1040 | |
| 1041 | if (ngx_strncmp(name->data, shm_zone[i].name.data, name->len) != 0) { |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | if (size && size != shm_zone[i].shm.size) { |
| 1046 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 1047 | "the size %uz of shared memory zone \"%V\" " |
Igor Sysoev | b94b180 | 2007-01-03 15:43:01 +0000 | [diff] [blame] | 1048 | "conflicts with already declared size %uz", |
| 1049 | size, &shm_zone[i].name, shm_zone[i].shm.size); |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 1050 | return NULL; |
| 1051 | } |
| 1052 | |
| 1053 | if (tag != shm_zone[i].tag) { |
| 1054 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 1055 | "the shared memory zone \"%V\" is " |
Igor Sysoev | b94b180 | 2007-01-03 15:43:01 +0000 | [diff] [blame] | 1056 | "already declared for a different use", |
| 1057 | &shm_zone[i].name); |
Igor Sysoev | 28c7f76 | 2007-01-02 23:54:14 +0000 | [diff] [blame] | 1058 | return NULL; |
| 1059 | } |
| 1060 | |
| 1061 | return &shm_zone[i]; |
| 1062 | } |
| 1063 | |
| 1064 | shm_zone = ngx_list_push(&cf->cycle->shared_memory); |
| 1065 | |
| 1066 | if (shm_zone == NULL) { |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | |
| 1070 | shm_zone->data = NULL; |
| 1071 | shm_zone->shm.log = cf->cycle->log; |
| 1072 | shm_zone->shm.size = size; |
| 1073 | shm_zone->init = NULL; |
| 1074 | shm_zone->name = *name; |
| 1075 | shm_zone->tag = tag; |
| 1076 | |
| 1077 | return shm_zone; |
| 1078 | } |
| 1079 | |
| 1080 | |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 1081 | static void |
| 1082 | ngx_clean_old_cycles(ngx_event_t *ev) |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1083 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 1084 | ngx_uint_t i, n, found, live; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1085 | ngx_log_t *log; |
| 1086 | ngx_cycle_t **cycle; |
| 1087 | |
| 1088 | log = ngx_cycle->log; |
| 1089 | ngx_temp_pool->log = log; |
| 1090 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 1091 | ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles"); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1092 | |
| 1093 | live = 0; |
| 1094 | |
| 1095 | cycle = ngx_old_cycles.elts; |
| 1096 | for (i = 0; i < ngx_old_cycles.nelts; i++) { |
| 1097 | |
| 1098 | if (cycle[i] == NULL) { |
| 1099 | continue; |
| 1100 | } |
| 1101 | |
| 1102 | found = 0; |
| 1103 | |
| 1104 | for (n = 0; n < cycle[i]->connection_n; n++) { |
Igor Sysoev | 7845223 | 2005-10-12 13:50:36 +0000 | [diff] [blame] | 1105 | if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1106 | found = 1; |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 1107 | |
| 1108 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%d", n); |
| 1109 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | if (found) { |
| 1115 | live = 1; |
| 1116 | continue; |
| 1117 | } |
| 1118 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 1119 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %d", i); |
| 1120 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1121 | ngx_destroy_pool(cycle[i]->pool); |
| 1122 | cycle[i] = NULL; |
| 1123 | } |
| 1124 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 1125 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %d", live); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1126 | |
| 1127 | if (live) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1128 | ngx_add_timer(ev, 30000); |
| 1129 | |
| 1130 | } else { |
| 1131 | ngx_destroy_pool(ngx_temp_pool); |
| 1132 | ngx_temp_pool = NULL; |
| 1133 | ngx_old_cycles.nelts = 0; |
| 1134 | } |
| 1135 | } |