worker.c revision 0aa8e8fd5a242948518655f2296b3c76386754e5
97a9a944b5887e91042b019776c41d5dd74557aferikabele/* ====================================================================
97a9a944b5887e91042b019776c41d5dd74557aferikabele * The Apache Software License, Version 1.1
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * reserved.
b686b6a420bde7f78c416b90be11db94cb789979nd * Redistribution and use in source and binary forms, with or without
b686b6a420bde7f78c416b90be11db94cb789979nd * modification, are permitted provided that the following conditions
b686b6a420bde7f78c416b90be11db94cb789979nd * are met:
b686b6a420bde7f78c416b90be11db94cb789979nd * 1. Redistributions of source code must retain the above copyright
b686b6a420bde7f78c416b90be11db94cb789979nd * notice, this list of conditions and the following disclaimer.
b686b6a420bde7f78c416b90be11db94cb789979nd * 2. Redistributions in binary form must reproduce the above copyright
b686b6a420bde7f78c416b90be11db94cb789979nd * notice, this list of conditions and the following disclaimer in
b686b6a420bde7f78c416b90be11db94cb789979nd * the documentation and/or other materials provided with the
b686b6a420bde7f78c416b90be11db94cb789979nd * distribution.
b686b6a420bde7f78c416b90be11db94cb789979nd * 3. The end-user documentation included with the redistribution,
b686b6a420bde7f78c416b90be11db94cb789979nd * if any, must include the following acknowledgment:
b686b6a420bde7f78c416b90be11db94cb789979nd * "This product includes software developed by the
b686b6a420bde7f78c416b90be11db94cb789979nd * Apache Software Foundation (http://www.apache.org/)."
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd * Alternately, this acknowledgment may appear in the software itself,
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd * if and wherever such third-party acknowledgments normally appear.
b686b6a420bde7f78c416b90be11db94cb789979nd * 4. The names "Apache" and "Apache Software Foundation" must
b686b6a420bde7f78c416b90be11db94cb789979nd * not be used to endorse or promote products derived from this
b686b6a420bde7f78c416b90be11db94cb789979nd * software without prior written permission. For written
b686b6a420bde7f78c416b90be11db94cb789979nd * permission, please contact apache@apache.org.
b686b6a420bde7f78c416b90be11db94cb789979nd * 5. Products derived from this software may not be called "Apache",
b686b6a420bde7f78c416b90be11db94cb789979nd * nor may "Apache" appear in their name, without prior written
06ba4a61654b3763ad65f52283832ebf058fdf1cslive * permission of the Apache Software Foundation.
06ba4a61654b3763ad65f52283832ebf058fdf1cslive * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
06ba4a61654b3763ad65f52283832ebf058fdf1cslive * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
06ba4a61654b3763ad65f52283832ebf058fdf1cslive * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b686b6a420bde7f78c416b90be11db94cb789979nd * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
b686b6a420bde7f78c416b90be11db94cb789979nd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b686b6a420bde7f78c416b90be11db94cb789979nd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b686b6a420bde7f78c416b90be11db94cb789979nd * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
117c1f888a14e73cdd821dc6c23eb0411144a41cnd * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
117c1f888a14e73cdd821dc6c23eb0411144a41cnd * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
b686b6a420bde7f78c416b90be11db94cb789979nd * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b686b6a420bde7f78c416b90be11db94cb789979nd * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b686b6a420bde7f78c416b90be11db94cb789979nd * SUCH DAMAGE.
b686b6a420bde7f78c416b90be11db94cb789979nd * ====================================================================
b686b6a420bde7f78c416b90be11db94cb789979nd * This software consists of voluntary contributions made by many
b686b6a420bde7f78c416b90be11db94cb789979nd * individuals on behalf of the Apache Software Foundation. For more
b686b6a420bde7f78c416b90be11db94cb789979nd * information on the Apache Software Foundation, please see
b686b6a420bde7f78c416b90be11db94cb789979nd * Portions of this software are based upon public domain software
b686b6a420bde7f78c416b90be11db94cb789979nd * originally written at the National Center for Supercomputing Applications,
b686b6a420bde7f78c416b90be11db94cb789979nd * University of Illinois, Urbana-Champaign.
b686b6a420bde7f78c416b90be11db94cb789979nd/* The purpose of this MPM is to fix the design flaws in the threaded
b686b6a420bde7f78c416b90be11db94cb789979nd * model. Because of the way that pthreads and mutex locks interact,
06ba4a61654b3763ad65f52283832ebf058fdf1cslive * it is basically impossible to cleanly gracefully shutdown a child
06ba4a61654b3763ad65f52283832ebf058fdf1cslive * process if multiple threads are all blocked in accept. This model
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd * fixes those problems.
b686b6a420bde7f78c416b90be11db94cb789979nd#error The Worker MPM requires APR threads, but they are unavailable.
b686b6a420bde7f78c416b90be11db94cb789979nd * Actual definitions of config globals
static int ap_daemons_to_start=0;
static int min_spare_threads=0;
static int max_spare_threads=0;
static int ap_daemons_limit=0;
static int dying = 0;
static int workers_may_exit = 0;
static int requests_this_child;
static int num_listensocks = 0;
int pid;
int tid;
int sd;
} proc_info;
int child_num_arg;
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) {
!= 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;
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 (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)
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 }