mpmt_pthread.c revision 0375f54d32af9c27de0006956d9f85d01b020351
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/* ====================================================================
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * The Apache Software License, Version 1.1
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * Copyright (c) 2000 The Apache Software Foundation. All rights
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * Redistribution and use in source and binary forms, with or without
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * modification, are permitted provided that the following conditions
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * 1. Redistributions of source code must retain the above copyright
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * notice, this list of conditions and the following disclaimer.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * 2. Redistributions in binary form must reproduce the above copyright
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * notice, this list of conditions and the following disclaimer in
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * the documentation and/or other materials provided with the
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * distribution.
72204f862232e7e51b0207bba020c1d781fa7798Christian Maeder * 3. The end-user documentation included with the redistribution,
72204f862232e7e51b0207bba020c1d781fa7798Christian Maeder * if any, must include the following acknowledgment:
72204f862232e7e51b0207bba020c1d781fa7798Christian Maeder * "This product includes software developed by the
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * Apache Software Foundation (http://www.apache.org/)."
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * Alternately, this acknowledgment may appear in the software itself,
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * if and wherever such third-party acknowledgments normally appear.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * 4. The names "Apache" and "Apache Software Foundation" must
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * not be used to endorse or promote products derived from this
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * software without prior written permission. For written
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * permission, please contact apache@apache.org.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * 5. Products derived from this software may not be called "Apache",
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * nor may "Apache" appear in their name, without prior written
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * permission of the Apache Software Foundation.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * SUCH DAMAGE.
96a6c2a58e8c534f983002f8d503f97ea1b4aa8dAlexis Tsogias * ====================================================================
96a6c2a58e8c534f983002f8d503f97ea1b4aa8dAlexis Tsogias * This software consists of voluntary contributions made by many
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * individuals on behalf of the Apache Software Foundation. For more
96a6c2a58e8c534f983002f8d503f97ea1b4aa8dAlexis Tsogias * information on the Apache Software Foundation, please see
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * Portions of this software are based upon public domain software
96a6c2a58e8c534f983002f8d503f97ea1b4aa8dAlexis Tsogias * originally written at the National Center for Supercomputing Applications,
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * University of Illinois, Urbana-Champaign.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#include "http_config.h" /* for read_config */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias#include "http_core.h" /* for get_remote_host */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * Actual definitions of config globals
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogiasint ap_threads_per_child=0; /* Worker threads per child */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/* The structure used to pass unique initialization info to each thread */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogiastypedef struct {
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias apr_pool_t *tpool; /* "pthread" would be confusing */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * The max child slot ever assigned, preserved across restarts. Necessary
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * to deal with MaxClients changes across SIGWINCH restarts. We use this
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * value to optimize routines that have to scan the entire scoreboard.
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/* *Non*-shared http_main globals... */
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias/* one_process --- debugging mode variable; can be set from the command line
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * with the -X flag. If set, this gets you the child_main loop running
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * in the process which originally started up (no detach, no make_child),
96a6c2a58e8c534f983002f8d503f97ea1b4aa8dAlexis Tsogias * which is a pretty nice debugging environment. (You'll get a SIGHUP
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * early in standalone_main; just continue through. This is the server
658187feb755694eb5ff29561bda7109c22c743cAlexis Tsogias * trying to kill off any child processes which it might have lying
96a6c2a58e8c534f983002f8d503f97ea1b4aa8dAlexis Tsogias * around --- Apache doesn't keep track of their pids, it just sends
96a6c2a58e8c534f983002f8d503f97ea1b4aa8dAlexis Tsogias * SIGHUP to the process group, ignoring it in the root process.
96a6c2a58e8c534f983002f8d503f97ea1b4aa8dAlexis Tsogias * Continue through and you'll be fine.).
72204f862232e7e51b0207bba020c1d781fa7798Christian Maederstatic apr_pool_t *pconf; /* Pool for config stuff */
72204f862232e7e51b0207bba020c1d781fa7798Christian Maederstatic apr_pool_t *pchild; /* Pool for httpd child stuff */
static int worker_thread_count;
static const char *lock_fname;
#ifdef NO_SERIALIZED_ACCEPT
return ap_max_daemons_limit;
if (pchild) {
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) {
#ifndef WIN32
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 is_graceful;
int csd;
static void check_pipe_of_death(void)
if (!workers_may_exit) {
char pipe_read_char;
for(n=0 ; n <= num_listensocks ; ++n)
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 (worker_thread_count == 0) {
return NULL;
int signal_received;
pchild));
if (unixd_setup_child()) {
#ifdef SIGPROCMASK_SETS_THREAD_MASK
#ifdef PTHREAD_SETS_ERRNO
worker_thread_count = 0;
for (i=0; i < ap_threads_per_child; i++) {
#ifndef NO_THREADS
#ifdef PTHREAD_SETS_ERRNO
switch (signal_received) {
case SIGTERM:
case SIGINT:
int pid;
if (one_process) {
set_signals();
if (!pid) {
#ifdef AIX_BIND_PROCESSOR
#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 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 idle_thread_addition = 0;
for (j = 0; j < ap_threads_per_child; j++) {
++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;
ap_server_conf = s;
if (!is_graceful) {
set_signals();
if (!is_graceful) {
if (shutdown_pending) {
if (one_process) {
if (is_graceful) {
for (i = 0; i < ap_daemons_limit;) {
for (i = 0; i < ap_daemons_limit; ++i) {
for (j = 0; j < ap_threads_per_child; j++) {
if (!is_graceful) {
static int restart_num = 0;
int no_detach = 0;
is_graceful = 0;
apr_detach();
ap_extended_status = 0;
one_process = 0;
return err;
return NULL;
const char *arg)
return err;
return NULL;
return err;
return NULL;
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;
const char *arg)
return err;
return NULL;
const char *arg)
const char *fname;
return err;
return NULL;
{ NULL }