perchild.c revision 10a4cdd68ef1ca0e54af296fe1d08ac00150c90b
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/* ====================================================================
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * The Apache Software License, Version 1.1
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * Copyright (c) 2000 The Apache Software Foundation. All rights
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * Redistribution and use in source and binary forms, with or without
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * modification, are permitted provided that the following conditions
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * 1. Redistributions of source code must retain the above copyright
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * notice, this list of conditions and the following disclaimer.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * 2. Redistributions in binary form must reproduce the above copyright
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * notice, this list of conditions and the following disclaimer in
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * the documentation and/or other materials provided with the
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * distribution.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * 3. The end-user documentation included with the redistribution,
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * if any, must include the following acknowledgment:
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * "This product includes software developed by the
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * Apache Software Foundation (http://www.apache.org/)."
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * Alternately, this acknowledgment may appear in the software itself,
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * if and wherever such third-party acknowledgments normally appear.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * 4. The names "Apache" and "Apache Software Foundation" must
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * not be used to endorse or promote products derived from this
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * software without prior written permission. For written
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * permission, please contact apache@apache.org.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * 5. Products derived from this software may not be called "Apache",
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * nor may "Apache" appear in their name, without prior written
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * permission of the Apache Software Foundation.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * SUCH DAMAGE.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * ====================================================================
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * This software consists of voluntary contributions made by many
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * individuals on behalf of the Apache Software Foundation. For more
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * information on the Apache Software Foundation, please see
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * Portions of this software are based upon public domain software
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * originally written at the National Center for Supercomputing Applications,
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * University of Illinois, Urbana-Champaign.
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering#include "http_config.h" /* for read_config */
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering#include "http_core.h" /* for get_remote_host */
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering * Actual definitions of config globals
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poetteringstatic int threads_to_start = 0; /* Worker threads per child */
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart PoetteringAPI_VAR_EXPORT const char *ap_scoreboard_fname=NULL;
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringtypedef struct {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering const char *sockname; /* The base name for the socket */
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering const char *fullsockname; /* socket base name + extension */
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringtypedef struct socket_info_t socket_info_t;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/* Tables used to determine the user and group each child process should
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * run as. The hash table is used to correlate a server name with a child
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringstatic child_info_t child_info_table[HARD_SERVER_LIMIT];
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringstatic int thread_socket_table[HARD_THREAD_LIMIT];
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringstruct ap_ctable ap_child_table[HARD_SERVER_LIMIT];
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek * The max child slot ever assigned, preserved across restarts. Necessary
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek * to deal with NumServers changes across SIGWINCH restarts. We use this
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek * value to optimize routines that have to scan the entire child table.
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * XXX - It might not be worth keeping this code in. There aren't very
7b77ed8cf36e8eca6017791626044b61ae2d68e7Lennart Poettering * many child processes in this MPM.
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poetteringmodule MODULE_VAR_EXPORT mpm_perchild_module;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringstatic apr_file_t *pipe_of_death_in = NULL;
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmekstatic apr_file_t *pipe_of_death_out = NULL;
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmekstatic pthread_mutex_t pipe_of_death_mutex;
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek/* *Non*-shared http_main globals... */
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/* one_process --- debugging mode variable; can be set from the command line
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering * with the -X flag. If set, this gets you the child_main loop running
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek * in the process which originally started up (no detach, no make_child),
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek * which is a pretty nice debugging environment. (You'll get a SIGHUP
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek * early in standalone_main; just continue through. This is the server
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek * trying to kill off any child processes which it might have lying
7b77ed8cf36e8eca6017791626044b61ae2d68e7Lennart Poettering * around --- Apache doesn't keep track of their pids, it just sends
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * SIGHUP to the process group, ignoring it in the root process.
7b77ed8cf36e8eca6017791626044b61ae2d68e7Lennart Poettering * Continue through and you'll be fine.).
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmekstatic apr_pool_t *pconf; /* Pool for config stuff */
65b5116220a8ebf8a260716152409aa05377aaccLennart Poetteringstatic apr_pool_t *pchild; /* Pool for httpd child stuff */
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poetteringstatic apr_pool_t *thread_pool_parent; /* Parent of per-thread pools */
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poetteringstatic pthread_mutex_t thread_pool_parent_mutex;
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poetteringstatic unsigned int my_pid; /* Linux getpid() doesn't work except in
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering main thread. Use this instead */
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering/* Keep track of the number of worker threads currently active */
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poetteringstatic pthread_mutex_t worker_thread_count_mutex;
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poetteringstatic int worker_thread_free_ids[HARD_THREAD_LIMIT];
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering/* Keep track of the number of idle worker threads */
65b5116220a8ebf8a260716152409aa05377aaccLennart Poetteringstatic pthread_mutex_t idle_thread_count_mutex;
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek/* Locks for accept serialization */
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek#define SAFE_ACCEPT(stmt) APR_SUCCESS
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering#endif /* NO_SERIALIZED_ACCEPT */
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poetteringstatic const char *lock_fname;
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poetteringstatic pthread_mutex_t thread_accept_mutex = PTHREAD_MUTEX_INITIALIZER;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/* a clean exit from a child with proper cleanup */
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering/* handle all varieties of core dumping signals */
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering /* At this point we've got sig blocked, because we're still inside
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * the signal handler. When we leave the signal handler it will
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * be unblocked, and we'll take the signal... and coredump or whatever
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * is appropriate for this particular Unix. In addition the parent
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * will see the real signal we received -- whereas if we called
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * abort() here, the parent would only see SIGABRT.
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering/*****************************************************************
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * Connection structures and accounting...
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering/* volatile just in case */
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic int volatile shutdown_pending;
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic int volatile restart_pending;
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic int volatile is_graceful;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * functions to initiate shutdown or restart without relying on signals.
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * Previously this was initiated in sig_term() and restart() signal handlers,
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * but we want to be able to start a shutdown/restart from other sources --
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * e.g. on Win32, from the service manager. Now the service manager can
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * these functions can also be called by the child processes, since global
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering * variables are no longer used to pass on the required action to the parent.
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * These should only be called from the parent process itself, since the
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering * parent process will use the shutdown_pending and restart_pending variables
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering * to determine whether to shutdown or restart. The child process should
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * call signal_parent() directly to tell the parent to die -- this will
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * cause neither of those variable to be set, which the parent will
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * assume means something serious is wrong (which it will be, for the
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * child to force an exit) and so do an exit anyway.
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering /* Um, is this _probably_ not an error, if the user has
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * tried to do a shutdown twice quickly, so we won't
ed4ba7e4f652150310d062ffbdfefb4521ce1054Lennart Poettering * worry about reporting it.
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;
conn_id);
static void *worker_thread(void *);
static int start_thread(void)
int rc;
#ifdef PTHREAD_SETS_ERRNO
static int reported = 0;
if (!reported) {
static void check_pipe_of_death(void)
if (!workers_may_exit) {
int ret;
char pipe_read_char;
int srv;
for(n=0 ; n <= num_listenfds ; ++n) {
while (!workers_may_exit) {
if (workers_may_exit) break;
if (!thread_just_started) {
thread_just_started = 0;
if (workers_may_exit) {
!= APR_SUCCESS) {
while (!workers_may_exit) {
if (workers_may_exit) break;
goto got_from_other_child;
goto got_fd;
curr_pollfd++;
goto got_fd;
if (!workers_may_exit) {
!= APR_SUCCESS) {
if (!start_thread()) {
!= APR_SUCCESS) {
if (worker_thread_count == 0) {
return NULL;
if (!geteuid()) {
const char *name;
(unsigned)uid);
(unsigned)gid);
return unixd_setup_child();
if (!geteuid() && (
#ifdef _OSD_POSIX
int signal_received;
pchild));
#ifdef SIGPROCMASK_SETS_THREAD_MASK
#ifdef PTHREAD_SETS_ERRNO
worker_thread_count = 0;
for (i = 0; i < max_threads; i++) {
worker_thread_free_ids[i] = i;
for (i=0; i < threads_to_start; i++) {
if (!start_thread()) {
switch (signal_received) {
case SIGTERM:
case SIGINT:
int pid;
if (one_process) {
set_signals();
if (!pid) {
#ifdef AIX_BIND_PROCESSOR
return number_to_start;
#ifndef MAX_SPAWN_RATE
static int hold_off_on_exponential_spawning;
static void perform_child_maintenance(void)
int free_length;
free_length = 0;
for (i = 0; i < num_daemons; ++i) {
++free_length;
last_non_dead = i;
if (free_length > 0) {
for (i = 0; i < free_length; ++i) {
int child_slot;
for (i = 0; i < ap_max_daemons_limit; ++i) {
child_slot = i;
for (j = 0; j < HARD_THREAD_LIMIT; j++) {
if (child_slot >= 0) {
else if (is_graceful) {
else if (remaining_children_to_start) {
ap_server_conf = s;
!= APR_SUCCESS) {
ap_server_conf = s;
my_pid);
if (!is_graceful) {
if (!is_graceful) {
for (i = 0; i < HARD_SERVER_LIMIT; i++) {
set_signals();
if (!is_graceful) {
if (shutdown_pending) {
if (one_process) {
if (is_graceful) {
for (i = 0; i < num_daemons; ++i) {
for (i = 0; i < num_daemons;) {
static int restart_num = 0;
int no_detach = 0;
is_graceful = 0;
apr_detach();
curr_child_num = 0;
socket_num = 0;
for (i = 0; i < HARD_SERVER_LIMIT; i++) {
for (i = 0; i < HARD_THREAD_LIMIT; i++) {
int sfd;
return NULL;
for (i = 0; i < num_daemons; i++) {
return OK;
return OK;
return OK;
static void perchild_hooks(void)
one_process = 0;
return err;
return NULL;
return err;
return NULL;
return err;
return NULL;
return err;
return NULL;
return err;
return NULL;
return err;
if (min_spare_threads <= 0) {
return NULL;
return err;
return NULL;
return err;
return NULL;
return err;
return NULL;
return err;
return NULL;
const char *fname;
return err;
return NULL;
const char *g, const char *num)
if (i > num_daemons) {
socket_num++;
return NULL;
const char *gid)
const char *errstr;
return errstr;
for (i = 0; i < num_daemons; i++) {
return NULL;
{ NULL }
perchild_server_conf *c =