1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2001 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.
61 #include "http_main.h"
63 #include "http_config.h" /* for read_config */
64 #include "http_core.h" /* for get_remote_host */
65 #include "http_connection.h"
66 #include "apr_portable.h"
67 #include "apr_getopt.h"
68 #include "apr_strings.h"
71 #include "ap_config.h"
72 #include "ap_listen.h"
73 #include "mpm_default.h"
74 #include "mpm_winnt.h"
75 #include "mpm_common.h"
77 typedef HANDLE thread;
78 #ifdef CONTAINING_RECORD
79 #undef CONTAINING_RECORD
81 #define CONTAINING_RECORD(address, type, field) ((type *)( \
83 (PCHAR)(&((type *)0)->field)))
84 #define PADDED_ADDR_SIZE sizeof(SOCKADDR_IN)+16
86 typedef struct CompContext {
87 struct CompContext *next;
88 OVERLAPPED Overlapped;
91 char buff[2*PADDED_ADDR_SIZE];
92 struct sockaddr *sa_server;
94 struct sockaddr *sa_client;
97 } COMP_CONTEXT, *PCOMP_CONTEXT;
100 IOCP_CONNECTION_ACCEPTED = 1,
101 IOCP_WAIT_FOR_RECEIVE = 2,
102 IOCP_WAIT_FOR_TRANSMITFILE = 3,
106 /* Queue for managing the passing of COMP_CONTEXTs from
107 * the accept thread to the worker threads and back again
110 COMP_CONTEXT *qhead = NULL;
111 COMP_CONTEXT *qtail = NULL;
113 static HANDLE ThreadDispatchIOCP = NULL;
115 /* Definitions of WINNT MPM specific config globals */
116 static server_rec *server_conf;
117 static apr_pool_t *pconf;
118 static apr_pool_t *pchild = NULL;
119 static int workers_may_exit = 0;
120 static int shutdown_in_progress = 0;
121 static unsigned int g_blocked_threads = 0;
122 static int ap_max_requests_per_child=0;
124 static HANDLE shutdown_event; /* used to signal the parent to shutdown */
125 static HANDLE restart_event; /* used to signal the parent to restart */
126 static HANDLE exit_event; /* used by parent to signal the child to exit */
127 static HANDLE maintenance_event;
129 static char ap_coredump_dir[MAX_STRING_LEN];
131 static int one_process = 0;
132 static char const* signal_arg = NULL;
134 OSVERSIONINFO osver; /* VER_PLATFORM_WIN32_NT */
136 apr_lock_t *start_mutex;
138 static DWORD parent_pid;
140 int ap_threads_per_child = 0;
141 int ap_daemons_to_start=0;
143 /* ap_get_max_daemons and ap_my_generation are used by the scoreboard
146 ap_generation_t volatile ap_my_generation=0; /* Used by the scoreboard */
148 /* This is the helper code to resolve late bound entry points
149 * missing from one or more releases of the Win32 API...
150 * but it sure would be nice if we didn't duplicate this code
153 static const char* const lateDllName[DLL_defined] = {
154 "kernel32", "advapi32", "mswsock", "ws2_32" };
155 static HMODULE lateDllHandle[DLL_defined] = {
156 NULL, NULL, NULL, NULL };
158 FARPROC ap_load_dll_func(ap_dlltoken_e fnLib, char* fnName, int ordinal)
160 if (!lateDllHandle[fnLib]) {
161 lateDllHandle[fnLib] = LoadLibrary(lateDllName[fnLib]);
162 if (!lateDllHandle[fnLib])
166 return GetProcAddress(lateDllHandle[fnLib], (char *) ordinal);
168 return GetProcAddress(lateDllHandle[fnLib], fnName);
171 /* To share the semaphores with other processes, we need a NULL ACL
172 * Code from MS KB Q106387
174 static PSECURITY_ATTRIBUTES GetNullACL()
176 PSECURITY_DESCRIPTOR pSD;
177 PSECURITY_ATTRIBUTES sa;
179 sa = (PSECURITY_ATTRIBUTES) LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES));
180 pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
181 SECURITY_DESCRIPTOR_MIN_LENGTH);
182 if (pSD == NULL || sa == NULL) {
186 if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
187 || apr_get_os_error()) {
192 if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE)
193 || apr_get_os_error()) {
198 sa->nLength = sizeof(sa);
199 sa->lpSecurityDescriptor = pSD;
200 sa->bInheritHandle = TRUE;
204 static void CleanNullACL( void *sa ) {
206 LocalFree( ((PSECURITY_ATTRIBUTES)sa)->lpSecurityDescriptor);
212 * The Win32 call WaitForMultipleObjects will only allow you to wait for
213 * a maximum of MAXIMUM_WAIT_OBJECTS (current 64). Since the threading
214 * model in the multithreaded version of apache wants to use this call,
215 * we are restricted to a maximum of 64 threads. This is a simplistic
216 * routine that will increase this size.
218 static DWORD wait_for_many_objects(DWORD nCount, CONST HANDLE *lpHandles,
222 DWORD dwRet = WAIT_TIMEOUT;
226 tStopTime = time(NULL) + dwSeconds;
234 for (dwIndex = 0; dwIndex * MAXIMUM_WAIT_OBJECTS < nCount; dwIndex++) {
235 dwRet = WaitForMultipleObjects(
236 min(MAXIMUM_WAIT_OBJECTS, nCount - (dwIndex * MAXIMUM_WAIT_OBJECTS)),
237 lpHandles + (dwIndex * MAXIMUM_WAIT_OBJECTS),
240 if (dwRet != WAIT_TIMEOUT) {
244 } while((time(NULL) < tStopTime) && (dwRet == WAIT_TIMEOUT));
250 * Signalling Apache on NT.
252 * Under Unix, Apache can be told to shutdown or restart by sending various
253 * signals (HUP, USR, TERM). On NT we don't have easy access to signals, so
254 * we use "events" instead. The parent apache process goes into a loop
255 * where it waits forever for a set of events. Two of those events are
261 * (where PID is the PID of the apache parent process). When one of these
262 * is signalled, the Apache parent performs the appropriate action. The events
263 * can become signalled through internal Apache methods (e.g. if the child
264 * finds a fatal error and needs to kill its parent), via the service
265 * control manager (the control thread will signal the shutdown event when
266 * requested to stop the Apache service), from the -k Apache command line,
267 * or from any external program which finds the Apache PID from the
270 * The signal_parent() function, below, is used to signal one of these events.
271 * It can be called by any child or parent process, since it does not
272 * rely on global variables.
274 * On entry, type gives the event to signal. 0 means shutdown, 1 means
278 * Initialise the signal names, in the global variables signal_name_prefix,
279 * signal_restart_name and signal_shutdown_name.
281 #define MAX_SIGNAL_NAME 30 /* Long enough for apPID_shutdown, where PID is an int */
282 char signal_name_prefix[MAX_SIGNAL_NAME];
283 char signal_restart_name[MAX_SIGNAL_NAME];
284 char signal_shutdown_name[MAX_SIGNAL_NAME];
285 void setup_signal_names(char *prefix)
287 apr_snprintf(signal_name_prefix, sizeof(signal_name_prefix), prefix);
288 apr_snprintf(signal_shutdown_name, sizeof(signal_shutdown_name),
289 "%s_shutdown", signal_name_prefix);
290 apr_snprintf(signal_restart_name, sizeof(signal_restart_name),
291 "%s_restart", signal_name_prefix);
294 void signal_parent(int type)
299 /* after updating the shutdown_pending or restart flags, we need
300 * to wake up the parent process so it can see the changes. The
301 * parent will normally be waiting for either a child process
302 * to die, or for a signal on the "spache-signal" event. So set the
303 * "apache-signal" event here.
310 case 0: signal_name = signal_shutdown_name; break;
311 case 1: signal_name = signal_restart_name; break;
314 e = OpenEvent(EVENT_ALL_ACCESS, FALSE, signal_name);
316 /* Um, problem, can't signal the parent, which means we can't
317 * signal ourselves to die. Ignore for now...
319 ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
320 "OpenEvent on %s event", signal_name);
323 if (SetEvent(e) == 0) {
324 /* Same problem as above */
325 ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
326 "SetEvent on %s event", signal_name);
333 static int volatile is_graceful = 0;
335 AP_DECLARE(int) ap_graceful_stop_signalled(void)
340 AP_DECLARE(void) ap_start_shutdown(void)
345 AP_DECLARE(void) ap_start_restart(int gracefully)
347 is_graceful = gracefully;
353 * find_ready_listener()
354 * Only used by Win9* and should go away when the win9*_accept() function is
355 * reimplemented using apr_poll().
357 static ap_listen_rec *head_listener;
358 static APR_INLINE ap_listen_rec *find_ready_listener(fd_set * main_fds)
363 for (lr = head_listener; lr ; lr = lr->next) {
364 apr_os_sock_get(&nsd, lr->sd);
365 if (FD_ISSET(nsd, main_fds)) {
366 head_listener = lr->next;
367 if (head_listener == NULL)
368 head_listener = ap_listeners;
377 * get_listeners_from_parent()
378 * The listen sockets are opened in the parent. This function, which runs
379 * exclusively in the child process, receives them from the parent and
380 * makes them availeble in the child.
382 static int get_listeners_from_parent(server_rec *s)
384 WSAPROTOCOL_INFO WSAProtocolInfo;
388 int num_listeners = 0;
391 /* Set up a default listener if necessary */
392 if (ap_listeners == NULL) {
394 lr = apr_palloc(s->process->pool, sizeof(ap_listen_rec));
398 lr->next = ap_listeners;
402 /* Open the pipe to the parent process to receive the inherited socket
403 * data. The sockets have been set to listening in the parent process.
405 pipe = GetStdHandle(STD_INPUT_HANDLE);
406 for (lr = ap_listeners; lr; lr = lr->next) {
407 if (!ReadFile(pipe, &WSAProtocolInfo, sizeof(WSAPROTOCOL_INFO),
408 &BytesRead, (LPOVERLAPPED) NULL)) {
409 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
410 "setup_inherited_listeners: Unable to read socket data from parent");
411 signal_parent(0); /* tell parent to die */
414 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
415 "Child %d: setup_inherited_listener() read = %d bytes of WSAProtocolInfo.", my_pid);
416 nsd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
417 &WSAProtocolInfo, 0, 0);
418 if (nsd == INVALID_SOCKET) {
419 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_netos_error(), server_conf,
420 "Child %d: setup_inherited_listeners(), WSASocket failed to open the inherited socket.", my_pid);
421 signal_parent(0); /* tell parent to die */
424 apr_os_sock_put(&lr->sd, &nsd, pconf);
428 return num_listeners;
432 /* Windows 9x specific code...
433 * Accept processing for on Windows 95/98 uses a producer/consumer queue
434 * model. A single thread accepts connections and queues the accepted socket
435 * to the accept queue for consumption by a pool of worker threads.
438 * The accept threads runs this function, which accepts connections off
439 * the network and calls add_job() to queue jobs to the accept_queue.
440 * add_job()/remove_job()
441 * Add or remove an accepted socket from the list of sockets
442 * connected to clients. allowed_globals.jobmutex protects
443 * against multiple concurrent access to the linked list of jobs.
444 * win9x_get_connection()
445 * Calls remove_job() to pull a job from the accept queue. All the worker
446 * threads block on remove_job.
449 typedef struct joblist_s {
450 struct joblist_s *next;
454 typedef struct globals_s {
458 apr_lock_t *jobmutex;
462 globals allowed_globals = {NULL, NULL, NULL, NULL, 0};
463 #define MAX_SELECT_ERRORS 100
465 static void add_job(int sock)
469 new_job = (joblist *) malloc(sizeof(joblist));
470 if (new_job == NULL) {
471 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
472 "Ouch! Out of memory in add_job()!");
475 new_job->next = NULL;
476 new_job->sock = sock;
478 apr_lock_acquire(allowed_globals.jobmutex);
480 if (allowed_globals.jobtail != NULL)
481 allowed_globals.jobtail->next = new_job;
482 allowed_globals.jobtail = new_job;
483 if (!allowed_globals.jobhead)
484 allowed_globals.jobhead = new_job;
485 allowed_globals.jobcount++;
486 ReleaseSemaphore(allowed_globals.jobsemaphore, 1, NULL);
488 apr_lock_release(allowed_globals.jobmutex);
491 static int remove_job(void)
496 WaitForSingleObject(allowed_globals.jobsemaphore, INFINITE);
497 apr_lock_acquire(allowed_globals.jobmutex);
499 if (shutdown_in_progress && !allowed_globals.jobhead) {
500 apr_lock_release(allowed_globals.jobmutex);
503 job = allowed_globals.jobhead;
505 allowed_globals.jobhead = job->next;
506 if (allowed_globals.jobhead == NULL)
507 allowed_globals.jobtail = NULL;
508 apr_lock_release(allowed_globals.jobmutex);
515 static void win9x_accept(void * dummy)
517 int requests_this_child = 0;
522 SOCKET nsd = INVALID_SOCKET;
523 struct sockaddr_in sa_client;
524 int count_select_errors = 0;
528 struct fd_set listenfds;
529 SOCKET listenmaxfd = INVALID_SOCKET;
531 /* Setup the listeners
532 * ToDo: Use apr_poll()
535 for (lr = ap_listeners; lr; lr = lr->next) {
536 if (lr->sd != NULL) {
537 apr_os_sock_get(&nsd, lr->sd);
538 FD_SET(nsd, &listenfds);
539 if (listenmaxfd == INVALID_SOCKET || nsd > listenmaxfd) {
544 head_listener = ap_listeners;
546 while (!shutdown_in_progress) {
547 if (ap_max_requests_per_child && (requests_this_child > ap_max_requests_per_child)) {
551 tv.tv_sec = wait_time;
553 memcpy(&main_fds, &listenfds, sizeof(fd_set));
555 rc = select(listenmaxfd + 1, &main_fds, NULL, NULL, &tv);
557 if (rc == 0 || (rc == SOCKET_ERROR && APR_STATUS_IS_EINTR(apr_get_netos_error()))) {
558 count_select_errors = 0; /* reset count of errors */
561 else if (rc == SOCKET_ERROR) {
562 /* A "real" error occurred, log it and increment the count of
563 * select errors. This count is used to ensure we don't go into
564 * a busy loop of continuous errors.
566 ap_log_error(APLOG_MARK, APLOG_INFO, apr_get_netos_error(), server_conf,
567 "select failed with error %d", apr_get_netos_error());
568 count_select_errors++;
569 if (count_select_errors > MAX_SELECT_ERRORS) {
570 shutdown_in_progress = 1;
571 ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
572 "Too many errors in select loop. Child process exiting.");
578 lr = find_ready_listener(&main_fds);
580 /* fetch the native socket descriptor */
581 apr_os_sock_get(&nsd, lr->sd);
586 clen = sizeof(sa_client);
587 csd = accept(nsd, (struct sockaddr *) &sa_client, &clen);
588 if (csd == INVALID_SOCKET) {
591 } while (csd < 0 && APR_STATUS_IS_EINTR(apr_get_netos_error()));
594 if (APR_STATUS_IS_ECONNABORTED(apr_get_netos_error())) {
595 ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
596 "accept: (client socket)");
601 requests_this_child++;
604 SetEvent(exit_event);
607 static PCOMP_CONTEXT win9x_get_connection(PCOMP_CONTEXT context)
611 if (context == NULL) {
612 /* allocate the completion context and the transaction pool */
613 context = apr_pcalloc(pconf, sizeof(COMP_CONTEXT));
615 ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
616 "win9x_get_connection: apr_pcalloc() failed. Process will exit.");
619 apr_pool_create(&context->ptrans, pconf);
624 apr_pool_clear(context->ptrans);
625 context->accept_socket = remove_job();
626 if (context->accept_socket == -1) {
629 len = sizeof(struct sockaddr);
630 context->sa_server = apr_palloc(context->ptrans, len);
631 if (getsockname(context->accept_socket,
632 context->sa_server, &len)== SOCKET_ERROR) {
633 ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), server_conf,
634 "getsockname failed");
637 len = sizeof(struct sockaddr);
638 context->sa_client = apr_palloc(context->ptrans, len);
639 if ((getpeername(context->accept_socket,
640 context->sa_client, &len)) == SOCKET_ERROR) {
641 ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), server_conf,
642 "getpeername failed");
643 memset(&context->sa_client, '\0', sizeof(context->sa_client));
646 /* do we NEED_DUPPED_CSD ?? */
651 /* Windows NT/2000 specific code...
652 * Accept processing for on Windows NT uses a producer/consumer queue
653 * model. An accept thread accepts connections off the network then issues
654 * PostQueuedCompletionStatus() to awake a thread blocked on the ThreadDispatch
658 * One or more accept threads run in this function, each of which accepts
659 * connections off the network and calls PostQueuedCompletionStatus() to
660 * queue an io completion packet to the ThreadDispatch IOCompletionPort.
661 * winnt_get_connection()
662 * Worker threads block on the ThreadDispatch IOCompletionPort awaiting
663 * connections to service.
665 static void winnt_accept(void *listen_socket)
667 static int num_completion_contexts = 0;
668 static int requests_this_child = 0;
669 PCOMP_CONTEXT pCompContext;
674 nlsd = (SOCKET) listen_socket;
676 while (!shutdown_in_progress) {
677 if (ap_max_requests_per_child && (requests_this_child > ap_max_requests_per_child)) {
681 /* Grab a context off the queue */
682 apr_lock_acquire(qlock);
684 pCompContext = qhead;
689 apr_lock_release(qlock);
691 /* If we failed to grab a context off the queue, alloc one out of
692 * the child pool. There may be up to ap_threads_per_child contexts
693 * in the system at once.
696 if (num_completion_contexts >= ap_threads_per_child) {
697 static int reported = 0;
699 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, server_conf,
700 "Server ran out of threads to serve requests. Consider "
701 "raising the ThreadsPerChild setting");
707 pCompContext = (PCOMP_CONTEXT) apr_pcalloc(pchild, sizeof(COMP_CONTEXT));
709 pCompContext->Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
710 if (pCompContext->Overlapped.hEvent == NULL) {
711 ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
712 "winnt_accept: CreateEvent failed. Process will exit.");
715 pCompContext->accept_socket = INVALID_SOCKET;
716 num_completion_contexts++;
720 /* Create and initialize the accept socket */
721 if (pCompContext->accept_socket == INVALID_SOCKET) {
722 pCompContext->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
724 if (pCompContext->accept_socket == INVALID_SOCKET) {
725 ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_netos_error(), server_conf,
726 "winnt_accept: socket() failed. Process will exit.");
730 /* SO_UPDATE_ACCEPT_CONTEXT is required for shutdown() to work */
731 if (setsockopt(pCompContext->accept_socket, SOL_SOCKET,
732 SO_UPDATE_ACCEPT_CONTEXT, (char *)&nlsd,
734 ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_netos_error(), server_conf,
735 "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.");
736 /* Not a failure condition. Keep running. */
740 /* AcceptEx on the completion context. The completion context will be
741 * signaled when a connection is accepted.
743 if (!AcceptEx(nlsd, pCompContext->accept_socket,
749 &pCompContext->Overlapped)) {
750 lasterror = apr_get_netos_error();
751 if (lasterror == APR_FROM_OS_ERROR(WSAEINVAL)) {
752 /* Hack alert. Occasionally, TransmitFile will not recycle the
753 * accept socket (usually when the client disconnects early).
754 * Get a new socket and try the call again.
756 pCompContext->accept_socket = INVALID_SOCKET;
757 ap_log_error(APLOG_MARK, APLOG_DEBUG, lasterror, server_conf,
758 "winnt_accept: AcceptEx failed. Reallocate the accept socket and try again.");
759 if (shutdown_in_progress)
764 else if (lasterror != APR_FROM_OS_ERROR(ERROR_IO_PENDING)) {
765 ap_log_error(APLOG_MARK,APLOG_ERR, lasterror, server_conf,
766 "winnt_accept: AcceptEx failed. Process will exit.");
770 /* Wait for pending i/o */
771 WaitForSingleObject(pCompContext->Overlapped.hEvent, INFINITE);
774 /* When a connection is received, send an io completion notification to
775 * the ThreadDispatchIOCP
777 PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, IOCP_CONNECTION_ACCEPTED,
778 &pCompContext->Overlapped);
779 requests_this_child++;
782 if (!shutdown_in_progress) {
783 /* Yow, hit an irrecoverable error! Tell the child to die. */
784 SetEvent(exit_event);
787 static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT pCompContext)
789 int requests_this_child = 0;
795 /* Recycle the completion context.
796 * - destroy the ptrans pool
797 * - put the context on the queue to be consumed by the accept thread
798 * Note: pCompContext->accept_socket may be in a disconnected
799 * but reusable state so -don't- close it.
802 apr_pool_clear(pCompContext->ptrans);
803 apr_pool_destroy(pCompContext->ptrans);
804 pCompContext->ptrans = NULL;
805 pCompContext->next = NULL;
806 apr_lock_acquire(qlock);
808 qtail->next = pCompContext;
810 qhead = pCompContext;
811 qtail = pCompContext;
812 apr_lock_release(qlock);
817 if (workers_may_exit) {
821 rc = GetQueuedCompletionStatus(ThreadDispatchIOCP, &BytesRead, &CompKey,
824 rc = apr_get_os_error();
825 ap_log_error(APLOG_MARK,APLOG_DEBUG, rc, server_conf,
826 "Child %d: GetQueuedComplationStatus returned %d", my_pid, rc);
831 case IOCP_CONNECTION_ACCEPTED:
832 pCompContext = CONTAINING_RECORD(pol, COMP_CONTEXT, Overlapped);
846 if ((rc = apr_pool_create(&pCompContext->ptrans, pconf)) != APR_SUCCESS) {
847 ap_log_error(APLOG_MARK,APLOG_DEBUG, rc, server_conf,
848 "Child %d: apr_pool_create failed with rc %d", my_pid, rc);
851 /* Get the local & remote address */
852 GetAcceptExSockaddrs(pCompContext->buff,
856 &pCompContext->sa_server,
857 &pCompContext->sa_server_len,
858 &pCompContext->sa_client,
859 &pCompContext->sa_client_len);
867 * Main entry point for the worker threads. Worker threads block in
868 * win*_get_connection() awaiting a connection to service.
870 static void worker_main(int thread_num)
872 PCOMP_CONTEXT context = NULL;
873 apr_os_sock_info_t sockinfo;
877 apr_int32_t disconnected;
879 ap_update_child_status(0, thread_num, SERVER_READY,
880 (request_rec *) NULL);
882 /* Grab a connection off the network */
883 if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
884 context = win9x_get_connection(context);
887 context = winnt_get_connection(context);
893 sockinfo.os_sock = &context->accept_socket;
894 sockinfo.local = context->sa_server;
895 sockinfo.remote = context->sa_client;
896 sockinfo.family = APR_INET;
897 sockinfo.type = SOCK_STREAM;
898 /* ### is this correct? Shouldn't be inheritable (at this point) */
899 apr_os_sock_make(&context->sock, &sockinfo, context->ptrans);
901 c = ap_new_connection(context->ptrans, server_conf, context->sock,
905 ap_process_connection(c);
906 apr_getsocketopt(context->sock, APR_SO_DISCONNECTED, &disconnected);
908 context->accept_socket = INVALID_SOCKET;
909 ap_lingering_close(c);
913 /* ap_new_connection closes the socket on failure */
914 context->accept_socket = INVALID_SOCKET;
918 ap_update_child_status(0, thread_num, SERVER_DEAD, (request_rec *) NULL);
920 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
921 "Child %d: Thread exiting.", my_pid);
924 static void cleanup_thread(thread *handles, int *thread_cnt, int thread_to_clean)
928 CloseHandle(handles[thread_to_clean]);
929 for (i = thread_to_clean; i < ((*thread_cnt) - 1); i++)
930 handles[i] = handles[i + 1];
936 * Entry point for the main control thread for the child process.
937 * This thread creates the accept thread, worker threads and
938 * monitors the child process for maintenance and shutdown
941 static void child_main()
944 HANDLE child_events[2];
945 char* exit_event_name;
946 int nthreads = ap_threads_per_child;
948 thread *child_handles;
954 /* This is the child process or we are running in single process mode. */
955 exit_event_name = apr_psprintf(pconf, "apC%d", my_pid);
956 setup_signal_names(apr_psprintf(pconf,"ap%d", parent_pid));
959 /* Single process mode */
960 apr_lock_create(&start_mutex, APR_MUTEX, APR_CROSS_PROCESS,
961 signal_name_prefix, pconf);
962 exit_event = CreateEvent(NULL, TRUE, FALSE, exit_event_name);
965 /* Child process mode */
966 apr_lock_child_init(&start_mutex, signal_name_prefix, pconf);
967 exit_event = OpenEvent(EVENT_ALL_ACCESS, FALSE, exit_event_name);
968 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
969 "Child %d: exit_event_name = %s", my_pid, exit_event_name);
972 /* Initialize the child_events */
973 maintenance_event = CreateEvent(NULL, TRUE, FALSE, NULL);
974 child_events[0] = exit_event;
975 child_events[1] = maintenance_event;
977 ap_assert(start_mutex);
978 ap_assert(exit_event);
979 ap_assert(maintenance_event);
981 apr_pool_create(&pchild, pconf);
982 allowed_globals.jobsemaphore = CreateSemaphore(NULL, 0, 1000000, NULL);
983 apr_lock_create(&allowed_globals.jobmutex, APR_MUTEX, APR_INTRAPROCESS, NULL, pchild);
986 * Wait until we have permission to start accepting connections.
987 * start_mutex is used to ensure that only one child ever
988 * goes into the listen/accept loop at once.
990 status = apr_lock_acquire(start_mutex);
991 if (status != APR_SUCCESS) {
992 ap_log_error(APLOG_MARK,APLOG_ERR, status, server_conf,
993 "Child %d: Failed to acquire the start_mutex. Process will exit.", my_pid);
994 signal_parent(0); /* tell parent to die */
997 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
998 "Child %d: Acquired the start mutex.", my_pid);
1001 * Create the worker thread dispatch IOCompletionPort
1002 * on Windows NT/2000
1004 if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
1005 /* Create the worker thread dispatch IOCP */
1006 ThreadDispatchIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
1009 0); /* CONCURRENT ACTIVE THREADS */
1010 apr_lock_create(&qlock, APR_MUTEX, APR_INTRAPROCESS, NULL, pchild);
1014 * Create the pool of worker threads
1016 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1017 "Child %d: Starting %d worker threads.", my_pid, nthreads);
1018 child_handles = (thread) alloca(nthreads * sizeof(int));
1019 for (i = 0; i < nthreads; i++) {
1020 ap_update_child_status(0, i, SERVER_STARTING, (request_rec *) NULL);
1021 child_handles[i] = (thread) _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) worker_main,
1022 (void *) i, 0, &tid);
1026 * Start the accept thread
1028 if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
1029 _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) win9x_accept,
1030 (void *) i, 0, &tid);
1032 /* Start an accept thread per listener */
1033 SOCKET nlsd; /* native listening sock descriptor */
1035 for (lr = ap_listeners; lr; lr = lr->next) {
1036 if (lr->sd != NULL) {
1037 apr_os_sock_get(&nlsd, lr->sd);
1038 _beginthreadex(NULL, 1000, (LPTHREAD_START_ROUTINE) winnt_accept,
1039 (void *) nlsd, 0, &tid);
1044 /* Wait for one of three events:
1046 * The exit_event is signaled by the parent process to notify
1047 * the child that it is time to exit.
1049 * maintenance_event:
1050 * This event is signaled by the worker thread pool to direct
1051 * this thread to create more completion contexts.
1054 * To do periodic maintenance on the server (check for thread exits,
1055 * number of completion contexts, etc.)
1058 rv = WaitForMultipleObjects(2, (HANDLE *) child_events, FALSE, 1000);
1059 cld = rv - WAIT_OBJECT_0;
1060 if (rv == WAIT_FAILED) {
1061 /* Something serious is wrong */
1062 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1063 "Child %d: WAIT_FAILED -- shutting down server");
1066 else if (rv == WAIT_TIMEOUT) {
1067 apr_proc_other_child_check();
1069 else if (cld == 0) {
1070 /* Exit event was signaled */
1071 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1072 "Child %d: Exit event signaled. Child process is ending.", my_pid);
1076 /* Child maintenance event signaled */
1077 ResetEvent(maintenance_event);
1078 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1079 "Child %d: Child maintenance event signaled.", my_pid);
1083 /* Setting is_graceful will cause keep-alive connections to be closed
1084 * rather than block on the next network read.
1088 /* Setting shutdown_in_progress prevents new connections from
1089 * being accepted but allows the worker threads to continue
1090 * handling connections that have already been accepted.
1092 shutdown_in_progress = 1;
1094 /* Close the listening sockets */
1097 for (lr = ap_listeners; lr ; lr = lr->next) {
1098 apr_socket_close(lr->sd);
1102 /* Give the worker threads time to handle already accepted connections */
1105 /* Release the start_mutex to let the new process (in the restart
1106 * scenario) a chance to begin accepting and servicing requests
1108 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1109 "Child %d: Releasing the start mutex", my_pid);
1110 apr_lock_release(start_mutex);
1112 /* Tell the worker threads they may exit when done handling
1115 workers_may_exit = 1;
1117 /* Shutdown the worker threads */
1118 if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
1119 for (i = 0; i < nthreads; i++) {
1123 else { /* Windows NT/2000 */
1124 /* Post worker threads blocked on the ThreadDispatch IOCompletion port */
1125 while (g_blocked_threads > 0) {
1126 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1127 "Child %d: %d threads blocked on the completion port", my_pid, g_blocked_threads);
1128 for (i=g_blocked_threads; i > 0; i--) {
1129 PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, IOCP_SHUTDOWN, NULL);
1133 /* Empty the accept queue of completion contexts */
1134 apr_lock_acquire(qlock);
1136 CloseHandle(qhead->Overlapped.hEvent);
1137 closesocket(qhead->accept_socket);
1138 qhead = qhead->next;
1140 apr_lock_release(qlock);
1143 /* Give busy worker threads a chance to service their connections */
1144 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1145 "Child %d: Waiting for %d threads to die.", my_pid, nthreads);
1146 end_time = time(NULL) + 180;
1148 rv = wait_for_many_objects(nthreads, child_handles, end_time - time(NULL));
1149 if (rv != WAIT_TIMEOUT) {
1150 rv = rv - WAIT_OBJECT_0;
1151 ap_assert((rv >= 0) && (rv < nthreads));
1152 cleanup_thread(child_handles, &nthreads, rv);
1158 /* Kill remaining threads off the hard way */
1159 for (i = 0; i < nthreads; i++) {
1160 TerminateThread(child_handles[i], 1);
1161 CloseHandle(child_handles[i]);
1163 ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf,
1164 "Child %d: All worker threads have ended.", my_pid);
1166 CloseHandle(allowed_globals.jobsemaphore);
1167 apr_lock_destroy(allowed_globals.jobmutex);
1168 if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
1169 apr_lock_destroy(qlock);
1171 apr_pool_destroy(pchild);
1172 CloseHandle(exit_event);
1175 static void cleanup_process(HANDLE *handles, HANDLE *events, int position, int *processes)
1180 CloseHandle(handles[position]);
1181 CloseHandle(events[position]);
1183 handle = (int)handles[position];
1185 for (i = position; i < (*processes)-1; i++) {
1186 handles[i] = handles[i + 1];
1187 events[i] = events[i + 1];
1192 static int create_process(apr_pool_t *p, HANDLE *handles, HANDLE *events, int *processes)
1201 STARTUPINFO si; /* Filled in prior to call to CreateProcess */
1202 PROCESS_INFORMATION pi; /* filled in on call to CreateProcess */
1206 HANDLE hPipeRead = NULL;
1207 HANDLE hPipeWrite = NULL;
1208 SECURITY_ATTRIBUTES sa = {0};
1211 LPWSAPROTOCOL_INFO lpWSAProtocolInfo;
1213 sa.nLength = sizeof(sa);
1214 sa.bInheritHandle = TRUE;
1215 sa.lpSecurityDescriptor = NULL;
1217 /* Build the command line. Should look something like this:
1218 * C:/apache/bin/apache.exe -f ap_server_confname
1219 * First, get the path to the executable...
1221 rv = GetModuleFileName(NULL, buf, sizeof(buf));
1222 if (rv == sizeof(buf)) {
1223 ap_log_error(APLOG_MARK, APLOG_CRIT, ERROR_BAD_PATHNAME, server_conf,
1224 "Parent: Path to Apache process too long");
1226 } else if (rv == 0) {
1227 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1228 "Parent: GetModuleFileName() returned NULL for current process.");
1232 /* Build the command line */
1233 pCommand = apr_psprintf(p, "\"%s\"", buf);
1234 for (i = 1; i < server_conf->process->argc; i++) {
1235 pCommand = apr_pstrcat(p, pCommand, " \"", server_conf->process->argv[i], "\"", NULL);
1238 /* Build the environment, since Win9x disrespects the active env */
1239 pEnvVar = apr_psprintf(p, "AP_PARENT_PID=%i", parent_pid);
1241 * Win32's CreateProcess call requires that the environment
1242 * be passed in an environment block, a null terminated block of
1243 * null terminated strings.
1247 while (_environ[i]) {
1248 iEnvBlockLen += strlen(_environ[i]) + 1;
1252 pEnvBlock = (char *)apr_pcalloc(p, iEnvBlockLen + strlen(pEnvVar) + 1);
1253 strcpy(pEnvBlock, pEnvVar);
1254 pEnvVar = strchr(pEnvBlock, '\0') + 1;
1257 while (_environ[i]) {
1258 strcpy(pEnvVar, _environ[i]);
1259 pEnvVar = strchr(pEnvVar, '\0') + 1;
1263 /* Create a pipe to send socket info to the child */
1264 if (!CreatePipe(&hPipeRead, &hPipeWrite, &sa, 0)) {
1265 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1266 "Parent: Unable to create pipe to child process.");
1270 /* Give the read end of the pipe (hPipeRead) to the child as stdin. The
1271 * parent will write the socket data to the child on this pipe.
1273 memset(&si, 0, sizeof(si));
1274 memset(&pi, 0, sizeof(pi));
1276 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
1277 si.wShowWindow = SW_HIDE;
1278 si.hStdInput = hPipeRead;
1280 if (!CreateProcess(NULL, pCommand, NULL, NULL,
1281 TRUE, /* Inherit handles */
1282 CREATE_SUSPENDED, /* Creation flags */
1283 pEnvBlock, /* Environment block */
1286 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1287 "Parent: Not able to create the child process.");
1289 * We must close the handles to the new process and its main thread
1290 * to prevent handle and memory leaks.
1292 CloseHandle(pi.hProcess);
1293 CloseHandle(pi.hThread);
1297 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1298 "Parent: Created child process %d", pi.dwProcessId);
1300 SetEnvironmentVariable("AP_PARENT_PID",NULL);
1302 /* Create the exit_event, apCchild_pid */
1303 sa.nLength = sizeof(sa);
1304 sa.bInheritHandle = TRUE;
1305 sa.lpSecurityDescriptor = NULL;
1306 kill_event = CreateEvent(&sa, TRUE, FALSE, apr_psprintf(pconf,"apC%d", pi.dwProcessId));
1308 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1309 "Parent: Could not create exit event for child process");
1310 CloseHandle(pi.hProcess);
1311 CloseHandle(pi.hThread);
1315 /* Assume the child process lives. Update the process and event tables */
1316 handles[*processes] = pi.hProcess;
1317 events[*processes] = kill_event;
1320 /* We never store the thread's handle, so close it now. */
1321 ResumeThread(pi.hThread);
1322 CloseHandle(pi.hThread);
1325 * Give the child process a chance to run before dup'ing the sockets.
1326 * We have already set the listening sockets noninheritable, but if
1327 * WSADuplicateSocket runs before the child process initializes
1328 * the listeners will be inherited anyway.
1332 /* Run the chain of open sockets. For each socket, duplicate it
1333 * for the target process then send the WSAPROTOCOL_INFO
1334 * (returned by dup socket) to the child.
1336 for (lr = ap_listeners; lr; lr = lr->next) {
1338 lpWSAProtocolInfo = apr_pcalloc(p, sizeof(WSAPROTOCOL_INFO));
1339 apr_os_sock_get(&nsd,lr->sd);
1340 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1341 "Parent: Duplicating socket %d and sending it to child process %d", nsd, pi.dwProcessId);
1342 if (WSADuplicateSocket(nsd, pi.dwProcessId,
1343 lpWSAProtocolInfo) == SOCKET_ERROR) {
1344 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_netos_error(), server_conf,
1345 "Parent: WSADuplicateSocket failed for socket %d. Check the FAQ.", lr->sd );
1349 if (!WriteFile(hPipeWrite, lpWSAProtocolInfo, (DWORD) sizeof(WSAPROTOCOL_INFO),
1351 (LPOVERLAPPED) NULL)) {
1352 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1353 "Parent: Unable to write duplicated socket %d to the child.", lr->sd );
1356 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
1357 "Parent: BytesWritten = %d WSAProtocolInfo = %x20", BytesWritten, *lpWSAProtocolInfo);
1360 CloseHandle(hPipeRead);
1361 CloseHandle(hPipeWrite);
1366 /**********************************************************************
1368 * This is the parent process. master_main() creates a multithreaded
1369 * child process to handle connections, then blocks waiting to receive
1370 * a shutdown, restart event or child exit event.
1373 * - Child is signaled to die gracefully
1375 * - Child is signaled to die gracefully
1377 * - Child has died, either normally (max_request_per_child)
1378 * or abnormally (seg fault, irrecoverable error condition detected by the
1380 **********************************************************************/
1381 #define MAX_PROCESSES 50 /* must be < MAX_WAIT_OBJECTS-1 */
1382 static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_event)
1384 int remaining_children_to_start = ap_daemons_to_start;
1388 int restart_pending = 0;
1389 int shutdown_pending = 0;
1390 int current_live_processes = 0; /* number of child process we know about */
1392 HANDLE process_handles[MAX_PROCESSES];
1393 HANDLE process_kill_events[MAX_PROCESSES];
1396 /* Create child process
1397 * Should only be one in this version of Apache for WIN32
1399 while (remaining_children_to_start--) {
1400 if (create_process(pconf, process_handles, process_kill_events,
1401 ¤t_live_processes) < 0) {
1402 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1403 "master_main: create child process failed. Exiting.");
1404 shutdown_pending = 1;
1409 restart_pending = shutdown_pending = 0;
1411 if (!strcasecmp(signal_arg, "runservice"))
1412 mpm_service_started();
1414 /* Wait for shutdown or restart events or for child death */
1415 process_handles[current_live_processes] = shutdown_event;
1416 process_handles[current_live_processes+1] = restart_event;
1418 rv = WaitForMultipleObjects(current_live_processes+2, (HANDLE *)process_handles,
1420 cld = rv - WAIT_OBJECT_0;
1421 if (rv == WAIT_FAILED) {
1422 /* Something serious is wrong */
1423 ap_log_error(APLOG_MARK,APLOG_CRIT, apr_get_os_error(), server_conf,
1424 "master_main: WaitForMultipeObjects WAIT_FAILED -- doing server shutdown");
1425 shutdown_pending = 1;
1427 else if (rv == WAIT_TIMEOUT) {
1428 /* Hey, this cannot happen */
1429 ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
1430 "master_main: WaitForMultipeObjects with INFINITE wait exited with WAIT_TIMEOUT");
1431 shutdown_pending = 1;
1433 else if (cld == current_live_processes) {
1434 /* shutdown_event signalled */
1435 shutdown_pending = 1;
1436 printf("shutdown event signaled\n");
1437 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, s,
1438 "Parent: SHUTDOWN EVENT SIGNALED -- Shutting down the server.");
1439 if (ResetEvent(shutdown_event) == 0) {
1440 ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
1441 "ResetEvent(shutdown_event)");
1445 else if (cld == current_live_processes+1) {
1446 /* restart_event signalled */
1447 int children_to_kill = current_live_processes;
1448 restart_pending = 1;
1449 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, s,
1450 "Parent: RESTART EVENT SIGNALED -- Restarting the server.");
1451 if (ResetEvent(restart_event) == 0) {
1452 ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
1453 "master_main: ResetEvent(restart_event) failed.");
1455 /* Signal each child process to die
1456 * We are making a big assumption here that the child process, once signaled,
1457 * will REALLY go away. Since this is a restart, we do not want to hold the
1458 * new child process up waiting for the old child to die. Remove the old
1459 * child out of the process_handles apr_table_t and hope for the best...
1461 for (i = 0; i < children_to_kill; i++) {
1462 if (SetEvent(process_kill_events[i]) == 0)
1463 ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(), s,
1464 "master_main: SetEvent for child process in slot #%d failed", i);
1465 cleanup_process(process_handles, process_kill_events, i, ¤t_live_processes);
1469 /* A child process must have exited because of a fatal error condition (seg fault, etc.).
1470 * Remove the dead process
1471 * from the process_handles and process_kill_events apr_table_t and create a new
1473 * TODO: Consider restarting the child immediately without looping through http_main
1474 * and without rereading the configuration. Will need this if we ever support multiple
1475 * children. One option, create a parent thread which waits on child death and restarts it.
1476 * Consider, however, that if the user makes httpd.conf invalid, we want to die before
1477 * our child tries it... otherwise we have a nasty loop.
1479 restart_pending = 1;
1480 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
1481 "Parent: CHILD PROCESS FAILED -- Restarting the child process.");
1482 ap_assert(cld < current_live_processes);
1483 cleanup_process(process_handles, process_kill_events, cld, ¤t_live_processes);
1484 /* APD2("main_process: child in slot %d died", rv); */
1485 /* restart_child(process_hancles, process_kill_events, cld, ¤t_live_processes); */
1489 if (shutdown_pending)
1491 int tmstart = time(NULL);
1493 if (strcasecmp(signal_arg, "runservice")) {
1494 mpm_service_stopping();
1496 /* Signal each child processes to die */
1497 for (i = 0; i < current_live_processes; i++) {
1498 printf("SetEvent handle = %d\n", process_kill_events[i]);
1499 if (SetEvent(process_kill_events[i]) == 0)
1500 ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_os_error(), server_conf,
1501 "master_main: SetEvent for child process in slot #%d failed", i);
1504 while (current_live_processes && ((tmstart+60) > time(NULL))) {
1505 rv = WaitForMultipleObjects(current_live_processes, (HANDLE *)process_handles, FALSE, 2000);
1506 if (rv == WAIT_TIMEOUT)
1508 ap_assert(rv != WAIT_FAILED);
1509 cld = rv - WAIT_OBJECT_0;
1510 ap_assert(rv < current_live_processes);
1511 cleanup_process(process_handles, process_kill_events, cld, ¤t_live_processes);
1513 for (i = 0; i < current_live_processes; i++) {
1514 ap_log_error(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO, APR_SUCCESS, server_conf,
1515 "Parent: Forcing termination of child #%d (handle %d)", i, process_handles[i]);
1516 TerminateProcess((HANDLE) process_handles[i], 1);
1518 return 0; /* Tell the caller we do not want to restart */
1521 return 1; /* Tell the caller we want a restart */
1524 /* set_listeners_noninheritable()
1525 * Make the listening socket handles noninheritable by processes
1526 * started out of this process.
1528 static int set_listeners_noninheritable(apr_pool_t *p)
1533 HANDLE hProcess = GetCurrentProcess();
1535 for (lr = ap_listeners; lr; lr = lr->next) {
1536 apr_os_sock_get(&nsd,lr->sd);
1537 if (!DuplicateHandle(hProcess, (HANDLE) nsd, hProcess, &dup, 0,
1538 FALSE, /* Inherit flag */
1539 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
1540 ap_log_error(APLOG_MARK, APLOG_ERR, apr_get_os_error(),
1542 "set_listeners_noninheritable: DuplicateHandle failed.");
1546 apr_os_sock_put(&lr->sd, &nsd, p);
1551 /* service_nt_main_fn needs to append the StartService() args
1552 * outside of our call stack and thread as the service starts...
1554 apr_array_header_t *mpm_new_argv;
1556 /* Remember service_to_start failures to log and fail in pre_config.
1557 * Remember inst_argc and inst_argv for installing or starting the
1558 * service after we preflight the config.
1561 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
1564 case AP_MPMQ_MAX_DAEMON_USED:
1565 *result = MAXIMUM_WAIT_OBJECTS;
1567 case AP_MPMQ_IS_THREADED:
1568 *result = AP_MPMQ_STATIC;
1570 case AP_MPMQ_IS_FORKED:
1571 *result = AP_MPMQ_NOT_SUPPORTED;
1573 case AP_MPMQ_HARD_LIMIT_DAEMONS:
1574 *result = HARD_SERVER_LIMIT;
1576 case AP_MPMQ_HARD_LIMIT_THREADS:
1577 *result = HARD_THREAD_LIMIT;
1579 case AP_MPMQ_MAX_THREADS:
1580 *result = ap_threads_per_child;
1582 case AP_MPMQ_MIN_SPARE_DEAMONS:
1585 case AP_MPMQ_MIN_SPARE_THREADS:
1588 case AP_MPMQ_MAX_SPARE_DAEMONS:
1591 case AP_MPMQ_MAX_SPARE_THREADS:
1594 case AP_MPMQ_MAX_REQUESTS_DEAMON:
1595 *result = ap_max_requests_per_child;
1597 case AP_MPMQ_MAX_DAEMONS:
1601 return APR_ENOTIMPL;
1604 #define SERVICE_UNSET (-1)
1605 static apr_status_t service_set = SERVICE_UNSET;
1606 static apr_status_t service_to_start_success;
1607 static int inst_argc;
1608 static const char * const *inst_argv;
1609 static char *service_name = NULL;
1611 void winnt_rewrite_args(process_rec *process)
1613 /* Handle the following SCM aspects in this phase:
1615 * -k runservice [transition for WinNT, nothing for Win9x]
1616 * -k (!)install [error out if name is not installed]
1618 * We can't leave this phase until we know our identity
1619 * and modify the command arguments appropriately.
1622 char *def_server_root;
1623 char fnbuf[MAX_PATH];
1629 int running_as_service = 1;
1631 osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1632 GetVersionEx(&osver);
1634 /* AP_PARENT_PID is only valid in the child */
1635 pid = getenv("AP_PARENT_PID");
1638 /* This is the child */
1639 my_pid = GetCurrentProcessId();
1640 parent_pid = (DWORD) atol(pid);
1642 /* The parent is responsible for providing the
1643 * COMPLETE ARGUMENTS REQUIRED to the child.
1645 * No further argument parsing is needed, but
1646 * for good measure we will provide a simple
1647 * signal string for later testing.
1649 signal_arg = "runchild";
1653 /* This is the parent, we have a long way to go :-) */
1654 parent_pid = my_pid = GetCurrentProcessId();
1656 /* Rewrite process->argv[];
1658 * strip out -k signal into signal_arg
1659 * strip out -n servicename and set the names
1660 * add default -d serverroot from the path of this executable
1662 * The end result will look like:
1664 * The invocation command (%0)
1665 * The -d serverroot default from the running executable
1666 * The requested service's (-n) registry ConfigArgs
1667 * The WinNT SCM's StartService() args
1669 if (!GetModuleFileName(NULL, fnbuf, sizeof(fnbuf))) {
1670 rv = apr_get_os_error();
1671 ap_log_error(APLOG_MARK,APLOG_CRIT, rv, NULL,
1672 "Failed to get the path of Apache.exe");
1675 /* WARNING: There is an implict assumption here that the
1676 * executable resides in ServerRoot or ServerRoot\bin
1678 def_server_root = (char *) apr_filename_of_pathname(fnbuf);
1679 if (def_server_root > fnbuf) {
1680 *(def_server_root - 1) = '\0';
1681 def_server_root = (char *) apr_filename_of_pathname(fnbuf);
1682 if (!strcasecmp(def_server_root, "bin"))
1683 *(def_server_root - 1) = '\0';
1685 apr_filepath_merge(&def_server_root, NULL, fnbuf,
1686 APR_FILEPATH_TRUENAME, process->pool);
1688 /* Use process->pool so that the rewritten argv
1689 * lasts for the lifetime of the server process,
1690 * because pconf will be destroyed after the
1691 * initial pre-flight of the config parser.
1693 mpm_new_argv = apr_array_make(process->pool, process->argc + 2,
1694 sizeof(const char *));
1695 *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
1696 *(const char **)apr_array_push(mpm_new_argv) = "-d";
1697 *(const char **)apr_array_push(mpm_new_argv) = def_server_root;
1699 fixed_args = mpm_new_argv->nelts;
1703 apr_getopt_init(&opt, process->pool, process->argc, (char**) process->argv);
1704 while (apr_getopt(opt, "n:k:iu" AP_SERVER_BASEARGS,
1705 optbuf + 1, &optarg) == APR_SUCCESS) {
1706 switch (optbuf[1]) {
1708 service_set = mpm_service_set_name(process->pool, &service_name,
1712 signal_arg = optarg;
1715 ap_log_error(APLOG_MARK,APLOG_WARNING, 0, NULL,
1716 "-i is deprecated. Use -k install.");
1717 signal_arg = "install";
1720 ap_log_error(APLOG_MARK,APLOG_WARNING, 0, NULL,
1721 "-u is deprecated. Use -k uninstall.");
1722 signal_arg = "uninstall";
1725 *(const char **)apr_array_push(mpm_new_argv) =
1726 apr_pstrdup(process->pool, optbuf);
1729 *(const char **)apr_array_push(mpm_new_argv) = optarg;
1735 /* Track the number of args actually entered by the user */
1736 inst_argc = mpm_new_argv->nelts - fixed_args;
1738 /* Provide a default 'run' -k arg to simplify signal_arg tests */
1742 running_as_service = 0;
1745 if (!strcasecmp(signal_arg, "runservice"))
1747 /* Start the NT Service _NOW_ because the WinNT SCM is
1748 * expecting us to rapidly assume control of our own
1749 * process, the SCM will tell us our service name, and
1750 * may have extra StartService() command arguments to
1753 * Any other process has a console, so we don't to begin
1754 * a Win9x service until the configuration is parsed and
1755 * any command line errors are reported.
1757 * We hold the return value so that we can die in pre_config
1758 * after logging begins, and the failure can land in the log.
1760 if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
1761 service_to_start_success = mpm_service_to_start(&service_name);
1762 if (service_to_start_success == APR_SUCCESS)
1763 service_set = APR_SUCCESS;
1767 /* Get the default for any -k option, except run */
1768 if (service_set == SERVICE_UNSET && strcasecmp(signal_arg, "run")) {
1769 service_set = mpm_service_set_name(process->pool, &service_name,
1770 AP_DEFAULT_SERVICE_NAME);
1773 if (!strcasecmp(signal_arg, "install")) /* -k install */
1775 if (service_set == APR_SUCCESS)
1777 ap_log_error(APLOG_MARK,APLOG_ERR, 0, NULL,
1778 "%s: Service is already installed.", service_name);
1782 else if (running_as_service)
1784 if (service_set == APR_SUCCESS)
1786 rv = mpm_merge_service_args(process->pool, mpm_new_argv,
1788 if (rv == APR_SUCCESS) {
1789 ap_log_error(APLOG_MARK,APLOG_NOERRNO|APLOG_INFO, 0, NULL,
1790 "Using ConfigArgs of the installed service "
1791 "\"%s\".", service_name);
1794 ap_log_error(APLOG_MARK,APLOG_WARNING, rv, NULL,
1795 "No installed ConfigArgs for the service "
1796 "\"%s\", using Apache defaults.", service_name);
1801 ap_log_error(APLOG_MARK,APLOG_ERR, service_set, NULL,
1802 "No installed service named \"%s\".", service_name);
1806 if (strcasecmp(signal_arg, "install") && service_set && service_set != SERVICE_UNSET)
1808 ap_log_error(APLOG_MARK,APLOG_ERR, service_set, NULL,
1809 "No installed service named \"%s\".", service_name);
1813 /* Track the args actually entered by the user.
1814 * These will be used for the -k install parameters, as well as
1815 * for the -k start service override arguments.
1817 inst_argv = (const char * const *)mpm_new_argv->elts
1818 + mpm_new_argv->nelts - inst_argc;
1820 process->argc = mpm_new_argv->nelts;
1821 process->argv = (const char * const *) mpm_new_argv->elts;
1825 static void winnt_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
1827 /* Handle the following SCM aspects in this phase:
1829 * -k runservice [WinNT errors logged from rewrite_args]
1833 * in these cases we -don't- care if httpd.conf has config errors!
1837 if (ap_exists_config_define("ONE_PROCESS"))
1840 if (!strcasecmp(signal_arg, "runservice")
1841 && (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
1842 && (service_to_start_success != APR_SUCCESS)) {
1843 ap_log_error(APLOG_MARK,APLOG_CRIT, service_to_start_success, NULL,
1844 "%s: Unable to start the service manager.",
1849 if (!strcasecmp(signal_arg, "uninstall")) {
1850 rv = mpm_service_uninstall();
1854 if (!strcasecmp(signal_arg, "stop")) {
1855 mpm_signal_service(ptemp, 0);
1859 ap_listen_pre_config();
1860 ap_daemons_to_start = DEFAULT_NUM_DAEMON;
1861 ap_threads_per_child = DEFAULT_START_THREAD;
1862 ap_pid_fname = DEFAULT_PIDLOG;
1863 ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
1865 apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1868 static void winnt_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec* server)
1870 static int restart_num = 0;
1871 apr_status_t rv = 0;
1873 server_conf = server;
1875 /* Handle the following SCM aspects in this phase:
1880 * -k runservice [Win95, only once - after we parsed the config]
1882 * because all of these signals are useful _only_ if there
1883 * is a valid conf\httpd.conf environment to start.
1885 * We reached this phase by avoiding errors that would cause
1886 * these options to fail unexpectedly in another process.
1889 if (!strcasecmp(signal_arg, "install")) {
1890 rv = mpm_service_install(ptemp, inst_argc, inst_argv);
1894 if (!strcasecmp(signal_arg, "start")) {
1895 rv = mpm_service_start(ptemp, inst_argc, inst_argv);
1899 if (!strcasecmp(signal_arg, "restart")) {
1900 mpm_signal_service(ptemp, 1);
1904 if (parent_pid == my_pid)
1906 if (restart_num++ == 1)
1908 /* This code should be run once in the parent and not run
1911 PSECURITY_ATTRIBUTES sa = GetNullACL(); /* returns NULL if invalid (Win95?) */
1912 setup_signal_names(apr_psprintf(pconf,"ap%d", parent_pid));
1914 ap_log_pid(pconf, ap_pid_fname);
1916 /* Create shutdown event, apPID_shutdown, where PID is the parent
1917 * Apache process ID. Shutdown is signaled by 'apache -k shutdown'.
1919 shutdown_event = CreateEvent(sa, FALSE, FALSE, signal_shutdown_name);
1920 if (!shutdown_event) {
1921 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1922 "Parent: Cannot create shutdown event %s", signal_shutdown_name);
1923 CleanNullACL((void *)sa);
1927 /* Create restart event, apPID_restart, where PID is the parent
1928 * Apache process ID. Restart is signaled by 'apache -k restart'.
1930 restart_event = CreateEvent(sa, FALSE, FALSE, signal_restart_name);
1931 if (!restart_event) {
1932 CloseHandle(shutdown_event);
1933 ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1934 "Parent: Cannot create restart event %s", signal_restart_name);
1935 CleanNullACL((void *)sa);
1938 CleanNullACL((void *)sa);
1940 /* Now that we are flying at 15000 feet...
1941 * wipe out the Win95 service console,
1942 * signal the SCM the WinNT service started, or
1943 * if not a service, setup console handlers instead.
1945 if (!strcasecmp(signal_arg, "runservice"))
1947 if (osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
1949 rv = mpm_service_to_start(&service_name);
1950 if (rv != APR_SUCCESS) {
1951 ap_log_error(APLOG_MARK,APLOG_ERR, rv, server_conf,
1952 "%s: Unable to start the service manager.",
1958 else /* ! -k runservice */
1960 mpm_start_console_handler();
1963 /* Create the start mutex, apPID, where PID is the parent Apache process ID.
1964 * Ths start mutex is used during a restart to prevent more than one
1965 * child process from entering the accept loop at once.
1967 apr_lock_create(&start_mutex,APR_MUTEX, APR_CROSS_PROCESS, signal_name_prefix,
1968 server_conf->process->pool);
1971 else /* parent_pid != my_pid */
1973 mpm_start_child_console_handler();
1977 AP_DECLARE(int) ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s )
1979 static int restart = 0; /* Default is "not a restart" */
1984 if ((parent_pid != my_pid) || one_process) {
1985 /* Child process or in one_process (debug) mode */
1986 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1987 "Child %d: Child process is running", my_pid);
1989 /* Set up the scoreboard. */
1990 ap_run_pre_mpm(pconf, SB_NOT_SHARED);
1992 if (ap_setup_listeners(server_conf) < 1) {
1997 get_listeners_from_parent(server_conf);
1999 if (!set_listeners_noninheritable(pconf)) {
2004 ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
2005 "Child %d: Child process is exiting", my_pid);
2009 /* Parent process */
2010 if (ap_setup_listeners(server_conf) < 1) {
2011 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, s,
2012 "no listening sockets available, shutting down");
2015 if (!set_listeners_noninheritable(pconf)) {
2018 restart = master_main(server_conf, shutdown_event, restart_event);
2021 /* Shutting down. Clean up... */
2022 const char *pidfile = ap_server_root_relative (pconf, ap_pid_fname);
2024 if (pidfile != NULL && unlink(pidfile) == 0) {
2025 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS,
2026 server_conf, "removed PID file %s (pid=%ld)",
2027 pidfile, GetCurrentProcessId());
2029 apr_lock_destroy(start_mutex);
2031 CloseHandle(restart_event);
2032 CloseHandle(shutdown_event);
2036 } /* Parent process */
2038 return 0; /* Restart */
2041 static void winnt_hooks(apr_pool_t *p)
2043 ap_hook_pre_config(winnt_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
2044 ap_hook_post_config(winnt_post_config, NULL, NULL, 0);
2048 * Command processors
2051 static const char *set_threads_per_child (cmd_parms *cmd, void *dummy, char *arg)
2053 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2058 ap_threads_per_child = atoi(arg);
2059 if (ap_threads_per_child > HARD_THREAD_LIMIT) {
2060 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
2061 "WARNING: ThreadsPerChild of %d exceeds compile time"
2062 " limit of %d threads,", ap_threads_per_child,
2064 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
2065 " lowering ThreadsPerChild to %d. To increase, please"
2066 " see the HARD_THREAD_LIMIT define in %s.",
2067 HARD_THREAD_LIMIT, AP_MPM_HARD_LIMITS_FILE);
2068 ap_threads_per_child = HARD_THREAD_LIMIT;
2070 else if (ap_threads_per_child < 1) {
2071 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
2072 "WARNING: Require ThreadsPerChild > 0, setting to 1");
2073 ap_threads_per_child = 1;
2078 /* Stub functions until this MPM supports the connection status API */
2080 AP_DECLARE(void) ap_update_connection_status(long conn_id, const char *key, \
2086 AP_DECLARE(void) ap_reset_connection_status(long conn_id)
2091 AP_DECLARE(apr_array_header_t *) ap_get_status_table(apr_pool_t *p)
2097 static const command_rec winnt_cmds[] = {
2099 { "ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF, TAKE1,
2100 "Number of threads each child creates" },
2104 AP_MODULE_DECLARE_DATA module mpm_winnt_module = {
2106 winnt_rewrite_args, /* hook to run before apache parses args */
2107 NULL, /* create per-directory config structure */
2108 NULL, /* merge per-directory config structures */
2109 NULL, /* create per-server config structure */
2110 NULL, /* merge per-server config structures */
2111 winnt_cmds, /* command apr_table_t */
2112 winnt_hooks /* register_hooks */