1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
59 /* The purpose of this MPM is to fix the design flaws in the threaded
60 * model. Because of the way that pthreads and mutex locks interact,
61 * it is basically impossible to cleanly gracefully shutdown a child
62 * process if multiple threads are all blocked in accept. This model
63 * fixes those problems.
67 #include "apr_portable.h"
68 #include "apr_strings.h"
69 #include "apr_file_io.h"
70 #include "apr_thread_proc.h"
71 #include "apr_signal.h"
72 #include "apr_thread_mutex.h"
73 #include "apr_proc_mutex.h"
74 #define APR_WANT_STRFUNC
80 #if APR_HAVE_SYS_SOCKET_H
81 #include <sys/socket.h>
83 #if APR_HAVE_SYS_WAIT_H
86 #ifdef HAVE_SYS_PROCESSOR_H
87 #include <sys/processor.h> /* for bindprocessor() */
91 #error The Worker MPM requires APR threads, but they are unavailable.
96 #include "ap_config.h"
98 #include "http_main.h"
100 #include "http_config.h" /* for read_config */
101 #include "http_core.h" /* for get_remote_host */
102 #include "http_connection.h"
105 #include "mpm_common.h"
106 #include "ap_listen.h"
107 #include "scoreboard.h"
109 #include "mpm_default.h"
112 #include <limits.h> /* for INT_MAX */
114 /* Limit on the total --- clients will be locked out if more servers than
115 * this are needed. It is intended solely to keep the server from crashing
116 * when things get out of hand.
118 * We keep a hard maximum number of servers, for two reasons --- first off,
119 * in case something goes seriously wrong, we want to stop the fork bomb
120 * short of actually crashing the machine we're running on by filling some
121 * kernel table. Secondly, it keeps the size of the scoreboard file small
122 * enough that we can read the whole thing without worrying too much about
125 #ifndef DEFAULT_SERVER_LIMIT
126 #define DEFAULT_SERVER_LIMIT 16
129 /* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
130 * some sort of compile-time limit to help catch typos.
132 #ifndef MAX_SERVER_LIMIT
133 #define MAX_SERVER_LIMIT 20000
136 /* Limit on the threads per process. Clients will be locked out if more than
137 * this * server_limit are needed.
139 * We keep this for one reason it keeps the size of the scoreboard file small
140 * enough that we can read the whole thing without worrying too much about
143 #ifndef DEFAULT_THREAD_LIMIT
144 #define DEFAULT_THREAD_LIMIT 64
147 /* Admin can't tune ThreadLimit beyond MAX_THREAD_LIMIT. We want
148 * some sort of compile-time limit to help catch typos.
150 #ifndef MAX_THREAD_LIMIT
151 #define MAX_THREAD_LIMIT 20000
155 * Actual definitions of config globals
158 int ap_threads_per_child = 0; /* Worker threads per child */
159 static int ap_daemons_to_start = 0;
160 static int min_spare_threads = 0;
161 static int max_spare_threads = 0;
162 static int ap_daemons_limit = 0;
163 static int server_limit = DEFAULT_SERVER_LIMIT;
164 static int first_server_limit;
165 static int thread_limit = DEFAULT_THREAD_LIMIT;
166 static int first_thread_limit;
167 static int changed_limit_at_restart;
168 static int dying = 0;
169 static int workers_may_exit = 0;
170 static int requests_this_child;
171 static int num_listensocks = 0;
172 static int resource_shortage = 0;
173 static fd_queue_t *worker_queue;
175 /* The structure used to pass unique initialization info to each thread */
182 /* Structure used to pass information to the thread responsible for
183 * creating the rest of the threads.
186 apr_thread_t **threads;
187 apr_thread_t *listener;
189 apr_threadattr_t *threadattr;
192 #define ID_FROM_CHILD_THREAD(c, t) ((c * thread_limit) + t)
195 * The max child slot ever assigned, preserved across restarts. Necessary
196 * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
197 * use this value to optimize routines that have to scan the entire
200 int ap_max_daemons_limit = -1;
202 static ap_pod_t *pod;
204 /* *Non*-shared http_main globals... */
206 server_rec *ap_server_conf;
208 /* The worker MPM respects a couple of runtime flags that can aid
209 * in debugging. Setting the -DNO_DETACH flag will prevent the root process
210 * from detaching from its controlling terminal. Additionally, setting
211 * the -DONE_PROCESS flag (which implies -DNO_DETACH) will get you the
212 * child_main loop running in the process which originally started up.
213 * This gives you a pretty nice debugging environment. (You'll get a SIGHUP
214 * early in standalone_main; just continue through. This is the server
215 * trying to kill off any child processes which it might have lying
216 * around --- Apache doesn't keep track of their pids, it just sends
217 * SIGHUP to the process group, ignoring it in the root process.
218 * Continue through and you'll be fine.).
221 static int one_process = 0;
224 int raise_sigstop_flags;
227 static apr_pool_t *pconf; /* Pool for config stuff */
228 static apr_pool_t *pchild; /* Pool for httpd child stuff */
230 static pid_t ap_my_pid; /* Linux getpid() doesn't work except in main
231 thread. Use this instead */
232 static pid_t parent_pid;
233 static apr_os_thread_t *listener_os_thread;
235 /* Locks for accept serialization */
236 static apr_proc_mutex_t *accept_mutex;
238 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
239 #define SAFE_ACCEPT(stmt) (ap_listeners->next ? (stmt) : APR_SUCCESS)
241 #define SAFE_ACCEPT(stmt) (stmt)
244 /* The LISTENER_SIGNAL signal will be sent from the main thread to the
245 * listener thread to wake it up for graceful termination (what a child
246 * process from an old generation does when the admin does "apachectl
247 * graceful"). This signal will be blocked in all threads of a child
248 * process except for the listener thread.
250 #define LISTENER_SIGNAL SIGHUP
252 static void wakeup_listener(void)
255 * we should just be able to "kill(ap_my_pid, LISTENER_SIGNAL)" and wake
256 * up the listener thread since it is the only thread with SIGHUP
257 * unblocked, but that doesn't work on Linux
259 pthread_kill(*listener_os_thread, LISTENER_SIGNAL);
262 static void signal_workers(void)
264 workers_may_exit = 1;
266 /* in case we weren't called from the listener thread, wake up the
271 /* XXX: This will happen naturally on a graceful, and we don't care
273 ap_queue_signal_all_wakeup(worker_queue); */
274 ap_queue_interrupt_all(worker_queue);
277 AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
280 case AP_MPMQ_MAX_DAEMON_USED:
281 *result = ap_max_daemons_limit;
283 case AP_MPMQ_IS_THREADED:
284 *result = AP_MPMQ_STATIC;
286 case AP_MPMQ_IS_FORKED:
287 *result = AP_MPMQ_DYNAMIC;
289 case AP_MPMQ_HARD_LIMIT_DAEMONS:
290 *result = server_limit;
292 case AP_MPMQ_HARD_LIMIT_THREADS:
293 *result = thread_limit;
295 case AP_MPMQ_MAX_THREADS:
296 *result = ap_threads_per_child;
298 case AP_MPMQ_MIN_SPARE_DAEMONS:
301 case AP_MPMQ_MIN_SPARE_THREADS:
302 *result = min_spare_threads;
304 case AP_MPMQ_MAX_SPARE_DAEMONS:
307 case AP_MPMQ_MAX_SPARE_THREADS:
308 *result = max_spare_threads;
310 case AP_MPMQ_MAX_REQUESTS_DAEMON:
311 *result = ap_max_requests_per_child;
313 case AP_MPMQ_MAX_DAEMONS:
314 *result = ap_daemons_limit;
320 /* a clean exit from a child with proper cleanup */
321 static void clean_child_exit(int code) __attribute__ ((noreturn));
322 static void clean_child_exit(int code)
325 apr_pool_destroy(pchild);
330 /* handle all varieties of core dumping signals */
331 static void sig_coredump(int sig)
333 apr_filepath_set(ap_coredump_dir, pconf);
334 apr_signal(sig, SIG_DFL);
335 if (ap_my_pid == parent_pid) {
336 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
338 "seg fault or similar nasty error detected "
339 "in the parent process");
341 /* XXX we can probably add some rudimentary cleanup code here,
342 * like getting rid of the pid file. If any additional bad stuff
343 * happens, we are protected from recursive errors taking down the
344 * system since this function is no longer the signal handler GLA
347 kill(ap_my_pid, sig);
348 /* At this point we've got sig blocked, because we're still inside
349 * the signal handler. When we leave the signal handler it will
350 * be unblocked, and we'll take the signal... and coredump or whatever
351 * is appropriate for this particular Unix. In addition the parent
352 * will see the real signal we received -- whereas if we called
353 * abort() here, the parent would only see SIGABRT.
357 static void just_die(int sig)
362 /*****************************************************************
363 * Connection structures and accounting...
366 /* volatile just in case */
367 static int volatile shutdown_pending;
368 static int volatile restart_pending;
369 static int volatile is_graceful;
370 static volatile int child_fatal;
371 ap_generation_t volatile ap_my_generation;
374 * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
375 * functions to initiate shutdown or restart without relying on signals.
376 * Previously this was initiated in sig_term() and restart() signal handlers,
377 * but we want to be able to start a shutdown/restart from other sources --
378 * e.g. on Win32, from the service manager. Now the service manager can
379 * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
380 * these functions can also be called by the child processes, since global
381 * variables are no longer used to pass on the required action to the parent.
383 * These should only be called from the parent process itself, since the
384 * parent process will use the shutdown_pending and restart_pending variables
385 * to determine whether to shutdown or restart. The child process should
386 * call signal_parent() directly to tell the parent to die -- this will
387 * cause neither of those variable to be set, which the parent will
388 * assume means something serious is wrong (which it will be, for the
389 * child to force an exit) and so do an exit anyway.
392 static void ap_start_shutdown(void)
394 if (shutdown_pending == 1) {
395 /* Um, is this _probably_ not an error, if the user has
396 * tried to do a shutdown twice quickly, so we won't
397 * worry about reporting it.
401 shutdown_pending = 1;
404 /* do a graceful restart if graceful == 1 */
405 static void ap_start_restart(int graceful)
408 if (restart_pending == 1) {
409 /* Probably not an error - don't bother reporting it */
413 is_graceful = graceful;
416 static void sig_term(int sig)
421 static void restart(int sig)
423 ap_start_restart(sig == AP_SIG_GRACEFUL);
426 static void set_signals(void)
428 #ifndef NO_USE_SIGACTION
431 sigemptyset(&sa.sa_mask);
435 sa.sa_handler = sig_coredump;
436 #if defined(SA_ONESHOT)
437 sa.sa_flags = SA_ONESHOT;
438 #elif defined(SA_RESETHAND)
439 sa.sa_flags = SA_RESETHAND;
441 if (sigaction(SIGSEGV, &sa, NULL) < 0)
442 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
443 "sigaction(SIGSEGV)");
445 if (sigaction(SIGBUS, &sa, NULL) < 0)
446 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
447 "sigaction(SIGBUS)");
450 if (sigaction(SIGABORT, &sa, NULL) < 0)
451 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
452 "sigaction(SIGABORT)");
455 if (sigaction(SIGABRT, &sa, NULL) < 0)
456 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
457 "sigaction(SIGABRT)");
460 if (sigaction(SIGILL, &sa, NULL) < 0)
461 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
462 "sigaction(SIGILL)");
466 sa.sa_handler = sig_term;
467 if (sigaction(SIGTERM, &sa, NULL) < 0)
468 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
469 "sigaction(SIGTERM)");
471 if (sigaction(SIGINT, &sa, NULL) < 0)
472 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
473 "sigaction(SIGINT)");
476 sa.sa_handler = SIG_DFL;
477 if (sigaction(SIGXCPU, &sa, NULL) < 0)
478 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
479 "sigaction(SIGXCPU)");
482 sa.sa_handler = SIG_DFL;
483 if (sigaction(SIGXFSZ, &sa, NULL) < 0)
484 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
485 "sigaction(SIGXFSZ)");
488 sa.sa_handler = SIG_IGN;
489 if (sigaction(SIGPIPE, &sa, NULL) < 0)
490 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
491 "sigaction(SIGPIPE)");
494 /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
496 sigaddset(&sa.sa_mask, SIGHUP);
497 sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
498 sa.sa_handler = restart;
499 if (sigaction(SIGHUP, &sa, NULL) < 0)
500 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
501 "sigaction(SIGHUP)");
502 if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
503 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
504 "sigaction(" AP_SIG_GRACEFUL_STRING ")");
507 apr_signal(SIGSEGV, sig_coredump);
509 apr_signal(SIGBUS, sig_coredump);
512 apr_signal(SIGABORT, sig_coredump);
513 #endif /* SIGABORT */
515 apr_signal(SIGABRT, sig_coredump);
518 apr_signal(SIGILL, sig_coredump);
521 apr_signal(SIGXCPU, SIG_DFL);
524 apr_signal(SIGXFSZ, SIG_DFL);
528 apr_signal(SIGTERM, sig_term);
530 apr_signal(SIGHUP, restart);
532 #ifdef AP_SIG_GRACEFUL
533 apr_signal(AP_SIG_GRACEFUL, restart);
534 #endif /* AP_SIG_GRACEFUL */
536 apr_signal(SIGPIPE, SIG_IGN);
542 /*****************************************************************
543 * Here follows a long bunch of generic server bookkeeping stuff...
546 int ap_graceful_stop_signalled(void)
547 /* XXX this is really a bad confusing obsolete name
548 * maybe it should be ap_mpm_process_exiting?
551 return workers_may_exit;
554 /*****************************************************************
555 * Child process main loop.
558 static void process_socket(apr_pool_t *p, apr_socket_t *sock, int my_child_num,
561 conn_rec *current_conn;
562 long conn_id = ID_FROM_CHILD_THREAD(my_child_num, my_thread_num);
566 ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
567 apr_os_sock_get(&csd, sock);
569 if (csd >= FD_SETSIZE) {
570 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, NULL,
571 "new file descriptor %d is too large; you probably need "
572 "to rebuild Apache with a larger FD_SETSIZE "
575 apr_socket_close(sock);
579 current_conn = ap_run_create_connection(p, ap_server_conf, sock, conn_id, sbh);
581 ap_process_connection(current_conn, sock);
582 ap_lingering_close(current_conn);
586 /* requests_this_child has gone to zero or below. See if the admin coded
587 "MaxRequestsPerChild 0", and keep going in that case. Doing it this way
588 simplifies the hot path in worker_thread */
589 static void check_infinite_requests(void)
591 if (ap_max_requests_per_child) {
595 /* wow! if you're executing this code, you may have set a record.
596 * either this child process has served over 2 billion requests, or
597 * you're running a threaded 2.0 on a 16 bit machine.
599 * I'll buy pizza and beers at Apachecon for the first person to do
600 * the former without cheating (dorking with INT_MAX, or running with
601 * uncommitted performance patches, for example).
603 * for the latter case, you probably deserve a beer too. Greg Ames
606 requests_this_child = INT_MAX; /* keep going */
610 static void unblock_the_listener(int sig)
612 /* XXX If specifying SIG_IGN is guaranteed to unblock a syscall,
613 * then we don't need this goofy function.
617 static void *listener_thread(apr_thread_t *thd, void * dummy)
619 proc_info * ti = dummy;
620 int process_slot = ti->pid;
621 int thread_slot = ti->tid;
622 apr_pool_t *tpool = apr_thread_pool_get(thd);
624 apr_pool_t *ptrans; /* Pool for per-transaction stuff */
625 apr_pool_t *recycled_pool = NULL;
627 apr_pollfd_t *pollset;
629 ap_listen_rec *lr, *last_lr = ap_listeners;
635 apr_poll_setup(&pollset, num_listensocks, tpool);
636 for(lr = ap_listeners ; lr != NULL ; lr = lr->next)
637 apr_poll_socket_add(pollset, lr->sd, APR_POLLIN);
639 sigemptyset(&sig_mask);
640 /* Unblock the signal used to wake this thread up, and set a handler for
643 sigaddset(&sig_mask, LISTENER_SIGNAL);
644 #if defined(SIGPROCMASK_SETS_THREAD_MASK)
645 sigprocmask(SIG_UNBLOCK, &sig_mask, NULL);
647 pthread_sigmask(SIG_UNBLOCK, &sig_mask, NULL);
649 sigemptyset(&sa.sa_mask);
651 sa.sa_handler = unblock_the_listener;
652 sigaction(LISTENER_SIGNAL, &sa, NULL);
654 /* TODO: Switch to a system where threads reuse the results from earlier
655 poll calls - manoj */
657 /* TODO: requests_this_child should be synchronized - aaron */
658 if (requests_this_child <= 0) {
659 check_infinite_requests();
661 if (workers_may_exit) break;
663 if ((rv = SAFE_ACCEPT(apr_proc_mutex_lock(accept_mutex)))
665 int level = APLOG_EMERG;
667 if (workers_may_exit) {
670 if (ap_scoreboard_image->parent[process_slot].generation !=
671 ap_scoreboard_image->global->running_generation) {
672 level = APLOG_DEBUG; /* common to get these at restart time */
674 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
675 "apr_proc_mutex_lock failed. Attempting to shutdown "
676 "process gracefully.");
678 break; /* skip the lock release */
681 if (!ap_listeners->next) {
682 /* Only one listener, so skip the poll */
686 while (!workers_may_exit) {
690 ret = apr_poll(pollset, &n, -1);
691 if (ret != APR_SUCCESS) {
692 if (APR_STATUS_IS_EINTR(ret)) {
696 /* apr_poll() will only return errors in catastrophic
697 * circumstances. Let's try exiting gracefully, for now. */
698 ap_log_error(APLOG_MARK, APLOG_ERR, ret, (const server_rec *)
699 ap_server_conf, "apr_poll: (listen)");
703 if (workers_may_exit) break;
705 /* find a listener */
712 /* XXX: Should we check for POLLERR? */
713 apr_poll_revents_get(&event, lr->sd, pollset);
714 if (event & APR_POLLIN) {
718 } while (lr != last_lr);
722 if (!workers_may_exit) {
723 /* create a new transaction pool for each accepted socket */
724 if (recycled_pool == NULL) {
725 apr_allocator_t *allocator;
727 apr_allocator_create(&allocator);
728 apr_pool_create_ex(&ptrans, NULL, NULL, allocator);
729 apr_allocator_set_owner(allocator, ptrans);
732 ptrans = recycled_pool;
734 apr_pool_tag(ptrans, "transaction");
735 rv = lr->accept_func(&csd, lr, ptrans);
737 /* If we were interrupted for whatever reason, just start
738 * the main loop over again.
740 if (APR_STATUS_IS_EINTR(rv)) {
743 if (rv == APR_EGENERAL) {
744 /* E[NM]FILE, ENOMEM, etc */
745 resource_shortage = 1;
748 if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
750 int level = APLOG_EMERG;
752 if (workers_may_exit) {
755 if (ap_scoreboard_image->parent[process_slot].generation !=
756 ap_scoreboard_image->global->running_generation) {
757 level = APLOG_DEBUG; /* common to get these at restart time */
759 ap_log_error(APLOG_MARK, level, rv, ap_server_conf,
760 "apr_proc_mutex_unlock failed. Attempting to "
761 "shutdown process gracefully.");
765 rv = ap_queue_push(worker_queue, csd, ptrans,
768 /* trash the connection; we couldn't queue the connected
771 apr_socket_close(csd);
772 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
773 "ap_queue_push failed");
778 if ((rv = SAFE_ACCEPT(apr_proc_mutex_unlock(accept_mutex)))
780 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
781 "apr_proc_mutex_unlock failed. Attempting to "
782 "shutdown process gracefully.");
789 ap_update_child_status_from_indexes(process_slot, thread_slot,
790 (dying) ? SERVER_DEAD : SERVER_GRACEFUL,
791 (request_rec *) NULL);
793 ap_scoreboard_image->parent[process_slot].quiescing = 1;
794 kill(ap_my_pid, SIGTERM);
796 apr_thread_exit(thd, APR_SUCCESS);
800 /* XXX For ungraceful termination/restart, we definitely don't want to
801 * wait for active connections to finish but we may want to wait
802 * for idle workers to get out of the queue code and release mutexes,
803 * since those mutexes are cleaned up pretty soon and some systems
804 * may not react favorably (i.e., segfault) if operations are attempted
805 * on cleaned-up mutexes.
807 static void * APR_THREAD_FUNC worker_thread(apr_thread_t *thd, void * dummy)
809 proc_info * ti = dummy;
810 int process_slot = ti->pid;
811 int thread_slot = ti->tid;
812 apr_socket_t *csd = NULL;
813 apr_pool_t *last_ptrans = NULL;
814 apr_pool_t *ptrans; /* Pool for per-transaction stuff */
819 ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_STARTING, NULL);
820 while (!workers_may_exit) {
821 ap_update_child_status_from_indexes(process_slot, thread_slot, SERVER_READY, NULL);
822 rv = ap_queue_pop(worker_queue, &csd, &ptrans, last_ptrans);
825 if (rv != APR_SUCCESS) {
826 /* We get APR_EINTR whenever ap_queue_pop() has been interrupted
827 * from an explicit call to ap_queue_interrupt_all(). This allows
828 * us to unblock threads stuck in ap_queue_pop() when a shutdown
831 * If workers_may_exit is set and this is ungraceful termination/
832 * restart, we are bound to get an error on some systems (e.g.,
833 * AIX, which sanity-checks mutex operations) since the queue
834 * may have already been cleaned up. Don't log the "error" if
835 * workers_may_exit is set.
837 if (rv != APR_EINTR && !workers_may_exit) {
838 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
839 "ap_queue_pop failed");
843 process_socket(ptrans, csd, process_slot, thread_slot);
844 requests_this_child--; /* FIXME: should be synchronized - aaron */
845 apr_pool_clear(ptrans);
846 last_ptrans = ptrans;
849 ap_update_child_status_from_indexes(process_slot, thread_slot,
850 (dying) ? SERVER_DEAD : SERVER_GRACEFUL, (request_rec *) NULL);
852 apr_thread_exit(thd, APR_SUCCESS);
856 static int check_signal(int signum)
866 /* XXX under some circumstances not understood, children can get stuck
867 * in start_threads forever trying to take over slots which will
868 * never be cleaned up; for now there is an APLOG_DEBUG message issued
869 * every so often when this condition occurs
871 static void * APR_THREAD_FUNC start_threads(apr_thread_t *thd, void *dummy)
873 thread_starter *ts = dummy;
874 apr_thread_t **threads = ts->threads;
875 apr_threadattr_t *thread_attr = ts->threadattr;
876 int child_num_arg = ts->child_num_arg;
877 int my_child_num = child_num_arg;
878 proc_info *my_info = NULL;
881 int threads_created = 0;
883 int prev_threads_created;
885 /* We must create the fd queues before we start up the listener
886 * and worker threads. */
887 worker_queue = apr_pcalloc(pchild, sizeof(*worker_queue));
888 rv = ap_queue_init(worker_queue, ap_threads_per_child, pchild);
889 if (rv != APR_SUCCESS) {
890 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
891 "ap_queue_init() failed");
892 clean_child_exit(APEXIT_CHILDFATAL);
895 my_info = (proc_info *)malloc(sizeof(proc_info));
896 my_info->pid = my_child_num;
899 rv = apr_thread_create(&ts->listener, thread_attr, listener_thread,
901 if (rv != APR_SUCCESS) {
902 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
903 "apr_thread_create: unable to create listener thread");
904 /* In case system resources are maxxed out, we don't want
905 * Apache running away with the CPU trying to fork over and
906 * over and over again if we exit.
907 * XXX Jeff doesn't see how Apache is going to try to fork again since
908 * the exit code is APEXIT_CHILDFATAL
910 apr_sleep(10 * APR_USEC_PER_SEC);
911 clean_child_exit(APEXIT_CHILDFATAL);
913 apr_os_thread_get(&listener_os_thread, ts->listener);
914 loops = prev_threads_created = 0;
916 /* ap_threads_per_child does not include the listener thread */
917 for (i = 0; i < ap_threads_per_child; i++) {
918 int status = ap_scoreboard_image->servers[child_num_arg][i].status;
920 if (status != SERVER_GRACEFUL && status != SERVER_DEAD) {
924 my_info = (proc_info *)malloc(sizeof(proc_info));
925 if (my_info == NULL) {
926 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
927 "malloc: out of memory");
928 clean_child_exit(APEXIT_CHILDFATAL);
930 my_info->pid = my_child_num;
934 /* We are creating threads right now */
935 ap_update_child_status_from_indexes(my_child_num, i,
936 SERVER_STARTING, NULL);
937 /* We let each thread update its own scoreboard entry. This is
938 * done because it lets us deal with tid better.
940 rv = apr_thread_create(&threads[i], thread_attr,
941 worker_thread, my_info, pchild);
942 if (rv != APR_SUCCESS) {
943 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
944 "apr_thread_create: unable to create worker thread");
945 /* In case system resources are maxxed out, we don't want
946 Apache running away with the CPU trying to fork over and
947 over and over again if we exit. */
948 apr_sleep(10 * APR_USEC_PER_SEC);
949 clean_child_exit(APEXIT_CHILDFATAL);
953 if (workers_may_exit || threads_created == ap_threads_per_child) {
956 /* wait for previous generation to clean up an entry */
957 apr_sleep(1 * APR_USEC_PER_SEC);
959 if (loops % 120 == 0) { /* every couple of minutes */
960 if (prev_threads_created == threads_created) {
961 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
962 "child %" APR_PID_T_FMT " isn't taking over "
963 "slots very quickly (%d of %d)",
964 ap_my_pid, threads_created, ap_threads_per_child);
966 prev_threads_created = threads_created;
970 /* What state should this child_main process be listed as in the
972 * ap_update_child_status_from_indexes(my_child_num, i, SERVER_STARTING,
973 * (request_rec *) NULL);
975 * This state should be listed separately in the scoreboard, in some kind
976 * of process_status, not mixed in with the worker threads' status.
977 * "life_status" is almost right, but it's in the worker's structure, and
978 * the name could be clearer. gla
980 apr_thread_exit(thd, APR_SUCCESS);
984 static void join_workers(apr_thread_t *listener, apr_thread_t **threads)
987 apr_status_t rv, thread_rv;
992 /* deal with a rare timing window which affects waking up the
993 * listener thread... if the signal sent to the listener thread
994 * is delivered between the time it verifies that the
995 * workers_may_exit flag is clear and the time it enters a
996 * blocking syscall, the signal didn't do any good... work around
997 * that by sleeping briefly and sending it again
1001 while (iter < 10 && pthread_kill(*listener_os_thread, 0) == 0) {
1002 /* listener not dead yet */
1003 apr_sleep(APR_USEC_PER_SEC / 2);
1008 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ap_server_conf,
1009 "the listener thread didn't exit");
1012 rv = apr_thread_join(&thread_rv, listener);
1013 if (rv != APR_SUCCESS) {
1014 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1015 "apr_thread_join: unable to join listener thread");
1020 for (i = 0; i < ap_threads_per_child; i++) {
1021 if (threads[i]) { /* if we ever created this thread */
1022 rv = apr_thread_join(&thread_rv, threads[i]);
1023 if (rv != APR_SUCCESS) {
1024 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1025 "apr_thread_join: unable to join worker "
1033 static void join_start_thread(apr_thread_t *start_thread_id)
1035 apr_status_t rv, thread_rv;
1037 workers_may_exit = 1; /* start thread may not want to exit until this
1040 rv = apr_thread_join(&thread_rv, start_thread_id);
1041 if (rv != APR_SUCCESS) {
1042 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf,
1043 "apr_thread_join: unable to join the start "
1048 static void child_main(int child_num_arg)
1050 apr_thread_t **threads;
1053 apr_threadattr_t *thread_attr;
1054 apr_thread_t *start_thread_id;
1056 ap_my_pid = getpid();
1057 apr_pool_create(&pchild, pconf);
1059 /*stuff to do before we switch id's, so we have permissions.*/
1060 ap_reopen_scoreboard(pchild, NULL, 0);
1062 rv = SAFE_ACCEPT(apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname,
1064 if (rv != APR_SUCCESS) {
1065 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1066 "Couldn't initialize cross-process lock in child");
1067 clean_child_exit(APEXIT_CHILDFATAL);
1070 if (unixd_setup_child()) {
1071 clean_child_exit(APEXIT_CHILDFATAL);
1074 ap_run_child_init(pchild, ap_server_conf);
1076 /* done with init critical section */
1078 /* Just use the standard apr_setup_signal_thread to block all signals
1079 * from being received. The child processes no longer use signals for
1080 * any communication with the parent process.
1082 rv = apr_setup_signal_thread();
1083 if (rv != APR_SUCCESS) {
1084 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1085 "Couldn't initialize signal thread");
1086 clean_child_exit(APEXIT_CHILDFATAL);
1089 if (ap_max_requests_per_child) {
1090 requests_this_child = ap_max_requests_per_child;
1093 /* coding a value of zero means infinity */
1094 requests_this_child = INT_MAX;
1097 /* Setup worker threads */
1099 /* clear the storage; we may not create all our threads immediately,
1100 * and we want a 0 entry to indicate a thread which was not created
1102 threads = (apr_thread_t **)calloc(1,
1103 sizeof(apr_thread_t *) * ap_threads_per_child);
1104 if (threads == NULL) {
1105 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
1106 "malloc: out of memory");
1107 clean_child_exit(APEXIT_CHILDFATAL);
1110 ts = (thread_starter *)apr_palloc(pchild, sizeof(*ts));
1112 apr_threadattr_create(&thread_attr, pchild);
1113 /* 0 means PTHREAD_CREATE_JOINABLE */
1114 apr_threadattr_detach_set(thread_attr, 0);
1116 ts->threads = threads;
1117 ts->listener = NULL;
1118 ts->child_num_arg = child_num_arg;
1119 ts->threadattr = thread_attr;
1121 rv = apr_thread_create(&start_thread_id, thread_attr, start_threads,
1123 if (rv != APR_SUCCESS) {
1124 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
1125 "apr_thread_create: unable to create worker thread");
1126 /* In case system resources are maxxed out, we don't want
1127 Apache running away with the CPU trying to fork over and
1128 over and over again if we exit. */
1129 apr_sleep(10 * APR_USEC_PER_SEC);
1130 clean_child_exit(APEXIT_CHILDFATAL);
1133 /* If we are only running in one_process mode, we will want to
1134 * still handle signals. */
1136 /* Block until we get a terminating signal. */
1137 apr_signal_thread(check_signal);
1138 /* make sure the start thread has finished; signal_workers()
1139 * and join_workers() depend on that
1141 join_start_thread(start_thread_id);
1142 signal_workers(); /* helps us terminate a little more quickly when
1143 * the dispatch of the signal thread
1144 * beats the Pipe of Death and the browsers
1146 /* A terminating signal was received. Now join each of the
1147 * workers to clean them up.
1148 * If the worker already exited, then the join frees
1149 * their resources and returns.
1150 * If the worker hasn't exited, then this blocks until
1151 * they have (then cleans up).
1153 join_workers(ts->listener, threads);
1155 else { /* !one_process */
1156 /* Watch for any messages from the parent over the POD */
1158 rv = ap_mpm_pod_check(pod);
1159 if (rv == AP_GRACEFUL || rv == AP_RESTART) {
1160 /* make sure the start thread has finished;
1161 * signal_workers() and join_workers depend on that
1163 join_start_thread(start_thread_id);
1169 if (rv == AP_GRACEFUL) {
1170 /* A terminating signal was received. Now join each of the
1171 * workers to clean them up.
1172 * If the worker already exited, then the join frees
1173 * their resources and returns.
1174 * If the worker hasn't exited, then this blocks until
1175 * they have (then cleans up).
1177 join_workers(ts->listener, threads);
1183 clean_child_exit(resource_shortage ? APEXIT_CHILDSICK : 0);
1186 static int make_child(server_rec *s, int slot)
1190 if (slot + 1 > ap_max_daemons_limit) {
1191 ap_max_daemons_limit = slot + 1;
1196 ap_scoreboard_image->parent[slot].pid = getpid();
1200 if ((pid = fork()) == -1) {
1201 ap_log_error(APLOG_MARK, APLOG_ERR, errno, s,
1202 "fork: Unable to fork new process");
1204 /* fork didn't succeed. Fix the scoreboard or else
1205 * it will say SERVER_STARTING forever and ever
1207 ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD, NULL);
1209 /* In case system resources are maxxed out, we don't want
1210 Apache running away with the CPU trying to fork over and
1211 over and over again. */
1212 apr_sleep(10 * APR_USEC_PER_SEC);
1218 #ifdef HAVE_BINDPROCESSOR
1219 /* By default, AIX binds to a single processor. This bit unbinds
1220 * children which will then bind to another CPU.
1222 int status = bindprocessor(BINDPROCESS, (int)getpid(),
1223 PROCESSOR_CLASS_ANY);
1225 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, errno,
1227 "processor unbind failed %d", status);
1229 RAISE_SIGSTOP(MAKE_CHILD);
1231 apr_signal(SIGTERM, just_die);
1234 clean_child_exit(0);
1237 ap_scoreboard_image->parent[slot].quiescing = 0;
1238 ap_scoreboard_image->parent[slot].pid = pid;
1242 /* start up a bunch of children */
1243 static void startup_children(int number_to_start)
1247 for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
1248 if (ap_scoreboard_image->parent[i].pid != 0) {
1251 if (make_child(ap_server_conf, i) < 0) {
1260 * idle_spawn_rate is the number of children that will be spawned on the
1261 * next maintenance cycle if there aren't enough idle servers. It is
1262 * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
1263 * without the need to spawn.
1265 static int idle_spawn_rate = 1;
1266 #ifndef MAX_SPAWN_RATE
1267 #define MAX_SPAWN_RATE (32)
1269 static int hold_off_on_exponential_spawning;
1271 static void perform_idle_server_maintenance(void)
1274 int idle_thread_count;
1278 int totally_free_length = 0;
1279 int free_slots[MAX_SPAWN_RATE];
1283 /* initialize the free_list */
1286 idle_thread_count = 0;
1290 ap_sync_scoreboard_image();
1291 for (i = 0; i < ap_daemons_limit; ++i) {
1292 /* Initialization to satisfy the compiler. It doesn't know
1293 * that ap_threads_per_child is always > 0 */
1294 int status = SERVER_DEAD;
1295 int any_dying_threads = 0;
1296 int any_dead_threads = 0;
1297 int all_dead_threads = 1;
1299 if (i >= ap_max_daemons_limit && totally_free_length == idle_spawn_rate)
1301 ps = &ap_scoreboard_image->parent[i];
1302 for (j = 0; j < ap_threads_per_child; j++) {
1303 ws = &ap_scoreboard_image->servers[i][j];
1304 status = ws->status;
1306 /* XXX any_dying_threads is probably no longer needed GLA */
1307 any_dying_threads = any_dying_threads ||
1308 (status == SERVER_GRACEFUL);
1309 any_dead_threads = any_dead_threads || (status == SERVER_DEAD);
1310 all_dead_threads = all_dead_threads &&
1311 (status == SERVER_DEAD ||
1312 status == SERVER_GRACEFUL);
1314 /* We consider a starting server as idle because we started it
1315 * at least a cycle ago, and if it still hasn't finished starting
1316 * then we're just going to swamp things worse by forking more.
1317 * So we hopefully won't need to fork more if we count it.
1318 * This depends on the ordering of SERVER_READY and SERVER_STARTING.
1320 if (status <= SERVER_READY && status != SERVER_DEAD &&
1322 ps->generation == ap_my_generation &&
1323 /* XXX the following shouldn't be necessary if we clean up
1324 * properly after seg faults, but we're not yet GLA
1327 ++idle_thread_count;
1330 if (any_dead_threads && totally_free_length < idle_spawn_rate
1331 && (!ps->pid /* no process in the slot */
1332 || ps->quiescing)) { /* or at least one is going away */
1333 if (all_dead_threads) {
1334 /* great! we prefer these, because the new process can
1335 * start more threads sooner. So prioritize this slot
1336 * by putting it ahead of any slots with active threads.
1338 * first, make room by moving a slot that's potentially still
1339 * in use to the end of the array
1341 free_slots[free_length] = free_slots[totally_free_length];
1342 free_slots[totally_free_length++] = i;
1345 /* slot is still in use - back of the bus
1347 free_slots[free_length] = i;
1351 /* XXX if (!ps->quiescing) is probably more reliable GLA */
1352 if (!any_dying_threads) {
1357 ap_max_daemons_limit = last_non_dead + 1;
1359 if (idle_thread_count > max_spare_threads) {
1360 /* Kill off one child */
1361 ap_mpm_pod_signal(pod, TRUE);
1362 idle_spawn_rate = 1;
1364 else if (idle_thread_count < min_spare_threads) {
1365 /* terminate the free list */
1366 if (free_length == 0) {
1367 /* only report this condition once */
1368 static int reported = 0;
1371 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0,
1373 "server reached MaxClients setting, consider"
1374 " raising the MaxClients setting");
1377 idle_spawn_rate = 1;
1380 if (free_length > idle_spawn_rate) {
1381 free_length = idle_spawn_rate;
1383 if (idle_spawn_rate >= 8) {
1384 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0,
1386 "server seems busy, (you may need "
1387 "to increase StartServers, ThreadsPerChild "
1388 "or Min/MaxSpareThreads), "
1389 "spawning %d children, there are around %d idle "
1390 "threads, and %d total children", free_length,
1391 idle_thread_count, total_non_dead);
1393 for (i = 0; i < free_length; ++i) {
1394 make_child(ap_server_conf, free_slots[i]);
1396 /* the next time around we want to spawn twice as many if this
1397 * wasn't good enough, but not if we've just done a graceful
1399 if (hold_off_on_exponential_spawning) {
1400 --hold_off_on_exponential_spawning;
1402 else if (idle_spawn_rate < MAX_SPAWN_RATE) {
1403 idle_spawn_rate *= 2;
1408 idle_spawn_rate = 1;
1412 static void server_main_loop(int remaining_children_to_start)
1415 apr_exit_why_e exitwhy;
1416 int status, processed_status;
1420 while (!restart_pending && !shutdown_pending) {
1421 ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
1423 if (pid.pid != -1) {
1424 processed_status = ap_process_child_status(&pid, exitwhy, status);
1425 if (processed_status == APEXIT_CHILDFATAL) {
1426 shutdown_pending = 1;
1430 /* non-fatal death... note that it's gone in the scoreboard. */
1431 child_slot = find_child_by_pid(&pid);
1432 if (child_slot >= 0) {
1433 for (i = 0; i < ap_threads_per_child; i++)
1434 ap_update_child_status_from_indexes(child_slot, i, SERVER_DEAD,
1435 (request_rec *) NULL);
1437 ap_scoreboard_image->parent[child_slot].pid = 0;
1438 ap_scoreboard_image->parent[child_slot].quiescing = 0;
1439 if (processed_status == APEXIT_CHILDSICK) {
1440 /* resource shortage, minimize the fork rate */
1441 idle_spawn_rate = 1;
1443 else if (remaining_children_to_start
1444 && child_slot < ap_daemons_limit) {
1445 /* we're still doing a 1-for-1 replacement of dead
1446 * children with new children
1448 make_child(ap_server_conf, child_slot);
1449 --remaining_children_to_start;
1451 #if APR_HAS_OTHER_CHILD
1453 else if (apr_proc_other_child_read(&pid, status) == 0) {
1457 else if (is_graceful) {
1458 /* Great, we've probably just lost a slot in the
1459 * scoreboard. Somehow we don't know about this child.
1461 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0,
1463 "long lost child came home! (pid %ld)",
1466 /* Don't perform idle maintenance when a child dies,
1467 * only do it when there's a timeout. Remember only a
1468 * finite number of children can die, and it's pretty
1469 * pathological for a lot to die suddenly.
1473 else if (remaining_children_to_start) {
1474 /* we hit a 1 second timeout in which none of the previous
1475 * generation of children needed to be reaped... so assume
1476 * they're all done, and pick up the slack if any is left.
1478 startup_children(remaining_children_to_start);
1479 remaining_children_to_start = 0;
1480 /* In any event we really shouldn't do the code below because
1481 * few of the servers we just started are in the IDLE state
1482 * yet, so we'd mistakenly create an extra server.
1487 perform_idle_server_maintenance();
1491 int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
1493 int remaining_children_to_start;
1496 ap_log_pid(pconf, ap_pid_fname);
1498 first_server_limit = server_limit;
1499 first_thread_limit = thread_limit;
1500 if (changed_limit_at_restart) {
1501 ap_log_error(APLOG_MARK, APLOG_WARNING | APLOG_NOERRNO, 0, s,
1502 "WARNING: Attempt to change ServerLimit or ThreadLimit "
1503 "ignored during restart");
1504 changed_limit_at_restart = 0;
1507 /* Initialize cross-process accept lock */
1508 ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
1509 ap_server_root_relative(_pconf, ap_lock_fname),
1512 rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname,
1513 ap_accept_lock_mech, _pconf);
1514 if (rv != APR_SUCCESS) {
1515 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1516 "Couldn't create accept lock");
1520 #if APR_USE_SYSVSEM_SERIALIZE
1521 if (ap_accept_lock_mech == APR_LOCK_DEFAULT ||
1522 ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1524 if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
1526 rv = unixd_set_proc_mutex_perms(accept_mutex);
1527 if (rv != APR_SUCCESS) {
1528 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1529 "Couldn't set permissions on cross-process lock");
1535 if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
1538 /* fix the generation number in the global score; we just got a new,
1539 * cleared scoreboard
1541 ap_scoreboard_image->global->running_generation = ap_my_generation;
1542 update_scoreboard_global();
1546 /* Don't thrash... */
1547 if (max_spare_threads < min_spare_threads + ap_threads_per_child)
1548 max_spare_threads = min_spare_threads + ap_threads_per_child;
1550 /* If we're doing a graceful_restart then we're going to see a lot
1551 * of children exiting immediately when we get into the main loop
1552 * below (because we just sent them AP_SIG_GRACEFUL). This happens pretty
1553 * rapidly... and for each one that exits we'll start a new one until
1554 * we reach at least daemons_min_free. But we may be permitted to
1555 * start more than that, so we'll just keep track of how many we're
1556 * supposed to start up without the 1 second penalty between each fork.
1558 remaining_children_to_start = ap_daemons_to_start;
1559 if (remaining_children_to_start > ap_daemons_limit) {
1560 remaining_children_to_start = ap_daemons_limit;
1563 startup_children(remaining_children_to_start);
1564 remaining_children_to_start = 0;
1567 /* give the system some time to recover before kicking into
1568 * exponential mode */
1569 hold_off_on_exponential_spawning = 10;
1572 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
1573 "%s configured -- resuming normal operations",
1574 ap_get_server_version());
1575 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, ap_server_conf,
1576 "Server built: %s", ap_get_server_built());
1577 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
1578 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, 0, ap_server_conf,
1579 "AcceptMutex: %s", ap_valid_accept_mutex_string);
1581 restart_pending = shutdown_pending = 0;
1583 server_main_loop(remaining_children_to_start);
1585 if (shutdown_pending) {
1586 /* Time to gracefully shut down:
1587 * Kill child processes, tell them to call child_exit, etc...
1588 * (By "gracefully" we don't mean graceful in the same sense as
1589 * "apachectl graceful" where we allow old connections to finish.)
1591 ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
1592 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1595 /* cleanup pid file on normal shutdown */
1596 const char *pidfile = NULL;
1597 pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1598 if ( pidfile != NULL && unlink(pidfile) == 0)
1599 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0,
1601 "removed PID file %s (pid=%ld)",
1602 pidfile, (long)getpid());
1604 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0,
1605 ap_server_conf, "caught SIGTERM, shutting down");
1610 /* we've been told to restart */
1611 apr_signal(SIGHUP, SIG_IGN);
1614 /* not worth thinking about */
1618 /* advance to the next generation */
1619 /* XXX: we really need to make sure this new generation number isn't in
1620 * use by any of the children.
1623 ap_scoreboard_image->global->running_generation = ap_my_generation;
1624 update_scoreboard_global();
1627 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
1628 AP_SIG_GRACEFUL_STRING " received. Doing graceful restart");
1629 /* wake up the children...time to die. But we'll have more soon */
1630 ap_mpm_pod_killpg(pod, ap_daemons_limit, TRUE);
1633 /* This is mostly for debugging... so that we know what is still
1634 * gracefully dealing with existing request.
1639 /* Kill 'em all. Since the child acts the same on the parents SIGTERM
1640 * and a SIGHUP, we may as well use the same signal, because some user
1641 * pthreads are stealing signals from us left and right.
1643 ap_mpm_pod_killpg(pod, ap_daemons_limit, FALSE);
1645 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1646 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
1647 "SIGHUP received. Attempting to restart");
1653 /* This really should be a post_config hook, but the error log is already
1654 * redirected by that point, so we need to do this in the open_logs phase.
1656 static int worker_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
1663 if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
1664 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ALERT|APLOG_STARTUP, 0,
1665 NULL, "no listening sockets available, shutting down");
1670 if ((rv = ap_mpm_pod_open(pconf, &pod))) {
1671 ap_log_error(APLOG_MARK, APLOG_CRIT|APLOG_STARTUP, rv, NULL,
1672 "Could not open pipe-of-death.");
1679 static int worker_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
1682 static int restart_num = 0;
1683 int no_detach, debug;
1684 ap_directive_t *pdir;
1685 ap_directive_t *max_clients = NULL;
1688 /* make sure that "ThreadsPerChild" gets set before "MaxClients" */
1689 for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next) {
1690 if (strncasecmp(pdir->directive, "ThreadsPerChild", 15) == 0) {
1692 break; /* we're in the clear, got ThreadsPerChild first */
1695 /* now to swap the data */
1696 ap_directive_t temp;
1698 temp.directive = pdir->directive;
1699 temp.args = pdir->args;
1700 /* Make sure you don't change 'next', or you may get loops! */
1701 /* XXX: first_child, parent, and data can never be set
1702 * for these directives, right? -aaron */
1703 temp.filename = pdir->filename;
1704 temp.line_num = pdir->line_num;
1706 pdir->directive = max_clients->directive;
1707 pdir->args = max_clients->args;
1708 pdir->filename = max_clients->filename;
1709 pdir->line_num = max_clients->line_num;
1711 max_clients->directive = temp.directive;
1712 max_clients->args = temp.args;
1713 max_clients->filename = temp.filename;
1714 max_clients->line_num = temp.line_num;
1718 else if (!max_clients
1719 && strncasecmp(pdir->directive, "MaxClients", 10) == 0) {
1724 debug = ap_exists_config_define("DEBUG");
1727 no_detach = one_process = 1;
1730 one_process = ap_exists_config_define("ONE_PROCESS");
1731 no_detach = ap_exists_config_define("NO_DETACH");
1734 /* sigh, want this only the second time around */
1735 if (restart_num++ == 1) {
1739 rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
1740 : APR_PROC_DETACH_DAEMONIZE);
1741 if (rv != APR_SUCCESS) {
1742 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
1743 "apr_proc_detach failed");
1744 return HTTP_INTERNAL_SERVER_ERROR;
1747 parent_pid = ap_my_pid = getpid();
1750 unixd_pre_config(ptemp);
1751 ap_listen_pre_config();
1752 ap_daemons_to_start = DEFAULT_START_DAEMON;
1753 min_spare_threads = DEFAULT_MIN_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1754 max_spare_threads = DEFAULT_MAX_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD;
1755 ap_daemons_limit = server_limit;
1756 ap_threads_per_child = DEFAULT_THREADS_PER_CHILD;
1757 ap_pid_fname = DEFAULT_PIDLOG;
1758 ap_lock_fname = DEFAULT_LOCKFILE;
1759 ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
1760 ap_extended_status = 0;
1762 apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1767 static void worker_hooks(apr_pool_t *p)
1769 /* The worker open_logs phase must run before the core's, or stderr
1770 * will be redirected to a file, and the messages won't print to the
1773 static const char *const aszSucc[] = {"core.c", NULL};
1776 ap_hook_open_logs(worker_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
1777 ap_hook_pre_config(worker_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1780 static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy,
1783 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1788 ap_daemons_to_start = atoi(arg);
1792 static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy,
1795 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1800 min_spare_threads = atoi(arg);
1801 if (min_spare_threads <= 0) {
1802 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1803 "WARNING: detected MinSpareThreads set to non-positive.");
1804 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1805 "Resetting to 1 to avoid almost certain Apache failure.");
1806 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1807 "Please read the documentation.");
1808 min_spare_threads = 1;
1814 static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy,
1817 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1822 max_spare_threads = atoi(arg);
1826 static const char *set_max_clients (cmd_parms *cmd, void *dummy,
1830 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1835 /* It is ok to use ap_threads_per_child here because we are
1836 * sure that it gets set before MaxClients in the pre_config stage. */
1837 max_clients = atoi(arg);
1838 if (max_clients < ap_threads_per_child) {
1839 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1840 "WARNING: MaxClients (%d) must be at least as large",
1842 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1843 " large as ThreadsPerChild (%d). Automatically",
1844 ap_threads_per_child);
1845 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1846 " increasing MaxClients to %d.",
1847 ap_threads_per_child);
1848 max_clients = ap_threads_per_child;
1850 ap_daemons_limit = max_clients / ap_threads_per_child;
1851 if ((max_clients > 0) && (max_clients % ap_threads_per_child)) {
1852 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1853 "WARNING: MaxClients (%d) is not an integer multiple",
1855 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1856 " of ThreadsPerChild (%d), lowering MaxClients to %d",
1857 ap_threads_per_child,
1858 ap_daemons_limit * ap_threads_per_child);
1859 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1860 " for a maximum of %d child processes,",
1862 max_clients = ap_daemons_limit * ap_threads_per_child;
1864 if (ap_daemons_limit > server_limit) {
1865 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1866 "WARNING: MaxClients of %d would require %d servers,",
1867 max_clients, ap_daemons_limit);
1868 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1869 " and would exceed the ServerLimit value of %d.",
1871 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1872 " Automatically lowering MaxClients to %d. To increase,",
1874 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1875 " please see the ServerLimit directive.");
1876 ap_daemons_limit = server_limit;
1878 else if (ap_daemons_limit < 1) {
1879 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1880 "WARNING: Require MaxClients > 0, setting to 1");
1881 ap_daemons_limit = 1;
1886 static const char *set_threads_per_child (cmd_parms *cmd, void *dummy,
1889 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1894 ap_threads_per_child = atoi(arg);
1895 if (ap_threads_per_child > thread_limit) {
1896 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1897 "WARNING: ThreadsPerChild of %d exceeds ThreadLimit "
1898 "value of %d", ap_threads_per_child,
1900 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1901 "threads, lowering ThreadsPerChild to %d. To increase, please"
1902 " see the", thread_limit);
1903 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1904 " ThreadLimit directive.");
1905 ap_threads_per_child = thread_limit;
1907 else if (ap_threads_per_child < 1) {
1908 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1909 "WARNING: Require ThreadsPerChild > 0, setting to 1");
1910 ap_threads_per_child = 1;
1915 static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
1917 int tmp_server_limit;
1919 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1924 tmp_server_limit = atoi(arg);
1925 /* you cannot change ServerLimit across a restart; ignore
1928 if (first_server_limit &&
1929 tmp_server_limit != server_limit) {
1930 /* how do we log a message? the error log is a bit bucket at this
1931 * point; we'll just have to set a flag so that ap_mpm_run()
1932 * logs a warning later
1934 changed_limit_at_restart = 1;
1937 server_limit = tmp_server_limit;
1939 if (server_limit > MAX_SERVER_LIMIT) {
1940 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1941 "WARNING: ServerLimit of %d exceeds compile time limit "
1942 "of %d servers,", server_limit, MAX_SERVER_LIMIT);
1943 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1944 " lowering ServerLimit to %d.", MAX_SERVER_LIMIT);
1945 server_limit = MAX_SERVER_LIMIT;
1947 else if (server_limit < 1) {
1948 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1949 "WARNING: Require ServerLimit > 0, setting to 1");
1955 static const char *set_thread_limit (cmd_parms *cmd, void *dummy, const char *arg)
1957 int tmp_thread_limit;
1959 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1964 tmp_thread_limit = atoi(arg);
1965 /* you cannot change ThreadLimit across a restart; ignore
1968 if (first_thread_limit &&
1969 tmp_thread_limit != thread_limit) {
1970 /* how do we log a message? the error log is a bit bucket at this
1971 * point; we'll just have to set a flag so that ap_mpm_run()
1972 * logs a warning later
1974 changed_limit_at_restart = 1;
1977 thread_limit = tmp_thread_limit;
1979 if (thread_limit > MAX_THREAD_LIMIT) {
1980 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1981 "WARNING: ThreadLimit of %d exceeds compile time limit "
1982 "of %d servers,", thread_limit, MAX_THREAD_LIMIT);
1983 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1984 " lowering ThreadLimit to %d.", MAX_THREAD_LIMIT);
1985 thread_limit = MAX_THREAD_LIMIT;
1987 else if (thread_limit < 1) {
1988 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1989 "WARNING: Require ThreadLimit > 0, setting to 1");
1995 static const command_rec worker_cmds[] = {
1996 UNIX_DAEMON_COMMANDS,
1998 AP_INIT_TAKE1("StartServers", set_daemons_to_start, NULL, RSRC_CONF,
1999 "Number of child processes launched at server startup"),
2000 AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
2001 "Minimum number of idle children, to handle request spikes"),
2002 AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
2003 "Maximum number of idle children"),
2004 AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
2005 "Maximum number of children alive at the same time"),
2006 AP_INIT_TAKE1("ThreadsPerChild", set_threads_per_child, NULL, RSRC_CONF,
2007 "Number of threads each child creates"),
2008 AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
2009 "Maximum value of MaxClients for this run of Apache"),
2010 AP_INIT_TAKE1("ThreadLimit", set_thread_limit, NULL, RSRC_CONF,
2011 "Maximum worker threads in a server for this run of Apache"),
2015 module AP_MODULE_DECLARE_DATA mpm_worker_module = {
2017 NULL, /* hook to run before apache parses args */
2018 NULL, /* create per-directory config structure */
2019 NULL, /* merge per-directory config structures */
2020 NULL, /* create per-server config structure */
2021 NULL, /* merge per-server config structures */
2022 worker_cmds, /* command apr_table_t */
2023 worker_hooks /* register_hooks */