1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 #include "ap_config.h"
62 #include "apr_portable.h"
63 #include "http_main.h"
65 #include "http_config.h" /* for read_config */
66 #include "http_core.h" /* for get_remote_host */
67 #include "http_connection.h"
69 #include "ap_config.h"
70 #include "ap_listen.h"
71 #include "mpm_default.h"
72 #include "iol_socket.h"
76 * Definitions of WINNT MPM specific config globals
79 static char *ap_pid_fname = NULL;
80 static int ap_threads_per_child = 0;
81 static int workers_may_exit = 0;
82 static int max_requests_per_child = 0;
83 static HANDLE shutdown_event; /* used to signal shutdown to parent */
84 static HANDLE restart_event; /* used to signal a restart to parent */
86 static struct fd_set listenfds;
87 static int num_listenfds = 0;
88 static SOCKET listenmaxfd = INVALID_SOCKET;
90 static ap_pool_t *pconf; /* Pool for config stuff */
92 static char ap_coredump_dir[MAX_STRING_LEN];
94 static server_rec *server_conf;
95 static HANDLE AcceptExCompPort = NULL;
97 static int one_process = 0;
99 static OSVERSIONINFO osver; /* VER_PLATFORM_WIN32_NT */
101 int ap_max_requests_per_child=0;
102 int ap_daemons_to_start=0;
104 static event *exit_event;
105 HANDLE maintenance_event;
106 ap_lock_t *start_mutex;
109 API_EXPORT_VAR ap_completion_t ap_mpm_init_complete = NULL;
111 static ap_status_t socket_cleanup(void *sock)
113 ap_socket_t *thesocket = sock;
115 if (ap_get_os_sock(&sd, thesocket) == APR_SUCCESS) {
121 /* A bunch or routines from os/win32/multithread.c that need to be merged into APR
122 * or thrown out entirely...
126 typedef void semaphore;
130 create_semaphore(int initial)
132 return(CreateSemaphore(NULL, initial, 1000000, NULL));
135 static void acquire_semaphore(semaphore *semaphore_id)
139 rv = WaitForSingleObject(semaphore_id, INFINITE);
144 static int release_semaphore(semaphore *semaphore_id)
146 return(ReleaseSemaphore(semaphore_id, 1, NULL));
149 static void destroy_semaphore(semaphore *semaphore_id)
151 CloseHandle(semaphore_id);
155 /* To share the semaphores with other processes, we need a NULL ACL
156 * Code from MS KB Q106387
158 static PSECURITY_ATTRIBUTES GetNullACL()
160 PSECURITY_DESCRIPTOR pSD;
161 PSECURITY_ATTRIBUTES sa;
163 sa = (PSECURITY_ATTRIBUTES) LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES));
164 pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
165 SECURITY_DESCRIPTOR_MIN_LENGTH);
166 if (pSD == NULL || sa == NULL) {
169 if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
175 if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE)
181 sa->nLength = sizeof(sa);
182 sa->lpSecurityDescriptor = pSD;
183 sa->bInheritHandle = TRUE;
187 static void CleanNullACL( void *sa ) {
189 LocalFree( ((PSECURITY_ATTRIBUTES)sa)->lpSecurityDescriptor);
195 * The Win32 call WaitForMultipleObjects will only allow you to wait for
196 * a maximum of MAXIMUM_WAIT_OBJECTS (current 64). Since the threading
197 * model in the multithreaded version of apache wants to use this call,
198 * we are restricted to a maximum of 64 threads. This is a simplistic
199 * routine that will increase this size.
201 static DWORD wait_for_many_objects(DWORD nCount, CONST HANDLE *lpHandles,
205 DWORD dwRet = WAIT_TIMEOUT;
209 tStopTime = time(NULL) + dwSeconds;
217 for (dwIndex = 0; dwIndex * MAXIMUM_WAIT_OBJECTS < nCount; dwIndex++) {
218 dwRet = WaitForMultipleObjects(
219 min(MAXIMUM_WAIT_OBJECTS, nCount - (dwIndex * MAXIMUM_WAIT_OBJECTS)),
220 lpHandles + (dwIndex * MAXIMUM_WAIT_OBJECTS),
223 if (dwRet != WAIT_TIMEOUT) {
227 } while((time(NULL) < tStopTime) && (dwRet == WAIT_TIMEOUT));
233 * Signalling Apache on NT.
235 * Under Unix, Apache can be told to shutdown or restart by sending various
236 * signals (HUP, USR, TERM). On NT we don't have easy access to signals, so
237 * we use "events" instead. The parent apache process goes into a loop
238 * where it waits forever for a set of events. Two of those events are
244 * (where PID is the PID of the apache parent process). When one of these
245 * is signalled, the Apache parent performs the appropriate action. The events
246 * can become signalled through internal Apache methods (e.g. if the child
247 * finds a fatal error and needs to kill its parent), via the service
248 * control manager (the control thread will signal the shutdown event when
249 * requested to stop the Apache service), from the -k Apache command line,
250 * or from any external program which finds the Apache PID from the
253 * The signal_parent() function, below, is used to signal one of these events.
254 * It can be called by any child or parent process, since it does not
255 * rely on global variables.
257 * On entry, type gives the event to signal. 0 means shutdown, 1 means
260 static void signal_parent(int type)
264 extern char signal_shutdown_name[];
265 extern char signal_restart_name[];
267 /* after updating the shutdown_pending or restart flags, we need
268 * to wake up the parent process so it can see the changes. The
269 * parent will normally be waiting for either a child process
270 * to die, or for a signal on the "spache-signal" event. So set the
271 * "apache-signal" event here.
278 case 0: signal_name = signal_shutdown_name; break;
279 case 1: signal_name = signal_restart_name; break;
282 e = OpenEvent(EVENT_ALL_ACCESS, FALSE, signal_name);
284 /* Um, problem, can't signal the parent, which means we can't
285 * signal ourselves to die. Ignore for now...
287 ap_log_error(APLOG_MARK, APLOG_EMERG, GetLastError(), server_conf,
288 "OpenEvent on %s event", signal_name);
291 if (SetEvent(e) == 0) {
292 /* Same problem as above */
293 ap_log_error(APLOG_MARK, APLOG_EMERG, GetLastError(), server_conf,
294 "SetEvent on %s event", signal_name);
301 static int volatile is_graceful = 0;
303 API_EXPORT(int) ap_graceful_stop_signalled(void)
308 API_EXPORT(void) ap_start_shutdown(void)
314 * Initialise the signal names, in the global variables signal_name_prefix,
315 * signal_restart_name and signal_shutdown_name.
318 #define MAX_SIGNAL_NAME 30 /* Long enough for apPID_shutdown, where PID is an int */
319 char signal_name_prefix[MAX_SIGNAL_NAME];
320 char signal_restart_name[MAX_SIGNAL_NAME];
321 char signal_shutdown_name[MAX_SIGNAL_NAME];
322 static void setup_signal_names(char *prefix)
324 ap_snprintf(signal_name_prefix, sizeof(signal_name_prefix), prefix);
325 ap_snprintf(signal_shutdown_name, sizeof(signal_shutdown_name),
326 "%s_shutdown", signal_name_prefix);
327 ap_snprintf(signal_restart_name, sizeof(signal_restart_name),
328 "%s_restart", signal_name_prefix);
332 * Routines that deal with sockets, some are WIN32 specific...
335 static void sock_disable_nagle(int s)
337 /* The Nagle algorithm says that we should delay sending partial
338 * packets in hopes of getting more data. We don't want to do
339 * this; we are not telnet. There are bad interactions between
340 * persistent connections and Nagle's algorithm that have very severe
341 * performance penalties. (Failing to disable Nagle is not much of a
342 * problem with simple HTTP.)
344 * In spite of these problems, failure here is not a shooting offense.
348 if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *) &just_say_no,
350 ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, server_conf,
351 "setsockopt: (TCP_NODELAY)");
356 * Routines to deal with managing the list of listening sockets.
358 static ap_listen_rec *head_listener;
360 static ap_inline ap_listen_rec *find_ready_listener(fd_set * main_fds)
365 for (lr = head_listener; lr ; lr = lr->next) {
366 ap_get_os_sock(&nsd, lr->sd);
367 if (FD_ISSET(nsd, main_fds)) {
368 head_listener = lr->next;
369 if (head_listener == NULL)
370 head_listener = ap_listeners;
378 static int setup_listeners(server_rec *s)
381 int num_listeners = 0;
384 /* Setup the listeners */
387 if (ap_listen_open(s->process, s->port)) {
390 for (lr = ap_listeners; lr; lr = lr->next) {
392 if (lr->sd != NULL) {
393 ap_get_os_sock(&nsd, lr->sd);
394 FD_SET(nsd, &listenfds);
395 if (listenmaxfd == INVALID_SOCKET || nsd > listenmaxfd) {
402 head_listener = ap_listeners;
404 return num_listeners;
407 static int setup_inherited_listeners(server_rec *s)
409 WSAPROTOCOL_INFO WSAProtocolInfo;
413 int num_listeners = 0;
416 /* Setup the listeners */
419 /* Set up a default listener if necessary */
421 if (ap_listeners == NULL) {
423 lr = ap_palloc(s->process->pool, sizeof(ap_listen_rec));
427 lr->next = ap_listeners;
431 /* Open the pipe to the parent process to receive the inherited socket
432 * data. The sockets have been set to listening in the parent process.
434 pipe = GetStdHandle(STD_INPUT_HANDLE);
435 for (lr = ap_listeners; lr; lr = lr->next) {
436 if (!ReadFile(pipe, &WSAProtocolInfo, sizeof(WSAPROTOCOL_INFO),
437 &BytesRead, (LPOVERLAPPED) NULL)) {
438 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
439 "setup_inherited_listeners: Unable to read socket data from parent");
440 signal_parent(0); /* tell parent to die */
443 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
444 "Child %d: setup_inherited_listener() read = %d bytes of WSAProtocolInfo.", my_pid);
445 nsd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
446 &WSAProtocolInfo, 0, 0);
447 if (nsd == INVALID_SOCKET) {
448 ap_log_error(APLOG_MARK, APLOG_CRIT, WSAGetLastError(), server_conf,
449 "Child %d: setup_inherited_listeners(), WSASocket failed to open the inherited socket.", my_pid);
450 signal_parent(0); /* tell parent to die */
454 FD_SET(nsd, &listenfds);
455 if (listenmaxfd == INVALID_SOCKET || nsd > listenmaxfd) {
459 // ap_register_cleanup(p, (void *)lr->sd, socket_cleanup, ap_null_cleanup);
460 ap_put_os_sock(&lr->sd, &nsd, pconf);
463 /* Now, read the AcceptExCompPort from the parent */
464 ReadFile(pipe, &AcceptExCompPort, sizeof(AcceptExCompPort), &BytesRead, (LPOVERLAPPED) NULL);
467 for (lr = ap_listeners; lr; lr = lr->next) {
471 head_listener = ap_listeners;
472 return num_listeners;
475 static void bind_listeners_to_completion_port()
477 /* Associate the open listeners with the completion port.
478 * Bypass the operation for Windows 95/98
482 if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
483 for (lr = ap_listeners; lr; lr = lr->next) {
485 ap_get_os_sock(&nsd,lr->sd);
486 CreateIoCompletionPort((HANDLE) nsd, AcceptExCompPort, 0, 0);
491 /**********************************************************************
492 * Multithreaded implementation
494 * This code is fairly specific to Win32.
496 * The model used to handle requests is a set of threads. One "main"
497 * thread listens for new requests. When something becomes
498 * available, it does a select and places the newly available socket
499 * onto a list of "jobs" (add_job()). Then any one of a fixed number
500 * of "worker" threads takes the top job off the job list with
501 * remove_job() and handles that connection to completion. After
502 * the connection has finished the thread is free to take another
503 * job from the job list.
505 * In the code, the "main" thread is running within the child_main()
506 * function. The first thing this function does is create the
507 * worker threads, which operate in the child_sub_main() function. The
508 * main thread then goes into a loop within child_main() where they
509 * do a select() on the listening sockets. The select times out once
510 * per second so that the thread can check for an "exit" signal
511 * from the parent process (see below). If this signal is set, the
512 * thread can exit, but only after it has accepted all incoming
513 * connections already in the listen queue (since Win32 appears
514 * to through away listened but unaccepted connections when a
517 * Because the main and worker threads exist within a single process
518 * they are vulnerable to crashes or memory leaks (crashes can also
519 * be caused within modules, of course). There also needs to be a
520 * mechanism to perform restarts and shutdowns. This is done by
521 * creating the main & worker threads within a subprocess. A
522 * main process (the "parent process") creates one (or more)
523 * processes to do the work, then the parent sits around waiting
524 * for the working process to die, in which case it starts a new
525 * one. The parent process also handles restarts (by creating
526 * a new working process then signalling the previous working process
527 * exit ) and shutdowns (by signalling the working process to exit).
528 * The parent process operates within the master_main() function. This
529 * process also handles requests from the service manager (NT only).
531 * Signalling between the parent and working process uses a Win32
532 * event. Each child has a unique name for the event, which is
533 * passed to it with the -Z argument when the child is spawned. The
534 * parent sets (signals) this event to tell the child to die.
535 * At present all children do a graceful die - they finish all
536 * current jobs _and_ empty the listen queue before they exit.
537 * A non-graceful die would need a second event. The -Z argument in
538 * the child is also used to create the shutdown and restart events,
539 * since the prefix (apPID) contains the parent process PID.
541 * The code below starts with functions at the lowest level -
542 * worker threads, and works up to the top level - the main()
543 * function of the parent process.
545 * The scoreboard (in process memory) contains details of the worker
546 * threads (within the active working process). There is no shared
547 * "scoreboard" between processes, since only one is ever active
548 * at once (or at most, two, when one has been told to shutdown but
549 * is processes outstanding requests, and a new one has been started).
550 * This is controlled by a "start_mutex" which ensures only one working
551 * process is active at once.
552 **********************************************************************/
556 * Definition of jobs, shared by main and worker threads.
559 typedef struct joblist_s {
560 struct joblist_s *next;
565 * Globals common to main and worker threads. This structure is not
566 * used by the parent process.
569 typedef struct globals_s {
570 semaphore *jobsemaphore;
577 globals allowed_globals =
578 {NULL, NULL, NULL, NULL, 0};
579 #define MAX_SELECT_ERRORS 100
580 #define PADDED_ADDR_SIZE sizeof(SOCKADDR_IN)+16
582 /* Windows 9x specific code...
583 * Accept processing for on Windows 95/98 uses a producer/consumer queue
584 * model. A single thread accepts connections and queues the accepted socket
585 * to the accept queue for consumption by a pool of worker threads.
587 * win9x_get_connection()
588 * Calls remove_job() to pull a job from the accept queue. All the worker
589 * threads block on remove_job.
590 * accept_and_queue_connections()
591 * The accept threads runs this function, which accepts connections off
592 * the network and calls add_job() to queue jobs to the accept_queue.
593 * add_job()/remove_job()
594 * Add or remove an accepted socket from the list of sockets
595 * connected to clients. allowed_globals.jobmutex protects
596 * against multiple concurrent access to the linked list of jobs.
598 static void add_job(int sock)
602 new_job = (joblist *) malloc(sizeof(joblist));
603 if (new_job == NULL) {
604 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
605 "Ouch! Out of memory in add_job()!");
608 new_job->next = NULL;
609 new_job->sock = sock;
611 ap_lock(allowed_globals.jobmutex);
613 if (allowed_globals.jobtail != NULL)
614 allowed_globals.jobtail->next = new_job;
615 allowed_globals.jobtail = new_job;
616 if (!allowed_globals.jobhead)
617 allowed_globals.jobhead = new_job;
618 allowed_globals.jobcount++;
619 release_semaphore(allowed_globals.jobsemaphore);
621 ap_unlock(allowed_globals.jobmutex);
624 static int remove_job(void)
629 acquire_semaphore(allowed_globals.jobsemaphore);
630 ap_lock(allowed_globals.jobmutex);
632 if (workers_may_exit && !allowed_globals.jobhead) {
633 ap_unlock(allowed_globals.jobmutex);
636 job = allowed_globals.jobhead;
638 allowed_globals.jobhead = job->next;
639 if (allowed_globals.jobhead == NULL)
640 allowed_globals.jobtail = NULL;
641 ap_unlock(allowed_globals.jobmutex);
648 static void accept_and_queue_connections(void * dummy)
650 int requests_this_child = 0;
655 int nsd = INVALID_SOCKET;
656 struct sockaddr_in sa_client;
657 int count_select_errors = 0;
661 while (!workers_may_exit) {
662 if (ap_max_requests_per_child && (requests_this_child > ap_max_requests_per_child)) {
666 tv.tv_sec = wait_time;
668 memcpy(&main_fds, &listenfds, sizeof(fd_set));
670 // rc = ap_select(listenmaxfd + 1, &main_fds, NULL, NULL, &tv);
671 rc = select(listenmaxfd + 1, &main_fds, NULL, NULL, &tv);
673 if (rc == 0 || (rc == SOCKET_ERROR && h_errno == WSAEINTR)) {
674 count_select_errors = 0; /* reset count of errors */
677 else if (rc == SOCKET_ERROR) {
678 /* A "real" error occurred, log it and increment the count of
679 * select errors. This count is used to ensure we don't go into
680 * a busy loop of continuous errors.
682 ap_log_error(APLOG_MARK, APLOG_INFO, h_errno, server_conf,
683 "select failed with errno %d", h_errno);
684 count_select_errors++;
685 if (count_select_errors > MAX_SELECT_ERRORS) {
686 workers_may_exit = 1;
687 ap_log_error(APLOG_MARK, APLOG_ERR, h_errno, server_conf,
688 "Too many errors in select loop. Child process exiting.");
694 lr = find_ready_listener(&main_fds);
696 /* fetch the native socket descriptor */
697 ap_get_os_sock(&nsd, lr->sd);
702 clen = sizeof(sa_client);
703 csd = accept(nsd, (struct sockaddr *) &sa_client, &clen);
704 if (csd == INVALID_SOCKET) {
707 } while (csd < 0 && h_errno == WSAEINTR);
710 if (h_errno != WSAECONNABORTED) {
711 ap_log_error(APLOG_MARK, APLOG_ERR, h_errno, server_conf,
712 "accept: (client socket)");
717 requests_this_child++;
720 SetEvent(exit_event);
722 static PCOMP_CONTEXT win9x_get_connection(PCOMP_CONTEXT context)
726 if (context == NULL) {
727 /* allocate the completion context and the transaction pool */
728 context = ap_pcalloc(pconf, sizeof(COMP_CONTEXT));
730 ap_log_error(APLOG_MARK,APLOG_ERR, GetLastError(), server_conf,
731 "win9x_get_connection: ap_pcalloc() failed. Process will exit.");
734 ap_create_pool(&context->ptrans, pconf);
739 ap_clear_pool(context->ptrans);
740 context->accept_socket = remove_job();
741 if (context->accept_socket == -1) {
744 //ap_note_cleanups_for_socket(ptrans, csd);
745 len = sizeof(struct sockaddr);
746 context->sa_server = ap_palloc(context->ptrans, len);
747 if (getsockname(context->accept_socket,
748 context->sa_server, &len)== SOCKET_ERROR) {
749 ap_log_error(APLOG_MARK, APLOG_WARNING, WSAGetLastError(), server_conf,
750 "getsockname failed");
753 len = sizeof(struct sockaddr);
754 context->sa_client = ap_palloc(context->ptrans, len);
755 if ((getpeername(context->accept_socket,
756 context->sa_client, &len)) == SOCKET_ERROR) {
757 ap_log_error(APLOG_MARK, APLOG_WARNING, h_errno, server_conf,
758 "getpeername failed with error %d\n", WSAGetLastError());
759 memset(&context->sa_client, '\0', sizeof(context->sa_client));
762 context->conn_io = ap_bcreate(context->ptrans, B_RDWR);
764 /* do we NEED_DUPPED_CSD ?? */
771 * Windows 2000/NT specific code...
772 * create_acceptex_context()
773 * reset_acceptex_context()
774 * drain_acceptex_complport()
775 * winnt_get_connection()
777 * TODO: Insert a discussion of 'completion contexts' and what these function do here...
779 static void drain_acceptex_complport(HANDLE hComplPort, BOOLEAN bCleanUp)
782 PCOMP_CONTEXT context;
790 rc = GetQueuedCompletionStatus(hComplPort, &BytesRead, &CompKey,
793 lastError = GetLastError();
794 if (lastError == ERROR_OPERATION_ABORTED) {
795 ap_log_error(APLOG_MARK,APLOG_INFO,lastError, server_conf,
796 "Child %d: - Draining a packet off the completion port.", my_pid);
801 ap_log_error(APLOG_MARK,APLOG_INFO,APR_SUCCESS, server_conf,
802 "Child %d: - Nuking an active connection. context = %x", my_pid, context);
803 context = (PCOMP_CONTEXT) pol;
804 if (context && bCleanUp) {
805 /* It is only valid to clean-up in the process that initiated the I/O */
806 closesocket(context->accept_socket);
807 CloseHandle(context->Overlapped.hEvent);
811 static int create_acceptex_context(ap_pool_t *_pconf, ap_listen_rec *lr)
813 PCOMP_CONTEXT context;
818 /* allocate the completion context */
819 context = ap_pcalloc(_pconf, sizeof(COMP_CONTEXT));
821 ap_log_error(APLOG_MARK,APLOG_ERR, GetLastError(), server_conf,
822 "create_acceptex_context: ap_pcalloc() failed. Process will exit.");
826 /* initialize the completion context */
828 context->Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
829 if (context->Overlapped.hEvent == NULL) {
830 ap_log_error(APLOG_MARK,APLOG_ERR, GetLastError(), server_conf,
831 "create_acceptex_context: CreateEvent() failed. Process will exit.");
835 /* create and initialize the accept socket */
836 ap_get_os_sock(&nsd, context->lr->sd);
837 context->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
838 if (context->accept_socket == INVALID_SOCKET) {
839 ap_log_error(APLOG_MARK,APLOG_ERR, WSAGetLastError(), server_conf,
840 "create_acceptex_context: socket() failed. Process will exit.");
844 /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
845 if (setsockopt(context->accept_socket, SOL_SOCKET,
846 SO_UPDATE_ACCEPT_CONTEXT, (char *)&nsd,
848 ap_log_error(APLOG_MARK, APLOG_ERR, WSAGetLastError(), server_conf,
849 "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.");
850 /* Not a failure condition. Keep running. */
853 ap_create_pool(&context->ptrans, _pconf);
854 context->conn_io = ap_bcreate(context->ptrans, B_RDWR);
855 context->recv_buf = context->conn_io->inbase;
856 context->recv_buf_size = context->conn_io->bufsiz - 2*PADDED_ADDR_SIZE;
859 /* AcceptEx on the completion context. The completion context will be signaled
860 * when a connection is accepted. */
861 if (!AcceptEx(nsd, context->accept_socket,
863 0, //context->recv_buf_size,
864 PADDED_ADDR_SIZE, PADDED_ADDR_SIZE,
866 (LPOVERLAPPED) context)) {
867 lasterror = WSAGetLastError();
868 if (lasterror != ERROR_IO_PENDING) {
869 ap_log_error(APLOG_MARK,APLOG_ERR, WSAGetLastError(), server_conf,
870 "create_acceptex_context: AcceptEx failed. Process will exit.");
879 static ap_inline ap_status_t reset_acceptex_context(PCOMP_CONTEXT context)
885 context->lr->count++;
887 /* recreate and initialize the accept socket if it is not being reused */
888 ap_get_os_sock(&nsd, context->lr->sd);
889 if (context->accept_socket == INVALID_SOCKET) {
890 context->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
891 if (context->accept_socket == INVALID_SOCKET) {
892 lasterror = WSAGetLastError();
893 ap_log_error(APLOG_MARK,APLOG_ERR, lasterror, server_conf,
894 "reset_acceptex_context: socket() failed. Process will exit.");
898 /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
899 if (setsockopt(context->accept_socket, SOL_SOCKET,
900 SO_UPDATE_ACCEPT_CONTEXT, (char *)&nsd,
902 ap_log_error(APLOG_MARK, APLOG_WARNING, WSAGetLastError(),
904 "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.");
905 /* Not a failure condition. Keep running. */
909 /* reset the completion context */
910 ap_clear_pool(context->ptrans);
911 context->sock = NULL;
912 context->conn_io = ap_bcreate(context->ptrans, B_RDWR);
913 context->recv_buf = context->conn_io->inbase;
914 context->recv_buf_size = context->conn_io->bufsiz - 2*PADDED_ADDR_SIZE;
916 /* AcceptEx on the completion context. The completion context will be signaled
917 * when a connection is accepted. */
918 if (!AcceptEx(nsd, context->accept_socket, context->recv_buf, 0,
919 PADDED_ADDR_SIZE, PADDED_ADDR_SIZE, &BytesRead,
920 (LPOVERLAPPED) context)) {
921 lasterror = WSAGetLastError();
922 if (lasterror != ERROR_IO_PENDING) {
923 ap_log_error(APLOG_MARK, APLOG_INFO, lasterror, server_conf,
924 "reset_acceptex_context: AcceptEx failed for "
925 "listening socket: %d and accept socket: %d",
926 nsd, context->accept_socket);
927 context->accept_socket = INVALID_SOCKET;
934 static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context)
936 int requests_this_child = 0;
942 if (workers_may_exit) {
943 /* Child shutdown has been signaled */
945 CloseHandle(context->Overlapped.hEvent);
948 if (!workers_may_exit && (context != NULL)) {
949 /* Prepare the completion context for reuse */
950 if (reset_acceptex_context(context) != APR_SUCCESS) {
951 /* Retry once, this time requesting a new socket */
952 if (context->accept_socket != INVALID_SOCKET) {
953 closesocket(context->accept_socket);
954 context->accept_socket = INVALID_SOCKET;
956 if (reset_acceptex_context(context) != APR_SUCCESS) {
957 /* Failed again, so give up, but leave the thread up
958 * Should we signal a shutdown now?
960 if (context->accept_socket != INVALID_SOCKET)
961 closesocket(context->accept_socket);
962 CloseHandle(context->Overlapped.hEvent);
968 rc = GetQueuedCompletionStatus(AcceptExCompPort,
975 /* During a restart, the new child process can catch
976 * ERROR_OPERATION_ABORTED completion packets
977 * posted by the old child process. Just continue...
979 ap_log_error(APLOG_MARK,APLOG_ERR, GetLastError(), server_conf,
980 "Child %d: - GetQueuedCompletionStatus() failed", my_pid);
984 /* Check the Completion Key.
985 * == my_pid indicate this process wants to exit
986 * == 0 implies valid i/o completion
987 * != 0 implies a posted completion packet by an old
988 * process. Just ignore it.
990 if (CompKey == my_pid) {
997 context = (PCOMP_CONTEXT) pol;
1001 /* Check to see if we need to create more completion contexts,
1002 * but only if we are not in the process of shutting down
1004 if (!workers_may_exit) {
1005 ap_lock(allowed_globals.jobmutex);
1006 context->lr->count--;
1007 if (context->lr->count < 2) {
1008 SetEvent(maintenance_event);
1010 ap_unlock(allowed_globals.jobmutex);
1013 /* Received a connection */
1014 context->conn_io->incnt = BytesRead;
1015 GetAcceptExSockaddrs(context->recv_buf,
1016 0, //context->recv_buf_size,
1019 &context->sa_server,
1020 &context->sa_server_len,
1021 &context->sa_client,
1022 &context->sa_client_len);
1028 * worker_main() - this is the main loop for the worker threads
1031 * Each thread runs within this function. They wait within remove_job()
1032 * for a job to become available, then handle all the requests on that
1033 * connection until it is closed, then return to remove_job().
1035 * The worker thread will exit when it removes a job which contains
1036 * socket number -1. This provides a graceful thread exit, since
1037 * it will never exit during a connection.
1039 * This code in this function is basically equivalent to the child_main()
1040 * from the multi-process (Unix) environment, except that we
1042 * - do not call child_init_modules (child init API phase)
1043 * - block in remove_job, and when unblocked we have an already
1044 * accepted socket, instead of blocking on a mutex or select().
1047 static void worker_main(int child_num)
1049 PCOMP_CONTEXT context = NULL;
1052 conn_rec *current_conn;
1055 /* Grab a connection off the network */
1056 if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
1057 context = win9x_get_connection(context);
1060 context = winnt_get_connection(context);
1065 sock_disable_nagle(context->accept_socket);
1066 ap_put_os_sock(&context->sock, &context->accept_socket, context->ptrans);
1068 iol = win32_attach_socket(context->ptrans, context->sock);
1070 ap_log_error(APLOG_MARK, APLOG_ERR, APR_ENOMEM, server_conf,
1071 "worker_main: attach_socket() failed. Continuing...");
1072 closesocket(context->accept_socket);
1075 ap_bpush_iol(context->conn_io, iol);
1076 current_conn = ap_new_connection(context->ptrans, server_conf, context->conn_io,
1077 (struct sockaddr_in *) context->sa_client,
1078 (struct sockaddr_in *) context->sa_server,
1081 ap_process_connection(current_conn);
1082 ap_lingering_close(current_conn);
1083 context->accept_socket = INVALID_SOCKET;
1086 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1087 "Child %d: Thread exiting.", my_pid);
1090 SetEvent(exit_event);
1092 /* TODO: Add code to clean-up completion contexts here */
1095 static void cleanup_thread(thread **handles, int *thread_cnt, int thread_to_clean)
1099 CloseHandle(handles[thread_to_clean]);
1100 for (i = thread_to_clean; i < ((*thread_cnt) - 1); i++)
1101 handles[i] = handles[i + 1];
1105 static void create_listeners()
1107 #define NUM_LISTENERS 5
1109 for (lr = ap_listeners; lr != NULL; lr = lr->next) {
1110 while (lr->count < NUM_LISTENERS) {
1111 if (create_acceptex_context(pconf, lr) == -1) {
1112 ap_log_error(APLOG_MARK,APLOG_ERR, GetLastError(), server_conf,
1113 "Unable to create an AcceptEx completion context -- process will exit");
1114 signal_parent(0); /* tell parent to die */
1120 * child_main() runs the main control thread for the child process.
1122 * The control thread:
1123 * - sets up the worker thread pool
1124 * - starts the accept thread (Win 9x)
1125 * - creates AcceptEx contexts (Win NT)
1126 * - waits for exit_event, maintenance_event or maintenance timeout
1127 * and does the right thing depending on which event is received.
1129 static void child_main()
1132 HANDLE child_events[2];
1133 char* exit_event_name;
1134 int nthreads = ap_threads_per_child;
1136 thread **child_handles;
1144 /* This is the child process or we are running in single process
1147 exit_event_name = ap_psprintf(pconf, "apC%d", my_pid);
1148 setup_signal_names(ap_psprintf(pconf,"ap%d", parent_pid));
1151 /* Single process mode */
1152 ap_create_lock(&start_mutex,APR_MUTEX, APR_CROSS_PROCESS,signal_name_prefix,pconf);
1153 exit_event = CreateEvent(NULL, TRUE, FALSE, exit_event_name);
1155 setup_listeners(server_conf);
1156 bind_listeners_to_completion_port();
1159 /* Child process mode */
1160 ap_child_init_lock(&start_mutex, signal_name_prefix, pconf);
1161 exit_event = OpenEvent(EVENT_ALL_ACCESS, FALSE, exit_event_name);
1162 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1163 "Child %d: exit_event_name = %s", my_pid, exit_event_name);
1165 setup_inherited_listeners(server_conf);
1168 /* Initialize the child_events */
1169 maintenance_event = CreateEvent(NULL, TRUE, FALSE, NULL);
1170 child_events[0] = exit_event;
1171 child_events[1] = maintenance_event;
1173 ap_assert(start_mutex);
1174 ap_assert(exit_event);
1175 ap_assert(maintenance_event);
1177 ap_create_pool(&pchild, pconf);
1178 allowed_globals.jobsemaphore = create_semaphore(0);
1179 ap_create_lock(&allowed_globals.jobmutex, APR_MUTEX, APR_INTRAPROCESS, NULL, pchild);
1182 * Wait until we have permission to start accepting connections.
1183 * start_mutex is used to ensure that only one child ever
1184 * goes into the listen/accept loop at once.
1186 status = ap_lock(start_mutex);
1187 if (status != APR_SUCCESS) {
1188 ap_log_error(APLOG_MARK,APLOG_ERR, status, server_conf,
1189 "Child %d: Failed to acquire the start_mutex. Process will exit.", my_pid);
1190 signal_parent(0); /* tell parent to die */
1193 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1194 "Child %d: Acquired the start mutex.", my_pid);
1196 /* Create the worker thread pool */
1197 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1198 "Child %d: Starting %d worker threads.", my_pid, nthreads);
1199 child_handles = (thread *) alloca(nthreads * sizeof(int));
1200 for (i = 0; i < nthreads; i++) {
1201 child_handles[i] = (thread *) _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) worker_main,
1202 NULL, 0, &thread_id);
1205 /* Begin accepting connections */
1206 if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
1207 /* Win95/98: Start the accept thread */
1208 _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) accept_and_queue_connections,
1209 (void *) i, 0, &thread_id);
1211 /* Windows NT/2000: Create AcceptEx completion contexts */
1215 /* Wait for one of three events:
1217 * The exit_event is signaled by the parent process to notify
1218 * the child that it is time to exit.
1220 * maintenance_event:
1221 * This event is signaled by the worker thread pool to direct
1222 * this thread to create more completion contexts.
1225 * To do periodic maintenance on the server (check for thread exits,
1226 * number of completion contexts, etc.)
1228 while (!workers_may_exit) {
1229 rv = WaitForMultipleObjects(2, (HANDLE *) child_events, FALSE, INFINITE);
1230 cld = rv - WAIT_OBJECT_0;
1231 if (rv == WAIT_FAILED) {
1232 /* Something serious is wrong */
1233 workers_may_exit = 1;
1234 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
1235 "Child %d: WAIT_FAILED -- shutting down server");
1237 else if (rv == WAIT_TIMEOUT) {
1238 /* Hey, this cannot happen */
1239 workers_may_exit = 1;
1240 ap_log_error(APLOG_MARK, APLOG_CRIT, APR_SUCCESS, server_conf,
1241 "Child %d: WAIT_TIMEOUT -- shutting down server", my_pid);
1243 else if (cld == 0) {
1244 /* Exit event was signaled */
1245 workers_may_exit = 1;
1246 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1247 "Child %d: Exit event signaled. Child process is ending.", my_pid);
1250 /* Child maintenance event signaled */
1251 if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
1254 ResetEvent(maintenance_event);
1255 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1256 "Child %d: Child maintenance event signaled.", my_pid);
1260 /* Setting is_graceful will close keep-alive connections
1263 /* Shutdown the worker threads */
1264 if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
1265 for (i = 0; i < nthreads; i++) {
1269 else { /* Windows NT/2000 */
1273 * First thing to do is to drain all the completion contexts off the
1274 * AcceptEx iocp. Give a busy server the chance to drain
1275 * the port by servicing connections (workers_may_exit prevents new
1276 * AcceptEx completion contexts from being queued to the port).
1280 /* Cancel any remaining pending async i/o.
1281 * This will cause io completion packets to be queued to the
1282 * port for any remaining active contexts
1284 for (lr = ap_listeners; lr != NULL; lr = lr->next) {
1285 ap_get_os_sock(&nsd,lr->sd);
1289 /* Drain the canceled contexts off the port */
1290 drain_acceptex_complport(AcceptExCompPort, FALSE);
1292 /* Hopefully by now, all the completion contexts should be drained
1293 * off the port. There could still be some cancel io completion packets
1294 * flying around in the kernel... We will cover this possibility later..
1296 * Consider using HasOverlappedIoCompleted()...
1298 * Next task is to unblock all the threads blocked on
1299 * GetQueuedCompletionStatus()
1302 for (i=0; i < nthreads*2; i++) {
1303 PostQueuedCompletionStatus(AcceptExCompPort, 0, my_pid, NULL);
1306 /* Give the worker threads time to realize they've been posted */
1310 /* Release the start_mutex to let the new process (in the restart
1311 * scenario) a chance to begin servicing requests
1313 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1314 "Child %d: Releasing the start mutex", my_pid);
1315 ap_unlock(start_mutex);
1317 /* Give busy worker threads a chance to service their connections.
1318 * Kill them off if they take too long
1320 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1321 "Child %d: Waiting for %d threads to die.", my_pid, nthreads);
1322 end_time = time(NULL) + 180;
1324 rv = wait_for_many_objects(nthreads, child_handles, end_time - time(NULL));
1325 if (rv != WAIT_TIMEOUT) {
1326 rv = rv - WAIT_OBJECT_0;
1327 ap_assert((rv >= 0) && (rv < nthreads));
1328 cleanup_thread(child_handles, &nthreads, rv);
1333 for (i = 0; i < nthreads; i++) {
1334 TerminateThread(child_handles[i], 1);
1335 CloseHandle(child_handles[i]);
1337 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1338 "Child %d: All worker threads have ended.", my_pid);
1340 CloseHandle(AcceptExCompPort);
1341 destroy_semaphore(allowed_globals.jobsemaphore);
1342 ap_destroy_lock(allowed_globals.jobmutex);
1344 ap_destroy_pool(pchild);
1345 CloseHandle(exit_event);
1349 * Spawn a child Apache process. The child process has the command line arguments from
1350 * argc and argv[], plus a -Z argument giving the name of an event. The child should
1351 * open and poll or wait on this event. When it is signalled, the child should die.
1352 * prefix is a prefix string for the event name.
1354 * The child_num argument on entry contains a serial number for this child (used to create
1355 * a unique event name). On exit, this number will have been incremented by one, ready
1356 * for the next call.
1358 * On exit, the value pointed to be *ev will contain the event created
1359 * to signal the new child process.
1361 * The return value is the handle to the child process if successful, else -1. If -1 is
1362 * returned the error will already have been logged by ap_log_error().
1365 /**********************************************************************
1366 * master_main - this is the parent (main) process. We create a
1367 * child process to do the work, then sit around waiting for either
1368 * the child to exit, or a restart or exit signal. If the child dies,
1369 * we just respawn a new one. If we have a shutdown or graceful restart,
1370 * tell the child to die when it is ready. If it is a non-graceful
1371 * restart, force the child to die immediately.
1372 **********************************************************************/
1374 #define MAX_PROCESSES 50 /* must be < MAX_WAIT_OBJECTS-1 */
1376 static void cleanup_process(HANDLE *handles, HANDLE *events, int position, int *processes)
1381 CloseHandle(handles[position]);
1382 CloseHandle(events[position]);
1384 handle = (int)handles[position];
1386 for (i = position; i < (*processes)-1; i++) {
1387 handles[i] = handles[i + 1];
1388 events[i] = events[i + 1];
1393 static int create_process(ap_pool_t *p, HANDLE *handles, HANDLE *events, int *processes)
1403 STARTUPINFO si; /* Filled in prior to call to CreateProcess */
1404 PROCESS_INFORMATION pi; /* filled in on call to CreateProces */
1408 HANDLE hPipeRead = NULL;
1409 HANDLE hPipeWrite = NULL;
1410 SECURITY_ATTRIBUTES sa = {0};
1412 sa.nLength = sizeof(sa);
1413 sa.bInheritHandle = TRUE;
1414 sa.lpSecurityDescriptor = NULL;
1416 /* Build the command line. Should look something like this:
1417 * C:/apache/bin/apache.exe -f ap_server_confname
1418 * First, get the path to the executable...
1420 rv = GetModuleFileName(NULL, buf, sizeof(buf));
1421 if (rv == sizeof(buf)) {
1422 ap_log_error(APLOG_MARK, APLOG_CRIT, ERROR_BAD_PATHNAME, server_conf,
1423 "Parent: Path to Apache process too long");
1425 } else if (rv == 0) {
1426 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
1427 "Parent: GetModuleFileName() returned NULL for current process.");
1431 /* Build the command line */
1432 pCommand = ap_psprintf(p, "\"%s\"", buf);
1433 for (i = 1; i < server_conf->process->argc; i++) {
1434 pCommand = ap_pstrcat(p, pCommand, " \"", server_conf->process->argv[i], "\"", NULL);
1437 /* Build the environment, since Win9x disrespects the active env */
1438 // SetEnvironmentVariable("AP_PARENT_PID",ap_psprintf(p,"%l",parent_pid));
1439 pEnvVar = ap_psprintf(p, "AP_PARENT_PID=%i", parent_pid);
1441 * Win32's CreateProcess call requires that the environment
1442 * be passed in an environment block, a null terminated block of
1443 * null terminated strings.
1447 while (_environ[i]) {
1448 iEnvBlockLen += strlen(_environ[i]) + 1;
1452 pEnvBlock = (char *)ap_pcalloc(p, iEnvBlockLen + strlen(pEnvVar) + 1);
1453 strcpy(pEnvBlock, pEnvVar);
1454 pEnvVar = strchr(pEnvBlock, '\0') + 1;
1457 while (_environ[i]) {
1458 strcpy(pEnvVar, _environ[i]);
1459 pEnvVar = strchr(pEnvVar, '\0') + 1;
1463 /* Create a pipe to send socket info to the child */
1464 if (!CreatePipe(&hPipeRead, &hPipeWrite, &sa, 0)) {
1465 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
1466 "Parent: Unable to create pipe to child process.\n");
1470 /* Give the read end of the pipe (hPipeRead) to the child as stdin. The
1471 * parent will write the socket data to the child on this pipe.
1473 memset(&si, 0, sizeof(si));
1474 memset(&pi, 0, sizeof(pi));
1476 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
1477 si.wShowWindow = SW_HIDE;
1478 si.hStdInput = hPipeRead;
1480 if (!CreateProcess(NULL, pCommand, NULL, NULL,
1481 TRUE, /* Inherit handles */
1482 CREATE_SUSPENDED, /* Creation flags */
1483 pEnvBlock, /* Environment block */
1486 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
1487 "Parent: Not able to create the child process.");
1489 * We must close the handles to the new process and its main thread
1490 * to prevent handle and memory leaks.
1492 CloseHandle(pi.hProcess);
1493 CloseHandle(pi.hThread);
1498 LPWSAPROTOCOL_INFO lpWSAProtocolInfo;
1499 HANDLE hDupedCompPort;
1501 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1502 "Parent: Created child process %d", pi.dwProcessId);
1504 SetEnvironmentVariable("AP_PARENT_PID",NULL);
1506 /* Create the exit_event, apCchild_pid */
1507 sa.nLength = sizeof(sa);
1508 sa.bInheritHandle = TRUE;
1509 sa.lpSecurityDescriptor = NULL;
1510 kill_event = CreateEvent(&sa, TRUE, FALSE, ap_psprintf(pconf,"apC%d", pi.dwProcessId));
1512 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
1513 "Parent: Could not create exit event for child process");
1514 CloseHandle(pi.hProcess);
1515 CloseHandle(pi.hThread);
1519 /* Assume the child process lives. Update the process and event tables */
1520 handles[*processes] = pi.hProcess;
1521 events[*processes] = kill_event;
1524 /* We never store the thread's handle, so close it now. */
1525 ResumeThread(pi.hThread);
1526 CloseHandle(pi.hThread);
1528 /* Run the chain of open sockets. For each socket, duplicate it
1529 * for the target process then send the WSAPROTOCOL_INFO
1530 * (returned by dup socket) to the child */
1531 for (lr = ap_listeners; lr; lr = lr->next) {
1533 lpWSAProtocolInfo = ap_pcalloc(p, sizeof(WSAPROTOCOL_INFO));
1534 ap_get_os_sock(&nsd,lr->sd);
1535 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1536 "Parent: Duplicating socket %d and sending it to child process %d", nsd, pi.dwProcessId);
1537 if (WSADuplicateSocket(nsd, pi.dwProcessId,
1538 lpWSAProtocolInfo) == SOCKET_ERROR) {
1539 ap_log_error(APLOG_MARK, APLOG_CRIT, h_errno, server_conf,
1540 "Parent: WSADuplicateSocket failed for socket %d.", lr->sd );
1544 if (!WriteFile(hPipeWrite, lpWSAProtocolInfo, (DWORD) sizeof(WSAPROTOCOL_INFO),
1546 (LPOVERLAPPED) NULL)) {
1547 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
1548 "Parent: Unable to write duplicated socket %d to the child.", lr->sd );
1551 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
1552 "Parent: BytesWritten = %d WSAProtocolInfo = %x20", BytesWritten, *lpWSAProtocolInfo);
1554 if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
1555 /* Now, send the AcceptEx completion port to the child */
1556 if (!DuplicateHandle(GetCurrentProcess(), AcceptExCompPort,
1557 pi.hProcess, &hDupedCompPort, 0,
1558 TRUE, DUPLICATE_SAME_ACCESS)) {
1559 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
1560 "Parent: Unable to duplicate AcceptEx completion port. Shutting down.");
1564 WriteFile(hPipeWrite, &hDupedCompPort, (DWORD) sizeof(hDupedCompPort), &BytesWritten, (LPOVERLAPPED) NULL);
1568 CloseHandle(hPipeRead);
1569 CloseHandle(hPipeWrite);
1574 static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_event)
1576 int remaining_children_to_start = ap_daemons_to_start;
1580 int restart_pending = 0;
1581 int shutdown_pending = 0;
1582 int current_live_processes = 0; /* number of child process we know about */
1584 HANDLE process_handles[MAX_PROCESSES];
1585 HANDLE process_kill_events[MAX_PROCESSES];
1588 bind_listeners_to_completion_port();
1590 /* Create child process
1591 * Should only be one in this version of Apache for WIN32
1593 //service_set_status(SERVICE_START_PENDING);
1594 while (remaining_children_to_start--) {
1595 if (create_process(pconf, process_handles, process_kill_events,
1596 ¤t_live_processes) < 0) {
1597 ap_log_error(APLOG_MARK, APLOG_CRIT, GetLastError(), server_conf,
1598 "master_main: create child process failed. Exiting.");
1599 shutdown_pending = 1;
1603 //service_set_status(SERVICE_RUNNING);
1605 restart_pending = shutdown_pending = 0;
1607 /* Wait for shutdown or restart events or for child death */
1608 process_handles[current_live_processes] = shutdown_event;
1609 process_handles[current_live_processes+1] = restart_event;
1611 rv = WaitForMultipleObjects(current_live_processes+2, (HANDLE *)process_handles,
1613 cld = rv - WAIT_OBJECT_0;
1614 if (rv == WAIT_FAILED) {
1615 /* Something serious is wrong */
1616 ap_log_error(APLOG_MARK,APLOG_CRIT, GetLastError(), server_conf,
1617 "master_main: WaitForMultipeObjects WAIT_FAILED -- doing server shutdown");
1618 shutdown_pending = 1;
1620 else if (rv == WAIT_TIMEOUT) {
1621 /* Hey, this cannot happen */
1622 ap_log_error(APLOG_MARK, APLOG_ERR, GetLastError(), s,
1623 "master_main: WaitForMultipeObjects with INFINITE wait exited with WAIT_TIMEOUT");
1624 shutdown_pending = 1;
1626 else if (cld == current_live_processes) {
1627 /* shutdown_event signalled */
1628 shutdown_pending = 1;
1629 printf("shutdown event signaled\n");
1630 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, s,
1631 "master_main: Shutdown event signaled -- doing server shutdown.");
1632 if (ResetEvent(shutdown_event) == 0) {
1633 ap_log_error(APLOG_MARK, APLOG_ERR, GetLastError(), s,
1634 "ResetEvent(shutdown_event)");
1638 else if (cld == current_live_processes+1) {
1639 /* restart_event signalled */
1640 int children_to_kill = current_live_processes;
1641 restart_pending = 1;
1642 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, s,
1643 "master_main: Restart event signaled. Doing a graceful restart.");
1644 if (ResetEvent(restart_event) == 0) {
1645 ap_log_error(APLOG_MARK, APLOG_ERR, GetLastError(), s,
1646 "master_main: ResetEvent(restart_event) failed.");
1648 /* Signal each child process to die
1649 * We are making a big assumption here that the child process, once signaled,
1650 * will REALLY go away. Since this is a restart, we do not want to hold the
1651 * new child process up waiting for the old child to die. Remove the old
1652 * child out of the process_handles ap_table_t and hope for the best...
1654 for (i = 0; i < children_to_kill; i++) {
1655 if (SetEvent(process_kill_events[i]) == 0)
1656 ap_log_error(APLOG_MARK, APLOG_ERR, GetLastError(), s,
1657 "master_main: SetEvent for child process in slot #%d failed", i);
1658 cleanup_process(process_handles, process_kill_events, i, ¤t_live_processes);
1662 /* A child process must have exited because of a fatal error condition (seg fault, etc.).
1663 * Remove the dead process
1664 * from the process_handles and process_kill_events ap_table_t and create a new
1666 * TODO: Consider restarting the child immediately without looping through http_main
1667 * and without rereading the configuration. Will need this if we ever support multiple
1668 * children. One option, create a parent thread which waits on child death and restarts it.
1670 restart_pending = 1;
1671 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
1672 "master_main: Child process failed. Restarting the child process.");
1673 ap_assert(cld < current_live_processes);
1674 cleanup_process(process_handles, process_kill_events, cld, ¤t_live_processes);
1675 /* APD2("main_process: child in slot %d died", rv); */
1676 /* restart_child(process_hancles, process_kill_events, cld, ¤t_live_processes); */
1678 /* Drain the AcceptEx completion port of any outstanding I/O pending for the dead
1680 drain_acceptex_complport(AcceptExCompPort, FALSE);
1684 if (shutdown_pending) {
1685 int tmstart = time(NULL);
1686 /* Signal each child processes to die */
1687 for (i = 0; i < current_live_processes; i++) {
1688 printf("SetEvent handle = %d\n", process_kill_events[i]);
1689 if (SetEvent(process_kill_events[i]) == 0)
1690 ap_log_error(APLOG_MARK,APLOG_ERR, GetLastError(), server_conf,
1691 "master_main: SetEvent for child process in slot #%d failed", i);
1694 while (current_live_processes && ((tmstart+60) > time(NULL))) {
1695 rv = WaitForMultipleObjects(current_live_processes, (HANDLE *)process_handles, FALSE, 2000);
1696 if (rv == WAIT_TIMEOUT)
1698 ap_assert(rv != WAIT_FAILED);
1699 cld = rv - WAIT_OBJECT_0;
1700 ap_assert(rv < current_live_processes);
1701 cleanup_process(process_handles, process_kill_events, cld, ¤t_live_processes);
1703 for (i = 0; i < current_live_processes; i++) {
1704 ap_log_error(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO, APR_SUCCESS, server_conf,
1705 "forcing termination of child #%d (handle %d)", i, process_handles[i]);
1706 TerminateProcess((HANDLE) process_handles[i], 1);
1708 return 0; /* Tell the caller we do not want to restart */
1711 return 1; /* Tell the caller we want a restart */
1715 * winnt_pre_config() hook
1717 static void winnt_pre_config(ap_pool_t *pconf, ap_pool_t *plog, ap_pool_t *ptemp)
1721 one_process = !!getenv("ONE_PROCESS");
1723 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1724 GetVersionEx(&osver);
1726 /* AP_PARENT_PID is only valid in the child */
1727 pid = getenv("AP_PARENT_PID");
1729 /* This is the child */
1730 parent_pid = (DWORD) atol(pid);
1731 my_pid = GetCurrentProcessId();
1734 /* This is the parent */
1735 parent_pid = my_pid = GetCurrentProcessId();
1738 ap_listen_pre_config();
1739 ap_daemons_to_start = DEFAULT_NUM_DAEMON;
1740 ap_threads_per_child = DEFAULT_START_THREAD;
1741 ap_pid_fname = DEFAULT_PIDLOG;
1742 max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
1744 ap_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1748 static void winnt_post_config(ap_pool_t *pconf, ap_pool_t *plog, ap_pool_t *ptemp, server_rec* server_conf)
1750 static int restart_num = 0;
1751 server_conf = server_conf;
1753 if (parent_pid == my_pid) {
1754 if (restart_num++ == 1) {
1755 /* This code should be run once in the parent and not run
1758 PSECURITY_ATTRIBUTES sa = GetNullACL(); /* returns NULL if invalid (Win95?) */
1759 setup_signal_names(ap_psprintf(pconf,"ap%d", parent_pid));
1760 if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
1761 /* Create the AcceptEx IoCompletionPort once in the parent.
1762 * The completion port persists across restarts.
1764 AcceptExCompPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
1767 0); /* CONCURRENT ACTIVE THREADS */
1768 if (AcceptExCompPort == NULL) {
1769 ap_log_error(APLOG_MARK,APLOG_ERR, GetLastError(), server_conf,
1770 "Parent: Unable to create the AcceptExCompletionPort -- process will exit");
1775 ap_log_pid(pconf, ap_pid_fname);
1776 //service_set_status(SERVICE_START_PENDING);
1778 /* Create shutdown event, apPID_shutdown, where PID is the parent
1779 * Apache process ID. Shutdown is signaled by 'apache -k shutdown'.
1781 shutdown_event = CreateEvent(sa, FALSE, FALSE, signal_shutdown_name);
1782 if (!shutdown_event) {
1783 ap_log_error(APLOG_MARK, APLOG_EMERG, GetLastError(), server_conf,
1784 "Parent: Cannot create shutdown event %s", signal_shutdown_name);
1785 CleanNullACL((void *)sa);
1789 /* Create restart event, apPID_restart, where PID is the parent
1790 * Apache process ID. Restart is signaled by 'apache -k restart'.
1792 restart_event = CreateEvent(sa, FALSE, FALSE, signal_restart_name);
1793 if (!restart_event) {
1794 CloseHandle(shutdown_event);
1795 ap_log_error(APLOG_MARK, APLOG_EMERG, GetLastError(), server_conf,
1796 "Parent: Cannot create restart event %s", signal_restart_name);
1797 CleanNullACL((void *)sa);
1800 CleanNullACL((void *)sa);
1802 if (ap_mpm_init_complete)
1803 ap_mpm_init_complete();
1805 /* Create the start mutex, apPID, where PID is the parent Apache process ID.
1806 * Ths start mutex is used during a restart to prevent more than one
1807 * child process from entering the accept loop at once.
1809 ap_create_lock(&start_mutex,APR_MUTEX, APR_CROSS_PROCESS, signal_name_prefix,
1810 server_conf->process->pool);
1815 API_EXPORT(int) ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s )
1817 static int restart = 0; /* Default is "not a restart" */
1822 if ((parent_pid != my_pid) || one_process) {
1823 /* Running as Child process or in one_process (debug) mode */
1824 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1825 "Child %d: Child process is running", my_pid);
1827 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1828 "Child %d: Child process is exiting", my_pid);
1831 } /* Child or single process */
1832 else { /* Parent process */
1833 restart = master_main(server_conf, shutdown_event, restart_event);
1836 /* Shutting down. Clean up... */
1837 const char *pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1839 if (pidfile != NULL && unlink(pidfile) == 0) {
1840 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO,APR_SUCCESS,
1841 server_conf, "removed PID file %s (pid=%ld)",
1842 pidfile, GetCurrentProcessId());
1844 ap_destroy_lock(start_mutex);
1846 CloseHandle(restart_event);
1847 CloseHandle(shutdown_event);
1849 //service_set_status(SERVICE_STOPPED);
1853 } /* Parent process */
1855 return 0; /* Restart */
1858 static void winnt_hooks(void)
1861 ap_hook_post_config(winnt_post_config, NULL, NULL, 0);
1865 * Command processors
1867 static const char *set_pidfile(cmd_parms *cmd, void *dummy, char *arg)
1869 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1874 if (cmd->server->is_virtual) {
1875 return "PidFile directive not allowed in <VirtualHost>";
1881 static const char *set_threads_per_child (cmd_parms *cmd, void *dummy, char *arg)
1883 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1888 ap_threads_per_child = atoi(arg);
1889 if (ap_threads_per_child > HARD_THREAD_LIMIT) {
1890 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1891 "WARNING: ThreadsPerChild of %d exceeds compile time"
1892 " limit of %d threads,", ap_threads_per_child,
1894 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1895 " lowering ThreadsPerChild to %d. To increase, please"
1896 " see the HARD_THREAD_LIMIT define in src/include/httpd.h.",
1899 else if (ap_threads_per_child < 1) {
1900 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1901 "WARNING: Require ThreadsPerChild > 0, setting to 1");
1902 ap_threads_per_child = 1;
1908 static const char *set_max_requests(cmd_parms *cmd, void *dummy, char *arg)
1910 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1915 max_requests_per_child = atoi(arg);
1920 static const char *set_coredumpdir (cmd_parms *cmd, void *dummy, char *arg)
1924 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1929 fname = ap_server_root_relative(cmd->pool, arg);
1930 if ((stat(fname, &finfo) == -1) || !S_ISDIR(finfo.st_mode)) {
1931 return ap_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
1932 " does not exist or is not a directory", NULL);
1934 ap_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
1938 /* Stub functions until this MPM supports the connection status API */
1940 API_EXPORT(void) ap_update_connection_status(long conn_id, const char *key, \
1946 API_EXPORT(void) ap_reset_connection_status(long conn_id)
1951 API_EXPORT(ap_array_header_t *) ap_get_status_table(ap_pool_t *p)
1957 static const command_rec winnt_cmds[] = {
1959 { "PidFile", set_pidfile, NULL, RSRC_CONF, TAKE1,
1960 "A file for logging the server process ID"},
1961 { "ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF, TAKE1,
1962 "Number of threads each child creates" },
1963 { "MaxRequestsPerChild", set_max_requests, NULL, RSRC_CONF, TAKE1,
1964 "Maximum number of requests a particular child serves before dying." },
1965 { "CoreDumpDirectory", set_coredumpdir, NULL, RSRC_CONF, TAKE1,
1966 "The location of the directory Apache changes to before dumping core" },
1970 module MODULE_EXPORT_VAR mpm_winnt_module = {
1972 NULL, /* hook run before arguments are parsed */
1973 winnt_pre_config, /* hook run before configuration is read */
1974 NULL, /* create per-directory config structure */
1975 NULL, /* merge per-directory config structures */
1976 NULL, /* create per-server config structure */
1977 NULL, /* merge per-server config structures */
1978 winnt_cmds, /* command ap_table_t */
1979 NULL, /* handlers */
1980 winnt_hooks /* register_hooks */