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.
53 #include "ap_config.h"
55 #include "http_main.h"
57 #include "http_config.h" /* for read_config */
58 #include "http_core.h" /* for get_remote_host */
59 #include "http_connection.h"
62 #include "mpm_common.h"
63 #include "ap_listen.h"
64 #include "scoreboard.h"
66 #include "mpm_default.h"
70 #include <limits.h> /* for INT_MAX */
72 /* Limit on the total --- clients will be locked out if more servers than
73 * this are needed. It is intended solely to keep the server from crashing
74 * when things get out of hand.
76 * We keep a hard maximum number of servers, for two reasons --- first off,
77 * in case something goes seriously wrong, we want to stop the fork bomb
78 * short of actually crashing the machine we're running on by filling some
79 * kernel table. Secondly, it keeps the size of the scoreboard file small
80 * enough that we can read the whole thing without worrying too much about
83 #ifndef DEFAULT_SERVER_LIMIT
84 #define DEFAULT_SERVER_LIMIT 16
87 /* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
88 * some sort of compile-time limit to help catch typos.
90 #ifndef MAX_SERVER_LIMIT
91 #define MAX_SERVER_LIMIT 20000
94 /* Limit on the threads per process. Clients will be locked out if more than
95 * this * server_limit are needed.
97 * We keep this for one reason it keeps the size of the scoreboard file small
98 * enough that we can read the whole thing without worrying too much about
101 #ifndef DEFAULT_THREAD_LIMIT
102 #define DEFAULT_THREAD_LIMIT 64
105 /* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT. We want
106 * some sort of compile-time limit to help catch typos.
108 #ifndef MAX_THREAD_LIMIT
109 #define MAX_THREAD_LIMIT 20000
113 * Actual definitions of config globals
116 static int threads_per_child = 0; /* Worker threads per child */
117 static int ap_daemons_to_start = 0;
118 static int min_spare_threads = 0;
119 static int max_spare_threads = 0;
120 static int ap_daemons_limit = 0;
121 static int max_clients = 0;
122 static int server_limit = 0;
123 static int first_server_limit = 0;
124 static int thread_limit = 0;
125 static int first_thread_limit = 0;
126 static int dying = 0;
127 static int workers_may_exit = 0;
128 static int start_thread_may_exit = 0;
129 static int listener_may_exit = 0;
130 static int requests_this_child;
131 static int num_listensocks = 0;
132 static int resource_shortage = 0;
133 static fd_queue_t *worker_queue;
134 static fd_queue_info_t *worker_queue_info;
135 static int mpm_state = AP_MPMQ_STARTING;
136 static int sick_child_detected;
137 static ap_generation_t volatile my_generation = 0;
139 #define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
141 /* The structure used to pass unique initialization info to each thread */
148 /* Structure used to pass information to the thread responsible for
149 * creating the rest of the threads.
152 apr_thread_t **threads;
153 apr_thread_t *listener;
155 apr_threadattr_t *threadattr;
158 #define ID_FROM_CHILD_THREAD(c, t) ((c * thread_limit) + t)
161 * The max child slot ever assigned, preserved across restarts. Necessary
162 * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
163 * use this value to optimize routines that have to scan the entire
166 static int max_daemons_limit = -1;
168 static ap_worker_pod_t *pod;
170 /* The worker MPM respects a couple of runtime flags that can aid
171 * in debugging. Setting the -DNO_DETACH flag will prevent the root process
172 * from detaching from its controlling terminal. Additionally, setting
173 * the -DONE_PROCESS flag (which implies -DNO_DETACH) will get you the
174 * child_main loop running in the process which originally started up.
175 * This gives you a pretty nice debugging environment. (You'll get a SIGHUP
176 * early in standalone_main; just continue through. This is the server
177 * trying to kill off any child processes which it might have lying
178 * around --- Apache doesn't keep track of their pids, it just sends
179 * SIGHUP to the process group, ignoring it in the root process.
180 * Continue through and you'll be fine.).
183 static int one_process = 0;
186 int raise_sigstop_flags;
189 static apr_pool_t *pconf; /* Pool for config stuff */
190 static apr_pool_t *pchild; /* Pool for httpd child stuff */
192 static pid_t ap_my_pid; /* Linux getpid() doesn't work except in main
193 thread. Use this instead */
194 static pid_t parent_pid;
195 static apr_os_thread_t *listener_os_thread;
197 /* Locks for accept serialization */
198 static apr_proc_mutex_t *accept_mutex;
200 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
201 #define SAFE_ACCEPT(stmt) (ap_listeners->next ? (stmt) : APR_SUCCESS)
203 #define SAFE_ACCEPT(stmt) (stmt)
206 /* The LISTENER_SIGNAL signal will be sent from the main thread to the
207 * listener thread to wake it up for graceful termination (what a child
208 * process from an old generation does when the admin does "apachectl
209 * graceful"). This signal will be blocked in all threads of a child
210 * process except for the listener thread.
212 #define LISTENER_SIGNAL SIGHUP
214 /* The WORKER_SIGNAL signal will be sent from the main thread to the
215 * worker threads during an ungraceful restart or shutdown.
216 * This ensures that on systems (i.e., Linux) where closing the worker
217 * socket doesn't awake the worker thread when it is polling on the socket
218 * (especially in apr_wait_for_io_or_timeout() when handling
219 * Keep-Alive connections), close_worker_sockets() and join_workers()
220 * still function in timely manner and allow ungraceful shutdowns to
221 * proceed to completion. Otherwise join_workers() doesn't return
222 * before the main process decides the child process is non-responsive
223 * and sends a SIGKILL.
225 #define WORKER_SIGNAL AP_SIG_GRACEFUL
227 /* An array of socket descriptors in use by each thread used to
228 * perform a non-graceful (forced) shutdown of the server. */
229 static apr_socket_t **worker_sockets;
231 static void close_worker_sockets(void)
234 for (i = 0; i < threads_per_child; i++) {
235 if (worker_sockets[i]) {
236 apr_socket_close(worker_sockets[i]);
237 worker_sockets[i] = NULL;
242 static void wakeup_listener(void)
244 listener_may_exit = 1;
245 if (!listener_os_thread) {
246 /* XXX there is an obscure path that this doesn't handle perfectly:
247 * right after listener thread is created but before
248 * listener_os_thread is set, the first worker thread hits an
249 * error and starts graceful termination
254 /* unblock the listener if it's waiting for a worker */
255 ap_queue_info_term(worker_queue_info);
258 * we should just be able to "kill(ap_my_pid, LISTENER_SIGNAL)" on all
259 * platforms and wake up the listener thread since it is the only thread
260 * with SIGHUP unblocked, but that doesn't work on Linux
262 #ifdef HAVE_PTHREAD_KILL
263 pthread_kill(*listener_os_thread, LISTENER_SIGNAL);
265 kill(ap_my_pid, LISTENER_SIGNAL);
270 #define ST_GRACEFUL 1
271 #define ST_UNGRACEFUL 2
273 static int terminate_mode = ST_INIT;
275 static void signal_threads(int mode)
277 if (terminate_mode == mode) {
280 terminate_mode = mode;
281 mpm_state = AP_MPMQ_STOPPING;
283 /* in case we weren't called from the listener thread, wake up the
288 /* for ungraceful termination, let the workers exit now;
289 * for graceful termination, the listener thread will notify the
290 * workers to exit once it has stopped accepting new connections
292 if (mode == ST_UNGRACEFUL) {
293 workers_may_exit = 1;
294 ap_queue_interrupt_all(worker_queue);
295 close_worker_sockets(); /* forcefully kill all current connections */
299 static apr_status_t worker_query(int query_code, int *result)
302 case AP_MPMQ_MAX_DAEMON_USED:
303 *result = max_daemons_limit;
305 case AP_MPMQ_IS_THREADED:
306 *result = AP_MPMQ_STATIC;
308 case AP_MPMQ_IS_FORKED:
309 *result = AP_MPMQ_DYNAMIC;
311 case AP_MPMQ_HARD_LIMIT_DAEMONS:
312 *result = server_limit;
314 case AP_MPMQ_HARD_LIMIT_THREADS:
315 *result = thread_limit;
317 case AP_MPMQ_MAX_THREADS:
318 *result = threads_per_child;
320 case AP_MPMQ_MIN_SPARE_DAEMONS:
323 case AP_MPMQ_MIN_SPARE_THREADS:
324 *result = min_spare_threads;
326 case AP_MPMQ_MAX_SPARE_DAEMONS:
329 case AP_MPMQ_MAX_SPARE_THREADS:
330 *result = max_spare_threads;
332 case AP_MPMQ_MAX_REQUESTS_DAEMON:
333 *result = ap_max_requests_per_child;
335 case AP_MPMQ_MAX_DAEMONS:
336 *result = ap_daemons_limit;
338 case AP_MPMQ_MPM_STATE:
341 case AP_MPMQ_GENERATION:
342 *result = my_generation;
348 static pid_t worker_get_child_pid(int childnum)
350 return ap_scoreboard_image->parent[childnum].pid;
353 static apr_status_t worker_note_child_killed(int childnum)
355 ap_scoreboard_image->parent[childnum].pid = 0;
359 /* a clean exit from a child with proper cleanup */
360 static void clean_child_exit(int code) __attribute__ ((noreturn));
361 static void clean_child_exit(int code)
363 mpm_state = AP_MPMQ_STOPPING;
365 apr_pool_destroy(pchild);
370 static void just_die(int sig)
375 /*****************************************************************
376 * Connection structures and accounting...
379 /* volatile just in case */
380 static int volatile shutdown_pending;
381 static int volatile restart_pending;
382 static int volatile is_graceful;
383 static volatile int child_fatal;
386 * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
387 * functions to initiate shutdown or restart without relying on signals.
388 * Previously this was initiated in sig_term() and restart() signal handlers,
389 * but we want to be able to start a shutdown/restart from other sources --
390 * e.g. on Win32, from the service manager. Now the service manager can
391 * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
392 * these functions can also be called by the child processes, since global
393 * variables are no longer used to pass on the required action to the parent.
395 * These should only be called from the parent process itself, since the
396 * parent process will use the shutdown_pending and restart_pending variables
397 * to determine whether to shutdown or restart. The child process should
398 * call signal_parent() directly to tell the parent to die -- this will
399 * cause neither of those variable to be set, which the parent will
400 * assume means something serious is wrong (which it will be, for the
401 * child to force an exit) and so do an exit anyway.
404 static void ap_start_shutdown(int graceful)
406 mpm_state = AP_MPMQ_STOPPING;
407 if (shutdown_pending == 1) {
408 /* Um, is this _probably_ not an error, if the user has
409 * tried to do a shutdown twice quickly, so we won't
410 * worry about reporting it.
414 shutdown_pending = 1;
415 is_graceful = graceful;
418 /* do a graceful restart if graceful == 1 */
419 static void ap_start_restart(int graceful)
421 mpm_state = AP_MPMQ_STOPPING;
422 if (restart_pending == 1) {
423 /* Probably not an error - don't bother reporting it */
427 is_graceful = graceful;
430 static void sig_term(int sig)
432 ap_start_shutdown(sig == AP_SIG_GRACEFUL_STOP);
435 static void restart(int sig)
437 ap_start_restart(sig == AP_SIG_GRACEFUL);
440 static void set_signals(void)
442 #ifndef NO_USE_SIGACTION
447 ap_fatal_signal_setup(ap_server_conf, pconf);
450 #ifndef NO_USE_SIGACTION
451 sigemptyset(&sa.sa_mask);
454 sa.sa_handler = sig_term;
455 if (sigaction(SIGTERM, &sa, NULL) < 0)
456 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
457 "sigaction(SIGTERM)");
458 #ifdef AP_SIG_GRACEFUL_STOP
459 if (sigaction(AP_SIG_GRACEFUL_STOP, &sa, NULL) < 0)
460 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
461 "sigaction(" AP_SIG_GRACEFUL_STOP_STRING ")");
464 if (sigaction(SIGINT, &sa, NULL) < 0)
465 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
466 "sigaction(SIGINT)");
469 sa.sa_handler = SIG_DFL;
470 if (sigaction(SIGXCPU, &sa, NULL) < 0)
471 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
472 "sigaction(SIGXCPU)");
475 sa.sa_handler = SIG_DFL;
476 if (sigaction(SIGXFSZ, &sa, NULL) < 0)
477 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
478 "sigaction(SIGXFSZ)");
481 sa.sa_handler = SIG_IGN;
482 if (sigaction(SIGPIPE, &sa, NULL) < 0)
483 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
484 "sigaction(SIGPIPE)");
487 /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
489 sigaddset(&sa.sa_mask, SIGHUP);
490 sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
491 sa.sa_handler = restart;
492 if (sigaction(SIGHUP, &sa, NULL) < 0)
493 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
494 "sigaction(SIGHUP)");
495 if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
496 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
497 "sigaction(" AP_SIG_GRACEFUL_STRING ")");
501 apr_signal(SIGXCPU, SIG_DFL);
504 apr_signal(SIGXFSZ, SIG_DFL);
508 apr_signal(SIGTERM, sig_term);
510 apr_signal(SIGHUP, restart);
512 #ifdef AP_SIG_GRACEFUL
513 apr_signal(AP_SIG_GRACEFUL, restart);
514 #endif /* AP_SIG_GRACEFUL */
515 #ifdef AP_SIG_GRACEFUL_STOP
516 apr_signal(AP_SIG_GRACEFUL_STOP, sig_term);
517 #endif /* AP_SIG_GRACEFUL_STOP */
519 apr_signal(SIGPIPE, SIG_IGN);
525 /*****************************************************************
526 * Here follows a long bunch of generic server bookkeeping stuff...
529 /*****************************************************************
530 * Child process main loop.
533 static void process_socket(apr_thread_t *thd, apr_pool_t *p, apr_socket_t *sock,
535 int my_thread_num, apr_bucket_alloc_t *bucket_alloc)
537 conn_rec *current_conn;
538 long conn_id = ID_FROM_CHILD_THREAD(my_child_num, my_thread_num);
541 ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
543 current_conn = ap_run_create_connection(p, ap_server_conf, sock,
544 conn_id, sbh, bucket_alloc);
546 current_conn->current_thread = thd;
547 ap_process_connection(current_conn, sock);
548 ap_lingering_close(current_conn);
552 /* requests_this_child has gone to zero or below. See if the admin coded
553 "MaxRequestsPerChild 0", and keep going in that case. Doing it this way
554 simplifies the hot path in worker_thread */
555 static void check_infinite_requests(void)
557 if (ap_max_requests_per_child) {
558 signal_threads(ST_GRACEFUL);
561 /* wow! if you're executing this code, you may have set a record.
562 * either this child process has served over 2 billion requests, or
563 * you're running a threaded 2.0 on a 16 bit machine.
565 * I'll buy pizza and beers at Apachecon for the first person to do
566 * the former without cheating (dorking with INT_MAX, or running with
567 * uncommitted performance patches, for example).
569 * for the latter case, you probably deserve a beer too. Greg Ames
572 requests_this_child = INT_MAX; /* keep going */
576 static void unblock_signal(int sig)
580 sigemptyset(&sig_mask);
581 sigaddset(&sig_mask, sig);
582 #if defined(SIGPROCMASK_SETS_THREAD_MASK)
583 sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);
585 pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL);
589 static void dummy_signal_handler(int sig)
591 /* XXX If specifying SIG_IGN is guaranteed to unblock a syscall,
592 * then we don't need this goofy function.
596 static void * APR_THREAD_FUNC listener_thread(apr_thread_t *thd, void * dummy)
598 proc_info * ti = dummy;
599 int process_slot = ti->pid;
600 apr_pool_t *tpool = apr_thread_pool_get(thd);
602 apr_pool_t *ptrans = NULL; /* Pool for per-transaction stuff */
603 apr_pollset_t *pollset;
606 int have_idle_worker = 0;
607 int last_poll_idx = 0;
611 /* ### check the status */
612 (void) apr_pollset_create(&pollset, num_listensocks, tpool, 0);
614 for (lr = ap_listeners; lr != NULL; lr = lr->next) {
615 apr_pollfd_t pfd = { 0 };
617 pfd.desc_type = APR_POLL_SOCKET;
619 pfd.reqevents = APR_POLLIN;
620 pfd.client_data = lr;
622 /* ### check the status */
623 (void) apr_pollset_add(pollset, &pfd);
625 lr->accept_func = ap_unixd_accept;
628 /* Unblock the signal used to wake this thread up, and set a handler for
631 unblock_signal(LISTENER_SIGNAL);
632 apr_signal(LISTENER_SIGNAL, dummy_signal_handler);
634 /* TODO: Switch to a system where threads reuse the results from earlier
635 poll calls - manoj */
637 /* TODO: requests_this_child should be synchronized - aaron */
638 if (requests_this_child <= 0) {
639 check_infinite_requests();
641 if (listener_may_exit) break;
643 if (!have_idle_worker) {
644 /* the following pops a recycled ptrans pool off a stack
645 * if there is one, in addition to reserving a worker thread
647 rv = ap_queue_info_wait_for_idler(worker_queue_info,
649 if (APR_STATUS_IS_EOF(rv)) {
650 break; /* we've been signaled to die now */
652 else if (rv != APR_SUCCESS) {
653 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
654 "apr_queue_info_wait failed. Attempting to "
655 " shutdown process gracefully.");
656 signal_threads(ST_GRACEFUL);
659 have_idle_worker = 1;
662 /* We've already decremented the idle worker count inside
663 * ap_queue_info_wait_for_idler. */
665 if ((rv = SAFE_ACCEPT(apr_proc_mutex_lock(accept_mutex)))
667 int level = APLOG_EMERG;
669 if (listener_may_exit) {
672 if (ap_scoreboard_image->parent[process_slot].generation !=
673 ap_scoreboard_image->global->running_generation) {
674 level = APLOG_DEBUG; /* common to get these at restart time */
676 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
677 "apr_proc_mutex_lock failed. Attempting to shutdown "
678 "process gracefully.");
679 signal_threads(ST_GRACEFUL);
680 break; /* skip the lock release */
683 if (!ap_listeners->next) {
684 /* Only one listener, so skip the poll */
688 while (!listener_may_exit) {
690 const apr_pollfd_t *pdesc;
692 rv = apr_pollset_poll(pollset, -1, &numdesc, &pdesc);
693 if (rv != APR_SUCCESS) {
694 if (APR_STATUS_IS_EINTR(rv)) {
698 /* apr_pollset_poll() will only return errors in catastrophic
699 * circumstances. Let's try exiting gracefully, for now. */
700 ap_log_error(APLOG_MARK, APLOG_ERR, rv,
701 (const server_rec *) ap_server_conf,
702 "apr_pollset_poll: (listen)");
703 signal_threads(ST_GRACEFUL);
706 if (listener_may_exit) break;
708 /* We can always use pdesc[0], but sockets at position N
709 * could end up completely starved of attention in a very
710 * busy server. Therefore, we round-robin across the
711 * returned set of descriptors. While it is possible that
712 * the returned set of descriptors might flip around and
713 * continue to starve some sockets, we happen to know the
714 * internal pollset implementation retains ordering
715 * stability of the sockets. Thus, the round-robin should
716 * ensure that a socket will eventually be serviced.
718 if (last_poll_idx >= numdesc)
721 /* Grab a listener record from the client_data of the poll
722 * descriptor, and advance our saved index to round-robin
725 * ### hmm... this descriptor might have POLLERR rather
728 lr = pdesc[last_poll_idx++].client_data;
735 if (!listener_may_exit) {
736 if (ptrans == NULL) {
737 /* we can't use a recycled transaction pool this time.
738 * create a new transaction pool */
739 apr_allocator_t *allocator;
741 apr_allocator_create(&allocator);
742 apr_allocator_max_free_set(allocator, ap_max_mem_free);
743 apr_pool_create_ex(&ptrans, pconf, NULL, allocator);
744 apr_allocator_owner_set(allocator, ptrans);
746 apr_pool_tag(ptrans, "transaction");
747 rv = lr->accept_func(&csd, lr, ptrans);
748 /* later we trash rv and rely on csd to indicate success/failure */
749 AP_DEBUG_ASSERT(rv == APR_SUCCESS || !csd);
751 if (rv == APR_EGENERAL) {
752 /* E[NM]FILE, ENOMEM, etc */
753 resource_shortage = 1;
754 signal_threads(ST_GRACEFUL);
756 if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
758 int level = APLOG_EMERG;
760 if (listener_may_exit) {
763 if (ap_scoreboard_image->parent[process_slot].generation !=
764 ap_scoreboard_image->global->running_generation) {
765 level = APLOG_DEBUG; /* common to get these at restart time */
767 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
768 "apr_proc_mutex_unlock failed. Attempting to "
769 "shutdown process gracefully.");
770 signal_threads(ST_GRACEFUL);
773 rv = ap_queue_push(worker_queue, csd, ptrans);
775 /* trash the connection; we couldn't queue the connected
778 apr_socket_close(csd);
779 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
780 "ap_queue_push failed");
783 have_idle_worker = 0;
788 if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
790 int level = APLOG_EMERG;
792 if (ap_scoreboard_image->parent[process_slot].generation !=
793 ap_scoreboard_image->global->running_generation) {
794 level = APLOG_DEBUG; /* common to get these at restart time */
796 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
797 "apr_proc_mutex_unlock failed. Attempting to "
798 "shutdown process gracefully.");
799 signal_threads(ST_GRACEFUL);
805 ap_close_listeners();
806 ap_queue_term(worker_queue);
808 ap_scoreboard_image->parent[process_slot].quiescing = 1;
810 /* wake up the main thread */
811 kill(ap_my_pid, SIGTERM);
813 apr_thread_exit(thd, APR_SUCCESS);
817 /* XXX For ungraceful termination/restart, we definitely don't want to
818 * wait for active connections to finish but we may want to wait
819 * for idle workers to get out of the queue code and release mutexes,
820 * since those mutexes are cleaned up pretty soon and some systems
821 * may not react favorably (i.e., segfault) if operations are attempted
822 * on cleaned-up mutexes.
824 static void * APR_THREAD_FUNC worker_thread(apr_thread_t *thd, void * dummy)
826 proc_info * ti = dummy;
827 int process_slot = ti->pid;
828 int thread_slot = ti->tid;
829 apr_socket_t *csd = NULL;
830 apr_bucket_alloc_t *bucket_alloc;
831 apr_pool_t *last_ptrans = NULL;
832 apr_pool_t *ptrans; /* Pool for per-transaction stuff */
838 ap_scoreboard_image->servers[process_slot][thread_slot].pid = ap_my_pid;
839 ap_scoreboard_image->servers[process_slot][thread_slot].tid = apr_os_thread_current();
840 ap_scoreboard_image->servers[process_slot][thread_slot].generation = my_generation;
841 ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_STARTING, NULL);
843 #ifdef HAVE_PTHREAD_KILL
844 unblock_signal(WORKER_SIGNAL);
845 apr_signal(WORKER_SIGNAL, dummy_signal_handler);
848 while (!workers_may_exit) {
850 rv = ap_queue_info_set_idle(worker_queue_info, last_ptrans);
852 if (rv != APR_SUCCESS) {
853 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
854 "ap_queue_info_set_idle failed. Attempting to "
855 "shutdown process gracefully.");
856 signal_threads(ST_GRACEFUL);
862 ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_READY, NULL);
864 if (workers_may_exit) {
867 rv = ap_queue_pop(worker_queue, &csd, &ptrans);
869 if (rv != APR_SUCCESS) {
870 /* We get APR_EOF during a graceful shutdown once all the connections
871 * accepted by this server process have been handled.
873 if (APR_STATUS_IS_EOF(rv)) {
876 /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
877 * from an explicit call to ap_queue_interrupt_all(). This allows
878 * us to unblock threads stuck in ap_queue_pop() when a shutdown
881 * If workers_may_exit is set and this is ungraceful termination/
882 * restart, we are bound to get an error on some systems (e.g.,
883 * AIX, which sanity-checks mutex operations) since the queue
884 * may have already been cleaned up. Don't log the "error" if
885 * workers_may_exit is set.
887 else if (APR_STATUS_IS_EINTR(rv)) {
890 /* We got some other error. */
891 else if (!workers_may_exit) {
892 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
893 "ap_queue_pop failed");
898 worker_sockets[thread_slot] = csd;
899 bucket_alloc = apr_bucket_alloc_create(ptrans);
900 process_socket(thd, ptrans, csd, process_slot, thread_slot, bucket_alloc);
901 worker_sockets[thread_slot] = NULL;
902 requests_this_child--;
903 apr_pool_clear(ptrans);
904 last_ptrans = ptrans;
907 ap_update_child_status_from_indexes(process_slot, thread_slot,
908 (dying) ? SERVER_DEAD : SERVER_GRACEFUL, (request_rec *) NULL);
910 apr_thread_exit(thd, APR_SUCCESS);
914 static int check_signal(int signum)
924 static void create_listener_thread(thread_starter *ts)
926 int my_child_num = ts->child_num_arg;
927 apr_threadattr_t *thread_attr = ts->threadattr;
931 my_info = (proc_info *)malloc(sizeof(proc_info));
932 my_info->pid = my_child_num;
933 my_info->tid = -1; /* listener thread doesn't have a thread slot */
935 rv = apr_thread_create(&ts->listener, thread_attr, listener_thread,
937 if (rv != APR_SUCCESS) {
938 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
939 "apr_thread_create: unable to create listener thread");
940 /* let the parent decide how bad this really is */
941 clean_child_exit(APEXIT_CHILDSICK);
943 apr_os_thread_get(&listener_os_thread, ts->listener);
946 /* XXX under some circumstances not understood, children can get stuck
947 * in start_threads forever trying to take over slots which will
948 * never be cleaned up; for now there is an APLOG_DEBUG message issued
949 * every so often when this condition occurs
951 static void * APR_THREAD_FUNC start_threads(apr_thread_t *thd, void *dummy)
953 thread_starter *ts = dummy;
954 apr_thread_t **threads = ts->threads;
955 apr_threadattr_t *thread_attr = ts->threadattr;
956 int child_num_arg = ts->child_num_arg;
957 int my_child_num = child_num_arg;
961 int threads_created = 0;
962 int listener_started = 0;
964 int prev_threads_created;
966 /* We must create the fd queues before we start up the listener
967 * and worker threads. */
968 worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
969 rv = ap_queue_init(worker_queue, threads_per_child, pchild);
970 if (rv != APR_SUCCESS) {
971 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
972 "ap_queue_init() failed");
973 clean_child_exit(APEXIT_CHILDFATAL);
976 rv = ap_queue_info_create(&worker_queue_info, pchild,
978 if (rv != APR_SUCCESS) {
979 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
980 "ap_queue_info_create() failed");
981 clean_child_exit(APEXIT_CHILDFATAL);
984 worker_sockets = apr_pcalloc(pchild, threads_per_child
985 * sizeof(apr_socket_t *));
987 loops = prev_threads_created = 0;
989 /* threads_per_child does not include the listener thread */
990 for (i = 0; i < threads_per_child; i++) {
991 int status = ap_scoreboard_image->servers[child_num_arg][i].status;
993 if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
997 my_info = (proc_info *)malloc(sizeof(proc_info));
998 if (my_info == NULL) {
999 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
1000 "malloc: out of memory");
1001 clean_child_exit(APEXIT_CHILDFATAL);
1003 my_info->pid = my_child_num;
1007 /* We are creating threads right now */
1008 ap_update_child_status_from_indexes(my_child_num, i,
1009 SERVER_STARTING, NULL);
1010 /* We let each thread update its own scoreboard entry. This is
1011 * done because it lets us deal with tid better.
1013 rv = apr_thread_create(&threads[i], thread_attr,
1014 worker_thread, my_info, pchild);
1015 if (rv != APR_SUCCESS) {
1016 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1017 "apr_thread_create: unable to create worker thread");
1018 /* let the parent decide how bad this really is */
1019 clean_child_exit(APEXIT_CHILDSICK);
1023 /* Start the listener only when there are workers available */
1024 if (!listener_started && threads_created) {
1025 create_listener_thread(ts);
1026 listener_started = 1;
1028 if (start_thread_may_exit || threads_created == threads_per_child) {
1031 /* wait for previous generation to clean up an entry */
1032 apr_sleep(apr_time_from_sec(1));
1034 if (loops % 120 == 0) { /* every couple of minutes */
1035 if (prev_threads_created == threads_created) {
1036 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1037 "child %" APR_PID_T_FMT " isn't taking over "
1038 "slots very quickly (%d of %d)",
1039 ap_my_pid, threads_created, threads_per_child);
1041 prev_threads_created = threads_created;
1045 /* What state should this child_main process be listed as in the
1047 * ap_update_child_status_from_indexes(my_child_num, i, SERVER_STARTING,
1048 * (request_rec *) NULL);
1050 * This state should be listed separately in the scoreboard, in some kind
1051 * of process_status, not mixed in with the worker threads' status.
1052 * "life_status" is almost right, but it's in the worker's structure, and
1053 * the name could be clearer. gla
1055 apr_thread_exit(thd, APR_SUCCESS);
1059 static void join_workers(apr_thread_t *listener, apr_thread_t **threads)
1062 apr_status_t rv, thread_rv;
1067 /* deal with a rare timing window which affects waking up the
1068 * listener thread... if the signal sent to the listener thread
1069 * is delivered between the time it verifies that the
1070 * listener_may_exit flag is clear and the time it enters a
1071 * blocking syscall, the signal didn't do any good... work around
1072 * that by sleeping briefly and sending it again
1077 #ifdef HAVE_PTHREAD_KILL
1078 pthread_kill(*listener_os_thread, 0)
1083 /* listener not dead yet */
1084 apr_sleep(apr_time_make(0, 500000));
1089 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1090 "the listener thread didn't exit");
1093 rv = apr_thread_join(&thread_rv, listener);
1094 if (rv != APR_SUCCESS) {
1095 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1096 "apr_thread_join: unable to join listener thread");
1101 for (i = 0; i < threads_per_child; i++) {
1102 if (threads[i]) { /* if we ever created this thread */
1103 #ifdef HAVE_PTHREAD_KILL
1104 apr_os_thread_t *worker_os_thread;
1106 apr_os_thread_get(&worker_os_thread, threads[i]);
1107 pthread_kill(*worker_os_thread, WORKER_SIGNAL);
1110 rv = apr_thread_join(&thread_rv, threads[i]);
1111 if (rv != APR_SUCCESS) {
1112 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1113 "apr_thread_join: unable to join worker "
1121 static void join_start_thread(apr_thread_t *start_thread_id)
1123 apr_status_t rv, thread_rv;
1125 start_thread_may_exit = 1; /* tell it to give up in case it is still
1126 * trying to take over slots from a
1127 * previous generation
1129 rv = apr_thread_join(&thread_rv, start_thread_id);
1130 if (rv != APR_SUCCESS) {
1131 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1132 "apr_thread_join: unable to join the start "
1137 static void child_main(int child_num_arg)
1139 apr_thread_t **threads;
1142 apr_threadattr_t *thread_attr;
1143 apr_thread_t *start_thread_id;
1145 mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
1148 ap_my_pid = getpid();
1149 ap_fatal_signal_child_setup(ap_server_conf);
1150 apr_pool_create(&pchild, pconf);
1152 /*stuff to do before we switch id's, so we have permissions.*/
1153 ap_reopen_scoreboard(pchild, NULL, 0);
1155 rv = SAFE_ACCEPT(apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname,
1157 if (rv != APR_SUCCESS) {
1158 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1159 "Couldn't initialize cross-process lock in child");
1160 clean_child_exit(APEXIT_CHILDFATAL);
1163 if (ap_run_drop_privileges(pchild, ap_server_conf)) {
1164 clean_child_exit(APEXIT_CHILDFATAL);
1167 ap_run_child_init(pchild, ap_server_conf);
1169 /* done with init critical section */
1171 /* Just use the standard apr_setup_signal_thread to block all signals
1172 * from being received. The child processes no longer use signals for
1173 * any communication with the parent process.
1175 rv = apr_setup_signal_thread();
1176 if (rv != APR_SUCCESS) {
1177 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1178 "Couldn't initialize signal thread");
1179 clean_child_exit(APEXIT_CHILDFATAL);
1182 if (ap_max_requests_per_child) {
1183 requests_this_child = ap_max_requests_per_child;
1186 /* coding a value of zero means infinity */
1187 requests_this_child = INT_MAX;
1190 /* Setup worker threads */
1192 /* clear the storage; we may not create all our threads immediately,
1193 * and we want a 0 entry to indicate a thread which was not created
1195 threads = (apr_thread_t **)calloc(1,
1196 sizeof(apr_thread_t *) * threads_per_child);
1197 if (threads == NULL) {
1198 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
1199 "malloc: out of memory");
1200 clean_child_exit(APEXIT_CHILDFATAL);
1203 ts = (thread_starter *)apr_palloc(pchild, sizeof(*ts));
1205 apr_threadattr_create(&thread_attr, pchild);
1206 /* 0 means PTHREAD_CREATE_JOINABLE */
1207 apr_threadattr_detach_set(thread_attr, 0);
1209 if (ap_thread_stacksize != 0) {
1210 apr_threadattr_stacksize_set(thread_attr, ap_thread_stacksize);
1213 ts->threads = threads;
1214 ts->listener = NULL;
1215 ts->child_num_arg = child_num_arg;
1216 ts->threadattr = thread_attr;
1218 rv = apr_thread_create(&start_thread_id, thread_attr, start_threads,
1220 if (rv != APR_SUCCESS) {
1221 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1222 "apr_thread_create: unable to create worker thread");
1223 /* let the parent decide how bad this really is */
1224 clean_child_exit(APEXIT_CHILDSICK);
1227 mpm_state = AP_MPMQ_RUNNING;
1229 /* If we are only running in one_process mode, we will want to
1230 * still handle signals. */
1232 /* Block until we get a terminating signal. */
1233 apr_signal_thread(check_signal);
1234 /* make sure the start thread has finished; signal_threads()
1235 * and join_workers() depend on that
1237 /* XXX join_start_thread() won't be awakened if one of our
1238 * threads encounters a critical error and attempts to
1239 * shutdown this child
1241 join_start_thread(start_thread_id);
1242 signal_threads(ST_UNGRACEFUL); /* helps us terminate a little more
1243 * quickly than the dispatch of the signal thread
1244 * beats the Pipe of Death and the browsers
1246 /* A terminating signal was received. Now join each of the
1247 * workers to clean them up.
1248 * If the worker already exited, then the join frees
1249 * their resources and returns.
1250 * If the worker hasn't exited, then this blocks until
1251 * they have (then cleans up).
1253 join_workers(ts->listener, threads);
1255 else { /* !one_process */
1256 /* remove SIGTERM from the set of blocked signals... if one of
1257 * the other threads in the process needs to take us down
1258 * (e.g., for MaxRequestsPerChild) it will send us SIGTERM
1260 unblock_signal(SIGTERM);
1261 apr_signal(SIGTERM, dummy_signal_handler);
1262 /* Watch for any messages from the parent over the POD */
1264 rv = ap_worker_pod_check(pod);
1265 if (rv == AP_NORESTART) {
1266 /* see if termination was triggered while we slept */
1267 switch(terminate_mode) {
1276 if (rv == AP_GRACEFUL || rv == AP_RESTART) {
1277 /* make sure the start thread has finished;
1278 * signal_threads() and join_workers depend on that
1280 join_start_thread(start_thread_id);
1281 signal_threads(rv == AP_GRACEFUL ? ST_GRACEFUL : ST_UNGRACEFUL);
1286 /* A terminating signal was received. Now join each of the
1287 * workers to clean them up.
1288 * If the worker already exited, then the join frees
1289 * their resources and returns.
1290 * If the worker hasn't exited, then this blocks until
1291 * they have (then cleans up).
1293 join_workers(ts->listener, threads);
1298 clean_child_exit(resource_shortage ? APEXIT_CHILDSICK : 0);
1301 static int make_child(server_rec *s, int slot)
1305 if (slot + 1 > max_daemons_limit) {
1306 max_daemons_limit = slot + 1;
1311 ap_scoreboard_image->parent[slot].pid = getpid();
1315 if ((pid = fork()) == -1) {
1316 ap_log_error(APLOG_MARK, APLOG_ERR, errno, s,
1317 "fork: Unable to fork new process");
1318 /* fork didn't succeed. There's no need to touch the scoreboard;
1319 * if we were trying to replace a failed child process, then
1320 * server_main_loop() marked its workers SERVER_DEAD, and if
1321 * we were trying to replace a child process that exited normally,
1322 * its worker_thread()s left SERVER_DEAD or SERVER_GRACEFUL behind.
1325 /* In case system resources are maxxed out, we don't want
1326 Apache running away with the CPU trying to fork over and
1327 over and over again. */
1328 apr_sleep(apr_time_from_sec(10));
1334 #ifdef HAVE_BINDPROCESSOR
1335 /* By default, AIX binds to a single processor. This bit unbinds
1336 * children which will then bind to another CPU.
1338 int status = bindprocessor(BINDPROCESS, (int)getpid(),
1339 PROCESSOR_CLASS_ANY);
1341 ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
1343 "processor unbind failed %d", status);
1345 RAISE_SIGSTOP(MAKE_CHILD);
1347 apr_signal(SIGTERM, just_die);
1350 clean_child_exit(0);
1353 if (ap_scoreboard_image->parent[slot].pid != 0) {
1354 /* This new child process is squatting on the scoreboard
1355 * entry owned by an exiting child process, which cannot
1356 * exit until all active requests complete.
1357 * Don't forget about this exiting child process, or we
1358 * won't be able to kill it if it doesn't exit by the
1359 * time the server is shut down.
1361 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1362 "taking over scoreboard slot from %" APR_PID_T_FMT "%s",
1363 ap_scoreboard_image->parent[slot].pid,
1364 ap_scoreboard_image->parent[slot].quiescing ?
1365 " (quiescing)" : "");
1366 ap_register_extra_mpm_process(ap_scoreboard_image->parent[slot].pid);
1368 ap_scoreboard_image->parent[slot].quiescing = 0;
1369 ap_scoreboard_image->parent[slot].pid = pid;
1373 /* start up a bunch of children */
1374 static void startup_children(int number_to_start)
1378 for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
1379 if (ap_scoreboard_image->parent[i].pid != 0) {
1382 if (make_child(ap_server_conf, i) < 0) {
1391 * idle_spawn_rate is the number of children that will be spawned on the
1392 * next maintenance cycle if there aren't enough idle servers. It is
1393 * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
1394 * without the need to spawn.
1396 static int idle_spawn_rate = 1;
1397 #ifndef MAX_SPAWN_RATE
1398 #define MAX_SPAWN_RATE (32)
1400 static int hold_off_on_exponential_spawning;
1402 static void perform_idle_server_maintenance(void)
1405 int idle_thread_count;
1409 int totally_free_length = 0;
1410 int free_slots[MAX_SPAWN_RATE];
1413 int active_thread_count = 0;
1415 /* initialize the free_list */
1418 idle_thread_count = 0;
1422 for (i = 0; i < ap_daemons_limit; ++i) {
1423 /* Initialization to satisfy the compiler. It doesn't know
1424 * that threads_per_child is always > 0 */
1425 int status = SERVER_DEAD;
1426 int any_dying_threads = 0;
1427 int any_dead_threads = 0;
1428 int all_dead_threads = 1;
1430 if (i >= max_daemons_limit && totally_free_length == idle_spawn_rate)
1431 /* short cut if all active processes have been examined and
1432 * enough empty scoreboard slots have been found
1435 ps = &ap_scoreboard_image->parent[i];
1436 for (j = 0; j < threads_per_child; j++) {
1437 ws = &ap_scoreboard_image->servers[i][j];
1438 status = ws->status;
1440 /* XXX any_dying_threads is probably no longer needed GLA */
1441 any_dying_threads = any_dying_threads ||
1442 (status == SERVER_GRACEFUL);
1443 any_dead_threads = any_dead_threads || (status == SERVER_DEAD);
1444 all_dead_threads = all_dead_threads &&
1445 (status == SERVER_DEAD ||
1446 status == SERVER_GRACEFUL);
1448 /* We consider a starting server as idle because we started it
1449 * at least a cycle ago, and if it still hasn't finished starting
1450 * then we're just going to swamp things worse by forking more.
1451 * So we hopefully won't need to fork more if we count it.
1452 * This depends on the ordering of SERVER_READY and SERVER_STARTING.
1454 if (ps->pid != 0) { /* XXX just set all_dead_threads in outer for
1455 loop if no pid? not much else matters */
1456 if (status <= SERVER_READY &&
1458 ps->generation == my_generation) {
1459 ++idle_thread_count;
1461 if (status >= SERVER_READY && status < SERVER_GRACEFUL) {
1462 ++active_thread_count;
1466 if (any_dead_threads && totally_free_length < idle_spawn_rate
1467 && free_length < MAX_SPAWN_RATE
1468 && (!ps->pid /* no process in the slot */
1469 || ps->quiescing)) { /* or at least one is going away */
1470 if (all_dead_threads) {
1471 /* great! we prefer these, because the new process can
1472 * start more threads sooner. So prioritize this slot
1473 * by putting it ahead of any slots with active threads.
1475 * first, make room by moving a slot that's potentially still
1476 * in use to the end of the array
1478 free_slots[free_length] = free_slots[totally_free_length];
1479 free_slots[totally_free_length++] = i;
1482 /* slot is still in use - back of the bus
1484 free_slots[free_length] = i;
1488 /* XXX if (!ps->quiescing) is probably more reliable GLA */
1489 if (!any_dying_threads) {
1495 if (sick_child_detected) {
1496 if (active_thread_count > 0) {
1497 /* some child processes appear to be working. don't kill the
1500 sick_child_detected = 0;
1503 /* looks like a basket case. give up.
1505 shutdown_pending = 1;
1507 ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
1509 "No active workers found..."
1510 " Apache is exiting!");
1511 /* the child already logged the failure details */
1516 max_daemons_limit = last_non_dead + 1;
1518 if (idle_thread_count > max_spare_threads) {
1519 /* Kill off one child */
1520 ap_worker_pod_signal(pod, TRUE);
1521 idle_spawn_rate = 1;
1523 else if (idle_thread_count < min_spare_threads) {
1524 /* terminate the free list */
1525 if (free_length == 0) { /* scoreboard is full, can't fork */
1527 if (active_thread_count >= ap_daemons_limit * threads_per_child) {
1528 static int reported = 0;
1530 /* only report this condition once */
1531 ap_log_error(APLOG_MARK, APLOG_ERR, 0,
1533 "server reached MaxClients setting, consider"
1534 " raising the MaxClients setting");
1539 ap_log_error(APLOG_MARK, APLOG_ERR, 0,
1541 "scoreboard is full, not at MaxClients");
1543 idle_spawn_rate = 1;
1546 if (free_length > idle_spawn_rate) {
1547 free_length = idle_spawn_rate;
1549 if (idle_spawn_rate >= 8) {
1550 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1552 "server seems busy, (you may need "
1553 "to increase StartServers, ThreadsPerChild "
1554 "or Min/MaxSpareThreads), "
1555 "spawning %d children, there are around %d idle "
1556 "threads, and %d total children", free_length,
1557 idle_thread_count, total_non_dead);
1559 for (i = 0; i < free_length; ++i) {
1560 make_child(ap_server_conf, free_slots[i]);
1562 /* the next time around we want to spawn twice as many if this
1563 * wasn't good enough, but not if we've just done a graceful
1565 if (hold_off_on_exponential_spawning) {
1566 --hold_off_on_exponential_spawning;
1568 else if (idle_spawn_rate < MAX_SPAWN_RATE) {
1569 idle_spawn_rate *= 2;
1574 idle_spawn_rate = 1;
1578 static void server_main_loop(int remaining_children_to_start)
1581 apr_exit_why_e exitwhy;
1582 int status, processed_status;
1586 while (!restart_pending && !shutdown_pending) {
1587 ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
1589 if (pid.pid != -1) {
1590 processed_status = ap_process_child_status(&pid, exitwhy, status);
1591 if (processed_status == APEXIT_CHILDFATAL) {
1592 shutdown_pending = 1;
1596 else if (processed_status == APEXIT_CHILDSICK) {
1597 /* tell perform_idle_server_maintenance to check into this
1598 * on the next timer pop
1600 sick_child_detected = 1;
1602 /* non-fatal death... note that it's gone in the scoreboard. */
1603 child_slot = ap_find_child_by_pid(&pid);
1604 if (child_slot >= 0) {
1605 for (i = 0; i < threads_per_child; i++)
1606 ap_update_child_status_from_indexes(child_slot, i, SERVER_DEAD,
1607 (request_rec *) NULL);
1609 ap_scoreboard_image->parent[child_slot].pid = 0;
1610 ap_scoreboard_image->parent[child_slot].quiescing = 0;
1611 if (processed_status == APEXIT_CHILDSICK) {
1612 /* resource shortage, minimize the fork rate */
1613 idle_spawn_rate = 1;
1615 else if (remaining_children_to_start
1616 && child_slot < ap_daemons_limit) {
1617 /* we're still doing a 1-for-1 replacement of dead
1618 * children with new children
1620 make_child(ap_server_conf, child_slot);
1621 --remaining_children_to_start;
1624 else if (ap_unregister_extra_mpm_process(pid.pid) == 1) {
1626 #if APR_HAS_OTHER_CHILD
1628 else if (apr_proc_other_child_alert(&pid, APR_OC_REASON_DEATH,
1633 else if (is_graceful) {
1634 /* Great, we've probably just lost a slot in the
1635 * scoreboard. Somehow we don't know about this child.
1637 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1639 "long lost child came home! (pid %ld)",
1642 /* Don't perform idle maintenance when a child dies,
1643 * only do it when there's a timeout. Remember only a
1644 * finite number of children can die, and it's pretty
1645 * pathological for a lot to die suddenly.
1649 else if (remaining_children_to_start) {
1650 /* we hit a 1 second timeout in which none of the previous
1651 * generation of children needed to be reaped... so assume
1652 * they're all done, and pick up the slack if any is left.
1654 startup_children(remaining_children_to_start);
1655 remaining_children_to_start = 0;
1656 /* In any event we really shouldn't do the code below because
1657 * few of the servers we just started are in the IDLE state
1658 * yet, so we'd mistakenly create an extra server.
1663 perform_idle_server_maintenance();
1667 static int worker_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
1669 int remaining_children_to_start;
1672 ap_log_pid(pconf, ap_pid_fname);
1674 /* Initialize cross-process accept lock */
1675 ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
1676 ap_server_root_relative(_pconf, ap_lock_fname),
1679 rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname,
1680 ap_accept_lock_mech, _pconf);
1681 if (rv != APR_SUCCESS) {
1682 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1683 "Couldn't create accept lock");
1684 mpm_state = AP_MPMQ_STOPPING;
1688 #if APR_USE_SYSVSEM_SERIALIZE
1689 if (ap_accept_lock_mech == APR_LOCK_DEFAULT ||
1690 ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1692 if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1694 rv = ap_unixd_set_proc_mutex_perms(accept_mutex);
1695 if (rv != APR_SUCCESS) {
1696 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1697 "Couldn't set permissions on cross-process lock; "
1698 "check User and Group directives");
1699 mpm_state = AP_MPMQ_STOPPING;
1705 if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
1706 mpm_state = AP_MPMQ_STOPPING;
1709 /* fix the generation number in the global score; we just got a new,
1710 * cleared scoreboard
1712 ap_scoreboard_image->global->running_generation = my_generation;
1716 /* Don't thrash... */
1717 if (max_spare_threads < min_spare_threads + threads_per_child)
1718 max_spare_threads = min_spare_threads + threads_per_child;
1720 /* If we're doing a graceful_restart then we're going to see a lot
1721 * of children exiting immediately when we get into the main loop
1722 * below (because we just sent them AP_SIG_GRACEFUL). This happens pretty
1723 * rapidly... and for each one that exits we may start a new one, until
1724 * there are at least min_spare_threads idle threads, counting across
1725 * all children. But we may be permitted to start more children than
1726 * that, so we'll just keep track of how many we're
1727 * supposed to start up without the 1 second penalty between each fork.
1729 remaining_children_to_start = ap_daemons_to_start;
1730 if (remaining_children_to_start > ap_daemons_limit) {
1731 remaining_children_to_start = ap_daemons_limit;
1734 startup_children(remaining_children_to_start);
1735 remaining_children_to_start = 0;
1738 /* give the system some time to recover before kicking into
1739 * exponential mode */
1740 hold_off_on_exponential_spawning = 10;
1743 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1744 "%s configured -- resuming normal operations",
1745 ap_get_server_description());
1746 ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
1747 "Server built: %s", ap_get_server_built());
1748 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
1749 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1750 "AcceptMutex: %s (default: %s)",
1751 apr_proc_mutex_name(accept_mutex),
1752 apr_proc_mutex_defname());
1754 restart_pending = shutdown_pending = 0;
1755 mpm_state = AP_MPMQ_RUNNING;
1757 server_main_loop(remaining_children_to_start);
1758 mpm_state = AP_MPMQ_STOPPING;
1760 if (shutdown_pending && !is_graceful) {
1761 /* Time to shut down:
1762 * Kill child processes, tell them to call child_exit, etc...
1764 ap_worker_pod_killpg(pod, ap_daemons_limit, FALSE);
1765 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1768 /* cleanup pid file on normal shutdown */
1769 const char *pidfile = NULL;
1770 pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1771 if ( pidfile != NULL && unlink(pidfile) == 0)
1772 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1774 "removed PID file %s (pid=%" APR_PID_T_FMT ")",
1777 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
1778 ap_server_conf, "caught SIGTERM, shutting down");
1781 } else if (shutdown_pending) {
1782 /* Time to gracefully shut down:
1783 * Kill child processes, tell them to call child_exit, etc...
1785 int active_children;
1787 apr_time_t cutoff = 0;
1789 /* Close our listeners, and then ask our children to do same */
1790 ap_close_listeners();
1791 ap_worker_pod_killpg(pod, ap_daemons_limit, TRUE);
1792 ap_relieve_child_processes();
1795 /* cleanup pid file on normal shutdown */
1796 const char *pidfile = NULL;
1797 pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1798 if ( pidfile != NULL && unlink(pidfile) == 0)
1799 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1801 "removed PID file %s (pid=%" APR_PID_T_FMT ")",
1804 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1805 "caught " AP_SIG_GRACEFUL_STOP_STRING
1806 ", shutting down gracefully");
1809 if (ap_graceful_shutdown_timeout) {
1810 cutoff = apr_time_now() +
1811 apr_time_from_sec(ap_graceful_shutdown_timeout);
1814 /* Don't really exit until each child has finished */
1815 shutdown_pending = 0;
1817 /* Pause for a second */
1818 apr_sleep(apr_time_from_sec(1));
1820 /* Relieve any children which have now exited */
1821 ap_relieve_child_processes();
1823 active_children = 0;
1824 for (index = 0; index < ap_daemons_limit; ++index) {
1825 if (ap_mpm_safe_kill(MPM_CHILD_PID(index), 0) == APR_SUCCESS) {
1826 active_children = 1;
1827 /* Having just one child is enough to stay around */
1831 } while (!shutdown_pending && active_children &&
1832 (!ap_graceful_shutdown_timeout || apr_time_now() < cutoff));
1834 /* We might be here because we received SIGTERM, either
1835 * way, try and make sure that all of our processes are
1838 ap_worker_pod_killpg(pod, ap_daemons_limit, FALSE);
1839 ap_reclaim_child_processes(1);
1844 /* we've been told to restart */
1845 apr_signal(SIGHUP, SIG_IGN);
1848 /* not worth thinking about */
1852 /* advance to the next generation */
1853 /* XXX: we really need to make sure this new generation number isn't in
1854 * use by any of the children.
1857 ap_scoreboard_image->global->running_generation = my_generation;
1860 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1861 AP_SIG_GRACEFUL_STRING " received. Doing graceful restart");
1862 /* wake up the children...time to die. But we'll have more soon */
1863 ap_worker_pod_killpg(pod, ap_daemons_limit, TRUE);
1866 /* This is mostly for debugging... so that we know what is still
1867 * gracefully dealing with existing request.
1872 /* Kill 'em all. Since the child acts the same on the parents SIGTERM
1873 * and a SIGHUP, we may as well use the same signal, because some user
1874 * pthreads are stealing signals from us left and right.
1876 ap_worker_pod_killpg(pod, ap_daemons_limit, FALSE);
1878 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1879 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1880 "SIGHUP received. Attempting to restart");
1886 /* This really should be a post_config hook, but the error log is already
1887 * redirected by that point, so we need to do this in the open_logs phase.
1889 static int worker_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
1891 static int restart_num = 0;
1893 int level_flags = 0;
1898 /* the reverse of pre_config, we want this only the first time around */
1899 if (restart_num++ == 0) {
1901 level_flags |= APLOG_STARTUP;
1904 if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
1905 ap_log_error(APLOG_MARK, APLOG_ALERT | level_flags, 0,
1906 (startup ? NULL : s),
1907 "no listening sockets available, shutting down");
1912 if ((rv = ap_worker_pod_open(pconf, &pod))) {
1913 ap_log_error(APLOG_MARK, APLOG_CRIT | level_flags, rv,
1914 (startup ? NULL : s),
1915 "could not open pipe-of-death");
1922 static int worker_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
1925 static int restart_num = 0;
1926 int no_detach, debug, foreground;
1929 mpm_state = AP_MPMQ_STARTING;
1931 debug = ap_exists_config_define("DEBUG");
1934 foreground = one_process = 1;
1938 one_process = ap_exists_config_define("ONE_PROCESS");
1939 no_detach = ap_exists_config_define("NO_DETACH");
1940 foreground = ap_exists_config_define("FOREGROUND");
1943 /* sigh, want this only the second time around */
1944 if (restart_num++ == 1) {
1947 if (!one_process && !foreground) {
1948 rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
1949 : APR_PROC_DETACH_DAEMONIZE);
1950 if (rv != APR_SUCCESS) {
1951 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
1952 "apr_proc_detach failed");
1953 return HTTP_INTERNAL_SERVER_ERROR;
1956 parent_pid = ap_my_pid = getpid();
1959 ap_listen_pre_config();
1960 ap_daemons_to_start = DEFAULT_START_DAEMON;
1961 min_spare_threads = DEFAULT_MIN_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1962 max_spare_threads = DEFAULT_MAX_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1963 server_limit = DEFAULT_SERVER_LIMIT;
1964 thread_limit = DEFAULT_THREAD_LIMIT;
1965 ap_daemons_limit = server_limit;
1966 threads_per_child = DEFAULT_THREADS_PER_CHILD;
1967 max_clients = ap_daemons_limit * threads_per_child;
1968 ap_pid_fname = DEFAULT_PIDLOG;
1969 ap_lock_fname = DEFAULT_LOCKFILE;
1970 ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
1971 ap_extended_status = 0;
1972 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
1973 ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
1976 apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1981 static int worker_check_config(apr_pool_t *p, apr_pool_t *plog,
1982 apr_pool_t *ptemp, server_rec *s)
1984 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 (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", 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 threads_per_child, thread_limit);
2097 threads_per_child = thread_limit;
2099 else if (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.", threads_per_child);
2105 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2106 "ThreadsPerChild of %d not allowed, increasing to 1",
2109 threads_per_child = 1;
2112 if (max_clients < 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 threads_per_child, threads_per_child);
2121 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2122 " as the number of threads in a single server.");
2124 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2125 "MaxClients of %d is less than ThreadsPerChild "
2126 "of %d, increasing to match",
2127 max_clients, threads_per_child);
2129 max_clients = threads_per_child;
2132 ap_daemons_limit = max_clients / threads_per_child;
2134 if (max_clients % threads_per_child) {
2135 int tmp_max_clients = ap_daemons_limit * 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,", 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, 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 * 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 * threads_per_child);
2176 ap_daemons_limit = server_limit;
2179 /* ap_daemons_to_start > ap_daemons_limit checked in worker_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 + threads_per_child
2211 * checked in worker_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);
2232 ap_hook_mpm(worker_run, NULL, NULL, APR_HOOK_MIDDLE);
2233 ap_hook_mpm_query(worker_query, NULL, NULL, APR_HOOK_MIDDLE);
2234 ap_hook_mpm_get_child_pid(worker_get_child_pid, NULL, NULL, APR_HOOK_MIDDLE);
2235 ap_hook_mpm_note_child_killed(worker_note_child_killed, NULL, NULL, APR_HOOK_MIDDLE);
2238 static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy,
2241 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2246 ap_daemons_to_start = atoi(arg);
2250 static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy,
2253 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2258 min_spare_threads = atoi(arg);
2262 static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy,
2265 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2270 max_spare_threads = atoi(arg);
2274 static const char *set_max_clients (cmd_parms *cmd, void *dummy,
2277 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2282 max_clients = atoi(arg);
2286 static const char *set_threads_per_child (cmd_parms *cmd, void *dummy,
2289 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2294 threads_per_child = atoi(arg);
2298 static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
2300 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2305 server_limit = atoi(arg);
2309 static const char *set_thread_limit (cmd_parms *cmd, void *dummy, const char *arg)
2311 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2316 thread_limit = atoi(arg);
2320 static const command_rec worker_cmds[] = {
2322 AP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
2323 "Number of child processes launched at server startup"),
2324 AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
2325 "Minimum number of idle threads, to handle request spikes"),
2326 AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
2327 "Maximum number of idle threads"),
2328 AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
2329 "Maximum number of threads alive at the same time"),
2330 AP_INIT_TAKE1("ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF,
2331 "Number of threads each child creates"),
2332 AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
2333 "Maximum number of child processes for this run of Apache"),
2334 AP_INIT_TAKE1("ThreadLimit", set_thread_limit, NULL, RSRC_CONF,
2335 "Maximum number of worker threads per child process for this run of Apache - Upper limit for ThreadsPerChild"),
2336 AP_GRACEFUL_SHUTDOWN_TIMEOUT_COMMAND,
2340 module AP_MODULE_DECLARE_DATA mpm_worker_module = {
2342 ap_mpm_rewrite_args, /* hook to run before apache parses args */
2343 NULL, /* create per-directory config structure */
2344 NULL, /* merge per-directory config structures */
2345 NULL, /* create per-server config structure */
2346 NULL, /* merge per-server config structures */
2347 worker_cmds, /* command apr_table_t */
2348 worker_hooks /* register_hooks */