comparison src/os/unix/ngx_process.c @ 4311:45272aab5eea

Unlock of shared memory zones on process crash. If process exited abnormally while holding lock on some shared memory zone - unlock it. It may be not safe thing to do (as crash with lock held may result in corrupted shared memory structure, and other processes will subsequently crash while trying to access shared data), therefore complain loudly if unlock succeeds.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 23 Nov 2011 14:09:19 +0000
parents 3f6040cd731e
children 424a1ac6af43
comparison
equal deleted inserted replaced
4310:13f108b9f3cf 4311:45272aab5eea
20 20
21 21
22 static void ngx_execute_proc(ngx_cycle_t *cycle, void *data); 22 static void ngx_execute_proc(ngx_cycle_t *cycle, void *data);
23 static void ngx_signal_handler(int signo); 23 static void ngx_signal_handler(int signo);
24 static void ngx_process_get_status(void); 24 static void ngx_process_get_status(void);
25 static void ngx_unlock_mutexes(ngx_pid_t pid);
25 26
26 27
27 int ngx_argc; 28 int ngx_argc;
28 char **ngx_argv; 29 char **ngx_argv;
29 char **ngx_os_argv; 30 char **ngx_os_argv;
492 #endif 493 #endif
493 494
494 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err, 495 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
495 "waitpid() failed"); 496 "waitpid() failed");
496 return; 497 return;
497 }
498
499
500 if (ngx_accept_mutex_ptr) {
501
502 /*
503 * unlock the accept mutex if the abnormally exited process
504 * held it
505 */
506
507 ngx_shmtx_force_unlock(&ngx_accept_mutex, pid);
508 } 498 }
509 499
510 500
511 one = 1; 501 one = 1;
512 process = "unknown process"; 502 process = "unknown process";
543 "%s %P exited with fatal code %d " 533 "%s %P exited with fatal code %d "
544 "and cannot be respawned", 534 "and cannot be respawned",
545 process, pid, WEXITSTATUS(status)); 535 process, pid, WEXITSTATUS(status));
546 ngx_processes[i].respawn = 0; 536 ngx_processes[i].respawn = 0;
547 } 537 }
538
539 ngx_unlock_mutexes(pid);
540 }
541 }
542
543
544 static void
545 ngx_unlock_mutexes(ngx_pid_t pid)
546 {
547 ngx_uint_t i;
548 ngx_shm_zone_t *shm_zone;
549 ngx_list_part_t *part;
550 ngx_slab_pool_t *sp;
551
552 /*
553 * unlock the accept mutex if the abnormally exited process
554 * held it
555 */
556
557 if (ngx_accept_mutex_ptr) {
558 ngx_shmtx_force_unlock(&ngx_accept_mutex, pid);
559 }
560
561 /*
562 * unlock shared memory mutexes if held by the abnormally exited
563 * process
564 */
565
566 part = (ngx_list_part_t *) &ngx_cycle->shared_memory.part;
567 shm_zone = part->elts;
568
569 for (i = 0; /* void */ ; i++) {
570
571 if (i >= part->nelts) {
572 if (part->next == NULL) {
573 break;
574 }
575 part = part->next;
576 shm_zone = part->elts;
577 i = 0;
578 }
579
580 sp = (ngx_slab_pool_t *) shm_zone[i].shm.addr;
581
582 if (ngx_shmtx_force_unlock(&sp->mutex, pid)) {
583 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
584 "shared memory zone \"%V\" was locked by %P",
585 &shm_zone[i].shm.name, pid);
586 }
548 } 587 }
549 } 588 }
550 589
551 590
552 void 591 void