prefork.c revision 6e0c23c94478376306d87615a0641bcc5381c411
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein/* ====================================================================
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * The Apache Software License, Version 1.1
bc8fd1b0b1afdf89b8d28eefa8cd74e26ba97986fielding * Copyright (c) 2000 The Apache Software Foundation. All rights
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * reserved.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * Redistribution and use in source and binary forms, with or without
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * modification, are permitted provided that the following conditions
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * 1. Redistributions of source code must retain the above copyright
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein * notice, this list of conditions and the following disclaimer.
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 * 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 * 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 * 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 * 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 * 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
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.
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * httpd.c: simple http daemon for answering WWW file requests
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * 03-21-93 Rob McCool wrote original code (up to NCSA HTTPd 1.3)
1b21d7b3d97def358b2e923655edeb16613a1c31gstein * 03-06-95 blong
f4c310fd2555c6faca1f980f00b161eadb089023gstein * changed server number for child-alone processes to 0 and changed name
f4c310fd2555c6faca1f980f00b161eadb089023gstein * of processes
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.
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.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * April-July '95 rst
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Extensive rework for Apache.
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#include <bstring.h> /* for IRIX, FD_SET calls bzero() */
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* config globals */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *ap_lock_fname;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int ap_extended_status = 0;
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.
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe/* *Non*-shared http_main globals... */
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.).
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int one_process = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic ap_pool_t *pchild; /* Pool for httpd child stuff */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinint ap_my_pid; /* it seems silly to call getpid all the time */
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe#endif /* TPF */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinAPI_VAR_EXPORT scoreboard *ap_scoreboard_image = NULL;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic new_scoreboard *ap_new_scoreboard_image = NULL;
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
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void chdir_for_gprof(void)
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_get_module_config(ap_server_conf->module_config, &core_module);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_snprintf(buf, sizeof(buf), "%sgprof.%d", dir, (int)getpid());
f4c310fd2555c6faca1f980f00b161eadb089023gstein dir = ap_server_root_relative(pconf, buf[0] ? buf : dir);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
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/* a clean exit from a child with proper cleanup */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void clean_child_exit(int code) __attribute__ ((noreturn));
48f35e10f195dd594d75738fc536bb885eda537cgstein /* XXXX possibly bogus cast */
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_server_root_relative(p, ap_lock_fname), (unsigned long)getpid());
48f35e10f195dd594d75738fc536bb885eda537cgstein/* Initialize mutex lock.
48f35e10f195dd594d75738fc536bb885eda537cgstein * Done by each child at its birth
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gstein/* Initialize mutex lock.
77c2148c53e0a4a20a80af735caa4e0e6bd448e0gstein * Must be safe to call this on a restart.
48f35e10f195dd594d75738fc536bb885eda537cgstein rv = ap_create_lock(&accept_lock, APR_MUTEX, APR_CROSS_PROCESS, ap_lock_fname, p);
48f35e10f195dd594d75738fc536bb885eda537cgstein ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "couldn't create accept mutex");
48f35e10f195dd594d75738fc536bb885eda537cgsteinstatic void accept_mutex_on(void)
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void accept_mutex_off(void)
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.
48f35e10f195dd594d75738fc536bb885eda537cgstein#define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *fname;
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_scoreboard_image = ap_shm_malloc(scoreboard_shm, SCOREBOARD_SIZE);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_new_scoreboard_image = ap_shm_malloc(scoreboard_shm, NEW_SCOREBOARD_SIZE);
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_register_cleanup(p, NULL, cleanup_shared_mem, ap_null_cleanup);
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe/* Called by parent process */
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe running_gen = ap_scoreboard_image->global.running_generation;
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe ap_scoreboard_image->global.running_generation = running_gen;
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).
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
f39230a531b23d94f86a087963299bbe2e431a4agstein force_read(scoreboard_fd, ap_scoreboard_image, sizeof(*ap_scoreboard_image))
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gsteinstatic ap_inline void put_scoreboard_info(int child_num,
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein lseek(scoreboard_fd, (long) child_num * sizeof(short_score), 0);
ab78b55c6dc4431d2c68d6bb4d169ba1554290a8gstein force_write(scoreboard_fd, new_score_rec, sizeof(short_score));
f39230a531b23d94f86a087963299bbe2e431a4agsteinint ap_update_child_status(int child_num, int status, request_rec *r)
f39230a531b23d94f86a087963299bbe2e431a4agstein if (status == SERVER_READY || status == SERVER_DEAD) {
f39230a531b23d94f86a087963299bbe2e431a4agstein * Reset individual counters
f39230a531b23d94f86a087963299bbe2e431a4agstein ap_cpystrn(ss->client, ap_get_remote_host(c, r->per_dir_config,
f39230a531b23d94f86a087963299bbe2e431a4agstein ap_cpystrn(ss->request, "NULL", sizeof(ss->request));
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ap_cpystrn(ss->request, r->the_request, sizeof(ss->request));
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 /* clean up the slot's vhostrec pointer (maybe re-used)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein * and mark the slot as belonging to a new generation.
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ap_scoreboard_image->parent[child_num].generation = ap_my_generation;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein lseek(scoreboard_fd, XtOffsetOf(scoreboard, parent[child_num]), 0);
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein force_write(scoreboard_fd, &ap_scoreboard_image->parent[child_num],
b5989e567e4fac5b3ab1252024ae19b0a54893a7gsteinstatic void update_scoreboard_global(void)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein (char *) &ap_scoreboard_image->global -(char *) ap_scoreboard_image, 0);
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein force_write(scoreboard_fd, &ap_scoreboard_image->global,
b5989e567e4fac5b3ab1252024ae19b0a54893a7gsteinvoid ap_time_process_request(int child_num, int status)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gsteinstatic void increment_counts(int child_num, request_rec *r)
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein long int bs = 0;
0946f90438dcf29a5fe5d9e21559b3b9d640bc12wrowe short_score *ss;
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ap_sync_scoreboard_image();
b5989e567e4fac5b3ab1252024ae19b0a54893a7gstein ss = &ap_scoreboard_image->servers[child_num];
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (r->sent_bodyct)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_bgetopt(r->connection->client, BO_BYTECT, &bs);
f4c310fd2555c6faca1f980f00b161eadb089023gstein#ifdef HAVE_TIMES
f4c310fd2555c6faca1f980f00b161eadb089023gstein times(&ss->times);
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 put_scoreboard_info(child_num, ss);
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; i < ap_max_daemons_limit; ++i)
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 for (n = 0; n < ap_max_daemons_limit; ++n) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image->servers[n].status != SERVER_DEAD &&
f4c310fd2555c6faca1f980f00b161eadb089023gstein kill((pid = ap_scoreboard_image->parent[n].pid), 0) == -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* just mark it as having a successful exit status */
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* handle all varieties of core dumping signals */
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 * Connection structures and accounting...
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile deferred_die;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile usr1_just_die;
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* volatile just in case */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile shutdown_pending;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile restart_pending;
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int volatile is_graceful;
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 /* Probably not an error - don't bother reporting it */
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void set_signals(void)
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
26250b0077972bf21b6d8a8d23772a4cf53f9477gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABORT)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we want to ignore HUPs and USR1 while we're busy processing one */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGUSR1)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGBUS */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGABORT */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGABRT */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGILL */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGXCPU */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGXFSZ */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGHUP */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGUSR1 */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* SIGPIPE */
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void sock_disable_nagle(int s)
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 * In spite of these problems, failure here is not a shooting offense.
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 * Child process main loop.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * The following vars are static to avoid getting clobbered by longjmp();
f4c310fd2555c6faca1f980f00b161eadb089023gstein * they are really private to child_main.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm ap_scoreboard_image->global.running_generation != ap_my_generation) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Get a sub context for global allocations in this child, so that
f4c310fd2555c6faca1f980f00b161eadb089023gstein * we can have cleanups occur when the child exits.
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* needs to be done before we switch UIDs so we have permissions */
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(my_child_num, SERVER_READY, (request_rec *) NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* Stop Ctrl-C/Ctrl-Break signals going to child processes */
f4c310fd2555c6faca1f980f00b161eadb089023gstein unsigned long ulTimes;
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Prepare to receive a SIGUSR1 due to graceful restart so that
f4c310fd2555c6faca1f980f00b161eadb089023gstein * we can exit cleanly.
f4c310fd2555c6faca1f980f00b161eadb089023gstein * (Re)initialize this child to a pre-connection state.
f4c310fd2555c6faca1f980f00b161eadb089023gstein && requests_this_child++ >= ap_max_requests_per_child)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(my_child_num, SERVER_READY, (request_rec *) NULL);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * Wait for an acceptable connection to arrive.
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Lock around "accept", if necessary */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* more than one socket */
f4c310fd2555c6faca1f980f00b161eadb089023gstein srv = select(listenmaxfd + 1, &main_fds, NULL, NULL, NULL);
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.
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf, "select: (listen)");
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 /* FIXME: if we get here, something bad has happened, and we're
f4c310fd2555c6faca1f980f00b161eadb089023gstein probably gonna spin forever.
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* only one socket, just pretend we did the other stuff */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* if we accept() something we don't want to die, so we have to
f4c310fd2555c6faca1f980f00b161eadb089023gstein * defer the exit
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we didn't get a socket, and we were told to die */
f4c310fd2555c6faca1f980f00b161eadb089023gstein break; /* We have a socket ready for reading */
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* TODO: this accept result handling stuff should be abstracted...
f4c310fd2555c6faca1f980f00b161eadb089023gstein * it's already out of date between the various unix mpms
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 /* 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 * 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 /* 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 * 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 ap_log_error(APLOG_MARK, APLOG_EMERG, stat, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "ap_accept: giving up.");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /*ENETDOWN*/
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_EMERG, stat, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "offload device inactive");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ERR, stat, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "ap_accept: (client socket)");
f4c310fd2555c6faca1f980f00b161eadb089023gstein SAFE_ACCEPT(accept_mutex_off()); /* unlock after "accept" */
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 * We now have a connection, so set it up with the appropriate
f4c310fd2555c6faca1f980f00b161eadb089023gstein * socket options, file descriptors, and read/write buffers.
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 (void) ap_update_child_status(my_child_num, SERVER_BUSY_READ,
f4c310fd2555c6faca1f980f00b161eadb089023gstein current_conn = ap_new_apr_connection(ptrans, ap_server_conf, conn_io, csd,
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic int make_child(server_rec *s, int slot, time_t now)
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(slot, SERVER_STARTING, (request_rec *) NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* BS2000 requires a "special" version of fork() before a setuid() call */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process");
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* fork didn't succeed. Fix the scoreboard or else
f4c310fd2555c6faca1f980f00b161eadb089023gstein * it will say SERVER_STARTING forever and ever
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(slot, SERVER_DEAD, (request_rec *) NULL);
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/* by default AIX binds to a single processor
f4c310fd2555c6faca1f980f00b161eadb089023gstein * this bit unbinds children which will then bind to another cpu
f4c310fd2555c6faca1f980f00b161eadb089023gstein int status = bindprocessor(BINDPROCESS, (int)getpid(),
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, ap_server_conf,
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 lseek(scoreboard_fd, XtOffsetOf(scoreboard, parent[slot]), 0);
f4c310fd2555c6faca1f980f00b161eadb089023gstein force_write(scoreboard_fd, &ap_scoreboard_image->parent[slot],
f4c310fd2555c6faca1f980f00b161eadb089023gstein/* start up a bunch of children */
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_scoreboard_image->servers[i].status != SERVER_DEAD) {
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.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* initialize the free_list */
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (i = 0; i < ap_daemons_limit; ++i) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (i >= ap_max_daemons_limit && free_length == idle_spawn_rate)
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* try to keep children numbers as low as possible */
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 /* 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 /* 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 kill(ap_scoreboard_image->parent[to_kill].pid, SIGUSR1);
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* terminate the free list */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* only report this condition once */
f4c310fd2555c6faca1f980f00b161eadb089023gstein static int reported = 0;
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 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 for (i = 0; i < free_length; ++i) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein if(make_child(ap_server_conf, free_slots[i], now) == -1) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "No active child processes: shutting down");
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /* TPF */
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
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void process_child_status(ap_proc_t *pid, ap_wait_t status)
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Child died... if it died due to a fatal error,
f4c310fd2555c6faca1f980f00b161eadb089023gstein * we should simply bail out.
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 "child pid %ld exit signal %s (%d), "
f4c310fd2555c6faca1f980f00b161eadb089023gstein "possible coredump in %s",
f4c310fd2555c6faca1f980f00b161eadb089023gstein "child pid %ld exit signal %d",
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ALERT, 0, s,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "no listening sockets available, shutting down");
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 for (i = 0; i < STATUSES_PER_CONNECTION; i++) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_new_scoreboard_image->table[conn_id][i].key[0] = '\0';
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gstein/*****************************************************************
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Executive routines.
52c1d304b1bd8e05da40a7cded2ecb9f0ba614c5gsteinint ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s)
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* XXX: hey, what's the right way for the mpm to indicate a fatal error? */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_scoreboard_fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (ap_daemons_max_free < ap_daemons_min_free + 1) /* Don't thrash... */
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 if (remaining_children_to_start > ap_daemons_limit) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* give the system some time to recover before kicking into
f4c310fd2555c6faca1f980f00b161eadb089023gstein * exponential mode */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "%s configured -- resuming normal operations",
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* this is a memory leak, but I'll fix it later. */
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 /* non-fatal death... note that it's gone in the scoreboard. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_prefork_force_reset_connection_status(child_slot);
f4c310fd2555c6faca1f980f00b161eadb089023gstein (void) ap_update_child_status(child_slot, SERVER_DEAD,
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we're still doing a 1-for-1 replacement of dead
f4c310fd2555c6faca1f980f00b161eadb089023gstein * children with new children
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* handled */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Great, we've probably just lost a slot in the
f4c310fd2555c6faca1f980f00b161eadb089023gstein * scoreboard. Somehow we don't know about this
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "long lost child came home! (pid %ld)", (long)pid.pid);
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 /* 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.
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.
f4c310fd2555c6faca1f980f00b161eadb089023gstein#endif /*TPF */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Time to gracefully shut down:
f4c310fd2555c6faca1f980f00b161eadb089023gstein * Kill child processes, tell them to call child_exit, etc...
f94aab38f6ee899f463f0118ea395291f7c5b4cegstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGTERM");
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_reclaim_child_processes(1); /* Start with SIGTERM */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* cleanup pid file on normal shutdown */
f4c310fd2555c6faca1f980f00b161eadb089023gstein pidfile = ap_server_root_relative (pconf, ap_pid_fname);
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein "removed PID file %s (pid=%ld)",
866b521be8a30b2798ad3c3b73de5e965edd7c2fgstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "caught SIGTERM, shutting down");
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* we've been told to restart */
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* not worth thinking about */
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 ap_scoreboard_image->global.running_generation = ap_my_generation;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm "SIGUSR1 received. Doing graceful restart");
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* kill off the idle ones */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGUSR1");
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 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 /* Kill 'em off */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGHUP");
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");
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void prefork_pre_config(ap_pool_t *p, ap_pool_t *plog, ap_pool_t *ptemp)
f4c310fd2555c6faca1f980f00b161eadb089023gstein static int restart_num = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* sigh, want this only the second time around */
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic void prefork_hooks(void)
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* TODO: set one_process properly */ one_process = 0;
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_hook_pre_config(prefork_pre_config, NULL, NULL, AP_HOOK_MIDDLE);
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *set_pidfile(cmd_parms *cmd, void *dummy, const char *arg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
f4c310fd2555c6faca1f980f00b161eadb089023gstein return "PidFile directive not allowed in <VirtualHost>";
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *set_scoreboard(cmd_parms *cmd, void *dummy, const char *arg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *set_lockfile(cmd_parms *cmd, void *dummy, const char *arg)
f4c310fd2555c6faca1f980f00b161eadb089023gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
f4c310fd2555c6faca1f980f00b161eadb089023gsteinstatic const char *set_daemons_to_start(cmd_parms *cmd, void *dummy, const char *arg)
f39230a531b23d94f86a087963299bbe2e431a4agstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
50bd75672ef114fb839dd9643c192b432fdf344cgsteinstatic const char *set_min_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
50bd75672ef114fb839dd9643c192b432fdf344cgstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
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.");
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinstatic const char *set_max_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
83719c22db4a6d0575bb4f7f34382d7b185a6f74gsteinstatic const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
f39230a531b23d94f86a087963299bbe2e431a4agstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
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 "
83719c22db4a6d0575bb4f7f34382d7b185a6f74gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
f4c310fd2555c6faca1f980f00b161eadb089023gstein " HARD_SERVER_LIMIT define in %s.",
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein "WARNING: Require MaxClients > 0, setting to 1");
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinstatic const char *set_max_requests(cmd_parms *cmd, void *dummy, const char *arg)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinstatic const char *set_coredumpdir (cmd_parms *cmd, void *dummy, const char *arg)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein const char *fname;
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if ((ap_stat(&finfo, fname, cmd->pool) != APR_SUCCESS) ||
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein return ap_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein ap_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
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)
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') {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinap_array_header_t *ap_get_connection_keys(ap_pool_t *p, long conn_id)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein key_list = ap_make_array(p, 0, KEY_LENGTH * sizeof(char));
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 */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gsteinvoid ap_update_connection_status(long conn_id, const char *key,
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein const char *value)
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein /* Not found. Add an entry for this value */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein /* No room. Oh well, not much anyone can do about it. */
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein server_status = ap_make_array(p, 0, sizeof(ap_status_table_row_t));
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 for (i = 0; i < ap_max_daemons_limit; i++) {
4d0b0b6d8341c5e54b2081665fc91b4e4f781753gstein if (ap_new_scoreboard_image->table[i][0].key[0] == '\0')
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (j = 0; j < STATUSES_PER_CONNECTION; j++) {
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"),