worker.c revision c4fbc4018fd2b6716673a38ee27eeb36cba41c5d
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz/* ====================================================================
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * The Apache Software License, Version 1.1
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * reserved.
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * Redistribution and use in source and binary forms, with or without
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * modification, are permitted provided that the following conditions
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * are met:
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * 1. Redistributions of source code must retain the above copyright
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * notice, this list of conditions and the following disclaimer.
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * 2. Redistributions in binary form must reproduce the above copyright
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * notice, this list of conditions and the following disclaimer in
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * the documentation and/or other materials provided with the
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * distribution.
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * 3. The end-user documentation included with the redistribution,
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd * if any, must include the following acknowledgment:
7db9f691a00ead175b03335457ca296a33ddf31bnd * "This product includes software developed by the
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * Apache Software Foundation (http://www.apache.org/)."
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * Alternately, this acknowledgment may appear in the software itself,
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * if and wherever such third-party acknowledgments normally appear.
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * 4. The names "Apache" and "Apache Software Foundation" must
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * not be used to endorse or promote products derived from this
169280c7e65362d4ed444ec262c3f22a6a280166nd * software without prior written permission. For written
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * permission, please contact apache@apache.org.
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * 5. Products derived from this software may not be called "Apache",
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * nor may "Apache" appear in their name, without prior written
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * permission of the Apache Software Foundation.
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * SUCH DAMAGE.
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * ====================================================================
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * This software consists of voluntary contributions made by many
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * individuals on behalf of the Apache Software Foundation. For more
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * information on the Apache Software Foundation, please see
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * Portions of this software are based upon public domain software
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * originally written at the National Center for Supercomputing Applications,
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * University of Illinois, Urbana-Champaign.
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz/* The purpose of this MPM is to fix the design flaws in the threaded
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * model. Because of the way that pthreads and mutex locks interact,
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * it is basically impossible to cleanly gracefully shutdown a child
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * process if multiple threads are all blocked in accept. This model
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * fixes those problems.
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz#error The Worker MPM requires APR threads, but they are unavailable.
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * Actual definitions of config globals
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantzint ap_threads_per_child=0; /* Worker threads per child */
35ffb30f57f777dbf3f17c5a5ddf706559942c16ndstatic int dying = 0;
35ffb30f57f777dbf3f17c5a5ddf706559942c16ndstatic int workers_may_exit = 0;
35ffb30f57f777dbf3f17c5a5ddf706559942c16ndstatic int num_listensocks = 0;
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz/* The structure used to pass unique initialization info to each thread */
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantztypedef struct {
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd/* Structure used to pass information to the thread responsible for
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * creating the rest of the threads.
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantztypedef struct {
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * The max child slot ever assigned, preserved across restarts. Necessary
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * use this value to optimize routines that have to scan the entire
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd * scoreboard.
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd/* insures that a child process only consumes one character */
35ffb30f57f777dbf3f17c5a5ddf706559942c16nd/* *Non*-shared http_main globals... */
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz/* The worker MPM respects a couple of runtime flags that can aid
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * in debugging. Setting the -DNO_DETACH flag will prevent the root process
f43ed9051a7f4db461d67ed4f7ece175b3dbca7cjerenkrantz * from detaching from its controlling terminal. Additionally, setting
static int one_process = 0;
#ifdef DEBUG_SIGSTOP
int raise_sigstop_flags;
static int worker_thread_count;
static const char *lock_fname;
#ifdef NO_SERIALIZED_ACCEPT
static void signal_workers(void)
switch(query_code){
case AP_MPMQ_MAX_DAEMON_USED:
return APR_SUCCESS;
case AP_MPMQ_IS_THREADED:
return APR_SUCCESS;
case AP_MPMQ_IS_FORKED:
return APR_SUCCESS;
return APR_SUCCESS;
return APR_SUCCESS;
case AP_MPMQ_MAX_THREADS:
return APR_SUCCESS;
*result = 0;
return APR_SUCCESS;
return APR_SUCCESS;
*result = 0;
return APR_SUCCESS;
return APR_SUCCESS;
return APR_SUCCESS;
case AP_MPMQ_MAX_DAEMONS:
return APR_SUCCESS;
return APR_ENOTIMPL;
if (pchild) {
0, ap_server_conf,
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
ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(" AP_SIG_GRACEFUL_STRING ")");
if (!one_process) {
#ifdef SIGBUS
#ifdef SIGABORT
#ifdef SIGABRT
#ifdef SIGILL
#ifdef SIGXCPU
#ifdef SIGXFSZ
#ifdef SIGHUP
#ifdef AP_SIG_GRACEFUL
#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) {
if (rv) {
rv);
!= APR_SUCCESS) {
return NULL;
while (!workers_may_exit) {
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 (!any_dying_threads) {
last_non_dead = i;
if (free_length == 0) {
static int reported = 0;
if (!reported) {
"or Min/MaxSpareThreads), "
for (i = 0; i < free_length; ++i) {
int child_slot;
int status;
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;
if (!max_clients) {
else if (!max_clients
if (debug)
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)
int max_clients;
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 }