mpm_winnt.c revision d8b6df105c25cda12b588da6981489f80662a7bf
/* ====================================================================
* Copyright (c) 1995-2000 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Software Foundation
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Software Foundation
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE Apache Software Foundation ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE Apache Software Foundation OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Software Foundation and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/
#define CORE_PRIVATE
#include "apr_portable.h"
#include "httpd.h"
#include "http_main.h"
#include "http_log.h"
#include "http_config.h" /* for read_config */
#include "http_core.h" /* for get_remote_host */
#include "http_connection.h"
#include "ap_mpm.h"
#include "ap_config.h"
#include "ap_listen.h"
#include "mpm_default.h"
#include "service.h"
#include "iol_socket.h"
#include "winnt.h"
/*
* Definitions of WINNT MPM specific config globals
*/
static char *ap_pid_fname = NULL;
static int ap_threads_per_child = 0;
static int workers_may_exit = 0;
static int max_requests_per_child = 0;
static int num_listenfds = 0;
static char ap_coredump_dir[MAX_STRING_LEN];
static server_rec *server_conf;
static int one_process = 0;
int ap_max_requests_per_child=0;
int ap_daemons_to_start=0;
static event *exit_event;
int my_pid;
int parent_pid;
{
}
return APR_SUCCESS;
}
/* A bunch or routines from os/win32/multithread.c that need to be merged into APR
* or thrown out entirely...
*/
static int
{
switch(rv)
{
case WAIT_OBJECT_0:
case WAIT_ABANDONED:
return(MULTI_OK);
case WAIT_TIMEOUT:
return(MULTI_TIMEOUT);
case WAIT_FAILED:
return(MULTI_ERR);
default:
ap_assert(0);
}
ap_assert(0);
return(0);
}
typedef void semaphore;
typedef void event;
static semaphore *
create_semaphore(int initial)
{
}
{
int rv;
}
{
}
{
}
/* To share the semaphores with other processes, we need a NULL ACL
* Code from MS KB Q106387
*/
static PSECURITY_ATTRIBUTES GetNullACL()
{
return NULL;
}
|| GetLastError()) {
return NULL;
}
|| GetLastError()) {
return NULL;
}
return sa;
}
static void CleanNullACL( void *sa ) {
if( sa ) {
}
}
/*
* The Win32 call WaitForMultipleObjects will only allow you to wait for
* a maximum of MAXIMUM_WAIT_OBJECTS (current 64). Since the threading
* model in the multithreaded version of apache wants to use this call,
* we are restricted to a maximum of 64 threads. This is a simplistic
* routine that will increase this size.
*/
{
do {
if (!bFirst)
Sleep(1000);
else
0, 0);
if (dwRet != WAIT_TIMEOUT) {
break;
}
}
return dwRet;
}
/*
* Signalling Apache on NT.
*
* Under Unix, Apache can be told to shutdown or restart by sending various
* signals (HUP, USR, TERM). On NT we don't have easy access to signals, so
* we use "events" instead. The parent apache process goes into a loop
* where it waits forever for a set of events. Two of those events are
* called
*
* apPID_shutdown
* apPID_restart
*
* (where PID is the PID of the apache parent process). When one of these
* is signalled, the Apache parent performs the appropriate action. The events
* can become signalled through internal Apache methods (e.g. if the child
* finds a fatal error and needs to kill its parent), via the service
* control manager (the control thread will signal the shutdown event when
* requested to stop the Apache service), from the -k Apache command line,
* or from any external program which finds the Apache PID from the
* httpd.pid file.
*
* The signal_parent() function, below, is used to signal one of these events.
* It can be called by any child or parent process, since it does not
* rely on global variables.
*
* On entry, type gives the event to signal. 0 means shutdown, 1 means
* graceful restart.
*/
static void signal_parent(int type)
{
HANDLE e;
char *signal_name;
extern char signal_shutdown_name[];
extern char signal_restart_name[];
/* after updating the shutdown_pending or restart flags, we need
* to wake up the parent process so it can see the changes. The
* parent will normally be waiting for either a child process
* to die, or for a signal on the "spache-signal" event. So set the
* "apache-signal" event here.
*/
if (one_process) {
return;
}
switch(type) {
case 0: signal_name = signal_shutdown_name; break;
default: return;
}
if (!e) {
/* Um, problem, can't signal the parent, which means we can't
* signal ourselves to die. Ignore for now...
*/
"OpenEvent on %s event", signal_name);
return;
}
if (SetEvent(e) == 0) {
/* Same problem as above */
"SetEvent on %s event", signal_name);
CloseHandle(e);
return;
}
CloseHandle(e);
}
static int volatile is_graceful = 0;
API_EXPORT(int) ap_graceful_stop_signalled(void)
{
return is_graceful;
}
void ap_start_shutdown(void)
{
signal_parent(0);
}
/*
* Initialise the signal names, in the global variables signal_name_prefix,
* signal_restart_name and signal_shutdown_name.
*/
static void setup_signal_names(char *prefix)
{
"%s_shutdown", signal_name_prefix);
"%s_restart", signal_name_prefix);
}
/*
* Routines that deal with sockets, some are WIN32 specific...
*/
static int s_iInitCount = 0;
static int AMCSocketInitialize(void)
{
int iVersionRequested;
int err;
if (s_iInitCount > 0) {
s_iInitCount++;
return (0);
}
else if (s_iInitCount < 0)
return (s_iInitCount);
/* s_iInitCount == 0. Do the initailization */
if (err) {
s_iInitCount = -1;
return (s_iInitCount);
}
s_iInitCount = -2;
WSACleanup();
return (s_iInitCount);
}
s_iInitCount++;
return (s_iInitCount);
}
static void AMCSocketCleanup(void)
{
if (--s_iInitCount == 0)
WSACleanup();
return;
}
static void sock_disable_nagle(int s)
{
/* The Nagle algorithm says that we should delay sending partial
* packets in hopes of getting more data. We don't want to do
* this; we are not telnet. There are bad interactions between
* persistent connections and Nagle's algorithm that have very severe
* performance penalties. (Failing to disable Nagle is not much of a
* problem with simple HTTP.)
*
* In spite of these problems, failure here is not a shooting offense.
*/
int just_say_no = 1;
sizeof(int)) < 0) {
"setsockopt: (TCP_NODELAY)");
}
}
/*
* Routines to deal with managing the list of listening sockets.
*/
static ap_listen_rec *head_listener;
{
if (head_listener == NULL)
return (lr);
}
}
return NULL;
}
static int setup_listeners(server_rec *s)
{
int num_listeners = 0;
/* Setup the listeners */
return 0;
}
listenmaxfd = nsd;
}
}
}
return num_listeners;
}
static int setup_inherited_listeners(server_rec *s)
{
int num_listeners = 0;
/* Setup the listeners */
/* Set up a default listener if necessary */
if (ap_listeners == NULL) {
if (!lr)
return 0;
ap_listeners = lr;
}
/* Open the pipe to the parent process to receive the inherited socket
* data. The sockets have been set to listening in the parent process.
*/
"setup_inherited_listeners: Unable to read socket data from parent");
signal_parent(0); /* tell parent to die */
exit(1);
}
"Child %d: setup_inherited_listener() read = %d bytes of WSAProtocolInfo.", my_pid);
&WSAProtocolInfo, 0, 0);
if (nsd == INVALID_SOCKET) {
"Child %d: setup_inherited_listeners(), WSASocket failed to open the inherited socket.", my_pid);
signal_parent(0); /* tell parent to die */
exit(1);
}
if (nsd >= 0) {
listenmaxfd = nsd;
}
}
// ap_register_cleanup(p, (void *)lr->sd, socket_cleanup, ap_null_cleanup);
}
/* Now, read the AcceptExCompPort from the parent */
}
return num_listeners;
}
/**********************************************************************
* Multithreaded implementation
*
* This code is fairly specific to Win32.
*
* The model used to handle requests is a set of threads. One "main"
* thread listens for new requests. When something becomes
* available, it does a select and places the newly available socket
* onto a list of "jobs" (add_job()). Then any one of a fixed number
* of "worker" threads takes the top job off the job list with
* remove_job() and handles that connection to completion. After
* the connection has finished the thread is free to take another
* job from the job list.
*
* In the code, the "main" thread is running within the child_main()
* function. The first thing this function does is create the
* worker threads, which operate in the child_sub_main() function. The
* main thread then goes into a loop within child_main() where they
* do a select() on the listening sockets. The select times out once
* per second so that the thread can check for an "exit" signal
* from the parent process (see below). If this signal is set, the
* thread can exit, but only after it has accepted all incoming
* connections already in the listen queue (since Win32 appears
* to through away listened but unaccepted connections when a
* process dies).
*
* Because the main and worker threads exist within a single process
* they are vulnerable to crashes or memory leaks (crashes can also
* be caused within modules, of course). There also needs to be a
* mechanism to perform restarts and shutdowns. This is done by
* creating the main & worker threads within a subprocess. A
* main process (the "parent process") creates one (or more)
* processes to do the work, then the parent sits around waiting
* for the working process to die, in which case it starts a new
* one. The parent process also handles restarts (by creating
* a new working process then signalling the previous working process
* exit ) and shutdowns (by signalling the working process to exit).
* The parent process operates within the master_main() function. This
* process also handles requests from the service manager (NT only).
*
* Signalling between the parent and working process uses a Win32
* event. Each child has a unique name for the event, which is
* passed to it with the -Z argument when the child is spawned. The
* parent sets (signals) this event to tell the child to die.
* At present all children do a graceful die - they finish all
* current jobs _and_ empty the listen queue before they exit.
* A non-graceful die would need a second event. The -Z argument in
* the child is also used to create the shutdown and restart events,
* since the prefix (apPID) contains the parent process PID.
*
* The code below starts with functions at the lowest level -
* worker threads, and works up to the top level - the main()
* function of the parent process.
*
* The scoreboard (in process memory) contains details of the worker
* threads (within the active working process). There is no shared
* "scoreboard" between processes, since only one is ever active
* at once (or at most, two, when one has been told to shutdown but
* is processes outstanding requests, and a new one has been started).
* This is controlled by a "start_mutex" which ensures only one working
* process is active at once.
**********************************************************************/
/*
* Definition of jobs, shared by main and worker threads.
*/
typedef struct joblist_s {
int sock;
} joblist;
/*
* Globals common to main and worker threads. This structure is not
* used by the parent process.
*/
typedef struct globals_s {
int jobcount;
} globals;
#define MAX_SELECT_ERRORS 100
/* Windows 9x specific code...
* model. A single thread accepts connections and queues the accepted socket
* to the accept queue for consumption by a pool of worker threads.
*
* win9x_get_connection()
* Calls remove_job() to pull a job from the accept queue. All the worker
* threads block on remove_job.
* accept_and_queue_connections()
* The accept threads runs this function, which accepts connections off
* the network and calls add_job() to queue jobs to the accept_queue.
* add_job()/remove_job()
* Add or remove an accepted socket from the list of sockets
* connected to clients. allowed_globals.jobmutex protects
* against multiple concurrent access to the linked list of jobs.
*/
{
"Ouch! Out of memory in add_job()!");
return;
}
if (!allowed_globals.jobhead)
}
static int remove_job(void)
{
int sock;
return (-1);
}
return (sock);
}
static void accept_and_queue_connections(void * dummy)
{
int requests_this_child = 0;
int wait_time = 1;
int csd;
int nsd = INVALID_SOCKET;
struct sockaddr_in sa_client;
int count_select_errors = 0;
int rc;
int clen;
while (!workers_may_exit) {
break;
}
// rc = ap_select(listenmaxfd + 1, &main_fds, NULL, NULL, &tv);
count_select_errors = 0; /* reset count of errors */
continue;
}
else if (rc == SOCKET_ERROR) {
/* A "real" error occurred, log it and increment the count of
* select errors. This count is used to ensure we don't go into
* a busy loop of continuous errors.
*/
"select failed with errno %d", h_errno);
if (count_select_errors > MAX_SELECT_ERRORS) {
workers_may_exit = 1;
"Too many errors in select loop. Child process exiting.");
break;
}
} else {
/* fetch the native socket descriptor */
}
}
do {
if (csd == INVALID_SOCKET) {
csd = -1;
}
if (csd < 0) {
if (h_errno != WSAECONNABORTED) {
"accept: (client socket)");
}
}
else {
}
}
}
{
int len;
/* allocate the completion context and the transaction pool */
if (!context) {
"win9x_get_connection: ap_pcalloc() failed. Process will exit.");
return NULL;
}
}
while (1) {
return NULL;
}
//ap_note_cleanups_for_socket(ptrans, csd);
"getsockname failed");
continue;
}
"getpeername failed with error %d\n", WSAGetLastError());
}
/* do we NEED_DUPPED_CSD ?? */
return context;
}
}
/*
* Windows 2000/NT specific code...
* create_acceptex_context()
* reset_acceptex_context()
* drain_acceptex_complport()
* winnt_get_connection()
*
* TODO: Insert a discussion of 'completion contexts' and what these function do here...
*/
{
int rc;
int lastError;
while (1) {
&pol, 1000);
if (!rc) {
lastError = GetLastError();
if (lastError == ERROR_OPERATION_ABORTED) {
"Child: %d - Draining a packet off the completion port.", my_pid);
continue;
}
break;
}
/* It is only valid to clean-up in the process that initiated the I/O */
}
}
}
{
int lasterror;
/* allocate the completion context */
if (!context) {
"create_acceptex_context: ap_pcalloc() failed. Process will exit.");
return -1;
}
/* initialize the completion context */
"create_acceptex_context: CreateEvent() failed. Process will exit.");
return -1;
}
"create_acceptex_context: socket() failed. Process will exit.");
return -1;
}
/* AcceptEx on the completion context. The completion context will be signaled
* when a connection is accepted. */
0, //context->recv_buf_size,
(LPOVERLAPPED) context)) {
lasterror = WSAGetLastError();
if (lasterror != ERROR_IO_PENDING) {
"create_acceptex_context: AcceptEx failed. Process will exit.");
return -1;
}
}
return 0;
}
{
int lasterror;
"reset_acceptex_context: socket() failed. Process will exit.");
return -1;
}
0, //context->recv_buf_size,
lasterror = WSAGetLastError();
if (lasterror != ERROR_IO_PENDING) {
"reset_acceptex_context: AcceptEx failed. Leaving the process running.");
return -1;
}
}
return 0;
}
{
int requests_this_child = 0;
int rc;
/* If child shutdown has been signaled, clean-up the completion context */
if (workers_may_exit) {
/* destroy pool */
}
else {
return NULL;
}
}
}
while (1) {
&CompKey,
&pol,
INFINITE);
if (!rc) {
"Child: %d - GetQueuedCompletionStatus() failed", my_pid);
continue;
}
break;
}
if (CompKey == 999) {
return NULL;
}
/* Each listener needs at least 1 context available to receive connections on.
* Create additional listener contexts if needed.
*/
"Unable to create an AcceptEx completion context -- process will exit");
signal_parent(0);
return NULL;
}
}
/* Received a connection */
0, //context->recv_buf_size,
&context->sa_client_len);
return context;
}
/*
* worker_main() - this is the main loop for the worker threads
*
* Windows 95/98
* Each thread runs within this function. They wait within remove_job()
* for a job to become available, then handle all the requests on that
* connection until it is closed, then return to remove_job().
*
* The worker thread will exit when it removes a job which contains
* socket number -1. This provides a graceful thread exit, since
* it will never exit during a connection.
*
* This code in this function is basically equivalent to the child_main()
* from the multi-process (Unix) environment, except that we
*
* - do not call child_init_modules (child init API phase)
* - block in remove_job, and when unblocked we have an already
* accepted socket, instead of blocking on a mutex or select().
*/
static void worker_main(int child_num)
{
/* Windows NT/2000: Create AcceptEx completion contexts for each of the
* listeners
*/
if (!bListenersStarted) {
int i;
for(i=0; i<2; i++) {
"Unable to create an AcceptEx completion context -- process will exit");
signal_parent(0); /* tell parent to die */
}
}
}
}
}
while (1) {
/* Grab a connection off the network */
}
else {
}
if (!context)
break;
"worker_main: attach_socket() failed. Continuing...");
continue;
}
}
#if 0
"child_main: Setting exit_event");
#endif
/* TODO: Add code to clean-up completion contexts here */
}
{
int i;
(*thread_cnt)--;
}
/*
* child_main() is main loop for the child process. The loop in
* this function becomes the controlling thread for the actually working
* threads (which run in a loop in child_sub_main()).
* Globals Used:
* exit_event, start_mutex, ap_threads_per_child, server_conf,
* h_errno defined to WSAGetLastError in winsock2.h,
*/
static void child_main()
{
int nthreads = ap_threads_per_child;
int thread_id;
int rv;
int i;
// ap_restart_time = time(NULL);
/*
* Wait until we have permission to start accepting connections.
* start_mutex is used to ensure that only one child ever
*/
if (status != APR_SUCCESS) {
"Child %d: Failed to acquire the start_mutex. Process will exit.", my_pid);
signal_parent(0); /* tell parent to die */
exit(0);
}
ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf, "Child %d: Acquired the start mutex", my_pid);
/* Setup the listening sockets */
if (one_process) {
/* Associate the socket with the IO Completion port */
int nsd;
}
} else {
}
if (listenmaxfd == INVALID_SOCKET) {
/* No sockets were made, better log something and exit */
"No sockets were created for listening");
signal_parent(0); /* tell parent to die */
exit(0);
}
/* Create the worker thread pool */
ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf, "Child %d: Creating %d worker threads",my_pid, nthreads);
for (i = 0; i < nthreads; i++) {
}
/* Win95/98: Create the accept thread */
(void *) i, 0, &thread_id);
}
/* Wait for the exit event to be signaled by the parent process */
"Child %d: Exit event signaled. Child process is ending.", my_pid);
workers_may_exit = 1;
/* Shutdown the worker threads */
for (i = 0; i < nthreads; i++) {
add_job(-1);
}
}
else { /* Windows NT/2000 */
/* Hack alert... Give the server a couple of seconds to receive
* connections and drain AcceptEx completion contexts. We will
* probably drop a few connections across a graceful restart, but
* hopefully not many. This needs work...*/
Sleep(2000);
/* Tell the worker threads to exit. Any connections accepted on
* the completion port from now will be dropped */
for (i=0; i < nthreads; i++) {
"PostQueuedCompletionStatus failed");
}
}
}
/* Wait for the worker threads to die */
while (nthreads) {
if (rv != WAIT_TIMEOUT) {
continue;
}
break;
}
for (i = 0; i < nthreads; i++) {
CloseHandle(child_handles[i]);
}
"Child %d: All worker threads have ended.", my_pid);
/* All the worker threads should have exited by now, which will
* cause any outstanding I/O on the completion port to be aborted.
* Drain the completion port of this aborted I/O/
*/
}
"Child %d: Releasing the start mutex", my_pid);
/* Still need to register cleanups for the sockets */
}
/*
* Spawn a child Apache process. The child process has the command line arguments from
* argc and argv[], plus a -Z argument giving the name of an event. The child should
* open and poll or wait on this event. When it is signalled, the child should die.
* prefix is a prefix string for the event name.
*
* The child_num argument on entry contains a serial number for this child (used to create
* a unique event name). On exit, this number will have been incremented by one, ready
* for the next call.
*
* On exit, the value pointed to be *ev will contain the event created
* to signal the new child process.
*
* The return value is the handle to the child process if successful, else -1. If -1 is
* returned the error will already have been logged by ap_log_error().
*/
/**********************************************************************
* master_main - this is the parent (main) process. We create a
* child process to do the work, then sit around waiting for either
* the child to exit, or a restart or exit signal. If the child dies,
* we just respawn a new one. If we have a shutdown or graceful restart,
* tell the child to die when it is ready. If it is a non-graceful
* restart, force the child to die immediately.
**********************************************************************/
{
int i;
int handle = 0;
}
(*processes)--;
}
{
int rv;
char buf[1024];
char *pCommand;
int i;
SECURITY_ATTRIBUTES sa = {0};
/* Create the IOCompletionPort */
if (AcceptExCompPort == NULL) {
NULL,
0,
0); /* CONCURRENT ACTIVE THREADS */
if (AcceptExCompPort == NULL) {
"Unable to create the AcceptExCompletionPort -- process will exit");
return -1;
}
}
/* Build the command line. Should look something like this:
* C:/apache/bin/apache.exe -f ap_server_confname
* First, get the path to the executable...
*/
"Parent: Path to Apache process too long");
return -1;
} else if (rv == 0) {
"Parent: GetModuleFileName() returned NULL for current process.");
return -1;
}
/* Build the command line */
}
/* Create a pipe to send socket info to the child */
"Parent: Unable to create pipe to child process.\n");
return -1;
}
/* Give the read end of the pipe (hPipeRead) to the child as stdin. The
* parent will write the socket data to the child on this pipe.
*/
TRUE, /* Inherit handles */
CREATE_SUSPENDED, /* Creation flags */
NULL, /* Environment block */
NULL,
"Parent: Not able to create the child process.");
/*
* We must close the handles to the new process and its main thread
* to prevent handle and memory leaks.
*/
return -1;
}
else {
/* Create the exit_event, apCchild_pid */
if (!kill_event) {
"Parent: Could not create exit event for child process");
return -1;
}
/* Assume the child process lives. Update the process and event tables */
(*processes)++;
/* We never store the thread's handle, so close it now. */
/* Run the chain of open sockets. For each socket, duplicate it
* for the target process then send the WSAPROTOCOL_INFO
* (returned by dup socket) to the child */
int nsd;
/* Associate the socket with the IOcompletion port */
lpWSAProtocolInfo) == SOCKET_ERROR) {
return -1;
}
(LPOVERLAPPED) NULL)) {
return -1;
}
}
/* Now, send the AcceptEx completion port to the child */
"Parent: Unable to duplicate AcceptEx completion port. Shutting down.");
return -1;
}
WriteFile(hPipeWrite, &hDupedCompPort, (DWORD) sizeof(hDupedCompPort), &BytesWritten, (LPOVERLAPPED) NULL);
}
return 0;
}
{
int i;
int child_num = 0;
int restart_pending = 0;
int shutdown_pending = 0;
int current_live_processes = 0; /* number of child process we know about */
setup_listeners(s);
/* Create child process
* Should only be one in this version of Apache for WIN32
*/
while (remaining_children_to_start--) {
¤t_live_processes) < 0) {
"master_main: create child process failed. Exiting.");
shutdown_pending = 1;
goto die_now;
}
}
restart_pending = shutdown_pending = 0;
/* Wait for shutdown or restart events or for child death */
printf("process/shutdown/restart %d %d %d\n", process_handles[0], process_handles[1], process_handles[2]);
if (rv == WAIT_FAILED) {
/* Something serious is wrong */
"master_main: WaitForMultipeObjects WAIT_FAILED -- doing server shutdown");
shutdown_pending = 1;
}
else if (rv == WAIT_TIMEOUT) {
/* Hey, this cannot happen */
"master_main: WaitForMultipeObjects with INFINITE wait exited with WAIT_TIMEOUT");
shutdown_pending = 1;
}
else if (cld == current_live_processes) {
/* shutdown_event signalled */
shutdown_pending = 1;
printf("shutdown event signaled\n");
"master_main: Shutdown event signaled -- doing server shutdown.");
if (ResetEvent(shutdown_event) == 0) {
"ResetEvent(shutdown_event)");
}
}
/* restart_event signalled */
restart_pending = 1;
"master_main: Restart event signaled. Doing a graceful restart.");
if (ResetEvent(restart_event) == 0) {
"master_main: ResetEvent(restart_event) failed.");
}
/* Signal each child process to die
* We are making a big assumption here that the child process, once signaled,
* will REALLY go away. Since this is a restart, we do not want to hold the
* new child process up waiting for the old child to die. Remove the old
* child out of the process_handles ap_table_t and hope for the best...
*/
for (i = 0; i < children_to_kill; i++) {
if (SetEvent(process_kill_events[i]) == 0)
"master_main: SetEvent for child process in slot #%d failed", i);
}
}
else {
/* A child process must have exited because of a fatal error condition (seg fault, etc.).
* Remove the dead process
* from the process_handles and process_kill_events ap_table_t and create a new
* child process.
* TODO: Consider restarting the child immediately without looping through http_main
* and without rereading the configuration. Will need this if we ever support multiple
* children. One option, create a parent thread which waits on child death and restarts it.
*/
restart_pending = 1;
"master_main: Child process failed. Restarting the child process.");
/* APD2("main_process: child in slot %d died", rv); */
/* restart_child(process_hancles, process_kill_events, cld, ¤t_live_processes); */
/* Drain the AcceptEx completion port of any outstanding I/O pending for the dead
* process. */
}
if (shutdown_pending) {
/* Signal each child processes to die */
for (i = 0; i < current_live_processes; i++) {
if (SetEvent(process_kill_events[i]) == 0)
"master_main: SetEvent for child process in slot #%d failed", i);
}
if (rv == WAIT_TIMEOUT)
continue;
}
for (i = 0; i < current_live_processes; i++) {
"forcing termination of child #%d (handle %d)", i, process_handles[i]);
}
return (0); /* Tell the caller we are shutting down */
}
return (1); /* Tell the caller we want a restart */
}
/*
* winnt_pre_config() hook
*/
{
char *pid;
/* AP_PARENT_PID is only valid in the child */
if (pid) {
/* This is the child */
}
else {
/* This is the parent */
}
}
static void winnt_post_config(ap_context_t *pconf, ap_context_t *plog, ap_context_t *ptemp, server_rec* server_conf)
{
}
{
char* exit_event_name;
static int restart = 0; /* Default is to not restart */
// time_t tmstart;
server_conf = s;
/* Child process */
"Child %d: Child process is running", my_pid);
if (one_process) {
/* Create the IO CompletionPort */
if (AcceptExCompPort == NULL) {
NULL, 0, 0);
if (AcceptExCompPort == NULL) {
"Unable to create the AcceptExCompletionPort -- process will exit");
return -1;
}
}
}
else {
}
child_main();
restart = 0;
}
else {
/* Parent process */
if (!restart) {
// setup_signal_names(ap_psprintf(pconf,"ap%d", parent_pid));
/* Create shutdown event, apPID_shutdown, where PID is the parent
* Apache process ID. Shutdown is signaled by 'apache -k shutdown'.
*/
if (!shutdown_event) {
"master_main: Cannot create shutdown event %s", signal_shutdown_name);
CleanNullACL((void *)sa);
exit(1);
}
/* Create restart event, apPID_restart, where PID is the parent
* Apache process ID. Restart is signaled by 'apache -k restart'.
*/
// restart_event = CreateEvent(sa, TRUE, FALSE, signal_restart_name);
if (!restart_event) {
"ap_run_mpm: Cannot create restart event %s", signal_restart_name);
CleanNullACL((void *)sa);
exit(1);
}
CleanNullACL((void *)sa);
/* Create the start mutex, apPID, where PID is the parent Apache process ID.
* Ths start mutex is used during a restart to prevent more than one
* child process from entering the accept loop at once.
*/
// ap_create_lock(&start_mutex,APR_MUTEX, APR_CROSS_PROCESS,signal_name_prefix,pconf);
/* TODO: Add some code to detect failure */
}
/* Go to work... */
if (!restart) {
/* Shutting down. Clean up... */
server_conf, "removed PID file %s (pid=%ld)",
}
}
return !restart;
}
static void winnt_hooks(void)
{
// INIT_SIGLIST()
one_process = 0;
/* Configuration hooks implemented by http_config.c ... */
}
/*
* Command processors
*/
{
return err;
}
return "PidFile directive not allowed in <VirtualHost>";
}
ap_pid_fname = arg;
return NULL;
}
{
return err;
}
if (ap_threads_per_child > HARD_THREAD_LIMIT) {
"WARNING: ThreadsPerChild of %d exceeds compile time"
" limit of %d threads,", ap_threads_per_child,
" lowering ThreadsPerChild to %d. To increase, please"
}
else if (ap_threads_per_child < 1) {
"WARNING: Require ThreadsPerChild > 0, setting to 1");
ap_threads_per_child = 1;
}
return NULL;
}
{
return err;
}
return NULL;
}
{
const char *fname;
return err;
}
" does not exist or is not a directory", NULL);
}
return NULL;
}
/* Stub functions until this MPM supports the connection status API */
const char *value)
{
/* NOP */
}
{
/* NOP */
}
static const command_rec winnt_cmds[] = {
"A file for logging the server process ID"},
"Number of threads each child creates" },
"Maximum number of requests a particular child serves before dying." },
"The location of the directory Apache changes to before dumping core" },
{ NULL }
};
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
winnt_cmds, /* command ap_table_t */
NULL, /* handlers */
winnt_hooks /* register_hooks */
};