mpm_common.c revision 5a63340978acb9dd7e87724be57d2bde1cf1f629
97a9a944b5887e91042b019776c41d5dd74557aferikabele/* ====================================================================
97a9a944b5887e91042b019776c41d5dd74557aferikabele * The Apache Software License, Version 1.1
97a9a944b5887e91042b019776c41d5dd74557aferikabele *
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * reserved.
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive *
5a58787efeb02a1c3f06569d019ad81fd2efa06end * Redistribution and use in source and binary forms, with or without
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletc * modification, are permitted provided that the following conditions
5a58787efeb02a1c3f06569d019ad81fd2efa06end * are met:
5a58787efeb02a1c3f06569d019ad81fd2efa06end *
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 1. Redistributions of source code must retain the above copyright
2e545ce2450a9953665f701bb05350f0d3f26275nd * notice, this list of conditions and the following disclaimer.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen *
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 2. Redistributions in binary form must reproduce the above copyright
5a58787efeb02a1c3f06569d019ad81fd2efa06end * notice, this list of conditions and the following disclaimer in
5a58787efeb02a1c3f06569d019ad81fd2efa06end * the documentation and/or other materials provided with the
5a58787efeb02a1c3f06569d019ad81fd2efa06end * distribution.
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen *
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * 3. The end-user documentation included with the redistribution,
5a58787efeb02a1c3f06569d019ad81fd2efa06end * if any, must include the following acknowledgment:
5a58787efeb02a1c3f06569d019ad81fd2efa06end * "This product includes software developed by the
5a58787efeb02a1c3f06569d019ad81fd2efa06end * Apache Software Foundation (http://www.apache.org/)."
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * Alternately, this acknowledgment may appear in the software itself,
5a58787efeb02a1c3f06569d019ad81fd2efa06end * if and wherever such third-party acknowledgments normally appear.
a63f0ab647ad2ab72efc9bea7a66e24e9ebc5cc2nd *
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd * 4. The names "Apache" and "Apache Software Foundation" must
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * not be used to endorse or promote products derived from this
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * software without prior written permission. For written
e1e8390280254f7f0580d701e583f670643d4f3fnilgun * permission, please contact apache@apache.org.
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung *
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd * 5. Products derived from this software may not be called "Apache",
5a58787efeb02a1c3f06569d019ad81fd2efa06end * nor may "Apache" appear in their name, without prior written
5a58787efeb02a1c3f06569d019ad81fd2efa06end * permission of the Apache Software Foundation.
5a58787efeb02a1c3f06569d019ad81fd2efa06end *
5a58787efeb02a1c3f06569d019ad81fd2efa06end * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
5a58787efeb02a1c3f06569d019ad81fd2efa06end * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5a58787efeb02a1c3f06569d019ad81fd2efa06end * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
5a58787efeb02a1c3f06569d019ad81fd2efa06end * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
06ba4a61654b3763ad65f52283832ebf058fdf1cslive * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ced7ef1f8c0df1805da0e87dbc5a1b6282910573nd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
ced7ef1f8c0df1805da0e87dbc5a1b6282910573nd * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
b21197dc8e6b8c764fdcc24d4bae8b0eebb6bc4end * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive * SUCH DAMAGE.
97a9a944b5887e91042b019776c41d5dd74557aferikabele * ====================================================================
f8396ed8364b56ec8adeaa49cac35a929758a29eslive *
ffb01336be79c64046b636e59fa8ddca8ec029edsf * This software consists of voluntary contributions made by many
f8396ed8364b56ec8adeaa49cac35a929758a29eslive * individuals on behalf of the Apache Software Foundation. For more
f8396ed8364b56ec8adeaa49cac35a929758a29eslive * information on the Apache Software Foundation, please see
5a58787efeb02a1c3f06569d019ad81fd2efa06end * <http://www.apache.org/>.
5a58787efeb02a1c3f06569d019ad81fd2efa06end *
5a58787efeb02a1c3f06569d019ad81fd2efa06end * Portions of this software are based upon public domain software
5a58787efeb02a1c3f06569d019ad81fd2efa06end * originally written at the National Center for Supercomputing Applications,
deeee6bb6fd94c0ba5f3730b58abd9d299c89ccdnd * University of Illinois, Urbana-Champaign.
4db28ee269aa06f7c6232e11cd01f58c3349af23noodl */
117c1f888a14e73cdd821dc6c23eb0411144a41cnd
117c1f888a14e73cdd821dc6c23eb0411144a41cnd/* The purpose of this file is to store the code that MOST mpm's will need
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf * this does not mean a function only goes into this file if every MPM needs
117c1f888a14e73cdd821dc6c23eb0411144a41cnd * it. It means that if a function is needed by more than one MPM, and
ffb01336be79c64046b636e59fa8ddca8ec029edsf * future maintenance would be served by making the code common, then the
117c1f888a14e73cdd821dc6c23eb0411144a41cnd * function belongs here.
117c1f888a14e73cdd821dc6c23eb0411144a41cnd *
117c1f888a14e73cdd821dc6c23eb0411144a41cnd * This is going in src/main because it is not platform specific, it is
2bc7f1cf720973a67f8ff7a8d523e40569ae5b6cnd * specific to multi-process servers, but NOT to Unix. Which is why it
117c1f888a14e73cdd821dc6c23eb0411144a41cnd * does not belong in src/os/unix
117c1f888a14e73cdd821dc6c23eb0411144a41cnd */
117c1f888a14e73cdd821dc6c23eb0411144a41cnd
117c1f888a14e73cdd821dc6c23eb0411144a41cnd#include "apr.h"
117c1f888a14e73cdd821dc6c23eb0411144a41cnd#include "apr_thread_proc.h"
117c1f888a14e73cdd821dc6c23eb0411144a41cnd#include "apr_signal.h"
87ffb6e33f3cbef3b9bb406cc2d27039fa336eaatrawick#include "apr_strings.h"
4db28ee269aa06f7c6232e11cd01f58c3349af23noodl#define APR_WANT_STRFUNC
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "apr_want.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "apr_getopt.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "apr_optional.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "httpd.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "http_config.h"
654d8eb036bedc99e90e11910ee02d3421417697rbowen#include "http_log.h"
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh#include "http_main.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "mpm.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "mpm_common.h"
5a58787efeb02a1c3f06569d019ad81fd2efa06end#include "ap_mpm.h"
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf#include "ap_listen.h"
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick#include "mpm_default.h"
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick#ifdef AP_MPM_WANT_SET_SCOREBOARD
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick#include "scoreboard.h"
06ba4a61654b3763ad65f52283832ebf058fdf1cslive#endif
654d8eb036bedc99e90e11910ee02d3421417697rbowen
06ba4a61654b3763ad65f52283832ebf058fdf1cslive#ifdef HAVE_PWD_H
06ba4a61654b3763ad65f52283832ebf058fdf1cslive#include <pwd.h>
06ba4a61654b3763ad65f52283832ebf058fdf1cslive#endif
b1b0d8ff12f69d873f28a9bf79dfba6b67b45a4bigalic#ifdef HAVE_GRP_H
97a9a944b5887e91042b019776c41d5dd74557aferikabele#include <grp.h>
654d8eb036bedc99e90e11910ee02d3421417697rbowen#endif
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive
97a9a944b5887e91042b019776c41d5dd74557aferikabele#ifdef AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slivevoid ap_reclaim_child_processes(int terminate)
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive{
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive int i;
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive long int waittime = 1024 * 16; /* in usecs */
ffb01336be79c64046b636e59fa8ddca8ec029edsf apr_status_t waitret;
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick int tries;
ffb01336be79c64046b636e59fa8ddca8ec029edsf int not_dead_yet;
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd int max_daemons;
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick for (tries = terminate ? 4 : 1; tries <= 9; ++tries) {
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf /* don't want to hold up progress any more than
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd * necessary, but we need to allow children a few moments to exit.
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf * Set delay with an exponential backoff.
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf */
ffb01336be79c64046b636e59fa8ddca8ec029edsf apr_sleep(waittime);
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd waittime = waittime * 4;
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick /* now see who is done */
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick not_dead_yet = 0;
77c77cf89621f21c8e2bbad63058b5eaa5f88d4ajim for (i = 0; i < max_daemons; ++i) {
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick pid_t pid = MPM_CHILD_PID(i);
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf apr_proc_t proc;
ced7ef1f8c0df1805da0e87dbc5a1b6282910573nd
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick if (pid == 0)
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf continue;
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf proc.pid = pid;
ced7ef1f8c0df1805da0e87dbc5a1b6282910573nd waitret = apr_proc_wait(&proc, NULL, NULL, APR_NOWAIT);
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick if (waitret != APR_CHILD_NOTDONE) {
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick MPM_NOTE_CHILD_KILLED(i);
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick continue;
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd }
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick ++not_dead_yet;
b244bbf442a0aea3dc397b4d0d751f4716c5891dnd switch (tries) {
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick case 1: /* 16ms */
ffb01336be79c64046b636e59fa8ddca8ec029edsf case 2: /* 82ms */
8a6d5edcb07aeccca7afba02a17dd6904d6b206ctrawick case 3: /* 344ms */
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive case 4: /* 16ms */
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive break;
97a9a944b5887e91042b019776c41d5dd74557aferikabele
92510838f2eb125726e15c5eb4f7a23c7a0396e4slive case 5: /* 82ms */
4aa603e6448b99f9371397d439795c91a93637eand case 6: /* 344ms */
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic case 7: /* 1.4sec */
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic /* ok, now it's being annoying */
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic ap_log_error(APLOG_MARK, APLOG_WARNING,
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic 0, ap_server_conf,
4aa603e6448b99f9371397d439795c91a93637eand "child process %ld still did not exit, "
f0fa55ff14fa0bf8fd72d989f6625de6dc3260c8igalic "sending a SIGTERM",
06ba4a61654b3763ad65f52283832ebf058fdf1cslive (long)pid);
e8d485701957d5c6de870111c112e168a894d49and kill(pid, SIGTERM);
e8d485701957d5c6de870111c112e168a894d49and break;
654d8eb036bedc99e90e11910ee02d3421417697rbowen
654d8eb036bedc99e90e11910ee02d3421417697rbowen case 8: /* 6 sec */
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive /* die child scum */
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive ap_log_error(APLOG_MARK, APLOG_ERR,
9bcfc3697a91b5215893a7d0206865b13fc72148nd 0, ap_server_conf,
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive "child process %ld still did not exit, "
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive "sending a SIGKILL",
06ba4a61654b3763ad65f52283832ebf058fdf1cslive (long)pid);
4a31db3c3a0202003c1b9f87affa7cc143e120e5sf#ifndef BEOS
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive kill(pid, SIGKILL);
9b6a3a558cc90ffdaa0b50bd02546ffec424ded7slive#else
709e3a21ba73b8433462959cd56c773454b34441trawick /* sending a SIGKILL kills the entire team on BeOS, and as
709e3a21ba73b8433462959cd56c773454b34441trawick * httpd thread is part of that team it removes any chance
709e3a21ba73b8433462959cd56c773454b34441trawick * of ever doing a restart. To counter this I'm changing to
709e3a21ba73b8433462959cd56c773454b34441trawick * use a kinder, gentler way of killing a specific thread
709e3a21ba73b8433462959cd56c773454b34441trawick * that is just as effective.
709e3a21ba73b8433462959cd56c773454b34441trawick */
709e3a21ba73b8433462959cd56c773454b34441trawick kill_thread(pid);
5a58787efeb02a1c3f06569d019ad81fd2efa06end#endif
5a58787efeb02a1c3f06569d019ad81fd2efa06end break;
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd case 9: /* 14 sec */
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd /* gave it our best shot, but alas... If this really
e1e8390280254f7f0580d701e583f670643d4f3fnilgun * is a child we are trying to kill and it really hasn't
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung * exited, we will likely fail to bind to the port
727872d18412fc021f03969b8641810d8896820bhumbedooh * after the restart.
0d0ba3a410038e179b695446bb149cce6264e0abnd */
727872d18412fc021f03969b8641810d8896820bhumbedooh ap_log_error(APLOG_MARK, APLOG_ERR,
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh 0, ap_server_conf,
0d0ba3a410038e179b695446bb149cce6264e0abnd "could not make child process %ld exit, "
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh "attempting to continue anyway",
727872d18412fc021f03969b8641810d8896820bhumbedooh (long)pid);
0d0ba3a410038e179b695446bb149cce6264e0abnd break;
0d0ba3a410038e179b695446bb149cce6264e0abnd }
0d0ba3a410038e179b695446bb149cce6264e0abnd }
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh
0d0ba3a410038e179b695446bb149cce6264e0abnd#if APR_HAS_OTHER_CHILD
0d0ba3a410038e179b695446bb149cce6264e0abnd apr_proc_other_child_check();
0d0ba3a410038e179b695446bb149cce6264e0abnd#endif
727872d18412fc021f03969b8641810d8896820bhumbedooh
0d0ba3a410038e179b695446bb149cce6264e0abnd if (!not_dead_yet) {
0d0ba3a410038e179b695446bb149cce6264e0abnd /* nothing left to wait for */
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh break;
205f749042ed530040a4f0080dbcb47ceae8a374rjung }
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen }
0d0ba3a410038e179b695446bb149cce6264e0abnd}
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd#endif /* AP_MPM_WANT_RECLAIM_CHILD_PROCESSES */
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd#ifdef AP_MPM_WANT_WAIT_OR_TIMEOUT
5a58787efeb02a1c3f06569d019ad81fd2efa06end
/* 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_exit_why_e *status, int *exitcode, 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;
}
rv = apr_proc_wait_all_procs(ret, exitcode, 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(exitcode, status)) > 0) {
return;
}
#endif
apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
ret->pid = -1;
return;
}
#endif /* AP_MPM_WANT_WAIT_OR_TIMEOUT */
#ifdef AP_MPM_WANT_PROCESS_CHILD_STATUS
int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
{
int signum = status;
const char *sigdesc = apr_signal_get_description(signum);
/* Child died... if it died due to a fatal error,
* we should simply bail out. The caller needs to
* check for bad rc from us and exit, running any
* appropriate cleanups.
*
* If the child died due to a resource shortage,
* the parent should limit the rate of forking
*/
if (APR_PROC_CHECK_EXIT(why)) {
if (status == APEXIT_CHILDSICK) {
return status;
}
if (status == APEXIT_CHILDFATAL) {
ap_log_error(APLOG_MARK, APLOG_ALERT,
0, ap_server_conf,
"Child %" APR_PID_T_FMT
" returned a Fatal error..." APR_EOL_STR
"Apache is exiting!",
pid->pid);
return APEXIT_CHILDFATAL;
}
return 0;
}
if (APR_PROC_CHECK_SIGNALED(why)) {
switch (signum) {
case SIGTERM:
case SIGHUP:
case AP_SIG_GRACEFUL:
case SIGKILL:
break;
default:
if (APR_PROC_CHECK_CORE_DUMP(why)) {
ap_log_error(APLOG_MARK, APLOG_NOTICE,
0, ap_server_conf,
"child pid %ld exit signal %s (%d), "
"possible coredump in %s",
(long)pid->pid, sigdesc, signum,
ap_coredump_dir);
}
else {
ap_log_error(APLOG_MARK, APLOG_NOTICE,
0, ap_server_conf,
"child pid %ld exit signal %s (%d)",
(long)pid->pid, sigdesc, signum);
}
}
}
return 0;
}
#endif /* AP_MPM_WANT_PROCESS_CHILD_STATUS */
#if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF) && !defined(WIN32)
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
#ifdef HAVE_GETPWNAM
AP_DECLARE(uid_t) ap_uname2id(const char *name)
{
struct passwd *ent;
if (name[0] == '#')
return (atoi(&name[1]));
if (!(ent = getpwnam(name))) {
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
"%s: bad user name %s", ap_server_argv0, name);
exit(1);
}
return (ent->pw_uid);
}
#endif
#ifdef HAVE_GETGRNAM
AP_DECLARE(gid_t) ap_gname2id(const char *name)
{
struct group *ent;
if (name[0] == '#')
return (atoi(&name[1]));
if (!(ent = getgrnam(name))) {
ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
"%s: bad group name %s", ap_server_argv0, name);
exit(1);
}
return (ent->gr_gid);
}
#endif
#ifndef HAVE_INITGROUPS
int initgroups(const char *name, gid_t basegid)
{
#if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32) || defined(NETWARE)
/* QNX, MPE and BeOS do not appear to support supplementary groups. */
return 0;
#else /* ndef QNX */
gid_t groups[NGROUPS_MAX];
struct group *g;
int index = 0;
setgrent();
groups[index++] = basegid;
while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
if (g->gr_gid != basegid) {
char **names;
for (names = g->gr_mem; *names != NULL; ++names) {
if (!strcmp(*names, name))
groups[index++] = g->gr_gid;
}
}
}
endgrent();
return setgroups(index, groups);
#endif /* def QNX */
}
#endif /* def NEED_INITGROUPS */
#ifdef AP_MPM_USES_POD
AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
{
apr_status_t rv;
*pod = apr_palloc(p, sizeof(**pod));
rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
if (rv != APR_SUCCESS) {
return rv;
}
apr_file_pipe_timeout_set((*pod)->pod_in, 0);
(*pod)->p = p;
apr_sockaddr_info_get(&(*pod)->sa, ap_listeners->bind_addr->hostname,
APR_UNSPEC, ap_listeners->bind_addr->port, 0, p);
return APR_SUCCESS;
}
AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
{
char c;
apr_size_t len = 1;
apr_status_t rv;
rv = apr_file_read(pod->pod_in, &c, &len);
if ((rv == APR_SUCCESS) && (len == 1)) {
return APR_SUCCESS;
}
if (rv != APR_SUCCESS) {
return rv;
}
return AP_NORESTART;
}
AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
{
apr_status_t rv;
rv = apr_file_close(pod->pod_out);
if (rv != APR_SUCCESS) {
return rv;
}
rv = apr_file_close(pod->pod_in);
if (rv != APR_SUCCESS) {
return rv;
}
return APR_SUCCESS;
}
static apr_status_t pod_signal_internal(ap_pod_t *pod)
{
apr_status_t rv;
char char_of_death = '!';
apr_size_t one = 1;
rv = apr_file_write(pod->pod_out, &char_of_death, &one);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
"write pipe_of_death");
}
return rv;
}
/* This function connects to the server, then immediately closes the connection.
* This permits the MPM to skip the poll when there is only one listening
* socket, because it provides a alternate way to unblock an accept() when
* the pod is used.
*/
static apr_status_t dummy_connection(ap_pod_t *pod)
{
apr_status_t rv;
apr_socket_t *sock;
apr_pool_t *p;
/* create a temporary pool for the socket. pconf stays around too long */
rv = apr_pool_create(&p, pod->p);
if (rv != APR_SUCCESS) {
return rv;
}
rv = apr_socket_create(&sock, pod->sa->family, SOCK_STREAM, p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
"get socket to connect to listener");
return rv;
}
/* on some platforms (e.g., FreeBSD), the kernel won't accept many
* queued connections before it starts blocking local connects...
* we need to keep from blocking too long and instead return an error,
* because the MPM won't want to hold up a graceful restart for a
* long time
*/
rv = apr_setsocketopt(sock, APR_SO_TIMEOUT, 3 * APR_USEC_PER_SEC);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
"set timeout on socket to connect to listener");
apr_socket_close(sock);
return rv;
}
rv = apr_connect(sock, pod->sa);
if (rv != APR_SUCCESS) {
int log_level = APLOG_WARNING;
if (APR_STATUS_IS_TIMEUP(rv)) {
/* probably some server processes bailed out already and there
* is nobody around to call accept and clear out the kernel
* connection queue; usually this is not worth logging
*/
log_level = APLOG_DEBUG;
}
ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
"connect to listener");
}
apr_socket_close(sock);
apr_pool_destroy(p);
return rv;
}
AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
{
apr_status_t rv;
rv = pod_signal_internal(pod);
if (rv != APR_SUCCESS) {
return rv;
}
return dummy_connection(pod);
}
void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
{
int i;
apr_status_t rv = APR_SUCCESS;
/* we don't write anything to the pod here... we assume
* that the would-be reader of the pod has another way to
* see that it is time to die once we wake it up
*
* writing lots of things to the pod at once is very
* problematic... we can fill the kernel pipe buffer and
* be blocked until somebody consumes some bytes or
* we hit a timeout... if we hit a timeout we can't just
* keep trying because maybe we'll never successfully
* write again... but then maybe we'll leave would-be
* readers stranded (a number of them could be tied up for
* a while serving time-consuming requests)
*/
for (i = 0; i < num && rv == APR_SUCCESS; i++) {
rv = dummy_connection(pod);
}
}
#endif /* #ifdef AP_MPM_USES_POD */
/* standard mpm configuration handling */
#ifdef AP_MPM_WANT_SET_PIDFILE
const char *ap_pid_fname = NULL;
const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
const char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
if (cmd->server->is_virtual) {
return "PidFile directive not allowed in <VirtualHost>";
}
ap_pid_fname = arg;
return NULL;
}
#endif
#ifdef AP_MPM_WANT_SET_SCOREBOARD
const char * ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
const char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
ap_scoreboard_fname = arg;
return NULL;
}
#endif
#ifdef AP_MPM_WANT_SET_LOCKFILE
const char *ap_lock_fname = NULL;
const char *ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
const char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
ap_lock_fname = arg;
return NULL;
}
#endif
#ifdef AP_MPM_WANT_SET_MAX_REQUESTS
int ap_max_requests_per_child = 0;
const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
const char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
ap_max_requests_per_child = atoi(arg);
return NULL;
}
#endif
#ifdef AP_MPM_WANT_SET_COREDUMPDIR
char ap_coredump_dir[MAX_STRING_LEN];
const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
const char *arg)
{
apr_status_t rv;
apr_finfo_t finfo;
const char *fname;
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
fname = ap_server_root_relative(cmd->pool, arg);
if (!fname) {
return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
arg, NULL);
}
if ((rv = apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) {
return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
" does not exist", NULL);
}
if (finfo.filetype != APR_DIR) {
return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
" is not a directory", NULL);
}
apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
return NULL;
}
#endif
#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
apr_lockmech_e ap_accept_lock_mech = APR_LOCK_DEFAULT;
const char ap_valid_accept_mutex_string[] =
"Valid accept mutexes for this platform and MPM are: default"
#if APR_HAS_FLOCK_SERIALIZE
", flock"
#endif
#if APR_HAS_FCNTL_SERIALIZE
", fcntl"
#endif
#if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
", sysvsem"
#endif
#if APR_HAS_POSIXSEM_SERIALIZE
", posixsem"
#endif
#if APR_HAS_PROC_PTHREAD_SERIALIZE
", pthread"
#endif
".";
AP_DECLARE(const char *) ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
void *dummy,
const char *arg)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL) {
return err;
}
if (!strcasecmp(arg, "default")) {
ap_accept_lock_mech = APR_LOCK_DEFAULT;
}
#if APR_HAS_FLOCK_SERIALIZE
else if (!strcasecmp(arg, "flock")) {
ap_accept_lock_mech = APR_LOCK_FLOCK;
}
#endif
#if APR_HAS_FCNTL_SERIALIZE
else if (!strcasecmp(arg, "fcntl")) {
ap_accept_lock_mech = APR_LOCK_FCNTL;
}
#endif
/* perchild can't use SysV sems because the permissions on the accept
* mutex can't be set to allow all processes to use the mutex and
* at the same time keep all users from being able to dink with the
* mutex
*/
#if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
else if (!strcasecmp(arg, "sysvsem")) {
ap_accept_lock_mech = APR_LOCK_SYSVSEM;
}
#endif
#if APR_HAS_POSIXSEM_SERIALIZE
else if (!strcasecmp(arg, "posixsem")) {
ap_accept_lock_mech = APR_LOCK_POSIXSEM;
}
#endif
#if APR_HAS_PROC_PTHREAD_SERIALIZE
else if (!strcasecmp(arg, "pthread")) {
ap_accept_lock_mech = APR_LOCK_PROC_PTHREAD;
}
#endif
else {
return apr_pstrcat(cmd->pool, arg, " is an invalid mutex mechanism; ",
ap_valid_accept_mutex_string, NULL);
}
return NULL;
}
#endif
#ifdef AP_MPM_WANT_SIGNAL_SERVER
static const char *dash_k_arg;
static int send_signal(pid_t pid, int sig)
{
if (kill(pid, sig) < 0) {
ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL,
"sending signal to server");
return 1;
}
return 0;
}
int ap_signal_server(int *exit_status, apr_pool_t *pconf)
{
apr_status_t rv;
pid_t otherpid;
int running = 0;
int have_pid_file = 0;
const char *status;
*exit_status = 0;
rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
if (rv != APR_SUCCESS) {
if (rv != APR_ENOENT) {
ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL,
"Error retrieving pid file %s", ap_pid_fname);
*exit_status = 1;
return 1;
}
status = "httpd (no pid file) not running";
}
else {
have_pid_file = 1;
if (kill(otherpid, 0) == 0) {
running = 1;
status = apr_psprintf(pconf,
"httpd (pid %" APR_PID_T_FMT ") already "
"running", otherpid);
}
else {
status = apr_psprintf(pconf,
"httpd (pid %" APR_PID_T_FMT "?) not running",
otherpid);
}
}
if (!strcmp(dash_k_arg, "start")) {
if (running) {
printf("%s\n", status);
return 1;
}
}
if (!strcmp(dash_k_arg, "stop")) {
if (!running) {
printf("%s\n", status);
}
else {
send_signal(otherpid, SIGTERM);
}
return 1;
}
if (!strcmp(dash_k_arg, "restart")) {
if (!running) {
printf("httpd not running, trying to start\n");
}
else {
*exit_status = send_signal(otherpid, SIGHUP);
return 1;
}
}
if (!strcmp(dash_k_arg, "graceful")) {
if (!running) {
printf("httpd not running, trying to start\n");
}
else {
*exit_status = send_signal(otherpid, SIGUSR1);
return 1;
}
}
return 0;
}
void ap_mpm_rewrite_args(process_rec *process)
{
apr_array_header_t *mpm_new_argv;
apr_status_t rv;
apr_getopt_t *opt;
char optbuf[3];
const char *optarg;
int fixed_args;
mpm_new_argv = apr_array_make(process->pool, process->argc,
sizeof(const char **));
*(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
fixed_args = mpm_new_argv->nelts;
apr_getopt_init(&opt, process->pool, process->argc, process->argv);
opt->errfn = NULL;
optbuf[0] = '-';
/* option char returned by apr_getopt() will be stored in optbuf[1] */
optbuf[2] = '\0';
while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
optbuf + 1, &optarg)) == APR_SUCCESS) {
switch(optbuf[1]) {
case 'k':
if (!dash_k_arg) {
if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
!strcmp(optarg, "restart") || !strcmp(optarg, "graceful")) {
dash_k_arg = optarg;
break;
}
}
default:
*(const char **)apr_array_push(mpm_new_argv) =
apr_pstrdup(process->pool, optbuf);
if (optarg) {
*(const char **)apr_array_push(mpm_new_argv) = optarg;
}
}
}
/* back up to capture the bad argument */
if (rv == APR_BADCH || rv == APR_BADARG) {
opt->ind--;
}
while (opt->ind < opt->argc) {
*(const char **)apr_array_push(mpm_new_argv) =
apr_pstrdup(process->pool, opt->argv[opt->ind++]);
}
process->argc = mpm_new_argv->nelts;
process->argv = (const char * const *)mpm_new_argv->elts;
if (dash_k_arg) {
APR_REGISTER_OPTIONAL_FN(ap_signal_server);
}
}
#endif /* AP_MPM_WANT_SIGNAL_SERVER */