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 thread_limit = 0;
124 static int dying = 0;
125 static int workers_may_exit = 0;
126 static int start_thread_may_exit = 0;
127 static int listener_may_exit = 0;
128 static int requests_this_child;
129 static int num_listensocks = 0;
130 static int resource_shortage = 0;
131 static fd_queue_t *worker_queue;
132 static fd_queue_info_t *worker_queue_info;
133 static int mpm_state = AP_MPMQ_STARTING;
134 static int sick_child_detected;
135 static ap_generation_t volatile my_generation = 0;
137 /* data retained by worker across load/unload of the module
138 * allocated on first call to pre-config hook; located on
139 * subsequent calls to pre-config hook
141 typedef struct worker_retained_data {
142 int first_server_limit;
143 int first_thread_limit;
145 } worker_retained_data;
146 static worker_retained_data *retained;
148 #define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
150 /* The structure used to pass unique initialization info to each thread */
157 /* Structure used to pass information to the thread responsible for
158 * creating the rest of the threads.
161 apr_thread_t **threads;
162 apr_thread_t *listener;
164 apr_threadattr_t *threadattr;
167 #define ID_FROM_CHILD_THREAD(c, t) ((c * thread_limit) + t)
170 * The max child slot ever assigned, preserved across restarts. Necessary
171 * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
172 * use this value to optimize routines that have to scan the entire
175 static int max_daemons_limit = -1;
177 static ap_worker_pod_t *pod;
179 /* The worker MPM respects a couple of runtime flags that can aid
180 * in debugging. Setting the -DNO_DETACH flag will prevent the root process
181 * from detaching from its controlling terminal. Additionally, setting
182 * the -DONE_PROCESS flag (which implies -DNO_DETACH) will get you the
183 * child_main loop running in the process which originally started up.
184 * This gives you a pretty nice debugging environment. (You'll get a SIGHUP
185 * early in standalone_main; just continue through. This is the server
186 * trying to kill off any child processes which it might have lying
187 * around --- Apache doesn't keep track of their pids, it just sends
188 * SIGHUP to the process group, ignoring it in the root process.
189 * Continue through and you'll be fine.).
192 static int one_process = 0;
195 int raise_sigstop_flags;
198 static apr_pool_t *pconf; /* Pool for config stuff */
199 static apr_pool_t *pchild; /* Pool for httpd child stuff */
201 static pid_t ap_my_pid; /* Linux getpid() doesn't work except in main
202 thread. Use this instead */
203 static pid_t parent_pid;
204 static apr_os_thread_t *listener_os_thread;
206 /* Locks for accept serialization */
207 static apr_proc_mutex_t *accept_mutex;
209 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
210 #define SAFE_ACCEPT(stmt) (ap_listeners->next ? (stmt) : APR_SUCCESS)
212 #define SAFE_ACCEPT(stmt) (stmt)
215 /* The LISTENER_SIGNAL signal will be sent from the main thread to the
216 * listener thread to wake it up for graceful termination (what a child
217 * process from an old generation does when the admin does "apachectl
218 * graceful"). This signal will be blocked in all threads of a child
219 * process except for the listener thread.
221 #define LISTENER_SIGNAL SIGHUP
223 /* The WORKER_SIGNAL signal will be sent from the main thread to the
224 * worker threads during an ungraceful restart or shutdown.
225 * This ensures that on systems (i.e., Linux) where closing the worker
226 * socket doesn't awake the worker thread when it is polling on the socket
227 * (especially in apr_wait_for_io_or_timeout() when handling
228 * Keep-Alive connections), close_worker_sockets() and join_workers()
229 * still function in timely manner and allow ungraceful shutdowns to
230 * proceed to completion. Otherwise join_workers() doesn't return
231 * before the main process decides the child process is non-responsive
232 * and sends a SIGKILL.
234 #define WORKER_SIGNAL AP_SIG_GRACEFUL
236 /* An array of socket descriptors in use by each thread used to
237 * perform a non-graceful (forced) shutdown of the server. */
238 static apr_socket_t **worker_sockets;
240 static void close_worker_sockets(void)
243 for (i = 0; i < threads_per_child; i++) {
244 if (worker_sockets[i]) {
245 apr_socket_close(worker_sockets[i]);
246 worker_sockets[i] = NULL;
251 static void wakeup_listener(void)
253 listener_may_exit = 1;
254 if (!listener_os_thread) {
255 /* XXX there is an obscure path that this doesn't handle perfectly:
256 * right after listener thread is created but before
257 * listener_os_thread is set, the first worker thread hits an
258 * error and starts graceful termination
263 /* unblock the listener if it's waiting for a worker */
264 ap_queue_info_term(worker_queue_info);
267 * we should just be able to "kill(ap_my_pid, LISTENER_SIGNAL)" on all
268 * platforms and wake up the listener thread since it is the only thread
269 * with SIGHUP unblocked, but that doesn't work on Linux
271 #ifdef HAVE_PTHREAD_KILL
272 pthread_kill(*listener_os_thread, LISTENER_SIGNAL);
274 kill(ap_my_pid, LISTENER_SIGNAL);
279 #define ST_GRACEFUL 1
280 #define ST_UNGRACEFUL 2
282 static int terminate_mode = ST_INIT;
284 static void signal_threads(int mode)
286 if (terminate_mode == mode) {
289 terminate_mode = mode;
290 mpm_state = AP_MPMQ_STOPPING;
292 /* in case we weren't called from the listener thread, wake up the
297 /* for ungraceful termination, let the workers exit now;
298 * for graceful termination, the listener thread will notify the
299 * workers to exit once it has stopped accepting new connections
301 if (mode == ST_UNGRACEFUL) {
302 workers_may_exit = 1;
303 ap_queue_interrupt_all(worker_queue);
304 close_worker_sockets(); /* forcefully kill all current connections */
308 static int worker_query(int query_code, int *result, apr_status_t *rv)
311 switch (query_code) {
312 case AP_MPMQ_MAX_DAEMON_USED:
313 *result = max_daemons_limit;
315 case AP_MPMQ_IS_THREADED:
316 *result = AP_MPMQ_STATIC;
318 case AP_MPMQ_IS_FORKED:
319 *result = AP_MPMQ_DYNAMIC;
321 case AP_MPMQ_HARD_LIMIT_DAEMONS:
322 *result = server_limit;
324 case AP_MPMQ_HARD_LIMIT_THREADS:
325 *result = thread_limit;
327 case AP_MPMQ_MAX_THREADS:
328 *result = threads_per_child;
330 case AP_MPMQ_MIN_SPARE_DAEMONS:
333 case AP_MPMQ_MIN_SPARE_THREADS:
334 *result = min_spare_threads;
336 case AP_MPMQ_MAX_SPARE_DAEMONS:
339 case AP_MPMQ_MAX_SPARE_THREADS:
340 *result = max_spare_threads;
342 case AP_MPMQ_MAX_REQUESTS_DAEMON:
343 *result = ap_max_requests_per_child;
345 case AP_MPMQ_MAX_DAEMONS:
346 *result = ap_daemons_limit;
348 case AP_MPMQ_MPM_STATE:
351 case AP_MPMQ_GENERATION:
352 *result = my_generation;
361 static apr_status_t worker_note_child_killed(int childnum)
363 ap_scoreboard_image->parent[childnum].pid = 0;
367 static const char *worker_get_name(void)
372 /* a clean exit from a child with proper cleanup */
373 static void clean_child_exit(int code) __attribute__ ((noreturn));
374 static void clean_child_exit(int code)
376 mpm_state = AP_MPMQ_STOPPING;
378 apr_pool_destroy(pchild);
383 static void just_die(int sig)
388 /*****************************************************************
389 * Connection structures and accounting...
392 /* volatile just in case */
393 static int volatile shutdown_pending;
394 static int volatile restart_pending;
395 static int volatile is_graceful;
396 static volatile int child_fatal;
399 * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
400 * functions to initiate shutdown or restart without relying on signals.
401 * Previously this was initiated in sig_term() and restart() signal handlers,
402 * but we want to be able to start a shutdown/restart from other sources --
403 * e.g. on Win32, from the service manager. Now the service manager can
404 * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
405 * these functions can also be called by the child processes, since global
406 * variables are no longer used to pass on the required action to the parent.
408 * These should only be called from the parent process itself, since the
409 * parent process will use the shutdown_pending and restart_pending variables
410 * to determine whether to shutdown or restart. The child process should
411 * call signal_parent() directly to tell the parent to die -- this will
412 * cause neither of those variable to be set, which the parent will
413 * assume means something serious is wrong (which it will be, for the
414 * child to force an exit) and so do an exit anyway.
417 static void ap_start_shutdown(int graceful)
419 mpm_state = AP_MPMQ_STOPPING;
420 if (shutdown_pending == 1) {
421 /* Um, is this _probably_ not an error, if the user has
422 * tried to do a shutdown twice quickly, so we won't
423 * worry about reporting it.
427 shutdown_pending = 1;
428 is_graceful = graceful;
431 /* do a graceful restart if graceful == 1 */
432 static void ap_start_restart(int graceful)
434 mpm_state = AP_MPMQ_STOPPING;
435 if (restart_pending == 1) {
436 /* Probably not an error - don't bother reporting it */
440 is_graceful = graceful;
443 static void sig_term(int sig)
445 ap_start_shutdown(sig == AP_SIG_GRACEFUL_STOP);
448 static void restart(int sig)
450 ap_start_restart(sig == AP_SIG_GRACEFUL);
453 static void set_signals(void)
455 #ifndef NO_USE_SIGACTION
460 ap_fatal_signal_setup(ap_server_conf, pconf);
463 #ifndef NO_USE_SIGACTION
464 sigemptyset(&sa.sa_mask);
467 sa.sa_handler = sig_term;
468 if (sigaction(SIGTERM, &sa, NULL) < 0)
469 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
470 "sigaction(SIGTERM)");
471 #ifdef AP_SIG_GRACEFUL_STOP
472 if (sigaction(AP_SIG_GRACEFUL_STOP, &sa, NULL) < 0)
473 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
474 "sigaction(" AP_SIG_GRACEFUL_STOP_STRING ")");
477 if (sigaction(SIGINT, &sa, NULL) < 0)
478 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
479 "sigaction(SIGINT)");
482 sa.sa_handler = SIG_DFL;
483 if (sigaction(SIGXCPU, &sa, NULL) < 0)
484 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
485 "sigaction(SIGXCPU)");
488 sa.sa_handler = SIG_DFL;
489 if (sigaction(SIGXFSZ, &sa, NULL) < 0)
490 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
491 "sigaction(SIGXFSZ)");
494 sa.sa_handler = SIG_IGN;
495 if (sigaction(SIGPIPE, &sa, NULL) < 0)
496 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
497 "sigaction(SIGPIPE)");
500 /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
502 sigaddset(&sa.sa_mask, SIGHUP);
503 sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
504 sa.sa_handler = restart;
505 if (sigaction(SIGHUP, &sa, NULL) < 0)
506 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
507 "sigaction(SIGHUP)");
508 if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
509 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
510 "sigaction(" AP_SIG_GRACEFUL_STRING ")");
514 apr_signal(SIGXCPU, SIG_DFL);
517 apr_signal(SIGXFSZ, SIG_DFL);
521 apr_signal(SIGTERM, sig_term);
523 apr_signal(SIGHUP, restart);
525 #ifdef AP_SIG_GRACEFUL
526 apr_signal(AP_SIG_GRACEFUL, restart);
527 #endif /* AP_SIG_GRACEFUL */
528 #ifdef AP_SIG_GRACEFUL_STOP
529 apr_signal(AP_SIG_GRACEFUL_STOP, sig_term);
530 #endif /* AP_SIG_GRACEFUL_STOP */
532 apr_signal(SIGPIPE, SIG_IGN);
538 /*****************************************************************
539 * Here follows a long bunch of generic server bookkeeping stuff...
542 /*****************************************************************
543 * Child process main loop.
546 static void process_socket(apr_thread_t *thd, apr_pool_t *p, apr_socket_t *sock,
548 int my_thread_num, apr_bucket_alloc_t *bucket_alloc)
550 conn_rec *current_conn;
551 long conn_id = ID_FROM_CHILD_THREAD(my_child_num, my_thread_num);
554 ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
556 current_conn = ap_run_create_connection(p, ap_server_conf, sock,
557 conn_id, sbh, bucket_alloc);
559 current_conn->current_thread = thd;
560 ap_process_connection(current_conn, sock);
561 ap_lingering_close(current_conn);
565 /* requests_this_child has gone to zero or below. See if the admin coded
566 "MaxRequestsPerChild 0", and keep going in that case. Doing it this way
567 simplifies the hot path in worker_thread */
568 static void check_infinite_requests(void)
570 if (ap_max_requests_per_child) {
571 signal_threads(ST_GRACEFUL);
574 requests_this_child = INT_MAX; /* keep going */
578 static void unblock_signal(int sig)
582 sigemptyset(&sig_mask);
583 sigaddset(&sig_mask, sig);
584 #if defined(SIGPROCMASK_SETS_THREAD_MASK)
585 sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);
587 pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL);
591 static void dummy_signal_handler(int sig)
593 /* XXX If specifying SIG_IGN is guaranteed to unblock a syscall,
594 * then we don't need this goofy function.
598 static void accept_mutex_error(char *func, apr_status_t rv, int process_slot)
600 int level = APLOG_EMERG;
602 if (ap_scoreboard_image->parent[process_slot].generation !=
603 ap_scoreboard_image->global->running_generation) {
604 level = APLOG_DEBUG; /* common to get these at restart time */
606 else if (requests_this_child == INT_MAX
607 || ((requests_this_child == ap_max_requests_per_child)
608 && ap_max_requests_per_child)) {
609 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
610 "apr_proc_mutex_%s failed "
611 "before this child process served any requests.",
613 clean_child_exit(APEXIT_CHILDSICK);
615 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
616 "apr_proc_mutex_%s failed. Attempting to "
617 "shutdown process gracefully.", func);
618 signal_threads(ST_GRACEFUL);
621 static void * APR_THREAD_FUNC listener_thread(apr_thread_t *thd, void * dummy)
623 proc_info * ti = dummy;
624 int process_slot = ti->pid;
625 apr_pool_t *tpool = apr_thread_pool_get(thd);
627 apr_pool_t *ptrans = NULL; /* Pool for per-transaction stuff */
628 apr_pollset_t *pollset;
631 int have_idle_worker = 0;
632 int last_poll_idx = 0;
636 rv = apr_pollset_create(&pollset, num_listensocks, tpool, 0);
637 if (rv != APR_SUCCESS) {
638 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
639 "Couldn't create pollset in thread;"
640 " check system or user limits");
641 /* let the parent decide how bad this really is */
642 clean_child_exit(APEXIT_CHILDSICK);
645 for (lr = ap_listeners; lr != NULL; lr = lr->next) {
646 apr_pollfd_t pfd = { 0 };
648 pfd.desc_type = APR_POLL_SOCKET;
650 pfd.reqevents = APR_POLLIN;
651 pfd.client_data = lr;
653 rv = apr_pollset_add(pollset, &pfd);
654 if (rv != APR_SUCCESS) {
655 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
656 "Couldn't create add listener to pollset;"
657 " check system or user limits");
658 /* let the parent decide how bad this really is */
659 clean_child_exit(APEXIT_CHILDSICK);
662 lr->accept_func = ap_unixd_accept;
665 /* Unblock the signal used to wake this thread up, and set a handler for
668 unblock_signal(LISTENER_SIGNAL);
669 apr_signal(LISTENER_SIGNAL, dummy_signal_handler);
671 /* TODO: Switch to a system where threads reuse the results from earlier
672 poll calls - manoj */
674 /* TODO: requests_this_child should be synchronized - aaron */
675 if (requests_this_child <= 0) {
676 check_infinite_requests();
678 if (listener_may_exit) break;
680 if (!have_idle_worker) {
681 /* the following pops a recycled ptrans pool off a stack
682 * if there is one, in addition to reserving a worker thread
684 rv = ap_queue_info_wait_for_idler(worker_queue_info,
686 if (APR_STATUS_IS_EOF(rv)) {
687 break; /* we've been signaled to die now */
689 else if (rv != APR_SUCCESS) {
690 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
691 "apr_queue_info_wait failed. Attempting to "
692 " shutdown process gracefully.");
693 signal_threads(ST_GRACEFUL);
696 have_idle_worker = 1;
699 /* We've already decremented the idle worker count inside
700 * ap_queue_info_wait_for_idler. */
702 if ((rv = SAFE_ACCEPT(apr_proc_mutex_lock(accept_mutex)))
705 if (!listener_may_exit) {
706 accept_mutex_error("lock", rv, process_slot);
708 break; /* skip the lock release */
711 if (!ap_listeners->next) {
712 /* Only one listener, so skip the poll */
716 while (!listener_may_exit) {
718 const apr_pollfd_t *pdesc;
720 rv = apr_pollset_poll(pollset, -1, &numdesc, &pdesc);
721 if (rv != APR_SUCCESS) {
722 if (APR_STATUS_IS_EINTR(rv)) {
726 /* apr_pollset_poll() will only return errors in catastrophic
727 * circumstances. Let's try exiting gracefully, for now. */
728 ap_log_error(APLOG_MARK, APLOG_ERR, rv,
729 (const server_rec *) ap_server_conf,
730 "apr_pollset_poll: (listen)");
731 signal_threads(ST_GRACEFUL);
734 if (listener_may_exit) break;
736 /* We can always use pdesc[0], but sockets at position N
737 * could end up completely starved of attention in a very
738 * busy server. Therefore, we round-robin across the
739 * returned set of descriptors. While it is possible that
740 * the returned set of descriptors might flip around and
741 * continue to starve some sockets, we happen to know the
742 * internal pollset implementation retains ordering
743 * stability of the sockets. Thus, the round-robin should
744 * ensure that a socket will eventually be serviced.
746 if (last_poll_idx >= numdesc)
749 /* Grab a listener record from the client_data of the poll
750 * descriptor, and advance our saved index to round-robin
753 * ### hmm... this descriptor might have POLLERR rather
756 lr = pdesc[last_poll_idx++].client_data;
763 if (!listener_may_exit) {
764 if (ptrans == NULL) {
765 /* we can't use a recycled transaction pool this time.
766 * create a new transaction pool */
767 apr_allocator_t *allocator;
769 apr_allocator_create(&allocator);
770 apr_allocator_max_free_set(allocator, ap_max_mem_free);
771 apr_pool_create_ex(&ptrans, pconf, NULL, allocator);
772 apr_allocator_owner_set(allocator, ptrans);
774 apr_pool_tag(ptrans, "transaction");
775 rv = lr->accept_func(&csd, lr, ptrans);
776 /* later we trash rv and rely on csd to indicate success/failure */
777 AP_DEBUG_ASSERT(rv == APR_SUCCESS || !csd);
779 if (rv == APR_EGENERAL) {
780 /* E[NM]FILE, ENOMEM, etc */
781 resource_shortage = 1;
782 signal_threads(ST_GRACEFUL);
784 if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
787 if (listener_may_exit) {
790 accept_mutex_error("unlock", rv, process_slot);
793 rv = ap_queue_push(worker_queue, csd, ptrans);
795 /* trash the connection; we couldn't queue the connected
798 apr_socket_close(csd);
799 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
800 "ap_queue_push failed");
803 have_idle_worker = 0;
808 if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
810 int level = APLOG_EMERG;
812 if (ap_scoreboard_image->parent[process_slot].generation !=
813 ap_scoreboard_image->global->running_generation) {
814 level = APLOG_DEBUG; /* common to get these at restart time */
816 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
817 "apr_proc_mutex_unlock failed. Attempting to "
818 "shutdown process gracefully.");
819 signal_threads(ST_GRACEFUL);
825 ap_close_listeners();
826 ap_queue_term(worker_queue);
828 ap_scoreboard_image->parent[process_slot].quiescing = 1;
830 /* wake up the main thread */
831 kill(ap_my_pid, SIGTERM);
833 apr_thread_exit(thd, APR_SUCCESS);
837 /* XXX For ungraceful termination/restart, we definitely don't want to
838 * wait for active connections to finish but we may want to wait
839 * for idle workers to get out of the queue code and release mutexes,
840 * since those mutexes are cleaned up pretty soon and some systems
841 * may not react favorably (i.e., segfault) if operations are attempted
842 * on cleaned-up mutexes.
844 static void * APR_THREAD_FUNC worker_thread(apr_thread_t *thd, void * dummy)
846 proc_info * ti = dummy;
847 int process_slot = ti->pid;
848 int thread_slot = ti->tid;
849 apr_socket_t *csd = NULL;
850 apr_bucket_alloc_t *bucket_alloc;
851 apr_pool_t *last_ptrans = NULL;
852 apr_pool_t *ptrans; /* Pool for per-transaction stuff */
858 ap_scoreboard_image->servers[process_slot][thread_slot].pid = ap_my_pid;
859 ap_scoreboard_image->servers[process_slot][thread_slot].tid = apr_os_thread_current();
860 ap_scoreboard_image->servers[process_slot][thread_slot].generation = my_generation;
861 ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_STARTING, NULL);
863 #ifdef HAVE_PTHREAD_KILL
864 unblock_signal(WORKER_SIGNAL);
865 apr_signal(WORKER_SIGNAL, dummy_signal_handler);
868 while (!workers_may_exit) {
870 rv = ap_queue_info_set_idle(worker_queue_info, last_ptrans);
872 if (rv != APR_SUCCESS) {
873 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
874 "ap_queue_info_set_idle failed. Attempting to "
875 "shutdown process gracefully.");
876 signal_threads(ST_GRACEFUL);
882 ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_READY, NULL);
884 if (workers_may_exit) {
887 rv = ap_queue_pop(worker_queue, &csd, &ptrans);
889 if (rv != APR_SUCCESS) {
890 /* We get APR_EOF during a graceful shutdown once all the connections
891 * accepted by this server process have been handled.
893 if (APR_STATUS_IS_EOF(rv)) {
896 /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
897 * from an explicit call to ap_queue_interrupt_all(). This allows
898 * us to unblock threads stuck in ap_queue_pop() when a shutdown
901 * If workers_may_exit is set and this is ungraceful termination/
902 * restart, we are bound to get an error on some systems (e.g.,
903 * AIX, which sanity-checks mutex operations) since the queue
904 * may have already been cleaned up. Don't log the "error" if
905 * workers_may_exit is set.
907 else if (APR_STATUS_IS_EINTR(rv)) {
910 /* We got some other error. */
911 else if (!workers_may_exit) {
912 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
913 "ap_queue_pop failed");
918 worker_sockets[thread_slot] = csd;
919 bucket_alloc = apr_bucket_alloc_create(ptrans);
920 process_socket(thd, ptrans, csd, process_slot, thread_slot, bucket_alloc);
921 worker_sockets[thread_slot] = NULL;
922 requests_this_child--;
923 apr_pool_clear(ptrans);
924 last_ptrans = ptrans;
927 ap_update_child_status_from_indexes(process_slot, thread_slot,
928 (dying) ? SERVER_DEAD : SERVER_GRACEFUL, (request_rec *) NULL);
930 apr_thread_exit(thd, APR_SUCCESS);
934 static int check_signal(int signum)
944 static void create_listener_thread(thread_starter *ts)
946 int my_child_num = ts->child_num_arg;
947 apr_threadattr_t *thread_attr = ts->threadattr;
951 my_info = (proc_info *)malloc(sizeof(proc_info));
952 my_info->pid = my_child_num;
953 my_info->tid = -1; /* listener thread doesn't have a thread slot */
955 rv = apr_thread_create(&ts->listener, thread_attr, listener_thread,
957 if (rv != APR_SUCCESS) {
958 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
959 "apr_thread_create: unable to create listener thread");
960 /* let the parent decide how bad this really is */
961 clean_child_exit(APEXIT_CHILDSICK);
963 apr_os_thread_get(&listener_os_thread, ts->listener);
966 /* XXX under some circumstances not understood, children can get stuck
967 * in start_threads forever trying to take over slots which will
968 * never be cleaned up; for now there is an APLOG_DEBUG message issued
969 * every so often when this condition occurs
971 static void * APR_THREAD_FUNC start_threads(apr_thread_t *thd, void *dummy)
973 thread_starter *ts = dummy;
974 apr_thread_t **threads = ts->threads;
975 apr_threadattr_t *thread_attr = ts->threadattr;
976 int child_num_arg = ts->child_num_arg;
977 int my_child_num = child_num_arg;
981 int threads_created = 0;
982 int listener_started = 0;
984 int prev_threads_created;
986 /* We must create the fd queues before we start up the listener
987 * and worker threads. */
988 worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
989 rv = ap_queue_init(worker_queue, threads_per_child, pchild);
990 if (rv != APR_SUCCESS) {
991 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
992 "ap_queue_init() failed");
993 clean_child_exit(APEXIT_CHILDFATAL);
996 rv = ap_queue_info_create(&worker_queue_info, pchild,
998 if (rv != APR_SUCCESS) {
999 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1000 "ap_queue_info_create() failed");
1001 clean_child_exit(APEXIT_CHILDFATAL);
1004 worker_sockets = apr_pcalloc(pchild, threads_per_child
1005 * sizeof(apr_socket_t *));
1007 loops = prev_threads_created = 0;
1009 /* threads_per_child does not include the listener thread */
1010 for (i = 0; i < threads_per_child; i++) {
1011 int status = ap_scoreboard_image->servers[child_num_arg][i].status;
1013 if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
1017 my_info = (proc_info *)malloc(sizeof(proc_info));
1018 if (my_info == NULL) {
1019 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
1020 "malloc: out of memory");
1021 clean_child_exit(APEXIT_CHILDFATAL);
1023 my_info->pid = my_child_num;
1027 /* We are creating threads right now */
1028 ap_update_child_status_from_indexes(my_child_num, i,
1029 SERVER_STARTING, NULL);
1030 /* We let each thread update its own scoreboard entry. This is
1031 * done because it lets us deal with tid better.
1033 rv = apr_thread_create(&threads[i], thread_attr,
1034 worker_thread, my_info, pchild);
1035 if (rv != APR_SUCCESS) {
1036 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1037 "apr_thread_create: unable to create worker thread");
1038 /* let the parent decide how bad this really is */
1039 clean_child_exit(APEXIT_CHILDSICK);
1043 /* Start the listener only when there are workers available */
1044 if (!listener_started && threads_created) {
1045 create_listener_thread(ts);
1046 listener_started = 1;
1048 if (start_thread_may_exit || threads_created == threads_per_child) {
1051 /* wait for previous generation to clean up an entry */
1052 apr_sleep(apr_time_from_sec(1));
1054 if (loops % 120 == 0) { /* every couple of minutes */
1055 if (prev_threads_created == threads_created) {
1056 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1057 "child %" APR_PID_T_FMT " isn't taking over "
1058 "slots very quickly (%d of %d)",
1059 ap_my_pid, threads_created, threads_per_child);
1061 prev_threads_created = threads_created;
1065 /* What state should this child_main process be listed as in the
1067 * ap_update_child_status_from_indexes(my_child_num, i, SERVER_STARTING,
1068 * (request_rec *) NULL);
1070 * This state should be listed separately in the scoreboard, in some kind
1071 * of process_status, not mixed in with the worker threads' status.
1072 * "life_status" is almost right, but it's in the worker's structure, and
1073 * the name could be clearer. gla
1075 apr_thread_exit(thd, APR_SUCCESS);
1079 static void join_workers(apr_thread_t *listener, apr_thread_t **threads)
1082 apr_status_t rv, thread_rv;
1087 /* deal with a rare timing window which affects waking up the
1088 * listener thread... if the signal sent to the listener thread
1089 * is delivered between the time it verifies that the
1090 * listener_may_exit flag is clear and the time it enters a
1091 * blocking syscall, the signal didn't do any good... work around
1092 * that by sleeping briefly and sending it again
1097 #ifdef HAVE_PTHREAD_KILL
1098 pthread_kill(*listener_os_thread, 0)
1103 /* listener not dead yet */
1104 apr_sleep(apr_time_make(0, 500000));
1109 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1110 "the listener thread didn't exit");
1113 rv = apr_thread_join(&thread_rv, listener);
1114 if (rv != APR_SUCCESS) {
1115 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1116 "apr_thread_join: unable to join listener thread");
1121 for (i = 0; i < threads_per_child; i++) {
1122 if (threads[i]) { /* if we ever created this thread */
1123 #ifdef HAVE_PTHREAD_KILL
1124 apr_os_thread_t *worker_os_thread;
1126 apr_os_thread_get(&worker_os_thread, threads[i]);
1127 pthread_kill(*worker_os_thread, WORKER_SIGNAL);
1130 rv = apr_thread_join(&thread_rv, threads[i]);
1131 if (rv != APR_SUCCESS) {
1132 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1133 "apr_thread_join: unable to join worker "
1141 static void join_start_thread(apr_thread_t *start_thread_id)
1143 apr_status_t rv, thread_rv;
1145 start_thread_may_exit = 1; /* tell it to give up in case it is still
1146 * trying to take over slots from a
1147 * previous generation
1149 rv = apr_thread_join(&thread_rv, start_thread_id);
1150 if (rv != APR_SUCCESS) {
1151 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1152 "apr_thread_join: unable to join the start "
1157 static void child_main(int child_num_arg)
1159 apr_thread_t **threads;
1162 apr_threadattr_t *thread_attr;
1163 apr_thread_t *start_thread_id;
1165 mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
1168 ap_my_pid = getpid();
1169 ap_fatal_signal_child_setup(ap_server_conf);
1170 apr_pool_create(&pchild, pconf);
1172 /*stuff to do before we switch id's, so we have permissions.*/
1173 ap_reopen_scoreboard(pchild, NULL, 0);
1175 rv = SAFE_ACCEPT(apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname,
1177 if (rv != APR_SUCCESS) {
1178 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1179 "Couldn't initialize cross-process lock in child");
1180 clean_child_exit(APEXIT_CHILDFATAL);
1183 if (ap_run_drop_privileges(pchild, ap_server_conf)) {
1184 clean_child_exit(APEXIT_CHILDFATAL);
1187 ap_run_child_init(pchild, ap_server_conf);
1189 /* done with init critical section */
1191 /* Just use the standard apr_setup_signal_thread to block all signals
1192 * from being received. The child processes no longer use signals for
1193 * any communication with the parent process.
1195 rv = apr_setup_signal_thread();
1196 if (rv != APR_SUCCESS) {
1197 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1198 "Couldn't initialize signal thread");
1199 clean_child_exit(APEXIT_CHILDFATAL);
1202 if (ap_max_requests_per_child) {
1203 requests_this_child = ap_max_requests_per_child;
1206 /* coding a value of zero means infinity */
1207 requests_this_child = INT_MAX;
1210 /* Setup worker threads */
1212 /* clear the storage; we may not create all our threads immediately,
1213 * and we want a 0 entry to indicate a thread which was not created
1215 threads = (apr_thread_t **)calloc(1,
1216 sizeof(apr_thread_t *) * threads_per_child);
1217 if (threads == NULL) {
1218 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
1219 "malloc: out of memory");
1220 clean_child_exit(APEXIT_CHILDFATAL);
1223 ts = (thread_starter *)apr_palloc(pchild, sizeof(*ts));
1225 apr_threadattr_create(&thread_attr, pchild);
1226 /* 0 means PTHREAD_CREATE_JOINABLE */
1227 apr_threadattr_detach_set(thread_attr, 0);
1229 if (ap_thread_stacksize != 0) {
1230 apr_threadattr_stacksize_set(thread_attr, ap_thread_stacksize);
1233 ts->threads = threads;
1234 ts->listener = NULL;
1235 ts->child_num_arg = child_num_arg;
1236 ts->threadattr = thread_attr;
1238 rv = apr_thread_create(&start_thread_id, thread_attr, start_threads,
1240 if (rv != APR_SUCCESS) {
1241 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1242 "apr_thread_create: unable to create worker thread");
1243 /* let the parent decide how bad this really is */
1244 clean_child_exit(APEXIT_CHILDSICK);
1247 mpm_state = AP_MPMQ_RUNNING;
1249 /* If we are only running in one_process mode, we will want to
1250 * still handle signals. */
1252 /* Block until we get a terminating signal. */
1253 apr_signal_thread(check_signal);
1254 /* make sure the start thread has finished; signal_threads()
1255 * and join_workers() depend on that
1257 /* XXX join_start_thread() won't be awakened if one of our
1258 * threads encounters a critical error and attempts to
1259 * shutdown this child
1261 join_start_thread(start_thread_id);
1262 signal_threads(ST_UNGRACEFUL); /* helps us terminate a little more
1263 * quickly than the dispatch of the signal thread
1264 * beats the Pipe of Death and the browsers
1266 /* A terminating signal was received. Now join each of the
1267 * workers to clean them up.
1268 * If the worker already exited, then the join frees
1269 * their resources and returns.
1270 * If the worker hasn't exited, then this blocks until
1271 * they have (then cleans up).
1273 join_workers(ts->listener, threads);
1275 else { /* !one_process */
1276 /* remove SIGTERM from the set of blocked signals... if one of
1277 * the other threads in the process needs to take us down
1278 * (e.g., for MaxRequestsPerChild) it will send us SIGTERM
1280 unblock_signal(SIGTERM);
1281 apr_signal(SIGTERM, dummy_signal_handler);
1282 /* Watch for any messages from the parent over the POD */
1284 rv = ap_worker_pod_check(pod);
1285 if (rv == AP_NORESTART) {
1286 /* see if termination was triggered while we slept */
1287 switch(terminate_mode) {
1296 if (rv == AP_GRACEFUL || rv == AP_RESTART) {
1297 /* make sure the start thread has finished;
1298 * signal_threads() and join_workers depend on that
1300 join_start_thread(start_thread_id);
1301 signal_threads(rv == AP_GRACEFUL ? ST_GRACEFUL : ST_UNGRACEFUL);
1306 /* A terminating signal was received. Now join each of the
1307 * workers to clean them up.
1308 * If the worker already exited, then the join frees
1309 * their resources and returns.
1310 * If the worker hasn't exited, then this blocks until
1311 * they have (then cleans up).
1313 join_workers(ts->listener, threads);
1318 clean_child_exit(resource_shortage ? APEXIT_CHILDSICK : 0);
1321 static int make_child(server_rec *s, int slot)
1325 if (slot + 1 > max_daemons_limit) {
1326 max_daemons_limit = slot + 1;
1331 ap_scoreboard_image->parent[slot].pid = getpid();
1335 if ((pid = fork()) == -1) {
1336 ap_log_error(APLOG_MARK, APLOG_ERR, errno, s,
1337 "fork: Unable to fork new process");
1338 /* fork didn't succeed. There's no need to touch the scoreboard;
1339 * if we were trying to replace a failed child process, then
1340 * server_main_loop() marked its workers SERVER_DEAD, and if
1341 * we were trying to replace a child process that exited normally,
1342 * its worker_thread()s left SERVER_DEAD or SERVER_GRACEFUL behind.
1345 /* In case system resources are maxxed out, we don't want
1346 Apache running away with the CPU trying to fork over and
1347 over and over again. */
1348 apr_sleep(apr_time_from_sec(10));
1354 #ifdef HAVE_BINDPROCESSOR
1355 /* By default, AIX binds to a single processor. This bit unbinds
1356 * children which will then bind to another CPU.
1358 int status = bindprocessor(BINDPROCESS, (int)getpid(),
1359 PROCESSOR_CLASS_ANY);
1361 ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
1363 "processor unbind failed %d", status);
1365 RAISE_SIGSTOP(MAKE_CHILD);
1367 apr_signal(SIGTERM, just_die);
1370 clean_child_exit(0);
1373 if (ap_scoreboard_image->parent[slot].pid != 0) {
1374 /* This new child process is squatting on the scoreboard
1375 * entry owned by an exiting child process, which cannot
1376 * exit until all active requests complete.
1377 * Don't forget about this exiting child process, or we
1378 * won't be able to kill it if it doesn't exit by the
1379 * time the server is shut down.
1381 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1382 "taking over scoreboard slot from %" APR_PID_T_FMT "%s",
1383 ap_scoreboard_image->parent[slot].pid,
1384 ap_scoreboard_image->parent[slot].quiescing ?
1385 " (quiescing)" : "");
1386 ap_register_extra_mpm_process(ap_scoreboard_image->parent[slot].pid);
1388 ap_scoreboard_image->parent[slot].quiescing = 0;
1389 ap_scoreboard_image->parent[slot].pid = pid;
1393 /* start up a bunch of children */
1394 static void startup_children(int number_to_start)
1398 for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
1399 if (ap_scoreboard_image->parent[i].pid != 0) {
1402 if (make_child(ap_server_conf, i) < 0) {
1411 * idle_spawn_rate is the number of children that will be spawned on the
1412 * next maintenance cycle if there aren't enough idle servers. It is
1413 * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
1414 * without the need to spawn.
1416 static int idle_spawn_rate = 1;
1417 #ifndef MAX_SPAWN_RATE
1418 #define MAX_SPAWN_RATE (32)
1420 static int hold_off_on_exponential_spawning;
1422 static void perform_idle_server_maintenance(void)
1425 int idle_thread_count;
1429 int totally_free_length = 0;
1430 int free_slots[MAX_SPAWN_RATE];
1433 int active_thread_count = 0;
1435 /* initialize the free_list */
1438 idle_thread_count = 0;
1442 for (i = 0; i < ap_daemons_limit; ++i) {
1443 /* Initialization to satisfy the compiler. It doesn't know
1444 * that threads_per_child is always > 0 */
1445 int status = SERVER_DEAD;
1446 int any_dying_threads = 0;
1447 int any_dead_threads = 0;
1448 int all_dead_threads = 1;
1450 if (i >= max_daemons_limit && totally_free_length == idle_spawn_rate)
1451 /* short cut if all active processes have been examined and
1452 * enough empty scoreboard slots have been found
1455 ps = &ap_scoreboard_image->parent[i];
1456 for (j = 0; j < threads_per_child; j++) {
1457 ws = &ap_scoreboard_image->servers[i][j];
1458 status = ws->status;
1460 /* XXX any_dying_threads is probably no longer needed GLA */
1461 any_dying_threads = any_dying_threads ||
1462 (status == SERVER_GRACEFUL);
1463 any_dead_threads = any_dead_threads || (status == SERVER_DEAD);
1464 all_dead_threads = all_dead_threads &&
1465 (status == SERVER_DEAD ||
1466 status == SERVER_GRACEFUL);
1468 /* We consider a starting server as idle because we started it
1469 * at least a cycle ago, and if it still hasn't finished starting
1470 * then we're just going to swamp things worse by forking more.
1471 * So we hopefully won't need to fork more if we count it.
1472 * This depends on the ordering of SERVER_READY and SERVER_STARTING.
1474 if (ps->pid != 0) { /* XXX just set all_dead_threads in outer for
1475 loop if no pid? not much else matters */
1476 if (status <= SERVER_READY &&
1478 ps->generation == my_generation) {
1479 ++idle_thread_count;
1481 if (status >= SERVER_READY && status < SERVER_GRACEFUL) {
1482 ++active_thread_count;
1486 if (any_dead_threads && totally_free_length < idle_spawn_rate
1487 && free_length < MAX_SPAWN_RATE
1488 && (!ps->pid /* no process in the slot */
1489 || ps->quiescing)) { /* or at least one is going away */
1490 if (all_dead_threads) {
1491 /* great! we prefer these, because the new process can
1492 * start more threads sooner. So prioritize this slot
1493 * by putting it ahead of any slots with active threads.
1495 * first, make room by moving a slot that's potentially still
1496 * in use to the end of the array
1498 free_slots[free_length] = free_slots[totally_free_length];
1499 free_slots[totally_free_length++] = i;
1502 /* slot is still in use - back of the bus
1504 free_slots[free_length] = i;
1508 /* XXX if (!ps->quiescing) is probably more reliable GLA */
1509 if (!any_dying_threads) {
1515 if (sick_child_detected) {
1516 if (active_thread_count > 0) {
1517 /* some child processes appear to be working. don't kill the
1520 sick_child_detected = 0;
1523 /* looks like a basket case. give up.
1525 shutdown_pending = 1;
1527 ap_log_error(APLOG_MARK, APLOG_ALERT, 0,
1529 "No active workers found..."
1530 " Apache is exiting!");
1531 /* the child already logged the failure details */
1536 max_daemons_limit = last_non_dead + 1;
1538 if (idle_thread_count > max_spare_threads) {
1539 /* Kill off one child */
1540 ap_worker_pod_signal(pod, TRUE);
1541 idle_spawn_rate = 1;
1543 else if (idle_thread_count < min_spare_threads) {
1544 /* terminate the free list */
1545 if (free_length == 0) { /* scoreboard is full, can't fork */
1547 if (active_thread_count >= ap_daemons_limit * threads_per_child) {
1548 static int reported = 0;
1550 /* only report this condition once */
1551 ap_log_error(APLOG_MARK, APLOG_ERR, 0,
1553 "server reached MaxClients setting, consider"
1554 " raising the MaxClients setting");
1559 ap_log_error(APLOG_MARK, APLOG_ERR, 0,
1561 "scoreboard is full, not at MaxClients");
1563 idle_spawn_rate = 1;
1566 if (free_length > idle_spawn_rate) {
1567 free_length = idle_spawn_rate;
1569 if (idle_spawn_rate >= 8) {
1570 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1572 "server seems busy, (you may need "
1573 "to increase StartServers, ThreadsPerChild "
1574 "or Min/MaxSpareThreads), "
1575 "spawning %d children, there are around %d idle "
1576 "threads, and %d total children", free_length,
1577 idle_thread_count, total_non_dead);
1579 for (i = 0; i < free_length; ++i) {
1580 make_child(ap_server_conf, free_slots[i]);
1582 /* the next time around we want to spawn twice as many if this
1583 * wasn't good enough, but not if we've just done a graceful
1585 if (hold_off_on_exponential_spawning) {
1586 --hold_off_on_exponential_spawning;
1588 else if (idle_spawn_rate < MAX_SPAWN_RATE) {
1589 idle_spawn_rate *= 2;
1594 idle_spawn_rate = 1;
1598 static void server_main_loop(int remaining_children_to_start)
1601 apr_exit_why_e exitwhy;
1602 int status, processed_status;
1606 while (!restart_pending && !shutdown_pending) {
1607 ap_wait_or_timeout(&exitwhy, &status, &pid, pconf, ap_server_conf);
1609 if (pid.pid != -1) {
1610 processed_status = ap_process_child_status(&pid, exitwhy, status);
1611 if (processed_status == APEXIT_CHILDFATAL) {
1612 shutdown_pending = 1;
1616 else if (processed_status == APEXIT_CHILDSICK) {
1617 /* tell perform_idle_server_maintenance to check into this
1618 * on the next timer pop
1620 sick_child_detected = 1;
1622 /* non-fatal death... note that it's gone in the scoreboard. */
1623 child_slot = ap_find_child_by_pid(&pid);
1624 if (child_slot >= 0) {
1625 for (i = 0; i < threads_per_child; i++)
1626 ap_update_child_status_from_indexes(child_slot, i, SERVER_DEAD,
1627 (request_rec *) NULL);
1629 ap_scoreboard_image->parent[child_slot].pid = 0;
1630 ap_scoreboard_image->parent[child_slot].quiescing = 0;
1631 if (processed_status == APEXIT_CHILDSICK) {
1632 /* resource shortage, minimize the fork rate */
1633 idle_spawn_rate = 1;
1635 else if (remaining_children_to_start
1636 && child_slot < ap_daemons_limit) {
1637 /* we're still doing a 1-for-1 replacement of dead
1638 * children with new children
1640 make_child(ap_server_conf, child_slot);
1641 --remaining_children_to_start;
1644 else if (ap_unregister_extra_mpm_process(pid.pid) == 1) {
1646 #if APR_HAS_OTHER_CHILD
1648 else if (apr_proc_other_child_alert(&pid, APR_OC_REASON_DEATH,
1653 else if (is_graceful) {
1654 /* Great, we've probably just lost a slot in the
1655 * scoreboard. Somehow we don't know about this child.
1657 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
1659 "long lost child came home! (pid %ld)",
1662 /* Don't perform idle maintenance when a child dies,
1663 * only do it when there's a timeout. Remember only a
1664 * finite number of children can die, and it's pretty
1665 * pathological for a lot to die suddenly.
1669 else if (remaining_children_to_start) {
1670 /* we hit a 1 second timeout in which none of the previous
1671 * generation of children needed to be reaped... so assume
1672 * they're all done, and pick up the slack if any is left.
1674 startup_children(remaining_children_to_start);
1675 remaining_children_to_start = 0;
1676 /* In any event we really shouldn't do the code below because
1677 * few of the servers we just started are in the IDLE state
1678 * yet, so we'd mistakenly create an extra server.
1683 perform_idle_server_maintenance();
1687 static int worker_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
1689 int remaining_children_to_start;
1692 ap_log_pid(pconf, ap_pid_fname);
1694 /* Initialize cross-process accept lock */
1695 ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
1696 ap_server_root_relative(_pconf, ap_lock_fname),
1699 rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname,
1700 ap_accept_lock_mech, _pconf);
1701 if (rv != APR_SUCCESS) {
1702 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1703 "Couldn't create accept lock");
1704 mpm_state = AP_MPMQ_STOPPING;
1708 #if APR_USE_SYSVSEM_SERIALIZE
1709 if (ap_accept_lock_mech == APR_LOCK_DEFAULT ||
1710 ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1712 if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1714 rv = ap_unixd_set_proc_mutex_perms(accept_mutex);
1715 if (rv != APR_SUCCESS) {
1716 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1717 "Couldn't set permissions on cross-process lock; "
1718 "check User and Group directives");
1719 mpm_state = AP_MPMQ_STOPPING;
1725 if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
1726 mpm_state = AP_MPMQ_STOPPING;
1729 /* fix the generation number in the global score; we just got a new,
1730 * cleared scoreboard
1732 ap_scoreboard_image->global->running_generation = my_generation;
1736 /* Don't thrash... */
1737 if (max_spare_threads < min_spare_threads + threads_per_child)
1738 max_spare_threads = min_spare_threads + threads_per_child;
1740 /* If we're doing a graceful_restart then we're going to see a lot
1741 * of children exiting immediately when we get into the main loop
1742 * below (because we just sent them AP_SIG_GRACEFUL). This happens pretty
1743 * rapidly... and for each one that exits we may start a new one, until
1744 * there are at least min_spare_threads idle threads, counting across
1745 * all children. But we may be permitted to start more children than
1746 * that, so we'll just keep track of how many we're
1747 * supposed to start up without the 1 second penalty between each fork.
1749 remaining_children_to_start = ap_daemons_to_start;
1750 if (remaining_children_to_start > ap_daemons_limit) {
1751 remaining_children_to_start = ap_daemons_limit;
1754 startup_children(remaining_children_to_start);
1755 remaining_children_to_start = 0;
1758 /* give the system some time to recover before kicking into
1759 * exponential mode */
1760 hold_off_on_exponential_spawning = 10;
1763 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1764 "%s configured -- resuming normal operations",
1765 ap_get_server_description());
1766 ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
1767 "Server built: %s", ap_get_server_built());
1768 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1769 "AcceptMutex: %s (default: %s)",
1770 apr_proc_mutex_name(accept_mutex),
1771 apr_proc_mutex_defname());
1772 restart_pending = shutdown_pending = 0;
1773 mpm_state = AP_MPMQ_RUNNING;
1775 server_main_loop(remaining_children_to_start);
1776 mpm_state = AP_MPMQ_STOPPING;
1778 if (shutdown_pending && !is_graceful) {
1779 /* Time to shut down:
1780 * Kill child processes, tell them to call child_exit, etc...
1782 ap_worker_pod_killpg(pod, ap_daemons_limit, FALSE);
1783 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1786 /* cleanup pid file on normal shutdown */
1787 const char *pidfile = NULL;
1788 pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1789 if ( pidfile != NULL && unlink(pidfile) == 0)
1790 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1792 "removed PID file %s (pid=%" APR_PID_T_FMT ")",
1795 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
1796 ap_server_conf, "caught SIGTERM, shutting down");
1799 } else if (shutdown_pending) {
1800 /* Time to gracefully shut down:
1801 * Kill child processes, tell them to call child_exit, etc...
1803 int active_children;
1805 apr_time_t cutoff = 0;
1807 /* Close our listeners, and then ask our children to do same */
1808 ap_close_listeners();
1809 ap_worker_pod_killpg(pod, ap_daemons_limit, TRUE);
1810 ap_relieve_child_processes();
1813 /* cleanup pid file on normal shutdown */
1814 const char *pidfile = NULL;
1815 pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1816 if ( pidfile != NULL && unlink(pidfile) == 0)
1817 ap_log_error(APLOG_MARK, APLOG_INFO, 0,
1819 "removed PID file %s (pid=%" APR_PID_T_FMT ")",
1822 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1823 "caught " AP_SIG_GRACEFUL_STOP_STRING
1824 ", shutting down gracefully");
1827 if (ap_graceful_shutdown_timeout) {
1828 cutoff = apr_time_now() +
1829 apr_time_from_sec(ap_graceful_shutdown_timeout);
1832 /* Don't really exit until each child has finished */
1833 shutdown_pending = 0;
1835 /* Pause for a second */
1836 apr_sleep(apr_time_from_sec(1));
1838 /* Relieve any children which have now exited */
1839 ap_relieve_child_processes();
1841 active_children = 0;
1842 for (index = 0; index < ap_daemons_limit; ++index) {
1843 if (ap_mpm_safe_kill(MPM_CHILD_PID(index), 0) == APR_SUCCESS) {
1844 active_children = 1;
1845 /* Having just one child is enough to stay around */
1849 } while (!shutdown_pending && active_children &&
1850 (!ap_graceful_shutdown_timeout || apr_time_now() < cutoff));
1852 /* We might be here because we received SIGTERM, either
1853 * way, try and make sure that all of our processes are
1856 ap_worker_pod_killpg(pod, ap_daemons_limit, FALSE);
1857 ap_reclaim_child_processes(1);
1862 /* we've been told to restart */
1863 apr_signal(SIGHUP, SIG_IGN);
1866 /* not worth thinking about */
1870 /* advance to the next generation */
1871 /* XXX: we really need to make sure this new generation number isn't in
1872 * use by any of the children.
1875 ap_scoreboard_image->global->running_generation = my_generation;
1878 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1879 AP_SIG_GRACEFUL_STRING " received. Doing graceful restart");
1880 /* wake up the children...time to die. But we'll have more soon */
1881 ap_worker_pod_killpg(pod, ap_daemons_limit, TRUE);
1884 /* This is mostly for debugging... so that we know what is still
1885 * gracefully dealing with existing request.
1890 /* Kill 'em all. Since the child acts the same on the parents SIGTERM
1891 * and a SIGHUP, we may as well use the same signal, because some user
1892 * pthreads are stealing signals from us left and right.
1894 ap_worker_pod_killpg(pod, ap_daemons_limit, FALSE);
1896 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1897 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
1898 "SIGHUP received. Attempting to restart");
1904 /* This really should be a post_config hook, but the error log is already
1905 * redirected by that point, so we need to do this in the open_logs phase.
1907 static int worker_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
1910 int level_flags = 0;
1915 /* the reverse of pre_config, we want this only the first time around */
1916 if (retained->module_loads == 1) {
1918 level_flags |= APLOG_STARTUP;
1921 if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
1922 ap_log_error(APLOG_MARK, APLOG_ALERT | level_flags, 0,
1923 (startup ? NULL : s),
1924 "no listening sockets available, shutting down");
1929 if ((rv = ap_worker_pod_open(pconf, &pod))) {
1930 ap_log_error(APLOG_MARK, APLOG_CRIT | level_flags, rv,
1931 (startup ? NULL : s),
1932 "could not open pipe-of-death");
1939 static int worker_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
1942 int no_detach, debug, foreground;
1944 const char *userdata_key = "mpm_worker_module";
1946 mpm_state = AP_MPMQ_STARTING;
1948 debug = ap_exists_config_define("DEBUG");
1951 foreground = one_process = 1;
1955 one_process = ap_exists_config_define("ONE_PROCESS");
1956 no_detach = ap_exists_config_define("NO_DETACH");
1957 foreground = ap_exists_config_define("FOREGROUND");
1960 /* sigh, want this only the second time around */
1961 retained = ap_retained_data_get(userdata_key);
1963 retained = ap_retained_data_create(userdata_key, sizeof(*retained));
1965 ++retained->module_loads;
1966 if (retained->module_loads == 2) {
1969 if (!one_process && !foreground) {
1970 rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
1971 : APR_PROC_DETACH_DAEMONIZE);
1972 if (rv != APR_SUCCESS) {
1973 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
1974 "apr_proc_detach failed");
1975 return HTTP_INTERNAL_SERVER_ERROR;
1978 parent_pid = ap_my_pid = getpid();
1981 ap_listen_pre_config();
1982 ap_daemons_to_start = DEFAULT_START_DAEMON;
1983 min_spare_threads = DEFAULT_MIN_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1984 max_spare_threads = DEFAULT_MAX_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1985 server_limit = DEFAULT_SERVER_LIMIT;
1986 thread_limit = DEFAULT_THREAD_LIMIT;
1987 ap_daemons_limit = server_limit;
1988 threads_per_child = DEFAULT_THREADS_PER_CHILD;
1989 max_clients = ap_daemons_limit * threads_per_child;
1990 ap_pid_fname = DEFAULT_PIDLOG;
1991 ap_lock_fname = DEFAULT_LOCKFILE;
1992 ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
1993 ap_extended_status = 0;
1994 ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
1996 apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
2001 static int worker_check_config(apr_pool_t *p, apr_pool_t *plog,
2002 apr_pool_t *ptemp, server_rec *s)
2004 static int restart_num = 0;
2007 /* the reverse of pre_config, we want this only the first time around */
2008 if (restart_num++ == 0) {
2012 if (server_limit > MAX_SERVER_LIMIT) {
2014 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2015 "WARNING: ServerLimit of %d exceeds compile-time "
2016 "limit of", server_limit);
2017 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2018 " %d servers, decreasing to %d.",
2019 MAX_SERVER_LIMIT, MAX_SERVER_LIMIT);
2021 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2022 "ServerLimit of %d exceeds compile-time limit "
2023 "of %d, decreasing to match",
2024 server_limit, MAX_SERVER_LIMIT);
2026 server_limit = MAX_SERVER_LIMIT;
2028 else if (server_limit < 1) {
2030 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2031 "WARNING: ServerLimit of %d not allowed, "
2032 "increasing to 1.", server_limit);
2034 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2035 "ServerLimit of %d not allowed, increasing to 1",
2041 /* you cannot change ServerLimit across a restart; ignore
2044 if (!retained->first_server_limit) {
2045 retained->first_server_limit = server_limit;
2047 else if (server_limit != retained->first_server_limit) {
2048 /* don't need a startup console version here */
2049 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2050 "changing ServerLimit to %d from original value of %d "
2051 "not allowed during restart",
2052 server_limit, retained->first_server_limit);
2053 server_limit = retained->first_server_limit;
2056 if (thread_limit > MAX_THREAD_LIMIT) {
2058 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2059 "WARNING: ThreadLimit of %d exceeds compile-time "
2060 "limit of", thread_limit);
2061 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2062 " %d threads, decreasing to %d.",
2063 MAX_THREAD_LIMIT, MAX_THREAD_LIMIT);
2065 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2066 "ThreadLimit of %d exceeds compile-time limit "
2067 "of %d, decreasing to match",
2068 thread_limit, MAX_THREAD_LIMIT);
2070 thread_limit = MAX_THREAD_LIMIT;
2072 else if (thread_limit < 1) {
2074 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2075 "WARNING: ThreadLimit of %d not allowed, "
2076 "increasing to 1.", thread_limit);
2078 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2079 "ThreadLimit of %d not allowed, increasing to 1",
2085 /* you cannot change ThreadLimit across a restart; ignore
2088 if (!retained->first_thread_limit) {
2089 retained->first_thread_limit = thread_limit;
2091 else if (thread_limit != retained->first_thread_limit) {
2092 /* don't need a startup console version here */
2093 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2094 "changing ThreadLimit to %d from original value of %d "
2095 "not allowed during restart",
2096 thread_limit, retained->first_thread_limit);
2097 thread_limit = retained->first_thread_limit;
2100 if (threads_per_child > thread_limit) {
2102 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2103 "WARNING: ThreadsPerChild of %d exceeds ThreadLimit "
2104 "of", threads_per_child);
2105 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2106 " %d threads, decreasing to %d.",
2107 thread_limit, thread_limit);
2108 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2109 " To increase, please see the ThreadLimit "
2112 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2113 "ThreadsPerChild of %d exceeds ThreadLimit "
2114 "of %d, decreasing to match",
2115 threads_per_child, thread_limit);
2117 threads_per_child = thread_limit;
2119 else if (threads_per_child < 1) {
2121 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2122 "WARNING: ThreadsPerChild of %d not allowed, "
2123 "increasing to 1.", threads_per_child);
2125 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2126 "ThreadsPerChild of %d not allowed, increasing to 1",
2129 threads_per_child = 1;
2132 if (max_clients < threads_per_child) {
2134 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2135 "WARNING: MaxClients of %d is less than "
2136 "ThreadsPerChild of", max_clients);
2137 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2138 " %d, increasing to %d. MaxClients must be at "
2140 threads_per_child, threads_per_child);
2141 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2142 " as the number of threads in a single server.");
2144 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2145 "MaxClients of %d is less than ThreadsPerChild "
2146 "of %d, increasing to match",
2147 max_clients, threads_per_child);
2149 max_clients = threads_per_child;
2152 ap_daemons_limit = max_clients / threads_per_child;
2154 if (max_clients % threads_per_child) {
2155 int tmp_max_clients = ap_daemons_limit * threads_per_child;
2158 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2159 "WARNING: MaxClients of %d is not an integer "
2160 "multiple of", max_clients);
2161 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2162 " ThreadsPerChild of %d, decreasing to nearest "
2163 "multiple %d,", threads_per_child,
2165 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2166 " for a maximum of %d servers.",
2169 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2170 "MaxClients of %d is not an integer multiple of "
2171 "ThreadsPerChild of %d, decreasing to nearest "
2172 "multiple %d", max_clients, threads_per_child,
2175 max_clients = tmp_max_clients;
2178 if (ap_daemons_limit > server_limit) {
2180 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2181 "WARNING: MaxClients of %d would require %d "
2182 "servers and ", max_clients, ap_daemons_limit);
2183 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2184 " would exceed ServerLimit of %d, decreasing to %d.",
2185 server_limit, server_limit * threads_per_child);
2186 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2187 " To increase, please see the ServerLimit "
2190 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2191 "MaxClients of %d would require %d servers and "
2192 "exceed ServerLimit of %d, decreasing to %d",
2193 max_clients, ap_daemons_limit, server_limit,
2194 server_limit * threads_per_child);
2196 ap_daemons_limit = server_limit;
2199 /* ap_daemons_to_start > ap_daemons_limit checked in worker_run() */
2200 if (ap_daemons_to_start < 0) {
2202 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2203 "WARNING: StartServers of %d not allowed, "
2204 "increasing to 1.", ap_daemons_to_start);
2206 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2207 "StartServers of %d not allowed, increasing to 1",
2208 ap_daemons_to_start);
2210 ap_daemons_to_start = 1;
2213 if (min_spare_threads < 1) {
2215 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2216 "WARNING: MinSpareThreads of %d not allowed, "
2217 "increasing to 1", min_spare_threads);
2218 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2219 " to avoid almost certain server failure.");
2220 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_STARTUP, 0, NULL,
2221 " Please read the documentation.");
2223 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2224 "MinSpareThreads of %d not allowed, increasing to 1",
2227 min_spare_threads = 1;
2230 /* max_spare_threads < min_spare_threads + threads_per_child
2231 * checked in worker_run()
2237 static void worker_hooks(apr_pool_t *p)
2239 /* Our open_logs hook function must run before the core's, or stderr
2240 * will be redirected to a file, and the messages won't print to the
2243 static const char *const aszSucc[] = {"core.c", NULL};
2246 ap_hook_open_logs(worker_open_logs, NULL, aszSucc, APR_HOOK_REALLY_FIRST);
2247 /* we need to set the MPM state before other pre-config hooks use MPM query
2248 * to retrieve it, so register as REALLY_FIRST
2250 ap_hook_pre_config(worker_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
2251 ap_hook_check_config(worker_check_config, NULL, NULL, APR_HOOK_MIDDLE);
2252 ap_hook_mpm(worker_run, NULL, NULL, APR_HOOK_MIDDLE);
2253 ap_hook_mpm_query(worker_query, NULL, NULL, APR_HOOK_MIDDLE);
2254 ap_hook_mpm_note_child_killed(worker_note_child_killed, NULL, NULL, APR_HOOK_MIDDLE);
2255 ap_hook_mpm_get_name(worker_get_name, NULL, NULL, APR_HOOK_MIDDLE);
2258 static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy,
2261 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2266 ap_daemons_to_start = atoi(arg);
2270 static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy,
2273 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2278 min_spare_threads = atoi(arg);
2282 static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy,
2285 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2290 max_spare_threads = atoi(arg);
2294 static const char *set_max_clients (cmd_parms *cmd, void *dummy,
2297 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2302 max_clients = atoi(arg);
2306 static const char *set_threads_per_child (cmd_parms *cmd, void *dummy,
2309 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2314 threads_per_child = atoi(arg);
2318 static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
2320 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2325 server_limit = atoi(arg);
2329 static const char *set_thread_limit (cmd_parms *cmd, void *dummy, const char *arg)
2331 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2336 thread_limit = atoi(arg);
2340 static const command_rec worker_cmds[] = {
2342 AP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
2343 "Number of child processes launched at server startup"),
2344 AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
2345 "Minimum number of idle threads, to handle request spikes"),
2346 AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
2347 "Maximum number of idle threads"),
2348 AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
2349 "Maximum number of threads alive at the same time"),
2350 AP_INIT_TAKE1("ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF,
2351 "Number of threads each child creates"),
2352 AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
2353 "Maximum number of child processes for this run of Apache"),
2354 AP_INIT_TAKE1("ThreadLimit", set_thread_limit, NULL, RSRC_CONF,
2355 "Maximum number of worker threads per child process for this run of Apache - Upper limit for ThreadsPerChild"),
2356 AP_GRACEFUL_SHUTDOWN_TIMEOUT_COMMAND,
2360 module AP_MODULE_DECLARE_DATA mpm_worker_module = {
2362 NULL, /* hook to run before apache parses args */
2363 NULL, /* create per-directory config structure */
2364 NULL, /* merge per-directory config structures */
2365 NULL, /* create per-server config structure */
2366 NULL, /* merge per-server config structures */
2367 worker_cmds, /* command apr_table_t */
2368 worker_hooks /* register_hooks */