Mercurial > hg > nginx
comparison src/core/ngx_cycle.c @ 611:3f8a2132b93d release-0.3.27
nginx-0.3.27-RELEASE import
*) Change: the "variables_hash_max_size" and
"variables_hash_bucket_size" directives.
*) Feature: the $body_bytes_sent variable can be used not only in the
"log_format" directive.
*) Feature: the $ssl_protocol and $ssl_cipher variables.
*) Feature: the cache line size detection for widespread CPUs at start
time.
*) Feature: now the "accept_mutex" directive is supported using
fcntl(2) on platforms different from i386, amd64, sparc64, and ppc.
*) Feature: the "lock_file" directive and the --with-lock-path=PATH
autoconfiguration directive.
*) Bugfix: if the HTTPS protocol was used in the "proxy_pass" directive
then the requests with the body was not transferred.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 08 Feb 2006 15:33:12 +0000 |
parents | 77cdfe394a94 |
children | 532d15ddbe68 |
comparison
equal
deleted
inserted
replaced
610:200d628b47dd | 611:3f8a2132b93d |
---|---|
7 #include <ngx_config.h> | 7 #include <ngx_config.h> |
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 #include <ngx_event.h> | 9 #include <ngx_event.h> |
10 | 10 |
11 | 11 |
12 static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log); | |
12 static void ngx_destroy_cycle_pools(ngx_conf_t *conf); | 13 static void ngx_destroy_cycle_pools(ngx_conf_t *conf); |
13 static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2); | 14 static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2); |
14 static void ngx_clean_old_cycles(ngx_event_t *ev); | 15 static void ngx_clean_old_cycles(ngx_event_t *ev); |
15 | 16 |
16 | 17 |
40 | 41 |
41 ngx_cycle_t * | 42 ngx_cycle_t * |
42 ngx_init_cycle(ngx_cycle_t *old_cycle) | 43 ngx_init_cycle(ngx_cycle_t *old_cycle) |
43 { | 44 { |
44 void *rv; | 45 void *rv; |
45 ngx_uint_t i, n, failed; | 46 ngx_uint_t i, n; |
46 ngx_log_t *log; | 47 ngx_log_t *log; |
47 ngx_conf_t conf; | 48 ngx_conf_t conf; |
48 ngx_pool_t *pool; | 49 ngx_pool_t *pool; |
49 ngx_cycle_t *cycle, **old; | 50 ngx_cycle_t *cycle, **old; |
50 ngx_list_part_t *part; | 51 ngx_list_part_t *part; |
51 ngx_open_file_t *file; | 52 ngx_open_file_t *file; |
52 ngx_listening_t *ls, *nls; | 53 ngx_listening_t *ls, *nls; |
53 ngx_core_conf_t *ccf; | 54 ngx_core_conf_t *ccf; |
54 ngx_core_module_t *module; | 55 ngx_core_module_t *module; |
55 | 56 #if !(WIN32) |
57 ngx_core_conf_t *old_ccf; | |
58 #endif | |
56 | 59 |
57 log = old_cycle->log; | 60 log = old_cycle->log; |
58 | 61 |
59 pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log); | 62 pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log); |
60 if (pool == NULL) { | 63 if (pool == NULL) { |
234 } | 237 } |
235 } | 238 } |
236 } | 239 } |
237 | 240 |
238 | 241 |
239 failed = 0; | 242 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
240 | |
241 | 243 |
242 #if !(NGX_WIN32) | 244 #if !(NGX_WIN32) |
243 if (ngx_create_pidfile(cycle, old_cycle) == NGX_ERROR) { | 245 |
244 failed = 1; | 246 if (ngx_test_config) { |
245 } | 247 |
246 #endif | 248 if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) { |
247 | 249 goto failed; |
248 | 250 } |
249 if (!failed) { | 251 |
250 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, | 252 } else if (!ngx_is_init_cycle(old_cycle)) { |
251 ngx_core_module); | 253 |
252 | 254 /* |
253 if (ngx_create_pathes(cycle, ccf->user) == NGX_ERROR) { | 255 * we do not create the pid file in the first ngx_init_cycle() call |
254 failed = 1; | 256 * because we need to write the demonized process pid |
255 } | 257 */ |
256 } | 258 |
257 | 259 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx, |
258 | 260 ngx_core_module); |
259 if (!failed) { | 261 if (ccf->pid.len != old_ccf->pid.len |
260 | 262 || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0) |
261 /* open the new files */ | 263 { |
262 | 264 /* new pid file name */ |
263 part = &cycle->open_files.part; | 265 |
264 file = part->elts; | 266 if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) { |
265 | 267 goto failed; |
266 for (i = 0; /* void */ ; i++) { | 268 } |
267 | 269 |
268 if (i >= part->nelts) { | 270 ngx_delete_pidfile(old_cycle); |
269 if (part->next == NULL) { | 271 } |
272 } | |
273 | |
274 #endif | |
275 | |
276 | |
277 if (ngx_test_lockfile(ccf->lock_file.data, log) != NGX_OK) { | |
278 goto failed; | |
279 } | |
280 | |
281 | |
282 if (ngx_create_pathes(cycle, ccf->user) != NGX_OK) { | |
283 goto failed; | |
284 } | |
285 | |
286 | |
287 /* open the new files */ | |
288 | |
289 part = &cycle->open_files.part; | |
290 file = part->elts; | |
291 | |
292 for (i = 0; /* void */ ; i++) { | |
293 | |
294 if (i >= part->nelts) { | |
295 if (part->next == NULL) { | |
296 break; | |
297 } | |
298 part = part->next; | |
299 file = part->elts; | |
300 i = 0; | |
301 } | |
302 | |
303 if (file[i].name.data == NULL) { | |
304 continue; | |
305 } | |
306 | |
307 file[i].fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR, | |
308 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); | |
309 | |
310 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0, | |
311 "log: %p %d \"%s\"", | |
312 &file[i], file[i].fd, file[i].name.data); | |
313 | |
314 if (file[i].fd == NGX_INVALID_FILE) { | |
315 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
316 ngx_open_file_n " \"%s\" failed", | |
317 file[i].name.data); | |
318 goto failed; | |
319 } | |
320 | |
321 #if (NGX_WIN32) | |
322 if (ngx_file_append_mode(file[i].fd) != NGX_OK) { | |
323 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
324 ngx_file_append_mode_n " \"%s\" failed", | |
325 file[i].name.data); | |
326 goto failed; | |
327 } | |
328 #else | |
329 if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) { | |
330 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
331 "fcntl(FD_CLOEXEC) \"%s\" failed", | |
332 file[i].name.data); | |
333 goto failed; | |
334 } | |
335 #endif | |
336 } | |
337 | |
338 cycle->log = cycle->new_log; | |
339 pool->log = cycle->new_log; | |
340 | |
341 if (cycle->log->log_level == 0) { | |
342 cycle->log->log_level = NGX_LOG_ERR; | |
343 } | |
344 | |
345 | |
346 /* handle the listening sockets */ | |
347 | |
348 if (old_cycle->listening.nelts) { | |
349 ls = old_cycle->listening.elts; | |
350 for (i = 0; i < old_cycle->listening.nelts; i++) { | |
351 ls[i].remain = 0; | |
352 } | |
353 | |
354 nls = cycle->listening.elts; | |
355 for (n = 0; n < cycle->listening.nelts; n++) { | |
356 | |
357 for (i = 0; i < old_cycle->listening.nelts; i++) { | |
358 if (ls[i].ignore) { | |
359 continue; | |
360 } | |
361 | |
362 if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) == NGX_OK) | |
363 { | |
364 nls[n].fd = ls[i].fd; | |
365 nls[n].previous = &ls[i]; | |
366 ls[i].remain = 1; | |
367 | |
368 if (ls[n].backlog != nls[i].backlog) { | |
369 nls[n].listen = 1; | |
370 } | |
371 | |
372 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) | |
373 | |
374 /* | |
375 * FreeBSD, except the most recent versions, | |
376 * could not remove accept filter | |
377 */ | |
378 nls[n].deferred_accept = ls[i].deferred_accept; | |
379 | |
380 if (ls[i].accept_filter && nls[n].accept_filter) { | |
381 if (ngx_strcmp(ls[i].accept_filter, | |
382 nls[n].accept_filter) | |
383 != 0) | |
384 { | |
385 nls[n].delete_deferred = 1; | |
386 nls[n].add_deferred = 1; | |
387 } | |
388 | |
389 } else if (ls[i].accept_filter) { | |
390 nls[n].delete_deferred = 1; | |
391 | |
392 } else if (nls[n].accept_filter) { | |
393 nls[n].add_deferred = 1; | |
394 } | |
395 #endif | |
396 | |
397 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) | |
398 | |
399 if (ls[n].deferred_accept && !nls[n].deferred_accept) { | |
400 nls[n].delete_deferred = 1; | |
401 | |
402 } else if (ls[i].deferred_accept != nls[n].deferred_accept) | |
403 { | |
404 nls[n].add_deferred = 1; | |
405 } | |
406 #endif | |
270 break; | 407 break; |
271 } | 408 } |
272 part = part->next; | 409 } |
273 file = part->elts; | 410 |
274 i = 0; | 411 if (nls[n].fd == -1) { |
275 } | 412 nls[n].open = 1; |
276 | 413 } |
277 if (file[i].name.data == NULL) { | 414 } |
278 continue; | 415 |
279 } | 416 } else { |
280 | |
281 file[i].fd = ngx_open_file(file[i].name.data, | |
282 NGX_FILE_RDWR, | |
283 NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); | |
284 | |
285 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0, | |
286 "log: %p %d \"%s\"", | |
287 &file[i], file[i].fd, file[i].name.data); | |
288 | |
289 if (file[i].fd == NGX_INVALID_FILE) { | |
290 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
291 ngx_open_file_n " \"%s\" failed", | |
292 file[i].name.data); | |
293 failed = 1; | |
294 break; | |
295 } | |
296 | |
297 #if (NGX_WIN32) | |
298 if (ngx_file_append_mode(file[i].fd) == NGX_ERROR) { | |
299 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
300 ngx_file_append_mode_n " \"%s\" failed", | |
301 file[i].name.data); | |
302 failed = 1; | |
303 break; | |
304 } | |
305 #else | |
306 if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) { | |
307 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
308 "fcntl(FD_CLOEXEC) \"%s\" failed", | |
309 file[i].name.data); | |
310 failed = 1; | |
311 break; | |
312 } | |
313 #endif | |
314 } | |
315 } | |
316 | |
317 cycle->log = cycle->new_log; | |
318 pool->log = cycle->new_log; | |
319 | |
320 if (cycle->log->log_level == 0) { | |
321 cycle->log->log_level = NGX_LOG_ERR; | |
322 } | |
323 | |
324 if (!failed) { | |
325 | |
326 /* handle the listening sockets */ | |
327 | |
328 if (old_cycle->listening.nelts) { | |
329 ls = old_cycle->listening.elts; | |
330 for (i = 0; i < old_cycle->listening.nelts; i++) { | |
331 ls[i].remain = 0; | |
332 } | |
333 | |
334 nls = cycle->listening.elts; | |
335 for (n = 0; n < cycle->listening.nelts; n++) { | |
336 | |
337 for (i = 0; i < old_cycle->listening.nelts; i++) { | |
338 if (ls[i].ignore) { | |
339 continue; | |
340 } | |
341 | |
342 if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) | |
343 == NGX_OK) | |
344 { | |
345 nls[n].fd = ls[i].fd; | |
346 nls[n].previous = &ls[i]; | |
347 ls[i].remain = 1; | |
348 | |
349 if (ls[n].backlog != nls[i].backlog) { | |
350 nls[n].listen = 1; | |
351 } | |
352 | |
353 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) | |
354 | |
355 /* | |
356 * FreeBSD, except the most recent versions, | |
357 * could not remove accept filter | |
358 */ | |
359 nls[n].deferred_accept = ls[i].deferred_accept; | |
360 | |
361 if (ls[i].accept_filter && nls[n].accept_filter) { | |
362 if (ngx_strcmp(ls[i].accept_filter, | |
363 nls[n].accept_filter) != 0) | |
364 { | |
365 nls[n].delete_deferred = 1; | |
366 nls[n].add_deferred = 1; | |
367 } | |
368 | |
369 } else if (ls[i].accept_filter) { | |
370 nls[n].delete_deferred = 1; | |
371 | |
372 } else if (nls[n].accept_filter) { | |
373 nls[n].add_deferred = 1; | |
374 } | |
375 #endif | |
376 | |
377 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) | |
378 | |
379 if (ls[n].deferred_accept && !nls[n].deferred_accept) { | |
380 nls[n].delete_deferred = 1; | |
381 | |
382 } else if (ls[i].deferred_accept | |
383 != nls[n].deferred_accept) | |
384 { | |
385 nls[n].add_deferred = 1; | |
386 } | |
387 #endif | |
388 break; | |
389 } | |
390 } | |
391 | |
392 if (nls[n].fd == -1) { | |
393 nls[n].open = 1; | |
394 } | |
395 } | |
396 | |
397 } else { | |
398 ls = cycle->listening.elts; | |
399 for (i = 0; i < cycle->listening.nelts; i++) { | |
400 ls[i].open = 1; | |
401 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) | |
402 if (ls[i].accept_filter) { | |
403 ls[i].add_deferred = 1; | |
404 } | |
405 #endif | |
406 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) | |
407 if (ls[i].deferred_accept) { | |
408 ls[i].add_deferred = 1; | |
409 } | |
410 #endif | |
411 } | |
412 } | |
413 | |
414 if (!failed) { | |
415 if (ngx_open_listening_sockets(cycle) == NGX_ERROR) { | |
416 failed = 1; | |
417 } | |
418 | |
419 if (!ngx_test_config && !failed) { | |
420 ngx_configure_listening_socket(cycle); | |
421 } | |
422 } | |
423 } | |
424 | |
425 | |
426 if (failed) { | |
427 | |
428 /* rollback the new cycle configuration */ | |
429 | |
430 part = &cycle->open_files.part; | |
431 file = part->elts; | |
432 | |
433 for (i = 0; /* void */ ; i++) { | |
434 | |
435 if (i >= part->nelts) { | |
436 if (part->next == NULL) { | |
437 break; | |
438 } | |
439 part = part->next; | |
440 file = part->elts; | |
441 i = 0; | |
442 } | |
443 | |
444 if (file[i].fd == NGX_INVALID_FILE | |
445 || file[i].fd == ngx_stderr_fileno) | |
446 { | |
447 continue; | |
448 } | |
449 | |
450 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { | |
451 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
452 ngx_close_file_n " \"%s\" failed", | |
453 file[i].name.data); | |
454 } | |
455 } | |
456 | |
457 if (ngx_test_config) { | |
458 ngx_destroy_cycle_pools(&conf); | |
459 return NULL; | |
460 } | |
461 | |
462 ls = cycle->listening.elts; | 417 ls = cycle->listening.elts; |
463 for (i = 0; i < cycle->listening.nelts; i++) { | 418 for (i = 0; i < cycle->listening.nelts; i++) { |
464 if (ls[i].fd == -1 || !ls[i].open) { | 419 ls[i].open = 1; |
465 continue; | 420 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER) |
466 } | 421 if (ls[i].accept_filter) { |
467 | 422 ls[i].add_deferred = 1; |
468 if (ngx_close_socket(ls[i].fd) == -1) { | 423 } |
469 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | 424 #endif |
470 ngx_close_socket_n " %V failed", | 425 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT) |
471 &ls[i].addr_text); | 426 if (ls[i].deferred_accept) { |
472 } | 427 ls[i].add_deferred = 1; |
473 } | 428 } |
474 | 429 #endif |
475 ngx_destroy_cycle_pools(&conf); | 430 } |
476 return NULL; | 431 } |
432 | |
433 if (ngx_open_listening_sockets(cycle) != NGX_OK) { | |
434 goto failed; | |
435 } | |
436 | |
437 if (!ngx_test_config) { | |
438 ngx_configure_listening_socket(cycle); | |
477 } | 439 } |
478 | 440 |
479 | 441 |
480 /* commit the new cycle configuration */ | 442 /* commit the new cycle configuration */ |
481 | 443 |
486 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0, | 448 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0, |
487 "dup2: %p %d \"%s\"", | 449 "dup2: %p %d \"%s\"", |
488 cycle->log->file, | 450 cycle->log->file, |
489 cycle->log->file->fd, cycle->log->file->name.data); | 451 cycle->log->file->fd, cycle->log->file->name.data); |
490 | 452 |
491 if (dup2(cycle->log->file->fd, STDERR_FILENO) == NGX_ERROR) { | 453 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) { |
492 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | 454 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
493 "dup2(STDERR) failed"); | 455 "dup2(STDERR) failed"); |
494 /* fatal */ | 456 /* fatal */ |
495 exit(1); | 457 exit(1); |
496 } | 458 } |
500 | 462 |
501 pool->log = cycle->log; | 463 pool->log = cycle->log; |
502 | 464 |
503 for (i = 0; ngx_modules[i]; i++) { | 465 for (i = 0; ngx_modules[i]; i++) { |
504 if (ngx_modules[i]->init_module) { | 466 if (ngx_modules[i]->init_module) { |
505 if (ngx_modules[i]->init_module(cycle) == NGX_ERROR) { | 467 if (ngx_modules[i]->init_module(cycle) != NGX_OK) { |
506 /* fatal */ | 468 /* fatal */ |
507 exit(1); | 469 exit(1); |
508 } | 470 } |
509 } | 471 } |
510 } | 472 } |
562 if (old_cycle->shm.addr) { | 524 if (old_cycle->shm.addr) { |
563 ngx_shm_free(&old_cycle->shm); | 525 ngx_shm_free(&old_cycle->shm); |
564 } | 526 } |
565 | 527 |
566 ngx_destroy_pool(old_cycle->pool); | 528 ngx_destroy_pool(old_cycle->pool); |
529 | |
530 cycle->old_cycle = NULL; | |
531 | |
567 return cycle; | 532 return cycle; |
568 } | 533 } |
569 | 534 |
570 | 535 |
571 if (ngx_temp_pool == NULL) { | 536 if (ngx_temp_pool == NULL) { |
605 ngx_add_timer(&ngx_cleaner_event, 30000); | 570 ngx_add_timer(&ngx_cleaner_event, 30000); |
606 ngx_cleaner_event.timer_set = 1; | 571 ngx_cleaner_event.timer_set = 1; |
607 } | 572 } |
608 | 573 |
609 return cycle; | 574 return cycle; |
575 | |
576 | |
577 failed: | |
578 | |
579 /* rollback the new cycle configuration */ | |
580 | |
581 part = &cycle->open_files.part; | |
582 file = part->elts; | |
583 | |
584 for (i = 0; /* void */ ; i++) { | |
585 | |
586 if (i >= part->nelts) { | |
587 if (part->next == NULL) { | |
588 break; | |
589 } | |
590 part = part->next; | |
591 file = part->elts; | |
592 i = 0; | |
593 } | |
594 | |
595 if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) { | |
596 continue; | |
597 } | |
598 | |
599 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) { | |
600 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
601 ngx_close_file_n " \"%s\" failed", | |
602 file[i].name.data); | |
603 } | |
604 } | |
605 | |
606 if (ngx_test_config) { | |
607 ngx_destroy_cycle_pools(&conf); | |
608 return NULL; | |
609 } | |
610 | |
611 ls = cycle->listening.elts; | |
612 for (i = 0; i < cycle->listening.nelts; i++) { | |
613 if (ls[i].fd == -1 || !ls[i].open) { | |
614 continue; | |
615 } | |
616 | |
617 if (ngx_close_socket(ls[i].fd) == -1) { | |
618 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, | |
619 ngx_close_socket_n " %V failed", | |
620 &ls[i].addr_text); | |
621 } | |
622 } | |
623 | |
624 ngx_destroy_cycle_pools(&conf); | |
625 | |
626 return NULL; | |
610 } | 627 } |
611 | 628 |
612 | 629 |
613 static void | 630 static void |
614 ngx_destroy_cycle_pools(ngx_conf_t *conf) | 631 ngx_destroy_cycle_pools(ngx_conf_t *conf) |
649 | 666 |
650 | 667 |
651 #if !(NGX_WIN32) | 668 #if !(NGX_WIN32) |
652 | 669 |
653 ngx_int_t | 670 ngx_int_t |
654 ngx_create_pidfile(ngx_cycle_t *cycle, ngx_cycle_t *old_cycle) | 671 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log) |
655 { | 672 { |
673 size_t len; | |
656 ngx_uint_t trunc; | 674 ngx_uint_t trunc; |
657 size_t len; | 675 ngx_file_t file; |
658 u_char pid[NGX_INT64_LEN]; | 676 u_char pid[NGX_INT64_LEN]; |
659 ngx_file_t file; | |
660 ngx_core_conf_t *ccf, *old_ccf; | |
661 | |
662 if (!ngx_test_config && ngx_is_init_cycle(old_cycle)) { | |
663 | |
664 /* | |
665 * do not create the pid file in the first ngx_init_cycle() call | |
666 * because we need to write the demonized process pid | |
667 */ | |
668 | |
669 return NGX_OK; | |
670 } | |
671 | |
672 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | |
673 | |
674 if (!ngx_test_config && old_cycle) { | |
675 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx, | |
676 ngx_core_module); | |
677 | |
678 if (ccf->pid.len == old_ccf->pid.len | |
679 && ngx_strcmp(ccf->pid.data, old_ccf->pid.data) == 0) | |
680 { | |
681 | |
682 /* pid file name is the same */ | |
683 | |
684 return NGX_OK; | |
685 } | |
686 } | |
687 | 677 |
688 ngx_memzero(&file, sizeof(ngx_file_t)); | 678 ngx_memzero(&file, sizeof(ngx_file_t)); |
689 | 679 |
690 file.name = ccf->pid; | 680 file.name = *name; |
691 file.log = cycle->log; | 681 file.log = log; |
692 | 682 |
693 trunc = ngx_test_config ? 0: NGX_FILE_TRUNCATE; | 683 trunc = ngx_test_config ? 0 : NGX_FILE_TRUNCATE; |
694 | 684 |
695 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR, | 685 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR, |
696 NGX_FILE_CREATE_OR_OPEN|trunc); | 686 NGX_FILE_CREATE_OR_OPEN|trunc); |
697 | 687 |
698 if (file.fd == NGX_INVALID_FILE) { | 688 if (file.fd == NGX_INVALID_FILE) { |
699 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, | 689 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
700 ngx_open_file_n " \"%s\" failed", file.name.data); | 690 ngx_open_file_n " \"%s\" failed", file.name.data); |
701 return NGX_ERROR; | 691 return NGX_ERROR; |
702 } | 692 } |
703 | 693 |
704 if (!ngx_test_config) { | 694 if (!ngx_test_config) { |
708 return NGX_ERROR; | 698 return NGX_ERROR; |
709 } | 699 } |
710 } | 700 } |
711 | 701 |
712 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { | 702 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { |
713 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, | 703 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
714 ngx_close_file_n " \"%s\" failed", file.name.data); | 704 ngx_close_file_n " \"%s\" failed", file.name.data); |
715 } | 705 } |
716 | |
717 ngx_delete_pidfile(old_cycle); | |
718 | 706 |
719 return NGX_OK; | 707 return NGX_OK; |
720 } | 708 } |
721 | 709 |
722 | 710 |
724 ngx_delete_pidfile(ngx_cycle_t *cycle) | 712 ngx_delete_pidfile(ngx_cycle_t *cycle) |
725 { | 713 { |
726 u_char *name; | 714 u_char *name; |
727 ngx_core_conf_t *ccf; | 715 ngx_core_conf_t *ccf; |
728 | 716 |
729 if (cycle == NULL || cycle->conf_ctx == NULL) { | |
730 return; | |
731 } | |
732 | |
733 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); | 717 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
734 | 718 |
735 name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data; | 719 name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data; |
736 | 720 |
737 if (ngx_delete_file(name) == NGX_FILE_ERROR) { | 721 if (ngx_delete_file(name) == NGX_FILE_ERROR) { |
739 ngx_delete_file_n " \"%s\" failed", name); | 723 ngx_delete_file_n " \"%s\" failed", name); |
740 } | 724 } |
741 } | 725 } |
742 | 726 |
743 #endif | 727 #endif |
728 | |
729 | |
730 static ngx_int_t | |
731 ngx_test_lockfile(u_char *file, ngx_log_t *log) | |
732 { | |
733 #if !(NGX_HAVE_ATOMIC_OPS) | |
734 ngx_fd_t fd; | |
735 | |
736 fd = ngx_open_file(file, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN); | |
737 | |
738 if (fd == NGX_INVALID_FILE) { | |
739 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
740 ngx_open_file_n " \"%s\" failed", file); | |
741 return NGX_ERROR; | |
742 } | |
743 | |
744 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
745 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
746 ngx_close_file_n " \"%s\" failed", file); | |
747 } | |
748 | |
749 if (ngx_delete_file(file) == NGX_FILE_ERROR) { | |
750 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
751 ngx_delete_file_n " \"%s\" failed", file); | |
752 } | |
753 | |
754 #endif | |
755 | |
756 return NGX_OK; | |
757 } | |
744 | 758 |
745 | 759 |
746 void | 760 void |
747 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user) | 761 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user) |
748 { | 762 { |