blob: 05a3a78f1b2d70e6d39197c366a0588e9a813f33 [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 Sysoev2b979932004-07-07 15:01:00 +000018#if (NGX_THREADS)
19ngx_tls_key_t ngx_core_tls_key;
20#endif
21
Igor Sysoev3c3ca172004-01-05 20:55:48 +000022
23/* STUB NAME */
24static ngx_connection_t dumb;
25/* STUB */
26
27
28ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle)
29{
Igor Sysoev43f13192004-04-12 16:38:09 +000030 void *rv;
31 ngx_uint_t i, n, failed;
Igor Sysoev68df19d2004-04-15 15:34:36 +000032 ngx_log_t *log;
Igor Sysoev43f13192004-04-12 16:38:09 +000033 ngx_conf_t conf;
34 ngx_pool_t *pool;
35 ngx_cycle_t *cycle, **old;
36 ngx_socket_t fd;
Igor Sysoevb9e34412004-09-03 15:50:30 +000037 ngx_list_part_t *part;
Igor Sysoev43f13192004-04-12 16:38:09 +000038 ngx_open_file_t *file;
39 ngx_listening_t *ls, *nls;
40 ngx_core_module_t *module;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000041
42 log = old_cycle->log;
43
44 if (!(pool = ngx_create_pool(16 * 1024, log))) {
45 return NULL;
46 }
Igor Sysoev68df19d2004-04-15 15:34:36 +000047 pool->log = log;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000048
49 if (!(cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t)))) {
50 ngx_destroy_pool(pool);
51 return NULL;
52 }
53 cycle->pool = pool;
Igor Sysoevcccc5522004-04-14 20:34:05 +000054 cycle->log = log;
Igor Sysoev3c3ca172004-01-05 20:55:48 +000055 cycle->old_cycle = old_cycle;
Igor Sysoeve9b2cb12004-02-09 20:47:18 +000056 cycle->conf_file = old_cycle->conf_file;
Igor Sysoev090849d2004-05-18 20:28:54 +000057 cycle->root.len = sizeof(NGX_PREFIX) - 1;
58 cycle->root.data = (u_char *) NGX_PREFIX;
Igor Sysoevab517d52004-05-18 15:29:08 +000059
60
Igor Sysoev3c3ca172004-01-05 20:55:48 +000061 n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
62 if (!(cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *)))) {
63 ngx_destroy_pool(pool);
64 return NULL;
65 }
66 cycle->pathes.nelts = 0;
67 cycle->pathes.size = sizeof(ngx_path_t *);
68 cycle->pathes.nalloc = n;
69 cycle->pathes.pool = pool;
70
71
Igor Sysoevb9e34412004-09-03 15:50:30 +000072 if (old_cycle->open_files.part.nelts) {
73 n = old_cycle->open_files.part.nelts;
74 for (part = old_cycle->open_files.part.next; part; part = part->next) {
75 n += part->nelts;
76 }
77
78 } else {
79 n = 20;
80 }
81
82 cycle->open_files.part.elts = ngx_palloc(pool, n * sizeof(ngx_open_file_t));
83 if (cycle->open_files.part.elts == NULL) {
84 ngx_destroy_pool(pool);
85 return NULL;
86 }
87 cycle->open_files.part.nelts = 0;
88 cycle->open_files.part.next = NULL;
89 cycle->open_files.last = &cycle->open_files.part;
90 cycle->open_files.size = sizeof(ngx_open_file_t);
91 cycle->open_files.nalloc = n;
92 cycle->open_files.pool = pool;
93
94
95#if 0
Igor Sysoev3c3ca172004-01-05 20:55:48 +000096 n = old_cycle->open_files.nelts ? old_cycle->open_files.nelts : 20;
97 cycle->open_files.elts = ngx_pcalloc(pool, n * sizeof(ngx_open_file_t));
98 if (cycle->open_files.elts == NULL) {
99 ngx_destroy_pool(pool);
100 return NULL;
101 }
102 cycle->open_files.nelts = 0;
103 cycle->open_files.size = sizeof(ngx_open_file_t);
104 cycle->open_files.nalloc = n;
105 cycle->open_files.pool = pool;
Igor Sysoevb9e34412004-09-03 15:50:30 +0000106#endif
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000107
108
Igor Sysoev68df19d2004-04-15 15:34:36 +0000109 if (!(cycle->new_log = ngx_log_create_errlog(cycle, NULL))) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000110 ngx_destroy_pool(pool);
111 return NULL;
112 }
113
114
115 n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;
116 cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));
117 if (cycle->listening.elts == NULL) {
118 ngx_destroy_pool(pool);
119 return NULL;
120 }
121 cycle->listening.nelts = 0;
122 cycle->listening.size = sizeof(ngx_listening_t);
123 cycle->listening.nalloc = n;
124 cycle->listening.pool = pool;
125
126
127 cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
128 if (cycle->conf_ctx == NULL) {
129 ngx_destroy_pool(pool);
130 return NULL;
131 }
132
133
Igor Sysoev43f13192004-04-12 16:38:09 +0000134 for (i = 0; ngx_modules[i]; i++) {
135 if (ngx_modules[i]->type != NGX_CORE_MODULE) {
136 continue;
137 }
138
139 module = ngx_modules[i]->ctx;
140
141 if (module->create_conf) {
142 rv = module->create_conf(cycle);
143 if (rv == NGX_CONF_ERROR) {
144 ngx_destroy_pool(pool);
145 return NULL;
146 }
147 cycle->conf_ctx[ngx_modules[i]->index] = rv;
148 }
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000149 }
150
151
152 ngx_memzero(&conf, sizeof(ngx_conf_t));
153 /* STUB: init array ? */
154 conf.args = ngx_create_array(pool, 10, sizeof(ngx_str_t));
155 if (conf.args == NULL) {
156 ngx_destroy_pool(pool);
157 return NULL;
158 }
159
160 conf.ctx = cycle->conf_ctx;
161 conf.cycle = cycle;
Igor Sysoev68df19d2004-04-15 15:34:36 +0000162 conf.pool = pool;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000163 conf.log = log;
164 conf.module_type = NGX_CORE_MODULE;
165 conf.cmd_type = NGX_MAIN_CONF;
166
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000167
Igor Sysoeve9b2cb12004-02-09 20:47:18 +0000168 if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000169 ngx_destroy_pool(pool);
170 return NULL;
171 }
172
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000173 if (ngx_test_config) {
174 ngx_log_error(NGX_LOG_INFO, log, 0,
175 "the configuration file %s syntax is ok",
176 cycle->conf_file.data);
177 }
178
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000179
Igor Sysoev43f13192004-04-12 16:38:09 +0000180 for (i = 0; ngx_modules[i]; i++) {
181 if (ngx_modules[i]->type != NGX_CORE_MODULE) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000182 continue;
183 }
184
Igor Sysoev43f13192004-04-12 16:38:09 +0000185 module = ngx_modules[i]->ctx;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000186
Igor Sysoev43f13192004-04-12 16:38:09 +0000187 if (module->init_conf) {
188 if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
189 == NGX_CONF_ERROR)
190 {
191 ngx_destroy_pool(pool);
192 return NULL;
193 }
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000194 }
Igor Sysoev43f13192004-04-12 16:38:09 +0000195 }
196
197
198 failed = 0;
199
200
201#if !(WIN32)
202 if (ngx_create_pidfile(cycle, old_cycle) == NGX_ERROR) {
203 failed = 1;
204 }
205#endif
206
207
208 if (!failed) {
Igor Sysoevb9e34412004-09-03 15:50:30 +0000209
210 part = &cycle->open_files.part;
211 file = part->elts;
212
213 for (i = 0; /* void */ ; i++) {
214
215 if (i >= part->nelts) {
216 if (part->next == NULL) {
217 break;
218 }
219 part = part->next;
220 file = part->elts;
221 i = 0;
222 }
223
224#if 0
Igor Sysoev43f13192004-04-12 16:38:09 +0000225 file = cycle->open_files.elts;
226 for (i = 0; i < cycle->open_files.nelts; i++) {
Igor Sysoevb9e34412004-09-03 15:50:30 +0000227#endif
228
Igor Sysoev43f13192004-04-12 16:38:09 +0000229 if (file[i].name.data == NULL) {
230 continue;
231 }
232
233 file[i].fd = ngx_open_file(file[i].name.data,
234 NGX_FILE_RDWR,
235 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);
236
Igor Sysoevb9e34412004-09-03 15:50:30 +0000237 log->log_level = NGX_LOG_DEBUG_ALL;
238 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
239 "log: %0X %d \"%s\"",
240 &file[i], file[i].fd, file[i].name.data);
241
Igor Sysoev43f13192004-04-12 16:38:09 +0000242 if (file[i].fd == NGX_INVALID_FILE) {
243 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
244 ngx_open_file_n " \"%s\" failed",
245 file[i].name.data);
246 failed = 1;
247 break;
248 }
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000249
250#if (WIN32)
Igor Sysoev43f13192004-04-12 16:38:09 +0000251 if (ngx_file_append_mode(file[i].fd) == NGX_ERROR) {
252 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
253 ngx_file_append_mode_n " \"%s\" failed",
254 file[i].name.data);
255 failed = 1;
256 break;
257 }
Igor Sysoev0911f772004-01-14 18:19:42 +0000258#else
Igor Sysoev43f13192004-04-12 16:38:09 +0000259 if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {
260 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
261 "fcntl(FD_CLOEXEC) \"%s\" failed",
262 file[i].name.data);
263 failed = 1;
264 break;
265 }
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000266#endif
Igor Sysoev43f13192004-04-12 16:38:09 +0000267 }
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000268 }
269
Igor Sysoev68df19d2004-04-15 15:34:36 +0000270 cycle->log = cycle->new_log;
271 pool->log = cycle->new_log;
Igor Sysoev43f13192004-04-12 16:38:09 +0000272
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000273 if (!failed) {
274 if (old_cycle->listening.nelts) {
275 ls = old_cycle->listening.elts;
276 for (i = 0; i < old_cycle->listening.nelts; i++) {
277 ls[i].remain = 0;
278 }
279
280 nls = cycle->listening.elts;
281 for (n = 0; n < cycle->listening.nelts; n++) {
282 for (i = 0; i < old_cycle->listening.nelts; i++) {
283 if (ls[i].ignore) {
284 continue;
285 }
286
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000287 if (ngx_memcmp(nls[n].sockaddr,
288 ls[i].sockaddr, ls[i].socklen) == 0)
289 {
290 fd = ls[i].fd;
291#if (WIN32)
292 /*
293 * Winsock assignes a socket number divisible by 4 so
294 * to find a connection we divide a socket number by 4.
295 */
296
297 fd /= 4;
298#endif
299 if (fd >= (ngx_socket_t) cycle->connection_n) {
300 ngx_log_error(NGX_LOG_EMERG, log, 0,
301 "%d connections is not enough to hold "
302 "an open listening socket on %s, "
303 "required at least %d connections",
304 cycle->connection_n,
305 ls[i].addr_text.data, fd);
306 failed = 1;
307 break;
308 }
309
310 nls[n].fd = ls[i].fd;
311 nls[i].remain = 1;
312 ls[i].remain = 1;
313 break;
314 }
315 }
316
317 if (nls[n].fd == -1) {
318 nls[n].new = 1;
319 }
320 }
321
322 } else {
323 ls = cycle->listening.elts;
324 for (i = 0; i < cycle->listening.nelts; i++) {
325 ls[i].new = 1;
326 }
327 }
328
Igor Sysoev630ad0c2004-04-16 05:14:16 +0000329 if (!ngx_test_config && !failed) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000330 if (ngx_open_listening_sockets(cycle) == NGX_ERROR) {
331 failed = 1;
332 }
333 }
334 }
335
Igor Sysoeva893eab2004-03-11 21:34:52 +0000336#if !(WIN32)
337
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000338 if (!failed && !ngx_test_config) {
Igor Sysoevb9e34412004-09-03 15:50:30 +0000339
340 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
341 "dup2: %0X %d \"%s\"",
342 cycle->log->file,
343 cycle->log->file->fd, cycle->log->file->name.data);
344
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000345 if (dup2(cycle->log->file->fd, STDERR_FILENO) == NGX_ERROR) {
346 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
347 "dup2(STDERR) failed");
348 failed = 1;
349 }
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000350 }
Igor Sysoeva893eab2004-03-11 21:34:52 +0000351
Igor Sysoeva5362982004-03-04 07:04:55 +0000352#endif
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000353
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000354 if (failed) {
355
356 /* rollback the new cycle configuration */
357
Igor Sysoevb9e34412004-09-03 15:50:30 +0000358 part = &cycle->open_files.part;
359 file = part->elts;
360
361 for (i = 0; /* void */ ; i++) {
362
363 if (i >= part->nelts) {
364 if (part->next == NULL) {
365 break;
366 }
367 part = part->next;
368 file = part->elts;
369 i = 0;
370 }
371
372#if 0
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000373 file = cycle->open_files.elts;
374 for (i = 0; i < cycle->open_files.nelts; i++) {
Igor Sysoevb9e34412004-09-03 15:50:30 +0000375#endif
376
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000377 if (file[i].fd == NGX_INVALID_FILE) {
378 continue;
379 }
380
381 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
382 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
383 ngx_close_file_n " \"%s\" failed",
384 file[i].name.data);
385 }
386 }
387
Igor Sysoev9bfb4342004-04-18 19:06:02 +0000388 if (ngx_test_config) {
389 ngx_destroy_pool(pool);
390 return NULL;
391 }
392
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000393 ls = cycle->listening.elts;
394 for (i = 0; i < cycle->listening.nelts; i++) {
Igor Sysoevb9e34412004-09-03 15:50:30 +0000395 if (ls[i].fd == -1 || !ls[i].new) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000396 continue;
397 }
398
399 if (ngx_close_socket(ls[i].fd) == -1) {
400 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
401 ngx_close_socket_n " %s failed",
402 ls[i].addr_text.data);
403 }
404 }
405
406 ngx_destroy_pool(pool);
407 return NULL;
408 }
409
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000410
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000411 /* commit the new cycle configuration */
412
413 pool->log = cycle->log;
414
415
416 for (i = 0; ngx_modules[i]; i++) {
417 if (ngx_modules[i]->init_module) {
418 if (ngx_modules[i]->init_module(cycle) == NGX_ERROR) {
419 /* fatal */
420 exit(1);
421 }
422 }
423 }
424
425 /* close and delete stuff that lefts from an old cycle */
426
427 /* close the unneeded listening sockets */
428
429 ls = old_cycle->listening.elts;
430 for (i = 0; i < old_cycle->listening.nelts; i++) {
431 if (ls[i].remain) {
432 continue;
433 }
434
435 if (ngx_close_socket(ls[i].fd) == -1) {
436 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
437 ngx_close_socket_n " %s failed",
438 ls[i].addr_text.data);
439 }
440 }
441
442
443 /* close the unneeded open files */
444
Igor Sysoevb9e34412004-09-03 15:50:30 +0000445 part = &old_cycle->open_files.part;
446 file = part->elts;
447
448 for (i = 0; /* void */ ; i++) {
449
450 if (i >= part->nelts) {
451 if (part->next == NULL) {
452 break;
453 }
454 part = part->next;
455 file = part->elts;
456 i = 0;
457 }
458
459#if 0
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000460 file = old_cycle->open_files.elts;
461 for (i = 0; i < old_cycle->open_files.nelts; i++) {
Igor Sysoevb9e34412004-09-03 15:50:30 +0000462#endif
463
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000464 if (file[i].fd == NGX_INVALID_FILE) {
465 continue;
466 }
467
468 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
469 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
470 ngx_close_file_n " \"%s\" failed",
471 file[i].name.data);
472 }
473 }
474
475 if (old_cycle->connections == NULL) {
476 /* an old cycle is an init cycle */
477 ngx_destroy_pool(old_cycle->pool);
478 return cycle;
479 }
480
Igor Sysoev3d58f8c2004-01-08 08:47:17 +0000481 if (ngx_process == NGX_PROCESS_MASTER) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000482 ngx_destroy_pool(old_cycle->pool);
483 return cycle;
484 }
485
486 if (ngx_temp_pool == NULL) {
487 ngx_temp_pool = ngx_create_pool(128, cycle->log);
488 if (ngx_temp_pool == NULL) {
489 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
490 "can not create ngx_temp_pool");
491 exit(1);
492 }
493
494 n = 10;
495 ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool,
496 n * sizeof(ngx_cycle_t *));
497 if (ngx_old_cycles.elts == NULL) {
498 exit(1);
499 }
500 ngx_old_cycles.nelts = 0;
501 ngx_old_cycles.size = sizeof(ngx_cycle_t *);
502 ngx_old_cycles.nalloc = n;
503 ngx_old_cycles.pool = ngx_temp_pool;
504
505 ngx_cleaner_event.event_handler = ngx_clean_old_cycles;
506 ngx_cleaner_event.log = cycle->log;
507 ngx_cleaner_event.data = &dumb;
508 dumb.fd = (ngx_socket_t) -1;
509 }
510
511 ngx_temp_pool->log = cycle->log;
512
513 old = ngx_push_array(&ngx_old_cycles);
514 if (old == NULL) {
515 exit(1);
516 }
517 *old = old_cycle;
518
519 if (!ngx_cleaner_event.timer_set) {
520 ngx_add_timer(&ngx_cleaner_event, 30000);
521 ngx_cleaner_event.timer_set = 1;
522 }
523
524 return cycle;
525}
526
527
Igor Sysoev43f13192004-04-12 16:38:09 +0000528#if !(WIN32)
529
530ngx_int_t ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle)
Igor Sysoev076498e2004-04-12 06:10:53 +0000531{
Igor Sysoev090849d2004-05-18 20:28:54 +0000532 ngx_uint_t trunc;
Igor Sysoev43f13192004-04-12 16:38:09 +0000533 size_t len;
534 u_char *name, pid[NGX_INT64_LEN + 1];
535 ngx_file_t file;
536 ngx_core_conf_t *ccf, *old_ccf;
537
Igor Sysoev090849d2004-05-18 20:28:54 +0000538 if (!ngx_test_config && old_cycle && old_cycle->conf_ctx == NULL) {
Igor Sysoev43f13192004-04-12 16:38:09 +0000539
540 /*
541 * do not create the pid file in the first ngx_init_cycle() call
542 * because we need to write the demonized process pid
543 */
544
545 return NGX_OK;
546 }
Igor Sysoev076498e2004-04-12 06:10:53 +0000547
548 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
549
Igor Sysoev090849d2004-05-18 20:28:54 +0000550 if (!ngx_test_config && old_cycle) {
Igor Sysoev43f13192004-04-12 16:38:09 +0000551 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
552 ngx_core_module);
Igor Sysoev076498e2004-04-12 06:10:53 +0000553
Igor Sysoev43f13192004-04-12 16:38:09 +0000554 if (ccf->pid.len == old_ccf->pid.len
555 && ngx_strcmp(ccf->pid.data, old_ccf->pid.data) == 0)
Igor Sysoev076498e2004-04-12 06:10:53 +0000556 {
557 return NGX_OK;
558 }
559 }
Igor Sysoev076498e2004-04-12 06:10:53 +0000560
561 len = ngx_snprintf((char *) pid, NGX_INT64_LEN + 1, PID_T_FMT, ngx_pid);
Igor Sysoev076498e2004-04-12 06:10:53 +0000562
Igor Sysoev43f13192004-04-12 16:38:09 +0000563 ngx_memzero(&file, sizeof(ngx_file_t));
564 file.name = (ngx_inherited && getppid() > 1) ? ccf->newpid : ccf->pid;
565 file.log = cycle->log;
Igor Sysoev076498e2004-04-12 06:10:53 +0000566
Igor Sysoev090849d2004-05-18 20:28:54 +0000567 trunc = ngx_test_config ? 0: NGX_FILE_TRUNCATE;
568
Igor Sysoev43f13192004-04-12 16:38:09 +0000569 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
Igor Sysoev090849d2004-05-18 20:28:54 +0000570 NGX_FILE_CREATE_OR_OPEN|trunc);
Igor Sysoev43f13192004-04-12 16:38:09 +0000571
572 if (file.fd == NGX_INVALID_FILE) {
Igor Sysoev076498e2004-04-12 06:10:53 +0000573 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
Igor Sysoev43f13192004-04-12 16:38:09 +0000574 ngx_open_file_n " \"%s\" failed", file.name.data);
Igor Sysoev076498e2004-04-12 06:10:53 +0000575 return NGX_ERROR;
576 }
577
Igor Sysoev090849d2004-05-18 20:28:54 +0000578 if (!ngx_test_config) {
579 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
580 return NGX_ERROR;
581 }
Igor Sysoev076498e2004-04-12 06:10:53 +0000582 }
583
Igor Sysoev43f13192004-04-12 16:38:09 +0000584 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
Igor Sysoev076498e2004-04-12 06:10:53 +0000585 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
Igor Sysoev43f13192004-04-12 16:38:09 +0000586 ngx_close_file_n " \"%s\" failed", file.name.data);
Igor Sysoev076498e2004-04-12 06:10:53 +0000587 }
588
Igor Sysoev43f13192004-04-12 16:38:09 +0000589 ngx_delete_pidfile(old_cycle);
590
Igor Sysoev076498e2004-04-12 06:10:53 +0000591 return NGX_OK;
592}
593
594
Igor Sysoev43f13192004-04-12 16:38:09 +0000595void ngx_delete_pidfile(ngx_cycle_t *cycle)
Igor Sysoev076498e2004-04-12 06:10:53 +0000596{
597 u_char *name;
598 ngx_core_conf_t *ccf;
599
Igor Sysoev43f13192004-04-12 16:38:09 +0000600 if (cycle == NULL || cycle->conf_ctx == NULL) {
601 return;
602 }
603
Igor Sysoev076498e2004-04-12 06:10:53 +0000604 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
605
606 if (ngx_inherited && getppid() > 1) {
607 name = ccf->newpid.data;
608
609 } else {
610 name = ccf->pid.data;
611 }
612
613 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
614 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
615 ngx_delete_file_n " \"%s\" failed", name);
616 }
617}
618
Igor Sysoev43f13192004-04-12 16:38:09 +0000619#endif
620
Igor Sysoev076498e2004-04-12 06:10:53 +0000621
Igor Sysoeva5362982004-03-04 07:04:55 +0000622void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000623{
624 ngx_fd_t fd;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000625 ngx_uint_t i;
Igor Sysoevb9e34412004-09-03 15:50:30 +0000626 ngx_list_part_t *part;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000627 ngx_open_file_t *file;
628
Igor Sysoevb9e34412004-09-03 15:50:30 +0000629 part = &cycle->open_files.part;
630 file = part->elts;
631
632 for (i = 0; /* void */ ; i++) {
633
634 if (i >= part->nelts) {
635 if (part->next == NULL) {
636 break;
637 }
638 part = part->next;
639 i = 0;
640 }
641
642#if 0
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000643 file = cycle->open_files.elts;
644 for (i = 0; i < cycle->open_files.nelts; i++) {
Igor Sysoevb9e34412004-09-03 15:50:30 +0000645#endif
646
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000647 if (file[i].name.data == NULL) {
648 continue;
649 }
650
651 fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR,
652 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);
653
654 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
655 "reopen file \"%s\", old:%d new:%d",
656 file[i].name.data, file[i].fd, fd);
657
658 if (fd == NGX_INVALID_FILE) {
659 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
660 ngx_open_file_n " \"%s\" failed", file[i].name.data);
661 continue;
662 }
663
664#if (WIN32)
665 if (ngx_file_append_mode(fd) == NGX_ERROR) {
666 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
667 ngx_file_append_mode_n " \"%s\" failed",
668 file[i].name.data);
669
670 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
671 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
672 ngx_close_file_n " \"%s\" failed",
673 file[i].name.data);
674 }
675
676 continue;
677 }
Igor Sysoev0911f772004-01-14 18:19:42 +0000678#else
Igor Sysoeva5362982004-03-04 07:04:55 +0000679 if (user != (ngx_uid_t) -1) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000680 if (chown((const char *) file[i].name.data, user, -1) == -1) {
Igor Sysoeva5362982004-03-04 07:04:55 +0000681 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
682 "chown \"%s\" failed", file[i].name.data);
683
684 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
685 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
686 ngx_close_file_n " \"%s\" failed",
687 file[i].name.data);
688 }
689 }
690 }
691
Igor Sysoev0911f772004-01-14 18:19:42 +0000692 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
693 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
694 "fcntl(FD_CLOEXEC) \"%s\" failed",
695 file[i].name.data);
696
697 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
698 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
699 ngx_close_file_n " \"%s\" failed",
700 file[i].name.data);
701 }
702
703 continue;
704 }
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000705#endif
706
707 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
708 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
709 ngx_close_file_n " \"%s\" failed",
710 file[i].name.data);
711 }
712
713 file[i].fd = fd;
714 }
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000715
Igor Sysoeva893eab2004-03-11 21:34:52 +0000716#if !(WIN32)
717
Igor Sysoev25b36fe2004-02-03 16:43:54 +0000718 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
719 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
720 "dup2(STDERR) failed");
721 }
Igor Sysoeva893eab2004-03-11 21:34:52 +0000722
Igor Sysoeva5362982004-03-04 07:04:55 +0000723#endif
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000724}
725
726
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000727static void ngx_clean_old_cycles(ngx_event_t *ev)
728{
Igor Sysoev10a543a2004-03-16 07:10:12 +0000729 ngx_uint_t i, n, found, live;
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000730 ngx_log_t *log;
731 ngx_cycle_t **cycle;
732
733 log = ngx_cycle->log;
734 ngx_temp_pool->log = log;
735
Igor Sysoev54498db2004-02-11 17:08:49 +0000736 ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000737
738 live = 0;
739
740 cycle = ngx_old_cycles.elts;
741 for (i = 0; i < ngx_old_cycles.nelts; i++) {
742
743 if (cycle[i] == NULL) {
744 continue;
745 }
746
747 found = 0;
748
749 for (n = 0; n < cycle[i]->connection_n; n++) {
Igor Sysoev10a543a2004-03-16 07:10:12 +0000750 if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000751 found = 1;
Igor Sysoev54498db2004-02-11 17:08:49 +0000752
753 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%d", n);
754
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000755 break;
756 }
757 }
758
759 if (found) {
760 live = 1;
761 continue;
762 }
763
Igor Sysoev54498db2004-02-11 17:08:49 +0000764 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %d", i);
765
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000766 ngx_destroy_pool(cycle[i]->pool);
767 cycle[i] = NULL;
768 }
769
Igor Sysoev54498db2004-02-11 17:08:49 +0000770 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %d", live);
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000771
772 if (live) {
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000773 ngx_add_timer(ev, 30000);
774
775 } else {
776 ngx_destroy_pool(ngx_temp_pool);
777 ngx_temp_pool = NULL;
778 ngx_old_cycles.nelts = 0;
779 }
780}