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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
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"
33 #define APR_WANT_STRFUNC
39 #if APR_HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
42 #if APR_HAVE_SYS_WAIT_H
45 #ifdef HAVE_SYS_PROCESSOR_H
46 #include <sys/processor.h> /* for bindprocessor() */
50 #error The Worker MPM requires APR threads, but they are unavailable.
55 #include "ap_config.h"
57 #include "http_main.h"
59 #include "http_config.h" /* for read_config */
60 #include "http_core.h" /* for get_remote_host */
61 #include "http_connection.h"
64 #include "mpm_common.h"
65 #include "ap_listen.h"
66 #include "scoreboard.h"
68 #include "mpm_default.h"
71 #include <limits.h> /* for INT_MAX */
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.
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
84 #ifndef DEFAULT_SERVER_LIMIT
85 #define DEFAULT_SERVER_LIMIT 16
88 /* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
89 * some sort of compile-time limit to help catch typos.
91 #ifndef MAX_SERVER_LIMIT
92 #define MAX_SERVER_LIMIT 20000
95 /* Limit on the threads per process. Clients will be locked out if more than
96 * this * server_limit are needed.
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
102 #ifndef DEFAULT_THREAD_LIMIT
103 #define DEFAULT_THREAD_LIMIT 64
106 /* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT. We want
107 * some sort of compile-time limit to help catch typos.
109 #ifndef MAX_THREAD_LIMIT
110 #define MAX_THREAD_LIMIT 20000
114 * Actual definitions of config globals
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;
139 /* The structure used to pass unique initialization info to each thread */
146 /* Structure used to pass information to the thread responsible for
147 * creating the rest of the threads.
150 apr_thread_t **threads;
151 apr_thread_t *listener;
153 apr_threadattr_t *threadattr;
156 #define ID_FROM_CHILD_THREAD(c, t) ((c * thread_limit) + t)
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
164 int ap_max_daemons_limit = -1;
166 static ap_pod_t *pod;
168 /* *Non*-shared http_main globals... */
170 server_rec *ap_server_conf;
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.).
185 static int one_process = 0;
188 int raise_sigstop_flags;
191 static apr_pool_t *pconf; /* Pool for config stuff */
192 static apr_pool_t *pchild; /* Pool for httpd child stuff */
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;
199 /* Locks for accept serialization */
200 static apr_proc_mutex_t *accept_mutex;
202 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
203 #define SAFE_ACCEPT(stmt) (ap_listeners->next ? (stmt) : APR_SUCCESS)
205 #define SAFE_ACCEPT(stmt) (stmt)
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.
214 #define LISTENER_SIGNAL SIGHUP
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.
227 #define WORKER_SIGNAL AP_SIG_GRACEFUL
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;
233 static void close_worker_sockets(void)
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;
244 static void wakeup_listener(void)
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
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
260 #ifdef HAVE_PTHREAD_KILL
261 pthread_kill(*listener_os_thread, LISTENER_SIGNAL);
263 kill(ap_my_pid, LISTENER_SIGNAL);
268 #define ST_GRACEFUL 1
269 #define ST_UNGRACEFUL 2
271 static int terminate_mode = ST_INIT;
273 static void signal_threads(int mode)
275 if (terminate_mode == mode) {
278 terminate_mode = mode;
279 mpm_state = AP_MPMQ_STOPPING;
281 /* in case we weren't called from the listener thread, wake up the
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
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 */
298 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
301 case AP_MPMQ_MAX_DAEMON_USED:
302 *result = ap_max_daemons_limit;
304 case AP_MPMQ_IS_THREADED:
305 *result = AP_MPMQ_STATIC;
307 case AP_MPMQ_IS_FORKED:
308 *result = AP_MPMQ_DYNAMIC;
310 case AP_MPMQ_HARD_LIMIT_DAEMONS:
311 *result = server_limit;
313 case AP_MPMQ_HARD_LIMIT_THREADS:
314 *result = thread_limit;
316 case AP_MPMQ_MAX_THREADS:
317 *result = ap_threads_per_child;
319 case AP_MPMQ_MIN_SPARE_DAEMONS:
322 case AP_MPMQ_MIN_SPARE_THREADS:
323 *result = min_spare_threads;
325 case AP_MPMQ_MAX_SPARE_DAEMONS:
328 case AP_MPMQ_MAX_SPARE_THREADS:
329 *result = max_spare_threads;
331 case AP_MPMQ_MAX_REQUESTS_DAEMON:
332 *result = ap_max_requests_per_child;
334 case AP_MPMQ_MAX_DAEMONS:
335 *result = ap_daemons_limit;
337 case AP_MPMQ_MPM_STATE:
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)
348 mpm_state = AP_MPMQ_STOPPING;
350 apr_pool_destroy(pchild);
355 static void just_die(int sig)
360 /*****************************************************************
361 * Connection structures and accounting...
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;
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.
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.
390 static void ap_start_shutdown(int graceful)
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.
400 shutdown_pending = 1;
401 is_graceful = graceful;
404 /* do a graceful restart if graceful == 1 */
405 static void ap_start_restart(int graceful)
407 mpm_state = AP_MPMQ_STOPPING;
408 if (restart_pending == 1) {
409 /* Probably not an error - don't bother reporting it */
413 is_graceful = graceful;
416 static void sig_term(int sig)
418 ap_start_shutdown(sig == AP_SIG_GRACEFUL_STOP);
421 static void restart(int sig)
423 ap_start_restart(sig == AP_SIG_GRACEFUL);
426 static void set_signals(void)
428 #ifndef NO_USE_SIGACTION
433 ap_fatal_signal_setup(ap_server_conf, pconf);
436 #ifndef NO_USE_SIGACTION
437 sigemptyset(&sa.sa_mask);
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 ")");
450 if (sigaction(SIGINT, &sa, NULL) < 0)
451 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
452 "sigaction(SIGINT)");
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)");
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)");
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)");
473 /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
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 ")");
487 apr_signal(SIGXCPU, SIG_DFL);
490 apr_signal(SIGXFSZ, SIG_DFL);
494 apr_signal(SIGTERM, sig_term);
496 apr_signal(SIGHUP, restart);
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 */
505 apr_signal(SIGPIPE, SIG_IGN);
511 /*****************************************************************
512 * Here follows a long bunch of generic server bookkeeping stuff...
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?
520 /* note: for a graceful termination, listener_may_exit will be set before
521 * workers_may_exit, so check listener_may_exit
523 return listener_may_exit;
526 /*****************************************************************
527 * Child process main loop.
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)
533 conn_rec *current_conn;
534 long conn_id = ID_FROM_CHILD_THREAD(my_child_num, my_thread_num);
538 ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
539 apr_os_sock_get(&csd, sock);
541 current_conn = ap_run_create_connection(p, ap_server_conf, sock,
542 conn_id, sbh, bucket_alloc);
544 ap_process_connection(current_conn, sock);
545 ap_lingering_close(current_conn);
549 /* requests_this_child has gone to zero or below. See if the admin coded
550 "MaxRequestsPerChild 0", and keep going in that case. Doing it this way
551 simplifies the hot path in worker_thread */
552 static void check_infinite_requests(void)
554 if (ap_max_requests_per_child) {
555 signal_threads(ST_GRACEFUL);
558 /* wow! if you're executing this code, you may have set a record.
559 * either this child process has served over 2 billion requests, or
560 * you're running a threaded 2.0 on a 16 bit machine.
562 * I'll buy pizza and beers at Apachecon for the first person to do
563 * the former without cheating (dorking with INT_MAX, or running with
564 * uncommitted performance patches, for example).
566 * for the latter case, you probably deserve a beer too. Greg Ames
569 requests_this_child = INT_MAX; /* keep going */
573 static void unblock_signal(int sig)
577 sigemptyset(&sig_mask);
578 sigaddset(&sig_mask, sig);
579 #if defined(SIGPROCMASK_SETS_THREAD_MASK)
580 sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);
582 pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL);
586 static void dummy_signal_handler(int sig)
588 /* XXX If specifying SIG_IGN is guaranteed to unblock a syscall,
589 * then we don't need this goofy function.
593 static void * APR_THREAD_FUNC listener_thread(apr_thread_t *thd, void * dummy)
595 proc_info * ti = dummy;
596 int process_slot = ti->pid;
597 apr_pool_t *tpool = apr_thread_pool_get(thd);
599 apr_pool_t *ptrans = NULL; /* Pool for per-transaction stuff */
600 apr_pollset_t *pollset;
603 int have_idle_worker = 0;
604 int last_poll_idx = 0;
608 /* ### check the status */
609 (void) apr_pollset_create(&pollset, num_listensocks, tpool, 0);
611 for (lr = ap_listeners; lr != NULL; lr = lr->next) {
612 apr_pollfd_t pfd = { 0 };
614 pfd.desc_type = APR_POLL_SOCKET;
616 pfd.reqevents = APR_POLLIN;
617 pfd.client_data = lr;
619 /* ### check the status */
620 (void) apr_pollset_add(pollset, &pfd);
623 /* Unblock the signal used to wake this thread up, and set a handler for
626 unblock_signal(LISTENER_SIGNAL);
627 apr_signal(LISTENER_SIGNAL, dummy_signal_handler);
629 /* TODO: Switch to a system where threads reuse the results from earlier
630 poll calls - manoj */
632 /* TODO: requests_this_child should be synchronized - aaron */
633 if (requests_this_child <= 0) {
634 check_infinite_requests();
636 if (listener_may_exit) break;
638 if (!have_idle_worker) {
639 /* the following pops a recycled ptrans pool off a stack
640 * if there is one, in addition to reserving a worker thread
642 rv = ap_queue_info_wait_for_idler(worker_queue_info,
644 if (APR_STATUS_IS_EOF(rv)) {
645 break; /* we've been signaled to die now */
647 else if (rv != APR_SUCCESS) {
648 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
649 "apr_queue_info_wait failed. Attempting to "
650 " shutdown process gracefully.");
651 signal_threads(ST_GRACEFUL);
654 have_idle_worker = 1;
657 /* We've already decremented the idle worker count inside
658 * ap_queue_info_wait_for_idler. */
660 if ((rv = SAFE_ACCEPT(apr_proc_mutex_lock(accept_mutex)))
662 int level = APLOG_EMERG;
664 if (listener_may_exit) {
667 if (ap_scoreboard_image->parent[process_slot].generation !=
668 ap_scoreboard_image->global->running_generation) {
669 level = APLOG_DEBUG; /* common to get these at restart time */
671 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
672 "apr_proc_mutex_lock failed. Attempting to shutdown "
673 "process gracefully.");
674 signal_threads(ST_GRACEFUL);
675 break; /* skip the lock release */
678 if (!ap_listeners->next) {
679 /* Only one listener, so skip the poll */
683 while (!listener_may_exit) {
685 const apr_pollfd_t *pdesc;
687 rv = apr_pollset_poll(pollset, -1, &numdesc, &pdesc);
688 if (rv != APR_SUCCESS) {
689 if (APR_STATUS_IS_EINTR(rv)) {
693 /* apr_pollset_poll() will only return errors in catastrophic
694 * circumstances. Let's try exiting gracefully, for now. */
695 ap_log_error(APLOG_MARK, APLOG_ERR, rv,
696 (const server_rec *) ap_server_conf,
697 "apr_pollset_poll: (listen)");
698 signal_threads(ST_GRACEFUL);
701 if (listener_may_exit) break;
703 /* We can always use pdesc[0], but sockets at position N
704 * could end up completely starved of attention in a very
705 * busy server. Therefore, we round-robin across the
706 * returned set of descriptors. While it is possible that
707 * the returned set of descriptors might flip around and
708 * continue to starve some sockets, we happen to know the
709 * internal pollset implementation retains ordering
710 * stability of the sockets. Thus, the round-robin should
711 * ensure that a socket will eventually be serviced.
713 if (last_poll_idx >= numdesc)
716 /* Grab a listener record from the client_data of the poll
717 * descriptor, and advance our saved index to round-robin
720 * ### hmm... this descriptor might have POLLERR rather
723 lr = pdesc[last_poll_idx++].client_data;
730 if (!listener_may_exit) {
731 if (ptrans == NULL) {
732 /* we can't use a recycled transaction pool this time.
733 * create a new transaction pool */
734 apr_allocator_t *allocator;
736 apr_allocator_create(&allocator);
737 apr_allocator_max_free_set(allocator, ap_max_mem_free);
738 apr_pool_create_ex(&ptrans, pconf, NULL, allocator);
739 apr_allocator_owner_set(allocator, ptrans);
741 apr_pool_tag(ptrans, "transaction");
742 rv = lr->accept_func(&csd, lr, ptrans);
743 /* later we trash rv and rely on csd to indicate success/failure */
744 AP_DEBUG_ASSERT(rv == APR_SUCCESS || !csd);
746 if (rv == APR_EGENERAL) {
747 /* E[NM]FILE, ENOMEM, etc */
748 resource_shortage = 1;
749 signal_threads(ST_GRACEFUL);
751 if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
753 int level = APLOG_EMERG;
755 if (listener_may_exit) {
758 if (ap_scoreboard_image->parent[process_slot].generation !=
759 ap_scoreboard_image->global->running_generation) {
760 level = APLOG_DEBUG; /* common to get these at restart time */
762 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
763 "apr_proc_mutex_unlock failed. Attempting to "
764 "shutdown process gracefully.");
765 signal_threads(ST_GRACEFUL);
768 rv = ap_queue_push(worker_queue, csd, ptrans);
770 /* trash the connection; we couldn't queue the connected
773 apr_socket_close(csd);
774 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
775 "ap_queue_push failed");
778 have_idle_worker = 0;
783 if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
785 int level = APLOG_EMERG;
787 if (ap_scoreboard_image->parent[process_slot].generation !=
788 ap_scoreboard_image->global->running_generation) {
789 level = APLOG_DEBUG; /* common to get these at restart time */
791 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
792 "apr_proc_mutex_unlock failed. Attempting to "
793 "shutdown process gracefully.");
794 signal_threads(ST_GRACEFUL);
800 ap_close_listeners();
801 ap_queue_term(worker_queue);
803 ap_scoreboard_image->parent[process_slot].quiescing = 1;
805 /* wake up the main thread */
806 kill(ap_my_pid, SIGTERM);
808 apr_thread_exit(thd, APR_SUCCESS);
812 /* XXX For ungraceful termination/restart, we definitely don't want to
813 * wait for active connections to finish but we may want to wait
814 * for idle workers to get out of the queue code and release mutexes,
815 * since those mutexes are cleaned up pretty soon and some systems
816 * may not react favorably (i.e., segfault) if operations are attempted
817 * on cleaned-up mutexes.
819 static void * APR_THREAD_FUNC worker_thread(apr_thread_t *thd, void * dummy)
821 proc_info * ti = dummy;
822 int process_slot = ti->pid;
823 int thread_slot = ti->tid;
824 apr_socket_t *csd = NULL;
825 apr_bucket_alloc_t *bucket_alloc;
826 apr_pool_t *last_ptrans = NULL;
827 apr_pool_t *ptrans; /* Pool for per-transaction stuff */
833 ap_scoreboard_image->servers[process_slot][thread_slot].pid = ap_my_pid;
834 ap_scoreboard_image->servers[process_slot][thread_slot].tid = apr_os_thread_current();
835 ap_scoreboard_image->servers[process_slot][thread_slot].generation = ap_my_generation;
836 ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_STARTING, NULL);
838 #ifdef HAVE_PTHREAD_KILL
839 unblock_signal(WORKER_SIGNAL);
840 apr_signal(WORKER_SIGNAL, dummy_signal_handler);
843 while (!workers_may_exit) {
845 rv = ap_queue_info_set_idle(worker_queue_info, last_ptrans);
847 if (rv != APR_SUCCESS) {
848 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
849 "ap_queue_info_set_idle failed. Attempting to "
850 "shutdown process gracefully.");
851 signal_threads(ST_GRACEFUL);
857 ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_READY, NULL);
859 if (workers_may_exit) {
862 rv = ap_queue_pop(worker_queue, &csd, &ptrans);
864 if (rv != APR_SUCCESS) {
865 /* We get APR_EOF during a graceful shutdown once all the connections
866 * accepted by this server process have been handled.
868 if (APR_STATUS_IS_EOF(rv)) {
871 /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
872 * from an explicit call to ap_queue_interrupt_all(). This allows
873 * us to unblock threads stuck in ap_queue_pop() when a shutdown
876 * If workers_may_exit is set and this is ungraceful termination/
877 * restart, we are bound to get an error on some systems (e.g.,
878 * AIX, which sanity-checks mutex operations) since the queue
879 * may have already been cleaned up. Don't log the "error" if
880 * workers_may_exit is set.
882 else if (APR_STATUS_IS_EINTR(rv)) {
885 /* We got some other error. */
886 else if (!workers_may_exit) {
887 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
888 "ap_queue_pop failed");
893 worker_sockets[thread_slot] = csd;
894 bucket_alloc = apr_bucket_alloc_create(ptrans);
895 process_socket(ptrans, csd, process_slot, thread_slot, bucket_alloc);
896 worker_sockets[thread_slot] = NULL;
897 requests_this_child--; /* FIXME: should be synchronized - aaron */
898 apr_pool_clear(ptrans);
899 last_ptrans = ptrans;
902 ap_update_child_status_from_indexes(process_slot, thread_slot,
903 (dying) ? SERVER_DEAD : SERVER_GRACEFUL, (request_rec *) NULL);
905 apr_thread_exit(thd, APR_SUCCESS);
909 static int check_signal(int signum)
919 static void create_listener_thread(thread_starter *ts)
921 int my_child_num = ts->child_num_arg;
922 apr_threadattr_t *thread_attr = ts->threadattr;
926 my_info = (proc_info *)malloc(sizeof(proc_info));
927 my_info->pid = my_child_num;
928 my_info->tid = -1; /* listener thread doesn't have a thread slot */
930 rv = apr_thread_create(&ts->listener, thread_attr, listener_thread,
932 if (rv != APR_SUCCESS) {
933 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
934 "apr_thread_create: unable to create listener thread");
935 /* let the parent decide how bad this really is */
936 clean_child_exit(APEXIT_CHILDSICK);
938 apr_os_thread_get(&listener_os_thread, ts->listener);
941 /* XXX under some circumstances not understood, children can get stuck
942 * in start_threads forever trying to take over slots which will
943 * never be cleaned up; for now there is an APLOG_DEBUG message issued
944 * every so often when this condition occurs
946 static void * APR_THREAD_FUNC start_threads(apr_thread_t *thd, void *dummy)
948 thread_starter *ts = dummy;
949 apr_thread_t **threads = ts->threads;
950 apr_threadattr_t *thread_attr = ts->threadattr;
951 int child_num_arg = ts->child_num_arg;
952 int my_child_num = child_num_arg;
956 int threads_created = 0;
957 int listener_started = 0;
959 int prev_threads_created;
961 /* We must create the fd queues before we start up the listener
962 * and worker threads. */
963 worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
964 rv = ap_queue_init(worker_queue, ap_threads_per_child, pchild);
965 if (rv != APR_SUCCESS) {
966 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
967 "ap_queue_init() failed");
968 clean_child_exit(APEXIT_CHILDFATAL);
971 rv = ap_queue_info_create(&worker_queue_info, pchild,
972 ap_threads_per_child);
973 if (rv != APR_SUCCESS) {
974 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
975 "ap_queue_info_create() failed");
976 clean_child_exit(APEXIT_CHILDFATAL);
979 worker_sockets = apr_pcalloc(pchild, ap_threads_per_child
980 * sizeof(apr_socket_t *));
982 loops = prev_threads_created = 0;
984 /* ap_threads_per_child does not include the listener thread */
985 for (i = 0; i < ap_threads_per_child; i++) {
986 int status = ap_scoreboard_image->servers[child_num_arg][i].status;
988 if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
992 my_info = (proc_info *)malloc(sizeof(proc_info));
993 if (my_info == NULL) {
994 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
995 "malloc: out of memory");
996 clean_child_exit(APEXIT_CHILDFATAL);
998 my_info->pid = my_child_num;
1002 /* We are creating threads right now */
1003 ap_update_child_status_from_indexes(my_child_num, i,
1004 SERVER_STARTING, NULL);
1005 /* We let each thread update its own scoreboard entry. This is
1006 * done because it lets us deal with tid better.
1008 rv = apr_thread_create(&threads[i], thread_attr,
1009 worker_thread, my_info, pchild);
1010 if (rv != APR_SUCCESS) {
1011 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1012 "apr_thread_create: unable to create worker thread");
1013 /* let the parent decide how bad this really is */
1014 clean_child_exit(APEXIT_CHILDSICK);
1018 /* Start the listener only when there are workers available */
1019 if (!listener_started && threads_created) {
1020 create_listener_thread(ts);
1021 listener_started = 1;
1023 if (start_thread_may_exit || threads_created == ap_threads_per_child) {
1026 /* wait for previous generation to clean up an entry */
1027 apr_sleep(apr_time_from_sec(1));
1029 if (loops % 120 == 0) { /* every couple of minutes */
1030 if (prev_threads_created == threads_created) {
1031 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1032 "child %" APR_PID_T_FMT " isn't taking over "
1033 "slots very quickly (%d of %d)",
1034 ap_my_pid, threads_created, ap_threads_per_child);
1036 prev_threads_created = threads_created;
1040 /* What state should this child_main process be listed as in the
1042 * ap_update_child_status_from_indexes(my_child_num, i, SERVER_STARTING,
1043 * (request_rec *) NULL);
1045 * This state should be listed separately in the scoreboard, in some kind
1046 * of process_status, not mixed in with the worker threads' status.
1047 * "life_status" is almost right, but it's in the worker's structure, and
1048 * the name could be clearer. gla
1050 apr_thread_exit(thd, APR_SUCCESS);
1054 static void join_workers(apr_thread_t *listener, apr_thread_t **threads)
1057 apr_status_t rv, thread_rv;
1062 /* deal with a rare timing window which affects waking up the
1063 * listener thread... if the signal sent to the listener thread
1064 * is delivered between the time it verifies that the
1065 * listener_may_exit flag is clear and the time it enters a
1066 * blocking syscall, the signal didn't do any good... work around
1067 * that by sleeping briefly and sending it again
1072 #ifdef HAVE_PTHREAD_KILL
1073 pthread_kill(*listener_os_thread, 0)
1078 /* listener not dead yet */
1079 apr_sleep(apr_time_make(0, 500000));
1084 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1085 "the listener thread didn't exit");
1088 rv = apr_thread_join(&thread_rv, listener);
1089 if (rv != APR_SUCCESS) {
1090 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1091 "apr_thread_join: unable to join listener thread");
1096 for (i = 0; i < ap_threads_per_child; i++) {
1097 if (threads[i]) { /* if we ever created this thread */
1098 #ifdef HAVE_PTHREAD_KILL
1099 apr_os_thread_t *worker_os_thread;
1101 apr_os_thread_get(&worker_os_thread, threads[i]);
1102 pthread_kill(*worker_os_thread, WORKER_SIGNAL);
1105 rv = apr_thread_join(&thread_rv, threads[i]);
1106 if (rv != APR_SUCCESS) {
1107 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1108 "apr_thread_join: unable to join worker "
1116 static void join_start_thread(apr_thread_t *start_thread_id)
1118 apr_status_t rv, thread_rv;
1120 start_thread_may_exit = 1; /* tell it to give up in case it is still
1121 * trying to take over slots from a
1122 * previous generation
1124 rv = apr_thread_join(&thread_rv, start_thread_id);
1125 if (rv != APR_SUCCESS) {
1126 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1127 "apr_thread_join: unable to join the start "
1132 static void child_main(int child_num_arg)
1134 apr_thread_t **threads;
1137 apr_threadattr_t *thread_attr;
1138 apr_thread_t *start_thread_id;
1140 mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
1143 ap_my_pid = getpid();
1144 ap_fatal_signal_child_setup(ap_server_conf);
1145 apr_pool_create(&pchild, pconf);
1147 /*stuff to do before we switch id's, so we have permissions.*/
1148 ap_reopen_scoreboard(pchild, NULL, 0);
1150 rv = SAFE_ACCEPT(apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname,
1152 if (rv != APR_SUCCESS) {
1153 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1154 "Couldn't initialize cross-process lock in child");
1155 clean_child_exit(APEXIT_CHILDFATAL);
1158 if (unixd_setup_child()) {
1159 clean_child_exit(APEXIT_CHILDFATAL);
1162 ap_run_child_init(pchild, ap_server_conf);
1164 /* done with init critical section */
1166 /* Just use the standard apr_setup_signal_thread to block all signals
1167 * from being received. The child processes no longer use signals for
1168 * any communication with the parent process.
1170 rv = apr_setup_signal_thread();
1171 if (rv != APR_SUCCESS) {
1172 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1173 "Couldn't initialize signal thread");
1174 clean_child_exit(APEXIT_CHILDFATAL);
1177 if (ap_max_requests_per_child) {
1178 requests_this_child = ap_max_requests_per_child;
1181 /* coding a value of zero means infinity */
1182 requests_this_child = INT_MAX;
1185 /* Setup worker threads */
1187 /* clear the storage; we may not create all our threads immediately,
1188 * and we want a 0 entry to indicate a thread which was not created
1190 threads = (apr_thread_t **)calloc(1,
1191 sizeof(apr_thread_t *) * ap_threads_per_child);
1192 if (threads == NULL) {
1193 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
1194 "malloc: out of memory");
1195 clean_child_exit(APEXIT_CHILDFATAL);
1198 ts = (thread_starter *)apr_palloc(pchild, sizeof(*ts));
1200 apr_threadattr_create(&thread_attr, pchild);
1201 /* 0 means PTHREAD_CREATE_JOINABLE */
1202 apr_threadattr_detach_set(thread_attr, 0);
1204 if (ap_thread_stacksize != 0) {
1205 apr_threadattr_stacksize_set(thread_attr, ap_thread_stacksize);
1208 ts->threads = threads;
1209 ts->listener = NULL;
1210 ts->child_num_arg = child_num_arg;
1211 ts->threadattr = thread_attr;
1213 rv = apr_thread_create(&start_thread_id, thread_attr, start_threads,
1215 if (rv != APR_SUCCESS) {
1216 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1217 "apr_thread_create: unable to create worker thread");
1218 /* let the parent decide how bad this really is */
1219 clean_child_exit(APEXIT_CHILDSICK);
1222 mpm_state = AP_MPMQ_RUNNING;
1224 /* If we are only running in one_process mode, we will want to
1225 * still handle signals. */
1227 /* Block until we get a terminating signal. */
1228 apr_signal_thread(check_signal);
1229 /* make sure the start thread has finished; signal_threads()
1230 * and join_workers() depend on that
1232 /* XXX join_start_thread() won't be awakened if one of our
1233 * threads encounters a critical error and attempts to
1234 * shutdown this child
1236 join_start_thread(start_thread_id);
1237 signal_threads(ST_UNGRACEFUL); /* helps us terminate a little more
1238 * quickly than the dispatch of the signal thread
1239 * beats the Pipe of Death and the browsers
1241 /* A terminating signal was received. Now join each of the
1242 * workers to clean them up.
1243 * If the worker already exited, then the join frees
1244 * their resources and returns.
1245 * If the worker hasn't exited, then this blocks until
1246 * they have (then cleans up).
1248 join_workers(ts->listener, threads);
1250 else { /* !one_process */
1251 /* remove SIGTERM from the set of blocked signals... if one of
1252 * the other threads in the process needs to take us down
1253 * (e.g., for MaxRequestsPerChild) it will send us SIGTERM
1255 unblock_signal(SIGTERM);
1256 apr_signal(SIGTERM, dummy_signal_handler);
1257 /* Watch for any messages from the parent over the POD */
1259 rv = ap_mpm_pod_check(pod);
1260 if (rv == AP_NORESTART) {
1261 /* see if termination was triggered while we slept */
1262 switch(terminate_mode) {
1271 if (rv == AP_GRACEFUL || rv == AP_RESTART) {
1272 /* make sure the start thread has finished;
1273 * signal_threads() and join_workers depend on that
1275 join_start_thread(start_thread_id);
1276 signal_threads(rv == AP_GRACEFUL ? ST_GRACEFUL : ST_UNGRACEFUL);
1281 /* A terminating signal was received. Now join each of the
1282 * workers to clean them up.
1283 * If the worker already exited, then the join frees
1284 * their resources and returns.
1285 * If the worker hasn't exited, then this blocks until
1286 * they have (then cleans up).
1288 join_workers(ts->listener, threads);
1293 clean_child_exit(resource_shortage ? APEXIT_CHILDSICK : 0);
1296 static int make_child(server_rec *s, int slot)
1300 if (slot + 1 > ap_max_daemons_limit) {
1301 ap_max_daemons_limit = slot + 1;
1306 ap_scoreboard_image->parent[slot].pid = getpid();
1310 if ((pid = fork()) == -1) {
1311 ap_log_error(APLOG_MARK, APLOG_ERR, errno, s,
1312 "fork: Unable to fork new process");
1313 /* fork didn't succeed. There's no need to touch the scoreboard;
1314 * if we were trying to replace a failed child process, then
1315 * server_main_loop() marked its workers SERVER_DEAD, and if
1316 * we were trying to replace a child process that exited normally,
1317 * its worker_thread()s left SERVER_DEAD or SERVER_GRACEFUL behind.
1320 /* In case system resources are maxxed out, we don't want
1321 Apache running away with the CPU trying to fork over and
1322 over and over again. */
1323 apr_sleep(apr_time_from_sec(10));
1329 #ifdef HAVE_BINDPROCESSOR
1330 /* By default, AIX binds to a single processor. This bit unbinds
1331 * children which will then bind to another CPU.
1333 int status = bindprocessor(BINDPROCESS, (int)getpid(),
1334 PROCESSOR_CLASS_ANY);
1336 ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
1338 "processor unbind failed %d", status);
1340 RAISE_SIGSTOP(MAKE_CHILD);
1342 apr_signal(SIGTERM, just_die);
1345 clean_child_exit(0);
1348 if (ap_scoreboard_image->parent[slot].pid != 0) {
1349 /* This new child process is squatting on the scoreboard
1350 * entry owned by an exiting child process, which cannot
1351 * exit until all active requests complete.
1352 * Don't forget about this exiting child process, or we
1353 * won't be able to kill it if it doesn't exit by the
1354 * time the server is shut down.
1356 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1357 "taking over scoreboard slot from %" APR_PID_T_FMT "%s",
1358 ap_scoreboard_image->parent[slot].pid,
1359 ap_scoreboard_image->parent[slot].quiescing ?
1360 " (quiescing)" : "");
1361 ap_register_extra_mpm_process(ap_scoreboard_image->parent[slot].pid);
1363 ap_scoreboard_image->parent[slot].quiescing = 0;
1364 ap_scoreboard_image->parent[slot].pid = pid;
1368 /* start up a bunch of children */
1369 static void startup_children(int number_to_start)
1373 for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
1374 if (ap_scoreboard_image->parent[i].pid != 0) {
1377 if (make_child(ap_server_conf, i) < 0) {
1386 * idle_spawn_rate is the number of children that will be spawned on the
1387 * next maintenance cycle if there aren't enough idle servers. It is
1388 * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
1389 * without the need to spawn.
1391 static int idle_spawn_rate = 1;
1392 #ifndef MAX_SPAWN_RATE
1393 #define MAX_SPAWN_RATE (32)
1395 static int hold_off_on_exponential_spawning;
1397 static void perform_idle_server_maintenance(void)
1400 int idle_thread_count;
1404 int totally_free_length = 0;
1405 int free_slots[MAX_SPAWN_RATE];
1408 int active_thread_count = 0;
1410 /* initialize the free_list */
1413 idle_thread_count = 0;
1417 for (i = 0; i < ap_daemons_limit; ++i) {
1418 /* Initialization to satisfy the compiler. It doesn't know
1419 * that ap_threads_per_child is always > 0 */
1420 int status = SERVER_DEAD;
1421 int any_dying_threads = 0;
1422 int any_dead_threads = 0;
1423 int all_dead_threads = 1;
1425 if (i >= ap_max_daemons_limit && totally_free_length == idle_spawn_rate)
1426 /* short cut if all active processes have been examined and
1427 * enough empty scoreboard slots have been found
1430 ps = &ap_scoreboard_image->parent[i];
1431 for (j = 0; j < ap_threads_per_child; j++) {
1432 ws = &ap_scoreboard_image->servers[i][j];
1433 status = ws->status;
1435 /* XXX any_dying_threads is probably no longer needed GLA */
1436 any_dying_threads = any_dying_threads ||
1437 (status == SERVER_GRACEFUL);
1438 any_dead_threads = any_dead_threads || (status == SERVER_DEAD);
1439 all_dead_threads = all_dead_threads &&
1440 (status == SERVER_DEAD ||
1441 status == SERVER_GRACEFUL);
1443 /* We consider a starting server as idle because we started it
1444 * at least a cycle ago, and if it still hasn't finished starting
1445 * then we're just going to swamp things worse by forking more.
1446 * So we hopefully won't need to fork more if we count it.
1447 * This depends on the ordering of SERVER_READY and SERVER_STARTING.
1449 if (ps->pid != 0) { /* XXX just set all_dead_threads in outer for
1450 loop if no pid? not much else matters */
1451 if (status <= SERVER_READY &&
1453 ps->generation == ap_my_generation) {
1454 ++idle_thread_count;
1456 if (status >= SERVER_READY && status < SERVER_GRACEFUL) {
1457 ++active_thread_count;
1461 if (any_dead_threads && totally_free_length < idle_spawn_rate
1462 && free_length < MAX_SPAWN_RATE
1463 && (!ps->pid /* no process in the slot */
1464 || ps->quiescing)) { /* or at least one is going away */
1465 if (all_dead_threads) {
1466 /* great! we prefer these, because the new process can
1467 * start more threads sooner. So prioritize this slot
1468 * by putting it ahead of any slots with active threads.
1470 * first, make room by moving a slot that's potentially still
1471 * in use to the end of the array
1473 free_slots[free_length] = free_slots[totally_free_length];
1474 free_slots[totally_free_length++] = i;
1477 /* slot is still in use - back of the bus
1479 free_slots[free_length] = i;
1483 /* XXX if (!ps->quiescing) is probably more reliable GLA */
1484 if (!any_dying_threads) {
1490 if (sick_child_detected) {
1491 if (active_thread_count > 0) {
1492 /* some child processes appear to be working. don't kill the
1495 sick_child_detected = 0;
1498 /* looks like a basket case. give up.
1500 shutdown_pending = 1;
1502 ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
1504 "No active workers found..."
1505 " Apache is exiting!");
1506 /* the child already logged the failure details */
1511 ap_max_daemons_limit = last_non_dead + 1;
1513 if (idle_thread_count > max_spare_threads) {
1514 /* Kill off one child */
1515 ap_mpm_pod_signal(pod, TRUE);
1516 idle_spawn_rate = 1;
1518 else if (idle_thread_count < min_spare_threads) {
1519 /* terminate the free list */
1520 if (free_length == 0) { /* scoreboard is full, can't fork */
1522 if (active_thread_count >= ap_daemons_limit * ap_threads_per_child) {
1523 static int reported = 0;
1525 /* only report this condition once */
1526 ap_log_error(APLOG_MARK, APLOG_ERR, 0,
1528 "server reached MaxClients setting, consider"
1529 " raising the MaxClients setting");
1534 ap_log_error(APLOG_MARK, APLOG_ERR, 0,
1536 "scoreboard is full, not at MaxClients");
1538 idle_spawn_rate = 1;
1541 if (free_length > idle_spawn_rate) {
1542 free_length = idle_spawn_rate;
1544 if (idle_spawn_rate >= 8) {
1545 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1547 "server seems busy, (you may need "
1548 "to increase StartServers, ThreadsPerChild "
1549 "or Min/MaxSpareThreads), "
1550 "spawning %d children, there are around %d idle "
1551 "threads, and %d total children", free_length,
1552 idle_thread_count, total_non_dead);
1554 for (i = 0; i < free_length; ++i) {
1555 make_child(ap_server_conf, free_slots[i]);
1557 /* the next time around we want to spawn twice as many if this
1558 * wasn't good enough, but not if we've just done a graceful
1560 if (hold_off_on_exponential_spawning) {
1561 --hold_off_on_exponential_spawning;
1563 else if (idle_spawn_rate < MAX_SPAWN_RATE) {
1564 idle_spawn_rate *= 2;
1569 idle_spawn_rate = 1;
1573 static void server_main_loop(int remaining_children_to_start)
1576 apr_exit_why_e exitwhy;
1577 int status, processed_status;
1581 while (!restart_pending && !shutdown_pending) {
1582 ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
1584 if (pid.pid != -1) {
1585 processed_status = ap_process_child_status(&pid, exitwhy, status);
1586 if (processed_status == APEXIT_CHILDFATAL) {
1587 shutdown_pending = 1;
1591 else if (processed_status == APEXIT_CHILDSICK) {
1592 /* tell perform_idle_server_maintenance to check into this
1593 * on the next timer pop
1595 sick_child_detected = 1;
1597 /* non-fatal death... note that it's gone in the scoreboard. */
1598 child_slot = find_child_by_pid(&pid);
1599 if (child_slot >= 0) {
1600 for (i = 0; i < ap_threads_per_child; i++)
1601 ap_update_child_status_from_indexes(child_slot, i, SERVER_DEAD,
1602 (request_rec *) NULL);
1604 ap_scoreboard_image->parent[child_slot].pid = 0;
1605 ap_scoreboard_image->parent[child_slot].quiescing = 0;
1606 if (processed_status == APEXIT_CHILDSICK) {
1607 /* resource shortage, minimize the fork rate */
1608 idle_spawn_rate = 1;
1610 else if (remaining_children_to_start
1611 && child_slot < ap_daemons_limit) {
1612 /* we're still doing a 1-for-1 replacement of dead
1613 * children with new children
1615 make_child(ap_server_conf, child_slot);
1616 --remaining_children_to_start;
1619 else if (ap_unregister_extra_mpm_process(pid.pid) == 1) {
1621 #if APR_HAS_OTHER_CHILD
1623 else if (apr_proc_other_child_alert(&pid, APR_OC_REASON_DEATH,
1628 else if (is_graceful) {
1629 /* Great, we've probably just lost a slot in the
1630 * scoreboard. Somehow we don't know about this child.
1632 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1634 "long lost child came home! (pid %ld)",
1637 /* Don't perform idle maintenance when a child dies,
1638 * only do it when there's a timeout. Remember only a
1639 * finite number of children can die, and it's pretty
1640 * pathological for a lot to die suddenly.
1644 else if (remaining_children_to_start) {
1645 /* we hit a 1 second timeout in which none of the previous
1646 * generation of children needed to be reaped... so assume
1647 * they're all done, and pick up the slack if any is left.
1649 startup_children(remaining_children_to_start);
1650 remaining_children_to_start = 0;
1651 /* In any event we really shouldn't do the code below because
1652 * few of the servers we just started are in the IDLE state
1653 * yet, so we'd mistakenly create an extra server.
1658 perform_idle_server_maintenance();
1662 int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
1664 int remaining_children_to_start;
1667 ap_log_pid(pconf, ap_pid_fname);
1669 /* Initialize cross-process accept lock */
1670 ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
1671 ap_server_root_relative(_pconf, ap_lock_fname),
1674 rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname,
1675 ap_accept_lock_mech, _pconf);
1676 if (rv != APR_SUCCESS) {
1677 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1678 "Couldn't create accept lock");
1679 mpm_state = AP_MPMQ_STOPPING;
1683 #if APR_USE_SYSVSEM_SERIALIZE
1684 if (ap_accept_lock_mech == APR_LOCK_DEFAULT ||
1685 ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1687 if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1689 rv = unixd_set_proc_mutex_perms(accept_mutex);
1690 if (rv != APR_SUCCESS) {
1691 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1692 "Couldn't set permissions on cross-process lock; "
1693 "check User and Group directives");
1694 mpm_state = AP_MPMQ_STOPPING;
1700 if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
1701 mpm_state = AP_MPMQ_STOPPING;
1704 /* fix the generation number in the global score; we just got a new,
1705 * cleared scoreboard
1707 ap_scoreboard_image->global->running_generation = ap_my_generation;
1711 /* Don't thrash... */
1712 if (max_spare_threads < min_spare_threads + ap_threads_per_child)
1713 max_spare_threads = min_spare_threads + ap_threads_per_child;
1715 /* If we're doing a graceful_restart then we're going to see a lot
1716 * of children exiting immediately when we get into the main loop
1717 * below (because we just sent them AP_SIG_GRACEFUL). This happens pretty
1718 * rapidly... and for each one that exits we may start a new one, until
1719 * there are at least min_spare_threads idle threads, counting across
1720 * all children. But we may be permitted to start more children than
1721 * that, so we'll just keep track of how many we're
1722 * supposed to start up without the 1 second penalty between each fork.
1724 remaining_children_to_start = ap_daemons_to_start;
1725 if (remaining_children_to_start > ap_daemons_limit) {
1726 remaining_children_to_start = ap_daemons_limit;
1729 startup_children(remaining_children_to_start);
1730 remaining_children_to_start = 0;
1733 /* give the system some time to recover before kicking into
1734 * exponential mode */
1735 hold_off_on_exponential_spawning = 10;
1738 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1739 "%s configured -- resuming normal operations",
1740 ap_get_server_description());
1741 ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
1742 "Server built: %s", ap_get_server_built());
1743 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
1744 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1745 "AcceptMutex: %s (default: %s)",
1746 apr_proc_mutex_name(accept_mutex),
1747 apr_proc_mutex_defname());
1749 restart_pending = shutdown_pending = 0;
1750 mpm_state = AP_MPMQ_RUNNING;
1752 server_main_loop(remaining_children_to_start);
1753 mpm_state = AP_MPMQ_STOPPING;
1755 if (shutdown_pending && !is_graceful) {
1756 /* Time to shut down:
1757 * Kill child processes, tell them to call child_exit, etc...
1759 ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
1760 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1763 /* cleanup pid file on normal shutdown */
1764 const char *pidfile = NULL;
1765 pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1766 if ( pidfile != NULL && unlink(pidfile) == 0)
1767 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1769 "removed PID file %s (pid=%" APR_PID_T_FMT ")",
1772 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
1773 ap_server_conf, "caught SIGTERM, shutting down");
1776 } else if (shutdown_pending) {
1777 /* Time to gracefully shut down:
1778 * Kill child processes, tell them to call child_exit, etc...
1780 int active_children;
1782 apr_time_t cutoff = 0;
1784 /* Close our listeners, and then ask our children to do same */
1785 ap_close_listeners();
1786 ap_mpm_pod_killpg(pod, ap_daemons_limit, TRUE);
1787 ap_relieve_child_processes();
1790 /* cleanup pid file on normal shutdown */
1791 const char *pidfile = NULL;
1792 pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1793 if ( pidfile != NULL && unlink(pidfile) == 0)
1794 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1796 "removed PID file %s (pid=%" APR_PID_T_FMT ")",
1799 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1800 "caught " AP_SIG_GRACEFUL_STOP_STRING
1801 ", shutting down gracefully");
1804 if (ap_graceful_shutdown_timeout) {
1805 cutoff = apr_time_now() +
1806 apr_time_from_sec(ap_graceful_shutdown_timeout);
1809 /* Don't really exit until each child has finished */
1810 shutdown_pending = 0;
1812 /* Pause for a second */
1813 apr_sleep(apr_time_from_sec(1));
1815 /* Relieve any children which have now exited */
1816 ap_relieve_child_processes();
1818 active_children = 0;
1819 for (index = 0; index < ap_daemons_limit; ++index) {
1820 if (MPM_CHILD_PID(index) != 0) {
1821 if (kill(MPM_CHILD_PID(index), 0) == 0) {
1822 active_children = 1;
1823 /* Having just one child is enough to stay around */
1828 } while (!shutdown_pending && active_children &&
1829 (!ap_graceful_shutdown_timeout || apr_time_now() < cutoff));
1831 /* We might be here because we received SIGTERM, either
1832 * way, try and make sure that all of our processes are
1835 ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
1836 ap_reclaim_child_processes(1);
1841 /* we've been told to restart */
1842 apr_signal(SIGHUP, SIG_IGN);
1845 /* not worth thinking about */
1849 /* advance to the next generation */
1850 /* XXX: we really need to make sure this new generation number isn't in
1851 * use by any of the children.
1854 ap_scoreboard_image->global->running_generation = ap_my_generation;
1857 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1858 AP_SIG_GRACEFUL_STRING " received. Doing graceful restart");
1859 /* wake up the children...time to die. But we'll have more soon */
1860 ap_mpm_pod_killpg(pod, ap_daemons_limit, TRUE);
1863 /* This is mostly for debugging... so that we know what is still
1864 * gracefully dealing with existing request.
1869 /* Kill 'em all. Since the child acts the same on the parents SIGTERM
1870 * and a SIGHUP, we may as well use the same signal, because some user
1871 * pthreads are stealing signals from us left and right.
1873 ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
1875 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1876 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1877 "SIGHUP received. Attempting to restart");
1883 /* This really should be a post_config hook, but the error log is already
1884 * redirected by that point, so we need to do this in the open_logs phase.
1886 static int worker_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
1888 static int restart_num = 0;
1890 int level_flags = 0;
1896 /* the reverse of pre_config, we want this only the first time around */
1897 if (restart_num++ == 0) {
1899 level_flags |= APLOG_STARTUP;
1902 if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
1903 ap_log_error(APLOG_MARK, APLOG_ALERT | level_flags, 0,
1904 (startup ? NULL : s),
1905 "no listening sockets available, shutting down");
1910 if ((rv = ap_mpm_pod_open(pconf, &pod))) {
1911 ap_log_error(APLOG_MARK, APLOG_CRIT | level_flags, rv,
1912 (startup ? NULL : s),
1913 "could not open pipe-of-death");
1920 static int worker_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
1923 static int restart_num = 0;
1924 int no_detach, debug, foreground;
1927 mpm_state = AP_MPMQ_STARTING;
1929 debug = ap_exists_config_define("DEBUG");
1932 foreground = one_process = 1;
1936 one_process = ap_exists_config_define("ONE_PROCESS");
1937 no_detach = ap_exists_config_define("NO_DETACH");
1938 foreground = ap_exists_config_define("FOREGROUND");
1941 /* sigh, want this only the second time around */
1942 if (restart_num++ == 1) {
1945 if (!one_process && !foreground) {
1946 rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
1947 : APR_PROC_DETACH_DAEMONIZE);
1948 if (rv != APR_SUCCESS) {
1949 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
1950 "apr_proc_detach failed");
1951 return HTTP_INTERNAL_SERVER_ERROR;
1954 parent_pid = ap_my_pid = getpid();
1957 unixd_pre_config(ptemp);
1958 ap_listen_pre_config();
1959 ap_daemons_to_start = DEFAULT_START_DAEMON;
1960 min_spare_threads = DEFAULT_MIN_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1961 max_spare_threads = DEFAULT_MAX_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1962 server_limit = DEFAULT_SERVER_LIMIT;
1963 thread_limit = DEFAULT_THREAD_LIMIT;
1964 ap_daemons_limit = server_limit;
1965 ap_threads_per_child = DEFAULT_THREADS_PER_CHILD;
1966 max_clients = ap_daemons_limit * ap_threads_per_child;
1967 ap_pid_fname = DEFAULT_PIDLOG;
1968 ap_lock_fname = DEFAULT_LOCKFILE;
1969 ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
1970 ap_extended_status = 0;
1971 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
1972 ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
1975 apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1980 static int worker_check_config(apr_pool_t *p, apr_pool_t *plog,
1981 apr_pool_t *ptemp, server_rec *s)
1983 static int restart_num = 0;
1987 /* the reverse of pre_config, we want this only the first time around */
1988 if (restart_num++ == 0) {
1992 if (server_limit > MAX_SERVER_LIMIT) {
1994 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
1995 "WARNING: ServerLimit of %d exceeds compile-time "
1996 "limit of", server_limit);
1997 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
1998 " %d servers, decreasing to %d.",
1999 MAX_SERVER_LIMIT, MAX_SERVER_LIMIT);
2001 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2002 "ServerLimit of %d exceeds compile-time limit "
2003 "of %d, decreasing to match",
2004 server_limit, MAX_SERVER_LIMIT);
2006 server_limit = MAX_SERVER_LIMIT;
2008 else if (server_limit < 1) {
2010 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2011 "WARNING: ServerLimit of %d not allowed, "
2012 "increasing to 1.", server_limit);
2014 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2015 "ServerLimit of %d not allowed, increasing to 1",
2021 /* you cannot change ServerLimit across a restart; ignore
2024 if (!first_server_limit) {
2025 first_server_limit = server_limit;
2027 else if (server_limit != first_server_limit) {
2028 /* don't need a startup console version here */
2029 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2030 "changing ServerLimit to %d from original value of %d "
2031 "not allowed during restart",
2032 server_limit, first_server_limit);
2033 server_limit = first_server_limit;
2036 if (thread_limit > MAX_THREAD_LIMIT) {
2038 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2039 "WARNING: ThreadLimit of %d exceeds compile-time "
2040 "limit of", thread_limit);
2041 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2042 " %d threads, decreasing to %d.",
2043 MAX_THREAD_LIMIT, MAX_THREAD_LIMIT);
2045 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2046 "ThreadLimit of %d exceeds compile-time limit "
2047 "of %d, decreasing to match",
2048 thread_limit, MAX_THREAD_LIMIT);
2050 thread_limit = MAX_THREAD_LIMIT;
2052 else if (thread_limit < 1) {
2054 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2055 "WARNING: ThreadLimit of %d not allowed, "
2056 "increasing to 1.", thread_limit);
2058 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2059 "ThreadLimit of %d not allowed, increasing to 1",
2065 /* you cannot change ThreadLimit across a restart; ignore
2068 if (!first_thread_limit) {
2069 first_thread_limit = thread_limit;
2071 else if (thread_limit != first_thread_limit) {
2072 /* don't need a startup console version here */
2073 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2074 "changing ThreadLimit to %d from original value of %d "
2075 "not allowed during restart",
2076 thread_limit, first_thread_limit);
2077 thread_limit = first_thread_limit;
2080 if (ap_threads_per_child > thread_limit) {
2082 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2083 "WARNING: ThreadsPerChild of %d exceeds ThreadLimit "
2084 "of", ap_threads_per_child);
2085 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2086 " %d threads, decreasing to %d.",
2087 thread_limit, thread_limit);
2088 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2089 " To increase, please see the ThreadLimit "
2092 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2093 "ThreadsPerChild of %d exceeds ThreadLimit "
2094 "of %d, decreasing to match",
2095 ap_threads_per_child, thread_limit);
2097 ap_threads_per_child = thread_limit;
2099 else if (ap_threads_per_child < 1) {
2101 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2102 "WARNING: ThreadsPerChild of %d not allowed, "
2103 "increasing to 1.", ap_threads_per_child);
2105 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2106 "ThreadsPerChild of %d not allowed, increasing to 1",
2107 ap_threads_per_child);
2109 ap_threads_per_child = 1;
2112 if (max_clients < ap_threads_per_child) {
2114 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2115 "WARNING: MaxClients of %d is less than "
2116 "ThreadsPerChild of", max_clients);
2117 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2118 " %d, increasing to %d. MaxClients must be at "
2120 ap_threads_per_child, ap_threads_per_child);
2121 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2122 " as the number of threads in a single server.");
2124 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2125 "MaxClients of %d is less than ThreadsPerChild "
2126 "of %d, increasing to match",
2127 max_clients, ap_threads_per_child);
2129 max_clients = ap_threads_per_child;
2132 ap_daemons_limit = max_clients / ap_threads_per_child;
2134 if (max_clients % ap_threads_per_child) {
2135 int tmp_max_clients = ap_daemons_limit * ap_threads_per_child;
2138 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2139 "WARNING: MaxClients of %d is not an integer "
2140 "multiple of", max_clients);
2141 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2142 " ThreadsPerChild of %d, decreasing to nearest "
2143 "multiple %d,", ap_threads_per_child,
2145 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2146 " for a maximum of %d servers.",
2149 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2150 "MaxClients of %d is not an integer multiple of "
2151 "ThreadsPerChild of %d, decreasing to nearest "
2152 "multiple %d", max_clients, ap_threads_per_child,
2155 max_clients = tmp_max_clients;
2158 if (ap_daemons_limit > server_limit) {
2160 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2161 "WARNING: MaxClients of %d would require %d "
2162 "servers and ", max_clients, ap_daemons_limit);
2163 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2164 " would exceed ServerLimit of %d, decreasing to %d.",
2165 server_limit, server_limit * ap_threads_per_child);
2166 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2167 " To increase, please see the ServerLimit "
2170 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2171 "MaxClients of %d would require %d servers and "
2172 "exceed ServerLimit of %d, decreasing to %d",
2173 max_clients, ap_daemons_limit, server_limit,
2174 server_limit * ap_threads_per_child);
2176 ap_daemons_limit = server_limit;
2179 /* ap_daemons_to_start > ap_daemons_limit checked in ap_mpm_run() */
2180 if (ap_daemons_to_start < 0) {
2182 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2183 "WARNING: StartServers of %d not allowed, "
2184 "increasing to 1.", ap_daemons_to_start);
2186 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2187 "StartServers of %d not allowed, increasing to 1",
2188 ap_daemons_to_start);
2190 ap_daemons_to_start = 1;
2193 if (min_spare_threads < 1) {
2195 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2196 "WARNING: MinSpareThreads of %d not allowed, "
2197 "increasing to 1", min_spare_threads);
2198 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2199 " to avoid almost certain server failure.");
2200 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2201 " Please read the documentation.");
2203 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2204 "MinSpareThreads of %d not allowed, increasing to 1",
2207 min_spare_threads = 1;
2210 /* max_spare_threads < min_spare_threads + ap_threads_per_child
2211 * checked in ap_mpm_run()
2217 static void worker_hooks(apr_pool_t *p)
2219 /* Our open_logs hook function must run before the core's, or stderr
2220 * will be redirected to a file, and the messages won't print to the
2223 static const char *const aszSucc[] = {"core.c", NULL};
2226 ap_hook_open_logs(worker_open_logs, NULL, aszSucc, APR_HOOK_REALLY_FIRST);
2227 /* we need to set the MPM state before other pre-config hooks use MPM query
2228 * to retrieve it, so register as REALLY_FIRST
2230 ap_hook_pre_config(worker_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
2231 ap_hook_check_config(worker_check_config, NULL, NULL, APR_HOOK_MIDDLE);
2234 static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy,
2237 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2242 ap_daemons_to_start = atoi(arg);
2246 static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy,
2249 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2254 min_spare_threads = atoi(arg);
2258 static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy,
2261 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2266 max_spare_threads = atoi(arg);
2270 static const char *set_max_clients (cmd_parms *cmd, void *dummy,
2273 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2278 max_clients = atoi(arg);
2282 static const char *set_threads_per_child (cmd_parms *cmd, void *dummy,
2285 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2290 ap_threads_per_child = atoi(arg);
2294 static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
2296 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2301 server_limit = atoi(arg);
2305 static const char *set_thread_limit (cmd_parms *cmd, void *dummy, const char *arg)
2307 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2312 thread_limit = atoi(arg);
2316 static const command_rec worker_cmds[] = {
2317 UNIX_DAEMON_COMMANDS,
2319 AP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
2320 "Number of child processes launched at server startup"),
2321 AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
2322 "Minimum number of idle threads, to handle request spikes"),
2323 AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
2324 "Maximum number of idle threads"),
2325 AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
2326 "Maximum number of threads alive at the same time"),
2327 AP_INIT_TAKE1("ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF,
2328 "Number of threads each child creates"),
2329 AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
2330 "Maximum number of child processes for this run of Apache"),
2331 AP_INIT_TAKE1("ThreadLimit", set_thread_limit, NULL, RSRC_CONF,
2332 "Maximum number of worker threads per child process for this run of Apache - Upper limit for ThreadsPerChild"),
2333 AP_GRACEFUL_SHUTDOWN_TIMEOUT_COMMAND,
2337 module AP_MODULE_DECLARE_DATA mpm_worker_module = {
2339 ap_mpm_rewrite_args, /* hook to run before apache parses args */
2340 NULL, /* create per-directory config structure */
2341 NULL, /* merge per-directory config structures */
2342 NULL, /* create per-server config structure */
2343 NULL, /* merge per-server config structures */
2344 worker_cmds, /* command apr_table_t */
2345 worker_hooks /* register_hooks */