mpm_common.c revision faf8593cc6d1c8e598ca434f7e88effa74f19e2f
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny/* ====================================================================
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * The Apache Software License, Version 1.1
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * reserved.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Redistribution and use in source and binary forms, with or without
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * modification, are permitted provided that the following conditions
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * are met:
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * 1. Redistributions of source code must retain the above copyright
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * notice, this list of conditions and the following disclaimer.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * 2. Redistributions in binary form must reproduce the above copyright
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * notice, this list of conditions and the following disclaimer in
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * the documentation and/or other materials provided with the
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * distribution.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * 3. The end-user documentation included with the redistribution,
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * if any, must include the following acknowledgment:
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * "This product includes software developed by the
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Apache Software Foundation (http://www.apache.org/)."
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Alternately, this acknowledgment may appear in the software itself,
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * if and wherever such third-party acknowledgments normally appear.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * 4. The names "Apache" and "Apache Software Foundation" must
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * not be used to endorse or promote products derived from this
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * software without prior written permission. For written
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * permission, please contact apache@apache.org.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * 5. Products derived from this software may not be called "Apache",
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * nor may "Apache" appear in their name, without prior written
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * permission of the Apache Software Foundation.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek *
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * SUCH DAMAGE.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * ====================================================================
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * This software consists of voluntary contributions made by many
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * individuals on behalf of the Apache Software Foundation. For more
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * information on the Apache Software Foundation, please see
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * <http://www.apache.org/>.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Portions of this software are based upon public domain software
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * originally written at the National Center for Supercomputing Applications,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * University of Illinois, Urbana-Champaign.
bdbf4f169e4d5d00b0616df19f7a55debb407f78Pavel Březina */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny/* The purpose of this file is to store the code that MOST mpm's will need
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * this does not mean a function only goes into this file if every MPM needs
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * it. It means that if a function is needed by more than one MPM, and
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * future maintenance would be served by making the code common, then the
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * function belongs here.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek *
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * This is going in src/main because it is not platform specific, it is
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * specific to multi-process servers, but NOT to Unix. Which is why it
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * does not belong in src/os/unix
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "apr.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "apr_thread_proc.h"
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#include "apr_signal.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "apr_strings.h"
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#define APR_WANT_STRFUNC
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#include "apr_want.h"
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#include "httpd.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "http_config.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "http_log.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "http_main.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "mpm.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "mpm_common.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "ap_mpm.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "ap_listen.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "mpm_default.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#ifdef AP_MPM_WANT_SET_SCOREBOARD
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include "scoreboard.h"
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#endif
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
04e870d99e72aa3160bdb6ab05d986fb4005c3edPavel Březina#ifdef HAVE_PWD_H
04e870d99e72aa3160bdb6ab05d986fb4005c3edPavel Březina#include <pwd.h>
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#endif
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#ifdef HAVE_GRP_H
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#include <grp.h>
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#endif
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#ifdef AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
ef2455b63380ecd17bea94270ceaabe15dcf6456Jakub Hrozekvoid ap_reclaim_child_processes(int terminate)
ef2455b63380ecd17bea94270ceaabe15dcf6456Jakub Hrozek{
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny int i;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny long int waittime = 1024 * 16; /* in usecs */
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny apr_status_t waitret;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny int tries;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny int not_dead_yet;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny int max_daemons;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov for (tries = terminate ? 4 : 1; tries <= 9; ++tries) {
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny /* don't want to hold up progress any more than
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * necessary, but we need to allow children a few moments to exit.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Set delay with an exponential backoff.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny */
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny apr_sleep(waittime);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny waittime = waittime * 4;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* now see who is done */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek not_dead_yet = 0;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek for (i = 0; i < max_daemons; ++i) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek pid_t pid = MPM_CHILD_PID(i);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek apr_proc_t proc;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (pid == 0)
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek continue;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek proc.pid = pid;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek waitret = apr_proc_wait(&proc, NULL, NULL, APR_NOWAIT);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (waitret != APR_CHILD_NOTDONE) {
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose MPM_NOTE_CHILD_KILLED(i);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek continue;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek }
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek ++not_dead_yet;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek switch (tries) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 1: /* 16ms */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 2: /* 82ms */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 3: /* 344ms */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 4: /* 16ms */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek break;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 5: /* 82ms */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 6: /* 344ms */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 7: /* 1.4sec */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* ok, now it's being annoying */
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny 0, ap_server_conf,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek "child process %ld still did not exit, "
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek "sending a SIGTERM",
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek (long)pid);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek kill(pid, SIGTERM);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek break;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 8: /* 6 sec */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* die child scum */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek 0, ap_server_conf,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "child process %ld still did not exit, "
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek "sending a SIGKILL",
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek (long)pid);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#ifndef BEOS
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek kill(pid, SIGKILL);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#else
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* sending a SIGKILL kills the entire team on BeOS, and as
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * httpd thread is part of that team it removes any chance
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * of ever doing a restart. To counter this I'm changing to
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * use a kinder, gentler way of killing a specific thread
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * that is just as effective.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov kill_thread(pid);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#endif
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek break;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek case 9: /* 14 sec */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* gave it our best shot, but alas... If this really
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * is a child we are trying to kill and it really hasn't
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * exited, we will likely fail to bind to the port
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * after the restart.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek 0, ap_server_conf,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek "could not make child process %ld exit, "
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek "attempting to continue anyway",
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek (long)pid);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek break;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek }
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek }
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek#if APR_HAS_OTHER_CHILD
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek apr_proc_other_child_check();
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#endif
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (!not_dead_yet) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* nothing left to wait for */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek break;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek }
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek }
ef2455b63380ecd17bea94270ceaabe15dcf6456Jakub Hrozek}
b6d5f2a91fbce15c7ef4d382fa6b52407adb26ddPavel Březina#endif /* AP_MPM_WANT_RECLAIM_CHILD_PROCESSES */
b6d5f2a91fbce15c7ef4d382fa6b52407adb26ddPavel Březina
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#ifdef AP_MPM_WANT_WAIT_OR_TIMEOUT
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek/* number of calls to wait_or_timeout between writable probes */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#ifndef INTERVAL_OF_WRITABLE_PROBES
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#define INTERVAL_OF_WRITABLE_PROBES 10
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek#endif
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozekstatic int wait_or_timeout_counter;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bosevoid ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek apr_pool_t *p)
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek{
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek apr_status_t rv;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek ++wait_or_timeout_counter;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek wait_or_timeout_counter = 0;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek }
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (APR_STATUS_IS_EINTR(rv)) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek ret->pid = -1;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek return;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek }
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (APR_STATUS_IS_CHILD_DONE(rv)) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek return;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny }
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#ifdef NEED_WAITPID
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny if ((ret = reap_children(exitcode, status)) > 0) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek return;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek }
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#endif
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny ret->pid = -1;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek return;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny}
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#endif /* AP_MPM_WANT_WAIT_OR_TIMEOUT */
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#ifdef AP_MPM_WANT_PROCESS_CHILD_STATUS
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zelenyint ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny{
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek int signum = status;
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek const char *sigdesc = apr_signal_get_description(signum);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* Child died... if it died due to a fatal error,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * we should simply bail out. The caller needs to
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek * check for bad rc from us and exit, running any
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * appropriate cleanups.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek *
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * If the child died due to a resource shortage,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * the parent should limit the rate of forking
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose */
bdbf4f169e4d5d00b0616df19f7a55debb407f78Pavel Březina if (APR_PROC_CHECK_EXIT(why)) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (status == APEXIT_CHILDSICK) {
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny return status;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny }
ef2455b63380ecd17bea94270ceaabe15dcf6456Jakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (status == APEXIT_CHILDFATAL) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek 0, ap_server_conf,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek "Child %" APR_PID_T_FMT
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek " returned a Fatal error..." APR_EOL_STR
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny "Apache is exiting!",
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny pid->pid);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny return APEXIT_CHILDFATAL;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny }
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny return 0;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny }
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (APR_PROC_CHECK_SIGNALED(why)) {
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny switch (signum) {
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny case SIGTERM:
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny case SIGHUP:
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce case AP_SIG_GRACEFUL:
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce case SIGKILL:
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce break;
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce
aac3ca699a09090072ae4d68bdda8dec990ae393Sumit Bose default:
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov if (APR_PROC_CHECK_CORE_DUMP(why)) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
aac3ca699a09090072ae4d68bdda8dec990ae393Sumit Bose 0, ap_server_conf,
aac3ca699a09090072ae4d68bdda8dec990ae393Sumit Bose "child pid %ld exit signal %s (%d), "
aac3ca699a09090072ae4d68bdda8dec990ae393Sumit Bose "possible coredump in %s",
aac3ca699a09090072ae4d68bdda8dec990ae393Sumit Bose (long)pid->pid, sigdesc, signum,
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce ap_coredump_dir);
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce }
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce else {
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce 0, ap_server_conf,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov "child pid %ld exit signal %s (%d)",
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce (long)pid->pid, sigdesc, signum);
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce }
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce }
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny }
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny return 0;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny}
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#endif /* AP_MPM_WANT_PROCESS_CHILD_STATUS */
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF) && !defined(WIN32)
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zelenyvoid ap_sock_disable_nagle(apr_socket_t *s)
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny{
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny /* The Nagle algorithm says that we should delay sending partial
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * packets in hopes of getting more data. We don't want to do
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * this; we are not telnet. There are bad interactions between
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * persistent connections and Nagle's algorithm that have very severe
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * performance penalties. (Failing to disable Nagle is not much of a
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * problem with simple HTTP.)
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny *
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * In spite of these problems, failure here is not a shooting offense.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny */
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny apr_status_t status = apr_setsocketopt(s, APR_TCP_NODELAY, 1);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny if (status != APR_SUCCESS) {
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny "setsockopt: (TCP_NODELAY)");
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny }
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny}
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#endif
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose#ifdef HAVE_GETPWNAM
99bac83188601c2b07e0b141aac7dc7d882b464aSumit BoseAP_DECLARE(uid_t) ap_uname2id(const char *name)
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose{
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose struct passwd *ent;
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose if (name[0] == '#')
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose return (atoi(&name[1]));
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov if (!(ent = getpwnam(name))) {
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, NULL,
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose "%s: bad user name %s", ap_server_argv0, name);
99bac83188601c2b07e0b141aac7dc7d882b464aSumit Bose exit(1);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny }
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny return (ent->pw_uid);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny}
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#endif
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#ifdef HAVE_GETGRNAM
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan ZelenyAP_DECLARE(gid_t) ap_gname2id(const char *name)
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny{
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny struct group *ent;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny if (name[0] == '#')
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny return (atoi(&name[1]));
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek
877b92e80bde510d5cd9f03dbf01e2bcf73ab072Michal Židek if (!(ent = getgrnam(name))) {
4f118e3e6a25762f40a43e6dbefb09f44adbef32Simo Sorce ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_NOERRNO, 0, NULL,
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce "%s: bad group name %s", ap_server_argv0, name);
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce exit(1);
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce }
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce return (ent->gr_gid);
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce}
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#endif
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#ifndef HAVE_INITGROUPS
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zelenyint initgroups(const char *name, gid_t basegid)
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny{
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32) || defined(NETWARE)
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny/* QNX, MPE and BeOS do not appear to support supplementary groups. */
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny return 0;
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny#else /* ndef QNX */
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose gid_t groups[NGROUPS_MAX];
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek struct group *g;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek int index = 0;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek setgrent();
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose groups[index++] = basegid;
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek if (g->gr_gid != basegid) {
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek char **names;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose for (names = g->gr_mem; *names != NULL; ++names) {
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose if (!strcmp(*names, name))
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose groups[index++] = g->gr_gid;
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose }
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek }
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose }
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek endgrent();
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek return setgroups(index, groups);
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek#endif /* def QNX */
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek}
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek#endif /* def NEED_INITGROUPS */
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek#ifdef AP_MPM_USES_POD
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
909a86af4eb99f5d311d7136cab78dca535ae304Sumit BoseAP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose{
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose apr_status_t rv;
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose *pod = apr_palloc(p, sizeof(**pod));
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose if (rv != APR_SUCCESS) {
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose return rv;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek }
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek apr_file_pipe_timeout_set((*pod)->pod_in, 0);
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose (*pod)->p = p;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose apr_sockaddr_info_get(&(*pod)->sa, ap_listeners->bind_addr->hostname,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov APR_UNSPEC, ap_listeners->bind_addr->port, 0, p);
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose return APR_SUCCESS;
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose}
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose
0528fdec17d0031996e919fcd852459e86592c35Jakub HrozekAP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose{
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose char c;
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose apr_size_t len = 1;
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose apr_status_t rv;
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek rv = apr_file_read(pod->pod_in, &c, &len);
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose if ((rv == APR_SUCCESS) && (len == 1)) {
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose return APR_SUCCESS;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek }
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek if (rv != APR_SUCCESS) {
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek return rv;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek }
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek return AP_NORESTART;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek}
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose
909a86af4eb99f5d311d7136cab78dca535ae304Sumit BoseAP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose{
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov apr_status_t rv;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose rv = apr_file_close(pod->pod_out);
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose if (rv != APR_SUCCESS) {
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose return rv;
0528fdec17d0031996e919fcd852459e86592c35Jakub Hrozek }
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose rv = apr_file_close(pod->pod_in);
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose if (rv != APR_SUCCESS) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return APR_SUCCESS;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek}
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozekstatic apr_status_t pod_signal_internal(ap_pod_t *pod)
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina{
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_status_t rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek char char_of_death = '!';
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_size_t one = 1;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek rv = apr_file_write(pod->pod_out, &char_of_death, &one);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (rv != APR_SUCCESS) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek "write pipe_of_death");
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek}
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek/* This function connects to the server, then immediately closes the connection.
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * This permits the MPM to skip the poll when there is only one listening
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * socket, because it provides a alternate way to unblock an accept() when
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * the pod is used.
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek */
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozekstatic apr_status_t dummy_connection(ap_pod_t *pod)
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek{
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_status_t rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_socket_t *sock;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_pool_t *p;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek /* create a temporary pool for the socket. pconf stays around too long */
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek rv = apr_pool_create(&p, pod->p);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (rv != APR_SUCCESS) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek rv = apr_socket_create(&sock, pod->sa->family, SOCK_STREAM, p);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (rv != APR_SUCCESS) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek "get socket to connect to listener");
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek /* on some platforms (e.g., FreeBSD), the kernel won't accept many
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * queued connections before it starts blocking local connects...
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * we need to keep from blocking too long and instead return an error,
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * because the MPM won't want to hold up a graceful restart for a
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * long time
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek */
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek rv = apr_setsocketopt(sock, APR_SO_TIMEOUT, 3 * APR_USEC_PER_SEC);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (rv != APR_SUCCESS) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek "set timeout on socket to connect to listener");
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_socket_close(sock);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek rv = apr_connect(sock, pod->sa);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (rv != APR_SUCCESS) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek int log_level = APLOG_WARNING;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (APR_STATUS_IS_TIMEUP(rv)) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek /* probably some server processes bailed out already and there
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * is nobody around to call accept and clear out the kernel
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * connection queue; usually this is not worth logging
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek */
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek log_level = APLOG_DEBUG;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek "connect to listener");
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_socket_close(sock);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_pool_destroy(p);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek}
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub HrozekAP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek{
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek apr_status_t rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek rv = pod_signal_internal(pod);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (rv != APR_SUCCESS) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return rv;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina return dummy_connection(pod);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek}
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozekvoid ap_mpm_pod_killpg(ap_pod_t *pod, int num)
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek{
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina int i;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina apr_status_t rv = APR_SUCCESS;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* we don't write anything to the pod here... we assume
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina * that the would-be reader of the pod has another way to
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * see that it is time to die once we wake it up
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek *
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina * writing lots of things to the pod at once is very
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina * problematic... we can fill the kernel pipe buffer and
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina * be blocked until somebody consumes some bytes or
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina * we hit a timeout... if we hit a timeout we can't just
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina * keep trying because maybe we'll never successfully
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * write again... but then maybe we'll leave would-be
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * readers stranded (a number of them could be tied up for
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek * a while serving time-consuming requests)
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek */
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek for (i = 0; i < num && rv == APR_SUCCESS; i++) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek rv = dummy_connection(pod);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek}
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek#endif /* #ifdef AP_MPM_USES_POD */
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek/* standard mpm configuration handling */
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek#ifdef AP_MPM_WANT_SET_PIDFILE
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozekconst char *ap_pid_fname = NULL;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březinaconst char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina const char *arg)
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek{
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (err != NULL) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return err;
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek if (cmd->server->is_virtual) {
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek return "PidFile directive not allowed in <VirtualHost>";
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
7caf7ed4f2eae1ec1c0717b4ee6ce78bdacd5926Jakub Hrozek 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