]> granicus.if.org Git - apache/blob - server/mpm/winnt/mpm_winnt.c
Eliminated ap_os_[systemcase|[case_]canonical]_filename() and
[apache] / server / mpm / winnt / mpm_winnt.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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.
25  *
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.
30  *
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.
34  *
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
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
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/>.
53  *
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.
57  */
58
59 #define CORE_PRIVATE 
60 #include "httpd.h" 
61 #include "http_main.h" 
62 #include "http_log.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"
69 #include "apr_lib.h"
70 #include "ap_mpm.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"
76
77 typedef HANDLE thread;
78 #ifdef CONTAINING_RECORD
79 #undef CONTAINING_RECORD
80 #endif
81 #define CONTAINING_RECORD(address, type, field) ((type *)( \
82                                                   (PCHAR)(address) - \
83                                                   (PCHAR)(&((type *)0)->field)))
84 #define PADDED_ADDR_SIZE sizeof(SOCKADDR_IN)+16
85
86 typedef struct CompContext {
87     struct CompContext *next;
88     OVERLAPPED Overlapped;
89     apr_socket_t *sock;
90     SOCKET accept_socket;
91     char buff[2*PADDED_ADDR_SIZE];
92     struct sockaddr *sa_server;
93     int sa_server_len;
94     struct sockaddr *sa_client;
95     int sa_client_len;
96     apr_pool_t *ptrans;
97 } COMP_CONTEXT, *PCOMP_CONTEXT;
98
99 typedef enum {
100     IOCP_CONNECTION_ACCEPTED = 1,
101     IOCP_WAIT_FOR_RECEIVE = 2,
102     IOCP_WAIT_FOR_TRANSMITFILE = 3,
103     IOCP_SHUTDOWN = 4
104 } io_state_e;
105
106 /* Queue for managing the passing of COMP_CONTEXTs from 
107  * the accept thread to the worker threads and back again
108  */
109 apr_lock_t  *qlock;
110 COMP_CONTEXT *qhead = NULL;
111 COMP_CONTEXT *qtail = NULL;
112
113 static HANDLE ThreadDispatchIOCP = NULL;
114
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;
123
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;
128
129 static char ap_coredump_dir[MAX_STRING_LEN];
130
131 static int one_process = 0;
132 static char const* signal_arg = NULL;
133
134 OSVERSIONINFO osver; /* VER_PLATFORM_WIN32_NT */
135
136 apr_lock_t *start_mutex;
137 static DWORD my_pid;
138 static DWORD parent_pid;
139
140 int ap_threads_per_child = 0;
141 int ap_daemons_to_start=0;
142
143 /* ap_get_max_daemons and ap_my_generation are used by the scoreboard
144  * code
145  */
146 ap_generation_t volatile ap_my_generation=0; /* Used by the scoreboard */
147
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
151  * from the APR ;-)
152  */
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      };
157
158 FARPROC ap_load_dll_func(ap_dlltoken_e fnLib, char* fnName, int ordinal)
159 {
160     if (!lateDllHandle[fnLib]) { 
161         lateDllHandle[fnLib] = LoadLibrary(lateDllName[fnLib]);
162         if (!lateDllHandle[fnLib])
163             return NULL;
164     }
165     if (ordinal)
166         return GetProcAddress(lateDllHandle[fnLib], (char *) ordinal);
167     else
168         return GetProcAddress(lateDllHandle[fnLib], fnName);
169 }
170
171 /* To share the semaphores with other processes, we need a NULL ACL
172  * Code from MS KB Q106387
173  */
174 static PSECURITY_ATTRIBUTES GetNullACL()
175 {
176     PSECURITY_DESCRIPTOR pSD;
177     PSECURITY_ATTRIBUTES sa;
178
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) {
183         return NULL;
184     }
185     apr_set_os_error(0);
186     if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION)
187         || apr_get_os_error()) {
188         LocalFree( pSD );
189         LocalFree( sa );
190         return NULL;
191     }
192     if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE)
193         || apr_get_os_error()) {
194         LocalFree( pSD );
195         LocalFree( sa );
196         return NULL;
197     }
198     sa->nLength = sizeof(sa);
199     sa->lpSecurityDescriptor = pSD;
200     sa->bInheritHandle = TRUE;
201     return sa;
202 }
203
204 static void CleanNullACL( void *sa ) {
205     if( sa ) {
206         LocalFree( ((PSECURITY_ATTRIBUTES)sa)->lpSecurityDescriptor);
207         LocalFree( sa );
208     }
209 }
210
211 /*
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.
217  */
218 static DWORD wait_for_many_objects(DWORD nCount, CONST HANDLE *lpHandles, 
219                                    DWORD dwSeconds)
220 {
221     time_t tStopTime;
222     DWORD dwRet = WAIT_TIMEOUT;
223     DWORD dwIndex=0;
224     BOOL bFirst = TRUE;
225   
226     tStopTime = time(NULL) + dwSeconds;
227   
228     do {
229         if (!bFirst)
230             Sleep(1000);
231         else
232             bFirst = FALSE;
233           
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), 
238                 0, 0);
239                                            
240             if (dwRet != WAIT_TIMEOUT) {                                          
241               break;
242             }
243         }
244     } while((time(NULL) < tStopTime) && (dwRet == WAIT_TIMEOUT));
245     
246     return dwRet;
247 }
248
249 /*
250  * Signalling Apache on NT.
251  *
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
256  * called
257  *
258  *    apPID_shutdown
259  *    apPID_restart
260  *
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
268  * httpd.pid file.
269  *
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.
273  *
274  * On entry, type gives the event to signal. 0 means shutdown, 1 means 
275  * graceful restart.
276  */
277 /*
278  * Initialise the signal names, in the global variables signal_name_prefix, 
279  * signal_restart_name and signal_shutdown_name.
280  */
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)
286 {
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);    
292 }
293
294 void signal_parent(int type)
295 {
296     HANDLE e;
297     char *signal_name;
298     
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.
304      */
305     if (one_process) {
306         return;
307     }
308
309     switch(type) {
310     case 0: signal_name = signal_shutdown_name; break;
311     case 1: signal_name = signal_restart_name; break;
312     default: return;
313     }
314     e = OpenEvent(EVENT_ALL_ACCESS, FALSE, signal_name);
315     if (!e) {
316         /* Um, problem, can't signal the parent, which means we can't
317          * signal ourselves to die. Ignore for now...
318          */
319         ap_log_error(APLOG_MARK, APLOG_EMERG, apr_get_os_error(), server_conf,
320                      "OpenEvent on %s event", signal_name);
321         return;
322     }
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);
327         CloseHandle(e);
328         return;
329     }
330     CloseHandle(e);
331 }
332
333 static int volatile is_graceful = 0;
334
335 AP_DECLARE(int) ap_graceful_stop_signalled(void)
336 {
337     return is_graceful;
338 }
339
340 AP_DECLARE(void) ap_start_shutdown(void)
341 {
342     signal_parent(0);
343 }
344
345 AP_DECLARE(void) ap_start_restart(int gracefully)
346 {
347     is_graceful = gracefully;
348     signal_parent(1);
349 }
350
351
352 /*
353  * find_ready_listener()
354  * Only used by Win9* and should go away when the win9*_accept() function is 
355  * reimplemented using apr_poll().
356  */
357 static ap_listen_rec *head_listener;
358 static APR_INLINE ap_listen_rec *find_ready_listener(fd_set * main_fds)
359 {
360     ap_listen_rec *lr;
361     SOCKET nsd;
362
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;
369
370             return (lr);
371         }
372     }
373     return NULL;
374 }
375
376 /* 
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.
381  */
382 static int get_listeners_from_parent(server_rec *s)
383 {
384     WSAPROTOCOL_INFO WSAProtocolInfo;
385     HANDLE pipe;
386     ap_listen_rec *lr;
387     DWORD BytesRead;
388     int num_listeners = 0;
389     SOCKET nsd;
390
391     /* Set up a default listener if necessary */
392     if (ap_listeners == NULL) {
393         ap_listen_rec *lr;
394         lr = apr_palloc(s->process->pool, sizeof(ap_listen_rec));
395         if (!lr)
396             return 0;
397         lr->sd = NULL;
398         lr->next = ap_listeners;
399         ap_listeners = lr;
400     }
401
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.
404      */
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 */
412             exit(1);
413         }
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 */
422             exit(1);
423         }
424         apr_os_sock_put(&lr->sd, &nsd, pconf);
425         num_listeners++;
426     }
427     CloseHandle(pipe);
428     return num_listeners;
429 }
430
431
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.
436  *
437  * win9x_accept()
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.
447  */
448
449 typedef struct joblist_s {
450     struct joblist_s *next;
451     int sock;
452 } joblist;
453
454 typedef struct globals_s {
455     HANDLE jobsemaphore;
456     joblist *jobhead;
457     joblist *jobtail;
458     apr_lock_t *jobmutex;
459     int jobcount;
460 } globals;
461
462 globals allowed_globals = {NULL, NULL, NULL, NULL, 0};
463 #define MAX_SELECT_ERRORS 100
464
465 static void add_job(int sock)
466 {
467     joblist *new_job;
468
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()!");
473         return;
474     }
475     new_job->next = NULL;
476     new_job->sock = sock;
477
478     apr_lock_acquire(allowed_globals.jobmutex);
479
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);
487
488     apr_lock_release(allowed_globals.jobmutex);
489 }
490
491 static int remove_job(void)
492 {
493     joblist *job;
494     int sock;
495
496     WaitForSingleObject(allowed_globals.jobsemaphore, INFINITE);
497     apr_lock_acquire(allowed_globals.jobmutex);
498
499     if (shutdown_in_progress && !allowed_globals.jobhead) {
500         apr_lock_release(allowed_globals.jobmutex);
501         return (-1);
502     }
503     job = allowed_globals.jobhead;
504     ap_assert(job);
505     allowed_globals.jobhead = job->next;
506     if (allowed_globals.jobhead == NULL)
507         allowed_globals.jobtail = NULL;
508     apr_lock_release(allowed_globals.jobmutex);
509     sock = job->sock;
510     free(job);
511
512     return (sock);
513 }
514
515 static void win9x_accept(void * dummy)
516 {
517     int requests_this_child = 0;
518     struct timeval tv;
519     fd_set main_fds;
520     int wait_time = 1;
521     int csd;
522     SOCKET nsd = INVALID_SOCKET;
523     struct sockaddr_in sa_client;
524     int count_select_errors = 0;
525     int rc;
526     int clen;
527     ap_listen_rec *lr;
528     struct fd_set listenfds;
529     SOCKET listenmaxfd = INVALID_SOCKET;
530
531     /* Setup the listeners 
532      * ToDo: Use apr_poll()
533      */
534     FD_ZERO(&listenfds);
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) {
540                 listenmaxfd = nsd;
541             }
542         }
543     }
544     head_listener = ap_listeners;
545
546     while (!shutdown_in_progress) {
547         if (ap_max_requests_per_child && (requests_this_child > ap_max_requests_per_child)) {
548             break;
549         }
550
551         tv.tv_sec = wait_time;
552         tv.tv_usec = 0;
553         memcpy(&main_fds, &listenfds, sizeof(fd_set));
554
555         rc = select(listenmaxfd + 1, &main_fds, NULL, NULL, &tv);
556
557         if (rc == 0 || (rc == SOCKET_ERROR && APR_STATUS_IS_EINTR(apr_get_netos_error()))) {
558             count_select_errors = 0;    /* reset count of errors */            
559             continue;
560         }
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.
565              */
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.");
573                 break;
574             }
575         } else {
576             ap_listen_rec *lr;
577
578             lr = find_ready_listener(&main_fds);
579             if (lr != NULL) {
580                 /* fetch the native socket descriptor */
581                 apr_os_sock_get(&nsd, lr->sd);
582             }
583         }
584
585         do {
586             clen = sizeof(sa_client);
587             csd = accept(nsd, (struct sockaddr *) &sa_client, &clen);
588             if (csd == INVALID_SOCKET) {
589                 csd = -1;
590             }
591         } while (csd < 0 && APR_STATUS_IS_EINTR(apr_get_netos_error()));
592
593         if (csd < 0) {
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)");
597             }
598         }
599         else {
600             add_job(csd);
601             requests_this_child++;
602         }
603     }
604     SetEvent(exit_event);
605 }
606
607 static PCOMP_CONTEXT win9x_get_connection(PCOMP_CONTEXT context)
608 {
609     int len;
610
611     if (context == NULL) {
612         /* allocate the completion context and the transaction pool */
613         context = apr_pcalloc(pconf, sizeof(COMP_CONTEXT));
614         if (!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.");
617             return NULL;
618         }
619         apr_pool_create(&context->ptrans, pconf);
620     }
621     
622
623     while (1) {
624         apr_pool_clear(context->ptrans);        
625         context->accept_socket = remove_job();
626         if (context->accept_socket == -1) {
627             return NULL;
628         }
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");
635             continue;
636         }
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));
644         }
645
646         /* do we NEED_DUPPED_CSD ?? */
647         
648         return context;
649     }
650 }
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 
655  * IOCompletionPort.
656  *
657  * winnt_accept()
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.
664  */
665 static void winnt_accept(void *listen_socket) 
666 {
667     static int num_completion_contexts = 0;
668     static int requests_this_child = 0;
669     PCOMP_CONTEXT pCompContext;
670     DWORD BytesRead;
671     SOCKET nlsd;
672     int lasterror;
673
674     nlsd = (SOCKET) listen_socket;
675
676     while (!shutdown_in_progress) {
677         if (ap_max_requests_per_child && (requests_this_child > ap_max_requests_per_child)) {
678             break;
679         }
680         pCompContext = NULL;
681         /* Grab a context off the queue */
682         apr_lock_acquire(qlock);
683         if (qhead) {
684             pCompContext = qhead;
685             qhead = qhead->next;
686             if (!qhead)
687                 qtail = NULL;
688         }
689         apr_lock_release(qlock);
690
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.
694          */
695         if (!pCompContext) {
696             if (num_completion_contexts >= ap_threads_per_child) {
697                 static int reported = 0;
698                 if (!reported) {
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");
702                     reported = 1;
703                 }
704                 Sleep(500);
705                 continue;
706             }
707             pCompContext = (PCOMP_CONTEXT) apr_pcalloc(pchild, sizeof(COMP_CONTEXT));
708
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.");
713                 // return -1;
714             }
715             pCompContext->accept_socket = INVALID_SOCKET;
716             num_completion_contexts++;
717         }
718
719     again:            
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);
723         
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.");
727                 // return -1;
728             }
729
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,
733                            sizeof(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. */
737             }
738         }
739
740         /* AcceptEx on the completion context. The completion context will be 
741          * signaled when a connection is accepted. 
742          */
743         if (!AcceptEx(nlsd, pCompContext->accept_socket,
744                       pCompContext->buff,
745                       0,
746                       PADDED_ADDR_SIZE, 
747                       PADDED_ADDR_SIZE,
748                       &BytesRead,
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.
755                  */
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)
760                     break;
761                 else
762                     goto again;
763             }
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.");
767                 // return -1;
768             }
769
770             /* Wait for pending i/o */
771             WaitForSingleObject(pCompContext->Overlapped.hEvent, INFINITE);
772         }
773
774         /* When a connection is received, send an io completion notification to
775          * the ThreadDispatchIOCP
776          */
777         PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, IOCP_CONNECTION_ACCEPTED,
778                                    &pCompContext->Overlapped);
779         requests_this_child++;
780     }
781
782     if (!shutdown_in_progress) {
783         /* Yow, hit an irrecoverable error! Tell the child to die. */
784         SetEvent(exit_event);
785     }
786 }
787 static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT pCompContext)
788 {
789     int requests_this_child = 0;
790     int rc;
791     DWORD BytesRead;
792     DWORD CompKey;
793     LPOVERLAPPED pol;
794
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.
800      */
801     if (pCompContext) {
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);
807         if (qtail)
808             qtail->next = pCompContext;
809         else
810             qhead = pCompContext;
811         qtail = pCompContext;
812         apr_lock_release(qlock);
813     }
814
815     g_blocked_threads++;        
816     while (1) {
817         if (workers_may_exit) {
818             g_blocked_threads--;
819             return NULL;
820         }
821         rc = GetQueuedCompletionStatus(ThreadDispatchIOCP, &BytesRead, &CompKey,
822                                        &pol, INFINITE);
823         if (!rc) {
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);
827             continue;
828         }
829
830         switch (CompKey) {
831         case IOCP_CONNECTION_ACCEPTED:
832             pCompContext = CONTAINING_RECORD(pol, COMP_CONTEXT, Overlapped);
833             break;
834         case IOCP_SHUTDOWN:
835             g_blocked_threads--;
836             return NULL;
837         default:
838             g_blocked_threads--;
839             return NULL;
840         }
841         break;
842     }
843
844     g_blocked_threads--;    
845
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);
849     }
850
851     /* Get the local & remote address */
852     GetAcceptExSockaddrs(pCompContext->buff,
853                          0,
854                          PADDED_ADDR_SIZE,
855                          PADDED_ADDR_SIZE,
856                          &pCompContext->sa_server,
857                          &pCompContext->sa_server_len,
858                          &pCompContext->sa_client,
859                          &pCompContext->sa_client_len);
860
861     return pCompContext;
862
863 }
864
865 /*
866  * worker_main()
867  * Main entry point for the worker threads. Worker threads block in 
868  * win*_get_connection() awaiting a connection to service.
869  */
870 static void worker_main(int thread_num)
871 {
872     PCOMP_CONTEXT context = NULL;
873     apr_os_sock_info_t sockinfo;
874
875     while (1) {
876         conn_rec *c;
877         apr_int32_t disconnected;
878
879         ap_update_child_status(0, thread_num, SERVER_READY, 
880                                (request_rec *) NULL);
881
882         /* Grab a connection off the network */
883         if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
884             context = win9x_get_connection(context);
885         }
886         else {
887             context = winnt_get_connection(context);
888         }
889
890         if (!context)
891             break;
892
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);
900
901         c = ap_new_connection(context->ptrans, server_conf, context->sock,
902                               thread_num);
903
904         if (c) {
905             ap_process_connection(c);
906             apr_getsocketopt(context->sock, APR_SO_DISCONNECTED, &disconnected);
907             if (!disconnected) {
908                 context->accept_socket = INVALID_SOCKET;
909                 ap_lingering_close(c);
910             }
911         }
912         else {
913             /* ap_new_connection closes the socket on failure */
914             context->accept_socket = INVALID_SOCKET;
915         }
916     }
917
918     ap_update_child_status(0, thread_num, SERVER_DEAD, (request_rec *) NULL);
919
920     ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
921                  "Child %d: Thread exiting.", my_pid);
922 }
923
924 static void cleanup_thread(thread *handles, int *thread_cnt, int thread_to_clean)
925 {
926     int i;
927
928     CloseHandle(handles[thread_to_clean]);
929     for (i = thread_to_clean; i < ((*thread_cnt) - 1); i++)
930         handles[i] = handles[i + 1];
931     (*thread_cnt)--;
932 }
933
934 /*
935  * child_main() 
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
939  * events.
940  */
941 static void child_main()
942 {
943     apr_status_t status;
944     HANDLE child_events[2];
945     char* exit_event_name;
946     int nthreads = ap_threads_per_child;
947     int tid;
948     thread *child_handles;
949     int rv;
950     time_t end_time;
951     int i;
952     int cld;
953
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));
957
958     if (one_process) {
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);
963     }
964     else {
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);
970     }
971
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;
976
977     ap_assert(start_mutex);
978     ap_assert(exit_event);
979     ap_assert(maintenance_event);
980
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);
984
985     /*
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.
989      */
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 */
995         exit(0);
996     }
997     ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf, 
998                  "Child %d: Acquired the start mutex.", my_pid);
999
1000     /*
1001      * Create the worker thread dispatch IOCompletionPort
1002      * on Windows NT/2000
1003      */
1004     if (osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
1005         /* Create the worker thread dispatch IOCP */
1006         ThreadDispatchIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
1007                                                     NULL,
1008                                                     0,
1009                                                     0); /* CONCURRENT ACTIVE THREADS */
1010         apr_lock_create(&qlock, APR_MUTEX, APR_INTRAPROCESS, NULL, pchild);
1011     }
1012
1013     /* 
1014      * Create the pool of worker threads
1015      */
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);
1023     }
1024
1025     /* 
1026      * Start the accept thread
1027      */
1028     if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
1029         _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) win9x_accept,
1030                        (void *) i, 0, &tid);
1031     } else {
1032         /* Start an accept thread per listener */
1033         SOCKET nlsd; /* native listening sock descriptor */
1034         ap_listen_rec *lr;
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);
1040             }
1041         }
1042     }
1043
1044     /* Wait for one of three events:
1045      * exit_event: 
1046      *    The exit_event is signaled by the parent process to notify 
1047      *    the child that it is time to exit.
1048      *
1049      * maintenance_event: 
1050      *    This event is signaled by the worker thread pool to direct 
1051      *    this thread to create more completion contexts.
1052      *
1053      * TIMEOUT:
1054      *    To do periodic maintenance on the server (check for thread exits,
1055      *    number of completion contexts, etc.)
1056      */
1057     while (1) {
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");
1064             break;
1065         }
1066         else if (rv == WAIT_TIMEOUT) {
1067             apr_proc_other_child_check();
1068         }
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);
1073             break;
1074         }
1075         else {
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);
1080         }
1081     }
1082
1083     /* Setting is_graceful will cause keep-alive connections to be closed
1084      * rather than block on the next network read.
1085      */
1086     is_graceful = 1;
1087
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.
1091      */
1092     shutdown_in_progress = 1;
1093
1094     /* Close the listening sockets */
1095     {
1096         ap_listen_rec *lr;
1097         for (lr = ap_listeners; lr ; lr = lr->next) {
1098             apr_socket_close(lr->sd);
1099         }
1100     }
1101
1102     /* Give the worker threads time to handle already accepted connections */
1103     Sleep(1000);
1104
1105     /* Release the start_mutex to let the new process (in the restart
1106      * scenario) a chance to begin accepting and servicing requests 
1107      */
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);
1111
1112     /* Tell the worker threads they may exit when done handling
1113      * a connection.
1114      */
1115     workers_may_exit = 1;
1116
1117     /* Shutdown the worker threads */
1118     if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
1119         for (i = 0; i < nthreads; i++) {
1120             add_job(-1);
1121         }
1122     }
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);
1130             }
1131             Sleep(1000);
1132         }
1133         /* Empty the accept queue of completion contexts */
1134         apr_lock_acquire(qlock);
1135         while (qhead) {
1136             CloseHandle(qhead->Overlapped.hEvent);
1137             closesocket(qhead->accept_socket);
1138             qhead = qhead->next;
1139         }
1140         apr_lock_release(qlock);
1141     }
1142
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;
1147     while (nthreads) {
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);
1153             continue;
1154         }
1155         break;
1156     }
1157
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]);
1162     }
1163     ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, server_conf, 
1164                  "Child %d: All worker threads have ended.", my_pid);
1165
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);
1170
1171     apr_pool_destroy(pchild);
1172     CloseHandle(exit_event);
1173 }
1174
1175 static void cleanup_process(HANDLE *handles, HANDLE *events, int position, int *processes)
1176 {
1177     int i;
1178     int handle = 0;
1179
1180     CloseHandle(handles[position]);
1181     CloseHandle(events[position]);
1182
1183     handle = (int)handles[position];
1184
1185     for (i = position; i < (*processes)-1; i++) {
1186         handles[i] = handles[i + 1];
1187         events[i] = events[i + 1];
1188     }
1189     (*processes)--;
1190 }
1191
1192 static int create_process(apr_pool_t *p, HANDLE *handles, HANDLE *events, int *processes)
1193 {
1194     int rv;
1195     char buf[1024];
1196     char *pCommand;
1197     char *pEnvVar;
1198     char *pEnvBlock;
1199     int i;
1200     int iEnvBlockLen;
1201     STARTUPINFO si;           /* Filled in prior to call to CreateProcess */
1202     PROCESS_INFORMATION pi;   /* filled in on call to CreateProcess */
1203
1204     ap_listen_rec *lr;
1205     DWORD BytesWritten;
1206     HANDLE hPipeRead = NULL;
1207     HANDLE hPipeWrite = NULL;
1208     SECURITY_ATTRIBUTES sa = {0};  
1209
1210     HANDLE kill_event;
1211     LPWSAPROTOCOL_INFO  lpWSAProtocolInfo;
1212
1213     sa.nLength = sizeof(sa);
1214     sa.bInheritHandle = TRUE;
1215     sa.lpSecurityDescriptor = NULL;
1216
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...
1220      */
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");
1225         return -1;
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.");
1229         return -1;
1230     }
1231
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);
1236     }
1237
1238     /* Build the environment, since Win9x disrespects the active env */
1239     pEnvVar = apr_psprintf(p, "AP_PARENT_PID=%i", parent_pid);
1240     /*
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.
1244      */  
1245     i = 0;
1246     iEnvBlockLen = 1;
1247     while (_environ[i]) {
1248         iEnvBlockLen += strlen(_environ[i]) + 1;
1249         i++;
1250     }
1251
1252     pEnvBlock = (char *)apr_pcalloc(p, iEnvBlockLen + strlen(pEnvVar) + 1);
1253     strcpy(pEnvBlock, pEnvVar);
1254     pEnvVar = strchr(pEnvBlock, '\0') + 1;
1255
1256     i = 0;
1257     while (_environ[i]) {
1258         strcpy(pEnvVar, _environ[i]);
1259         pEnvVar = strchr(pEnvVar, '\0') + 1;
1260         i++;
1261     }
1262     pEnvVar = '\0';
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.");
1267         return -1;
1268     }
1269
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.
1272      */
1273     memset(&si, 0, sizeof(si));
1274     memset(&pi, 0, sizeof(pi));
1275     si.cb = sizeof(si);
1276     si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
1277     si.wShowWindow = SW_HIDE;
1278     si.hStdInput   = hPipeRead;
1279
1280     if (!CreateProcess(NULL, pCommand, NULL, NULL, 
1281                        TRUE,               /* Inherit handles */
1282                        CREATE_SUSPENDED,   /* Creation flags */
1283                        pEnvBlock,          /* Environment block */
1284                        NULL,
1285                        &si, &pi)) {
1286         ap_log_error(APLOG_MARK, APLOG_CRIT, apr_get_os_error(), server_conf,
1287                      "Parent: Not able to create the child process.");
1288         /*
1289          * We must close the handles to the new process and its main thread
1290          * to prevent handle and memory leaks.
1291          */ 
1292         CloseHandle(pi.hProcess);
1293         CloseHandle(pi.hThread);
1294         return -1;
1295     }
1296     
1297     ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
1298                  "Parent: Created child process %d", pi.dwProcessId);
1299
1300     SetEnvironmentVariable("AP_PARENT_PID",NULL);
1301
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));
1307     if (!kill_event) {
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);
1312         return -1;
1313     }
1314     
1315     /* Assume the child process lives. Update the process and event tables */
1316     handles[*processes] = pi.hProcess;
1317     events[*processes] = kill_event;
1318     (*processes)++;
1319
1320     /* We never store the thread's handle, so close it now. */
1321     ResumeThread(pi.hThread);
1322     CloseHandle(pi.hThread);
1323
1324     /* Important:
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.
1329      */
1330     Sleep(1000);
1331
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.
1335      */
1336     for (lr = ap_listeners; lr; lr = lr->next) {
1337         int nsd;
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 );
1346             return -1;
1347         }
1348
1349         if (!WriteFile(hPipeWrite, lpWSAProtocolInfo, (DWORD) sizeof(WSAPROTOCOL_INFO),
1350                        &BytesWritten,
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 );
1354             return -1;
1355         }
1356         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, APR_SUCCESS, server_conf,
1357                      "Parent: BytesWritten = %d WSAProtocolInfo = %x20", BytesWritten, *lpWSAProtocolInfo);
1358     }
1359
1360     CloseHandle(hPipeRead);
1361     CloseHandle(hPipeWrite);        
1362
1363     return 0;
1364 }
1365
1366 /**********************************************************************
1367  * master_main()
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. 
1371  * 
1372  * restart_event 
1373  * - Child is signaled to die gracefully
1374  * shutdown_event 
1375  * - Child is signaled to die gracefully
1376  * child_exit_event 
1377  * - Child has died, either normally (max_request_per_child)
1378  * or abnormally (seg fault, irrecoverable error condition detected by the 
1379  * child)
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)
1383 {
1384     int remaining_children_to_start = ap_daemons_to_start;
1385     int i;
1386     int rv, cld;
1387     int child_num = 0;
1388     int restart_pending = 0;
1389     int shutdown_pending = 0;
1390     int current_live_processes = 0; /* number of child process we know about */
1391
1392     HANDLE process_handles[MAX_PROCESSES];
1393     HANDLE process_kill_events[MAX_PROCESSES];
1394
1395
1396     /* Create child process 
1397      * Should only be one in this version of Apache for WIN32 
1398      */
1399     while (remaining_children_to_start--) {
1400         if (create_process(pconf, process_handles, process_kill_events, 
1401                            &current_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;
1405             goto die_now;
1406         }
1407     }
1408     
1409     restart_pending = shutdown_pending = 0;
1410
1411     if (!strcasecmp(signal_arg, "runservice"))
1412         mpm_service_started();
1413
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;
1417
1418     rv = WaitForMultipleObjects(current_live_processes+2, (HANDLE *)process_handles, 
1419                                 FALSE, INFINITE);
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;
1426     }
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;
1432     }
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)");
1442         }
1443
1444     }
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.");
1454         }
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...
1460          */
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, &current_live_processes);
1466         }
1467     } 
1468     else {
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
1472          * child process.
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.
1478          */
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, &current_live_processes);
1484         /* APD2("main_process: child in slot %d died", rv); */
1485         /* restart_child(process_hancles, process_kill_events, cld, &current_live_processes); */
1486     }
1487
1488 die_now:
1489     if (shutdown_pending) 
1490     {
1491         int tmstart = time(NULL);
1492         
1493         if (strcasecmp(signal_arg, "runservice")) {
1494             mpm_service_stopping();
1495         }
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);
1502         }
1503
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)
1507                 continue;
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, &current_live_processes);
1512         }
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);
1517         }
1518         return 0;  /* Tell the caller we do not want to restart */
1519     }
1520
1521     return 1;      /* Tell the caller we want a restart */
1522 }
1523
1524 /* set_listeners_noninheritable()
1525  * Make the listening socket handles noninheritable by processes
1526  * started out of this process.
1527  */
1528 static int set_listeners_noninheritable(apr_pool_t *p) 
1529 {
1530     ap_listen_rec *lr;
1531     HANDLE dup;
1532     SOCKET nsd;
1533     HANDLE hProcess = GetCurrentProcess();
1534
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(), 
1541                          server_conf,
1542                          "set_listeners_noninheritable: DuplicateHandle failed.");
1543             return 0;
1544         }
1545         nsd = (SOCKET) dup;
1546         apr_os_sock_put(&lr->sd, &nsd, p);
1547     }
1548     return 1;
1549 }
1550
1551 /* service_nt_main_fn needs to append the StartService() args 
1552  * outside of our call stack and thread as the service starts...
1553  */
1554 apr_array_header_t *mpm_new_argv;
1555
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.
1559  */
1560
1561 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
1562 {
1563     switch(query_code){
1564         case AP_MPMQ_MAX_DAEMON_USED:
1565             *result = MAXIMUM_WAIT_OBJECTS;
1566             return APR_SUCCESS;
1567         case AP_MPMQ_IS_THREADED:
1568             *result = AP_MPMQ_STATIC;
1569             return APR_SUCCESS;
1570         case AP_MPMQ_IS_FORKED:
1571             *result = AP_MPMQ_NOT_SUPPORTED;
1572             return APR_SUCCESS;
1573         case AP_MPMQ_HARD_LIMIT_DAEMONS:
1574             *result = HARD_SERVER_LIMIT;
1575             return APR_SUCCESS;
1576         case AP_MPMQ_HARD_LIMIT_THREADS:
1577             *result = HARD_THREAD_LIMIT;
1578             return APR_SUCCESS;
1579         case AP_MPMQ_MAX_THREADS:
1580             *result = ap_threads_per_child;
1581             return APR_SUCCESS;
1582         case AP_MPMQ_MIN_SPARE_DEAMONS:
1583             *result = 0;
1584             return APR_SUCCESS;
1585         case AP_MPMQ_MIN_SPARE_THREADS:    
1586             *result = 0;
1587             return APR_SUCCESS;
1588         case AP_MPMQ_MAX_SPARE_DAEMONS:
1589             *result = 0;
1590             return APR_SUCCESS;
1591         case AP_MPMQ_MAX_SPARE_THREADS:
1592             *result = 0;
1593             return APR_SUCCESS;
1594         case AP_MPMQ_MAX_REQUESTS_DEAMON:
1595             *result = ap_max_requests_per_child;
1596             return APR_SUCCESS;
1597         case AP_MPMQ_MAX_DAEMONS:
1598             *result = 0;
1599             return APR_SUCCESS;
1600     }
1601     return APR_ENOTIMPL;
1602
1603
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;
1610     
1611 void winnt_rewrite_args(process_rec *process) 
1612 {
1613     /* Handle the following SCM aspects in this phase:
1614      *
1615      *   -k runservice [transition for WinNT, nothing for Win9x]
1616      *   -k (!)install [error out if name is not installed]
1617      *
1618      * We can't leave this phase until we know our identity
1619      * and modify the command arguments appropriately.
1620      */
1621     apr_status_t rv;
1622     char *def_server_root;
1623     char fnbuf[MAX_PATH];
1624     char optbuf[3];
1625     const char *optarg;
1626     int fixed_args;
1627     char *pid;
1628     apr_getopt_t *opt;
1629     int running_as_service = 1;
1630
1631     osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1632     GetVersionEx(&osver);
1633
1634     /* AP_PARENT_PID is only valid in the child */
1635     pid = getenv("AP_PARENT_PID");
1636     if (pid) 
1637     {
1638         /* This is the child */
1639         my_pid = GetCurrentProcessId();
1640         parent_pid = (DWORD) atol(pid);
1641
1642         /* The parent is responsible for providing the
1643          * COMPLETE ARGUMENTS REQUIRED to the child.
1644          *
1645          * No further argument parsing is needed, but
1646          * for good measure we will provide a simple
1647          * signal string for later testing.
1648          */
1649         signal_arg = "runchild";
1650         return;
1651     }
1652     
1653     /* This is the parent, we have a long way to go :-) */
1654     parent_pid = my_pid = GetCurrentProcessId();
1655     
1656     /* Rewrite process->argv[]; 
1657      *
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
1661      * 
1662      * The end result will look like:
1663      *
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
1668      */
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");
1673         exit(1);
1674     }
1675     /* WARNING: There is an implict assumption here that the
1676      * executable resides in ServerRoot or ServerRoot\bin
1677      */
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';
1684     }
1685     apr_filepath_merge(&def_server_root, NULL, fnbuf, 
1686                        APR_FILEPATH_TRUENAME, process->pool);
1687
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.
1692      */
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;
1698
1699     fixed_args = mpm_new_argv->nelts;
1700
1701     optbuf[0] = '-';
1702     optbuf[2] = '\0';
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]) {
1707         case 'n':
1708             service_set = mpm_service_set_name(process->pool, &service_name, 
1709                                                optarg);
1710             break;
1711         case 'k':
1712             signal_arg = optarg;
1713             break;
1714         case 'i':
1715             ap_log_error(APLOG_MARK,APLOG_WARNING, 0, NULL,
1716                 "-i is deprecated.  Use -k install.");
1717             signal_arg = "install";
1718             break;
1719         case 'u':
1720             ap_log_error(APLOG_MARK,APLOG_WARNING, 0, NULL,
1721                 "-u is deprecated.  Use -k uninstall.");
1722             signal_arg = "uninstall";
1723             break;
1724         default:
1725             *(const char **)apr_array_push(mpm_new_argv) =
1726                 apr_pstrdup(process->pool, optbuf);
1727
1728             if (optarg) {
1729                 *(const char **)apr_array_push(mpm_new_argv) = optarg;
1730             }
1731             break;
1732         }
1733     }
1734     
1735     /* Track the number of args actually entered by the user */
1736     inst_argc = mpm_new_argv->nelts - fixed_args;
1737
1738     /* Provide a default 'run' -k arg to simplify signal_arg tests */
1739     if (!signal_arg)
1740     {
1741         signal_arg = "run";
1742         running_as_service = 0;
1743     }
1744
1745     if (!strcasecmp(signal_arg, "runservice")) 
1746     {
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
1751          * add for us.
1752          *
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.
1756          *
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.
1759          */
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;
1764         }
1765     }
1766
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);
1771     }
1772
1773     if (!strcasecmp(signal_arg, "install")) /* -k install */
1774     {
1775         if (service_set == APR_SUCCESS) 
1776         {
1777             ap_log_error(APLOG_MARK,APLOG_ERR, 0, NULL,
1778                  "%s: Service is already installed.", service_name);
1779             exit(1);
1780         }
1781     }
1782     else if (running_as_service)
1783     {
1784         if (service_set == APR_SUCCESS) 
1785         {
1786             rv = mpm_merge_service_args(process->pool, mpm_new_argv, 
1787                                         fixed_args);
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);
1792             }
1793             else  {
1794                 ap_log_error(APLOG_MARK,APLOG_WARNING, rv, NULL,
1795                              "No installed ConfigArgs for the service "
1796                              "\"%s\", using Apache defaults.", service_name);
1797             }
1798         }
1799         else
1800         {
1801             ap_log_error(APLOG_MARK,APLOG_ERR, service_set, NULL,
1802                  "No installed service named \"%s\".", service_name);
1803             exit(1);
1804         }
1805     }
1806     if (strcasecmp(signal_arg, "install") && service_set && service_set != SERVICE_UNSET) 
1807     {
1808         ap_log_error(APLOG_MARK,APLOG_ERR, service_set, NULL,
1809              "No installed service named \"%s\".", service_name);
1810         exit(1);
1811     }
1812     
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.
1816      */
1817     inst_argv = (const char * const *)mpm_new_argv->elts
1818         + mpm_new_argv->nelts - inst_argc;
1819
1820     process->argc = mpm_new_argv->nelts; 
1821     process->argv = (const char * const *) mpm_new_argv->elts;
1822 }
1823
1824
1825 static void winnt_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp) 
1826 {
1827     /* Handle the following SCM aspects in this phase:
1828      *
1829      *   -k runservice [WinNT errors logged from rewrite_args]
1830      *   -k uninstall
1831      *   -k stop
1832      *
1833      * in these cases we -don't- care if httpd.conf has config errors!
1834      */
1835     apr_status_t rv;
1836
1837     if (ap_exists_config_define("ONE_PROCESS"))
1838         one_process = -1;
1839
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.",
1845                      service_name);
1846         exit(1);
1847     }
1848
1849     if (!strcasecmp(signal_arg, "uninstall")) {
1850         rv = mpm_service_uninstall();
1851         exit(rv);
1852     }
1853
1854     if (!strcasecmp(signal_arg, "stop")) {
1855         mpm_signal_service(ptemp, 0);
1856         exit(0);
1857     }
1858
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;
1864
1865     apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1866 }
1867
1868 static void winnt_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec* server)
1869 {
1870     static int restart_num = 0;
1871     apr_status_t rv = 0;
1872
1873     server_conf = server;
1874     
1875     /* Handle the following SCM aspects in this phase:
1876      *
1877      *   -k install
1878      *   -k start
1879      *   -k restart
1880      *   -k runservice [Win95, only once - after we parsed the config]
1881      *
1882      * because all of these signals are useful _only_ if there
1883      * is a valid conf\httpd.conf environment to start.
1884      *
1885      * We reached this phase by avoiding errors that would cause
1886      * these options to fail unexpectedly in another process.
1887      */
1888
1889     if (!strcasecmp(signal_arg, "install")) {
1890         rv = mpm_service_install(ptemp, inst_argc, inst_argv);
1891         exit (rv);
1892     }
1893
1894     if (!strcasecmp(signal_arg, "start")) {
1895         rv = mpm_service_start(ptemp, inst_argc, inst_argv);
1896         exit (rv);
1897     }
1898
1899     if (!strcasecmp(signal_arg, "restart")) {
1900         mpm_signal_service(ptemp, 1);
1901         exit (rv);
1902     }
1903
1904     if (parent_pid == my_pid) 
1905     {
1906         if (restart_num++ == 1) 
1907         {
1908             /* This code should be run once in the parent and not run
1909              * across a restart
1910              */
1911             PSECURITY_ATTRIBUTES sa = GetNullACL();  /* returns NULL if invalid (Win95?) */
1912             setup_signal_names(apr_psprintf(pconf,"ap%d", parent_pid));
1913
1914             ap_log_pid(pconf, ap_pid_fname);
1915             
1916             /* Create shutdown event, apPID_shutdown, where PID is the parent 
1917              * Apache process ID. Shutdown is signaled by 'apache -k shutdown'.
1918              */
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);
1924                 exit(1);
1925             }
1926
1927             /* Create restart event, apPID_restart, where PID is the parent 
1928              * Apache process ID. Restart is signaled by 'apache -k restart'.
1929              */
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);
1936                 exit(1);
1937             }
1938             CleanNullACL((void *)sa);
1939
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.
1944              */
1945             if (!strcasecmp(signal_arg, "runservice"))
1946             {
1947                 if (osver.dwPlatformId != VER_PLATFORM_WIN32_NT) 
1948                 {
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.",
1953                                      service_name);
1954                         exit(1);
1955                     }            
1956                 }
1957             }
1958             else /* ! -k runservice */
1959             {
1960                 mpm_start_console_handler();
1961             }
1962
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.
1966              */
1967             apr_lock_create(&start_mutex,APR_MUTEX, APR_CROSS_PROCESS, signal_name_prefix,
1968                                server_conf->process->pool);
1969         }
1970     }
1971     else /* parent_pid != my_pid */
1972     {
1973         mpm_start_child_console_handler();
1974     }
1975 }
1976
1977 AP_DECLARE(int) ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s )
1978 {
1979     static int restart = 0;            /* Default is "not a restart" */
1980
1981     pconf = _pconf;
1982     server_conf = s;
1983
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);
1988
1989         /* Set up the scoreboard. */
1990         ap_run_pre_mpm(pconf, SB_NOT_SHARED);
1991         if (one_process) {
1992             if (ap_setup_listeners(server_conf) < 1) {
1993                 return 1;
1994             }
1995         }
1996         else {
1997             get_listeners_from_parent(server_conf);
1998         }
1999         if (!set_listeners_noninheritable(pconf)) {
2000             return 1;
2001         }
2002         child_main();
2003
2004         ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, server_conf,
2005                      "Child %d: Child process is exiting", my_pid);        
2006         return 1;
2007     }
2008     else { 
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");
2013             return 1;
2014         }
2015         if (!set_listeners_noninheritable(pconf)) {
2016             return 1;
2017         }
2018         restart = master_main(server_conf, shutdown_event, restart_event);
2019
2020         if (!restart) {
2021             /* Shutting down. Clean up... */
2022             const char *pidfile = ap_server_root_relative (pconf, ap_pid_fname);
2023
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());
2028             }
2029             apr_lock_destroy(start_mutex);
2030
2031             CloseHandle(restart_event);
2032             CloseHandle(shutdown_event);
2033
2034             return 1;
2035         }
2036     }  /* Parent process */
2037
2038     return 0; /* Restart */
2039 }
2040
2041 static void winnt_hooks(apr_pool_t *p)
2042 {
2043     ap_hook_pre_config(winnt_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
2044     ap_hook_post_config(winnt_post_config, NULL, NULL, 0);
2045 }
2046
2047 /* 
2048  * Command processors 
2049  */
2050
2051 static const char *set_threads_per_child (cmd_parms *cmd, void *dummy, char *arg) 
2052 {
2053     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2054     if (err != NULL) {
2055         return err;
2056     }
2057
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, 
2063                      HARD_THREAD_LIMIT);
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;
2069     }
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;
2074     }
2075     return NULL;
2076 }
2077
2078 /* Stub functions until this MPM supports the connection status API */
2079
2080 AP_DECLARE(void) ap_update_connection_status(long conn_id, const char *key, \
2081                                              const char *value)
2082 {
2083     /* NOP */
2084 }
2085
2086 AP_DECLARE(void) ap_reset_connection_status(long conn_id)
2087 {
2088     /* NOP */
2089 }
2090
2091 AP_DECLARE(apr_array_header_t *) ap_get_status_table(apr_pool_t *p)
2092 {
2093     /* NOP */
2094     return NULL;
2095 }
2096
2097 static const command_rec winnt_cmds[] = {
2098 LISTEN_COMMANDS
2099 { "ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF, TAKE1,
2100   "Number of threads each child creates" },
2101 { NULL }
2102 };
2103
2104 AP_MODULE_DECLARE_DATA module mpm_winnt_module = {
2105     MPM20_MODULE_STUFF,
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 */
2113 };