mpm_common.c revision cccd31fa4a72fe23cc3249c06db181b274a55a69
252N/A/* ====================================================================
252N/A * The Apache Software License, Version 1.1
252N/A *
252N/A * Copyright (c) 2000 The Apache Software Foundation. All rights
252N/A * reserved.
252N/A *
252N/A * Redistribution and use in source and binary forms, with or without
252N/A * modification, are permitted provided that the following conditions
252N/A * are met:
252N/A *
252N/A * 1. Redistributions of source code must retain the above copyright
252N/A * notice, this list of conditions and the following disclaimer.
252N/A *
252N/A * 2. Redistributions in binary form must reproduce the above copyright
252N/A * notice, this list of conditions and the following disclaimer in
252N/A * the documentation and/or other materials provided with the
252N/A * distribution.
252N/A *
252N/A * 3. The end-user documentation included with the redistribution,
252N/A * if any, must include the following acknowledgment:
252N/A * "This product includes software developed by the
252N/A * Apache Software Foundation (http://www.apache.org/)."
252N/A * Alternately, this acknowledgment may appear in the software itself,
252N/A * if and wherever such third-party acknowledgments normally appear.
252N/A *
252N/A * 4. The names "Apache" and "Apache Software Foundation" must
252N/A * not be used to endorse or promote products derived from this
252N/A * software without prior written permission. For written
252N/A * permission, please contact apache@apache.org.
252N/A *
252N/A * 5. Products derived from this software may not be called "Apache",
252N/A * nor may "Apache" appear in their name, without prior written
252N/A * permission of the Apache Software Foundation.
252N/A *
252N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
252N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
252N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
252N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
252N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
252N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
252N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
252N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
252N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
252N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
252N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252N/A * SUCH DAMAGE.
252N/A * ====================================================================
252N/A *
252N/A * This software consists of voluntary contributions made by many
252N/A * individuals on behalf of the Apache Software Foundation. For more
252N/A * information on the Apache Software Foundation, please see
252N/A * <http://www.apache.org/>.
252N/A *
252N/A * Portions of this software are based upon public domain software
252N/A * originally written at the National Center for Supercomputing Applications,
252N/A * University of Illinois, Urbana-Champaign.
252N/A */
252N/A
252N/A/* The purpose of this file is to store the code that MOST mpm's will need
252N/A * this does not mean a function only goes into this file if every MPM needs
252N/A * it. It means that if a function is needed by more than one MPM, and
252N/A * future maintenance would be served by making the code common, then the
252N/A * function belongs here.
252N/A *
252N/A * This is going in src/main because it is not platform specific, it is
252N/A * specific to multi-process servers, but NOT to Unix. Which is why it
252N/A * does not belong in src/os/unix
252N/A */
252N/A
252N/A#include "apr.h"
252N/A#include "apr_thread_proc.h"
252N/A#include "apr_general.h" /* for signal stuff */
252N/A
252N/A#include "httpd.h"
252N/A#include "http_config.h"
252N/A#include "http_log.h"
252N/A#include "mpm.h"
252N/A#include "mpm_common.h"
252N/A
252N/A#ifdef MPM_NEEDS_RECLAIM_CHILD_PROCESSES
252N/Avoid ap_reclaim_child_processes(int terminate)
252N/A{
252N/A int i;
252N/A long int waittime = 1024 * 16; /* in usecs */
252N/A apr_status_t waitret;
252N/A int tries;
252N/A int not_dead_yet;
252N/A int max_daemons = ap_get_max_daemons();
MPM_SYNC_CHILD_TABLE();
for (tries = terminate ? 4 : 1; tries <= 9; ++tries) {
/* don't want to hold up progress any more than
* necessary, but we need to allow children a few moments to exit.
* Set delay with an exponential backoff.
*/
waittime = waittime * 4;
apr_sleep(waittime);
/* now see who is done */
not_dead_yet = 0;
for (i = 0; i < max_daemons; ++i) {
pid_t pid = MPM_CHILD_PID(i);
apr_proc_t proc;
if (pid == 0)
continue;
proc.pid = pid;
waitret = apr_wait_proc(&proc, APR_NOWAIT);
if (waitret != APR_CHILD_NOTDONE) {
MPM_NOTE_CHILD_KILLED(i);
continue;
}
++not_dead_yet;
switch (tries) {
case 1: /* 16ms */
case 2: /* 82ms */
break;
case 3: /* 344ms */
case 4: /* 16ms */
case 5: /* 82ms */
case 6: /* 344ms */
case 7: /* 1.4sec */
/* ok, now it's being annoying */
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
0, ap_server_conf,
"child process %ld still did not exit, sending a SIGTERM",
(long)pid);
kill(pid, SIGTERM);
break;
case 8: /* 6 sec */
/* die child scum */
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
0, ap_server_conf,
"child process %ld still did not exit, sending a SIGKILL",
(long)pid);
#ifndef BEOS
kill(pid, SIGKILL);
#else
/* sending a SIGKILL kills the entire team on BeOS, and as
* httpd thread is part of that team it removes any chance
* of ever doing a restart. To counter this I'm changing to
* use a kinder, gentler way of killing a specific thread
* that is just as effective.
*/
kill_thread(pid);
#endif
break;
case 9: /* 14 sec */
/* gave it our best shot, but alas... If this really
* is a child we are trying to kill and it really hasn't
* exited, we will likely fail to bind to the port
* after the restart.
*/
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
0, ap_server_conf,
"could not make child process %ld exit, "
"attempting to continue anyway", (long)pid);
break;
}
}
apr_check_other_child();
if (!not_dead_yet) {
/* nothing left to wait for */
break;
}
}
}
#endif /* NEED_RECLAIM_CHILD_PROCESSES */
/* number of calls to wait_or_timeout between writable probes */
#ifndef INTERVAL_OF_WRITABLE_PROBES
#define INTERVAL_OF_WRITABLE_PROBES 10
#endif
static int wait_or_timeout_counter;
void ap_wait_or_timeout(apr_wait_t *status, apr_proc_t *ret, apr_pool_t *p)
{
apr_status_t rv;
++wait_or_timeout_counter;
if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
wait_or_timeout_counter = 0;
#if APR_HAS_OTHER_CHILD
apr_probe_writable_fds();
#endif
}
rv = apr_wait_all_procs(ret, status, APR_NOWAIT, p);
if (APR_STATUS_IS_EINTR(rv)) {
ret->pid = -1;
return;
}
if (APR_STATUS_IS_CHILD_DONE(rv)) {
return;
}
#ifdef NEED_WAITPID
if ((ret = reap_children(status)) > 0) {
return;
}
#endif
apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
ret->pid = -1;
return;
}
void ap_process_child_status(apr_proc_t *pid, apr_wait_t status)
{
/* Child died... if it died due to a fatal error,
* we should simply bail out.
*/
if ((WIFEXITED(status)) &&
WEXITSTATUS(status) == APEXIT_CHILDFATAL) {
ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, ap_server_conf,
"Child %ld returned a Fatal error..." APR_EOL_STR
"Apache is exiting!",
(long)pid->pid);
exit(APEXIT_CHILDFATAL);
}
if (WIFSIGNALED(status)) {
switch (WTERMSIG(status)) {
case SIGTERM:
case SIGHUP:
case SIGUSR1:
case SIGKILL:
break;
default:
#ifdef SYS_SIGLIST
#ifdef WCOREDUMP
if (WCOREDUMP(status)) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
0, ap_server_conf,
"child pid %ld exit signal %s (%d), "
"possible coredump in %s",
(long)pid->pid, (WTERMSIG(status) >= NumSIG) ? "" :
SYS_SIGLIST[WTERMSIG(status)], WTERMSIG(status),
ap_coredump_dir);
}
else {
#endif
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
0, ap_server_conf,
"child pid %ld exit signal %s (%d)",
(long)pid->pid,
SYS_SIGLIST[WTERMSIG(status)], WTERMSIG(status));
#ifdef WCOREDUMP
}
#endif
#else
ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
0, ap_server_conf,
"child pid %ld exit signal %d",
(long)pid->pid, WTERMSIG(status));
#endif
}
}
}
#if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
void ap_sock_disable_nagle(apr_socket_t *s)
{
/* The Nagle algorithm says that we should delay sending partial
* packets in hopes of getting more data. We don't want to do
* this; we are not telnet. There are bad interactions between
* persistent connections and Nagle's algorithm that have very severe
* performance penalties. (Failing to disable Nagle is not much of a
* problem with simple HTTP.)
*
* In spite of these problems, failure here is not a shooting offense.
*/
apr_status_t status = apr_setsocketopt(s, APR_TCP_NODELAY, 1);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
"setsockopt: (TCP_NODELAY)");
}
}
#endif