mpm_winnt.c revision 0375f54d32af9c27de0006956d9f85d01b020351
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* ====================================================================
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The Apache Software License, Version 1.1
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Copyright (c) 2000 The Apache Software Foundation. All rights
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * reserved.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Redistribution and use in source and binary forms, with or without
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * modification, are permitted provided that the following conditions
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * are met:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 1. Redistributions of source code must retain the above copyright
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * notice, this list of conditions and the following disclaimer.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 2. Redistributions in binary form must reproduce the above copyright
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * notice, this list of conditions and the following disclaimer in
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * the documentation and/or other materials provided with the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * distribution.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 3. The end-user documentation included with the redistribution,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if any, must include the following acknowledgment:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * "This product includes software developed by the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Apache Software Foundation (http://www.apache.org/)."
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Alternately, this acknowledgment may appear in the software itself,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * if and wherever such third-party acknowledgments normally appear.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 4. The names "Apache" and "Apache Software Foundation" must
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * not be used to endorse or promote products derived from this
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * software without prior written permission. For written
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * permission, please contact apache@apache.org.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * 5. Products derived from this software may not be called "Apache",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * nor may "Apache" appear in their name, without prior written
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * permission of the Apache Software Foundation.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * SUCH DAMAGE.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * ====================================================================
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * This software consists of voluntary contributions made by many
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * individuals on behalf of the Apache Software Foundation. For more
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * information on the Apache Software Foundation, please see
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Portions of this software are based upon public domain software
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * originally written at the National Center for Supercomputing Applications,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * University of Illinois, Urbana-Champaign.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Definitions of WINNT MPM specific config globals
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int workers_may_exit = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int shutdown_in_progress = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic unsigned int g_blocked_threads = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic HANDLE shutdown_event; /* used to signal shutdown to parent */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic HANDLE restart_event; /* used to signal a restart to parent */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#define MAX_SIGNAL_NAME 30 /* Long enough for apPID_shutdown, where PID is an int */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzstatic int num_listenfds = 0;
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzstatic apr_pool_t *pconf; /* Pool for config stuff */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int one_process = 0;
4f9c22c4f27571d54197be9674e1fc0d528192aestrikerstatic char const* signal_arg;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/* ap_get_max_daemons and ap_my_generation are used by the scoreboard
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewroweap_generation_t volatile ap_my_generation=0; /* Used by the scoreboard */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/* This is the helper code to resolve late bound entry points
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * missing from one or more releases of the Win32 API...
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * but it sure would be nice if we didn't duplicate this code
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * from the APR ;-)
3e392a5afd51526de3cb15d57ee46d8cb160ae65gregamesFARPROC ap_load_dll_func(ap_dlltoken_e fnLib, char* fnName, int ordinal)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe lateDllHandle[fnLib] = LoadLibrary(lateDllName[fnLib]);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz return GetProcAddress(lateDllHandle[fnLib], (char *) ordinal);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe/* A bunch or routines from os/win32/multithread.c that need to be merged into APR
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * or thrown out entirely...
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return(CreateSemaphore(NULL, initial, 1000000, NULL));
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe/* To share the semaphores with other processes, we need a NULL ACL
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * Code from MS KB Q106387
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe sa = (PSECURITY_ATTRIBUTES) LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES));
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE)
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz LocalFree( ((PSECURITY_ATTRIBUTES)sa)->lpSecurityDescriptor);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * The Win32 call WaitForMultipleObjects will only allow you to wait for
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * a maximum of MAXIMUM_WAIT_OBJECTS (current 64). Since the threading
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * model in the multithreaded version of apache wants to use this call,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * we are restricted to a maximum of 64 threads. This is a simplistic
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * routine that will increase this size.
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantzstatic DWORD wait_for_many_objects(DWORD nCount, CONST HANDLE *lpHandles,
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz for (dwIndex = 0; dwIndex * MAXIMUM_WAIT_OBJECTS < nCount; dwIndex++) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe min(MAXIMUM_WAIT_OBJECTS, nCount - (dwIndex * MAXIMUM_WAIT_OBJECTS)),
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe } while((time(NULL) < tStopTime) && (dwRet == WAIT_TIMEOUT));
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Signalling Apache on NT.
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Under Unix, Apache can be told to shutdown or restart by sending various
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * signals (HUP, USR, TERM). On NT we don't have easy access to signals, so
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * we use "events" instead. The parent apache process goes into a loop
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * where it waits forever for a set of events. Two of those events are
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * apPID_shutdown
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * apPID_restart
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * (where PID is the PID of the apache parent process). When one of these
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * is signalled, the Apache parent performs the appropriate action. The events
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * can become signalled through internal Apache methods (e.g. if the child
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * finds a fatal error and needs to kill its parent), via the service
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * control manager (the control thread will signal the shutdown event when
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * requested to stop the Apache service), from the -k Apache command line,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * or from any external program which finds the Apache PID from the
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * httpd.pid file.
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * The signal_parent() function, below, is used to signal one of these events.
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * It can be called by any child or parent process, since it does not
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * rely on global variables.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * On entry, type gives the event to signal. 0 means shutdown, 1 means
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * graceful restart.
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* after updating the shutdown_pending or restart flags, we need
a2b181763cb35fd899feb4a436aeadaa80bf91eabrianp * to wake up the parent process so it can see the changes. The
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * parent will normally be waiting for either a child process
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * to die, or for a signal on the "spache-signal" event. So set the
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * "apache-signal" event here.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker default: return;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Um, problem, can't signal the parent, which means we can't
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * signal ourselves to die. Ignore for now...
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
7a2edaa0193cbb0d79a65a8461a609a9402aea49brianp if (SetEvent(e) == 0) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* Same problem as above */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowestatic int volatile is_graceful = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Initialise the signal names, in the global variables signal_name_prefix,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * signal_restart_name and signal_shutdown_name.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_snprintf(signal_name_prefix, sizeof(signal_name_prefix), prefix);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_snprintf(signal_shutdown_name, sizeof(signal_shutdown_name),
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_snprintf(signal_restart_name, sizeof(signal_restart_name),
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * Routines that deal with sockets, some are WIN32 specific...
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void sock_disable_nagle(int s)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* The Nagle algorithm says that we should delay sending partial
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * packets in hopes of getting more data. We don't want to do
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * this; we are not telnet. There are bad interactions between
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * persistent connections and Nagle's algorithm that have very severe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * performance penalties. (Failing to disable Nagle is not much of a
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * problem with simple HTTP.)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * In spite of these problems, failure here is not a shooting offense.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *) &just_say_no,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe sizeof(int)) < 0) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, server_conf,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe "setsockopt: (TCP_NODELAY)");
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * Routines to deal with managing the list of listening sockets.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowestatic apr_inline ap_listen_rec *find_ready_listener(fd_set * main_fds)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* Setup the listeners */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar if (listenmaxfd == INVALID_SOCKET || nsd > listenmaxfd) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Setup the listeners */
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Set up a default listener if necessary */
8aefbd756763807188d2e3ce336a8680e4893066wrowe lr = apr_palloc(s->process->pool, sizeof(ap_listen_rec));
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Open the pipe to the parent process to receive the inherited socket
2fa5b5878e7567e2875807c3e2a2b3b0d3ef74bewrowe * data. The sockets have been set to listening in the parent process.
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (!ReadFile(pipe, &WSAProtocolInfo, sizeof(WSAPROTOCOL_INFO),
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
a8d11d78181478da6a672f7fbc58b8d523351f49wrowe "setup_inherited_listeners: Unable to read socket data from parent");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
23c6309e36a63b13b61c35999c978017521993d6wrowe "Child %d: setup_inherited_listener() read = %d bytes of WSAProtocolInfo.", my_pid);
8aefbd756763807188d2e3ce336a8680e4893066wrowe nsd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_netos_error(), server_conf,
23c6309e36a63b13b61c35999c978017521993d6wrowe "Child %d: setup_inherited_listeners(), WSASocket failed to open the inherited socket.", my_pid);
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (nsd >= 0) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (listenmaxfd == INVALID_SOCKET || nsd > listenmaxfd) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Now, read the AcceptExCompPort from the parent */
a8d11d78181478da6a672f7fbc58b8d523351f49wrowe ReadFile(pipe, &AcceptExCompPort, sizeof(AcceptExCompPort), &BytesRead, (LPOVERLAPPED) NULL);
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* Associate the open listeners with the completion port.
cf6ef072483172309861d06e85b1aeff4573c060wrowe * Bypass the operation for Windows 95/98
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe CreateIoCompletionPort((HANDLE) nsd, AcceptExCompPort, 0, 0);
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick/**********************************************************************
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * Multithreaded implementation
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * This code is fairly specific to Win32.
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * The model used to handle requests is a set of threads. One "main"
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * thread listens for new requests. When something becomes
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * available, it does a select and places the newly available socket
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * onto a list of "jobs" (add_job()). Then any one of a fixed number
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * of "worker" threads takes the top job off the job list with
d75626f0952c6152a99acd013a4f127d46f0f9edtrawick * remove_job() and handles that connection to completion. After
cf6ef072483172309861d06e85b1aeff4573c060wrowe * the connection has finished the thread is free to take another
cf6ef072483172309861d06e85b1aeff4573c060wrowe * job from the job list.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * In the code, the "main" thread is running within the child_main()
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * function. The first thing this function does is create the
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * worker threads, which operate in the child_sub_main() function. The
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * main thread then goes into a loop within child_main() where they
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * do a select() on the listening sockets. The select times out once
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * per second so that the thread can check for an "exit" signal
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * from the parent process (see below). If this signal is set, the
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * thread can exit, but only after it has accepted all incoming
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * connections already in the listen queue (since Win32 appears
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * to through away listened but unaccepted connections when a
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * process dies).
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * Because the main and worker threads exist within a single process
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * they are vulnerable to crashes or memory leaks (crashes can also
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * be caused within modules, of course). There also needs to be a
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * mechanism to perform restarts and shutdowns. This is done by
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * creating the main & worker threads within a subprocess. A
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * main process (the "parent process") creates one (or more)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * processes to do the work, then the parent sits around waiting
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * for the working process to die, in which case it starts a new
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * one. The parent process also handles restarts (by creating
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * a new working process then signalling the previous working process
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * exit ) and shutdowns (by signalling the working process to exit).
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * The parent process operates within the master_main() function. This
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * process also handles requests from the service manager (NT only).
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * Signalling between the parent and working process uses a Win32
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * event. Each child has a unique name for the event, which is
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * passed to it with the -Z argument when the child is spawned. The
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * parent sets (signals) this event to tell the child to die.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * At present all children do a graceful die - they finish all
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * current jobs _and_ empty the listen queue before they exit.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * A non-graceful die would need a second event. The -Z argument in
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * the child is also used to create the shutdown and restart events,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * since the prefix (apPID) contains the parent process PID.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * The code below starts with functions at the lowest level -
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * worker threads, and works up to the top level - the main()
8aefbd756763807188d2e3ce336a8680e4893066wrowe * function of the parent process.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * The scoreboard (in process memory) contains details of the worker
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * threads (within the active working process). There is no shared
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * "scoreboard" between processes, since only one is ever active
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * at once (or at most, two, when one has been told to shutdown but
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * is processes outstanding requests, and a new one has been started).
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * This is controlled by a "start_mutex" which ensures only one working
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * process is active at once.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe **********************************************************************/
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Definition of jobs, shared by main and worker threads.
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantztypedef struct joblist_s {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * Globals common to main and worker threads. This structure is not
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * used by the parent process.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowetypedef struct globals_s {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe/* Windows 9x specific code...
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * Accept processing for on Windows 95/98 uses a producer/consumer queue
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * model. A single thread accepts connections and queues the accepted socket
59513b1275fdc2021d4949ee03ae8229469abb86wrowe * to the accept queue for consumption by a pool of worker threads.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * win9x_get_connection()
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * Calls remove_job() to pull a job from the accept queue. All the worker
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * threads block on remove_job.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * accept_and_queue_connections()
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * The accept threads runs this function, which accepts connections off
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * the network and calls add_job() to queue jobs to the accept_queue.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * add_job()/remove_job()
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * Add or remove an accepted socket from the list of sockets
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * connected to clients. allowed_globals.jobmutex protects
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe * against multiple concurrent access to the linked list of jobs.
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "Ouch! Out of memory in add_job()!");
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowestatic int remove_job(void)
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (shutdown_in_progress && !allowed_globals.jobhead) {
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe return (-1);
1b315ee865b0f11e582beb64127ca3a99a319d2fwrowe if (ap_max_requests_per_child && (requests_this_child > ap_max_requests_per_child)) {
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz rc = select(listenmaxfd + 1, &main_fds, NULL, NULL, &tv);
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (rc == 0 || (rc == SOCKET_ERROR && APR_STATUS_IS_EINTR(apr_get_netos_error()))) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* A "real" error occurred, log it and increment the count of
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * select errors. This count is used to ensure we don't go into
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * a busy loop of continuous errors.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_INFO, apr_get_netos_error(), server_conf,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "select failed with error %d", apr_get_netos_error());
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "Too many errors in select loop. Child process exiting.");
548b2980e83f609186a76e98fb245d02e8547bc3jerenkrantz /* fetch the native socket descriptor */
8aefbd756763807188d2e3ce336a8680e4893066wrowe csd = accept(nsd, (struct sockaddr *) &sa_client, &clen);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe } while (csd < 0 && APR_STATUS_IS_EINTR(apr_get_netos_error()));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (csd < 0) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (APR_STATUS_IS_ECONNABORTED(apr_get_netos_error())) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
8aefbd756763807188d2e3ce336a8680e4893066wrowe "accept: (client socket)");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowestatic PCOMP_CONTEXT win9x_get_connection(PCOMP_CONTEXT context)
f08810eff40a2bddd2bc0103453c4ae775ea62bewrowe /* allocate the completion context and the transaction pool */
f08810eff40a2bddd2bc0103453c4ae775ea62bewrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "win9x_get_connection: apr_pcalloc() failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe while (1) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->sa_server = apr_palloc(context->ptrans, len);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "getsockname failed");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->sa_client = apr_palloc(context->ptrans, len);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "getpeername failed");
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar memset(&context->sa_client, '\0', sizeof(context->sa_client));
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* do we NEED_DUPPED_CSD ?? */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Windows 2000/NT specific code...
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * create_acceptex_context()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * reset_acceptex_context()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * drain_acceptex_complport()
8aefbd756763807188d2e3ce336a8680e4893066wrowe * winnt_get_connection()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * TODO: Insert a discussion of 'completion contexts' and what these function do here...
8aefbd756763807188d2e3ce336a8680e4893066wrowestatic void drain_acceptex_complport(HANDLE hComplPort, BOOLEAN bCleanUp)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe while (1) {
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz rc = GetQueuedCompletionStatus(hComplPort, &BytesRead, &CompKey,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if (rc == APR_FROM_OS_ERROR(ERROR_OPERATION_ABORTED)) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "Child %d: Draining an ABORTED packet off "
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "Child %d: Draining and discarding an active connection "
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* It is only valid to clean-up in the process that initiated the I/O */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowestatic int create_acceptex_context(apr_pool_t *_pconf, ap_listen_rec *lr)
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* allocate the completion context */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "create_acceptex_context: apr_pcalloc() failed. Process will exit.");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* initialize the completion context */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe context->Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "create_acceptex_context: CreateEvent() failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* create and initialize the accept socket */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar context->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_netos_error(), server_conf,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar "create_acceptex_context: socket() failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.");
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /* Not a failure condition. Keep running. */
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* recv_buf must be large enough to hold the remote and local
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * addresses. Note that recv_buf_size is the amount of recv_buf
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * available for AcceptEx to receive bytes into. Since we
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe * don't want AcceptEx to do a recv, set the size to 0.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker context->recv_buf = apr_pcalloc(_pconf, 2*PADDED_ADDR_SIZE);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz /* AcceptEx on the completion context. The completion context will be signaled
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe * when a connection is accepted. */
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe if (lasterror != APR_FROM_OS_ERROR(ERROR_IO_PENDING)) {
2787ef5edc27fa4f6777ba8d51aa48fd9fdf54bbwrowe ap_log_error(APLOG_MARK,APLOG_ERR, lasterror, server_conf,
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "create_acceptex_context: AcceptEx failed. Process will exit.");
8aefbd756763807188d2e3ce336a8680e4893066wrowestatic apr_inline apr_status_t reset_acceptex_context(PCOMP_CONTEXT context)
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* reset the buffer pools */
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* recreate and initialize the accept socket if it is not being reused */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* AcceptEx on the completion context. The completion context will be signaled
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * when a connection is accepted. Hack Alert: TransmitFile, under certain
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz * circumstances, can 'recycle' accept sockets, saving the overhead of calling
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * socket(). Occasionally this fails (usually when the client closes his end
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz * of the connection early). When this occurs, AcceptEx will fail with 10022,
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz * Invalid Parameter. When this occurs, just open a fresh accept socket and
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * retry the call.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe for (i=0; i<2; i++) {
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz context->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
0e58e92812f2f679d6bf2ff66cbcfa6c1d1e14bbjerenkrantz ap_log_error(APLOG_MARK,APLOG_ERR, rc, server_conf,
8aefbd756763807188d2e3ce336a8680e4893066wrowe "reset_acceptex_context: socket() failed. Process will exit.");
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar SO_UPDATE_ACCEPT_CONTEXT, (char *)&nsd, sizeof(nsd))) {
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(),
8aefbd756763807188d2e3ce336a8680e4893066wrowe "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.");
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, rc, server_conf,
cf6ef072483172309861d06e85b1aeff4573c060wrowe "reset_acceptex_context: AcceptEx failed for "
cf6ef072483172309861d06e85b1aeff4573c060wrowe "listening socket: %d and accept socket: %d. Getting a new accept socket.",
c2cf53a40a9814eb91db2cdf820f97d943f21628coarstatic PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context)
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Clean-up the AcceptEx completion context */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Prepare the completion context for reuse */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe if ((rc = reset_acceptex_context(context)) != APR_SUCCESS) {
69adb3d949e3dd17c0492a01fc2cf298832c7eefwrowe "Child %d: winnt_get_connection: reset_acceptex_context failed.",
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Probably should just die now... */
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* May need to atomize the workers_may_exit check with the
cadddb2c31d24d48f4017db4df0a29687432326cwrowe * g_blocked_threads++ */
8aefbd756763807188d2e3ce336a8680e4893066wrowe while (1) {
cf6ef072483172309861d06e85b1aeff4573c060wrowe rc = GetQueuedCompletionStatus(AcceptExCompPort, &BytesRead, &CompKey,
cf6ef072483172309861d06e85b1aeff4573c060wrowe if (rc != APR_FROM_OS_ERROR(ERROR_OPERATION_ABORTED)) {
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick /* Is this a deadly condition?
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * We sometimes get ERROR_NETNAME_DELETED when a client
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * disconnects when attempting to reuse sockets. Not sure why
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * we see this now and not during AcceptEx(). Reset the
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * AcceptEx context and continue...
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe "Child %d: - GetQueuedCompletionStatus() failed",
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Reset the completion context */
2d399cd7535887fceaa9f8f116eb98ce68ddd602trawick if ((rc = reset_acceptex_context(context)) != APR_SUCCESS) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, rc, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Child %d: winnt_get_connection: reset_acceptex_context failed.",
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Probably should just die now... */
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* Sometimes we catch ERROR_OPERATION_ABORTED completion packets
cf6ef072483172309861d06e85b1aeff4573c060wrowe * from the old child process (during a restart). Ignore them.
cf6ef072483172309861d06e85b1aeff4573c060wrowe "Child %d: - Draining ERROR_OPERATION_ABORTED packet off "
cf6ef072483172309861d06e85b1aeff4573c060wrowe /* CompKey == my_pid means this thread was unblocked by
cf6ef072483172309861d06e85b1aeff4573c060wrowe * the shutdown code (not by io completion).
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Sometimes we catch shutdown io completion packets
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * posted by the old child process (during a restart). Ignore them.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Check to see if we need to create more completion contexts,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * but only if we are not in the process of shutting down
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe /* Received a connection */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * worker_main() - this is the main loop for the worker threads
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * Windows 95/98
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * Each thread runs within this function. They wait within remove_job()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * for a job to become available, then handle all the requests on that
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * connection until it is closed, then return to remove_job().
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * The worker thread will exit when it removes a job which contains
8aefbd756763807188d2e3ce336a8680e4893066wrowe * socket number -1. This provides a graceful thread exit, since
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * it will never exit during a connection.
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * This code in this function is basically equivalent to the child_main()
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * from the multi-process (Unix) environment, except that we
e4a3f3c2f080cac75a15a6454cca429b8161c050wrowe * - do not call child_init_modules (child init API phase)
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * - block in remove_job, and when unblocked we have an already
8aefbd756763807188d2e3ce336a8680e4893066wrowe * accepted socket, instead of blocking on a mutex or select().
8aefbd756763807188d2e3ce336a8680e4893066wrowe while (1) {
8aefbd756763807188d2e3ce336a8680e4893066wrowe /* Grab a connection off the network */
8aefbd756763807188d2e3ce336a8680e4893066wrowe if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_make_os_sock(&context->sock, &sockinfo, context->ptrans);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe c = ap_new_connection(context->ptrans, server_conf, context->sock,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar apr_getsocketopt(context->sock, APR_SO_DISCONNECTED, &disconnected);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_update_child_status(0, thread_num, SERVER_DEAD, (request_rec *) NULL);
8aefbd756763807188d2e3ce336a8680e4893066wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* TODO: Add code to clean-up completion contexts here */
948096a99010fccf648814fecf38f75c689172d7wrowestatic void cleanup_thread(thread *handles, int *thread_cnt, int thread_to_clean)
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe for (i = thread_to_clean; i < ((*thread_cnt) - 1); i++)
948096a99010fccf648814fecf38f75c689172d7wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
948096a99010fccf648814fecf38f75c689172d7wrowe "Unable to create an AcceptEx completion context -- process will exit");
948096a99010fccf648814fecf38f75c689172d7wrowe * child_main() runs the main control thread for the child process.
948096a99010fccf648814fecf38f75c689172d7wrowe * The control thread:
948096a99010fccf648814fecf38f75c689172d7wrowe * - sets up the worker thread pool
948096a99010fccf648814fecf38f75c689172d7wrowe * - starts the accept thread (Win 9x)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * - creates AcceptEx contexts (Win NT)
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * - waits for exit_event, maintenance_event or maintenance timeout
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * and does the right thing depending on which event is received.
948096a99010fccf648814fecf38f75c689172d7wrowestatic void child_main()
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* Set up the scoreboard. The scoreboard in this MPM only applies to the
948096a99010fccf648814fecf38f75c689172d7wrowe * child process and is not shared across processes
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* This is the child process or we are running in single process
0540a0b469147b52e858587270dba31c2aaa9e09wrowe exit_event_name = apr_psprintf(pconf, "apC%d", my_pid);
948096a99010fccf648814fecf38f75c689172d7wrowe setup_signal_names(apr_psprintf(pconf,"ap%d", parent_pid));
948096a99010fccf648814fecf38f75c689172d7wrowe /* Single process mode */
948096a99010fccf648814fecf38f75c689172d7wrowe apr_create_lock(&start_mutex,APR_MUTEX, APR_CROSS_PROCESS,signal_name_prefix,pconf);
948096a99010fccf648814fecf38f75c689172d7wrowe exit_event = CreateEvent(NULL, TRUE, FALSE, exit_event_name);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe /* Child process mode */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker apr_child_init_lock(&start_mutex, signal_name_prefix, pconf);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe exit_event = OpenEvent(EVENT_ALL_ACCESS, FALSE, exit_event_name);
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Child %d: exit_event_name = %s", my_pid, exit_event_name);
053497224246c4dbef9af594cacf5c00ed271e6cwrowe /* Initialize the child_events */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker maintenance_event = CreateEvent(NULL, TRUE, FALSE, NULL);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_create_lock(&allowed_globals.jobmutex, APR_MUTEX, APR_INTRAPROCESS, NULL, pchild);
948096a99010fccf648814fecf38f75c689172d7wrowe * Wait until we have permission to start accepting connections.
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * start_mutex is used to ensure that only one child ever
948096a99010fccf648814fecf38f75c689172d7wrowe * goes into the listen/accept loop at once.
948096a99010fccf648814fecf38f75c689172d7wrowe ap_log_error(APLOG_MARK,APLOG_ERR, status, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Child %d: Failed to acquire the start_mutex. Process will exit.", my_pid);
948096a99010fccf648814fecf38f75c689172d7wrowe ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Create the worker thread pool */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Child %d: Starting %d worker threads.", my_pid, nthreads);
948096a99010fccf648814fecf38f75c689172d7wrowe child_handles = (thread) alloca(nthreads * sizeof(int));
0540a0b469147b52e858587270dba31c2aaa9e09wrowe for (i = 0; i < nthreads; i++) {
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_update_child_status(0, i, SERVER_STARTING, (request_rec *) NULL);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar child_handles[i] = (thread) _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) worker_main,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe (void *) i, 0, &tid);
948096a99010fccf648814fecf38f75c689172d7wrowe /* Begin accepting connections */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
948096a99010fccf648814fecf38f75c689172d7wrowe /* Win95/98: Start the accept thread */
948096a99010fccf648814fecf38f75c689172d7wrowe _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) accept_and_queue_connections,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker (void *) i, 0, &tid);
948096a99010fccf648814fecf38f75c689172d7wrowe /* Windows NT/2000: Create AcceptEx completion contexts */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* Wait for one of three events:
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * exit_event:
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * The exit_event is signaled by the parent process to notify
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * the child that it is time to exit.
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * maintenance_event:
948096a99010fccf648814fecf38f75c689172d7wrowe * This event is signaled by the worker thread pool to direct
948096a99010fccf648814fecf38f75c689172d7wrowe * this thread to create more completion contexts.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * TIMEOUT:
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * To do periodic maintenance on the server (check for thread exits,
053497224246c4dbef9af594cacf5c00ed271e6cwrowe * number of completion contexts, etc.)
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar while (1) {
948096a99010fccf648814fecf38f75c689172d7wrowe rv = WaitForMultipleObjects(2, (HANDLE *) child_events, FALSE, INFINITE);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Something serious is wrong */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe "Child %d: WAIT_FAILED -- shutting down server");
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Hey, this cannot happen */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, APR_SUCCESS, server_conf,
0540a0b469147b52e858587270dba31c2aaa9e09wrowe "Child %d: WAIT_TIMEOUT -- shutting down server", my_pid);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb else if (cld == 0) {
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe /* Exit event was signaled */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe "Child %d: Exit event signaled. Child process is ending.", my_pid);
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe /* Child maintenance event signaled */
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
aa047239dedf0d26e8efecfade32e7337f35df19wrowe ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
aa047239dedf0d26e8efecfade32e7337f35df19wrowe "Child %d: Child maintenance event signaled.", my_pid);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Setting is_graceful will close keep-alive connections */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Shutdown the worker threads */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* workers_may_exit = 1; Not used on Win9x */
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz for (i = 0; i < nthreads; i++) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb else { /* Windows NT/2000 */
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * Setting shutdown_in_progress prevents new AcceptEx completion
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * contexts from being queued to the port but allows threads to
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * continue consuming from the port. This gives the server a
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * chance to handle any accepted connections.
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Setting workers_may_exit prevents threads from consumimg from the
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * completion port (especially threads that unblock off of keep-alive
a9a4544168a37b43bd180b3703ccee995f27a80awrowe * connections later on).
a9a4544168a37b43bd180b3703ccee995f27a80awrowe /* Unblock threads blocked on the completion port */
a9a4544168a37b43bd180b3703ccee995f27a80awrowe ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Child %d: %d threads blocked on the completion port", my_pid, g_blocked_threads);
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar for (i=g_blocked_threads; i > 0; i--) {
aa047239dedf0d26e8efecfade32e7337f35df19wrowe PostQueuedCompletionStatus(AcceptExCompPort, 0, my_pid, NULL);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe /* Cancel any remaining pending AcceptEx completion contexts */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Drain the canceled contexts off the port */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Release the start_mutex to let the new process (in the restart
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * scenario) a chance to begin servicing requests
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* Give busy worker threads a chance to service their connections.
c2cf53a40a9814eb91db2cdf820f97d943f21628coar * Kill them off if they take too long
c2cf53a40a9814eb91db2cdf820f97d943f21628coar ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
aa047239dedf0d26e8efecfade32e7337f35df19wrowe "Child %d: Waiting for %d threads to die.", my_pid, nthreads);
aa047239dedf0d26e8efecfade32e7337f35df19wrowe rv = wait_for_many_objects(nthreads, child_handles, end_time - time(NULL));
4f9c22c4f27571d54197be9674e1fc0d528192aestriker for (i = 0; i < nthreads; i++) {
aa047239dedf0d26e8efecfade32e7337f35df19wrowe ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * Spawn a child Apache process. The child process has the command line arguments from
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * argc and argv[], plus a -Z argument giving the name of an event. The child should
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * open and poll or wait on this event. When it is signalled, the child should die.
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * prefix is a prefix string for the event name.
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * The child_num argument on entry contains a serial number for this child (used to create
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * a unique event name). On exit, this number will have been incremented by one, ready
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * for the next call.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * On exit, the value pointed to be *ev will contain the event created
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * to signal the new child process.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The return value is the handle to the child process if successful, else -1. If -1 is
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe * returned the error will already have been logged by ap_log_error().
a9a4544168a37b43bd180b3703ccee995f27a80awrowe/**********************************************************************
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * master_main - this is the parent (main) process. We create a
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * child process to do the work, then sit around waiting for either
dc8692c6c0ca616a09aa12dad005f2ef23baa1a0wrowe * the child to exit, or a restart or exit signal. If the child dies,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * we just respawn a new one. If we have a shutdown or graceful restart,
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * tell the child to die when it is ready. If it is a non-graceful
aa047239dedf0d26e8efecfade32e7337f35df19wrowe * restart, force the child to die immediately.
aa047239dedf0d26e8efecfade32e7337f35df19wrowe **********************************************************************/
aa047239dedf0d26e8efecfade32e7337f35df19wrowe#define MAX_PROCESSES 50 /* must be < MAX_WAIT_OBJECTS-1 */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void cleanup_process(HANDLE *handles, HANDLE *events, int position, int *processes)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int create_process(apr_pool_t *p, HANDLE *handles, HANDLE *events, int *processes)
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe STARTUPINFO si; /* Filled in prior to call to CreateProcess */
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe PROCESS_INFORMATION pi; /* filled in on call to CreateProcess */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* Build the command line. Should look something like this:
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * C:/apache/bin/apache.exe -f ap_server_confname
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe * First, get the path to the executable...
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, ERROR_BAD_PATHNAME, server_conf,
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe "Parent: Path to Apache process too long");
ebe7da316894e2b93b4a905fccd2496d0ed1bc78rbb } else if (rv == 0) {
ebe7da316894e2b93b4a905fccd2496d0ed1bc78rbb ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
ebe7da316894e2b93b4a905fccd2496d0ed1bc78rbb "Parent: GetModuleFileName() returned NULL for current process.");
ebe7da316894e2b93b4a905fccd2496d0ed1bc78rbb return -1;
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe /* Build the command line */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker pCommand = apr_pstrcat(p, pCommand, " \"", server_conf->process->argv[i], "\"", NULL);
e57e920838f31508f1418aa4c25ce55b345b2cebrbb /* Build the environment, since Win9x disrespects the active env */
e57e920838f31508f1418aa4c25ce55b345b2cebrbb pEnvVar = apr_psprintf(p, "AP_PARENT_PID=%i", parent_pid);
e57e920838f31508f1418aa4c25ce55b345b2cebrbb * Win32's CreateProcess call requires that the environment
e57e920838f31508f1418aa4c25ce55b345b2cebrbb * be passed in an environment block, a null terminated block of
e57e920838f31508f1418aa4c25ce55b345b2cebrbb * null terminated strings.
e57e920838f31508f1418aa4c25ce55b345b2cebrbb while (_environ[i]) {
e68544ae924174ca227ede8e2e722cefa00ea0d3wrowe pEnvBlock = (char *)apr_pcalloc(p, iEnvBlockLen + strlen(pEnvVar) + 1);
d2220a04f870f632b8cec1e6713dbb980ed5e386wrowe while (_environ[i]) {
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* Create a pipe to send socket info to the child */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Parent: Unable to create pipe to child process.");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return -1;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Give the read end of the pipe (hPipeRead) to the child as stdin. The
68b29bcadd6c46aecdc9fe14c93555a2238ad2aagregames * parent will write the socket data to the child on this pipe.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar "Parent: Not able to create the child process.");
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * We must close the handles to the new process and its main thread
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * to prevent handle and memory leaks.
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar return -1;
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "Parent: Created child process %d", pi.dwProcessId);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* Create the exit_event, apCchild_pid */
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe kill_event = CreateEvent(&sa, TRUE, FALSE, apr_psprintf(pconf,"apC%d", pi.dwProcessId));
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe "Parent: Could not create exit event for child process");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Assume the child process lives. Update the process and event tables */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar /* We never store the thread's handle, so close it now. */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Run the chain of open sockets. For each socket, duplicate it
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * for the target process then send the WSAPROTOCOL_INFO
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard * (returned by dup socket) to the child */
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard lpWSAProtocolInfo = apr_pcalloc(p, sizeof(WSAPROTOCOL_INFO));
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard "Parent: Duplicating socket %d and sending it to child process %d", nsd, pi.dwProcessId);
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_netos_error(), server_conf,
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard "Parent: WSADuplicateSocket failed for socket %d.", lr->sd );
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return -1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (!WriteFile(hPipeWrite, lpWSAProtocolInfo, (DWORD) sizeof(WSAPROTOCOL_INFO),
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Parent: Unable to write duplicated socket %d to the child.", lr->sd );
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return -1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, APR_SUCCESS, server_conf,
efa1a34b0a7785fc72863eff175b0cfc1ecb0e38wrowe "Parent: BytesWritten = %d WSAProtocolInfo = %x20", BytesWritten, *lpWSAProtocolInfo);
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
117026201e6d8fe7d82416b8a7324830f5a87292wrowe /* Now, send the AcceptEx completion port to the child */
117026201e6d8fe7d82416b8a7324830f5a87292wrowe if (!DuplicateHandle(GetCurrentProcess(), AcceptExCompPort,
117026201e6d8fe7d82416b8a7324830f5a87292wrowe ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
6c24fd6cfe148639988d5b335185ffb215662801wrowe "Parent: Unable to duplicate AcceptEx completion port. Shutting down.");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker WriteFile(hPipeWrite, &hDupedCompPort, (DWORD) sizeof(hDupedCompPort), &BytesWritten, (LPOVERLAPPED) NULL);
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowestatic int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_event)
f888346b48f5e5b5e3f0a47dedb8cefd2759a4e2gregames int remaining_children_to_start = ap_daemons_to_start;
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe int current_live_processes = 0; /* number of child process we know about */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Create child process
cadddb2c31d24d48f4017db4df0a29687432326cwrowe * Should only be one in this version of Apache for WIN32
e7505ba54ac56ae30e4e250f912f3dbaf92ca45fwrowe if (create_process(pconf, process_handles, process_kill_events,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "master_main: create child process failed. Exiting.");
4f9c22c4f27571d54197be9674e1fc0d528192aestriker /* Wait for shutdown or restart events or for child death */
4f9c22c4f27571d54197be9674e1fc0d528192aestriker process_handles[current_live_processes] = shutdown_event;
beda1fb2f11c52ca4612460a5d5ba47398143efbwrowe process_handles[current_live_processes+1] = restart_event;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe rv = WaitForMultipleObjects(current_live_processes+2, (HANDLE *)process_handles,
c2cf53a40a9814eb91db2cdf820f97d943f21628coar /* Something serious is wrong */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_log_error(APLOG_MARK,APLOG_CRIT, apr_get_os_error(), server_conf,
c2cf53a40a9814eb91db2cdf820f97d943f21628coar "master_main: WaitForMultipeObjects WAIT_FAILED -- doing server shutdown");
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* Hey, this cannot happen */
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe "master_main: WaitForMultipeObjects with INFINITE wait exited with WAIT_TIMEOUT");
117026201e6d8fe7d82416b8a7324830f5a87292wrowe /* shutdown_event signalled */
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, s,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "Parent: SHUTDOWN EVENT SIGNALED -- Shutting down the server.");
cadddb2c31d24d48f4017db4df0a29687432326cwrowe ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "ResetEvent(shutdown_event)");
cadddb2c31d24d48f4017db4df0a29687432326cwrowe /* restart_event signalled */
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "Parent: RESTART EVENT SIGNALED -- Restarting the server.");
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
cadddb2c31d24d48f4017db4df0a29687432326cwrowe "master_main: ResetEvent(restart_event) failed.");
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe /* Signal each child process to die
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe * We are making a big assumption here that the child process, once signaled,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar * will REALLY go away. Since this is a restart, we do not want to hold the
cadddb2c31d24d48f4017db4df0a29687432326cwrowe * new child process up waiting for the old child to die. Remove the old
cadddb2c31d24d48f4017db4df0a29687432326cwrowe * child out of the process_handles apr_table_t and hope for the best...
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe for (i = 0; i < children_to_kill; i++) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
4f9c22c4f27571d54197be9674e1fc0d528192aestriker "master_main: SetEvent for child process in slot #%d failed", i);
3cd826b00280881e5a2f03d8ec1f8d55802b93dewrowe cleanup_process(process_handles, process_kill_events, i, ¤t_live_processes);
cadddb2c31d24d48f4017db4df0a29687432326cwrowe /* A child process must have exited because of a fatal error condition (seg fault, etc.).
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Remove the dead process
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe * from the process_handles and process_kill_events apr_table_t and create a new
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe * child process.
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * TODO: Consider restarting the child immediately without looping through http_main
1067418d9ed9ed9daeb3ca4f74e72db810c49833wrowe * and without rereading the configuration. Will need this if we ever support multiple
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * children. One option, create a parent thread which waits on child death and restarts it.
117026201e6d8fe7d82416b8a7324830f5a87292wrowe * Consider, however, that if the user makes httpd.conf invalid, we want to die before
b67fb549910fa0faf4cdd8aeaf9aeab51d4b6a92wrowe * our child tries it... otherwise we have a nasty loop.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "Parent: CHILD PROCESS FAILED -- Restarting the child process.");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb cleanup_process(process_handles, process_kill_events, cld, ¤t_live_processes);
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe /* APD2("main_process: child in slot %d died", rv); */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* restart_child(process_hancles, process_kill_events, cld, ¤t_live_processes); */
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb /* Drain the AcceptEx completion port of any outstanding I/O pending for the dead
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * process. */
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe /* Signal each child processes to die */
5bb29f57ae0184d2b3c1cdf35132f8ceb011f882wrowe for (i = 0; i < current_live_processes; i++) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker printf("SetEvent handle = %d\n", process_kill_events[i]);
ecc4a080f07af3fbc1b91bbd00997ec1d592c6f9wrowe ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
731344ed8f3677d1661c261ca5fcdd2ee3dbc74ccoar "master_main: SetEvent for child process in slot #%d failed", i);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb while (current_live_processes && ((tmstart+60) > time(NULL))) {
4f9c22c4f27571d54197be9674e1fc0d528192aestriker rv = WaitForMultipleObjects(current_live_processes, (HANDLE *)process_handles, FALSE, 2000);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar cleanup_process(process_handles, process_kill_events, cld, ¤t_live_processes);
0540a0b469147b52e858587270dba31c2aaa9e09wrowe for (i = 0; i < current_live_processes; i++) {
0540a0b469147b52e858587270dba31c2aaa9e09wrowe ap_log_error(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO, APR_SUCCESS, server_conf,
c2cf53a40a9814eb91db2cdf820f97d943f21628coar "Parent: Forcing termination of child #%d (handle %d)", i, process_handles[i]);
c2cf53a40a9814eb91db2cdf820f97d943f21628coar return 0; /* Tell the caller we do not want to restart */
7763a4beb8afca9c8f93db0cb6836124901af52awrowe/* service_nt_main_fn needs to append the StartService() args
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * outside of our call stack and thread as the service starts...
0540a0b469147b52e858587270dba31c2aaa9e09wrowe/* Remember service_to_start failures to log and fail in pre_config.
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * Remember inst_argc and inst_argv for installing or starting the
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * service after we preflight the config.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic const char * const *inst_argv;
5b3abd2fecc712f08ad728114aa77137b9f67716wrowe /* Handle the following SCM aspects in this phase:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * -k runservice [transition for WinNT, nothing for Win9x]
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * -k (!)install [error out if name is not installed]
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * We can't leave this phase until we know our identity
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * and modify the command arguments appropriately.
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe const char *optarg;
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard /* AP_PARENT_PID is only valid in the child */
30b4a330a5f651eb5198fa93dbb9f3d3594564c9stoddard /* This is the child */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* The parent is responsible for providing the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * COMPLETE ARGUMENTS REQUIRED to the child.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * No further argument parsing is needed, but
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * for good measure we will provide a simple
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * signal string for later testing.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* This is the parent, we have a long way to go :-) */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Rewrite process->argv[];
d4abb06ac220bb280ae996b6d21bbd257db51bb1jerenkrantz * strip out -k signal into signal_arg
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * strip out -n servicename into service_name & display_name
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * add default -d serverroot from the path of this executable
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The end result will look like:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The invocation command (%0)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The -d serverroot default from the running executable
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * The requested service's (-n) registry ConfigArgs
4f9c22c4f27571d54197be9674e1fc0d528192aestriker * The WinNT SCM's StartService() args
4f9c22c4f27571d54197be9674e1fc0d528192aestriker if (!GetModuleFileName(NULL, fnbuf, sizeof(fnbuf))) {
if (optarg) {
if (!signal_arg)
running_as_service = 0;
else if (running_as_service)
* in these cases we -don't- care if httpd.conf has config errors!
exit(0);
static void winnt_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec* server)
static int restart_num = 0;
* is a valid conf\httpd.conf environment to start.
NULL,
if (!shutdown_event) {
if (!restart_event) {
server_conf = s;
child_main();
if (!restart) {
return err;
return NULL;
return err;
return NULL;
return err;
return NULL;
const char *fname;
return err;
return NULL;
const char *value)
return NULL;
{ NULL }