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