1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 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.
61 #include "ap_config.h"
63 #include "apr_strings.h"
64 #include "apr_portable.h"
65 #include "apr_file_io.h"
67 #include "http_main.h"
69 #include "http_config.h" /* for read_config */
70 #include "http_core.h" /* for get_remote_host */
71 #include "http_protocol.h"
72 #include "http_connection.h"
75 #include "mpm_common.h"
77 #include "ap_listen.h"
78 #include "mpm_default.h"
80 #include "scoreboard.h"
86 #ifdef HAVE_SYS_SOCKET_H
87 #include <sys/socket.h>
89 #ifdef HAVE_NETINET_TCP_H
90 #include <netinet/tcp.h>
100 * Actual definitions of config globals
103 static int threads_to_start = 0; /* Worker threads per child */
104 static int min_spare_threads = 0;
105 static int max_spare_threads = 0;
106 static int max_threads = 0;
107 static int max_requests_per_child = 0;
108 static const char *ap_pid_fname=NULL;
109 API_VAR_EXPORT const char *ap_scoreboard_fname=NULL;
110 static int num_daemons=0;
111 static int workers_may_exit = 0;
112 static int requests_this_child;
113 static int num_listenfds = 0;
114 static ap_socket_t **listenfds;
116 struct child_info_t {
121 struct socket_info_t {
122 const char *sname; /* The servername */
123 int cnum; /* The child number */
127 const char *sockname;
128 } perchild_server_conf;
130 typedef struct child_info_t child_info_t;
131 typedef struct socket_info_t socket_info_t;
133 /* Tables used to determine the user and group each child process should
134 * run as. The hash table is used to correlate a server name with a child
137 static child_info_t child_info_table[HARD_SERVER_LIMIT];
138 static ap_hash_t *socket_info_table = NULL;
141 struct ap_ctable ap_child_table[HARD_SERVER_LIMIT];
144 * The max child slot ever assigned, preserved across restarts. Necessary
145 * to deal with NumServers changes across SIGWINCH restarts. We use this
146 * value to optimize routines that have to scan the entire child table.
148 * XXX - It might not be worth keeping this code in. There aren't very
149 * many child processes in this MPM.
151 int ap_max_daemons_limit = -1;
153 char ap_coredump_dir[MAX_STRING_LEN];
155 module MODULE_VAR_EXPORT mpm_perchild_module;
157 static ap_file_t *pipe_of_death_in = NULL;
158 static ap_file_t *pipe_of_death_out = NULL;
159 static pthread_mutex_t pipe_of_death_mutex;
161 /* *Non*-shared http_main globals... */
163 server_rec *ap_server_conf;
165 /* one_process --- debugging mode variable; can be set from the command line
166 * with the -X flag. If set, this gets you the child_main loop running
167 * in the process which originally started up (no detach, no make_child),
168 * which is a pretty nice debugging environment. (You'll get a SIGHUP
169 * early in standalone_main; just continue through. This is the server
170 * trying to kill off any child processes which it might have lying
171 * around --- Apache doesn't keep track of their pids, it just sends
172 * SIGHUP to the process group, ignoring it in the root process.
173 * Continue through and you'll be fine.).
176 static int one_process = 0;
179 int raise_sigstop_flags;
182 static ap_pool_t *pconf; /* Pool for config stuff */
183 static ap_pool_t *pchild; /* Pool for httpd child stuff */
184 static ap_pool_t *thread_pool_parent; /* Parent of per-thread pools */
185 static pthread_mutex_t thread_pool_parent_mutex;
187 static int child_num;
188 static unsigned int my_pid; /* Linux getpid() doesn't work except in
189 main thread. Use this instead */
190 /* Keep track of the number of worker threads currently active */
191 static int worker_thread_count;
192 static pthread_mutex_t worker_thread_count_mutex;
193 static int worker_thread_free_ids[HARD_THREAD_LIMIT];
194 static pthread_attr_t worker_thread_attr;
196 /* Keep track of the number of idle worker threads */
197 static int idle_thread_count;
198 static pthread_mutex_t idle_thread_count_mutex;
200 /* Locks for accept serialization */
201 #ifdef NO_SERIALIZED_ACCEPT
202 #define SAFE_ACCEPT(stmt) APR_SUCCESS
204 #define SAFE_ACCEPT(stmt) (stmt)
205 static ap_lock_t *process_accept_mutex;
206 #endif /* NO_SERIALIZED_ACCEPT */
207 static const char *lock_fname;
208 static pthread_mutex_t thread_accept_mutex = PTHREAD_MUTEX_INITIALIZER;
210 API_EXPORT(int) ap_get_max_daemons(void)
212 return ap_max_daemons_limit;
215 /* a clean exit from a child with proper cleanup */
216 static void clean_child_exit(int code)
219 ap_destroy_pool(pchild);
224 /* handle all varieties of core dumping signals */
225 static void sig_coredump(int sig)
227 chdir(ap_coredump_dir);
228 ap_signal(sig, SIG_DFL);
230 /* At this point we've got sig blocked, because we're still inside
231 * the signal handler. When we leave the signal handler it will
232 * be unblocked, and we'll take the signal... and coredump or whatever
233 * is appropriate for this particular Unix. In addition the parent
234 * will see the real signal we received -- whereas if we called
235 * abort() here, the parent would only see SIGABRT.
239 static void just_die(int sig)
244 /*****************************************************************
245 * Connection structures and accounting...
248 /* volatile just in case */
249 static int volatile shutdown_pending;
250 static int volatile restart_pending;
251 static int volatile is_graceful;
254 * ap_start_shutdown() and ap_start_restart(), below, are a first stab at
255 * functions to initiate shutdown or restart without relying on signals.
256 * Previously this was initiated in sig_term() and restart() signal handlers,
257 * but we want to be able to start a shutdown/restart from other sources --
258 * e.g. on Win32, from the service manager. Now the service manager can
259 * call ap_start_shutdown() or ap_start_restart() as appropiate. Note that
260 * these functions can also be called by the child processes, since global
261 * variables are no longer used to pass on the required action to the parent.
263 * These should only be called from the parent process itself, since the
264 * parent process will use the shutdown_pending and restart_pending variables
265 * to determine whether to shutdown or restart. The child process should
266 * call signal_parent() directly to tell the parent to die -- this will
267 * cause neither of those variable to be set, which the parent will
268 * assume means something serious is wrong (which it will be, for the
269 * child to force an exit) and so do an exit anyway.
272 void ap_start_shutdown(void)
274 if (shutdown_pending == 1) {
275 /* Um, is this _probably_ not an error, if the user has
276 * tried to do a shutdown twice quickly, so we won't
277 * worry about reporting it.
281 shutdown_pending = 1;
284 /* do a graceful restart if graceful == 1 */
285 static void ap_start_restart(int graceful)
288 if (restart_pending == 1) {
289 /* Probably not an error - don't bother reporting it */
293 is_graceful = graceful;
295 ap_kill_cleanup(pconf, NULL, ap_cleanup_shared_mem);
299 static void sig_term(int sig)
304 static void restart(int sig)
307 ap_start_restart(sig == SIGWINCH);
313 static void set_signals(void)
315 #ifndef NO_USE_SIGACTION
318 sigemptyset(&sa.sa_mask);
322 sa.sa_handler = sig_coredump;
323 #if defined(SA_ONESHOT)
324 sa.sa_flags = SA_ONESHOT;
325 #elif defined(SA_RESETHAND)
326 sa.sa_flags = SA_RESETHAND;
328 if (sigaction(SIGSEGV, &sa, NULL) < 0)
329 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
331 if (sigaction(SIGBUS, &sa, NULL) < 0)
332 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
335 if (sigaction(SIGABORT, &sa, NULL) < 0)
336 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABORT)");
339 if (sigaction(SIGABRT, &sa, NULL) < 0)
340 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
343 if (sigaction(SIGILL, &sa, NULL) < 0)
344 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
348 sa.sa_handler = sig_term;
349 if (sigaction(SIGTERM, &sa, NULL) < 0)
350 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
352 if (sigaction(SIGINT, &sa, NULL) < 0)
353 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
356 sa.sa_handler = SIG_DFL;
357 if (sigaction(SIGXCPU, &sa, NULL) < 0)
358 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");
361 sa.sa_handler = SIG_DFL;
362 if (sigaction(SIGXFSZ, &sa, NULL) < 0)
363 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");
366 sa.sa_handler = SIG_IGN;
367 if (sigaction(SIGPIPE, &sa, NULL) < 0)
368 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
371 /* we want to ignore HUPs and WINCH while we're busy processing one */
372 sigaddset(&sa.sa_mask, SIGHUP);
373 sigaddset(&sa.sa_mask, SIGWINCH);
374 sa.sa_handler = restart;
375 if (sigaction(SIGHUP, &sa, NULL) < 0)
376 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
377 if (sigaction(SIGWINCH, &sa, NULL) < 0)
378 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGWINCH)");
381 ap_signal(SIGSEGV, sig_coredump);
383 ap_signal(SIGBUS, sig_coredump);
386 ap_signal(SIGABORT, sig_coredump);
387 #endif /* SIGABORT */
389 ap_signal(SIGABRT, sig_coredump);
392 ap_signal(SIGILL, sig_coredump);
395 ap_signal(SIGXCPU, SIG_DFL);
398 ap_signal(SIGXFSZ, SIG_DFL);
402 ap_signal(SIGTERM, sig_term);
404 ap_signal(SIGHUP, restart);
407 ap_signal(SIGWINCH, restart);
408 #endif /* SIGWINCH */
410 ap_signal(SIGPIPE, SIG_IGN);
416 /*****************************************************************
417 * Here follows a long bunch of generic server bookkeeping stuff...
420 int ap_graceful_stop_signalled(void)
422 /* XXX - Does this really work? - Manoj */
426 /*****************************************************************
427 * Child process main loop.
430 static void process_socket(ap_pool_t *p, ap_socket_t *sock, long conn_id)
433 conn_rec *current_conn;
438 if ((rv = ap_get_os_sock(&csd, sock)) != APR_SUCCESS) {
439 ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "ap_get_os_sock");
442 if (csd >= FD_SETSIZE) {
443 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, NULL,
444 "new file descriptor %d is too large; you probably need "
445 "to rebuild Apache with a larger FD_SETSIZE "
448 ap_close_socket(sock);
452 ap_sock_disable_nagle(csd);
453 iol = ap_iol_attach_socket(p, sock);
454 conn_io = ap_bcreate(p, B_RDWR);
455 ap_bpush_iol(conn_io, iol);
457 current_conn = ap_new_apr_connection(p, ap_server_conf, conn_io, sock,
460 ap_process_connection(current_conn);
461 ap_lingering_close(current_conn);
464 static void *worker_thread(void *);
466 /* Starts a thread as long as we're below max_threads */
467 static int start_thread(void)
472 pthread_mutex_lock(&worker_thread_count_mutex);
473 if (worker_thread_count < max_threads) {
474 if ((rc = pthread_create(&thread, &worker_thread_attr, worker_thread,
475 &worker_thread_free_ids[worker_thread_count]))) {
476 #ifdef PTHREAD_SETS_ERRNO
479 ap_log_error(APLOG_MARK, APLOG_ALERT, rc, ap_server_conf,
480 "pthread_create: unable to create worker thread");
481 /* In case system resources are maxxed out, we don't want
482 Apache running away with the CPU trying to fork over and
483 over and over again if we exit. */
485 workers_may_exit = 1;
486 pthread_mutex_unlock(&worker_thread_count_mutex);
490 worker_thread_count++;
494 static int reported = 0;
497 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, ap_server_conf,
498 "server reached MaxThreadsPerChild setting, consider raising the"
499 " MaxThreadsPerChild or NumServers settings");
502 pthread_mutex_unlock(&worker_thread_count_mutex);
505 pthread_mutex_unlock(&worker_thread_count_mutex);
509 /* Sets workers_may_exit if we received a character on the pipe_of_death */
510 static void check_pipe_of_death(void)
512 pthread_mutex_lock(&pipe_of_death_mutex);
513 if (!workers_may_exit) {
518 ret = ap_recv(listenfds[0], &pipe_read_char, &n);
519 if (ap_canonical_error(ret) == APR_EAGAIN) {
520 /* It lost the lottery. It must continue to suffer
521 * through a life of servitude. */
524 /* It won the lottery (or something else is very
525 * wrong). Embrace death with open arms. */
526 workers_may_exit = 1;
529 pthread_mutex_unlock(&pipe_of_death_mutex);
532 /* idle_thread_count should be incremented before starting a worker_thread */
534 static void *worker_thread(void *arg)
536 ap_socket_t *csd = NULL;
537 ap_pool_t *tpool; /* Pool for this thread */
538 ap_pool_t *ptrans; /* Pool for per-transaction stuff */
539 ap_socket_t *sd = NULL;
541 int curr_pollfd, last_pollfd = 0;
542 int thread_just_started = 1;
543 int thread_num = *((int *) arg);
544 long conn_id = child_num * HARD_THREAD_LIMIT + thread_num;
545 ap_pollfd_t *pollset;
549 pthread_mutex_lock(&thread_pool_parent_mutex);
550 ap_create_pool(&tpool, thread_pool_parent);
551 pthread_mutex_unlock(&thread_pool_parent_mutex);
552 ap_create_pool(&ptrans, tpool);
554 ap_setup_poll(&pollset, num_listenfds+1, tpool);
555 for(n=0 ; n <= num_listenfds ; ++n)
556 ap_add_poll_socket(pollset, listenfds[n], APR_POLLIN);
558 while (!workers_may_exit) {
559 workers_may_exit |= (max_requests_per_child != 0) && (requests_this_child <= 0);
560 if (workers_may_exit) break;
561 if (!thread_just_started) {
562 pthread_mutex_lock(&idle_thread_count_mutex);
563 if (idle_thread_count < max_spare_threads) {
565 pthread_mutex_unlock(&idle_thread_count_mutex);
568 pthread_mutex_unlock(&idle_thread_count_mutex);
573 thread_just_started = 0;
575 pthread_mutex_lock(&thread_accept_mutex);
576 if (workers_may_exit) {
577 pthread_mutex_unlock(&thread_accept_mutex);
580 if ((rv = SAFE_ACCEPT(ap_lock(process_accept_mutex)))
582 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
583 "ap_lock failed. Attempting to shutdown "
584 "process gracefully.");
585 workers_may_exit = 1;
588 while (!workers_may_exit) {
590 srv = ap_poll(pollset, &n, -1);
592 if (srv != APR_SUCCESS) {
593 if (ap_canonical_error(srv) == APR_EINTR) {
597 /* ap_poll() will only return errors in catastrophic
598 * circumstances. Let's try exiting gracefully, for now. */
599 ap_log_error(APLOG_MARK, APLOG_ERR, srv, (const server_rec *)
600 ap_server_conf, "ap_poll: (listen)");
601 workers_may_exit = 1;
603 if (workers_may_exit) break;
605 ap_get_revents(&event, listenfds[0], pollset);
606 if (event & APR_POLLIN) {
607 /* A process got a signal on the shutdown pipe. Check if we're
608 * the lucky process to die. */
609 check_pipe_of_death();
613 if (num_listenfds == 1) {
614 sd = ap_listeners->sd;
618 /* find a listener */
619 curr_pollfd = last_pollfd;
622 if (curr_pollfd > num_listenfds) {
625 /* XXX: Should we check for POLLERR? */
626 ap_get_revents(&event, listenfds[curr_pollfd], pollset);
627 if (event & APR_POLLIN) {
628 last_pollfd = curr_pollfd;
629 sd = listenfds[curr_pollfd];
632 } while (curr_pollfd != last_pollfd);
636 if (!workers_may_exit) {
637 if ((rv = ap_accept(&csd, sd, ptrans)) != APR_SUCCESS) {
638 ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, "ap_accept");
640 if ((rv = SAFE_ACCEPT(ap_unlock(process_accept_mutex)))
642 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
643 "ap_unlock failed. Attempting to shutdown "
644 "process gracefully.");
645 workers_may_exit = 1;
647 pthread_mutex_unlock(&thread_accept_mutex);
648 pthread_mutex_lock(&idle_thread_count_mutex);
649 if (idle_thread_count > min_spare_threads) {
653 if (!start_thread()) {
657 pthread_mutex_unlock(&idle_thread_count_mutex);
658 process_socket(ptrans, csd, conn_id);
659 requests_this_child--;
661 if ((rv = SAFE_ACCEPT(ap_unlock(process_accept_mutex)))
663 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
664 "ap_unlock failed. Attempting to shutdown "
665 "process gracefully.");
666 workers_may_exit = 1;
668 pthread_mutex_unlock(&thread_accept_mutex);
669 pthread_mutex_lock(&idle_thread_count_mutex);
671 pthread_mutex_unlock(&idle_thread_count_mutex);
674 ap_clear_pool(ptrans);
677 pthread_mutex_lock(&thread_pool_parent_mutex);
678 ap_destroy_pool(tpool);
679 pthread_mutex_unlock(&thread_pool_parent_mutex);
680 pthread_mutex_lock(&worker_thread_count_mutex);
681 worker_thread_count--;
682 worker_thread_free_ids[worker_thread_count] = thread_num;
683 if (worker_thread_count == 0) {
684 /* All the threads have exited, now finish the shutdown process
685 * by signalling the sigwait thread */
686 kill(my_pid, SIGTERM);
688 pthread_mutex_unlock(&worker_thread_count_mutex);
693 /* Set group privileges.
695 * Note that we use the username as set in the config files, rather than
696 * the lookup of to uid --- the same uid may have multiple passwd entries,
697 * with different sets of groups for each.
700 static int set_group_privs(uid_t uid, gid_t gid)
705 /* Get username if passed as a uid */
709 if ((ent = getpwuid(uid)) == NULL) {
710 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
711 "getpwuid: couldn't determine user name from uid %u, "
712 "you probably need to modify the User directive",
720 * Set the GID before initgroups(), since on some platforms
721 * setgid() is known to zap the group list.
723 if (setgid(gid) == -1) {
724 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
725 "setgid: unable to set group id to Group %u",
730 /* Reset `groups' attributes. */
732 if (initgroups(name, gid) == -1) {
733 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
734 "initgroups: unable to set groups for User %s "
735 "and Group %u", name, (unsigned)gid);
743 static int perchild_setup_child(int childnum)
745 child_info_t *ug = &child_info_table[childnum];
747 if (ug->uid == -1 && ug->gid == -1) {
748 return unixd_setup_child();
750 if (set_group_privs(ug->uid, ug->gid)) {
753 /* Only try to switch if we're running as root */
756 os_init_job_environment(server_conf, unixd_config.user_name, one_process) != 0 ||
758 setuid(ug->uid) == -1)) {
759 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
760 "setuid: unable to change to uid: %ld",
767 static int create_child_socket(int child_num, ap_pool_t *p)
769 struct sockaddr_un unix_addr;
773 perchild_server_conf *sconf = (perchild_server_conf *)
774 ap_get_module_config(ap_server_conf->module_config, &mpm_perchild_module);
775 char *socket_name = ap_palloc(p, strlen(sconf->sockname) + 6);
777 ap_snprintf(socket_name, strlen(socket_name), "%s.%d", sconf->sockname, child_num);
778 if (unlink(socket_name) < 0 &&
780 ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
781 "Couldn't unlink unix domain socket %s",
783 /* Just a warning; don't bail out */
786 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
787 ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
788 "Couldn't create unix domain socket");
792 memset(&unix_addr, 0, sizeof(unix_addr));
793 unix_addr.sun_family = AF_UNIX;
794 strcpy(unix_addr.sun_path, socket_name);
796 omask = umask(0077); /* so that only Apache can use socket */
797 rc = bind(sd, (struct sockaddr *)&unix_addr, sizeof(unix_addr));
798 umask(omask); /* can't fail, so can't clobber errno */
800 ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
801 "Couldn't bind unix domain socket %s",
806 if (listen(sd, DEFAULT_PERCHILD_LISTENBACKLOG) < 0) {
807 ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
808 "Couldn't listen on unix domain socket");
813 if (chown(socket_name, unixd_config.user_id, -1) < 0) {
814 ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
815 "Couldn't change owner of unix domain socket %s",
823 static void child_main(int child_num_arg)
833 child_num = child_num_arg;
834 ap_create_pool(&pchild, pconf);
836 /*stuff to do before we switch id's, so we have permissions.*/
838 rv = SAFE_ACCEPT(ap_child_init_lock(&process_accept_mutex, lock_fname,
840 if (rv != APR_SUCCESS) {
841 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
842 "Couldn't initialize cross-process lock in child");
843 clean_child_exit(APEXIT_CHILDFATAL);
846 /* Add the sockets for this child process's virtual hosts */
847 sd = create_child_socket(child_num, pchild);
849 if (perchild_setup_child(child_num)) {
850 clean_child_exit(APEXIT_CHILDFATAL);
853 ap_child_init_hook(pchild, ap_server_conf);
855 /*done with init critical section */
857 /* All threads should mask signals out, accoring to sigwait(2) man page */
858 sigfillset(&sig_mask);
860 #ifdef SIGPROCMASK_SETS_THREAD_MASK
861 if (sigprocmask(SIG_SETMASK, &sig_mask, NULL) != 0) {
862 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf, "sigprocmask");
865 if ((rv = pthread_sigmask(SIG_SETMASK, &sig_mask, NULL)) != 0) {
866 #ifdef PTHREAD_SETS_ERRNO
869 ap_log_error(APLOG_MARK, APLOG_ALERT, rv, ap_server_conf,
874 requests_this_child = max_requests_per_child;
876 /* Set up the pollfd array, num_listenfds + 1 for the pipe and 1 for
879 listenfds = ap_pcalloc(pchild, sizeof(*listenfds) * (num_listenfds + 2));
880 #if APR_FILES_AS_SOCKETS
881 ap_socket_from_file(&listenfds[0], pipe_of_death_in);
883 for (lr = ap_listeners, i = 1; i <= num_listenfds; lr = lr->next, ++i)
886 /* Setup worker threads */
888 if (threads_to_start > max_threads) {
889 threads_to_start = max_threads;
891 idle_thread_count = threads_to_start;
892 worker_thread_count = 0;
893 for (i = 0; i < max_threads; i++) {
894 worker_thread_free_ids[i] = i;
896 ap_create_pool(&thread_pool_parent, pchild);
897 pthread_mutex_init(&thread_pool_parent_mutex, NULL);
898 pthread_mutex_init(&idle_thread_count_mutex, NULL);
899 pthread_mutex_init(&worker_thread_count_mutex, NULL);
900 pthread_mutex_init(&pipe_of_death_mutex, NULL);
901 pthread_attr_init(&worker_thread_attr);
902 #ifdef PTHREAD_ATTR_SETDETACHSTATE_ARG2_ADDR
906 pthread_attr_setdetachstate(&worker_thread_attr, &on);
909 pthread_attr_setdetachstate(&worker_thread_attr, PTHREAD_CREATE_DETACHED);
912 /* We are creating worker threads right now */
913 for (i=0; i < threads_to_start; i++) {
914 /* start_thread shouldn't fail here */
915 if (!start_thread()) {
920 /* This thread will be the one responsible for handling signals */
921 sigemptyset(&sig_mask);
922 sigaddset(&sig_mask, SIGTERM);
923 sigaddset(&sig_mask, SIGINT);
924 ap_sigwait(&sig_mask, &signal_received);
925 switch (signal_received) {
928 just_die(signal_received);
931 ap_log_error(APLOG_MARK, APLOG_ALERT, errno, ap_server_conf,
932 "received impossible signal: %d", signal_received);
937 static int make_child(server_rec *s, int slot, time_t now)
941 if (slot + 1 > ap_max_daemons_limit) {
942 ap_max_daemons_limit = slot + 1;
947 ap_child_table[slot].pid = getpid();
948 ap_child_table[slot].status = SERVER_ALIVE;
952 if ((pid = fork()) == -1) {
953 ap_log_error(APLOG_MARK, APLOG_ERR, errno, s,
954 "fork: Unable to fork new process");
955 /* In case system resources are maxxed out, we don't want
956 Apache running away with the CPU trying to fork over and
957 over and over again. */
964 #ifdef AIX_BIND_PROCESSOR
965 /* By default, AIX binds to a single processor. This bit unbinds
966 children which will then bind to another CPU.
968 #include <sys/processor.h>
969 int status = bindprocessor(BINDPROCESS, (int)getpid(),
970 PROCESSOR_CLASS_ANY);
972 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, errno,
973 ap_server_conf, "processor unbind failed %d", status);
976 RAISE_SIGSTOP(MAKE_CHILD);
978 /* XXX - For an unthreaded server, a signal handler will be necessary
979 ap_signal(SIGTERM, just_die);
986 ap_child_table[slot].pid = pid;
987 ap_child_table[slot].status = SERVER_ALIVE;
992 /* start up a bunch of children */
993 static int startup_children(int number_to_start)
997 for (i = 0; number_to_start && i < num_daemons; ++i) {
998 if (ap_child_table[i].pid) {
1001 if (make_child(ap_server_conf, i, 0) < 0) {
1006 return number_to_start;
1011 * spawn_rate is the number of children that will be spawned on the
1012 * next maintenance cycle if there aren't enough servers. It is
1013 * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
1014 * without the need to spawn.
1016 static int spawn_rate = 1;
1017 #ifndef MAX_SPAWN_RATE
1018 #define MAX_SPAWN_RATE (32)
1020 static int hold_off_on_exponential_spawning;
1022 static void perform_child_maintenance(void)
1027 int free_slots[MAX_SPAWN_RATE];
1028 int last_non_dead = -1;
1030 /* initialize the free_list */
1033 for (i = 0; i < num_daemons; ++i) {
1034 if (ap_child_table[i].pid == 0) {
1035 if (free_length < spawn_rate) {
1036 free_slots[free_length] = i;
1044 if (i >= ap_max_daemons_limit && free_length >= spawn_rate) {
1048 ap_max_daemons_limit = last_non_dead + 1;
1050 if (free_length > 0) {
1051 for (i = 0; i < free_length; ++i) {
1052 make_child(ap_server_conf, free_slots[i], now);
1054 /* the next time around we want to spawn twice as many if this
1055 * wasn't good enough, but not if we've just done a graceful
1057 if (hold_off_on_exponential_spawning) {
1058 --hold_off_on_exponential_spawning;
1060 else if (spawn_rate < MAX_SPAWN_RATE) {
1069 static void server_main_loop(int remaining_children_to_start)
1076 while (!restart_pending && !shutdown_pending) {
1077 ap_wait_or_timeout(&status, &pid, pconf);
1079 if (pid.pid != -1) {
1080 ap_process_child_status(&pid, status);
1081 /* non-fatal death... note that it's gone in the child table and
1082 * clean out the status table. */
1084 for (i = 0; i < ap_max_daemons_limit; ++i) {
1085 if (ap_child_table[i].pid == pid.pid) {
1089 for (j = 0; j < HARD_THREAD_LIMIT; j++) {
1090 ap_perchild_force_reset_connection_status(i * HARD_THREAD_LIMIT + j);
1095 if (child_slot >= 0) {
1096 ap_child_table[child_slot].pid = 0;
1098 if (remaining_children_to_start
1099 && child_slot < num_daemons) {
1100 /* we're still doing a 1-for-1 replacement of dead
1101 * children with new children
1103 make_child(ap_server_conf, child_slot, time(NULL));
1104 --remaining_children_to_start;
1106 #if APR_HAS_OTHER_CHILD
1108 else if (ap_reap_other_child(&pid, status) == 0) {
1112 else if (is_graceful) {
1113 /* Great, we've probably just lost a slot in the
1114 * child table. Somehow we don't know about this
1117 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0,
1119 "long lost child came home! (pid %ld)",
1122 /* Don't perform idle maintenance when a child dies,
1123 * only do it when there's a timeout. Remember only a
1124 * finite number of children can die, and it's pretty
1125 * pathological for a lot to die suddenly.
1129 else if (remaining_children_to_start) {
1130 /* we hit a 1 second timeout in which none of the previous
1131 * generation of children needed to be reaped... so assume
1132 * they're all done, and pick up the slack if any is left.
1134 remaining_children_to_start = \
1135 startup_children(remaining_children_to_start);
1136 /* In any event we really shouldn't do the code below because
1137 * few of the servers we just started are in the IDLE state
1138 * yet, so we'd mistakenly create an extra server.
1143 perform_child_maintenance();
1147 int ap_mpm_run(ap_pool_t *_pconf, ap_pool_t *plog, server_rec *s)
1149 int remaining_children_to_start;
1156 if ((rv = ap_create_pipe(&pipe_of_death_in, &pipe_of_death_out, pconf))
1158 ap_log_error(APLOG_MARK, APLOG_ERR, rv,
1159 (const server_rec*) ap_server_conf,
1160 "ap_create_pipe (pipe_of_death)");
1163 if ((rv = ap_set_pipe_timeout(pipe_of_death_in, 0)) != APR_SUCCESS) {
1164 ap_log_error(APLOG_MARK, APLOG_ERR, rv,
1165 (const server_rec*) ap_server_conf,
1166 "ap_set_pipe_timeout (pipe_of_death)");
1170 if ((num_listenfds = ap_setup_listeners(ap_server_conf)) < 1) {
1171 /* XXX: hey, what's the right way for the mpm to indicate a fatal error? */
1172 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ALERT, 0, s,
1173 "no listening sockets available, shutting down");
1176 ap_log_pid(pconf, ap_pid_fname);
1178 /* Initialize cross-process accept lock */
1179 lock_fname = ap_psprintf(_pconf, "%s.%u",
1180 ap_server_root_relative(_pconf, lock_fname),
1182 rv = SAFE_ACCEPT(ap_create_lock(&process_accept_mutex, APR_MUTEX,
1183 APR_CROSS_PROCESS, lock_fname, _pconf));
1184 if (rv != APR_SUCCESS) {
1185 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
1186 "Couldn't create cross-process lock");
1191 reinit_scoreboard(pconf);
1193 /* Initialize the child table */
1195 for (i = 0; i < HARD_SERVER_LIMIT; i++) {
1196 ap_child_table[i].pid = 0;
1202 /* If we're doing a graceful_restart then we're going to see a lot
1203 * of children exiting immediately when we get into the main loop
1204 * below (because we just sent them SIGWINCH). This happens pretty
1205 * rapidly... and for each one that exits we'll start a new one until
1206 * we reach at least daemons_min_free. But we may be permitted to
1207 * start more than that, so we'll just keep track of how many we're
1208 * supposed to start up without the 1 second penalty between each fork.
1210 remaining_children_to_start = num_daemons;
1212 remaining_children_to_start = \
1213 startup_children(remaining_children_to_start);
1216 /* give the system some time to recover before kicking into
1217 * exponential mode */
1218 hold_off_on_exponential_spawning = 10;
1221 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
1222 "%s configured -- resuming normal operations",
1223 ap_get_server_version());
1224 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, ap_server_conf,
1225 "Server built: %s", ap_get_server_built());
1226 restart_pending = shutdown_pending = 0;
1228 server_main_loop(remaining_children_to_start);
1230 if (shutdown_pending) {
1231 /* Time to gracefully shut down:
1232 * Kill child processes, tell them to call child_exit, etc...
1234 if (unixd_killpg(getpgrp(), SIGTERM) < 0) {
1235 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
1238 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1240 /* cleanup pid file on normal shutdown */
1242 const char *pidfile = NULL;
1243 pidfile = ap_server_root_relative (pconf, ap_pid_fname);
1244 if ( pidfile != NULL && unlink(pidfile) == 0)
1245 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0,
1247 "removed PID file %s (pid=%ld)",
1248 pidfile, (long)getpid());
1251 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0,
1252 ap_server_conf, "caught SIGTERM, shutting down");
1257 /* we've been told to restart */
1258 ap_signal(SIGHUP, SIG_IGN);
1261 /* not worth thinking about */
1266 char char_of_death = '!';
1268 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf,
1269 "SIGWINCH received. Doing graceful restart");
1271 /* This is mostly for debugging... so that we know what is still
1272 * gracefully dealing with existing request.
1275 for (i = 0; i < num_daemons; ++i) {
1276 if (ap_child_table[i].pid) {
1277 ap_child_table[i].status = SERVER_DYING;
1280 /* give the children the signal to die */
1281 for (i = 0; i < num_daemons;) {
1282 if ((rv = ap_write(pipe_of_death_out, &char_of_death, &one)) != APR_SUCCESS) {
1283 if (ap_canonical_error(rv) == APR_EINTR) continue;
1284 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
1285 "write pipe_of_death");
1291 /* Kill 'em all. Since the child acts the same on the parents SIGTERM
1292 * and a SIGHUP, we may as well use the same signal, because some user
1293 * pthreads are stealing signals from us left and right.
1295 if (unixd_killpg(getpgrp(), SIGTERM) < 0) {
1296 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf,
1299 ap_reclaim_child_processes(1); /* Start with SIGTERM */
1300 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0,
1301 ap_server_conf, "SIGHUP received. Attempting to restart");
1306 static void perchild_pre_config(ap_pool_t *p, ap_pool_t *plog, ap_pool_t *ptemp)
1308 static int restart_num = 0;
1311 one_process = !!getenv("ONE_PROCESS");
1313 /* sigh, want this only the second time around */
1314 if (restart_num++ == 1) {
1325 ap_listen_pre_config();
1326 num_daemons = DEFAULT_NUM_DAEMON;
1327 threads_to_start = DEFAULT_START_THREAD;
1328 min_spare_threads = DEFAULT_MIN_SPARE_THREAD;
1329 max_spare_threads = DEFAULT_MAX_SPARE_THREAD;
1330 max_threads = HARD_THREAD_LIMIT;
1331 ap_pid_fname = DEFAULT_PIDLOG;
1332 ap_scoreboard_fname = DEFAULT_SCOREBOARD;
1333 lock_fname = DEFAULT_LOCKFILE;
1334 max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
1335 ap_perchild_set_maintain_connection_status(1);
1337 ap_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
1339 for (i = 0; i < HARD_SERVER_LIMIT; i++) {
1340 child_info_table[i].uid = -1;
1341 child_info_table[i].gid = -1;
1345 static int perchild_post_read(request_rec *r)
1347 const char *hostname = ap_table_get(r->headers_in, "Host");
1351 fprintf(stderr, "In perchild_post_read\n");
1352 process_num = ap_hash_get(socket_info_table, hostname, 0);
1354 num = atoi(process_num);
1360 if (num != child_num) {
1361 /* package the request and send it to another child */
1362 fprintf(stderr, "leaving DECLINED\n");
1365 fprintf(stderr, "leaving OK\n");
1369 static void perchild_hooks(void)
1374 ap_hook_pre_config(perchild_pre_config, NULL, NULL, AP_HOOK_MIDDLE);
1375 /* This must be run absolutely first. If this request isn't for this
1376 * server then we need to forward it to the proper child. No sense
1377 * tying up this server running more post_read request hooks if it is
1378 * just going to be forwarded along.
1380 ap_hook_post_read_request(perchild_post_read, NULL, NULL, AP_HOOK_REALLY_FIRST);
1383 static const char *set_pidfile(cmd_parms *cmd, void *dummy, const char *arg)
1385 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1390 if (cmd->server->is_virtual) {
1391 return "PidFile directive not allowed in <VirtualHost>";
1397 static const char *set_scoreboard(cmd_parms *cmd, void *dummy, const char *arg)
1399 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1404 ap_scoreboard_fname = arg;
1408 static const char *set_lockfile(cmd_parms *cmd, void *dummy, const char *arg)
1410 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1418 static const char *set_num_daemons (cmd_parms *cmd, void *dummy, const char *arg)
1420 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1425 num_daemons = atoi(arg);
1426 if (num_daemons > HARD_SERVER_LIMIT) {
1427 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1428 "WARNING: NumServers of %d exceeds compile time limit "
1429 "of %d servers,", num_daemons, HARD_SERVER_LIMIT);
1430 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1431 " lowering NumServers to %d. To increase, please "
1432 "see the", HARD_SERVER_LIMIT);
1433 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1434 " HARD_SERVER_LIMIT define in %s.",
1435 AP_MPM_HARD_LIMITS_FILE);
1436 num_daemons = HARD_SERVER_LIMIT;
1438 else if (num_daemons < 1) {
1439 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1440 "WARNING: Require NumServers > 0, setting to 1");
1446 static const char *set_threads_to_start (cmd_parms *cmd, void *dummy, const char *arg)
1448 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1453 threads_to_start = atoi(arg);
1454 if (threads_to_start > HARD_THREAD_LIMIT) {
1455 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1456 "WARNING: StartThreads of %d exceeds compile time"
1457 " limit of %d threads,", threads_to_start,
1459 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1460 " lowering StartThreads to %d. To increase, please"
1461 " see the", HARD_THREAD_LIMIT);
1462 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1463 " HARD_THREAD_LIMIT define in %s.",
1464 AP_MPM_HARD_LIMITS_FILE);
1466 else if (threads_to_start < 1) {
1467 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1468 "WARNING: Require StartThreads > 0, setting to 1");
1469 threads_to_start = 1;
1474 static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy, const char *arg)
1476 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1481 min_spare_threads = atoi(arg);
1482 if (min_spare_threads <= 0) {
1483 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1484 "WARNING: detected MinSpareThreads set to non-positive.");
1485 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1486 "Resetting to 1 to avoid almost certain Apache failure.");
1487 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1488 "Please read the documentation.");
1489 min_spare_threads = 1;
1495 static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy, const char *arg)
1497 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1502 max_spare_threads = atoi(arg);
1503 if (max_spare_threads >= HARD_THREAD_LIMIT) {
1504 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1505 "WARNING: detected MinSpareThreads set higher than");
1506 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1507 "HARD_THREAD_LIMIT. Resetting to %d", HARD_THREAD_LIMIT);
1508 max_spare_threads = HARD_THREAD_LIMIT;
1513 static const char *set_max_threads(cmd_parms *cmd, void *dummy, const char *arg)
1515 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1520 max_threads = atoi(arg);
1521 if (max_threads > HARD_THREAD_LIMIT) {
1522 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1523 "WARNING: detected MaxThreadsPerChild set higher than");
1524 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1525 "HARD_THREAD_LIMIT. Resetting to %d", HARD_THREAD_LIMIT);
1526 max_threads = HARD_THREAD_LIMIT;
1531 static const char *set_max_requests(cmd_parms *cmd, void *dummy, const char *arg)
1533 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1538 max_requests_per_child = atoi(arg);
1543 static const char *set_maintain_connection_status(cmd_parms *cmd,
1544 void *dummy, int arg)
1546 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1551 ap_perchild_set_maintain_connection_status(arg != 0);
1555 static const char *set_coredumpdir (cmd_parms *cmd, void *dummy, const char *arg)
1559 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1564 fname = ap_server_root_relative(cmd->pool, arg);
1565 if ((ap_stat(&finfo, fname, cmd->pool) != APR_SUCCESS) ||
1566 (finfo.filetype != APR_DIR)) {
1567 return ap_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
1568 " does not exist or is not a directory", NULL);
1570 ap_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
1574 static const char *set_childprocess(cmd_parms *cmd, void *dummy, const char *p,
1575 const char *u, const char *g)
1577 int curr_child_num = atoi(p);
1578 child_info_t *ug = &child_info_table[curr_child_num - 1];
1580 if (curr_child_num > num_daemons) {
1581 return "Trying to use more child ID's than NumServers. Increase "
1582 "NumServers in your config file.";
1591 static const char *set_socket_name(cmd_parms *cmd, void *dummy, const char *arg)
1593 server_rec *s = cmd->server;
1594 perchild_server_conf *conf = (perchild_server_conf *)
1595 ap_get_module_config(s->module_config, &mpm_perchild_module);
1597 conf->sockname = ap_server_root_relative(cmd->pool, arg);
1601 static ap_status_t cleanup_hash(void *dptr)
1603 socket_info_table = NULL;
1607 static const char *assign_childprocess(cmd_parms *cmd, void *dummy, const char *p)
1609 if (socket_info_table == NULL) {
1610 socket_info_table = ap_make_hash(cmd->pool);
1611 ap_register_cleanup(cmd->pool, socket_info_table, cleanup_hash, NULL);
1613 ap_hash_set(socket_info_table, cmd->server->server_hostname, 0, p);
1618 static const command_rec perchild_cmds[] = {
1619 UNIX_DAEMON_COMMANDS
1621 AP_INIT_TAKE1("PidFile", set_pidfile, NULL, RSRC_CONF,
1622 "A file for logging the server process ID"),
1623 AP_INIT_TAKE1("ScoreBoardFile", set_scoreboard, NULL, RSRC_CONF,
1624 "A file for Apache to maintain runtime process management information"),
1625 AP_INIT_TAKE1("LockFile", set_lockfile, NULL, RSRC_CONF,
1626 "The lockfile used when Apache needs to lock the accept() call"),
1627 AP_INIT_TAKE1("NumServers", set_num_daemons, NULL, RSRC_CONF,
1628 "Number of children alive at the same time"),
1629 AP_INIT_TAKE1("StartThreads", set_threads_to_start, NULL, RSRC_CONF,
1630 "Number of threads each child creates"),
1631 AP_INIT_TAKE1("MinSpareThreads", set_min_spare_threads, NULL, RSRC_CONF,
1632 "Minimum number of idle threads per child, to handle request spikes"),
1633 AP_INIT_TAKE1("MaxSpareThreads", set_max_spare_threads, NULL, RSRC_CONF,
1634 "Maximum number of idle threads per child"),
1635 AP_INIT_TAKE1("MaxThreadsPerChild", set_max_threads, NULL, RSRC_CONF,
1636 "Maximum number of threads per child"),
1637 AP_INIT_TAKE1("MaxRequestsPerChild", set_max_requests, NULL, RSRC_CONF,
1638 "Maximum number of requests a particular child serves before dying."),
1639 AP_INIT_FLAG("ConnectionStatus", set_maintain_connection_status, NULL, RSRC_CONF,
1640 "Whether or not to maintain status information on current connections"),
1641 AP_INIT_TAKE1("CoreDumpDirectory", set_coredumpdir, NULL, RSRC_CONF,
1642 "The location of the directory Apache changes to before dumping core"),
1643 AP_INIT_TAKE3("ChildProcess", set_childprocess, NULL, RSRC_CONF,
1644 "Specify a User and Group for a specific child process."),
1645 AP_INIT_TAKE1("AssignChild", assign_childprocess, NULL, RSRC_CONF,
1646 "Tie a virtual host to a specific child process."),
1647 AP_INIT_TAKE1("ChildSockName", set_socket_name, NULL, RSRC_CONF,
1648 "the base name of the socket to use for communication between "
1649 "child processes. The actual socket will be "
1650 "basename.childnum"),
1654 static void *perchild_create_config(ap_pool_t *p, server_rec *s)
1656 perchild_server_conf *c =
1657 (perchild_server_conf *) ap_pcalloc(p, sizeof(perchild_server_conf));
1659 c->sockname = ap_server_root_relative(p, DEFAULT_PERCHILD_SOCKET);
1663 module MODULE_VAR_EXPORT mpm_perchild_module = {
1665 NULL, /* hook to run before apache parses args */
1666 NULL, /* create per-directory config structure */
1667 NULL, /* merge per-directory config structures */
1668 perchild_create_config, /* create per-server config structure */
1669 NULL, /* merge per-server config structures */
1670 perchild_cmds, /* command ap_table_t */
1671 NULL, /* handlers */
1672 perchild_hooks /* register_hooks */