prefork.c revision 6e0c23c94478376306d87615a0641bcc5381c411
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein/* ====================================================================
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * The Apache Software License, Version 1.1
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
bc8fd1b0b1afdf89b8d28eefa8cd74e26ba97986fielding * Copyright (c) 2000 The Apache Software Foundation. All rights
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * reserved.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * Redistribution and use in source and binary forms, with or without
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * modification, are permitted provided that the following conditions
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * are met:
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 1. Redistributions of source code must retain the above copyright
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * notice, this list of conditions and the following disclaimer.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 2. Redistributions in binary form must reproduce the above copyright
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * notice, this list of conditions and the following disclaimer in
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * the documentation and/or other materials provided with the
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * distribution.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 3. The end-user documentation included with the redistribution,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * if any, must include the following acknowledgment:
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * "This product includes software developed by the
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * Apache Software Foundation (http://www.apache.org/)."
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * Alternately, this acknowledgment may appear in the software itself,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * if and wherever such third-party acknowledgments normally appear.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 4. The names "Apache" and "Apache Software Foundation" must
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * not be used to endorse or promote products derived from this
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * software without prior written permission. For written
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * permission, please contact apache@apache.org.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 5. Products derived from this software may not be called "Apache",
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * nor may "Apache" appear in their name, without prior written
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * permission of the Apache Software Foundation.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * SUCH DAMAGE.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * ====================================================================
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * This software consists of voluntary contributions made by many
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * individuals on behalf of the Apache Software Foundation. For more
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * information on the Apache Software Foundation, please see
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * <http://www.apache.org/>.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Portions of this software are based upon public domain software
f4c310fd2555c6faca1f980f00b161eadb089023gstein * originally written at the National Center for Supercomputing Applications,
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * University of Illinois, Urbana-Champaign.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * httpd.c: simple http daemon for answering WWW file requests
952023a04a2f9d51553babd9094fb857f1c97548trawick *
1b21d7b3d97def358b2e923655edeb16613a1c31gstein *
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * 03-21-93 Rob McCool wrote original code (up to NCSA HTTPd 1.3)
1b21d7b3d97def358b2e923655edeb16613a1c31gstein *
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * 03-06-95 blong
f4c310fd2555c6faca1f980f00b161eadb089023gstein * changed server number for child-alone processes to 0 and changed name
f4c310fd2555c6faca1f980f00b161eadb089023gstein * of processes
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * 03-10-95 blong
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Added numerous speed hacks proposed by Robert S. Thau (rst@ai.mit.edu)
f4c310fd2555c6faca1f980f00b161eadb089023gstein * including set group before fork, and call gettime before to fork
f4c310fd2555c6faca1f980f00b161eadb089023gstein * to set up libraries.
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe * 04-14-95 rst / rh
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe * Brandon's code snarfed from NCSA 1.4, but tinkered to work with the
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Apache server, and also to have child processes do accept() directly.
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * April-July '95 rst
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Extensive rework for Apache.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* TODO: this is a cobbled together prefork MPM example... it should mostly
f4c310fd2555c6faca1f980f00b161eadb089023gstein * TODO: behave like apache-1.3... here's a short list of things I think
f4c310fd2555c6faca1f980f00b161eadb089023gstein * TODO: need cleaning up still:
f4c310fd2555c6faca1f980f00b161eadb089023gstein * TODO: - clean up scoreboard stuff when we figure out how to do it in 2.0
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#define CORE_PRIVATE
f4c310fd2555c6faca1f980f00b161eadb089023gstein
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "ap_config.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "apr_portable.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "apr_thread_proc.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "httpd.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "mpm_default.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "mpm_status.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "http_main.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "http_log.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "http_config.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "http_core.h" /* for get_remote_host */
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "http_connection.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "scoreboard.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "ap_mpm.h"
5b03ba47ff7225cacb131f14b019332af27da960gstein#include "unixd.h"
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "mpm_common.h"
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "iol_socket.h"
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include "ap_listen.h"
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include "ap_mmn.h"
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#ifdef HAVE_SYS_TYPES_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <sys/types.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_SYS_WAIT_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <sys/wait.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_SYS_SOCKET_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <sys/socket.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_UNISTD_H
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#include <unistd.h>
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_NETINET_TCP_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <netinet/tcp.h> /* for TCP_NODELAY */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_BSTRING_H
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <bstring.h> /* for IRIX, FD_SET calls bzero() */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <signal.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <sys/times.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* config globals */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowestatic int ap_max_requests_per_child=0;
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowestatic const char *ap_pid_fname=NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic ap_lock_t *accept_lock;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *ap_scoreboard_fname=NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *ap_lock_fname;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int ap_daemons_to_start=0;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int ap_daemons_min_free=0;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int ap_daemons_max_free=0;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int ap_daemons_limit=0;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic time_t ap_restart_time=0;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int ap_extended_status = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int maintain_connection_status = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * The max child slot ever assigned, preserved across restarts. Necessary
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * to deal with MaxClients changes across SIGUSR1 restarts. We use this
f4c310fd2555c6faca1f980f00b161eadb089023gstein * value to optimize routines that have to scan the entire scoreboard.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinint ap_max_daemons_limit = -1;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinserver_rec *ap_server_conf;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic char ap_coredump_dir[MAX_STRING_LEN];
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe/* *Non*-shared http_main globals... */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic ap_socket_t *sd;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic fd_set listenfds;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int listenmaxfd;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* one_process --- debugging mode variable; can be set from the command line
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe * with the -X flag. If set, this gets you the child_main loop running
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe * in the process which originally started up (no detach, no make_child),
f4c310fd2555c6faca1f980f00b161eadb089023gstein * which is a pretty nice debugging environment. (You'll get a SIGHUP
f4c310fd2555c6faca1f980f00b161eadb089023gstein * early in standalone_main; just continue through. This is the server
f4c310fd2555c6faca1f980f00b161eadb089023gstein * trying to kill off any child processes which it might have lying
f4c310fd2555c6faca1f980f00b161eadb089023gstein * around --- Apache doesn't keep track of their pids, it just sends
f4c310fd2555c6faca1f980f00b161eadb089023gstein * SIGHUP to the process group, ignoring it in the root process.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Continue through and you'll be fine.).
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int one_process = 0;
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowestatic ap_pool_t *pconf; /* Pool for config stuff */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic ap_pool_t *pchild; /* Pool for httpd child stuff */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinint ap_my_pid; /* it seems silly to call getpid all the time */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifndef MULTITHREAD
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int my_child_num;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef TPF
98e9c4a310bb623ff788680f88b6bd200ff36a24wroweint tpf_child = 0;
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowechar tpf_server_name[INETD_SERVNAME_LENGTH+1];
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#endif /* TPF */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAPI_VAR_EXPORT scoreboard *ap_scoreboard_image = NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic new_scoreboard *ap_new_scoreboard_image = NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef GPROF
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * change directory for gprof to plop the gmon.out file
f4c310fd2555c6faca1f980f00b161eadb089023gstein * configure in httpd.conf:
f4c310fd2555c6faca1f980f00b161eadb089023gstein * GprofDir logs/ -> $ServerRoot/logs/gmon.out
f4c310fd2555c6faca1f980f00b161eadb089023gstein * GprofDir logs/% -> $ServerRoot/logs/gprof.$pid/gmon.out
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void chdir_for_gprof(void)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein core_server_config *sconf =
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_get_module_config(ap_server_conf->module_config, &core_module);
48f35e10f195dd594d75738fc536bb885eda537cgstein char *dir = sconf->gprof_dir;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if(dir) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein char buf[512];
91644a5f4d3e992dc208304b50e80bbb236fca89trawick int len = strlen(sconf->gprof_dir) - 1;
8b99f2a316c5e2fa6ab208206fdd7fc2bfc4a921dougm if(*(dir + len) == '%') {
f4c310fd2555c6faca1f980f00b161eadb089023gstein dir[len] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_snprintf(buf, sizeof(buf), "%sgprof.%d", dir, (int)getpid());
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein dir = ap_server_root_relative(pconf, buf[0] ? buf : dir);
8b99f2a316c5e2fa6ab208206fdd7fc2bfc4a921dougm if(mkdir(dir, 0755) < 0 && errno != EEXIST) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "gprof: error creating directory %s", dir);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein dir = ap_server_root_relative(pconf, "logs");
48f35e10f195dd594d75738fc536bb885eda537cgstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein chdir(dir);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein#define chdir_for_gprof()
48f35e10f195dd594d75738fc536bb885eda537cgstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* XXX - I don't know if TPF will ever use this module or not, so leave
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the ap_check_signals calls in but disable them - manoj */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#define ap_check_signals()
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* a clean exit from a child with proper cleanup */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void clean_child_exit(int code) __attribute__ ((noreturn));
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void clean_child_exit(int code)
48f35e10f195dd594d75738fc536bb885eda537cgstein{
48f35e10f195dd594d75738fc536bb885eda537cgstein if (pchild) {
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_destroy_pool(pchild);
48f35e10f195dd594d75738fc536bb885eda537cgstein }
48f35e10f195dd594d75738fc536bb885eda537cgstein chdir_for_gprof();
48f35e10f195dd594d75738fc536bb885eda537cgstein exit(code);
48f35e10f195dd594d75738fc536bb885eda537cgstein}
48f35e10f195dd594d75738fc536bb885eda537cgstein
48f35e10f195dd594d75738fc536bb885eda537cgsteinstatic void expand_lock_fname(ap_pool_t *p)
48f35e10f195dd594d75738fc536bb885eda537cgstein{
48f35e10f195dd594d75738fc536bb885eda537cgstein /* XXXX possibly bogus cast */
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_lock_fname = ap_psprintf(p, "%s.%lu",
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_server_root_relative(p, ap_lock_fname), (unsigned long)getpid());
48f35e10f195dd594d75738fc536bb885eda537cgstein}
48f35e10f195dd594d75738fc536bb885eda537cgstein
48f35e10f195dd594d75738fc536bb885eda537cgstein/* Initialize mutex lock.
48f35e10f195dd594d75738fc536bb885eda537cgstein * Done by each child at its birth
864c5615d55b8ebbde24e72043f6325741335a74fielding */
48f35e10f195dd594d75738fc536bb885eda537cgsteinstatic void accept_mutex_child_init(ap_pool_t *p)
48f35e10f195dd594d75738fc536bb885eda537cgstein{
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_child_init_lock(&accept_lock, ap_lock_fname, p);
48f35e10f195dd594d75738fc536bb885eda537cgstein}
48f35e10f195dd594d75738fc536bb885eda537cgstein
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gstein/* Initialize mutex lock.
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gstein * Must be safe to call this on a restart.
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gstein */
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gsteinstatic void accept_mutex_init(ap_pool_t *p)
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gstein{
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_status_t rv;
48f35e10f195dd594d75738fc536bb885eda537cgstein
48f35e10f195dd594d75738fc536bb885eda537cgstein expand_lock_fname(p);
48f35e10f195dd594d75738fc536bb885eda537cgstein rv = ap_create_lock(&accept_lock, APR_MUTEX, APR_CROSS_PROCESS, ap_lock_fname, p);
48f35e10f195dd594d75738fc536bb885eda537cgstein if (rv) {
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "couldn't create accept mutex");
48f35e10f195dd594d75738fc536bb885eda537cgstein }
48f35e10f195dd594d75738fc536bb885eda537cgstein}
48f35e10f195dd594d75738fc536bb885eda537cgstein
48f35e10f195dd594d75738fc536bb885eda537cgsteinstatic void accept_mutex_on(void)
48f35e10f195dd594d75738fc536bb885eda537cgstein{
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_status_t rv = ap_lock(accept_lock);
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_assert(!rv);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void accept_mutex_off(void)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_status_t rv = ap_unlock(accept_lock);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_assert(!rv);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* On some architectures it's safe to do unserialized accept()s in the single
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Listen case. But it's never safe to do it in the case where there's
f4c310fd2555c6faca1f980f00b161eadb089023gstein * multiple Listen statements. Define SINGLE_LISTEN_UNSERIALIZED_ACCEPT
f4c310fd2555c6faca1f980f00b161eadb089023gstein * when it's safe in the single Listen case.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
48f35e10f195dd594d75738fc536bb885eda537cgstein#define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)
48f35e10f195dd594d75738fc536bb885eda537cgstein#else
48f35e10f195dd594d75738fc536bb885eda537cgstein#define SAFE_ACCEPT(stmt) do {stmt;} while(0)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if APR_HAS_SHARED_MEMORY
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include "apr_shmem.h"
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic ap_shmem_t *scoreboard_shm = NULL;
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic ap_status_t cleanup_shared_mem(void *d)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_shm_free(scoreboard_shm, ap_scoreboard_image);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_image = NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_shm_destroy(scoreboard_shm);
f4c310fd2555c6faca1f980f00b161eadb089023gstein return APR_SUCCESS;
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void setup_shared_mem(ap_pool_t *p)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein char buf[512];
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *fname;
8b99f2a316c5e2fa6ab208206fdd7fc2bfc4a921dougm
f4c310fd2555c6faca1f980f00b161eadb089023gstein fname = ap_server_root_relative(p, ap_scoreboard_fname);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_shm_init(&scoreboard_shm, SCOREBOARD_SIZE + NEW_SCOREBOARD_SIZE + 40, fname, p) != APR_SUCCESS) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_snprintf(buf, sizeof(buf), "%s: could not open(create) scoreboard",
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_server_argv0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein perror(buf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein exit(APEXIT_INIT);
f958dac1550254a59b45f4655138bb34dad5e76egstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_image = ap_shm_malloc(scoreboard_shm, SCOREBOARD_SIZE);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_new_scoreboard_image = ap_shm_malloc(scoreboard_shm, NEW_SCOREBOARD_SIZE);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image == NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_server_argv0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein perror(buf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_shm_destroy(scoreboard_shm);
f4c310fd2555c6faca1f980f00b161eadb089023gstein exit(APEXIT_INIT);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_register_cleanup(p, NULL, cleanup_shared_mem, ap_null_cleanup);
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe ap_scoreboard_image->global.running_generation = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowestatic void reopen_scoreboard(ap_pool_t *p)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe/* Called by parent process */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void reinit_scoreboard(ap_pool_t *p)
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int running_gen = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image)
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe running_gen = ap_scoreboard_image->global.running_generation;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image == NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein setup_shared_mem(p);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f39230a531b23d94f86a087963299bbe2e431a4agstein memset(ap_scoreboard_image, 0, SCOREBOARD_SIZE);
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe ap_scoreboard_image->global.running_generation = running_gen;
f39230a531b23d94f86a087963299bbe2e431a4agstein}
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe/* Routines called to deal with the scoreboard image
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe * --- note that we do *not* need write locks, since update_child_status
f39230a531b23d94f86a087963299bbe2e431a4agstein * only updates a *single* record in place, and only one process writes to
f39230a531b23d94f86a087963299bbe2e431a4agstein * a given scoreboard slot at a time (either the child process owning that
f39230a531b23d94f86a087963299bbe2e431a4agstein * slot, or the parent, noting that the child has died).
1e2133fe37e6cbcd683233057ef62236bc8e5826trawick *
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein * As a final note --- setting the score entry to getpid() is always safe,
f39230a531b23d94f86a087963299bbe2e431a4agstein * since when the parent is writing an entry, it's only noting SERVER_DEAD
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein * anyway.
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein */
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gsteinap_inline void ap_sync_scoreboard_image(void)
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein{
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein#ifdef SCOREBOARD_FILE
f39230a531b23d94f86a087963299bbe2e431a4agstein lseek(scoreboard_fd, 0L, 0);
f39230a531b23d94f86a087963299bbe2e431a4agstein force_read(scoreboard_fd, ap_scoreboard_image, sizeof(*ap_scoreboard_image))
f39230a531b23d94f86a087963299bbe2e431a4agstein;
f39230a531b23d94f86a087963299bbe2e431a4agstein#endif
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein}
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gsteinAPI_EXPORT(int) ap_exists_scoreboard_image(void)
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein{
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein return (ap_scoreboard_image ? 1 : 0);
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein}
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gsteinAPI_EXPORT(int) ap_get_max_daemons(void)
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein{
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein return ap_max_daemons_limit;
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein}
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gsteinstatic ap_inline void put_scoreboard_info(int child_num,
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein short_score *new_score_rec)
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein{
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein#ifdef SCOREBOARD_FILE
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein lseek(scoreboard_fd, (long) child_num * sizeof(short_score), 0);
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein force_write(scoreboard_fd, new_score_rec, sizeof(short_score));
f39230a531b23d94f86a087963299bbe2e431a4agstein#endif
f39230a531b23d94f86a087963299bbe2e431a4agstein}
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agsteinint ap_update_child_status(int child_num, int status, request_rec *r)
f39230a531b23d94f86a087963299bbe2e431a4agstein{
f39230a531b23d94f86a087963299bbe2e431a4agstein int old_status;
f39230a531b23d94f86a087963299bbe2e431a4agstein short_score *ss;
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein if (child_num < 0)
f39230a531b23d94f86a087963299bbe2e431a4agstein return -1;
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein ap_check_signals();
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein ss = &ap_scoreboard_image->servers[child_num];
f39230a531b23d94f86a087963299bbe2e431a4agstein old_status = ss->status;
f39230a531b23d94f86a087963299bbe2e431a4agstein ss->status = status;
f39230a531b23d94f86a087963299bbe2e431a4agstein
f39230a531b23d94f86a087963299bbe2e431a4agstein if (ap_extended_status) {
f39230a531b23d94f86a087963299bbe2e431a4agstein if (status == SERVER_READY || status == SERVER_DEAD) {
f39230a531b23d94f86a087963299bbe2e431a4agstein /*
f39230a531b23d94f86a087963299bbe2e431a4agstein * Reset individual counters
f39230a531b23d94f86a087963299bbe2e431a4agstein */
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein if (status == SERVER_DEAD) {
f39230a531b23d94f86a087963299bbe2e431a4agstein ss->my_access_count = 0L;
f39230a531b23d94f86a087963299bbe2e431a4agstein ss->my_bytes_served = 0L;
f39230a531b23d94f86a087963299bbe2e431a4agstein }
f39230a531b23d94f86a087963299bbe2e431a4agstein ss->conn_count = (unsigned short) 0;
f39230a531b23d94f86a087963299bbe2e431a4agstein ss->conn_bytes = (unsigned long) 0;
f39230a531b23d94f86a087963299bbe2e431a4agstein }
f39230a531b23d94f86a087963299bbe2e431a4agstein if (r) {
f39230a531b23d94f86a087963299bbe2e431a4agstein conn_rec *c = r->connection;
f39230a531b23d94f86a087963299bbe2e431a4agstein ap_cpystrn(ss->client, ap_get_remote_host(c, r->per_dir_config,
f39230a531b23d94f86a087963299bbe2e431a4agstein REMOTE_NOLOOKUP), sizeof(ss->client));
f39230a531b23d94f86a087963299bbe2e431a4agstein if (r->the_request == NULL) {
f39230a531b23d94f86a087963299bbe2e431a4agstein ap_cpystrn(ss->request, "NULL", sizeof(ss->request));
f39230a531b23d94f86a087963299bbe2e431a4agstein } else if (r->parsed_uri.password == NULL) {
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ap_cpystrn(ss->request, r->the_request, sizeof(ss->request));
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein } else {
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein /* Don't reveal the password in the server-status view */
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ap_cpystrn(ss->request, ap_pstrcat(r->pool, r->method, " ",
6810bf1570bed88d16239b27ce47d48408bb2e51gstein ap_unparse_uri_components(r->pool, &r->parsed_uri, UNP_OMITPASSWORD),
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein r->assbackwards ? NULL : " ", r->protocol, NULL),
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein sizeof(ss->request));
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein }
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ss->vhostrec = r->server;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein }
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein }
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein if (status == SERVER_STARTING && r == NULL) {
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein /* clean up the slot's vhostrec pointer (maybe re-used)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein * and mark the slot as belonging to a new generation.
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein */
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ss->vhostrec = NULL;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ap_scoreboard_image->parent[child_num].generation = ap_my_generation;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein#ifdef SCOREBOARD_FILE
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein lseek(scoreboard_fd, XtOffsetOf(scoreboard, parent[child_num]), 0);
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein force_write(scoreboard_fd, &ap_scoreboard_image->parent[child_num],
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein sizeof(parent_score));
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein#endif
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein }
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein put_scoreboard_info(child_num, ss);
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein return old_status;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein}
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gsteinstatic void update_scoreboard_global(void)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein{
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein#ifdef SCOREBOARD_FILE
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein lseek(scoreboard_fd,
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein (char *) &ap_scoreboard_image->global -(char *) ap_scoreboard_image, 0);
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein force_write(scoreboard_fd, &ap_scoreboard_image->global,
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein sizeof ap_scoreboard_image->global);
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein#endif
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein}
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gsteinvoid ap_time_process_request(int child_num, int status)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein{
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein short_score *ss;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein if (child_num < 0)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein return;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ap_sync_scoreboard_image();
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ss = &ap_scoreboard_image->servers[child_num];
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein if (status == START_PREQUEST) {
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ss->start_time = ap_now();
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein }
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein else if (status == STOP_PREQUEST) {
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ss->stop_time = ap_now();
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein }
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein put_scoreboard_info(child_num, ss);
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein}
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein/*
b5989e567e4fac5b3ab1252024ae19b0a54893a7gsteinstatic void increment_counts(int child_num, request_rec *r)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein{
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein long int bs = 0;
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe short_score *ss;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ap_sync_scoreboard_image();
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ss = &ap_scoreboard_image->servers[child_num];
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (r->sent_bodyct)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_bgetopt(r->connection->client, BO_BYTECT, &bs);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_TIMES
f4c310fd2555c6faca1f980f00b161eadb089023gstein times(&ss->times);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein ss->access_count++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ss->my_access_count++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ss->conn_count++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ss->bytes_served += (unsigned long) bs;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ss->my_bytes_served += (unsigned long) bs;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ss->conn_bytes += (unsigned long) bs;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein put_scoreboard_info(child_num, ss);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein*/
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic int find_child_by_pid(ap_proc_t *pid)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; i < ap_max_daemons_limit; ++i)
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image->parent[i].pid == pid->pid)
f4c310fd2555c6faca1f980f00b161eadb089023gstein return i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if defined(NEED_WAITPID)
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein Systems without a real waitpid sometimes lose a child's exit while waiting
f4c310fd2555c6faca1f980f00b161eadb089023gstein for another. Search through the scoreboard for missing children.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinint reap_children(ap_wait_t *status)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int n, pid;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (n = 0; n < ap_max_daemons_limit; ++n) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_sync_scoreboard_image();
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image->servers[n].status != SERVER_DEAD &&
f4c310fd2555c6faca1f980f00b161eadb089023gstein kill((pid = ap_scoreboard_image->parent[n].pid), 0) == -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_update_child_status(n, SERVER_DEAD, NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* just mark it as having a successful exit status */
f4c310fd2555c6faca1f980f00b161eadb089023gstein bzero((char *) status, sizeof(ap_wait_t));
f4c310fd2555c6faca1f980f00b161eadb089023gstein return(pid);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* handle all varieties of core dumping signals */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void sig_coredump(int sig)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm chdir(ap_coredump_dir);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(sig, SIG_DFL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein kill(getpid(), sig);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* At this point we've got sig blocked, because we're still inside
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the signal handler. When we leave the signal handler it will
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * be unblocked, and we'll take the signal... and coredump or whatever
f4c310fd2555c6faca1f980f00b161eadb089023gstein * is appropriate for this particular Unix. In addition the parent
f4c310fd2555c6faca1f980f00b161eadb089023gstein * will see the real signal we received -- whereas if we called
f4c310fd2555c6faca1f980f00b161eadb089023gstein * abort() here, the parent would only see SIGABRT.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*****************************************************************
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Connection structures and accounting...
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void just_die(int sig)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm{
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile deferred_die;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile usr1_just_die;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void usr1_handler(int sig)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (usr1_just_die) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein just_die(sig);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein deferred_die = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* volatile just in case */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile shutdown_pending;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile restart_pending;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile is_graceful;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinap_generation_t volatile ap_my_generation=0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void sig_term(int sig)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (shutdown_pending == 1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Um, is this _probably_ not an error, if the user has
f4c310fd2555c6faca1f980f00b161eadb089023gstein * tried to do a shutdown twice quickly, so we won't
f4c310fd2555c6faca1f980f00b161eadb089023gstein * worry about reporting it.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein shutdown_pending = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void restart(int sig)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (restart_pending == 1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Probably not an error - don't bother reporting it */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein restart_pending = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((is_graceful = (sig == SIGUSR1))) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_kill_cleanup(pconf, NULL, cleanup_shared_mem);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void set_signals(void)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifndef NO_USE_SIGACTION
f4c310fd2555c6faca1f980f00b161eadb089023gstein struct sigaction sa;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein sigemptyset(&sa.sa_mask);
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_flags = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!one_process) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_handler = sig_coredump;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if defined(SA_ONESHOT)
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_flags = SA_ONESHOT;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#elif defined(SA_RESETHAND)
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_flags = SA_RESETHAND;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGSEGV, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGBUS
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGBUS, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGABORT
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGABORT, &sa, NULL) < 0)
26250b0077972bf21b6d8a8d23772a4cf53f9477gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABORT)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
8b99f2a316c5e2fa6ab208206fdd7fc2bfc4a921dougm#ifdef SIGABRT
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGABRT, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGILL
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGILL, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_flags = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_handler = sig_term;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGTERM, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGINT
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGINT, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGXCPU
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_handler = SIG_DFL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGXCPU, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGXFSZ
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_handler = SIG_DFL;
8b99f2a316c5e2fa6ab208206fdd7fc2bfc4a921dougm if (sigaction(SIGXFSZ, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGPIPE
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_handler = SIG_IGN;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGPIPE, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we want to ignore HUPs and USR1 while we're busy processing one */
f4c310fd2555c6faca1f980f00b161eadb089023gstein sigaddset(&sa.sa_mask, SIGHUP);
f4c310fd2555c6faca1f980f00b161eadb089023gstein sigaddset(&sa.sa_mask, SIGUSR1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein sa.sa_handler = restart;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGHUP, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sigaction(SIGUSR1, &sa, NULL) < 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGUSR1)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!one_process) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGSEGV, sig_coredump);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGBUS
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGBUS, sig_coredump);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGBUS */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGABORT
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGABORT, sig_coredump);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGABORT */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGABRT
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGABRT, sig_coredump);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGABRT */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGILL
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGILL, sig_coredump);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGILL */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGXCPU
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGXCPU, SIG_DFL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGXCPU */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGXFSZ
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGXFSZ, SIG_DFL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGXFSZ */
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGTERM, sig_term);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGHUP
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGHUP, restart);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGHUP */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGUSR1
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGUSR1, restart);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGUSR1 */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGPIPE
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGPIPE, SIG_IGN);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGPIPE */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void sock_disable_nagle(int s)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* The Nagle algorithm says that we should delay sending partial
f4c310fd2555c6faca1f980f00b161eadb089023gstein * packets in hopes of getting more data. We don't want to do
f4c310fd2555c6faca1f980f00b161eadb089023gstein * this; we are not telnet. There are bad interactions between
f4c310fd2555c6faca1f980f00b161eadb089023gstein * persistent connections and Nagle's algorithm that have very severe
f4c310fd2555c6faca1f980f00b161eadb089023gstein * performance penalties. (Failing to disable Nagle is not much of a
f4c310fd2555c6faca1f980f00b161eadb089023gstein * problem with simple HTTP.)
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * In spite of these problems, failure here is not a shooting offense.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein int just_say_no = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *) &just_say_no,
f4c310fd2555c6faca1f980f00b161eadb089023gstein sizeof(int)) < 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "setsockopt: (TCP_NODELAY)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein#define sock_disable_nagle(s) /* NOOP */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*****************************************************************
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Child process main loop.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * The following vars are static to avoid getting clobbered by longjmp();
f4c310fd2555c6faca1f980f00b161eadb089023gstein * they are really private to child_main.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int srv;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic ap_socket_t *csd;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int requests_this_child;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic fd_set main_fds;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinint ap_graceful_stop_signalled(void)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_sync_scoreboard_image();
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (deferred_die ||
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_scoreboard_image->global.running_generation != ap_my_generation) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void child_main(int child_num_arg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_listen_rec *lr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_listen_rec *last_lr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_listen_rec *first_lr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_pool_t *ptrans;
f4c310fd2555c6faca1f980f00b161eadb089023gstein conn_rec *current_conn;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_iol *iol;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_status_t stat;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int sockdes;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_my_pid = getpid();
f4c310fd2555c6faca1f980f00b161eadb089023gstein csd = NULL;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm my_child_num = child_num_arg;
f4c310fd2555c6faca1f980f00b161eadb089023gstein requests_this_child = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein last_lr = NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Get a sub context for global allocations in this child, so that
f4c310fd2555c6faca1f980f00b161eadb089023gstein * we can have cleanups occur when the child exits.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_create_pool(&pchild, pconf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_create_pool(&ptrans, pchild);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* needs to be done before we switch UIDs so we have permissions */
f4c310fd2555c6faca1f980f00b161eadb089023gstein reopen_scoreboard(pchild);
f4c310fd2555c6faca1f980f00b161eadb089023gstein SAFE_ACCEPT(accept_mutex_child_init(pchild));
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (unixd_setup_child()) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(APEXIT_CHILDFATAL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_child_init_hook(pchild, ap_server_conf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(my_child_num, SERVER_READY, (request_rec *) NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGHUP, just_die);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGTERM, just_die);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef OS2
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Stop Ctrl-C/Ctrl-Break signals going to child processes */
f4c310fd2555c6faca1f980f00b161eadb089023gstein {
f4c310fd2555c6faca1f980f00b161eadb089023gstein unsigned long ulTimes;
f4c310fd2555c6faca1f980f00b161eadb089023gstein DosSetSignalExceptionFocus(0, &ulTimes);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (!ap_graceful_stop_signalled()) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein BUFF *conn_io;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Prepare to receive a SIGUSR1 due to graceful restart so that
f4c310fd2555c6faca1f980f00b161eadb089023gstein * we can exit cleanly.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein usr1_just_die = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGUSR1, usr1_handler);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * (Re)initialize this child to a pre-connection state.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein current_conn = NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_clear_pool(ptrans);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((ap_max_requests_per_child > 0
f4c310fd2555c6faca1f980f00b161eadb089023gstein && requests_this_child++ >= ap_max_requests_per_child)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(my_child_num, SERVER_READY, (request_rec *) NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * Wait for an acceptable connection to arrive.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Lock around "accept", if necessary */
f4c310fd2555c6faca1f980f00b161eadb089023gstein SAFE_ACCEPT(accept_mutex_on());
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (;;) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_listeners->next) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* more than one socket */
f4c310fd2555c6faca1f980f00b161eadb089023gstein memcpy(&main_fds, &listenfds, sizeof(fd_set));
f4c310fd2555c6faca1f980f00b161eadb089023gstein srv = select(listenmaxfd + 1, &main_fds, NULL, NULL, NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (srv < 0 && errno != EINTR) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Single Unix documents select as returning errnos
f4c310fd2555c6faca1f980f00b161eadb089023gstein * EBADF, EINTR, and EINVAL... and in none of those
f4c310fd2555c6faca1f980f00b161eadb089023gstein * cases does it make sense to continue. In fact
f4c310fd2555c6faca1f980f00b161eadb089023gstein * on Linux 2.0.x we seem to end up with EFAULT
f4c310fd2555c6faca1f980f00b161eadb089023gstein * occasionally, and we'd loop forever due to it.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf, "select: (listen)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (srv <= 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we remember the last_lr we searched last time around so that
f4c310fd2555c6faca1f980f00b161eadb089023gstein we don't end up starving any particular listening socket */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (last_lr == NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein lr = ap_listeners;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein lr = last_lr->next;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!lr)
f4c310fd2555c6faca1f980f00b161eadb089023gstein lr = ap_listeners;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein first_lr=lr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein do {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_get_os_sock(&sockdes, lr->sd);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (FD_ISSET(sockdes, &main_fds))
f4c310fd2555c6faca1f980f00b161eadb089023gstein goto got_listener;
f4c310fd2555c6faca1f980f00b161eadb089023gstein lr = lr->next;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!lr)
f4c310fd2555c6faca1f980f00b161eadb089023gstein lr = ap_listeners;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (lr != first_lr);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* FIXME: if we get here, something bad has happened, and we're
f4c310fd2555c6faca1f980f00b161eadb089023gstein probably gonna spin forever.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein got_listener:
f4c310fd2555c6faca1f980f00b161eadb089023gstein last_lr = lr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein sd = lr->sd;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* only one socket, just pretend we did the other stuff */
f4c310fd2555c6faca1f980f00b161eadb089023gstein sd = ap_listeners->sd;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* if we accept() something we don't want to die, so we have to
f4c310fd2555c6faca1f980f00b161eadb089023gstein * defer the exit
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein usr1_just_die = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (;;) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (deferred_die) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we didn't get a socket, and we were told to die */
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein stat = ap_accept(&csd, sd, ptrans);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (stat == APR_SUCCESS || stat != APR_EINTR)
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (stat == APR_SUCCESS)
f4c310fd2555c6faca1f980f00b161eadb089023gstein break; /* We have a socket ready for reading */
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* TODO: this accept result handling stuff should be abstracted...
f4c310fd2555c6faca1f980f00b161eadb089023gstein * it's already out of date between the various unix mpms
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Our old behaviour here was to continue after accept()
f4c310fd2555c6faca1f980f00b161eadb089023gstein * errors. But this leads us into lots of troubles
f4c310fd2555c6faca1f980f00b161eadb089023gstein * because most of the errors are quite fatal. For
f4c310fd2555c6faca1f980f00b161eadb089023gstein * example, EMFILE can be caused by slow descriptor
f4c310fd2555c6faca1f980f00b161eadb089023gstein * leaks (say in a 3rd party module, or libc). It's
f4c310fd2555c6faca1f980f00b161eadb089023gstein * foolish for us to continue after an EMFILE. We also
f4c310fd2555c6faca1f980f00b161eadb089023gstein * seem to tickle kernel bugs on some platforms which
f4c310fd2555c6faca1f980f00b161eadb089023gstein * lead to never-ending loops here. So it seems best
f4c310fd2555c6faca1f980f00b161eadb089023gstein * to just exit in most cases.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein switch (stat) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef EPROTO
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* EPROTO on certain older kernels really means
f4c310fd2555c6faca1f980f00b161eadb089023gstein * ECONNABORTED, so we need to ignore it for them.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * See discussion in new-httpd archives nh.9701
f4c310fd2555c6faca1f980f00b161eadb089023gstein * search for EPROTO.
f4c310fd2555c6faca1f980f00b161eadb089023gstein *
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Also see nh.9603, search for EPROTO:
f4c310fd2555c6faca1f980f00b161eadb089023gstein * There is potentially a bug in Solaris 2.x x<6,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * and other boxes that implement tcp sockets in
f4c310fd2555c6faca1f980f00b161eadb089023gstein * userland (i.e. on top of STREAMS). On these
f4c310fd2555c6faca1f980f00b161eadb089023gstein * systems, EPROTO can actually result in a fatal
f4c310fd2555c6faca1f980f00b161eadb089023gstein * loop. See PR#981 for example. It's hard to
f4c310fd2555c6faca1f980f00b161eadb089023gstein * handle both uses of EPROTO.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein case EPROTO:
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef ECONNABORTED
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ECONNABORTED:
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Linux generates the rest of these, other tcp
f4c310fd2555c6faca1f980f00b161eadb089023gstein * stacks (i.e. bsd) tend to hide them behind
f4c310fd2555c6faca1f980f00b161eadb089023gstein * getsockopt() interfaces. They occur when
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the net goes sour or the client disconnects
f4c310fd2555c6faca1f980f00b161eadb089023gstein * after the three-way handshake has been done
f4c310fd2555c6faca1f980f00b161eadb089023gstein * in the kernel but before userland has picked
f4c310fd2555c6faca1f980f00b161eadb089023gstein * up the socket.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef ECONNRESET
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ECONNRESET:
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef ETIMEDOUT
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ETIMEDOUT:
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef EHOSTUNREACH
f4c310fd2555c6faca1f980f00b161eadb089023gstein case EHOSTUNREACH:
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef ENETUNREACH
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ENETUNREACH:
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef ENETDOWN
f4c310fd2555c6faca1f980f00b161eadb089023gstein case ENETDOWN:
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * When the network layer has been shut down, there
f4c310fd2555c6faca1f980f00b161eadb089023gstein * is not much use in simply exiting: the parent
f4c310fd2555c6faca1f980f00b161eadb089023gstein * would simply re-create us (and we'd fail again).
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Use the CHILDFATAL code to tear the server down.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * @@@ Martin's idea for possible improvement:
f4c310fd2555c6faca1f980f00b161eadb089023gstein * A different approach would be to define
f4c310fd2555c6faca1f980f00b161eadb089023gstein * a new APEXIT_NETDOWN exit code, the reception
f4c310fd2555c6faca1f980f00b161eadb089023gstein * of which would make the parent shutdown all
f4c310fd2555c6faca1f980f00b161eadb089023gstein * children, then idle-loop until it detected that
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the network is up again, and restart the children.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Ben Hyde noted that temporary ENETDOWN situations
f4c310fd2555c6faca1f980f00b161eadb089023gstein * occur in mobile IP.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_EMERG, stat, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "ap_accept: giving up.");
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(APEXIT_CHILDFATAL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /*ENETDOWN*/
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef TPF
f4c310fd2555c6faca1f980f00b161eadb089023gstein case EINACT:
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_EMERG, stat, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "offload device inactive");
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(APEXIT_CHILDFATAL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein default:
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "select/accept error (%u)", stat);
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(APEXIT_CHILDFATAL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein default:
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ERR, stat, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "ap_accept: (client socket)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_graceful_stop_signalled()) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein clean_child_exit(0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein usr1_just_die = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein SAFE_ACCEPT(accept_mutex_off()); /* unlock after "accept" */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* We've got a socket, let's at least process one request off the
f4c310fd2555c6faca1f980f00b161eadb089023gstein * socket before we accept a graceful restart request. We set
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the signal to ignore because we don't want to disturb any
f4c310fd2555c6faca1f980f00b161eadb089023gstein * third party code.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGUSR1, SIG_IGN);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * We now have a connection, so set it up with the appropriate
f4c310fd2555c6faca1f980f00b161eadb089023gstein * socket options, file descriptors, and read/write buffers.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_get_os_sock(&sockdes, csd);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sockdes >= FD_SETSIZE) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, NULL,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "new file descriptor %d is too large; you probably need "
f4c310fd2555c6faca1f980f00b161eadb089023gstein "to rebuild Apache with a larger FD_SETSIZE "
f4c310fd2555c6faca1f980f00b161eadb089023gstein "(currently %d)",
f4c310fd2555c6faca1f980f00b161eadb089023gstein sockdes, FD_SETSIZE);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_close_socket(csd);
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef TPF
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sockdes == 0) /* 0 is invalid socket for TPF */
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein sock_disable_nagle(sockdes);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein iol = unix_attach_socket(csd);
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(my_child_num, SERVER_BUSY_READ,
f4c310fd2555c6faca1f980f00b161eadb089023gstein (request_rec *) NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein conn_io = ap_bcreate(ptrans, B_RDWR);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_bpush_iol(conn_io, iol);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein current_conn = ap_new_apr_connection(ptrans, ap_server_conf, conn_io, csd,
f4c310fd2555c6faca1f980f00b161eadb089023gstein my_child_num);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_process_connection(current_conn);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_lingering_close(current_conn);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int make_child(server_rec *s, int slot, time_t now)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int pid;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (slot + 1 > ap_max_daemons_limit) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_max_daemons_limit = slot + 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (one_process) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGHUP, just_die);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGINT, just_die);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SIGQUIT
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGQUIT, SIG_DFL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGTERM, just_die);
f4c310fd2555c6faca1f980f00b161eadb089023gstein child_main(slot);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(slot, SERVER_STARTING, (request_rec *) NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef _OSD_POSIX
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* BS2000 requires a "special" version of fork() before a setuid() call */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((pid = os_fork(unixd_config.user_name)) == -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#elif defined(TPF)
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((pid = os_fork(s, slot)) == -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((pid = fork()) == -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process");
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* fork didn't succeed. Fix the scoreboard or else
f4c310fd2555c6faca1f980f00b161eadb089023gstein * it will say SERVER_STARTING forever and ever
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(slot, SERVER_DEAD, (request_rec *) NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* In case system resources are maxxed out, we don't want
f4c310fd2555c6faca1f980f00b161eadb089023gstein Apache running away with the CPU trying to fork over and
f4c310fd2555c6faca1f980f00b161eadb089023gstein over and over again. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein sleep(10);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!pid) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef AIX_BIND_PROCESSOR
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* by default AIX binds to a single processor
f4c310fd2555c6faca1f980f00b161eadb089023gstein * this bit unbinds children which will then bind to another cpu
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include <sys/processor.h>
f4c310fd2555c6faca1f980f00b161eadb089023gstein int status = bindprocessor(BINDPROCESS, (int)getpid(),
f4c310fd2555c6faca1f980f00b161eadb089023gstein PROCESSOR_CLASS_ANY);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (status != OK) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "processor unbind failed %d", status);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein RAISE_SIGSTOP(MAKE_CHILD);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Disable the restart signal handlers and enable the just_die stuff.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Note that since restart() just notes that a restart has been
f4c310fd2555c6faca1f980f00b161eadb089023gstein * requested there's no race condition here.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGHUP, just_die);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGUSR1, just_die);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGTERM, just_die);
f4c310fd2555c6faca1f980f00b161eadb089023gstein child_main(slot);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_image->parent[slot].pid = pid;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SCOREBOARD_FILE
f4c310fd2555c6faca1f980f00b161eadb089023gstein lseek(scoreboard_fd, XtOffsetOf(scoreboard, parent[slot]), 0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein force_write(scoreboard_fd, &ap_scoreboard_image->parent[slot],
f4c310fd2555c6faca1f980f00b161eadb089023gstein sizeof(parent_score));
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* start up a bunch of children */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void startup_children(int number_to_start)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein time_t now = time(0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image->servers[i].status != SERVER_DEAD) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (make_child(ap_server_conf, i, now) < 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein --number_to_start;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/*
f4c310fd2555c6faca1f980f00b161eadb089023gstein * idle_spawn_rate is the number of children that will be spawned on the
f4c310fd2555c6faca1f980f00b161eadb089023gstein * next maintenance cycle if there aren't enough idle servers. It is
f4c310fd2555c6faca1f980f00b161eadb089023gstein * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
f4c310fd2555c6faca1f980f00b161eadb089023gstein * without the need to spawn.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int idle_spawn_rate = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifndef MAX_SPAWN_RATE
f4c310fd2555c6faca1f980f00b161eadb089023gstein#define MAX_SPAWN_RATE (32)
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int hold_off_on_exponential_spawning;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void perform_idle_server_maintenance(void)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int to_kill;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int idle_count;
f4c310fd2555c6faca1f980f00b161eadb089023gstein short_score *ss;
f4c310fd2555c6faca1f980f00b161eadb089023gstein time_t now = time(0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein int free_length;
f5ec9b038bb9db933072ba2c0a8e7bb2a3cedbdagstein int free_slots[MAX_SPAWN_RATE];
f5ec9b038bb9db933072ba2c0a8e7bb2a3cedbdagstein int last_non_dead;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int total_non_dead;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* initialize the free_list */
f5ec9b038bb9db933072ba2c0a8e7bb2a3cedbdagstein free_length = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein to_kill = -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein idle_count = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein last_non_dead = -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein total_non_dead = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_sync_scoreboard_image();
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; i < ap_daemons_limit; ++i) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein int status;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (i >= ap_max_daemons_limit && free_length == idle_spawn_rate)
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ss = &ap_scoreboard_image->servers[i];
f4c310fd2555c6faca1f980f00b161eadb089023gstein status = ss->status;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (status == SERVER_DEAD) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* try to keep children numbers as low as possible */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (free_length < idle_spawn_rate) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein free_slots[free_length] = i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++free_length;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* We consider a starting server as idle because we started it
f4c310fd2555c6faca1f980f00b161eadb089023gstein * at least a cycle ago, and if it still hasn't finished starting
f4c310fd2555c6faca1f980f00b161eadb089023gstein * then we're just going to swamp things worse by forking more.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * So we hopefully won't need to fork more if we count it.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * This depends on the ordering of SERVER_READY and SERVER_STARTING.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (status <= SERVER_READY) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++ idle_count;
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* always kill the highest numbered child if we have to...
f4c310fd2555c6faca1f980f00b161eadb089023gstein * no really well thought out reason ... other than observing
f4c310fd2555c6faca1f980f00b161eadb089023gstein * the server behaviour under linux where lower numbered children
f4c310fd2555c6faca1f980f00b161eadb089023gstein * tend to service more hits (and hence are more likely to have
f4c310fd2555c6faca1f980f00b161eadb089023gstein * their data in cpu caches).
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein to_kill = i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ++total_non_dead;
f4c310fd2555c6faca1f980f00b161eadb089023gstein last_non_dead = i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_max_daemons_limit = last_non_dead + 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (idle_count > ap_daemons_max_free) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* kill off one child... we use SIGUSR1 because that'll cause it to
f4c310fd2555c6faca1f980f00b161eadb089023gstein * shut down gracefully, in case it happened to pick up a request
f4c310fd2555c6faca1f980f00b161eadb089023gstein * while we were counting
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein kill(ap_scoreboard_image->parent[to_kill].pid, SIGUSR1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein idle_spawn_rate = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (idle_count < ap_daemons_min_free) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* terminate the free list */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (free_length == 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* only report this condition once */
f4c310fd2555c6faca1f980f00b161eadb089023gstein static int reported = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!reported) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "server reached MaxClients setting, consider"
f4c310fd2555c6faca1f980f00b161eadb089023gstein " raising the MaxClients setting");
f4c310fd2555c6faca1f980f00b161eadb089023gstein reported = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein idle_spawn_rate = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (idle_spawn_rate >= 8) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "server seems busy, (you may need "
f4c310fd2555c6faca1f980f00b161eadb089023gstein "to increase StartServers, or Min/MaxSpareServers), "
f4c310fd2555c6faca1f980f00b161eadb089023gstein "spawning %d children, there are %d idle, and "
f4c310fd2555c6faca1f980f00b161eadb089023gstein "%d total children", idle_spawn_rate,
f4c310fd2555c6faca1f980f00b161eadb089023gstein idle_count, total_non_dead);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; i < free_length; ++i) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef TPF
f4c310fd2555c6faca1f980f00b161eadb089023gstein if(make_child(ap_server_conf, free_slots[i], now) == -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if(free_length == 1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein shutdown_pending = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "No active child processes: shutting down");
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein make_child(ap_server_conf, free_slots[i], now);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* TPF */
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* the next time around we want to spawn twice as many if this
f4c310fd2555c6faca1f980f00b161eadb089023gstein * wasn't good enough, but not if we've just done a graceful
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (hold_off_on_exponential_spawning) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein --hold_off_on_exponential_spawning;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (idle_spawn_rate < MAX_SPAWN_RATE) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein idle_spawn_rate *= 2;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f80b9e9d38dff8bc3f51406475adb99d7fe888cegstein }
f80b9e9d38dff8bc3f51406475adb99d7fe888cegstein }
f80b9e9d38dff8bc3f51406475adb99d7fe888cegstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein idle_spawn_rate = 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void process_child_status(ap_proc_t *pid, ap_wait_t status)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Child died... if it died due to a fatal error,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * we should simply bail out.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ((WIFEXITED(status)) &&
f4c310fd2555c6faca1f980f00b161eadb089023gstein WEXITSTATUS(status) == APEXIT_CHILDFATAL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "Child %ld returned a Fatal error... \n"
f4c310fd2555c6faca1f980f00b161eadb089023gstein "Apache is exiting!",
f4c310fd2555c6faca1f980f00b161eadb089023gstein (long)pid->pid);
f4c310fd2555c6faca1f980f00b161eadb089023gstein exit(APEXIT_CHILDFATAL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (WIFSIGNALED(status)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein switch (WTERMSIG(status)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein case SIGTERM:
f4c310fd2555c6faca1f980f00b161eadb089023gstein case SIGHUP:
f4c310fd2555c6faca1f980f00b161eadb089023gstein case SIGUSR1:
f4c310fd2555c6faca1f980f00b161eadb089023gstein case SIGKILL:
f4c310fd2555c6faca1f980f00b161eadb089023gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein default:
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef SYS_SIGLIST
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef WCOREDUMP
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (WCOREDUMP(status)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
f4c310fd2555c6faca1f980f00b161eadb089023gstein 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "child pid %ld exit signal %s (%d), "
f4c310fd2555c6faca1f980f00b161eadb089023gstein "possible coredump in %s",
f4c310fd2555c6faca1f980f00b161eadb089023gstein (long)pid->pid, (WTERMSIG(status) >= NumSIG) ? "" :
f4c310fd2555c6faca1f980f00b161eadb089023gstein SYS_SIGLIST[WTERMSIG(status)], WTERMSIG(status),
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_coredump_dir);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
f4c310fd2555c6faca1f980f00b161eadb089023gstein 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "child pid %ld exit signal %s (%d)", (long)pid->pid,
f4c310fd2555c6faca1f980f00b161eadb089023gstein SYS_SIGLIST[WTERMSIG(status)], WTERMSIG(status));
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef WCOREDUMP
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein#else
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
f4c310fd2555c6faca1f980f00b161eadb089023gstein 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "child pid %ld exit signal %d",
f4c310fd2555c6faca1f980f00b161eadb089023gstein (long)pid->pid, WTERMSIG(status));
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int setup_listeners(server_rec *s)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_listen_rec *lr;
f4c310fd2555c6faca1f980f00b161eadb089023gstein int sockdes;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_listen_open(s->process, s->port)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ALERT, 0, s,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "no listening sockets available, shutting down");
f4c310fd2555c6faca1f980f00b161eadb089023gstein return -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein listenmaxfd = -1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein FD_ZERO(&listenfds);
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (lr = ap_listeners; lr; lr = lr->next) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_get_os_sock(&sockdes, lr->sd);
f4c310fd2555c6faca1f980f00b161eadb089023gstein FD_SET(sockdes, &listenfds);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (sockdes > listenmaxfd) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein listenmaxfd = sockdes;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Useful to erase the status of children that might be from previous
f4c310fd2555c6faca1f980f00b161eadb089023gstein * generations */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void ap_prefork_force_reset_connection_status(long conn_id)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; i < STATUSES_PER_CONNECTION; i++) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_new_scoreboard_image->table[conn_id][i].key[0] = '\0';
f4c310fd2555c6faca1f980f00b161eadb089023gstein } }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinvoid ap_reset_connection_status(long conn_id)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (maintain_connection_status) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_prefork_force_reset_connection_status(conn_id);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein/*****************************************************************
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Executive routines.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gsteinint ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int remaining_children_to_start;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein pconf = _pconf;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_server_conf = s;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_pid(pconf, ap_pid_fname);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (setup_listeners(s)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* XXX: hey, what's the right way for the mpm to indicate a fatal error? */
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein return 1;
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein SAFE_ACCEPT(accept_mutex_init(pconf));
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!is_graceful) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein reinit_scoreboard(pconf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein#ifdef SCOREBOARD_FILE
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_note_cleanups_for_fd(pconf, scoreboard_fd);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein set_signals();
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_daemons_max_free < ap_daemons_min_free + 1) /* Don't thrash... */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_daemons_max_free = ap_daemons_min_free + 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* If we're doing a graceful_restart then we're going to see a lot
f4c310fd2555c6faca1f980f00b161eadb089023gstein * of children exiting immediately when we get into the main loop
f4c310fd2555c6faca1f980f00b161eadb089023gstein * below (because we just sent them SIGUSR1). This happens pretty
f4c310fd2555c6faca1f980f00b161eadb089023gstein * rapidly... and for each one that exits we'll start a new one until
f4c310fd2555c6faca1f980f00b161eadb089023gstein * we reach at least daemons_min_free. But we may be permitted to
f4c310fd2555c6faca1f980f00b161eadb089023gstein * start more than that, so we'll just keep track of how many we're
f4c310fd2555c6faca1f980f00b161eadb089023gstein * supposed to start up without the 1 second penalty between each fork.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein remaining_children_to_start = ap_daemons_to_start;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (remaining_children_to_start > ap_daemons_limit) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein remaining_children_to_start = ap_daemons_limit;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!is_graceful) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein startup_children(remaining_children_to_start);
f4c310fd2555c6faca1f980f00b161eadb089023gstein remaining_children_to_start = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* give the system some time to recover before kicking into
f4c310fd2555c6faca1f980f00b161eadb089023gstein * exponential mode */
f4c310fd2555c6faca1f980f00b161eadb089023gstein hold_off_on_exponential_spawning = 10;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "%s configured -- resuming normal operations",
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_get_server_version());
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "Server built: %s", ap_get_server_built());
f4c310fd2555c6faca1f980f00b161eadb089023gstein restart_pending = shutdown_pending = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein while (!restart_pending && !shutdown_pending) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein int child_slot;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_wait_t status;
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* this is a memory leak, but I'll fix it later. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_proc_t pid;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_wait_or_timeout(&status, &pid, pconf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* XXX: if it takes longer than 1 second for all our children
f4c310fd2555c6faca1f980f00b161eadb089023gstein * to start up and get into IDLE state then we may spawn an
f4c310fd2555c6faca1f980f00b161eadb089023gstein * extra child
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (pid.pid != -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein process_child_status(&pid, status);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* non-fatal death... note that it's gone in the scoreboard. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_sync_scoreboard_image();
f4c310fd2555c6faca1f980f00b161eadb089023gstein child_slot = find_child_by_pid(&pid);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (child_slot >= 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_prefork_force_reset_connection_status(child_slot);
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(child_slot, SERVER_DEAD,
f4c310fd2555c6faca1f980f00b161eadb089023gstein (request_rec *) NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (remaining_children_to_start
f4c310fd2555c6faca1f980f00b161eadb089023gstein && child_slot < ap_daemons_limit) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we're still doing a 1-for-1 replacement of dead
f4c310fd2555c6faca1f980f00b161eadb089023gstein * children with new children
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein make_child(ap_server_conf, child_slot, time(0));
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm --remaining_children_to_start;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef APR_HAS_OTHER_CHILD
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (ap_reap_other_child(&pid, status) == 0) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* handled */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (is_graceful) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Great, we've probably just lost a slot in the
f4c310fd2555c6faca1f980f00b161eadb089023gstein * scoreboard. Somehow we don't know about this
f4c310fd2555c6faca1f980f00b161eadb089023gstein * child.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
f4c310fd2555c6faca1f980f00b161eadb089023gstein 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "long lost child came home! (pid %ld)", (long)pid.pid);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Don't perform idle maintenance when a child dies,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * only do it when there's a timeout. Remember only a
f4c310fd2555c6faca1f980f00b161eadb089023gstein * finite number of children can die, and it's pretty
f4c310fd2555c6faca1f980f00b161eadb089023gstein * pathological for a lot to die suddenly.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else if (remaining_children_to_start) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we hit a 1 second timeout in which none of the previous
f4c310fd2555c6faca1f980f00b161eadb089023gstein * generation of children needed to be reaped... so assume
f4c310fd2555c6faca1f980f00b161eadb089023gstein * they're all done, and pick up the slack if any is left.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein startup_children(remaining_children_to_start);
f4c310fd2555c6faca1f980f00b161eadb089023gstein remaining_children_to_start = 0;
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein /* In any event we really shouldn't do the code below because
f94aab38f6ee899f463f0118ea395291f7c5b4cegstein * few of the servers we just started are in the IDLE state
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein * yet, so we'd mistakenly create an extra server.
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein */
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein continue;
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein }
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein perform_idle_server_maintenance();
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef TPF
f4c310fd2555c6faca1f980f00b161eadb089023gstein shutdown_pending = os_check_server(tpf_server_name);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_check_signals();
f4c310fd2555c6faca1f980f00b161eadb089023gstein sleep(1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /*TPF */
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein if (shutdown_pending) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Time to gracefully shut down:
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Kill child processes, tell them to call child_exit, etc...
f94aab38f6ee899f463f0118ea395291f7c5b4cegstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (unixd_killpg(getpgrp(), SIGTERM) < 0) {
f94aab38f6ee899f463f0118ea395291f7c5b4cegstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGTERM");
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_reclaim_child_processes(1); /* Start with SIGTERM */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* cleanup pid file on normal shutdown */
f4c310fd2555c6faca1f980f00b161eadb089023gstein {
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *pidfile = NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein pidfile = ap_server_root_relative (pconf, ap_pid_fname);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if ( pidfile != NULL && unlink(pidfile) == 0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO,
f4c310fd2555c6faca1f980f00b161eadb089023gstein 0, ap_server_conf,
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein "removed PID file %s (pid=%ld)",
f4c310fd2555c6faca1f980f00b161eadb089023gstein pidfile, (long)getpid());
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein }
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "caught SIGTERM, shutting down");
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we've been told to restart */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGHUP, SIG_IGN);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_signal(SIGUSR1, SIG_IGN);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (one_process) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* not worth thinking about */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 1;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* advance to the next generation */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* XXX: we really need to make sure this new generation number isn't in
f4c310fd2555c6faca1f980f00b161eadb089023gstein * use by any of the children.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ++ap_my_generation;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_image->global.running_generation = ap_my_generation;
f4c310fd2555c6faca1f980f00b161eadb089023gstein update_scoreboard_global();
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (is_graceful) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifndef SCOREBOARD_FILE
f4c310fd2555c6faca1f980f00b161eadb089023gstein int i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm "SIGUSR1 received. Doing graceful restart");
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* kill off the idle ones */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (unixd_killpg(getpgrp(), SIGUSR1) < 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGUSR1");
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifndef SCOREBOARD_FILE
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* This is mostly for debugging... so that we know what is still
f4c310fd2555c6faca1f980f00b161eadb089023gstein * gracefully dealing with existing request. But we can't really
f4c310fd2555c6faca1f980f00b161eadb089023gstein * do it if we're in a SCOREBOARD_FILE because it'll cause
f4c310fd2555c6faca1f980f00b161eadb089023gstein * corruption too easily.
f4c310fd2555c6faca1f980f00b161eadb089023gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_sync_scoreboard_image();
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; i < ap_daemons_limit; ++i) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image->servers[i].status != SERVER_DEAD) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_image->servers[i].status = SERVER_GRACEFUL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein else {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Kill 'em off */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (unixd_killpg(getpgrp(), SIGHUP) < 0) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGHUP");
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_reclaim_child_processes(0); /* Not when just starting up */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe "SIGHUP received. Attempting to restart");
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!is_graceful) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_restart_time = time(NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein return 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void prefork_pre_config(ap_pool_t *p, ap_pool_t *plog, ap_pool_t *ptemp)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein static int restart_num = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein one_process = !!getenv("ONE_PROCESS");
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* sigh, want this only the second time around */
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (restart_num++ == 1) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm is_graceful = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (!one_process) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_detach();
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_my_pid = getpid();
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm unixd_pre_config();
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_listen_pre_config();
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_daemons_to_start = DEFAULT_START_DAEMON;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_daemons_min_free = DEFAULT_MIN_FREE_DAEMON;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_daemons_max_free = DEFAULT_MAX_FREE_DAEMON;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_daemons_limit = HARD_SERVER_LIMIT;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_pid_fname = DEFAULT_PIDLOG;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_fname = DEFAULT_SCOREBOARD;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_lock_fname = DEFAULT_LOCKFILE;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_extended_status = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void prefork_hooks(void)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein INIT_SIGLIST();
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef AUX3
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) set42sig();
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* TODO: set one_process properly */ one_process = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_hook_pre_config(prefork_pre_config, NULL, NULL, AP_HOOK_MIDDLE);
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *set_pidfile(cmd_parms *cmd, void *dummy, const char *arg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (err != NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return err;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (cmd->server->is_virtual) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return "PidFile directive not allowed in <VirtualHost>";
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_pid_fname = arg;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *set_scoreboard(cmd_parms *cmd, void *dummy, const char *arg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (err != NULL) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm return err;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_fname = arg;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *set_lockfile(cmd_parms *cmd, void *dummy, const char *arg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (err != NULL) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein return err;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_lock_fname = arg;
f4c310fd2555c6faca1f980f00b161eadb089023gstein return NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *set_daemons_to_start(cmd_parms *cmd, void *dummy, const char *arg)
f39230a531b23d94f86a087963299bbe2e431a4agstein{
f39230a531b23d94f86a087963299bbe2e431a4agstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
50bd75672ef114fb839dd9643c192b432fdf344cgstein if (err != NULL) {
50bd75672ef114fb839dd9643c192b432fdf344cgstein return err;
50bd75672ef114fb839dd9643c192b432fdf344cgstein }
50bd75672ef114fb839dd9643c192b432fdf344cgstein
50bd75672ef114fb839dd9643c192b432fdf344cgstein ap_daemons_to_start = atoi(arg);
50bd75672ef114fb839dd9643c192b432fdf344cgstein return NULL;
50bd75672ef114fb839dd9643c192b432fdf344cgstein}
50bd75672ef114fb839dd9643c192b432fdf344cgstein
50bd75672ef114fb839dd9643c192b432fdf344cgsteinstatic const char *set_min_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
50bd75672ef114fb839dd9643c192b432fdf344cgstein{
50bd75672ef114fb839dd9643c192b432fdf344cgstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
50bd75672ef114fb839dd9643c192b432fdf344cgstein if (err != NULL) {
50bd75672ef114fb839dd9643c192b432fdf344cgstein return err;
50bd75672ef114fb839dd9643c192b432fdf344cgstein }
50bd75672ef114fb839dd9643c192b432fdf344cgstein
50bd75672ef114fb839dd9643c192b432fdf344cgstein ap_daemons_min_free = atoi(arg);
50bd75672ef114fb839dd9643c192b432fdf344cgstein if (ap_daemons_min_free <= 0) {
50bd75672ef114fb839dd9643c192b432fdf344cgstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
50bd75672ef114fb839dd9643c192b432fdf344cgstein "WARNING: detected MinSpareServers set to non-positive.");
50bd75672ef114fb839dd9643c192b432fdf344cgstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
50bd75672ef114fb839dd9643c192b432fdf344cgstein "Resetting to 1 to avoid almost certain Apache failure.");
f39230a531b23d94f86a087963299bbe2e431a4agstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
50bd75672ef114fb839dd9643c192b432fdf344cgstein "Please read the documentation.");
f39230a531b23d94f86a087963299bbe2e431a4agstein ap_daemons_min_free = 1;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein }
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein return NULL;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein}
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinstatic const char *set_max_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein{
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (err != NULL) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein return err;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein }
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
f39230a531b23d94f86a087963299bbe2e431a4agstein ap_daemons_max_free = atoi(arg);
f39230a531b23d94f86a087963299bbe2e431a4agstein return NULL;
f39230a531b23d94f86a087963299bbe2e431a4agstein}
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinstatic const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein{
f39230a531b23d94f86a087963299bbe2e431a4agstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (err != NULL) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein return err;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein }
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_daemons_limit = atoi(arg);
f39230a531b23d94f86a087963299bbe2e431a4agstein if (ap_daemons_limit > HARD_SERVER_LIMIT) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
f39230a531b23d94f86a087963299bbe2e431a4agstein "WARNING: MaxClients of %d exceeds compile time limit "
f39230a531b23d94f86a087963299bbe2e431a4agstein "of %d servers,", ap_daemons_limit, HARD_SERVER_LIMIT);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein " lowering MaxClients to %d. To increase, please "
7281ea331999debdc337b02ce37a3169e0e033a2gstein "see the", HARD_SERVER_LIMIT);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
f4c310fd2555c6faca1f980f00b161eadb089023gstein " HARD_SERVER_LIMIT define in %s.",
f4c310fd2555c6faca1f980f00b161eadb089023gstein AP_MPM_HARD_LIMITS_FILE);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_daemons_limit = HARD_SERVER_LIMIT;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein else if (ap_daemons_limit < 1) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "WARNING: Require MaxClients > 0, setting to 1");
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_daemons_limit = 1;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return NULL;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinstatic const char *set_max_requests(cmd_parms *cmd, void *dummy, const char *arg)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (err != NULL) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return err;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_max_requests_per_child = atoi(arg);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return NULL;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinstatic const char *set_coredumpdir (cmd_parms *cmd, void *dummy, const char *arg)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_finfo_t finfo;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein const char *fname;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (err != NULL) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return err;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein fname = ap_server_root_relative(cmd->pool, arg);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if ((ap_stat(&finfo, fname, cmd->pool) != APR_SUCCESS) ||
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein (finfo.filetype != APR_DIR)) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return ap_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein " does not exist or is not a directory", NULL);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return NULL;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein/* Stub functions until this MPM supports the connection status API */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein/* Don't mess with the string you get back from this function */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinconst char *ap_get_connection_status(long conn_id, const char *key)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein int i = 0;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein status_table_entry *ss;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (!maintain_connection_status) return "";
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein while (i < STATUSES_PER_CONNECTION) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ss = &(ap_new_scoreboard_image->table[conn_id][i]);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (ss->key[0] == '\0') {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein break;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (0 == strcmp(ss->key, key)) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return ss->value;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
50bd75672ef114fb839dd9643c192b432fdf344cgstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return NULL;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinap_array_header_t *ap_get_connections(ap_pool_t *p)
f4c310fd2555c6faca1f980f00b161eadb089023gstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int i;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_array_header_t *connection_list;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein long *array_slot;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein connection_list = ap_make_array(p, 0, sizeof(long));
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein /* We assume that there is a connection iff it has an entry in the status
f4c310fd2555c6faca1f980f00b161eadb089023gstein * table. Connections without any status sound problematic to me, so this
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * is probably for the best. - manoj */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein for (i = 0; i < ap_max_daemons_limit; i++) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (ap_new_scoreboard_image->table[i][0].key[0] != '\0') {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein array_slot = ap_push_array(connection_list);
f4c310fd2555c6faca1f980f00b161eadb089023gstein *array_slot = i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein }
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein return connection_list;
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein}
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinap_array_header_t *ap_get_connection_keys(ap_pool_t *p, long conn_id)
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein{
f4c310fd2555c6faca1f980f00b161eadb089023gstein int i = 0;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein status_table_entry *ss;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_array_header_t *key_list;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein char **array_slot;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein key_list = ap_make_array(p, 0, KEY_LENGTH * sizeof(char));
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein while (i < STATUSES_PER_CONNECTION) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ss = &(ap_new_scoreboard_image->table[conn_id][i]);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ss->key[0] == '\0') {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein break;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein array_slot = ap_push_array(key_list);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein *array_slot = ap_pstrdup(p, ss->key);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein i++;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return key_list;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein/* Note: no effort is made here to prevent multiple threads from messing with
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * a single connection at the same time. ap_update_connection_status should
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * only be called by the thread that owns the connection */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinvoid ap_update_connection_status(long conn_id, const char *key,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein const char *value)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein int i = 0;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein status_table_entry *ss;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (!maintain_connection_status) return;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein while (i < STATUSES_PER_CONNECTION) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ss = &(ap_new_scoreboard_image->table[conn_id][i]);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (ss->key[0] == '\0') {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein break;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein if (0 == strcmp(ss->key, key)) {
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_cpystrn(ss->value, value, VALUE_LENGTH);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein i++;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein /* Not found. Add an entry for this value */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (i >= STATUSES_PER_CONNECTION) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein /* No room. Oh well, not much anyone can do about it. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return;
50bd75672ef114fb839dd9643c192b432fdf344cgstein }
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_cpystrn(ss->key, key, KEY_LENGTH);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_cpystrn(ss->value, value, VALUE_LENGTH);
f4c310fd2555c6faca1f980f00b161eadb089023gstein return;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
f4c310fd2555c6faca1f980f00b161eadb089023gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinap_array_header_t *ap_get_status_table(ap_pool_t *p)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein{
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein int i, j;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_array_header_t *server_status;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_status_table_row_t *array_slot;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein status_table_entry *ss;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein server_status = ap_make_array(p, 0, sizeof(ap_status_table_row_t));
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein /* Go ahead and return what's in the connection status table even if we
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * aren't maintaining it. We can at least look at what children from
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein * previous generations are up to. */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein for (i = 0; i < ap_max_daemons_limit; i++) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (ap_new_scoreboard_image->table[i][0].key[0] == '\0')
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein continue;
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein array_slot = ap_push_array(server_status);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein array_slot->data = ap_make_table(p, 0);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein array_slot->conn_id = i;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (j = 0; j < STATUSES_PER_CONNECTION; j++) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ss = &(ap_new_scoreboard_image->table[i][j]);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (ss->key[0] != '\0') {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_table_add(array_slot->data, ss->key, ss->value);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein else {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein break;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return server_status;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein}
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinstatic const command_rec prefork_cmds[] = {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinUNIX_DAEMON_COMMANDS
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinLISTEN_COMMANDS
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_INIT_TAKE1("PidFile", set_pidfile, NULL, RSRC_CONF,
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein "A file for logging the server process ID"),
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinAP_INIT_TAKE1("ScoreBoardFile", set_scoreboard, NULL, RSRC_CONF,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "A file for Apache to maintain runtime process management information"),
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_INIT_TAKE1("LockFile", set_lockfile, NULL, RSRC_CONF,
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein "The lockfile used when Apache needs to lock the accept() call"),
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinAP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "Number of child processes launched at server startup"),
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_INIT_TAKE1("MinSpareServers", set_min_free_servers, NULL, RSRC_CONF,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "Minimum number of idle children, to handle request spikes"),
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_INIT_TAKE1("MaxSpareServers", set_max_free_servers, NULL, RSRC_CONF,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "Maximum number of idle children"),
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinAP_INIT_TAKE1("MaxClients", set_server_limit, NULL, RSRC_CONF,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "Maximum number of children alive at the same time"),
50bd75672ef114fb839dd9643c192b432fdf344cgsteinAP_INIT_TAKE1("MaxRequestsPerChild", set_max_requests, NULL, RSRC_CONF,
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein "Maximum number of requests a particular child serves before dying."),
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinAP_INIT_TAKE1("CoreDumpDirectory", set_coredumpdir, NULL, RSRC_CONF,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "The location of the directory Apache changes to before dumping core"),
f4c310fd2555c6faca1f980f00b161eadb089023gstein{ NULL }
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein};
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinmodule MODULE_VAR_EXPORT mpm_prefork_module = {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein MPM20_MODULE_STUFF,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein NULL, /* hook to run before apache parses args */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein NULL, /* create per-directory config structure */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein NULL, /* merge per-directory config structures */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein NULL, /* create per-server config structure */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein NULL, /* merge per-server config structures */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein prefork_cmds, /* command ap_table_t */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein NULL, /* handlers */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein prefork_hooks, /* register hooks */
f4c310fd2555c6faca1f980f00b161eadb089023gstein};
f4c310fd2555c6faca1f980f00b161eadb089023gstein