threaded.c revision c26b079f6995cd8fb30c68d2e17b69b2f91ac700
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* ====================================================================
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * The Apache Software License, Version 1.1
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * reserved.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Redistribution and use in source and binary forms, with or without
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * modification, are permitted provided that the following conditions
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * are met:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * 1. Redistributions of source code must retain the above copyright
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * notice, this list of conditions and the following disclaimer.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * 2. Redistributions in binary form must reproduce the above copyright
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * notice, this list of conditions and the following disclaimer in
27e52281f1522522b170cafc76b08b58aa70ccaand * the documentation and/or other materials provided with the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * distribution.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * 3. The end-user documentation included with the redistribution,
4b5981e276e93df97c34e4da05ca5cf8bbd937dand * if any, must include the following acknowledgment:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * "This product includes software developed by the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Apache Software Foundation (http://www.apache.org/)."
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * Alternately, this acknowledgment may appear in the software itself,
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * if and wherever such third-party acknowledgments normally appear.
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * 4. The names "Apache" and "Apache Software Foundation" must
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * not be used to endorse or promote products derived from this
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * software without prior written permission. For written
a76bfec941c4c5e3e1e24d7007108f4d3dac91a5nd * permission, please contact apache@apache.org.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * 5. Products derived from this software may not be called "Apache",
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * nor may "Apache" appear in their name, without prior written
6df89e6e4adeb986b41b7ec6b7593a887e031ce7nd * permission of the Apache Software Foundation.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * SUCH DAMAGE.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * ====================================================================
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * This software consists of voluntary contributions made by many
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * individuals on behalf of the Apache Software Foundation. For more
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * information on the Apache Software Foundation, please see
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Portions of this software are based upon public domain software
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * originally written at the National Center for Supercomputing Applications,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * University of Illinois, Urbana-Champaign.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#error The threaded MPM requires APR threads, but they are unavailable.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Actual definitions of config globals
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndint ap_threads_per_child=0; /* Worker threads per child */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int dying = 0;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int workers_may_exit = 0;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int num_listensocks = 0;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* The structure used to pass unique initialization info to each thread */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndtypedef struct {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* Structure used to pass information to the thread responsible for
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * creating the rest of the threads.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndtypedef struct {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * The max child slot ever assigned, preserved across restarts. Necessary
a8703cfb67133446eef7af1043640e71486e9ecand * to deal with MaxClients changes across SIGWINCH restarts. We use this
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * value to optimize routines that have to scan the entire scoreboard.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic apr_lock_t *pipe_of_death_mutex; /* insures that a child process only
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd consumes one character */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* *Non*-shared http_main globals... */
71fccc298df6a1540d408151a26aa22beed55d0bnd/* The threaded MPM respects a couple of runtime flags that can aid
71fccc298df6a1540d408151a26aa22beed55d0bnd * in debugging. Setting the -DNO_DETACH flag will prevent the root process
71fccc298df6a1540d408151a26aa22beed55d0bnd * from detaching from its controlling terminal. Additionally, setting
71fccc298df6a1540d408151a26aa22beed55d0bnd * the -DONE_PROCESS flag (which implies -DNO_DETACH) will get you the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * child_main loop running in the process which originally started up.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * This gives you a pretty nice debugging environment. (You'll get a SIGHUP
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * early in standalone_main; just continue through. This is the server
b3c7a2279fa7a45f5807d9a404760b9b3760df50nd * trying to kill off any child processes which it might have lying
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * around --- Apache doesn't keep track of their pids, it just sends
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * SIGHUP to the process group, ignoring it in the root process.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Continue through and you'll be fine.).
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic int one_process = 0;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic apr_pool_t *pchild; /* Pool for httpd child stuff */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic pid_t ap_my_pid; /* Linux getpid() doesn't work except in main
909ce17e2bd0faef7b1c294f2307f009793fd493nd thread. Use this instead */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* Keep track of the number of worker threads currently active */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* Locks for accept serialization */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndAP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* a clean exit from a child with proper cleanup */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic void clean_child_exit(int code) __attribute__ ((noreturn));
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* handle all varieties of core dumping signals */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "seg fault or similar nasty error detected "
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd "in the parent process");
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* XXX we can probably add some rudimentary cleanup code here,
71fccc298df6a1540d408151a26aa22beed55d0bnd * like getting rid of the pid file. If any additional bad stuff
71fccc298df6a1540d408151a26aa22beed55d0bnd * happens, we are protected from recursive errors taking down the
71fccc298df6a1540d408151a26aa22beed55d0bnd * system since this function is no longer the signal handler GLA
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* At this point we've got sig blocked, because we're still inside
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * the signal handler. When we leave the signal handler it will
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * be unblocked, and we'll take the signal... and coredump or whatever
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * is appropriate for this particular Unix. In addition the parent
9472e4d3c410be3b3f1addbf3b1db1769f64e765nd * will see the real signal we received -- whereas if we called
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * abort() here, the parent would only see SIGABRT.
clean_child_exit(0);
static int volatile shutdown_pending;
static int volatile restart_pending;
static int volatile is_graceful;
static void ap_start_shutdown(void)
if (is_graceful) {
static void set_signals(void)
#ifndef NO_USE_SIGACTION
if (!one_process) {
#if defined(SA_ONESHOT)
#ifdef SIGBUS
#ifdef SIGABORT
#ifdef SIGABRT
#ifdef SIGILL
#ifdef SIGINT
#ifdef SIGXCPU
#ifdef SIGXFSZ
#ifdef SIGPIPE
if (!one_process) {
#ifdef SIGBUS
#ifdef SIGABORT
#ifdef SIGABRT
#ifdef SIGILL
#ifdef SIGXCPU
#ifdef SIGXFSZ
#ifdef SIGHUP
#ifdef SIGWINCH
#ifdef SIGPIPE
int ap_graceful_stop_signalled(void)
return workers_may_exit;
int csd;
if (current_conn) {
static void check_infinite_requests(void)
if (ap_max_requests_per_child) {
static void check_pipe_of_death(void)
if (!workers_may_exit) {
char pipe_read_char;
for(n=0 ; n <= num_listensocks ; ++n)
if (requests_this_child <= 0) {
if (workers_may_exit) break;
!= APR_SUCCESS) {
while (!workers_may_exit) {
if (workers_may_exit) break;
goto got_fd;
curr_pollfd++;
goto got_fd;
if (!workers_may_exit) {
!= APR_SUCCESS) {
!= APR_SUCCESS) {
if (!dying) {
if (worker_thread_count == 0) {
return NULL;
switch (signum) {
case SIGTERM:
case SIGINT:
int threads_created = 0;
for (i=0; i < ap_threads_per_child; i++) {
return NULL;
pchild));
if (unixd_setup_child()) {
if (ap_max_requests_per_child) {
worker_thread_count = 0;
for (i = 0; i < ap_threads_per_child; i++) {
clean_child_exit(0);
int pid;
if (one_process) {
set_signals();
if (!pid) {
#ifdef HAVE_BINDPROCESSOR
clean_child_exit(0);
static void wake_up_and_die(void)
for (i = 0; i < ap_daemons_limit;) {
!= APR_SUCCESS) {
#ifndef MAX_SPAWN_RATE
static int hold_off_on_exponential_spawning;
static void perform_idle_server_maintenance(void)
int idle_thread_count;
int free_length;
int totally_free_length = 0;
int last_non_dead;
int total_non_dead;
free_length = 0;
idle_thread_count = 0;
total_non_dead = 0;
for (i = 0; i < ap_daemons_limit; ++i) {
int any_dying_threads = 0;
int any_dead_threads = 0;
for (j = 0; j < ap_threads_per_child; j++) {
if (all_dead_threads) {
++free_length;
if (!all_dead_threads) {
last_non_dead = i;
if (!any_dying_threads) {
if (free_length == 0) {
static int reported = 0;
if (!reported) {
"or Min/MaxSpareThreads), "
for (i = 0; i < free_length; ++i) {
int child_slot;
if (child_slot >= 0) {
for (i = 0; i < ap_threads_per_child; i++)
else if (is_graceful) {
else if (remaining_children_to_start) {
ap_server_conf = s;
if (!is_graceful) {
set_signals();
if (!is_graceful) {
if (shutdown_pending) {
if (one_process) {
/* wake up the children...time to die. But we'll have more soon */
if (is_graceful) {
static int restart_num = 0;
int no_detach = 0;
is_graceful = 0;
ap_extended_status = 0;
one_process = 0;
const char *arg)
return err;
return NULL;
const char *arg)
return err;
if (min_spare_threads <= 0) {
return NULL;
const char *arg)
return err;
return NULL;
const char *arg)
return err;
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "WARNING: Require MaxClients > 0, setting to 1");
return NULL;
const char *arg)
return err;
return NULL;
{ NULL }