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