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