Mercurial > hg > nginx
comparison src/core/ngx_cycle.c @ 311:11ff50a35d6d
nginx-0.0.3-2004-04-12-20:38:09 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 12 Apr 2004 16:38:09 +0000 |
parents | a9a9af2c7370 |
children | a0beefedaf94 |
comparison
equal
deleted
inserted
replaced
310:a9a9af2c7370 | 311:11ff50a35d6d |
---|---|
1 | 1 |
2 #include <ngx_config.h> | 2 #include <ngx_config.h> |
3 #include <ngx_core.h> | 3 #include <ngx_core.h> |
4 #include <ngx_event.h> | 4 #include <ngx_event.h> |
5 /* STUB */ | |
6 #include <nginx.h> | |
7 | 5 |
8 | 6 |
9 static void ngx_clean_old_cycles(ngx_event_t *ev); | 7 static void ngx_clean_old_cycles(ngx_event_t *ev); |
10 | 8 |
11 | 9 |
21 /* STUB */ | 19 /* STUB */ |
22 | 20 |
23 | 21 |
24 ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle) | 22 ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle) |
25 { | 23 { |
26 ngx_uint_t i, n, failed; | 24 void *rv; |
27 ngx_log_t *log; | 25 ngx_uint_t i, n, failed; |
28 ngx_conf_t conf; | 26 ngx_log_t *log; |
29 ngx_pool_t *pool; | 27 ngx_conf_t conf; |
30 ngx_cycle_t *cycle, **old; | 28 ngx_pool_t *pool; |
31 ngx_socket_t fd; | 29 ngx_cycle_t *cycle, **old; |
32 ngx_open_file_t *file; | 30 ngx_socket_t fd; |
33 ngx_core_conf_t *ccf; | 31 ngx_open_file_t *file; |
34 ngx_listening_t *ls, *nls; | 32 ngx_listening_t *ls, *nls; |
33 ngx_core_module_t *module; | |
35 | 34 |
36 log = old_cycle->log; | 35 log = old_cycle->log; |
37 | 36 |
38 if (!(pool = ngx_create_pool(16 * 1024, log))) { | 37 if (!(pool = ngx_create_pool(16 * 1024, log))) { |
39 return NULL; | 38 return NULL; |
95 ngx_destroy_pool(pool); | 94 ngx_destroy_pool(pool); |
96 return NULL; | 95 return NULL; |
97 } | 96 } |
98 | 97 |
99 | 98 |
100 if (ngx_core_module.init_module(cycle) == NGX_ERROR) { | 99 for (i = 0; ngx_modules[i]; i++) { |
101 ngx_destroy_pool(pool); | 100 if (ngx_modules[i]->type != NGX_CORE_MODULE) { |
102 return NULL; | 101 continue; |
102 } | |
103 | |
104 module = ngx_modules[i]->ctx; | |
105 | |
106 if (module->create_conf) { | |
107 rv = module->create_conf(cycle); | |
108 if (rv == NGX_CONF_ERROR) { | |
109 ngx_destroy_pool(pool); | |
110 return NULL; | |
111 } | |
112 cycle->conf_ctx[ngx_modules[i]->index] = rv; | |
113 } | |
103 } | 114 } |
104 | 115 |
105 | 116 |
106 ngx_memzero(&conf, sizeof(ngx_conf_t)); | 117 ngx_memzero(&conf, sizeof(ngx_conf_t)); |
107 /* STUB: init array ? */ | 118 /* STUB: init array ? */ |
123 ngx_destroy_pool(pool); | 134 ngx_destroy_pool(pool); |
124 return NULL; | 135 return NULL; |
125 } | 136 } |
126 | 137 |
127 | 138 |
139 for (i = 0; ngx_modules[i]; i++) { | |
140 if (ngx_modules[i]->type != NGX_CORE_MODULE) { | |
141 continue; | |
142 } | |
143 | |
144 module = ngx_modules[i]->ctx; | |
145 | |
146 if (module->init_conf) { | |
147 if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index]) | |
148 == NGX_CONF_ERROR) | |
149 { | |
150 ngx_destroy_pool(pool); | |
151 return NULL; | |
152 } | |
153 } | |
154 } | |
155 | |
156 | |
128 failed = 0; | 157 failed = 0; |
129 | 158 |
130 | 159 |
131 | 160 #if !(WIN32) |
132 file = cycle->open_files.elts; | 161 if (ngx_create_pidfile(cycle, old_cycle) == NGX_ERROR) { |
133 for (i = 0; i < cycle->open_files.nelts; i++) { | 162 failed = 1; |
134 if (file[i].name.data == NULL) { | 163 } |
135 continue; | 164 #endif |
136 } | 165 |
137 | 166 |
138 file[i].fd = ngx_open_file(file[i].name.data, | 167 if (!failed) { |
139 NGX_FILE_RDWR, | 168 file = cycle->open_files.elts; |
140 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); | 169 for (i = 0; i < cycle->open_files.nelts; i++) { |
141 | 170 if (file[i].name.data == NULL) { |
142 if (file[i].fd == NGX_INVALID_FILE) { | 171 continue; |
143 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | 172 } |
144 ngx_open_file_n " \"%s\" failed", | 173 |
145 file[i].name.data); | 174 file[i].fd = ngx_open_file(file[i].name.data, |
146 failed = 1; | 175 NGX_FILE_RDWR, |
147 break; | 176 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); |
148 } | 177 |
178 if (file[i].fd == NGX_INVALID_FILE) { | |
179 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
180 ngx_open_file_n " \"%s\" failed", | |
181 file[i].name.data); | |
182 failed = 1; | |
183 break; | |
184 } | |
149 | 185 |
150 #if (WIN32) | 186 #if (WIN32) |
151 if (ngx_file_append_mode(file[i].fd) == NGX_ERROR) { | 187 if (ngx_file_append_mode(file[i].fd) == NGX_ERROR) { |
152 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | 188 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
153 ngx_file_append_mode_n " \"%s\" failed", | 189 ngx_file_append_mode_n " \"%s\" failed", |
154 file[i].name.data); | 190 file[i].name.data); |
155 failed = 1; | 191 failed = 1; |
156 break; | 192 break; |
157 } | 193 } |
158 #else | 194 #else |
159 if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) { | 195 if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) { |
160 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | 196 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
161 "fcntl(FD_CLOEXEC) \"%s\" failed", | 197 "fcntl(FD_CLOEXEC) \"%s\" failed", |
162 file[i].name.data); | 198 file[i].name.data); |
163 failed = 1; | 199 failed = 1; |
164 break; | 200 break; |
165 } | 201 } |
166 #endif | 202 #endif |
167 } | 203 } |
204 } | |
205 | |
168 | 206 |
169 if (!failed) { | 207 if (!failed) { |
170 if (old_cycle->listening.nelts) { | 208 if (old_cycle->listening.nelts) { |
171 ls = old_cycle->listening.elts; | 209 ls = old_cycle->listening.elts; |
172 for (i = 0; i < old_cycle->listening.nelts; i++) { | 210 for (i = 0; i < old_cycle->listening.nelts; i++) { |
372 | 410 |
373 return cycle; | 411 return cycle; |
374 } | 412 } |
375 | 413 |
376 | 414 |
377 static ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle) | 415 #if !(WIN32) |
416 | |
417 ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle) | |
378 { | 418 { |
379 size_t len; | 419 size_t len; |
380 u_char pid[NGX_INT64_LEN + 1]; | 420 u_char *name, pid[NGX_INT64_LEN + 1]; |
381 ngx_str_t name; | 421 ngx_file_t file; |
382 ngx_core_conf_t *ccf; | 422 ngx_core_conf_t *ccf, *old_ccf; |
423 | |
424 if (old_cycle && old_cycle->conf_ctx == NULL) { | |
425 | |
426 /* | |
427 * do not create the pid file in the first ngx_init_cycle() call | |
428 * because we need to write the demonized process pid | |
429 */ | |
430 | |
431 return NGX_OK; | |
432 } | |
383 | 433 |
384 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | 434 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
385 | 435 |
386 if (ctx->pid.len) { | 436 if (old_cycle) { |
387 if (ccf->pid.len == 0) { | 437 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx, |
388 return NGX_OK; | 438 ngx_core_module); |
389 } | 439 |
390 | 440 if (ccf->pid.len == old_ccf->pid.len |
391 if (ctx->pid.len == ccf->pid.len | 441 && ngx_strcmp(ccf->pid.data, old_ccf->pid.data) == 0) |
392 && ngx_strcmp(ctx->pid.data, ccf->pid.data) == 0) | |
393 { | 442 { |
394 return NGX_OK; | 443 return NGX_OK; |
395 } | 444 } |
396 } | 445 } |
397 | |
398 if (ccf->pid.len == 0) { | |
399 ccf->pid.len = sizeof(NGINX_PID) - 1; | |
400 ccf->pid.data = NGINX_PID; | |
401 ccf->newpid.len = sizeof(NGINX_NEWPID) - 1; | |
402 ccf->newpid.data = NGINX_NEWPID; | |
403 | |
404 } else { | |
405 ccf->newpid.len = ccf->pid.len + sizeof(NGINX_NEWPID_EXT); | |
406 if (!(ccf->newpid.data = ngx_alloc(ccf->newpid.len, cycle->log))) { | |
407 return NGX_ERROR; | |
408 } | |
409 | |
410 ngx_memcpy(ngx_cpymem(ccf->newpid.data, ccf->pid.data, ccf->pid.len), | |
411 NGINX_NEWPID_EXT, sizeof(NGINX_NEWPID_EXT)); | |
412 } | |
413 | 446 |
414 len = ngx_snprintf((char *) pid, NGX_INT64_LEN + 1, PID_T_FMT, ngx_pid); | 447 len = ngx_snprintf((char *) pid, NGX_INT64_LEN + 1, PID_T_FMT, ngx_pid); |
415 ngx_memzero(&ctx->pid, sizeof(ngx_file_t)); | 448 |
416 ctx->pid.name = ngx_inherited ? ccf->newpid : ccf->pid; | 449 ngx_memzero(&file, sizeof(ngx_file_t)); |
417 ctx->name = ccf->pid.data; | 450 file.name = (ngx_inherited && getppid() > 1) ? ccf->newpid : ccf->pid; |
418 | 451 file.log = cycle->log; |
419 ctx->pid.fd = ngx_open_file(ctx->pid.name.data, NGX_FILE_RDWR, | 452 |
420 NGX_FILE_CREATE_OR_OPEN); | 453 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR, |
421 | 454 NGX_FILE_CREATE_OR_OPEN); |
422 if (ctx->pid.fd == NGX_INVALID_FILE) { | 455 |
456 if (file.fd == NGX_INVALID_FILE) { | |
423 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | 457 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
424 ngx_open_file_n " \"%s\" failed", ctx->pid.name.data); | 458 ngx_open_file_n " \"%s\" failed", file.name.data); |
425 return NGX_ERROR; | 459 return NGX_ERROR; |
426 } | 460 } |
427 | 461 |
428 if (ngx_write_file(&ctx->pid, pid, len, 0) == NGX_ERROR) { | 462 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) { |
429 return NGX_ERROR; | 463 return NGX_ERROR; |
430 } | 464 } |
431 | 465 |
432 if (ngx_close_file(ctx->pid.fd) == NGX_FILE_ERROR) { | 466 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { |
433 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | 467 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
434 ngx_close_file_n " \"%s\" failed", ctx->pid.name.data); | 468 ngx_close_file_n " \"%s\" failed", file.name.data); |
435 } | 469 } |
470 | |
471 ngx_delete_pidfile(old_cycle); | |
436 | 472 |
437 return NGX_OK; | 473 return NGX_OK; |
438 } | 474 } |
439 | 475 |
440 | 476 |
441 static void ngx_delete_pidfile(ngx_cycle_t *cycle) | 477 void ngx_delete_pidfile(ngx_cycle_t *cycle) |
442 { | 478 { |
443 u_char *name; | 479 u_char *name; |
444 ngx_core_conf_t *ccf; | 480 ngx_core_conf_t *ccf; |
445 | 481 |
482 if (cycle == NULL || cycle->conf_ctx == NULL) { | |
483 return; | |
484 } | |
485 | |
446 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | 486 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
447 | 487 |
448 if (ngx_inherited && getppid() > 1) { | 488 if (ngx_inherited && getppid() > 1) { |
449 name = ccf->newpid.data; | 489 name = ccf->newpid.data; |
450 | 490 |
455 if (ngx_delete_file(name) == NGX_FILE_ERROR) { | 495 if (ngx_delete_file(name) == NGX_FILE_ERROR) { |
456 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | 496 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
457 ngx_delete_file_n " \"%s\" failed", name); | 497 ngx_delete_file_n " \"%s\" failed", name); |
458 } | 498 } |
459 } | 499 } |
500 | |
501 #endif | |
460 | 502 |
461 | 503 |
462 void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user) | 504 void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user) |
463 { | 505 { |
464 ngx_fd_t fd; | 506 ngx_fd_t fd; |