perchild.c revision ed4f9ec869d7fc5bd220c8d70ee58eed9b32d60a
3802a3d3d7af51ddff31943d5514382f01265770Lennart Poettering/* ====================================================================
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * The Apache Software License, Version 1.1
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * Redistribution and use in source and binary forms, with or without
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek * modification, are permitted provided that the following conditions
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek * 1. Redistributions of source code must retain the above copyright
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek * notice, this list of conditions and the following disclaimer.
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * 2. Redistributions in binary form must reproduce the above copyright
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek * notice, this list of conditions and the following disclaimer in
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek * the documentation and/or other materials provided with the
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * 3. The end-user documentation included with the redistribution,
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek * if any, must include the following acknowledgment:
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek * "This product includes software developed by the
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * Apache Software Foundation (http://www.apache.org/)."
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * Alternately, this acknowledgment may appear in the software itself,
48f69d8fb4d0cf34d578352572c96d86e13caa79Lennart Poettering * if and wherever such third-party acknowledgments normally appear.
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * 4. The names "Apache" and "Apache Software Foundation" must
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * not be used to endorse or promote products derived from this
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * software without prior written permission. For written
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * permission, please contact apache@apache.org.
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * 5. Products derived from this software may not be called "Apache",
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * nor may "Apache" appear in their name, without prior written
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * permission of the Apache Software Foundation.
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * SUCH DAMAGE.
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmann * ====================================================================
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmann * This software consists of voluntary contributions made by many
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * individuals on behalf of the Apache Software Foundation. For more
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * information on the Apache Software Foundation, please see
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * Portions of this software are based upon public domain software
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * originally written at the National Center for Supercomputing Applications,
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * University of Illinois, Urbana-Champaign.
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering#error The perchild MPM requires APR threads, but they are unavailable.
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering#include "http_config.h" /* for read_config */
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering#include "http_core.h" /* for get_remote_host */
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering/* ### should be APR-ized */
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering#include <sys/processor.h> /* for bindprocessor() */
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * Actual definitions of config globals
a6278b88305b237b02eabff0d870b57fe851822dLennart Poetteringstatic int threads_to_start = 0; /* Worker threads per child */
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmanntypedef struct {
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmann const char *sockname; /* The base name for the socket */
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmann const char *fullsockname; /* socket base name + extension */
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmann/* Tables used to determine the user and group each child process should
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmann * run as. The hash table is used to correlate a server name with a child
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmannstatic child_info_t child_info_table[HARD_SERVER_LIMIT];
dfb815c36df6e5f2089672b1d986d38b44c7ad17David Herrmannstatic int thread_socket_table[HARD_THREAD_LIMIT];
a6278b88305b237b02eabff0d870b57fe851822dLennart Poetteringstruct ap_ctable ap_child_table[HARD_SERVER_LIMIT];
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * The max child slot ever assigned, preserved across restarts. Necessary
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * to deal with NumServers changes across SIGWINCH restarts. We use this
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * value to optimize routines that have to scan the entire child table.
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * XXX - It might not be worth keeping this code in. There aren't very
b8bde11658366290521e3d03316378b482600323Jan Engelhardt * many child processes in this MPM.
a6278b88305b237b02eabff0d870b57fe851822dLennart Poetteringint ap_threads_per_child = HARD_THREAD_LIMIT;
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmekchar ap_coredump_dir[MAX_STRING_LEN];
a6278b88305b237b02eabff0d870b57fe851822dLennart Poetteringmodule AP_MODULE_DECLARE_DATA mpm_perchild_module;
a6278b88305b237b02eabff0d870b57fe851822dLennart Poetteringstatic apr_file_t *pipe_of_death_in = NULL;
a6278b88305b237b02eabff0d870b57fe851822dLennart Poetteringstatic apr_file_t *pipe_of_death_out = NULL;
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering/* *Non*-shared http_main globals... */
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering/* one_process --- debugging mode variable; can be set from the command line
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * with the -X flag. If set, this gets you the child_main loop running
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * in the process which originally started up (no detach, no make_child),
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * which is a pretty nice debugging environment. (You'll get a SIGHUP
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * early in standalone_main; just continue through. This is the server
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * trying to kill off any child processes which it might have lying
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * around --- Apache doesn't keep track of their pids, it just sends
a6278b88305b237b02eabff0d870b57fe851822dLennart Poettering * SIGHUP to the process group, ignoring it in the root process.
5aded369782f28255bc6b494ca905d7acaea7a56Zbigniew Jędrzejewski-Szmek * Continue through and you'll be fine.).
#ifdef DEBUG_SIGSTOP
int raise_sigstop_flags;
static int child_num;
static int worker_thread_count;
static int idle_thread_count;
#ifdef NO_SERIALIZED_ACCEPT
static const char *lock_fname;
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) {
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;
if (current_conn) {
static void *worker_thread(void *);
static int start_thread(void)
int rc;
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
switch (signum) {
case SIGTERM:
case SIGINT:
pchild));
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()) {
int pid;
if (one_process) {
set_signals();
if (!pid) {
#ifdef HAVE_BINDPROCESSOR
clean_child_exit(0);
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;
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;
curr_child_num = 0;
for (i = 0; i < HARD_SERVER_LIMIT; i++) {
for (i = 0; i < HARD_THREAD_LIMIT; i++) {
int sfd;
char *foo;
int zero = 0;
apr_bucket *e;
const char *str;
return NULL;
for (i = 0; i < num_daemons; i++) {
f = f->next;
return OK;
return OK;
return OK;
static apr_status_t perchild_buffer(ap_filter_t *f, apr_bucket_brigade *b, ap_input_mode_t mode, apr_size_t *readbytes)
apr_bucket *e;
const char *str;
return rv;
APR_BRIGADE_FOREACH(e, b) {
if (e->length != 0) {
return APR_SUCCESS;
return OK;
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;
const char *arg)
const char *fname;
return err;
return NULL;
const char *g, const char *num)
if (i > num_daemons) {
return NULL;
const char *gid)
const char *errstr;
return errstr;
for (i = 0; i < num_daemons; i++) {
return NULL;
return err;
, NULL);
return NULL;
{ NULL }
perchild_server_conf *c =