blob: 989973a98e9b22749eadd64b106d5a63dee1264e [file] [log] [blame]
Igor Sysoev3c3ca172004-01-05 20:55:48 +00001
2#include <ngx_config.h>
3#include <ngx_core.h>
4#include <ngx_event.h>
Igor Sysoev3c3ca172004-01-05 20:55:48 +00005
6
7static void ngx_clean_old_cycles(ngx_event_t *ev);
8
9
10volatile ngx_cycle_t *ngx_cycle;
11ngx_array_t ngx_old_cycles;
12
13static ngx_pool_t *ngx_temp_pool;
14static ngx_event_t ngx_cleaner_event;
15
Igor Sysoev630ad0c2004-04-16 05:14:16 +000016ngx_uint_t ngx_test_config;
17
Igor Sysoev3c3ca172004-01-05 20:55:48 +000018
19/* STUB NAME */
20static ngx_connection_t dumb;
21/* STUB */
22
23
24ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle)
25{
Igor Sysoev43f13192004-04-12 16:38:09 +000026 void *rv;
Igor Sysoevab517d52004-05-18 15:29:08 +000027 u_char *root;
Igor Sysoev43f13192004-04-12 16:38:09 +000028 ngx_uint_t i, n, failed;
Igor Sysoev68df19d2004-04-15 15:34:36 +000029 ngx_log_t *log;
Igor Sysoev43f13192004-04-12 16:38:09 +000030 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 Sysoevab517d52004-05-18 15:29:08 +000037 char cwd[NGX_MAX_PATH + 1];
Igor Sysoev3c3ca172004-01-05 20:55:48 +000038
39 log = old_cycle->log;
40
41 if (!(pool = ngx_create_pool(16 * 1024, log))) {
42 return NULL;
43 }
Igor Sysoev68df19d2004-04-15 15:34:36 +000044 pool->log = log;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000045
46 if (!(cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)))) {
47 ngx_destroy_pool(pool);
48 return NULL;
49 }
50 cycle->pool = pool;
Igor Sysoevcccc5522004-04-14 20:34:05 +000051 cycle->log = log;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000052 cycle->old_cycle = old_cycle;
Igor Sysoeve9b2cb12004-02-09 20:47:18 +000053 cycle->conf_file = old_cycle->conf_file;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000054
55
Igor Sysoevab517d52004-05-18 15:29:08 +000056 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 Sysoev3c3ca172004-01-05 20:55:48 +000092 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 Sysoev68df19d2004-04-15 15:34:36 +0000115 if (!(cycle->new_log = ngx_log_create_errlog(cycle, NULL))) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000116 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 Sysoev43f13192004-04-12 16:38:09 +0000140 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 Sysoev3c3ca172004-01-05 20:55:48 +0000155 }
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 Sysoev68df19d2004-04-15 15:34:36 +0000168 conf.pool = pool;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000169 conf.log = log;
170 conf.module_type = NGX_CORE_MODULE;
171 conf.cmd_type = NGX_MAIN_CONF;
172
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000173
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000174 if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000175 ngx_destroy_pool(pool);
176 return NULL;
177 }
178
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000179 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 Sysoev3c3ca172004-01-05 20:55:48 +0000185
Igor Sysoev43f13192004-04-12 16:38:09 +0000186 for (i = 0; ngx_modules[i]; i++) {
187 if (ngx_modules[i]->type != NGX_CORE_MODULE) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000188 continue;
189 }
190
Igor Sysoev43f13192004-04-12 16:38:09 +0000191 module = ngx_modules[i]->ctx;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000192
Igor Sysoev43f13192004-04-12 16:38:09 +0000193 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 Sysoev3c3ca172004-01-05 20:55:48 +0000200 }
Igor Sysoev43f13192004-04-12 16:38:09 +0000201 }
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 Sysoev3c3ca172004-01-05 20:55:48 +0000232
233#if (WIN32)
Igor Sysoev43f13192004-04-12 16:38:09 +0000234 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 Sysoev0911f772004-01-14 18:19:42 +0000241#else
Igor Sysoev43f13192004-04-12 16:38:09 +0000242 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 Sysoev3c3ca172004-01-05 20:55:48 +0000249#endif
Igor Sysoev43f13192004-04-12 16:38:09 +0000250 }
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000251 }
252
Igor Sysoev68df19d2004-04-15 15:34:36 +0000253 cycle->log = cycle->new_log;
254 pool->log = cycle->new_log;
Igor Sysoev43f13192004-04-12 16:38:09 +0000255
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000256 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 Sysoev3c3ca172004-01-05 20:55:48 +0000270 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 Sysoev630ad0c2004-04-16 05:14:16 +0000312 if (!ngx_test_config && !failed) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000313 if (ngx_open_listening_sockets(cycle) == NGX_ERROR) {
314 failed = 1;
315 }
316 }
317 }
318
Igor Sysoeva893eab2004-03-11 21:34:52 +0000319#if !(WIN32)
320
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000321 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 Sysoev25b36fe2004-02-03 16:43:54 +0000327 }
Igor Sysoeva893eab2004-03-11 21:34:52 +0000328
Igor Sysoeva5362982004-03-04 07:04:55 +0000329#endif
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000330
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000331 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 Sysoev9bfb4342004-04-18 19:06:02 +0000348 if (ngx_test_config) {
349 ngx_destroy_pool(pool);
350 return NULL;
351 }
352
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000353 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 Sysoev25b36fe2004-02-03 16:43:54 +0000370
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000371 /* 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 Sysoev3d58f8c2004-01-08 08:47:17 +0000424 if (ngx_process == NGX_PROCESS_MASTER) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000425 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 Sysoev43f13192004-04-12 16:38:09 +0000471#if !(WIN32)
472
473ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle)
Igor Sysoev076498e2004-04-12 06:10:53 +0000474{
Igor Sysoev43f13192004-04-12 16:38:09 +0000475 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 Sysoev076498e2004-04-12 06:10:53 +0000489
490 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
491
Igor Sysoev43f13192004-04-12 16:38:09 +0000492 if (old_cycle) {
493 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
494 ngx_core_module);
Igor Sysoev076498e2004-04-12 06:10:53 +0000495
Igor Sysoev43f13192004-04-12 16:38:09 +0000496 if (ccf->pid.len == old_ccf->pid.len
497 && ngx_strcmp(ccf->pid.data, old_ccf->pid.data) == 0)
Igor Sysoev076498e2004-04-12 06:10:53 +0000498 {
499 return NGX_OK;
500 }
501 }
Igor Sysoev076498e2004-04-12 06:10:53 +0000502
503 len = ngx_snprintf((char *) pid, NGX_INT64_LEN + 1, PID_T_FMT, ngx_pid);
Igor Sysoev076498e2004-04-12 06:10:53 +0000504
Igor Sysoev43f13192004-04-12 16:38:09 +0000505 ngx_memzero(&file, sizeof(ngx_file_t));
506 file.name = (ngx_inherited && getppid() > 1) ? ccf->newpid : ccf->pid;
507 file.log = cycle->log;
Igor Sysoev076498e2004-04-12 06:10:53 +0000508
Igor Sysoev43f13192004-04-12 16:38:09 +0000509 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
Igor Sysoevd365af42004-05-11 16:16:13 +0000510 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_TRUNCATE);
Igor Sysoev43f13192004-04-12 16:38:09 +0000511
512 if (file.fd == NGX_INVALID_FILE) {
Igor Sysoev076498e2004-04-12 06:10:53 +0000513 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
Igor Sysoev43f13192004-04-12 16:38:09 +0000514 ngx_open_file_n " \"%s\" failed", file.name.data);
Igor Sysoev076498e2004-04-12 06:10:53 +0000515 return NGX_ERROR;
516 }
517
Igor Sysoev43f13192004-04-12 16:38:09 +0000518 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
Igor Sysoev076498e2004-04-12 06:10:53 +0000519 return NGX_ERROR;
520 }
521
Igor Sysoev43f13192004-04-12 16:38:09 +0000522 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
Igor Sysoev076498e2004-04-12 06:10:53 +0000523 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
Igor Sysoev43f13192004-04-12 16:38:09 +0000524 ngx_close_file_n " \"%s\" failed", file.name.data);
Igor Sysoev076498e2004-04-12 06:10:53 +0000525 }
526
Igor Sysoev43f13192004-04-12 16:38:09 +0000527 ngx_delete_pidfile(old_cycle);
528
Igor Sysoev076498e2004-04-12 06:10:53 +0000529 return NGX_OK;
530}
531
532
Igor Sysoev43f13192004-04-12 16:38:09 +0000533void ngx_delete_pidfile(ngx_cycle_t *cycle)
Igor Sysoev076498e2004-04-12 06:10:53 +0000534{
535 u_char *name;
536 ngx_core_conf_t *ccf;
537
Igor Sysoev43f13192004-04-12 16:38:09 +0000538 if (cycle == NULL || cycle->conf_ctx == NULL) {
539 return;
540 }
541
Igor Sysoev076498e2004-04-12 06:10:53 +0000542 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 Sysoev43f13192004-04-12 16:38:09 +0000557#endif
558
Igor Sysoev076498e2004-04-12 06:10:53 +0000559
Igor Sysoeva5362982004-03-04 07:04:55 +0000560void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000561{
562 ngx_fd_t fd;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000563 ngx_uint_t i;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000564 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 Sysoev0911f772004-01-14 18:19:42 +0000599#else
Igor Sysoeva5362982004-03-04 07:04:55 +0000600 if (user != (ngx_uid_t) -1) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000601 if (chown((const char *) file[i].name.data, user, -1) == -1) {
Igor Sysoeva5362982004-03-04 07:04:55 +0000602 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 Sysoev0911f772004-01-14 18:19:42 +0000613 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 Sysoev3c3ca172004-01-05 20:55:48 +0000626#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 Sysoev25b36fe2004-02-03 16:43:54 +0000636
Igor Sysoeva893eab2004-03-11 21:34:52 +0000637#if !(WIN32)
638
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000639 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 Sysoeva893eab2004-03-11 21:34:52 +0000643
Igor Sysoeva5362982004-03-04 07:04:55 +0000644#endif
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000645}
646
647
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000648static void ngx_clean_old_cycles(ngx_event_t *ev)
649{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000650 ngx_uint_t i, n, found, live;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000651 ngx_log_t *log;
652 ngx_cycle_t **cycle;
653
654 log = ngx_cycle->log;
655 ngx_temp_pool->log = log;
656
Igor Sysoev54498db2004-02-11 17:08:49 +0000657 ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000658
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 Sysoev10a543a2004-03-16 07:10:12 +0000671 if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000672 found = 1;
Igor Sysoev54498db2004-02-11 17:08:49 +0000673
674 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%d", n);
675
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000676 break;
677 }
678 }
679
680 if (found) {
681 live = 1;
682 continue;
683 }
684
Igor Sysoev54498db2004-02-11 17:08:49 +0000685 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %d", i);
686
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000687 ngx_destroy_pool(cycle[i]->pool);
688 cycle[i] = NULL;
689 }
690
Igor Sysoev54498db2004-02-11 17:08:49 +0000691 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %d", live);
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000692
693 if (live) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000694 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}