comparison src/core/ngx_cycle.c @ 337:4feff829a849

nginx-0.0.3-2004-05-19-00:28:54 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 18 May 2004 20:28:54 +0000
parents ca9a7f8c86da
children 02a511569afb
comparison
equal deleted inserted replaced
336:ca9a7f8c86da 337:4feff829a849
22 22
23 23
24 ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle) 24 ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle)
25 { 25 {
26 void *rv; 26 void *rv;
27 u_char *root;
28 ngx_uint_t i, n, failed; 27 ngx_uint_t i, n, failed;
29 ngx_log_t *log; 28 ngx_log_t *log;
30 ngx_conf_t conf; 29 ngx_conf_t conf;
31 ngx_pool_t *pool; 30 ngx_pool_t *pool;
32 ngx_cycle_t *cycle, **old; 31 ngx_cycle_t *cycle, **old;
33 ngx_socket_t fd; 32 ngx_socket_t fd;
34 ngx_open_file_t *file; 33 ngx_open_file_t *file;
35 ngx_listening_t *ls, *nls; 34 ngx_listening_t *ls, *nls;
36 ngx_core_module_t *module; 35 ngx_core_module_t *module;
37 char cwd[NGX_MAX_PATH + 1];
38 36
39 log = old_cycle->log; 37 log = old_cycle->log;
40 38
41 if (!(pool = ngx_create_pool(16 * 1024, log))) { 39 if (!(pool = ngx_create_pool(16 * 1024, log))) {
42 return NULL; 40 return NULL;
49 } 47 }
50 cycle->pool = pool; 48 cycle->pool = pool;
51 cycle->log = log; 49 cycle->log = log;
52 cycle->old_cycle = old_cycle; 50 cycle->old_cycle = old_cycle;
53 cycle->conf_file = old_cycle->conf_file; 51 cycle->conf_file = old_cycle->conf_file;
54 52 cycle->root.len = sizeof(NGX_PREFIX) - 1;
55 53 cycle->root.data = (u_char *) NGX_PREFIX;
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 54
91 55
92 n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10; 56 n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
93 if (!(cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *)))) { 57 if (!(cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *)))) {
94 ngx_destroy_pool(pool); 58 ngx_destroy_pool(pool);
470 434
471 #if !(WIN32) 435 #if !(WIN32)
472 436
473 ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle) 437 ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle)
474 { 438 {
439 ngx_uint_t trunc;
475 size_t len; 440 size_t len;
476 u_char *name, pid[NGX_INT64_LEN + 1]; 441 u_char *name, pid[NGX_INT64_LEN + 1];
477 ngx_file_t file; 442 ngx_file_t file;
478 ngx_core_conf_t *ccf, *old_ccf; 443 ngx_core_conf_t *ccf, *old_ccf;
479 444
480 if (old_cycle && old_cycle->conf_ctx == NULL) { 445 if (!ngx_test_config && old_cycle && old_cycle->conf_ctx == NULL) {
481 446
482 /* 447 /*
483 * do not create the pid file in the first ngx_init_cycle() call 448 * do not create the pid file in the first ngx_init_cycle() call
484 * because we need to write the demonized process pid 449 * because we need to write the demonized process pid
485 */ 450 */
487 return NGX_OK; 452 return NGX_OK;
488 } 453 }
489 454
490 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); 455 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
491 456
492 if (old_cycle) { 457 if (!ngx_test_config && old_cycle) {
493 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx, 458 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
494 ngx_core_module); 459 ngx_core_module);
495 460
496 if (ccf->pid.len == old_ccf->pid.len 461 if (ccf->pid.len == old_ccf->pid.len
497 && ngx_strcmp(ccf->pid.data, old_ccf->pid.data) == 0) 462 && ngx_strcmp(ccf->pid.data, old_ccf->pid.data) == 0)
504 469
505 ngx_memzero(&file, sizeof(ngx_file_t)); 470 ngx_memzero(&file, sizeof(ngx_file_t));
506 file.name = (ngx_inherited && getppid() > 1) ? ccf->newpid : ccf->pid; 471 file.name = (ngx_inherited && getppid() > 1) ? ccf->newpid : ccf->pid;
507 file.log = cycle->log; 472 file.log = cycle->log;
508 473
474 trunc = ngx_test_config ? 0: NGX_FILE_TRUNCATE;
475
509 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR, 476 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
510 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_TRUNCATE); 477 NGX_FILE_CREATE_OR_OPEN|trunc);
511 478
512 if (file.fd == NGX_INVALID_FILE) { 479 if (file.fd == NGX_INVALID_FILE) {
513 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, 480 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
514 ngx_open_file_n " \"%s\" failed", file.name.data); 481 ngx_open_file_n " \"%s\" failed", file.name.data);
515 return NGX_ERROR; 482 return NGX_ERROR;
516 } 483 }
517 484
518 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) { 485 if (!ngx_test_config) {
519 return NGX_ERROR; 486 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
487 return NGX_ERROR;
488 }
520 } 489 }
521 490
522 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { 491 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
523 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 492 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
524 ngx_close_file_n " \"%s\" failed", file.name.data); 493 ngx_close_file_n " \"%s\" failed", file.name.data);