rbb 99/02/18 10:06:51
Modified: pthreads/src/main http_main.c Log: Logic for single thread signal handler. Submitted by: Manoj Kasichainula Revision Changes Path 1.46 +78 -59 apache-apr/pthreads/src/main/http_main.c Index: http_main.c =================================================================== RCS file: /home/cvs/apache-apr/pthreads/src/main/http_main.c,v retrieving revision 1.45 retrieving revision 1.46 diff -u -r1.45 -r1.46 --- http_main.c 1999/02/18 05:43:51 1.45 +++ http_main.c 1999/02/18 18:06:49 1.46 @@ -176,7 +176,6 @@ int ap_listenbacklog; int ap_dump_settings = 0; int exiting_now = 0; -pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER; API_VAR_EXPORT int ap_extended_status = 0; @@ -2287,17 +2286,7 @@ } } ap_update_child_status(my_pid, my_tid, SERVER_DEAD, (request_rec *) NULL); - pthread_mutex_lock(&exit_mutex); - if (exiting_now == 0) { - exiting_now++; - pthread_mutex_unlock(&exit_mutex); - graceful_killer(); - clean_child_exit(0); - } - else { - pthread_mutex_unlock(&exit_mutex); - } - pthread_exit(NULL); + return NULL; } void * worker_thread(void * dummy) @@ -2325,17 +2314,7 @@ } ap_destroy_pool(ptrans); ap_update_child_status(my_pid, my_tid, SERVER_DEAD, (request_rec *) NULL); - pthread_mutex_lock(&exit_mutex); - if (exiting_now == 0) { - exiting_now++; - pthread_mutex_unlock(&exit_mutex); - graceful_killer(); - clean_child_exit(0); - } - else { - pthread_mutex_unlock(&exit_mutex); - } - pthread_exit(NULL); + return NULL; } /***************************************************************** @@ -2461,39 +2440,14 @@ #endif /* ndef WIN32 */ } -static void child_main(int child_num_arg) -{ +static void *thread_starter_thread(void *thread_arg) { listen_rec *lr; int i, curr; pthread_t thread; - int my_child_num = child_num_arg; + int my_child_num = *((int *) thread_arg); proc_info *my_info = NULL; sigset_t sig_mask; - int ret; - - my_pid = getpid(); - requests_this_child = ap_max_requests_per_child; - - pchild = ap_make_sub_pool(pconf); - - /*stuff to do before we switch id's, so we have permissions.*/ - reopen_scoreboard(pchild); - SAFE_ACCEPT(accept_mutex_child_init(pchild)); - - set_group_privs(); - if (!geteuid() && (setuid(ap_user_id) == -1)) { - ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf, - "setuid: unable to change uid"); - clean_child_exit(APEXIT_CHILDFATAL); - } - - ap_child_init_modules(pchild, server_conf); - - /*done with init critical section */ - - queue_init(&csd_queue, ap_threads_per_child, pchild); - /* Setup worker threads */ for (i=0; i < ap_threads_per_child; i++) { my_info = NULL; @@ -2559,14 +2513,6 @@ /* no listening sockets???? Kill the server please. */ exit(0); } -#if 0 - /* This thread will be the one responsible for handling signals */ - sigfillset(&sig_mask); - if ((ret = pthread_sigmask(SIG_SETMASK, &sig_mask, NULL)) != 0) { - ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf, "pthread_sigmask failed"); - } -#endif - (void) ap_update_child_status(my_child_num, i, SERVER_STARTING, (request_rec *) NULL); @@ -2581,6 +2527,77 @@ accept_thread(my_info); } +static void child_main(int child_num_arg) +{ + sigset_t sig_mask; + int signal_received; + pthread_t thread; + + my_pid = getpid(); + requests_this_child = ap_max_requests_per_child; + + pchild = ap_make_sub_pool(pconf); + + /*stuff to do before we switch id's, so we have permissions.*/ + reopen_scoreboard(pchild); + SAFE_ACCEPT(accept_mutex_child_init(pchild)); + + set_group_privs(); + + if (!geteuid() && (setuid(ap_user_id) == -1)) { + ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf, + "setuid: unable to change uid"); + clean_child_exit(APEXIT_CHILDFATAL); + } + + ap_child_init_modules(pchild, server_conf); + + /*done with init critical section */ + + /* All threads should mask signals out, accoring to sigwait(2) man page */ + sigemptyset(&sig_mask); + /* Linux 2.0 and its annoying use of SIGUSR{1,2} */ +#ifdef LINUX + /* + sigaddset(&sig_mask, SIGUSR1); + sigaddset(&sig_mask, SIGUSR2); + */ +#endif + + if (pthread_sigmask(SIG_SETMASK, &sig_mask, NULL) != 0) { + ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf, "pthread_sigmask"); + } + + queue_init(&csd_queue, ap_threads_per_child, pchild); + + if (pthread_create(&thread, NULL, thread_starter_thread, &child_num_arg)) { + ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf, + "pthread_create: unable to create thread starter thread"); + clean_child_exit(1); + } + + /* This thread will be the one responsible for handling signals */ + sigemptyset(&sig_mask); + sigaddset(&sig_mask, SIGWINCH); + sigaddset(&sig_mask, SIGTERM); + sigwait(&sig_mask, &signal_received); + /* XXX - Do the appropriate thing for each signal */ + switch (signal_received) { + case SIGWINCH: + graceful_sig_handler(SIGWINCH); + graceful_killer(); + clean_child_exit(0); + break; + case SIGTERM: + just_die(SIGTERM); + break; + default: + ap_log_error(APLOG_MARK, APLOG_ALERT, server_conf, + "received strange signal: %d", signal_received); + + } +} + static int make_child(server_rec *s, int slot, time_t now) /* ZZZ */ { pthread_t tid; /* ZZZZ */ @@ -2638,9 +2655,11 @@ RAISE_SIGSTOP(MAKE_CHILD); MONCONTROL(1); + /* signal(SIGWINCH, graceful_sig_handler); signal(SIGTERM, just_die); - child_main(slot); + */ + child_main(slot); return 0; }