]> granicus.if.org Git - apache/blob - server/mpm/worker/worker.c
consolidate code related to sig_coredump
[apache] / server / mpm / worker / worker.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2003 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 /* The purpose of this MPM is to fix the design flaws in the threaded
60  * model.  Because of the way that pthreads and mutex locks interact,
61  * it is basically impossible to cleanly gracefully shutdown a child
62  * process if multiple threads are all blocked in accept.  This model
63  * fixes those problems.
64  */
65
66 #include "apr.h"
67 #include "apr_portable.h"
68 #include "apr_strings.h"
69 #include "apr_file_io.h"
70 #include "apr_thread_proc.h"
71 #include "apr_signal.h"
72 #include "apr_thread_mutex.h"
73 #include "apr_proc_mutex.h"
74 #include "apr_poll.h"
75 #define APR_WANT_STRFUNC
76 #include "apr_want.h"
77
78 #if APR_HAVE_UNISTD_H
79 #include <unistd.h>
80 #endif
81 #if APR_HAVE_SYS_SOCKET_H
82 #include <sys/socket.h>
83 #endif
84 #if APR_HAVE_SYS_WAIT_H
85 #include <sys/wait.h> 
86 #endif
87 #ifdef HAVE_SYS_PROCESSOR_H
88 #include <sys/processor.h> /* for bindprocessor() */
89 #endif
90
91 #if !APR_HAS_THREADS
92 #error The Worker MPM requires APR threads, but they are unavailable.
93 #endif
94
95 #define CORE_PRIVATE 
96  
97 #include "ap_config.h"
98 #include "httpd.h" 
99 #include "http_main.h" 
100 #include "http_log.h" 
101 #include "http_config.h"        /* for read_config */ 
102 #include "http_core.h"          /* for get_remote_host */ 
103 #include "http_connection.h"
104 #include "ap_mpm.h"
105 #include "pod.h"
106 #include "mpm_common.h"
107 #include "ap_listen.h"
108 #include "scoreboard.h" 
109 #include "fdqueue.h"
110 #include "mpm_default.h"
111
112 #include <signal.h>
113 #include <limits.h>             /* for INT_MAX */
114
115 /* Limit on the total --- clients will be locked out if more servers than
116  * this are needed.  It is intended solely to keep the server from crashing
117  * when things get out of hand.
118  *
119  * We keep a hard maximum number of servers, for two reasons --- first off,
120  * in case something goes seriously wrong, we want to stop the fork bomb
121  * short of actually crashing the machine we're running on by filling some
122  * kernel table.  Secondly, it keeps the size of the scoreboard file small
123  * enough that we can read the whole thing without worrying too much about
124  * the overhead.
125  */
126 #ifndef DEFAULT_SERVER_LIMIT
127 #define DEFAULT_SERVER_LIMIT 16
128 #endif
129
130 /* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT.  We want
131  * some sort of compile-time limit to help catch typos.
132  */
133 #ifndef MAX_SERVER_LIMIT
134 #define MAX_SERVER_LIMIT 20000
135 #endif
136
137 /* Limit on the threads per process.  Clients will be locked out if more than
138  * this  * server_limit are needed.
139  *
140  * We keep this for one reason it keeps the size of the scoreboard file small
141  * enough that we can read the whole thing without worrying too much about
142  * the overhead.
143  */
144 #ifndef DEFAULT_THREAD_LIMIT
145 #define DEFAULT_THREAD_LIMIT 64 
146 #endif
147
148 /* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT.  We want
149  * some sort of compile-time limit to help catch typos.
150  */
151 #ifndef MAX_THREAD_LIMIT
152 #define MAX_THREAD_LIMIT 20000
153 #endif
154
155 /*
156  * Actual definitions of config globals
157  */
158
159 int ap_threads_per_child = 0;         /* Worker threads per child */
160 static int ap_daemons_to_start = 0;
161 static int min_spare_threads = 0;
162 static int max_spare_threads = 0;
163 static int ap_daemons_limit = 0;
164 static int server_limit = DEFAULT_SERVER_LIMIT;
165 static int first_server_limit;
166 static int thread_limit = DEFAULT_THREAD_LIMIT;
167 static int first_thread_limit;
168 static int changed_limit_at_restart;
169 static int dying = 0;
170 static int workers_may_exit = 0;
171 static int start_thread_may_exit = 0;
172 static int listener_may_exit = 0;
173 static int requests_this_child;
174 static int num_listensocks = 0;
175 static int resource_shortage = 0;
176 static fd_queue_t *worker_queue;
177 static fd_queue_info_t *worker_queue_info;
178
179 /* The structure used to pass unique initialization info to each thread */
180 typedef struct {
181     int pid;
182     int tid;
183     int sd;
184 } proc_info;
185
186 /* Structure used to pass information to the thread responsible for 
187  * creating the rest of the threads.
188  */
189 typedef struct {
190     apr_thread_t **threads;
191     apr_thread_t *listener;
192     int child_num_arg;
193     apr_threadattr_t *threadattr;
194 } thread_starter;
195
196 #define ID_FROM_CHILD_THREAD(c, t)    ((c * thread_limit) + t)
197
198 /*
199  * The max child slot ever assigned, preserved across restarts.  Necessary
200  * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts.  We 
201  * use this value to optimize routines that have to scan the entire 
202  * scoreboard.
203  */
204 int ap_max_daemons_limit = -1;
205
206 static ap_pod_t *pod;
207
208 /* *Non*-shared http_main globals... */
209
210 server_rec *ap_server_conf;
211
212 /* The worker MPM respects a couple of runtime flags that can aid
213  * in debugging. Setting the -DNO_DETACH flag will prevent the root process
214  * from detaching from its controlling terminal. Additionally, setting
215  * the -DONE_PROCESS flag (which implies -DNO_DETACH) will get you the
216  * child_main loop running in the process which originally started up.
217  * This gives you a pretty nice debugging environment.  (You'll get a SIGHUP
218  * early in standalone_main; just continue through.  This is the server
219  * trying to kill off any child processes which it might have lying
220  * around --- Apache doesn't keep track of their pids, it just sends
221  * SIGHUP to the process group, ignoring it in the root process.
222  * Continue through and you'll be fine.).
223  */
224
225 static int one_process = 0;
226
227 #ifdef DEBUG_SIGSTOP
228 int raise_sigstop_flags;
229 #endif
230
231 static apr_pool_t *pconf;                 /* Pool for config stuff */
232 static apr_pool_t *pchild;                /* Pool for httpd child stuff */
233
234 static pid_t ap_my_pid; /* Linux getpid() doesn't work except in main 
235                            thread. Use this instead */
236 static pid_t parent_pid;
237 static apr_os_thread_t *listener_os_thread;
238
239 /* Locks for accept serialization */
240 static apr_proc_mutex_t *accept_mutex;
241
242 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
243 #define SAFE_ACCEPT(stmt) (ap_listeners->next ? (stmt) : APR_SUCCESS)
244 #else
245 #define SAFE_ACCEPT(stmt) (stmt)
246 #endif
247
248 /* The LISTENER_SIGNAL signal will be sent from the main thread to the 
249  * listener thread to wake it up for graceful termination (what a child 
250  * process from an old generation does when the admin does "apachectl 
251  * graceful").  This signal will be blocked in all threads of a child
252  * process except for the listener thread.
253  */
254 #define LISTENER_SIGNAL     SIGHUP
255
256 /* An array of socket descriptors in use by each thread used to
257  * perform a non-graceful (forced) shutdown of the server. */
258 static apr_socket_t **worker_sockets;
259
260 static void close_worker_sockets(void)
261 {
262     int i;
263     for (i = 0; i < ap_threads_per_child; i++) {
264         if (worker_sockets[i]) {
265             apr_socket_close(worker_sockets[i]);
266             worker_sockets[i] = NULL;
267         }
268     }
269 }
270   
271 static void wakeup_listener(void)
272 {
273     listener_may_exit = 1;
274     if (!listener_os_thread) {
275         /* XXX there is an obscure path that this doesn't handle perfectly:
276          *     right after listener thread is created but before 
277          *     listener_os_thread is set, the first worker thread hits an
278          *     error and starts graceful termination
279          */
280         return;
281     }
282     /*
283      * we should just be able to "kill(ap_my_pid, LISTENER_SIGNAL)" on all
284      * platforms and wake up the listener thread since it is the only thread 
285      * with SIGHUP unblocked, but that doesn't work on Linux
286      */
287 #ifdef HAVE_PTHREAD_KILL
288     pthread_kill(*listener_os_thread, LISTENER_SIGNAL);
289 #else
290     kill(ap_my_pid, LISTENER_SIGNAL);
291 #endif
292 }
293
294 #define ST_INIT              0
295 #define ST_GRACEFUL          1
296 #define ST_UNGRACEFUL        2
297
298 static int terminate_mode = ST_INIT;
299
300 static void signal_threads(int mode)
301 {
302     if (terminate_mode == mode) {
303         return;
304     }
305     terminate_mode = mode;
306
307     /* in case we weren't called from the listener thread, wake up the
308      * listener thread
309      */
310     wakeup_listener();
311
312     /* for ungraceful termination, let the workers exit now;
313      * for graceful termination, the listener thread will notify the
314      * workers to exit once it has stopped accepting new connections
315      */
316     if (mode == ST_UNGRACEFUL) {
317         workers_may_exit = 1;
318         ap_queue_interrupt_all(worker_queue);
319         ap_queue_info_term(worker_queue_info);
320         close_worker_sockets(); /* forcefully kill all current connections */
321     }
322 }
323
324 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
325 {
326     switch(query_code){
327         case AP_MPMQ_MAX_DAEMON_USED:
328             *result = ap_max_daemons_limit;
329             return APR_SUCCESS;
330         case AP_MPMQ_IS_THREADED:
331             *result = AP_MPMQ_STATIC;
332             return APR_SUCCESS;
333         case AP_MPMQ_IS_FORKED:
334             *result = AP_MPMQ_DYNAMIC;
335             return APR_SUCCESS;
336         case AP_MPMQ_HARD_LIMIT_DAEMONS:
337             *result = server_limit;
338             return APR_SUCCESS;
339         case AP_MPMQ_HARD_LIMIT_THREADS:
340             *result = thread_limit;
341             return APR_SUCCESS;
342         case AP_MPMQ_MAX_THREADS:
343             *result = ap_threads_per_child;
344             return APR_SUCCESS;
345         case AP_MPMQ_MIN_SPARE_DAEMONS:
346             *result = 0;
347             return APR_SUCCESS;
348         case AP_MPMQ_MIN_SPARE_THREADS:    
349             *result = min_spare_threads;
350             return APR_SUCCESS;
351         case AP_MPMQ_MAX_SPARE_DAEMONS:
352             *result = 0;
353             return APR_SUCCESS;
354         case AP_MPMQ_MAX_SPARE_THREADS:
355             *result = max_spare_threads;
356             return APR_SUCCESS;
357         case AP_MPMQ_MAX_REQUESTS_DAEMON:
358             *result = ap_max_requests_per_child;
359             return APR_SUCCESS;
360         case AP_MPMQ_MAX_DAEMONS:
361             *result = ap_daemons_limit;
362             return APR_SUCCESS;
363     }
364     return APR_ENOTIMPL;
365 }
366
367 /* a clean exit from a child with proper cleanup */ 
368 static void clean_child_exit(int code) __attribute__ ((noreturn));
369 static void clean_child_exit(int code)
370 {
371     if (pchild) {
372         apr_pool_destroy(pchild);
373     }
374     exit(code);
375 }
376
377 static void just_die(int sig)
378 {
379     clean_child_exit(0);
380 }
381
382 /*****************************************************************
383  * Connection structures and accounting...
384  */
385
386 /* volatile just in case */
387 static int volatile shutdown_pending;
388 static int volatile restart_pending;
389 static int volatile is_graceful;
390 static volatile int child_fatal;
391 ap_generation_t volatile ap_my_generation;
392
393 /*
394  * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
395  * functions to initiate shutdown or restart without relying on signals. 
396  * Previously this was initiated in sig_term() and restart() signal handlers, 
397  * but we want to be able to start a shutdown/restart from other sources --
398  * e.g. on Win32, from the service manager. Now the service manager can
399  * call ap_start_shutdown() or ap_start_restart() as appropiate.  Note that
400  * these functions can also be called by the child processes, since global
401  * variables are no longer used to pass on the required action to the parent.
402  *
403  * These should only be called from the parent process itself, since the
404  * parent process will use the shutdown_pending and restart_pending variables
405  * to determine whether to shutdown or restart. The child process should
406  * call signal_parent() directly to tell the parent to die -- this will
407  * cause neither of those variable to be set, which the parent will
408  * assume means something serious is wrong (which it will be, for the
409  * child to force an exit) and so do an exit anyway.
410  */
411
412 static void ap_start_shutdown(void)
413 {
414     if (shutdown_pending == 1) {
415         /* Um, is this _probably_ not an error, if the user has
416          * tried to do a shutdown twice quickly, so we won't
417          * worry about reporting it.
418          */
419         return;
420     }
421     shutdown_pending = 1;
422 }
423
424 /* do a graceful restart if graceful == 1 */
425 static void ap_start_restart(int graceful)
426 {
427
428     if (restart_pending == 1) {
429         /* Probably not an error - don't bother reporting it */
430         return;
431     }
432     restart_pending = 1;
433     is_graceful = graceful;
434 }
435
436 static void sig_term(int sig)
437 {
438     ap_start_shutdown();
439 }
440
441 static void restart(int sig)
442 {
443     ap_start_restart(sig == AP_SIG_GRACEFUL);
444 }
445
446 static void set_signals(void)
447 {
448 #ifndef NO_USE_SIGACTION
449     struct sigaction sa;
450 #endif
451
452     if (!one_process) {
453         ap_fatal_signal_setup(ap_server_conf, pconf);
454     }
455
456 #ifndef NO_USE_SIGACTION
457     sigemptyset(&sa.sa_mask);
458     sa.sa_flags = 0;
459
460     sa.sa_handler = sig_term;
461     if (sigaction(SIGTERM, &sa, NULL) < 0)
462         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, 
463                      "sigaction(SIGTERM)");
464 #ifdef SIGINT
465     if (sigaction(SIGINT, &sa, NULL) < 0)
466         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, 
467                      "sigaction(SIGINT)");
468 #endif
469 #ifdef SIGXCPU
470     sa.sa_handler = SIG_DFL;
471     if (sigaction(SIGXCPU, &sa, NULL) < 0)
472         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, 
473                      "sigaction(SIGXCPU)");
474 #endif
475 #ifdef SIGXFSZ
476     sa.sa_handler = SIG_DFL;
477     if (sigaction(SIGXFSZ, &sa, NULL) < 0)
478         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, 
479                      "sigaction(SIGXFSZ)");
480 #endif
481 #ifdef SIGPIPE
482     sa.sa_handler = SIG_IGN;
483     if (sigaction(SIGPIPE, &sa, NULL) < 0)
484         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, 
485                      "sigaction(SIGPIPE)");
486 #endif
487
488     /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy 
489      * processing one */
490     sigaddset(&sa.sa_mask, SIGHUP);
491     sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
492     sa.sa_handler = restart;
493     if (sigaction(SIGHUP, &sa, NULL) < 0)
494         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, 
495                      "sigaction(SIGHUP)");
496     if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
497         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, 
498                      "sigaction(" AP_SIG_GRACEFUL_STRING ")");
499 #else
500     if (!one_process) {
501 #ifdef SIGXCPU
502         apr_signal(SIGXCPU, SIG_DFL);
503 #endif /* SIGXCPU */
504 #ifdef SIGXFSZ
505         apr_signal(SIGXFSZ, SIG_DFL);
506 #endif /* SIGXFSZ */
507     }
508
509     apr_signal(SIGTERM, sig_term);
510 #ifdef SIGHUP
511     apr_signal(SIGHUP, restart);
512 #endif /* SIGHUP */
513 #ifdef AP_SIG_GRACEFUL
514     apr_signal(AP_SIG_GRACEFUL, restart);
515 #endif /* AP_SIG_GRACEFUL */
516 #ifdef SIGPIPE
517     apr_signal(SIGPIPE, SIG_IGN);
518 #endif /* SIGPIPE */
519
520 #endif
521 }
522
523 /*****************************************************************
524  * Here follows a long bunch of generic server bookkeeping stuff...
525  */
526
527 int ap_graceful_stop_signalled(void)
528     /* XXX this is really a bad confusing obsolete name
529      * maybe it should be ap_mpm_process_exiting?
530      */
531 {
532     /* note: for a graceful termination, listener_may_exit will be set before
533      *       workers_may_exit, so check listener_may_exit
534      */
535     return listener_may_exit;
536 }
537
538 /*****************************************************************
539  * Child process main loop.
540  */
541
542 static void process_socket(apr_pool_t *p, apr_socket_t *sock, int my_child_num,
543                            int my_thread_num, apr_bucket_alloc_t *bucket_alloc)
544 {
545     conn_rec *current_conn;
546     long conn_id = ID_FROM_CHILD_THREAD(my_child_num, my_thread_num);
547     int csd;
548     ap_sb_handle_t *sbh;
549
550     ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
551     apr_os_sock_get(&csd, sock);
552
553     if (csd >= FD_SETSIZE) {
554         ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
555                      "new file descriptor %d is too large; you probably need "
556                      "to rebuild Apache with a larger FD_SETSIZE "
557                      "(currently %d)", 
558                      csd, FD_SETSIZE);
559         apr_socket_close(sock);
560         return;
561     }
562
563     current_conn = ap_run_create_connection(p, ap_server_conf, sock,
564                                             conn_id, sbh, bucket_alloc);
565     if (current_conn) {
566         ap_process_connection(current_conn, sock);
567         ap_lingering_close(current_conn);
568     }
569 }
570
571 /* requests_this_child has gone to zero or below.  See if the admin coded
572    "MaxRequestsPerChild 0", and keep going in that case.  Doing it this way
573    simplifies the hot path in worker_thread */
574 static void check_infinite_requests(void)
575 {
576     if (ap_max_requests_per_child) {
577         signal_threads(ST_GRACEFUL);
578     }
579     else {
580         /* wow! if you're executing this code, you may have set a record.
581          * either this child process has served over 2 billion requests, or
582          * you're running a threaded 2.0 on a 16 bit machine.  
583          *
584          * I'll buy pizza and beers at Apachecon for the first person to do
585          * the former without cheating (dorking with INT_MAX, or running with
586          * uncommitted performance patches, for example).    
587          *
588          * for the latter case, you probably deserve a beer too.   Greg Ames
589          */
590             
591         requests_this_child = INT_MAX;      /* keep going */ 
592     }
593 }
594
595 static void unblock_signal(int sig)
596 {
597     sigset_t sig_mask;
598
599     sigemptyset(&sig_mask);
600     sigaddset(&sig_mask, sig);
601 #if defined(SIGPROCMASK_SETS_THREAD_MASK)
602     sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);
603 #else
604     pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL);
605 #endif
606 }
607
608 static void dummy_signal_handler(int sig)
609 {
610     /* XXX If specifying SIG_IGN is guaranteed to unblock a syscall,
611      *     then we don't need this goofy function.
612      */
613 }
614
615 static void *listener_thread(apr_thread_t *thd, void * dummy)
616 {
617     proc_info * ti = dummy;
618     int process_slot = ti->pid;
619     apr_pool_t *tpool = apr_thread_pool_get(thd);
620     void *csd = NULL;
621     apr_pool_t *ptrans;                /* Pool for per-transaction stuff */
622     apr_pool_t *recycled_pool = NULL;
623     int n;
624     apr_pollfd_t *pollset;
625     apr_status_t rv;
626     ap_listen_rec *lr, *last_lr = ap_listeners;
627     int have_idle_worker = 0;
628
629     free(ti);
630
631     apr_poll_setup(&pollset, num_listensocks, tpool);
632     for(lr = ap_listeners ; lr != NULL ; lr = lr->next)
633         apr_poll_socket_add(pollset, lr->sd, APR_POLLIN);
634
635     /* Unblock the signal used to wake this thread up, and set a handler for
636      * it.
637      */
638     unblock_signal(LISTENER_SIGNAL);
639     apr_signal(LISTENER_SIGNAL, dummy_signal_handler);
640
641     /* TODO: Switch to a system where threads reuse the results from earlier
642        poll calls - manoj */
643     while (1) {
644         /* TODO: requests_this_child should be synchronized - aaron */
645         if (requests_this_child <= 0) {
646             check_infinite_requests();
647         }
648         if (listener_may_exit) break;
649
650         if (!have_idle_worker) {
651             rv = ap_queue_info_wait_for_idler(worker_queue_info,
652                                               &recycled_pool);
653             if (APR_STATUS_IS_EOF(rv)) {
654                 break; /* we've been signaled to die now */
655             }
656             else if (rv != APR_SUCCESS) {
657                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
658                              "apr_queue_info_wait failed. Attempting to "
659                              " shutdown process gracefully.");
660                 signal_threads(ST_GRACEFUL);
661                 break;
662             }
663             have_idle_worker = 1;
664         }
665             
666         /* We've already decremented the idle worker count inside
667          * ap_queue_info_wait_for_idler. */
668
669         if ((rv = SAFE_ACCEPT(apr_proc_mutex_lock(accept_mutex)))
670             != APR_SUCCESS) {
671             int level = APLOG_EMERG;
672
673             if (listener_may_exit) {
674                 break;
675             }
676             if (ap_scoreboard_image->parent[process_slot].generation != 
677                 ap_scoreboard_image->global->running_generation) {
678                 level = APLOG_DEBUG; /* common to get these at restart time */
679             }
680             ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
681                          "apr_proc_mutex_lock failed. Attempting to shutdown "
682                          "process gracefully.");
683             signal_threads(ST_GRACEFUL);
684             break;                    /* skip the lock release */
685         }
686
687         if (!ap_listeners->next) {
688             /* Only one listener, so skip the poll */
689             lr = ap_listeners;
690         }
691         else {
692             while (!listener_may_exit) {
693                 apr_status_t ret;
694                 apr_int16_t event;
695
696                 ret = apr_poll(pollset, num_listensocks, &n, -1);
697                 if (ret != APR_SUCCESS) {
698                     if (APR_STATUS_IS_EINTR(ret)) {
699                         continue;
700                     }
701
702                     /* apr_poll() will only return errors in catastrophic
703                      * circumstances. Let's try exiting gracefully, for now. */
704                     ap_log_error(APLOG_MARK, APLOG_ERR, ret, (const server_rec *)
705                                  ap_server_conf, "apr_poll: (listen)");
706                     signal_threads(ST_GRACEFUL);
707                 }
708
709                 if (listener_may_exit) break;
710
711                 /* find a listener */
712                 lr = last_lr;
713                 do {
714                     lr = lr->next;
715                     if (lr == NULL) {
716                         lr = ap_listeners;
717                     }
718                     /* XXX: Should we check for POLLERR? */
719                     apr_poll_revents_get(&event, lr->sd, pollset);
720                     if (event & APR_POLLIN) {
721                         last_lr = lr;
722                         goto got_fd;
723                     }
724                 } while (lr != last_lr);
725             }
726         }
727     got_fd:
728         if (!listener_may_exit) {
729             /* create a new transaction pool for each accepted socket */
730             if (recycled_pool == NULL) {
731                 apr_allocator_t *allocator;
732
733                 apr_allocator_create(&allocator);
734                 apr_allocator_max_free_set(allocator, ap_max_mem_free);
735                 apr_pool_create_ex(&ptrans, NULL, NULL, allocator);
736                 apr_allocator_owner_set(allocator, ptrans);
737             }
738             else {
739                 ptrans = recycled_pool;
740             }
741             apr_pool_tag(ptrans, "transaction");
742             rv = lr->accept_func(&csd, lr, ptrans);
743             /* later we trash rv and rely on csd to indicate success/failure */
744             AP_DEBUG_ASSERT(rv == APR_SUCCESS || !csd);
745
746             if (rv == APR_EGENERAL) {
747                 /* E[NM]FILE, ENOMEM, etc */
748                 resource_shortage = 1;
749                 signal_threads(ST_GRACEFUL);
750             }
751             if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
752                 != APR_SUCCESS) {
753                 int level = APLOG_EMERG;
754
755                 if (listener_may_exit) {
756                     break;
757                 }
758                 if (ap_scoreboard_image->parent[process_slot].generation != 
759                     ap_scoreboard_image->global->running_generation) {
760                     level = APLOG_DEBUG; /* common to get these at restart time */
761                 }
762                 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
763                              "apr_proc_mutex_unlock failed. Attempting to "
764                              "shutdown process gracefully.");
765                 signal_threads(ST_GRACEFUL);
766             }
767             if (csd != NULL) {
768                 rv = ap_queue_push(worker_queue, csd, ptrans);
769                 if (rv) {
770                     /* trash the connection; we couldn't queue the connected
771                      * socket to a worker 
772                      */
773                     apr_socket_close(csd);
774                     ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
775                                  "ap_queue_push failed");
776                 }
777                 else {
778                     have_idle_worker = 0;
779                 }
780             }
781         }
782         else {
783             if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
784                 != APR_SUCCESS) {
785                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
786                              "apr_proc_mutex_unlock failed. Attempting to "
787                              "shutdown process gracefully.");
788                 signal_threads(ST_GRACEFUL);
789             }
790             break;
791         }
792     }
793
794     ap_queue_term(worker_queue);
795     dying = 1;
796     ap_scoreboard_image->parent[process_slot].quiescing = 1;
797
798     /* wake up the main thread */
799     kill(ap_my_pid, SIGTERM);
800
801     apr_thread_exit(thd, APR_SUCCESS);
802     return NULL;
803 }
804
805 /* XXX For ungraceful termination/restart, we definitely don't want to
806  *     wait for active connections to finish but we may want to wait
807  *     for idle workers to get out of the queue code and release mutexes,
808  *     since those mutexes are cleaned up pretty soon and some systems
809  *     may not react favorably (i.e., segfault) if operations are attempted
810  *     on cleaned-up mutexes.
811  */
812 static void * APR_THREAD_FUNC worker_thread(apr_thread_t *thd, void * dummy)
813 {
814     proc_info * ti = dummy;
815     int process_slot = ti->pid;
816     int thread_slot = ti->tid;
817     apr_socket_t *csd = NULL;
818     apr_bucket_alloc_t *bucket_alloc;
819     apr_pool_t *last_ptrans = NULL;
820     apr_pool_t *ptrans;                /* Pool for per-transaction stuff */
821     apr_status_t rv;
822     int is_idle = 0;
823
824     free(ti);
825
826     ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_STARTING, NULL);
827
828     bucket_alloc = apr_bucket_alloc_create(apr_thread_pool_get(thd));
829
830     while (!workers_may_exit) {
831         if (!is_idle) {
832             rv = ap_queue_info_set_idle(worker_queue_info, last_ptrans);
833             last_ptrans = NULL;
834             if (rv != APR_SUCCESS) {
835                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
836                              "ap_queue_info_set_idle failed. Attempting to "
837                              "shutdown process gracefully.");
838                 signal_threads(ST_GRACEFUL);
839                 break;
840             }
841             is_idle = 1;
842         }
843
844         ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_READY, NULL);
845 worker_pop:
846         if (workers_may_exit) {
847             break;
848         }
849         rv = ap_queue_pop(worker_queue, &csd, &ptrans);
850
851         if (rv != APR_SUCCESS) {
852             /* We get APR_EOF during a graceful shutdown once all the connections
853              * accepted by this server process have been handled.
854              */
855             if (APR_STATUS_IS_EOF(rv)) {
856                 break;
857             }
858             /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
859              * from an explicit call to ap_queue_interrupt_all(). This allows
860              * us to unblock threads stuck in ap_queue_pop() when a shutdown
861              * is pending.
862              *
863              * If workers_may_exit is set and this is ungraceful termination/
864              * restart, we are bound to get an error on some systems (e.g.,
865              * AIX, which sanity-checks mutex operations) since the queue
866              * may have already been cleaned up.  Don't log the "error" if
867              * workers_may_exit is set.
868              */
869             else if (APR_STATUS_IS_EINTR(rv)) {
870                 goto worker_pop;
871             }
872             /* We got some other error. */
873             else if (!workers_may_exit) {
874                 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
875                              "ap_queue_pop failed");
876             }
877             continue;
878         }
879         is_idle = 0;
880         worker_sockets[thread_slot] = csd;
881         process_socket(ptrans, csd, process_slot, thread_slot, bucket_alloc);
882         worker_sockets[thread_slot] = NULL;
883         requests_this_child--; /* FIXME: should be synchronized - aaron */
884         apr_pool_clear(ptrans);
885         last_ptrans = ptrans;
886     }
887
888     ap_update_child_status_from_indexes(process_slot, thread_slot,
889         (dying) ? SERVER_DEAD : SERVER_GRACEFUL, (request_rec *) NULL);
890
891     apr_bucket_alloc_destroy(bucket_alloc);
892
893     apr_thread_exit(thd, APR_SUCCESS);
894     return NULL;
895 }
896
897 static int check_signal(int signum)
898 {
899     switch (signum) {
900     case SIGTERM:
901     case SIGINT:
902         return 1;
903     }
904     return 0;
905 }
906
907 static void create_listener_thread(thread_starter *ts)
908 {
909     int my_child_num = ts->child_num_arg;
910     apr_threadattr_t *thread_attr = ts->threadattr;
911     proc_info *my_info;
912     apr_status_t rv;
913
914     my_info = (proc_info *)malloc(sizeof(proc_info));
915     my_info->pid = my_child_num;
916     my_info->tid = -1; /* listener thread doesn't have a thread slot */
917     my_info->sd = 0;
918     rv = apr_thread_create(&ts->listener, thread_attr, listener_thread,
919                            my_info, pchild);
920     if (rv != APR_SUCCESS) {
921         ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
922                      "apr_thread_create: unable to create listener thread");
923         /* In case system resources are maxxed out, we don't want
924          * Apache running away with the CPU trying to fork over and
925          * over and over again if we exit.
926          * XXX Jeff doesn't see how Apache is going to try to fork again since
927          * the exit code is APEXIT_CHILDFATAL
928          */
929         apr_sleep(apr_time_from_sec(10));
930         clean_child_exit(APEXIT_CHILDFATAL);
931     }
932     apr_os_thread_get(&listener_os_thread, ts->listener);
933 }
934
935 /* XXX under some circumstances not understood, children can get stuck
936  *     in start_threads forever trying to take over slots which will
937  *     never be cleaned up; for now there is an APLOG_DEBUG message issued
938  *     every so often when this condition occurs
939  */
940 static void * APR_THREAD_FUNC start_threads(apr_thread_t *thd, void *dummy)
941 {
942     thread_starter *ts = dummy;
943     apr_thread_t **threads = ts->threads;
944     apr_threadattr_t *thread_attr = ts->threadattr;
945     int child_num_arg = ts->child_num_arg;
946     int my_child_num = child_num_arg;
947     proc_info *my_info;
948     apr_status_t rv;
949     int i;
950     int threads_created = 0;
951     int listener_started = 0;
952     int loops;
953     int prev_threads_created;
954
955     /* We must create the fd queues before we start up the listener
956      * and worker threads. */
957     worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
958     rv = ap_queue_init(worker_queue, ap_threads_per_child, pchild);
959     if (rv != APR_SUCCESS) {
960         ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
961                      "ap_queue_init() failed");
962         clean_child_exit(APEXIT_CHILDFATAL);
963     }
964
965     rv = ap_queue_info_create(&worker_queue_info, pchild,
966                               ap_threads_per_child);
967     if (rv != APR_SUCCESS) {
968         ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
969                      "ap_queue_info_create() failed");
970         clean_child_exit(APEXIT_CHILDFATAL);
971     }
972
973     worker_sockets = apr_pcalloc(pchild, ap_threads_per_child
974                                         * sizeof(apr_socket_t *));
975
976     loops = prev_threads_created = 0;
977     while (1) {
978         /* ap_threads_per_child does not include the listener thread */
979         for (i = 0; i < ap_threads_per_child; i++) {
980             int status = ap_scoreboard_image->servers[child_num_arg][i].status;
981
982             if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
983                 continue;
984             }
985
986             my_info = (proc_info *)malloc(sizeof(proc_info));
987             if (my_info == NULL) {
988                 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
989                              "malloc: out of memory");
990                 clean_child_exit(APEXIT_CHILDFATAL);
991             }
992             my_info->pid = my_child_num;
993             my_info->tid = i;
994             my_info->sd = 0;
995         
996             /* We are creating threads right now */
997             ap_update_child_status_from_indexes(my_child_num, i,
998                                                 SERVER_STARTING, NULL);
999             /* We let each thread update its own scoreboard entry.  This is
1000              * done because it lets us deal with tid better.
1001              */
1002             rv = apr_thread_create(&threads[i], thread_attr, 
1003                                    worker_thread, my_info, pchild);
1004             if (rv != APR_SUCCESS) {
1005                 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1006                     "apr_thread_create: unable to create worker thread");
1007                 /* In case system resources are maxxed out, we don't want
1008                    Apache running away with the CPU trying to fork over and
1009                    over and over again if we exit. */
1010                 apr_sleep(apr_time_from_sec(10));
1011                 clean_child_exit(APEXIT_CHILDFATAL);
1012             }
1013             threads_created++;
1014         }
1015         /* Start the listener only when there are workers available */
1016         if (!listener_started && threads_created) {
1017             create_listener_thread(ts);
1018             listener_started = 1;
1019         }
1020         if (start_thread_may_exit || threads_created == ap_threads_per_child) {
1021             break;
1022         }
1023         /* wait for previous generation to clean up an entry */
1024         apr_sleep(apr_time_from_sec(1));
1025         ++loops;
1026         if (loops % 120 == 0) { /* every couple of minutes */
1027             if (prev_threads_created == threads_created) {
1028                 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1029                              "child %" APR_PID_T_FMT " isn't taking over "
1030                              "slots very quickly (%d of %d)",
1031                              ap_my_pid, threads_created, ap_threads_per_child);
1032             }
1033             prev_threads_created = threads_created;
1034         }
1035     }
1036     
1037     /* What state should this child_main process be listed as in the 
1038      * scoreboard...?
1039      *  ap_update_child_status_from_indexes(my_child_num, i, SERVER_STARTING, 
1040      *                                      (request_rec *) NULL);
1041      * 
1042      *  This state should be listed separately in the scoreboard, in some kind
1043      *  of process_status, not mixed in with the worker threads' status.   
1044      *  "life_status" is almost right, but it's in the worker's structure, and 
1045      *  the name could be clearer.   gla
1046      */
1047     apr_thread_exit(thd, APR_SUCCESS);
1048     return NULL;
1049 }
1050
1051 static void join_workers(apr_thread_t *listener, apr_thread_t **threads)
1052 {
1053     int i;
1054     apr_status_t rv, thread_rv;
1055
1056     if (listener) {
1057         int iter;
1058         
1059         /* deal with a rare timing window which affects waking up the
1060          * listener thread...  if the signal sent to the listener thread
1061          * is delivered between the time it verifies that the
1062          * listener_may_exit flag is clear and the time it enters a
1063          * blocking syscall, the signal didn't do any good...  work around
1064          * that by sleeping briefly and sending it again
1065          */
1066
1067         iter = 0;
1068         while (iter < 10 && 
1069 #ifdef HAVE_PTHREAD_KILL
1070                pthread_kill(*listener_os_thread, 0)
1071 #else
1072                kill(ap_my_pid, 0)
1073 #endif
1074                == 0) {
1075             /* listener not dead yet */
1076             apr_sleep(apr_time_make(0, 500000));
1077             wakeup_listener();
1078             ++iter;
1079         }
1080         if (iter >= 10) {
1081             ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ap_server_conf,
1082                          "the listener thread didn't exit");
1083         }
1084         else {
1085             rv = apr_thread_join(&thread_rv, listener);
1086             if (rv != APR_SUCCESS) {
1087                 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1088                              "apr_thread_join: unable to join listener thread");
1089             }
1090         }
1091     }
1092     
1093     for (i = 0; i < ap_threads_per_child; i++) {
1094         if (threads[i]) { /* if we ever created this thread */
1095             rv = apr_thread_join(&thread_rv, threads[i]);
1096             if (rv != APR_SUCCESS) {
1097                 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1098                              "apr_thread_join: unable to join worker "
1099                              "thread %d",
1100                              i);
1101             }
1102         }
1103     }
1104 }
1105
1106 static void join_start_thread(apr_thread_t *start_thread_id)
1107 {
1108     apr_status_t rv, thread_rv;
1109
1110     start_thread_may_exit = 1; /* tell it to give up in case it is still 
1111                                 * trying to take over slots from a 
1112                                 * previous generation
1113                                 */
1114     rv = apr_thread_join(&thread_rv, start_thread_id);
1115     if (rv != APR_SUCCESS) {
1116         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1117                      "apr_thread_join: unable to join the start "
1118                      "thread");
1119     }
1120 }
1121
1122 static void child_main(int child_num_arg)
1123 {
1124     apr_thread_t **threads;
1125     apr_status_t rv;
1126     thread_starter *ts;
1127     apr_threadattr_t *thread_attr;
1128     apr_thread_t *start_thread_id;
1129
1130     ap_my_pid = getpid();
1131     ap_fatal_signal_child_setup(ap_server_conf);
1132     apr_pool_create(&pchild, pconf);
1133
1134     /*stuff to do before we switch id's, so we have permissions.*/
1135     ap_reopen_scoreboard(pchild, NULL, 0);
1136
1137     rv = SAFE_ACCEPT(apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname,
1138                                                pchild));
1139     if (rv != APR_SUCCESS) {
1140         ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1141                      "Couldn't initialize cross-process lock in child");
1142         clean_child_exit(APEXIT_CHILDFATAL);
1143     }
1144
1145     if (unixd_setup_child()) {
1146         clean_child_exit(APEXIT_CHILDFATAL);
1147     }
1148
1149     ap_run_child_init(pchild, ap_server_conf);
1150
1151     /* done with init critical section */
1152
1153     /* Just use the standard apr_setup_signal_thread to block all signals
1154      * from being received.  The child processes no longer use signals for
1155      * any communication with the parent process.
1156      */
1157     rv = apr_setup_signal_thread();
1158     if (rv != APR_SUCCESS) {
1159         ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1160                      "Couldn't initialize signal thread");
1161         clean_child_exit(APEXIT_CHILDFATAL);
1162     }
1163
1164     if (ap_max_requests_per_child) {
1165         requests_this_child = ap_max_requests_per_child;
1166     }
1167     else {
1168         /* coding a value of zero means infinity */
1169         requests_this_child = INT_MAX;
1170     }
1171     
1172     /* Setup worker threads */
1173
1174     /* clear the storage; we may not create all our threads immediately, 
1175      * and we want a 0 entry to indicate a thread which was not created
1176      */
1177     threads = (apr_thread_t **)calloc(1, 
1178                                 sizeof(apr_thread_t *) * ap_threads_per_child);
1179     if (threads == NULL) {
1180         ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
1181                      "malloc: out of memory");
1182         clean_child_exit(APEXIT_CHILDFATAL);
1183     }
1184
1185     ts = (thread_starter *)apr_palloc(pchild, sizeof(*ts));
1186
1187     apr_threadattr_create(&thread_attr, pchild);
1188     /* 0 means PTHREAD_CREATE_JOINABLE */
1189     apr_threadattr_detach_set(thread_attr, 0);
1190
1191     ts->threads = threads;
1192     ts->listener = NULL;
1193     ts->child_num_arg = child_num_arg;
1194     ts->threadattr = thread_attr;
1195
1196     rv = apr_thread_create(&start_thread_id, thread_attr, start_threads,
1197                            ts, pchild);
1198     if (rv != APR_SUCCESS) {
1199         ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1200                      "apr_thread_create: unable to create worker thread");
1201         /* In case system resources are maxxed out, we don't want
1202            Apache running away with the CPU trying to fork over and
1203            over and over again if we exit. */
1204         apr_sleep(apr_time_from_sec(10));
1205         clean_child_exit(APEXIT_CHILDFATAL);
1206     }
1207
1208     /* If we are only running in one_process mode, we will want to
1209      * still handle signals. */
1210     if (one_process) {
1211         /* Block until we get a terminating signal. */
1212         apr_signal_thread(check_signal);
1213         /* make sure the start thread has finished; signal_threads() 
1214          * and join_workers() depend on that
1215          */
1216         /* XXX join_start_thread() won't be awakened if one of our
1217          *     threads encounters a critical error and attempts to
1218          *     shutdown this child
1219          */
1220         join_start_thread(start_thread_id);
1221         signal_threads(ST_UNGRACEFUL); /* helps us terminate a little more
1222                            * quickly than the dispatch of the signal thread
1223                            * beats the Pipe of Death and the browsers
1224                            */
1225         /* A terminating signal was received. Now join each of the
1226          * workers to clean them up.
1227          *   If the worker already exited, then the join frees
1228          *   their resources and returns.
1229          *   If the worker hasn't exited, then this blocks until
1230          *   they have (then cleans up).
1231          */
1232         join_workers(ts->listener, threads);
1233     }
1234     else { /* !one_process */
1235         /* remove SIGTERM from the set of blocked signals...  if one of
1236          * the other threads in the process needs to take us down
1237          * (e.g., for MaxRequestsPerChild) it will send us SIGTERM
1238          */
1239         unblock_signal(SIGTERM);
1240         apr_signal(SIGTERM, dummy_signal_handler);
1241         /* Watch for any messages from the parent over the POD */
1242         while (1) {
1243             rv = ap_mpm_pod_check(pod);
1244             if (rv == AP_NORESTART) {
1245                 /* see if termination was triggered while we slept */
1246                 switch(terminate_mode) {
1247                 case ST_GRACEFUL:
1248                     rv = AP_GRACEFUL;
1249                     break;
1250                 case ST_UNGRACEFUL:
1251                     rv = AP_RESTART;
1252                     break;
1253                 }
1254             }
1255             if (rv == AP_GRACEFUL || rv == AP_RESTART) {
1256                 /* make sure the start thread has finished; 
1257                  * signal_threads() and join_workers depend on that
1258                  */
1259                 join_start_thread(start_thread_id);
1260                 signal_threads(rv == AP_GRACEFUL ? ST_GRACEFUL : ST_UNGRACEFUL);
1261                 break;
1262             }
1263         }
1264
1265         /* A terminating signal was received. Now join each of the
1266          * workers to clean them up.
1267          *   If the worker already exited, then the join frees
1268          *   their resources and returns.
1269          *   If the worker hasn't exited, then this blocks until
1270          *   they have (then cleans up).
1271          */
1272         join_workers(ts->listener, threads);
1273     }
1274
1275     free(threads);
1276
1277     clean_child_exit(resource_shortage ? APEXIT_CHILDSICK : 0);
1278 }
1279
1280 static int make_child(server_rec *s, int slot) 
1281 {
1282     int pid;
1283
1284     if (slot + 1 > ap_max_daemons_limit) {
1285         ap_max_daemons_limit = slot + 1;
1286     }
1287
1288     if (one_process) {
1289         set_signals();
1290         ap_scoreboard_image->parent[slot].pid = getpid();
1291         child_main(slot);
1292     }
1293
1294     if ((pid = fork()) == -1) {
1295         ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, 
1296                      "fork: Unable to fork new process");
1297
1298         /* fork didn't succeed. Fix the scoreboard or else
1299          * it will say SERVER_STARTING forever and ever
1300          */
1301         ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD, NULL);
1302
1303         /* In case system resources are maxxed out, we don't want
1304            Apache running away with the CPU trying to fork over and
1305            over and over again. */
1306         apr_sleep(apr_time_from_sec(10));
1307
1308         return -1;
1309     }
1310
1311     if (!pid) {
1312 #ifdef HAVE_BINDPROCESSOR
1313         /* By default, AIX binds to a single processor.  This bit unbinds
1314          * children which will then bind to another CPU.
1315          */
1316         int status = bindprocessor(BINDPROCESS, (int)getpid(),
1317                                PROCESSOR_CLASS_ANY);
1318         if (status != OK)
1319             ap_log_error(APLOG_MARK, APLOG_WARNING, errno, 
1320                          ap_server_conf,
1321                          "processor unbind failed %d", status);
1322 #endif
1323         RAISE_SIGSTOP(MAKE_CHILD);
1324
1325         apr_signal(SIGTERM, just_die);
1326         child_main(slot);
1327
1328         clean_child_exit(0);
1329     }
1330     /* else */
1331     ap_scoreboard_image->parent[slot].quiescing = 0;
1332     ap_scoreboard_image->parent[slot].pid = pid;
1333     return 0;
1334 }
1335
1336 /* start up a bunch of children */
1337 static void startup_children(int number_to_start)
1338 {
1339     int i;
1340
1341     for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
1342         if (ap_scoreboard_image->parent[i].pid != 0) {
1343             continue;
1344         }
1345         if (make_child(ap_server_conf, i) < 0) {
1346             break;
1347         }
1348         --number_to_start;
1349     }
1350 }
1351
1352
1353 /*
1354  * idle_spawn_rate is the number of children that will be spawned on the
1355  * next maintenance cycle if there aren't enough idle servers.  It is
1356  * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
1357  * without the need to spawn.
1358  */
1359 static int idle_spawn_rate = 1;
1360 #ifndef MAX_SPAWN_RATE
1361 #define MAX_SPAWN_RATE        (32)
1362 #endif
1363 static int hold_off_on_exponential_spawning;
1364
1365 static void perform_idle_server_maintenance(void)
1366 {
1367     int i, j;
1368     int idle_thread_count;
1369     worker_score *ws;
1370     process_score *ps;
1371     int free_length;
1372     int totally_free_length = 0;
1373     int free_slots[MAX_SPAWN_RATE];
1374     int last_non_dead;
1375     int total_non_dead;
1376
1377     /* initialize the free_list */
1378     free_length = 0;
1379
1380     idle_thread_count = 0;
1381     last_non_dead = -1;
1382     total_non_dead = 0;
1383
1384     for (i = 0; i < ap_daemons_limit; ++i) {
1385         /* Initialization to satisfy the compiler. It doesn't know
1386          * that ap_threads_per_child is always > 0 */
1387         int status = SERVER_DEAD;
1388         int any_dying_threads = 0;
1389         int any_dead_threads = 0;
1390         int all_dead_threads = 1;
1391
1392         if (i >= ap_max_daemons_limit && totally_free_length == idle_spawn_rate)
1393             break;
1394         ps = &ap_scoreboard_image->parent[i];
1395         for (j = 0; j < ap_threads_per_child; j++) {
1396             ws = &ap_scoreboard_image->servers[i][j];
1397             status = ws->status;
1398
1399             /* XXX any_dying_threads is probably no longer needed    GLA */
1400             any_dying_threads = any_dying_threads || 
1401                                 (status == SERVER_GRACEFUL);
1402             any_dead_threads = any_dead_threads || (status == SERVER_DEAD);
1403             all_dead_threads = all_dead_threads &&
1404                                    (status == SERVER_DEAD ||
1405                                     status == SERVER_GRACEFUL);
1406
1407             /* We consider a starting server as idle because we started it
1408              * at least a cycle ago, and if it still hasn't finished starting
1409              * then we're just going to swamp things worse by forking more.
1410              * So we hopefully won't need to fork more if we count it.
1411              * This depends on the ordering of SERVER_READY and SERVER_STARTING.
1412              */
1413             if (status <= SERVER_READY && status != SERVER_DEAD &&
1414                     !ps->quiescing &&
1415                     ps->generation == ap_my_generation &&
1416                  /* XXX the following shouldn't be necessary if we clean up 
1417                   *     properly after seg faults, but we're not yet    GLA 
1418                   */     
1419                     ps->pid != 0) {
1420                 ++idle_thread_count;
1421             }
1422         }
1423         if (any_dead_threads && totally_free_length < idle_spawn_rate 
1424                 && (!ps->pid               /* no process in the slot */
1425                     || ps->quiescing)) {   /* or at least one is going away */
1426             if (all_dead_threads) {
1427                 /* great! we prefer these, because the new process can
1428                  * start more threads sooner.  So prioritize this slot 
1429                  * by putting it ahead of any slots with active threads.
1430                  *
1431                  * first, make room by moving a slot that's potentially still
1432                  * in use to the end of the array
1433                  */
1434                 free_slots[free_length] = free_slots[totally_free_length];
1435                 free_slots[totally_free_length++] = i;
1436             }
1437             else {
1438                 /* slot is still in use - back of the bus
1439                  */
1440             free_slots[free_length] = i;
1441             }
1442             ++free_length;
1443         }
1444         /* XXX if (!ps->quiescing)     is probably more reliable  GLA */
1445         if (!any_dying_threads) {
1446             last_non_dead = i;
1447             ++total_non_dead;
1448         }
1449     }
1450     ap_max_daemons_limit = last_non_dead + 1;
1451
1452     if (idle_thread_count > max_spare_threads) {
1453         /* Kill off one child */
1454         ap_mpm_pod_signal(pod, TRUE);
1455         idle_spawn_rate = 1;
1456     }
1457     else if (idle_thread_count < min_spare_threads) {
1458         /* terminate the free list */
1459         if (free_length == 0) {
1460             /* only report this condition once */
1461             static int reported = 0;
1462             
1463             if (!reported) {
1464                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, 
1465                              ap_server_conf,
1466                              "server reached MaxClients setting, consider"
1467                              " raising the MaxClients setting");
1468                 reported = 1;
1469             }
1470             idle_spawn_rate = 1;
1471         }
1472         else {
1473             if (free_length > idle_spawn_rate) {
1474                 free_length = idle_spawn_rate;
1475             }
1476             if (idle_spawn_rate >= 8) {
1477                 ap_log_error(APLOG_MARK, APLOG_INFO, 0, 
1478                              ap_server_conf,
1479                              "server seems busy, (you may need "
1480                              "to increase StartServers, ThreadsPerChild "
1481                              "or Min/MaxSpareThreads), "
1482                              "spawning %d children, there are around %d idle "
1483                              "threads, and %d total children", free_length,
1484                              idle_thread_count, total_non_dead);
1485             }
1486             for (i = 0; i < free_length; ++i) {
1487                 make_child(ap_server_conf, free_slots[i]);
1488             }
1489             /* the next time around we want to spawn twice as many if this
1490              * wasn't good enough, but not if we've just done a graceful
1491              */
1492             if (hold_off_on_exponential_spawning) {
1493                 --hold_off_on_exponential_spawning;
1494             }
1495             else if (idle_spawn_rate < MAX_SPAWN_RATE) {
1496                 idle_spawn_rate *= 2;
1497             }
1498         }
1499     }
1500     else {
1501       idle_spawn_rate = 1;
1502     }
1503 }
1504
1505 static void server_main_loop(int remaining_children_to_start)
1506 {
1507     int child_slot;
1508     apr_exit_why_e exitwhy;
1509     int status, processed_status;
1510     apr_proc_t pid;
1511     int i;
1512
1513     while (!restart_pending && !shutdown_pending) {
1514         ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
1515         
1516         if (pid.pid != -1) {
1517             processed_status = ap_process_child_status(&pid, exitwhy, status);
1518             if (processed_status == APEXIT_CHILDFATAL) {
1519                 shutdown_pending = 1;
1520                 child_fatal = 1;
1521                 return;
1522             }
1523             /* non-fatal death... note that it's gone in the scoreboard. */
1524             child_slot = find_child_by_pid(&pid);
1525             if (child_slot >= 0) {
1526                 for (i = 0; i < ap_threads_per_child; i++)
1527                     ap_update_child_status_from_indexes(child_slot, i, SERVER_DEAD, 
1528                                                         (request_rec *) NULL);
1529                 
1530                 ap_scoreboard_image->parent[child_slot].pid = 0;
1531                 ap_scoreboard_image->parent[child_slot].quiescing = 0;
1532                 if (processed_status == APEXIT_CHILDSICK) {
1533                     /* resource shortage, minimize the fork rate */
1534                     idle_spawn_rate = 1;
1535                 }
1536                 else if (remaining_children_to_start
1537                     && child_slot < ap_daemons_limit) {
1538                     /* we're still doing a 1-for-1 replacement of dead
1539                      * children with new children
1540                      */
1541                     make_child(ap_server_conf, child_slot);
1542                     --remaining_children_to_start;
1543                 }
1544 #if APR_HAS_OTHER_CHILD
1545             }
1546             else if (apr_proc_other_child_read(&pid, status) == 0) {
1547                 /* handled */
1548 #endif
1549             }
1550             else if (is_graceful) {
1551                 /* Great, we've probably just lost a slot in the
1552                  * scoreboard.  Somehow we don't know about this child.
1553                  */
1554                 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1555                              ap_server_conf,
1556                              "long lost child came home! (pid %ld)",
1557                              (long)pid.pid);
1558             }
1559             /* Don't perform idle maintenance when a child dies,
1560              * only do it when there's a timeout.  Remember only a
1561              * finite number of children can die, and it's pretty
1562              * pathological for a lot to die suddenly.
1563              */
1564             continue;
1565         }
1566         else if (remaining_children_to_start) {
1567             /* we hit a 1 second timeout in which none of the previous
1568              * generation of children needed to be reaped... so assume
1569              * they're all done, and pick up the slack if any is left.
1570              */
1571             startup_children(remaining_children_to_start);
1572             remaining_children_to_start = 0;
1573             /* In any event we really shouldn't do the code below because
1574              * few of the servers we just started are in the IDLE state
1575              * yet, so we'd mistakenly create an extra server.
1576              */
1577             continue;
1578         }
1579
1580         perform_idle_server_maintenance();
1581     }
1582 }
1583
1584 int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
1585 {
1586     int remaining_children_to_start;
1587     apr_status_t rv;
1588
1589     ap_log_pid(pconf, ap_pid_fname);
1590
1591     first_server_limit = server_limit;
1592     first_thread_limit = thread_limit;
1593     if (changed_limit_at_restart) {
1594         ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
1595                      "WARNING: Attempt to change ServerLimit or ThreadLimit "
1596                      "ignored during restart");
1597         changed_limit_at_restart = 0;
1598     }
1599     
1600     /* Initialize cross-process accept lock */
1601     ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
1602                                  ap_server_root_relative(_pconf, ap_lock_fname),
1603                                  ap_my_pid);
1604
1605     rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname, 
1606                                ap_accept_lock_mech, _pconf);
1607     if (rv != APR_SUCCESS) {
1608         ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1609                      "Couldn't create accept lock");
1610         return 1;
1611     }
1612
1613 #if APR_USE_SYSVSEM_SERIALIZE
1614     if (ap_accept_lock_mech == APR_LOCK_DEFAULT || 
1615         ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1616 #else
1617     if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1618 #endif
1619         rv = unixd_set_proc_mutex_perms(accept_mutex);
1620         if (rv != APR_SUCCESS) {
1621             ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1622                          "Couldn't set permissions on cross-process lock; "
1623                          "check User and Group directives");
1624             return 1;
1625         }
1626     }
1627
1628     if (!is_graceful) {
1629         if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
1630             return 1;
1631         }
1632         /* fix the generation number in the global score; we just got a new,
1633          * cleared scoreboard
1634          */
1635         ap_scoreboard_image->global->running_generation = ap_my_generation;
1636     }
1637
1638     set_signals();
1639     /* Don't thrash... */
1640     if (max_spare_threads < min_spare_threads + ap_threads_per_child)
1641         max_spare_threads = min_spare_threads + ap_threads_per_child;
1642
1643     /* If we're doing a graceful_restart then we're going to see a lot
1644      * of children exiting immediately when we get into the main loop
1645      * below (because we just sent them AP_SIG_GRACEFUL).  This happens pretty
1646      * rapidly... and for each one that exits we'll start a new one until
1647      * we reach at least daemons_min_free.  But we may be permitted to
1648      * start more than that, so we'll just keep track of how many we're
1649      * supposed to start up without the 1 second penalty between each fork.
1650      */
1651     remaining_children_to_start = ap_daemons_to_start;
1652     if (remaining_children_to_start > ap_daemons_limit) {
1653         remaining_children_to_start = ap_daemons_limit;
1654     }
1655     if (!is_graceful) {
1656         startup_children(remaining_children_to_start);
1657         remaining_children_to_start = 0;
1658     }
1659     else {
1660         /* give the system some time to recover before kicking into
1661             * exponential mode */
1662         hold_off_on_exponential_spawning = 10;
1663     }
1664
1665     ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1666                 "%s configured -- resuming normal operations",
1667                 ap_get_server_version());
1668     ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
1669                 "Server built: %s", ap_get_server_built());
1670 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
1671     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1672                 "AcceptMutex: %s (default: %s)",
1673                 apr_proc_mutex_name(accept_mutex),
1674                 apr_proc_mutex_defname());
1675 #endif
1676     restart_pending = shutdown_pending = 0;
1677
1678     server_main_loop(remaining_children_to_start);
1679
1680     if (shutdown_pending) {
1681         /* Time to gracefully shut down:
1682          * Kill child processes, tell them to call child_exit, etc...
1683          * (By "gracefully" we don't mean graceful in the same sense as 
1684          * "apachectl graceful" where we allow old connections to finish.)
1685          */
1686         ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
1687         ap_reclaim_child_processes(1);                /* Start with SIGTERM */
1688
1689         if (!child_fatal) {
1690             /* cleanup pid file on normal shutdown */
1691             const char *pidfile = NULL;
1692             pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1693             if ( pidfile != NULL && unlink(pidfile) == 0)
1694                 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1695                              ap_server_conf,
1696                              "removed PID file %s (pid=%ld)",
1697                              pidfile, (long)getpid());
1698     
1699             ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
1700                          ap_server_conf, "caught SIGTERM, shutting down");
1701         }
1702         return 1;
1703     }
1704
1705     /* we've been told to restart */
1706     apr_signal(SIGHUP, SIG_IGN);
1707
1708     if (one_process) {
1709         /* not worth thinking about */
1710         return 1;
1711     }
1712
1713     /* advance to the next generation */
1714     /* XXX: we really need to make sure this new generation number isn't in
1715      * use by any of the children.
1716      */
1717     ++ap_my_generation;
1718     ap_scoreboard_image->global->running_generation = ap_my_generation;
1719     
1720     if (is_graceful) {
1721         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1722                      AP_SIG_GRACEFUL_STRING " received.  Doing graceful restart");
1723         /* wake up the children...time to die.  But we'll have more soon */
1724         ap_mpm_pod_killpg(pod, ap_daemons_limit, TRUE);
1725     
1726
1727         /* This is mostly for debugging... so that we know what is still
1728          * gracefully dealing with existing request.
1729          */
1730         
1731     }
1732     else {
1733         /* Kill 'em all.  Since the child acts the same on the parents SIGTERM 
1734          * and a SIGHUP, we may as well use the same signal, because some user
1735          * pthreads are stealing signals from us left and right.
1736          */
1737         ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
1738
1739         ap_reclaim_child_processes(1);                /* Start with SIGTERM */
1740         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1741                     "SIGHUP received.  Attempting to restart");
1742     }
1743
1744     return 0;
1745 }
1746
1747 /* This really should be a post_config hook, but the error log is already
1748  * redirected by that point, so we need to do this in the open_logs phase.
1749  */
1750 static int worker_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
1751 {
1752     apr_status_t rv;
1753
1754     pconf = p;
1755     ap_server_conf = s;
1756
1757     if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
1758         ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0,
1759                      NULL, "no listening sockets available, shutting down");
1760         return DONE;
1761     }
1762
1763     if (!one_process) {
1764         if ((rv = ap_mpm_pod_open(pconf, &pod))) {
1765             ap_log_error(APLOG_MARK, APLOG_CRIT|APLOG_STARTUP, rv, NULL,
1766                     "Could not open pipe-of-death.");
1767             return DONE;
1768         }
1769     }
1770     return OK;
1771 }
1772
1773 static int worker_pre_config(apr_pool_t *pconf, apr_pool_t *plog, 
1774                              apr_pool_t *ptemp)
1775 {
1776     static int restart_num = 0;
1777     int no_detach, debug, foreground;
1778     ap_directive_t *pdir;
1779     ap_directive_t *max_clients = NULL;
1780     apr_status_t rv;
1781
1782     /* make sure that "ThreadsPerChild" gets set before "MaxClients" */
1783     for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next) {
1784         if (strncasecmp(pdir->directive, "ThreadsPerChild", 15) == 0) {
1785             if (!max_clients) {
1786                 break; /* we're in the clear, got ThreadsPerChild first */
1787             }
1788             else {
1789                 /* now to swap the data */
1790                 ap_directive_t temp;
1791
1792                 temp.directive = pdir->directive;
1793                 temp.args = pdir->args;
1794                 /* Make sure you don't change 'next', or you may get loops! */
1795                 /* XXX: first_child, parent, and data can never be set
1796                  * for these directives, right? -aaron */
1797                 temp.filename = pdir->filename;
1798                 temp.line_num = pdir->line_num;
1799
1800                 pdir->directive = max_clients->directive;
1801                 pdir->args = max_clients->args;
1802                 pdir->filename = max_clients->filename;
1803                 pdir->line_num = max_clients->line_num;
1804                 
1805                 max_clients->directive = temp.directive;
1806                 max_clients->args = temp.args;
1807                 max_clients->filename = temp.filename;
1808                 max_clients->line_num = temp.line_num;
1809                 break;
1810             }
1811         }
1812         else if (!max_clients
1813                  && strncasecmp(pdir->directive, "MaxClients", 10) == 0) {
1814             max_clients = pdir;
1815         }
1816     }
1817
1818     debug = ap_exists_config_define("DEBUG");
1819
1820     if (debug) {
1821         foreground = one_process = 1;
1822         no_detach = 0;
1823     }
1824     else {
1825         one_process = ap_exists_config_define("ONE_PROCESS");
1826         no_detach = ap_exists_config_define("NO_DETACH");
1827         foreground = ap_exists_config_define("FOREGROUND");
1828     }
1829
1830     /* sigh, want this only the second time around */
1831     if (restart_num++ == 1) {
1832         is_graceful = 0;
1833
1834         if (!one_process && !foreground) {
1835             rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
1836                                            : APR_PROC_DETACH_DAEMONIZE);
1837             if (rv != APR_SUCCESS) {
1838                 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
1839                              "apr_proc_detach failed");
1840                 return HTTP_INTERNAL_SERVER_ERROR;
1841             }
1842         }
1843         parent_pid = ap_my_pid = getpid();
1844     }
1845
1846     unixd_pre_config(ptemp);
1847     ap_listen_pre_config();
1848     ap_daemons_to_start = DEFAULT_START_DAEMON;
1849     min_spare_threads = DEFAULT_MIN_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1850     max_spare_threads = DEFAULT_MAX_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1851     ap_daemons_limit = server_limit;
1852     ap_threads_per_child = DEFAULT_THREADS_PER_CHILD;
1853     ap_pid_fname = DEFAULT_PIDLOG;
1854     ap_lock_fname = DEFAULT_LOCKFILE;
1855     ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
1856     ap_extended_status = 0;
1857
1858     apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1859
1860     return OK;
1861 }
1862
1863 static void worker_hooks(apr_pool_t *p)
1864 {
1865     /* The worker open_logs phase must run before the core's, or stderr
1866      * will be redirected to a file, and the messages won't print to the
1867      * console.
1868      */
1869     static const char *const aszSucc[] = {"core.c", NULL};
1870     one_process = 0;
1871
1872     ap_hook_open_logs(worker_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
1873     ap_hook_pre_config(worker_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1874 }
1875
1876 static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy,
1877                                         const char *arg) 
1878 {
1879     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1880     if (err != NULL) {
1881         return err;
1882     }
1883
1884     ap_daemons_to_start = atoi(arg);
1885     return NULL;
1886 }
1887
1888 static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy,
1889                                          const char *arg)
1890 {
1891     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1892     if (err != NULL) {
1893         return err;
1894     }
1895
1896     min_spare_threads = atoi(arg);
1897     if (min_spare_threads <= 0) {
1898        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1899                     "WARNING: detected MinSpareThreads set to non-positive.");
1900        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1901                     "Resetting to 1 to avoid almost certain Apache failure.");
1902        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1903                     "Please read the documentation.");
1904        min_spare_threads = 1;
1905     }
1906        
1907     return NULL;
1908 }
1909
1910 static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy,
1911                                          const char *arg)
1912 {
1913     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1914     if (err != NULL) {
1915         return err;
1916     }
1917
1918     max_spare_threads = atoi(arg);
1919     return NULL;
1920 }
1921
1922 static const char *set_max_clients (cmd_parms *cmd, void *dummy,
1923                                      const char *arg) 
1924 {
1925     int max_clients;
1926     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1927     if (err != NULL) {
1928         return err;
1929     }
1930
1931     /* It is ok to use ap_threads_per_child here because we are
1932      * sure that it gets set before MaxClients in the pre_config stage. */
1933     max_clients = atoi(arg);
1934     if (max_clients < ap_threads_per_child) {
1935        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1936                     "WARNING: MaxClients (%d) must be at least as large",
1937                     max_clients);
1938        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1939                     " as ThreadsPerChild (%d). Automatically",
1940                     ap_threads_per_child);
1941        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1942                     " increasing MaxClients to %d.",
1943                     ap_threads_per_child);
1944        max_clients = ap_threads_per_child;
1945     }
1946     ap_daemons_limit = max_clients / ap_threads_per_child;
1947     if ((max_clients > 0) && (max_clients % ap_threads_per_child)) {
1948        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1949                     "WARNING: MaxClients (%d) is not an integer multiple",
1950                     max_clients);
1951        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1952                     " of ThreadsPerChild (%d), lowering MaxClients to %d",
1953                     ap_threads_per_child,
1954                     ap_daemons_limit * ap_threads_per_child);
1955        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1956                     " for a maximum of %d child processes,",
1957                     ap_daemons_limit);
1958        max_clients = ap_daemons_limit * ap_threads_per_child; 
1959     }
1960     if (ap_daemons_limit > server_limit) {
1961        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1962                     "WARNING: MaxClients of %d would require %d servers,",
1963                     max_clients, ap_daemons_limit);
1964        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1965                     " and would exceed the ServerLimit value of %d.",
1966                     server_limit);
1967        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1968                     " Automatically lowering MaxClients to %d.  To increase,",
1969                     server_limit);
1970        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1971                     " please see the ServerLimit directive.");
1972        ap_daemons_limit = server_limit;
1973     } 
1974     else if (ap_daemons_limit < 1) {
1975         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1976                      "WARNING: Require MaxClients > 0, setting to 1");
1977         ap_daemons_limit = 1;
1978     }
1979     return NULL;
1980 }
1981
1982 static const char *set_threads_per_child (cmd_parms *cmd, void *dummy,
1983                                           const char *arg) 
1984 {
1985     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1986     if (err != NULL) {
1987         return err;
1988     }
1989
1990     ap_threads_per_child = atoi(arg);
1991     if (ap_threads_per_child > thread_limit) {
1992         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1993                      "WARNING: ThreadsPerChild of %d exceeds ThreadLimit "
1994                      "value of %d", ap_threads_per_child,
1995                      thread_limit);
1996         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
1997                      "threads, lowering ThreadsPerChild to %d. To increase, please"
1998                      " see the", thread_limit);
1999         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2000                      " ThreadLimit directive.");
2001         ap_threads_per_child = thread_limit;
2002     }
2003     else if (ap_threads_per_child < 1) {
2004         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2005                      "WARNING: Require ThreadsPerChild > 0, setting to 1");
2006         ap_threads_per_child = 1;
2007     }
2008     return NULL;
2009 }
2010
2011 static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg) 
2012 {
2013     int tmp_server_limit;
2014     
2015     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2016     if (err != NULL) {
2017         return err;
2018     }
2019
2020     tmp_server_limit = atoi(arg);
2021     /* you cannot change ServerLimit across a restart; ignore
2022      * any such attempts
2023      */
2024     if (first_server_limit &&
2025         tmp_server_limit != server_limit) {
2026         /* how do we log a message?  the error log is a bit bucket at this
2027          * point; we'll just have to set a flag so that ap_mpm_run()
2028          * logs a warning later
2029          */
2030         changed_limit_at_restart = 1;
2031         return NULL;
2032     }
2033     server_limit = tmp_server_limit;
2034     
2035     if (server_limit > MAX_SERVER_LIMIT) {
2036        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2037                     "WARNING: ServerLimit of %d exceeds compile time limit "
2038                     "of %d servers,", server_limit, MAX_SERVER_LIMIT);
2039        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2040                     " lowering ServerLimit to %d.", MAX_SERVER_LIMIT);
2041        server_limit = MAX_SERVER_LIMIT;
2042     } 
2043     else if (server_limit < 1) {
2044         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2045                      "WARNING: Require ServerLimit > 0, setting to 1");
2046         server_limit = 1;
2047     }
2048     return NULL;
2049 }
2050
2051 static const char *set_thread_limit (cmd_parms *cmd, void *dummy, const char *arg) 
2052 {
2053     int tmp_thread_limit;
2054     
2055     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2056     if (err != NULL) {
2057         return err;
2058     }
2059
2060     tmp_thread_limit = atoi(arg);
2061     /* you cannot change ThreadLimit across a restart; ignore
2062      * any such attempts
2063      */
2064     if (first_thread_limit &&
2065         tmp_thread_limit != thread_limit) {
2066         /* how do we log a message?  the error log is a bit bucket at this
2067          * point; we'll just have to set a flag so that ap_mpm_run()
2068          * logs a warning later
2069          */
2070         changed_limit_at_restart = 1;
2071         return NULL;
2072     }
2073     thread_limit = tmp_thread_limit;
2074     
2075     if (thread_limit > MAX_THREAD_LIMIT) {
2076        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2077                     "WARNING: ThreadLimit of %d exceeds compile time limit "
2078                     "of %d servers,", thread_limit, MAX_THREAD_LIMIT);
2079        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2080                     " lowering ThreadLimit to %d.", MAX_THREAD_LIMIT);
2081        thread_limit = MAX_THREAD_LIMIT;
2082     } 
2083     else if (thread_limit < 1) {
2084         ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2085                      "WARNING: Require ThreadLimit > 0, setting to 1");
2086         thread_limit = 1;
2087     }
2088     return NULL;
2089 }
2090
2091 static const command_rec worker_cmds[] = {
2092 UNIX_DAEMON_COMMANDS,
2093 LISTEN_COMMANDS,
2094 AP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
2095   "Number of child processes launched at server startup"),
2096 AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
2097   "Minimum number of idle threads, to handle request spikes"),
2098 AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
2099   "Maximum number of idle threads"),
2100 AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
2101   "Maximum number of threads alive at the same time"),
2102 AP_INIT_TAKE1("ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF,
2103   "Number of threads each child creates"),
2104 AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
2105   "Maximum number of child processes for this run of Apache"),
2106 AP_INIT_TAKE1("ThreadLimit", set_thread_limit, NULL, RSRC_CONF,
2107   "Maximum number of worker threads per child process for this run of Apache - Upper limit for ThreadsPerChild"),
2108 { NULL }
2109 };
2110
2111 module AP_MODULE_DECLARE_DATA mpm_worker_module = {
2112     MPM20_MODULE_STUFF,
2113     ap_mpm_rewrite_args,        /* hook to run before apache parses args */
2114     NULL,                       /* create per-directory config structure */
2115     NULL,                       /* merge per-directory config structures */
2116     NULL,                       /* create per-server config structure */
2117     NULL,                       /* merge per-server config structures */
2118     worker_cmds,                /* command apr_table_t */
2119     worker_hooks                /* register_hooks */
2120 };
2121