Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 1 | |
| 2 | #include <ngx_config.h> |
| 3 | #include <ngx_core.h> |
| 4 | #include <ngx_event.h> |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 5 | |
| 6 | |
| 7 | static void ngx_clean_old_cycles(ngx_event_t *ev); |
| 8 | |
| 9 | |
| 10 | volatile ngx_cycle_t *ngx_cycle; |
| 11 | ngx_array_t ngx_old_cycles; |
| 12 | |
| 13 | static ngx_pool_t *ngx_temp_pool; |
| 14 | static ngx_event_t ngx_cleaner_event; |
| 15 | |
Igor Sysoev | 630ad0c | 2004-04-16 05:14:16 +0000 | [diff] [blame] | 16 | ngx_uint_t ngx_test_config; |
| 17 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 18 | |
| 19 | /* STUB NAME */ |
| 20 | static ngx_connection_t dumb; |
| 21 | /* STUB */ |
| 22 | |
| 23 | |
| 24 | ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle) |
| 25 | { |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 26 | void *rv; |
Igor Sysoev | ab517d5 | 2004-05-18 15:29:08 +0000 | [diff] [blame^] | 27 | u_char *root; |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 28 | ngx_uint_t i, n, failed; |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 29 | ngx_log_t *log; |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 30 | ngx_conf_t conf; |
| 31 | ngx_pool_t *pool; |
| 32 | ngx_cycle_t *cycle, **old; |
| 33 | ngx_socket_t fd; |
| 34 | ngx_open_file_t *file; |
| 35 | ngx_listening_t *ls, *nls; |
| 36 | ngx_core_module_t *module; |
Igor Sysoev | ab517d5 | 2004-05-18 15:29:08 +0000 | [diff] [blame^] | 37 | char cwd[NGX_MAX_PATH + 1]; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 38 | |
| 39 | log = old_cycle->log; |
| 40 | |
| 41 | if (!(pool = ngx_create_pool(16 * 1024, log))) { |
| 42 | return NULL; |
| 43 | } |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 44 | pool->log = log; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 45 | |
| 46 | if (!(cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)))) { |
| 47 | ngx_destroy_pool(pool); |
| 48 | return NULL; |
| 49 | } |
| 50 | cycle->pool = pool; |
Igor Sysoev | cccc552 | 2004-04-14 20:34:05 +0000 | [diff] [blame] | 51 | cycle->log = log; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 52 | cycle->old_cycle = old_cycle; |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 53 | cycle->conf_file = old_cycle->conf_file; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 54 | |
| 55 | |
Igor Sysoev | ab517d5 | 2004-05-18 15:29:08 +0000 | [diff] [blame^] | 56 | for (i = cycle->conf_file.len; i > 0; i--) { |
| 57 | if (cycle->conf_file.data[i] == '/') { |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (i == 0 && cycle->conf_file.data[i] != '/') { |
| 63 | if (ngx_getcwd(cwd, NGX_MAX_PATH) == 0) { |
| 64 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 65 | ngx_getcwd_n " failed"); |
| 66 | ngx_destroy_pool(pool); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | for ( /* void */; i < NGX_MAX_PATH && cwd[i]; i++) /* void */; |
| 71 | cwd[i] = '/'; |
| 72 | cwd[i + 1] = '\0'; |
| 73 | |
| 74 | root = (u_char *) cwd; |
| 75 | |
| 76 | } else { |
| 77 | root = cycle->conf_file.data; |
| 78 | } |
| 79 | |
| 80 | cycle->root.len = ++i; |
| 81 | cycle->root.data = ngx_palloc(pool, ++i); |
| 82 | if (cycle->root.data == NULL) { |
| 83 | ngx_destroy_pool(pool); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | ngx_cpystrn(cycle->root.data, root, i); |
| 88 | |
| 89 | ngx_log_error(NGX_LOG_INFO, log, 0, "root: %s", cycle->root.data); |
| 90 | |
| 91 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 92 | n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10; |
| 93 | if (!(cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *)))) { |
| 94 | ngx_destroy_pool(pool); |
| 95 | return NULL; |
| 96 | } |
| 97 | cycle->pathes.nelts = 0; |
| 98 | cycle->pathes.size = sizeof(ngx_path_t *); |
| 99 | cycle->pathes.nalloc = n; |
| 100 | cycle->pathes.pool = pool; |
| 101 | |
| 102 | |
| 103 | n = old_cycle->open_files.nelts ? old_cycle->open_files.nelts : 20; |
| 104 | cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t)); |
| 105 | if (cycle->open_files.elts == NULL) { |
| 106 | ngx_destroy_pool(pool); |
| 107 | return NULL; |
| 108 | } |
| 109 | cycle->open_files.nelts = 0; |
| 110 | cycle->open_files.size = sizeof(ngx_open_file_t); |
| 111 | cycle->open_files.nalloc = n; |
| 112 | cycle->open_files.pool = pool; |
| 113 | |
| 114 | |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 115 | if (!(cycle->new_log = ngx_log_create_errlog(cycle, NULL))) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 116 | ngx_destroy_pool(pool); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10; |
| 122 | cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t)); |
| 123 | if (cycle->listening.elts == NULL) { |
| 124 | ngx_destroy_pool(pool); |
| 125 | return NULL; |
| 126 | } |
| 127 | cycle->listening.nelts = 0; |
| 128 | cycle->listening.size = sizeof(ngx_listening_t); |
| 129 | cycle->listening.nalloc = n; |
| 130 | cycle->listening.pool = pool; |
| 131 | |
| 132 | |
| 133 | cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *)); |
| 134 | if (cycle->conf_ctx == NULL) { |
| 135 | ngx_destroy_pool(pool); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 140 | for (i = 0; ngx_modules[i]; i++) { |
| 141 | if (ngx_modules[i]->type != NGX_CORE_MODULE) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | module = ngx_modules[i]->ctx; |
| 146 | |
| 147 | if (module->create_conf) { |
| 148 | rv = module->create_conf(cycle); |
| 149 | if (rv == NGX_CONF_ERROR) { |
| 150 | ngx_destroy_pool(pool); |
| 151 | return NULL; |
| 152 | } |
| 153 | cycle->conf_ctx[ngx_modules[i]->index] = rv; |
| 154 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | ngx_memzero(&conf, sizeof(ngx_conf_t)); |
| 159 | /* STUB: init array ? */ |
| 160 | conf.args = ngx_create_array(pool, 10, sizeof(ngx_str_t)); |
| 161 | if (conf.args == NULL) { |
| 162 | ngx_destroy_pool(pool); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | conf.ctx = cycle->conf_ctx; |
| 167 | conf.cycle = cycle; |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 168 | conf.pool = pool; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 169 | conf.log = log; |
| 170 | conf.module_type = NGX_CORE_MODULE; |
| 171 | conf.cmd_type = NGX_MAIN_CONF; |
| 172 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 173 | |
Igor Sysoev | e9b2cb1 | 2004-02-09 20:47:18 +0000 | [diff] [blame] | 174 | if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 175 | ngx_destroy_pool(pool); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 179 | if (ngx_test_config) { |
| 180 | ngx_log_error(NGX_LOG_INFO, log, 0, |
| 181 | "the configuration file %s syntax is ok", |
| 182 | cycle->conf_file.data); |
| 183 | } |
| 184 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 185 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 186 | for (i = 0; ngx_modules[i]; i++) { |
| 187 | if (ngx_modules[i]->type != NGX_CORE_MODULE) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 188 | continue; |
| 189 | } |
| 190 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 191 | module = ngx_modules[i]->ctx; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 192 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 193 | if (module->init_conf) { |
| 194 | if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index]) |
| 195 | == NGX_CONF_ERROR) |
| 196 | { |
| 197 | ngx_destroy_pool(pool); |
| 198 | return NULL; |
| 199 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 200 | } |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | failed = 0; |
| 205 | |
| 206 | |
| 207 | #if !(WIN32) |
| 208 | if (ngx_create_pidfile(cycle, old_cycle) == NGX_ERROR) { |
| 209 | failed = 1; |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | |
| 214 | if (!failed) { |
| 215 | file = cycle->open_files.elts; |
| 216 | for (i = 0; i < cycle->open_files.nelts; i++) { |
| 217 | if (file[i].name.data == NULL) { |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | file[i].fd = ngx_open_file(file[i].name.data, |
| 222 | NGX_FILE_RDWR, |
| 223 | NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); |
| 224 | |
| 225 | if (file[i].fd == NGX_INVALID_FILE) { |
| 226 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 227 | ngx_open_file_n " \"%s\" failed", |
| 228 | file[i].name.data); |
| 229 | failed = 1; |
| 230 | break; |
| 231 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 232 | |
| 233 | #if (WIN32) |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 234 | if (ngx_file_append_mode(file[i].fd) == NGX_ERROR) { |
| 235 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 236 | ngx_file_append_mode_n " \"%s\" failed", |
| 237 | file[i].name.data); |
| 238 | failed = 1; |
| 239 | break; |
| 240 | } |
Igor Sysoev | 0911f77 | 2004-01-14 18:19:42 +0000 | [diff] [blame] | 241 | #else |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 242 | if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) { |
| 243 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 244 | "fcntl(FD_CLOEXEC) \"%s\" failed", |
| 245 | file[i].name.data); |
| 246 | failed = 1; |
| 247 | break; |
| 248 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 249 | #endif |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 250 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Igor Sysoev | 68df19d | 2004-04-15 15:34:36 +0000 | [diff] [blame] | 253 | cycle->log = cycle->new_log; |
| 254 | pool->log = cycle->new_log; |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 255 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 256 | if (!failed) { |
| 257 | if (old_cycle->listening.nelts) { |
| 258 | ls = old_cycle->listening.elts; |
| 259 | for (i = 0; i < old_cycle->listening.nelts; i++) { |
| 260 | ls[i].remain = 0; |
| 261 | } |
| 262 | |
| 263 | nls = cycle->listening.elts; |
| 264 | for (n = 0; n < cycle->listening.nelts; n++) { |
| 265 | for (i = 0; i < old_cycle->listening.nelts; i++) { |
| 266 | if (ls[i].ignore) { |
| 267 | continue; |
| 268 | } |
| 269 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 270 | if (ngx_memcmp(nls[n].sockaddr, |
| 271 | ls[i].sockaddr, ls[i].socklen) == 0) |
| 272 | { |
| 273 | fd = ls[i].fd; |
| 274 | #if (WIN32) |
| 275 | /* |
| 276 | * Winsock assignes a socket number divisible by 4 so |
| 277 | * to find a connection we divide a socket number by 4. |
| 278 | */ |
| 279 | |
| 280 | fd /= 4; |
| 281 | #endif |
| 282 | if (fd >= (ngx_socket_t) cycle->connection_n) { |
| 283 | ngx_log_error(NGX_LOG_EMERG, log, 0, |
| 284 | "%d connections is not enough to hold " |
| 285 | "an open listening socket on %s, " |
| 286 | "required at least %d connections", |
| 287 | cycle->connection_n, |
| 288 | ls[i].addr_text.data, fd); |
| 289 | failed = 1; |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | nls[n].fd = ls[i].fd; |
| 294 | nls[i].remain = 1; |
| 295 | ls[i].remain = 1; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (nls[n].fd == -1) { |
| 301 | nls[n].new = 1; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | } else { |
| 306 | ls = cycle->listening.elts; |
| 307 | for (i = 0; i < cycle->listening.nelts; i++) { |
| 308 | ls[i].new = 1; |
| 309 | } |
| 310 | } |
| 311 | |
Igor Sysoev | 630ad0c | 2004-04-16 05:14:16 +0000 | [diff] [blame] | 312 | if (!ngx_test_config && !failed) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 313 | if (ngx_open_listening_sockets(cycle) == NGX_ERROR) { |
| 314 | failed = 1; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Igor Sysoev | a893eab | 2004-03-11 21:34:52 +0000 | [diff] [blame] | 319 | #if !(WIN32) |
| 320 | |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 321 | if (!failed && !ngx_test_config) { |
| 322 | if (dup2(cycle->log->file->fd, STDERR_FILENO) == NGX_ERROR) { |
| 323 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 324 | "dup2(STDERR) failed"); |
| 325 | failed = 1; |
| 326 | } |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 327 | } |
Igor Sysoev | a893eab | 2004-03-11 21:34:52 +0000 | [diff] [blame] | 328 | |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 329 | #endif |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 330 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 331 | if (failed) { |
| 332 | |
| 333 | /* rollback the new cycle configuration */ |
| 334 | |
| 335 | file = cycle->open_files.elts; |
| 336 | for (i = 0; i < cycle->open_files.nelts; i++) { |
| 337 | if (file[i].fd == NGX_INVALID_FILE) { |
| 338 | continue; |
| 339 | } |
| 340 | |
| 341 | if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { |
| 342 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 343 | ngx_close_file_n " \"%s\" failed", |
| 344 | file[i].name.data); |
| 345 | } |
| 346 | } |
| 347 | |
Igor Sysoev | 9bfb434 | 2004-04-18 19:06:02 +0000 | [diff] [blame] | 348 | if (ngx_test_config) { |
| 349 | ngx_destroy_pool(pool); |
| 350 | return NULL; |
| 351 | } |
| 352 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 353 | ls = cycle->listening.elts; |
| 354 | for (i = 0; i < cycle->listening.nelts; i++) { |
| 355 | if (ls[i].new && ls[i].fd == -1) { |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | if (ngx_close_socket(ls[i].fd) == -1) { |
| 360 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
| 361 | ngx_close_socket_n " %s failed", |
| 362 | ls[i].addr_text.data); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | ngx_destroy_pool(pool); |
| 367 | return NULL; |
| 368 | } |
| 369 | |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 370 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 371 | /* commit the new cycle configuration */ |
| 372 | |
| 373 | pool->log = cycle->log; |
| 374 | |
| 375 | |
| 376 | for (i = 0; ngx_modules[i]; i++) { |
| 377 | if (ngx_modules[i]->init_module) { |
| 378 | if (ngx_modules[i]->init_module(cycle) == NGX_ERROR) { |
| 379 | /* fatal */ |
| 380 | exit(1); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* close and delete stuff that lefts from an old cycle */ |
| 386 | |
| 387 | /* close the unneeded listening sockets */ |
| 388 | |
| 389 | ls = old_cycle->listening.elts; |
| 390 | for (i = 0; i < old_cycle->listening.nelts; i++) { |
| 391 | if (ls[i].remain) { |
| 392 | continue; |
| 393 | } |
| 394 | |
| 395 | if (ngx_close_socket(ls[i].fd) == -1) { |
| 396 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
| 397 | ngx_close_socket_n " %s failed", |
| 398 | ls[i].addr_text.data); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | |
| 403 | /* close the unneeded open files */ |
| 404 | |
| 405 | file = old_cycle->open_files.elts; |
| 406 | for (i = 0; i < old_cycle->open_files.nelts; i++) { |
| 407 | if (file[i].fd == NGX_INVALID_FILE) { |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { |
| 412 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 413 | ngx_close_file_n " \"%s\" failed", |
| 414 | file[i].name.data); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (old_cycle->connections == NULL) { |
| 419 | /* an old cycle is an init cycle */ |
| 420 | ngx_destroy_pool(old_cycle->pool); |
| 421 | return cycle; |
| 422 | } |
| 423 | |
Igor Sysoev | 3d58f8c | 2004-01-08 08:47:17 +0000 | [diff] [blame] | 424 | if (ngx_process == NGX_PROCESS_MASTER) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 425 | ngx_destroy_pool(old_cycle->pool); |
| 426 | return cycle; |
| 427 | } |
| 428 | |
| 429 | if (ngx_temp_pool == NULL) { |
| 430 | ngx_temp_pool = ngx_create_pool(128, cycle->log); |
| 431 | if (ngx_temp_pool == NULL) { |
| 432 | ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| 433 | "can not create ngx_temp_pool"); |
| 434 | exit(1); |
| 435 | } |
| 436 | |
| 437 | n = 10; |
| 438 | ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool, |
| 439 | n * sizeof(ngx_cycle_t *)); |
| 440 | if (ngx_old_cycles.elts == NULL) { |
| 441 | exit(1); |
| 442 | } |
| 443 | ngx_old_cycles.nelts = 0; |
| 444 | ngx_old_cycles.size = sizeof(ngx_cycle_t *); |
| 445 | ngx_old_cycles.nalloc = n; |
| 446 | ngx_old_cycles.pool = ngx_temp_pool; |
| 447 | |
| 448 | ngx_cleaner_event.event_handler = ngx_clean_old_cycles; |
| 449 | ngx_cleaner_event.log = cycle->log; |
| 450 | ngx_cleaner_event.data = &dumb; |
| 451 | dumb.fd = (ngx_socket_t) -1; |
| 452 | } |
| 453 | |
| 454 | ngx_temp_pool->log = cycle->log; |
| 455 | |
| 456 | old = ngx_push_array(&ngx_old_cycles); |
| 457 | if (old == NULL) { |
| 458 | exit(1); |
| 459 | } |
| 460 | *old = old_cycle; |
| 461 | |
| 462 | if (!ngx_cleaner_event.timer_set) { |
| 463 | ngx_add_timer(&ngx_cleaner_event, 30000); |
| 464 | ngx_cleaner_event.timer_set = 1; |
| 465 | } |
| 466 | |
| 467 | return cycle; |
| 468 | } |
| 469 | |
| 470 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 471 | #if !(WIN32) |
| 472 | |
| 473 | ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle) |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 474 | { |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 475 | size_t len; |
| 476 | u_char *name, pid[NGX_INT64_LEN + 1]; |
| 477 | ngx_file_t file; |
| 478 | ngx_core_conf_t *ccf, *old_ccf; |
| 479 | |
| 480 | if (old_cycle && old_cycle->conf_ctx == NULL) { |
| 481 | |
| 482 | /* |
| 483 | * do not create the pid file in the first ngx_init_cycle() call |
| 484 | * because we need to write the demonized process pid |
| 485 | */ |
| 486 | |
| 487 | return NGX_OK; |
| 488 | } |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 489 | |
| 490 | ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
| 491 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 492 | if (old_cycle) { |
| 493 | old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx, |
| 494 | ngx_core_module); |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 495 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 496 | if (ccf->pid.len == old_ccf->pid.len |
| 497 | && ngx_strcmp(ccf->pid.data, old_ccf->pid.data) == 0) |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 498 | { |
| 499 | return NGX_OK; |
| 500 | } |
| 501 | } |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 502 | |
| 503 | len = ngx_snprintf((char *) pid, NGX_INT64_LEN + 1, PID_T_FMT, ngx_pid); |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 504 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 505 | ngx_memzero(&file, sizeof(ngx_file_t)); |
| 506 | file.name = (ngx_inherited && getppid() > 1) ? ccf->newpid : ccf->pid; |
| 507 | file.log = cycle->log; |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 508 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 509 | file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR, |
Igor Sysoev | d365af4 | 2004-05-11 16:16:13 +0000 | [diff] [blame] | 510 | NGX_FILE_CREATE_OR_OPEN|NGX_FILE_TRUNCATE); |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 511 | |
| 512 | if (file.fd == NGX_INVALID_FILE) { |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 513 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 514 | ngx_open_file_n " \"%s\" failed", file.name.data); |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 515 | return NGX_ERROR; |
| 516 | } |
| 517 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 518 | if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) { |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 519 | return NGX_ERROR; |
| 520 | } |
| 521 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 522 | if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 523 | ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 524 | ngx_close_file_n " \"%s\" failed", file.name.data); |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 527 | ngx_delete_pidfile(old_cycle); |
| 528 | |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 529 | return NGX_OK; |
| 530 | } |
| 531 | |
| 532 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 533 | void ngx_delete_pidfile(ngx_cycle_t *cycle) |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 534 | { |
| 535 | u_char *name; |
| 536 | ngx_core_conf_t *ccf; |
| 537 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 538 | if (cycle == NULL || cycle->conf_ctx == NULL) { |
| 539 | return; |
| 540 | } |
| 541 | |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 542 | ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
| 543 | |
| 544 | if (ngx_inherited && getppid() > 1) { |
| 545 | name = ccf->newpid.data; |
| 546 | |
| 547 | } else { |
| 548 | name = ccf->pid.data; |
| 549 | } |
| 550 | |
| 551 | if (ngx_delete_file(name) == NGX_FILE_ERROR) { |
| 552 | ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
| 553 | ngx_delete_file_n " \"%s\" failed", name); |
| 554 | } |
| 555 | } |
| 556 | |
Igor Sysoev | 43f1319 | 2004-04-12 16:38:09 +0000 | [diff] [blame] | 557 | #endif |
| 558 | |
Igor Sysoev | 076498e | 2004-04-12 06:10:53 +0000 | [diff] [blame] | 559 | |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 560 | void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user) |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 561 | { |
| 562 | ngx_fd_t fd; |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 563 | ngx_uint_t i; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 564 | ngx_open_file_t *file; |
| 565 | |
| 566 | file = cycle->open_files.elts; |
| 567 | for (i = 0; i < cycle->open_files.nelts; i++) { |
| 568 | if (file[i].name.data == NULL) { |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR, |
| 573 | NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); |
| 574 | |
| 575 | ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
| 576 | "reopen file \"%s\", old:%d new:%d", |
| 577 | file[i].name.data, file[i].fd, fd); |
| 578 | |
| 579 | if (fd == NGX_INVALID_FILE) { |
| 580 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 581 | ngx_open_file_n " \"%s\" failed", file[i].name.data); |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | #if (WIN32) |
| 586 | if (ngx_file_append_mode(fd) == NGX_ERROR) { |
| 587 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 588 | ngx_file_append_mode_n " \"%s\" failed", |
| 589 | file[i].name.data); |
| 590 | |
| 591 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 592 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 593 | ngx_close_file_n " \"%s\" failed", |
| 594 | file[i].name.data); |
| 595 | } |
| 596 | |
| 597 | continue; |
| 598 | } |
Igor Sysoev | 0911f77 | 2004-01-14 18:19:42 +0000 | [diff] [blame] | 599 | #else |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 600 | if (user != (ngx_uid_t) -1) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 601 | if (chown((const char *) file[i].name.data, user, -1) == -1) { |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 602 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 603 | "chown \"%s\" failed", file[i].name.data); |
| 604 | |
| 605 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 606 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 607 | ngx_close_file_n " \"%s\" failed", |
| 608 | file[i].name.data); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
Igor Sysoev | 0911f77 | 2004-01-14 18:19:42 +0000 | [diff] [blame] | 613 | if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { |
| 614 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 615 | "fcntl(FD_CLOEXEC) \"%s\" failed", |
| 616 | file[i].name.data); |
| 617 | |
| 618 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 619 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 620 | ngx_close_file_n " \"%s\" failed", |
| 621 | file[i].name.data); |
| 622 | } |
| 623 | |
| 624 | continue; |
| 625 | } |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 626 | #endif |
| 627 | |
| 628 | if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { |
| 629 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 630 | ngx_close_file_n " \"%s\" failed", |
| 631 | file[i].name.data); |
| 632 | } |
| 633 | |
| 634 | file[i].fd = fd; |
| 635 | } |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 636 | |
Igor Sysoev | a893eab | 2004-03-11 21:34:52 +0000 | [diff] [blame] | 637 | #if !(WIN32) |
| 638 | |
Igor Sysoev | 25b36fe | 2004-02-03 16:43:54 +0000 | [diff] [blame] | 639 | if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) { |
| 640 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 641 | "dup2(STDERR) failed"); |
| 642 | } |
Igor Sysoev | a893eab | 2004-03-11 21:34:52 +0000 | [diff] [blame] | 643 | |
Igor Sysoev | a536298 | 2004-03-04 07:04:55 +0000 | [diff] [blame] | 644 | #endif |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 648 | static void ngx_clean_old_cycles(ngx_event_t *ev) |
| 649 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 650 | ngx_uint_t i, n, found, live; |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 651 | ngx_log_t *log; |
| 652 | ngx_cycle_t **cycle; |
| 653 | |
| 654 | log = ngx_cycle->log; |
| 655 | ngx_temp_pool->log = log; |
| 656 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 657 | ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles"); |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 658 | |
| 659 | live = 0; |
| 660 | |
| 661 | cycle = ngx_old_cycles.elts; |
| 662 | for (i = 0; i < ngx_old_cycles.nelts; i++) { |
| 663 | |
| 664 | if (cycle[i] == NULL) { |
| 665 | continue; |
| 666 | } |
| 667 | |
| 668 | found = 0; |
| 669 | |
| 670 | for (n = 0; n < cycle[i]->connection_n; n++) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 671 | if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 672 | found = 1; |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 673 | |
| 674 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%d", n); |
| 675 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 676 | break; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | if (found) { |
| 681 | live = 1; |
| 682 | continue; |
| 683 | } |
| 684 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 685 | ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %d", i); |
| 686 | |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 687 | ngx_destroy_pool(cycle[i]->pool); |
| 688 | cycle[i] = NULL; |
| 689 | } |
| 690 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 691 | 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] | 692 | |
| 693 | if (live) { |
Igor Sysoev | 3c3ca17 | 2004-01-05 20:55:48 +0000 | [diff] [blame] | 694 | ngx_add_timer(ev, 30000); |
| 695 | |
| 696 | } else { |
| 697 | ngx_destroy_pool(ngx_temp_pool); |
| 698 | ngx_temp_pool = NULL; |
| 699 | ngx_old_cycles.nelts = 0; |
| 700 | } |
| 701 | } |