1 diff -Nur httpd-2.0.52/server/mpm/config.m4 httpd-2.0.52-peruser/server/mpm/config.m4
2 --- httpd-2.0.52/server/mpm/config.m4 2003-03-11 00:07:52.000000000 -0700
3 +++ httpd-2.0.52-peruser/server/mpm/config.m4 2005-07-12 01:57:22.000000000 -0600
5 AC_MSG_CHECKING(which MPM to use)
7 APACHE_HELP_STRING(--with-mpm=MPM,Choose the process model for Apache to use.
8 - MPM={beos|worker|prefork|mpmt_os2|perchild|leader|threadpool|metuxmpm}),[
9 + MPM={beos|worker|prefork|mpmt_os2|perchild|peruser|leader|threadpool|metuxmpm}),[
12 if test "x$APACHE_MPM" = "x"; then
14 APACHE_FAST_OUTPUT(server/mpm/Makefile)
16 MPM_NAME=$apache_cv_mpm
17 -if test "$MPM_NAME" = "leader" -o "$MPM_NAME" = "threadpool" -o "$MPM_NAME" = "perchild" -o "$MPM_NAME" = "metuxmpm"; then
18 +if test "$MPM_NAME" = "leader" -o "$MPM_NAME" = "threadpool" -o "$MPM_NAME" = "perchild" -o "$MPM_NAME" = "metuxmpm" -o "$MPM_NAME" = "peruser"; then
19 AC_MSG_WARN(You have selected an EXPERIMENTAL MPM. Be warned!)
20 MPM_SUBDIR_NAME=experimental/$MPM_NAME
22 diff -Nur httpd-2.0.52/server/mpm/experimental/peruser/Makefile.in httpd-2.0.52-peruser/server/mpm/experimental/peruser/Makefile.in
23 --- httpd-2.0.52/server/mpm/experimental/peruser/Makefile.in 1969-12-31 17:00:00.000000000 -0700
24 +++ httpd-2.0.52-peruser/server/mpm/experimental/peruser/Makefile.in 2005-07-12 01:57:22.000000000 -0600
27 +LTLIBRARY_NAME = libperuser.la
28 +LTLIBRARY_SOURCES = peruser.c
30 +include $(top_srcdir)/build/ltlib.mk
31 diff -Nur httpd-2.0.52/server/mpm/experimental/peruser/config.m4 httpd-2.0.52-peruser/server/mpm/experimental/peruser/config.m4
32 --- httpd-2.0.52/server/mpm/experimental/peruser/config.m4 1969-12-31 17:00:00.000000000 -0700
33 +++ httpd-2.0.52-peruser/server/mpm/experimental/peruser/config.m4 2005-07-12 01:57:22.000000000 -0600
35 +if test "$MPM_NAME" = "peruser" ; then
36 + APACHE_FAST_OUTPUT(server/mpm/experimental/$MPM_NAME/Makefile)
38 diff -Nur httpd-2.0.52/server/mpm/experimental/peruser/mpm.h httpd-2.0.52-peruser/server/mpm/experimental/peruser/mpm.h
39 --- httpd-2.0.52/server/mpm/experimental/peruser/mpm.h 1969-12-31 17:00:00.000000000 -0700
40 +++ httpd-2.0.52-peruser/server/mpm/experimental/peruser/mpm.h 2005-07-12 01:57:22.000000000 -0600
42 +/* ====================================================================
43 + * The Apache Software License, Version 1.1
45 + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
48 + * Redistribution and use in source and binary forms, with or without
49 + * modification, are permitted provided that the following conditions
52 + * 1. Redistributions of source code must retain the above copyright
53 + * notice, this list of conditions and the following disclaimer.
55 + * 2. Redistributions in binary form must reproduce the above copyright
56 + * notice, this list of conditions and the following disclaimer in
57 + * the documentation and/or other materials provided with the
60 + * 3. The end-user documentation included with the redistribution,
61 + * if any, must include the following acknowledgment:
62 + * "This product includes software developed by the
63 + * Apache Software Foundation (http://www.apache.org/)."
64 + * Alternately, this acknowledgment may appear in the software itself,
65 + * if and wherever such third-party acknowledgments normally appear.
67 + * 4. The names "Apache" and "Apache Software Foundation" must
68 + * not be used to endorse or promote products derived from this
69 + * software without prior written permission. For written
70 + * permission, please contact apache@apache.org.
72 + * 5. Products derived from this software may not be called "Apache",
73 + * nor may "Apache" appear in their name, without prior written
74 + * permission of the Apache Software Foundation.
76 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
77 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
78 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
79 + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
80 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
81 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
82 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
83 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
84 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
85 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
86 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 + * ====================================================================
90 + * This software consists of voluntary contributions made by many
91 + * individuals on behalf of the Apache Software Foundation. For more
92 + * information on the Apache Software Foundation, please see
93 + * <http://www.apache.org/>.
95 + * Portions of this software are based upon public domain software
96 + * originally written at the National Center for Supercomputing Applications,
97 + * University of Illinois, Urbana-Champaign.
101 +#include "mpm_default.h"
102 +#include "scoreboard.h"
105 +#ifndef APACHE_MPM_PERUSER_H
106 +#define APACHE_MPM_PERUSER_H
110 +#define MPM_NAME "Peruser"
112 +#define AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
113 +#define AP_MPM_WANT_WAIT_OR_TIMEOUT
114 +#define AP_MPM_WANT_PROCESS_CHILD_STATUS
115 +#define AP_MPM_WANT_SET_PIDFILE
116 +#define AP_MPM_WANT_SET_SCOREBOARD
117 +#define AP_MPM_WANT_SET_LOCKFILE
118 +#define AP_MPM_WANT_SET_MAX_REQUESTS
119 +#define AP_MPM_WANT_SET_COREDUMPDIR
120 +#define AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
121 +#define AP_MPM_WANT_SIGNAL_SERVER
122 +#define AP_MPM_WANT_SET_MAX_MEM_FREE
123 +#define AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
125 +#define AP_MPM_USES_POD 1
126 +#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
127 +#define MPM_NOTE_CHILD_KILLED(i) (MPM_CHILD_PID(i) = 0)
128 +#define MPM_ACCEPT_FUNC unixd_accept
130 +extern int ap_threads_per_child;
131 +extern int ap_max_daemons_limit;
132 +extern server_rec *ap_server_conf;
134 +/* Table of child status */
135 +#define SERVER_DEAD 0
136 +#define SERVER_DYING 1
137 +#define SERVER_ALIVE 2
139 +typedef struct ap_ctable {
141 + unsigned char status;
144 +#endif /* APACHE_MPM_PERUSER_H */
145 diff -Nur httpd-2.0.52/server/mpm/experimental/peruser/mpm_default.h httpd-2.0.52-peruser/server/mpm/experimental/peruser/mpm_default.h
146 --- httpd-2.0.52/server/mpm/experimental/peruser/mpm_default.h 1969-12-31 17:00:00.000000000 -0700
147 +++ httpd-2.0.52-peruser/server/mpm/experimental/peruser/mpm_default.h 2005-07-12 01:57:22.000000000 -0600
149 +/* ====================================================================
150 + * The Apache Software License, Version 1.1
152 + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
155 + * Redistribution and use in source and binary forms, with or without
156 + * modification, are permitted provided that the following conditions
159 + * 1. Redistributions of source code must retain the above copyright
160 + * notice, this list of conditions and the following disclaimer.
162 + * 2. Redistributions in binary form must reproduce the above copyright
163 + * notice, this list of conditions and the following disclaimer in
164 + * the documentation and/or other materials provided with the
167 + * 3. The end-user documentation included with the redistribution,
168 + * if any, must include the following acknowledgment:
169 + * "This product includes software developed by the
170 + * Apache Software Foundation (http://www.apache.org/)."
171 + * Alternately, this acknowledgment may appear in the software itself,
172 + * if and wherever such third-party acknowledgments normally appear.
174 + * 4. The names "Apache" and "Apache Software Foundation" must
175 + * not be used to endorse or promote products derived from this
176 + * software without prior written permission. For written
177 + * permission, please contact apache@apache.org.
179 + * 5. Products derived from this software may not be called "Apache",
180 + * nor may "Apache" appear in their name, without prior written
181 + * permission of the Apache Software Foundation.
183 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
184 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
186 + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
187 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
188 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
189 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
190 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
191 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
192 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
193 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
195 + * ====================================================================
197 + * This software consists of voluntary contributions made by many
198 + * individuals on behalf of the Apache Software Foundation. For more
199 + * information on the Apache Software Foundation, please see
200 + * <http://www.apache.org/>.
202 + * Portions of this software are based upon public domain software
203 + * originally written at the National Center for Supercomputing Applications,
204 + * University of Illinois, Urbana-Champaign.
207 +#ifndef APACHE_MPM_DEFAULT_H
208 +#define APACHE_MPM_DEFAULT_H
210 +/* Number of processors to spawn off for each ServerEnvironment by default */
212 +#ifndef DEFAULT_START_PROCESSORS
213 +#define DEFAULT_START_PROCESSORS 0
216 +/* Minimum number of running processors per ServerEnvironment */
218 +#ifndef DEFAULT_MIN_PROCESSORS
219 +#define DEFAULT_MIN_PROCESSORS 0
222 +/* Minimum --- fewer than this, and more will be created */
224 +#ifndef DEFAULT_MIN_FREE_PROCESSORS
225 +#define DEFAULT_MIN_FREE_PROCESSORS 2
228 +/* Maximum processors per ServerEnvironment */
230 +#ifndef DEFAULT_MAX_PROCESSORS
231 +#define DEFAULT_MAX_PROCESSORS 10
234 +/* File used for accept locking, when we use a file */
235 +#ifndef DEFAULT_LOCKFILE
236 +#define DEFAULT_LOCKFILE DEFAULT_REL_RUNTIMEDIR "/accept.lock"
239 +/* Where the main/parent process's pid is logged */
240 +#ifndef DEFAULT_PIDLOG
241 +#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
245 + * Interval, in microseconds, between scoreboard maintenance.
247 +#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
248 +#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
251 +/* Number of requests to try to handle in a single process. If <= 0,
252 + * the children don't die off.
254 +#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
255 +#define DEFAULT_MAX_REQUESTS_PER_CHILD 10000
258 +#endif /* AP_MPM_DEFAULT_H */
259 diff -Nur httpd-2.0.52/server/mpm/experimental/peruser/peruser.c httpd-2.0.52-peruser/server/mpm/experimental/peruser/peruser.c
260 --- httpd-2.0.52/server/mpm/experimental/peruser/peruser.c 1969-12-31 17:00:00.000000000 -0700
261 +++ httpd-2.0.52-peruser/server/mpm/experimental/peruser/peruser.c 2005-07-12 01:57:38.000000000 -0600
263 +/* ====================================================================
264 + * The Apache Software License, Version 1.1
266 + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
269 + * Redistribution and use in source and binary forms, with or without
270 + * modification, are permitted provided that the following conditions
273 + * 1. Redistributions of source code must retain the above copyright
274 + * notice, this list of conditions and the following disclaimer.
276 + * 2. Redistributions in binary form must reproduce the above copyright
277 + * notice, this list of conditions and the following disclaimer in
278 + * the documentation and/or other materials provided with the
281 + * 3. The end-user documentation included with the redistribution,
282 + * if any, must include the following acknowledgment:
283 + * "This product includes software developed by the
284 + * Apache Software Foundation (http://www.apache.org/)."
285 + * Alternately, this acknowledgment may appear in the software itself,
286 + * if and wherever such third-party acknowledgments normally appear.
288 + * 4. The names "Apache" and "Apache Software Foundation" must
289 + * not be used to endorse or promote products derived from this
290 + * software without prior written permission. For written
291 + * permission, please contact apache@apache.org.
293 + * 5. Products derived from this software may not be called "Apache",
294 + * nor may "Apache" appear in their name, without prior written
295 + * permission of the Apache Software Foundation.
297 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
298 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
299 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
300 + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
301 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
302 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
303 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
304 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
305 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
306 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
307 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309 + * ====================================================================
311 + * This software consists of voluntary contributions made by many
312 + * individuals on behalf of the Apache Software Foundation. For more
313 + * information on the Apache Software Foundation, please see
314 + * <http://www.apache.org/>.
316 + * Portions of this software are based upon public domain software
317 + * originally written at the National Center for Supercomputing Applications,
318 + * University of Illinois, Urbana-Champaign.
321 +/* #define MPM_PERUSER_DEBUG */
324 +#include "apr_hash.h"
325 +#include "apr_pools.h"
326 +#include "apr_file_io.h"
327 +#include "apr_portable.h"
328 +#include "apr_strings.h"
329 +#include "apr_thread_proc.h"
330 +#include "apr_signal.h"
332 +#define APR_WANT_STDIO
333 +#define APR_WANT_STRFUNC
334 +#define APR_WANT_IOVEC
335 +#include "apr_want.h"
337 +#if APR_HAVE_UNISTD_H
340 +#if APR_HAVE_SYS_TYPES_H
341 +#include <sys/types.h>
344 +#define CORE_PRIVATE
346 +#include "ap_config.h"
348 +#include "mpm_default.h"
349 +#include "http_main.h"
350 +#include "http_log.h"
351 +#include "http_config.h"
352 +#include "http_core.h" /* for get_remote_host */
353 +#include "http_connection.h"
354 +#include "http_protocol.h" /* for ap_hook_post_read_request */
355 +#include "scoreboard.h"
358 +#include "mpm_common.h"
359 +#include "ap_listen.h"
361 +#include "apr_poll.h"
362 +#include "util_ebcdic.h"
364 +#ifdef HAVE_BSTRING_H
365 +#include <bstring.h> /* for IRIX, FD_SET calls bzero() */
372 +#ifdef HAVE_SYS_PROCESSOR_H
373 +#include <sys/processor.h> /* for bindprocessor() */
376 +#if APR_HAS_SHARED_MEMORY
377 +#include "apr_shm.h"
379 +#error "Peruser MPM requres shared memory support."
383 +/* should be APR-ized */
386 +#include <sys/stat.h>
391 +#include <sys/times.h>
394 +#ifdef MPM_PERUSER_DEBUG
395 +# define _DBG(text,par...) \
396 + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL, \
397 + "(peruser: pid=%d uid=%d child=%d) %s(): " text, \
398 + getpid(), getuid(), my_child_num, __FUNCTION__, ##par, 0)
400 +# define _TRACE_CALL(text,par...) _DBG("calling " text, ##par)
401 +# define _TRACE_RET(text,par...) _DBG("returned from " text, ##par)
403 +# define _DBG(text,par...)
404 +# define _TRACE_RET(text,par...)
405 +# define _TRACE_CALL(text,par...)
406 +#endif /* MPM_PERUSER_DEBUG */
408 +/* char of death - for signalling children to die */
409 +#define AP_PERUSER_CHAR_OF_DEATH '!'
411 +#define PERUSER_SERVER_CONF(cf) \
412 + ((peruser_server_conf *) ap_get_module_config(cf, &mpm_peruser_module))
414 +#define SCOREBOARD_STATUS(i) ap_scoreboard_image->servers[i][0].status
417 + * Define some magic numbers that we use for the state of the incomming
418 + * request. These must be < 0 so they don't collide with a file descriptor.
420 +#define AP_PERUSER_THISCHILD -1
421 +#define AP_PERUSER_OTHERCHILD -2
424 +/* Limit on the total --- clients will be locked out if more servers than
425 + * this are needed. It is intended solely to keep the server from crashing
426 + * when things get out of hand.
428 + * We keep a hard maximum number of servers, for two reasons --- first off,
429 + * in case something goes seriously wrong, we want to stop the fork bomb
430 + * short of actually crashing the machine we're running on by filling some
431 + * kernel table. Secondly, it keeps the size of the scoreboard file small
432 + * enough that we can read the whole thing without worrying too much about
435 +#ifndef DEFAULT_SERVER_LIMIT
436 +#define DEFAULT_SERVER_LIMIT 256
439 +/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
440 + * some sort of compile-time limit to help catch typos.
442 +#ifndef MAX_SERVER_LIMIT
443 +#define MAX_SERVER_LIMIT 20000
446 +#ifndef HARD_THREAD_LIMIT
447 +#define HARD_THREAD_LIMIT 1
450 +#define CHILD_TYPE_UNKNOWN 0
451 +#define CHILD_TYPE_MULTIPLEXER 1
452 +#define CHILD_TYPE_PROCESSOR 2
453 +#define CHILD_TYPE_WORKER 3
455 +#define CHILD_STATUS_STANDBY 0 /* wait for a request before starting */
456 +#define CHILD_STATUS_STARTING 1 /* wait for socket creation */
457 +#define CHILD_STATUS_READY 2 /* wait for mux to restart */
458 +#define CHILD_STATUS_ACTIVE 3 /* ready to take requests */
459 +#define CHILD_STATUS_RESTART 4 /* child about to die and restart */
461 +/* config globals */
463 +int ap_threads_per_child=0; /* Worker threads per child */
464 +static apr_proc_mutex_t *accept_mutex;
465 +static int ap_min_processors=DEFAULT_MIN_PROCESSORS;
466 +static int ap_min_free_processors=DEFAULT_MIN_FREE_PROCESSORS;
467 +static int ap_max_processors=DEFAULT_MAX_PROCESSORS;
468 +static int ap_daemons_limit=0; /* MaxClients */
469 +static int expire_timeout=1800;
470 +static int server_limit = DEFAULT_SERVER_LIMIT;
471 +static int first_server_limit;
472 +static int changed_limit_at_restart;
473 +static int requests_this_child;
474 +static int mpm_state = AP_MPMQ_STARTING;
475 +static ap_pod_t *pod;
477 +/* === configuration stuff === */
483 + /* security settings */
484 + uid_t uid; /* user id */
485 + gid_t gid; /* group id */
486 + const char *chroot; /* directory to chroot() to, can be null */
488 + /* resource settings */
489 + int min_processors;
490 + int min_free_processors;
491 + int max_processors;
494 + int input; /* The socket descriptor */
495 + int output; /* The socket descriptor */
501 +} server_env_control;
505 + server_env_control *control;
506 + server_env_t *table;
512 + /* identification */
513 + int id; /* index in child_info_table */
514 + pid_t pid; /* process id */
515 + int status; /* status of child */
516 + int type; /* multiplexer or processor */
517 + server_env_t *senv;
522 + /* stack context saved state */
529 +} child_info_control;
533 + child_info_control *control;
534 + child_info_t *table;
540 + server_env_t *senv;
541 +} peruser_server_conf;
544 +typedef struct peruser_header
551 +/* Tables used to determine the user and group each child process should
552 + * run as. The hash table is used to correlate a server name with a child
555 +static apr_size_t child_info_size;
556 +static child_info *child_info_image;
557 +struct ap_ctable *ap_child_table;
559 +#define NUM_CHILDS (child_info_image != NULL ? child_info_image->control->num : 0)
560 +#define CHILD_INFO_TABLE (child_info_image != NULL ? child_info_image->table : NULL)
562 +static apr_size_t server_env_size;
563 +static server_env *server_env_image;
565 +#define NUM_SENV (server_env_image != NULL ? server_env_image->control->num : 0)
566 +#define SENV (server_env_image != NULL ? server_env_image->table : NULL)
568 +#if APR_HAS_SHARED_MEMORY
570 +static /* but must be exported to mpm_winnt */
572 + apr_shm_t *child_info_shm = NULL;
573 + apr_shm_t *server_env_shm = NULL;
577 + * The max child slot ever assigned, preserved across restarts. Necessary
578 + * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
579 + * use this value to optimize routines that have to scan the entire scoreboard.
581 +int ap_max_daemons_limit = -1;
582 +server_rec *ap_server_conf;
584 +module AP_MODULE_DECLARE_DATA mpm_peruser_module;
586 +/* -- replace the pipe-of-death by an control socket -- */
587 +static apr_file_t *pipe_of_death_in = NULL;
588 +static apr_file_t *pipe_of_death_out = NULL;
591 +/* one_process --- debugging mode variable; can be set from the command line
592 + * with the -X flag. If set, this gets you the child_main loop running
593 + * in the process which originally started up (no detach, no make_child),
594 + * which is a pretty nice debugging environment. (You'll get a SIGHUP
595 + * early in standalone_main; just continue through. This is the server
596 + * trying to kill off any child processes which it might have lying
597 + * around --- Apache doesn't keep track of their pids, it just sends
598 + * SIGHUP to the process group, ignoring it in the root process.
599 + * Continue through and you'll be fine.).
602 +static int one_process = 0;
604 +static apr_pool_t *pconf; /* Pool for config stuff */
605 +static apr_pool_t *pchild; /* Pool for httpd child stuff */
607 +static pid_t ap_my_pid; /* it seems silly to call getpid all the time */
608 +static pid_t parent_pid;
609 +static int my_child_num;
610 +ap_generation_t volatile ap_my_generation=0;
614 +char tpf_server_name[INETD_SERVNAME_LENGTH+1];
617 +static int die_now = 0;
621 + * change directory for gprof to plop the gmon.out file
622 + * configure in httpd.conf:
623 + * GprofDir $RuntimeDir/ -> $ServerRoot/$RuntimeDir/gmon.out
624 + * GprofDir $RuntimeDir/% -> $ServerRoot/$RuntimeDir/gprof.$pid/gmon.out
626 +static void chdir_for_gprof(void)
628 + core_server_config *sconf =
629 + ap_get_module_config(ap_server_conf->module_config, &core_module);
630 + char *dir = sconf->gprof_dir;
631 + const char *use_dir;
636 + int len = strlen(sconf->gprof_dir) - 1;
637 + if(*(dir + len) == '%') {
639 + apr_snprintf(buf, sizeof(buf), "%sgprof.%d", dir, (int)getpid());
641 + use_dir = ap_server_root_relative(pconf, buf[0] ? buf : dir);
642 + res = apr_dir_make(use_dir, 0755, pconf);
643 + if(res != APR_SUCCESS && !APR_STATUS_IS_EEXIST(res)) {
644 + ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
645 + "gprof: error creating directory %s", dir);
649 + use_dir = ap_server_root_relative(pconf, DEFAULT_REL_RUNTIMEDIR);
655 +#define chdir_for_gprof()
658 +char* child_type_string(int type)
662 + case CHILD_TYPE_MULTIPLEXER: return "MULTIPLEXER";
663 + case CHILD_TYPE_PROCESSOR: return "PROCESSOR";
664 + case CHILD_TYPE_WORKER: return "WORKER";
670 +char* child_status_string(int status)
674 + case CHILD_STATUS_STANDBY: return "STANDBY";
675 + case CHILD_STATUS_STARTING: return "STARTING";
676 + case CHILD_STATUS_READY: return "READY";
677 + case CHILD_STATUS_ACTIVE: return "ACTIVE";
678 + case CHILD_STATUS_RESTART: return "RESTART";
684 +void dump_child_table()
687 + server_env_t *senv;
689 + _DBG("%-3s %-5s %-8s %-12s %-4s %-4s %-25s %5s %6s %7s",
690 + "ID", "PID", "STATUS", "TYPE", "UID", "GID", "CHROOT", "INPUT", "OUTPUT", "SOCK_FD");
692 + for(x = 0; x < NUM_CHILDS; x++)
694 + senv = CHILD_INFO_TABLE[x].senv;
695 + _DBG("%-3d %-5d %-8s %-12s %-4d %-4d %-25s %-5d %-6d %-7d",
696 + CHILD_INFO_TABLE[x].id,
697 + CHILD_INFO_TABLE[x].pid,
698 + child_status_string(CHILD_INFO_TABLE[x].status),
699 + child_type_string(CHILD_INFO_TABLE[x].type),
700 + senv == NULL ? -1 : senv->uid,
701 + senv == NULL ? -1 : senv->gid,
702 + senv == NULL ? NULL : senv->chroot,
703 + senv == NULL ? -1 : CHILD_INFO_TABLE[x].senv->input,
704 + senv == NULL ? -1 : CHILD_INFO_TABLE[x].senv->output,
705 + CHILD_INFO_TABLE[x].sock_fd);
709 +/* XXX - I don't know if TPF will ever use this module or not, so leave
710 + * the ap_check_signals calls in but disable them - manoj */
711 +#define ap_check_signals()
713 +/* a clean exit from a child with proper cleanup */
714 +static inline int clean_child_exit(int code) __attribute__ ((noreturn));
715 +static inline int clean_child_exit(int code)
719 + mpm_state = AP_MPMQ_STOPPING;
721 + if (CHILD_INFO_TABLE[my_child_num].type != CHILD_TYPE_MULTIPLEXER &&
722 + CHILD_INFO_TABLE[my_child_num].senv)
724 + retval = close(CHILD_INFO_TABLE[my_child_num].senv->input);
725 + _DBG("close(CHILD_INFO_TABLE[%d].senv->input) = %d",
726 + my_child_num, retval);
728 + retval = close(CHILD_INFO_TABLE[my_child_num].senv->output);
729 + _DBG("close(CHILD_INFO_TABLE[%d].senv->output) = %d",
730 + my_child_num, retval);
734 + apr_pool_destroy(pchild);
736 + ap_mpm_pod_close(pod);
741 +static void accept_mutex_on(void)
743 +/* for some reason this fails if we listen on the pipe_of_death.
744 + fortunately I don't think we currently need it */
747 + apr_status_t rv = apr_proc_mutex_lock(accept_mutex);
748 + if (rv != APR_SUCCESS) {
749 + const char *msg = "couldn't grab the accept mutex";
751 + if (ap_my_generation !=
752 + ap_scoreboard_image->global->running_generation) {
753 + ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, msg);
754 + clean_child_exit(0);
757 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, msg);
758 + exit(APEXIT_CHILDFATAL);
764 +static void accept_mutex_off(void)
767 + apr_status_t rv = apr_proc_mutex_unlock(accept_mutex);
768 + if (rv != APR_SUCCESS) {
769 + const char *msg = "couldn't release the accept mutex";
771 + if (ap_my_generation !=
772 + ap_scoreboard_image->global->running_generation) {
773 + ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, msg);
774 + /* don't exit here... we have a connection to
775 + * process, after which point we'll see that the
776 + * generation changed and we'll exit cleanly
780 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, msg);
781 + exit(APEXIT_CHILDFATAL);
787 +/* On some architectures it's safe to do unserialized accept()s in the single
788 + * Listen case. But it's never safe to do it in the case where there's
789 + * multiple Listen statements. Define SINGLE_LISTEN_UNSERIALIZED_ACCEPT
790 + * when it's safe in the single Listen case.
792 +#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
793 +#define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)
795 +#define SAFE_ACCEPT(stmt) do {stmt;} while(0)
798 +AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
800 + switch(query_code){
801 + case AP_MPMQ_MAX_DAEMON_USED:
802 + *result = ap_daemons_limit;
803 + return APR_SUCCESS;
804 + case AP_MPMQ_IS_THREADED:
805 + *result = AP_MPMQ_NOT_SUPPORTED;
806 + return APR_SUCCESS;
807 + case AP_MPMQ_IS_FORKED:
808 + *result = AP_MPMQ_DYNAMIC;
809 + return APR_SUCCESS;
810 + case AP_MPMQ_HARD_LIMIT_DAEMONS:
811 + *result = server_limit;
812 + return APR_SUCCESS;
813 + case AP_MPMQ_HARD_LIMIT_THREADS:
814 + *result = HARD_THREAD_LIMIT;
815 + return APR_SUCCESS;
816 + case AP_MPMQ_MAX_THREADS:
818 + return APR_SUCCESS;
819 + case AP_MPMQ_MIN_SPARE_DAEMONS:
820 + *result = ap_min_free_processors;
821 + return APR_SUCCESS;
822 + case AP_MPMQ_MIN_SPARE_THREADS:
824 + return APR_SUCCESS;
825 + case AP_MPMQ_MAX_SPARE_THREADS:
827 + return APR_SUCCESS;
828 + case AP_MPMQ_MAX_REQUESTS_DAEMON:
829 + *result = ap_max_requests_per_child;
830 + return APR_SUCCESS;
831 + case AP_MPMQ_MAX_DAEMONS:
832 + *result = server_limit;
833 + return APR_SUCCESS;
834 + case AP_MPMQ_MPM_STATE:
835 + *result = mpm_state;
836 + return APR_SUCCESS;
838 + return APR_ENOTIMPL;
841 +#if defined(NEED_WAITPID)
843 + Systems without a real waitpid sometimes lose a child's exit while waiting
844 + for another. Search through the scoreboard for missing children.
846 +int reap_children(int *exitcode, apr_exit_why_e *status)
850 + for (n = 0; n < ap_max_daemons_limit; ++n) {
851 + if (ap_scoreboard_image->servers[n][0].status != SERVER_DEAD &&
852 + kill((pid = ap_scoreboard_image->parent[n].pid), 0) == -1) {
853 + ap_update_child_status_from_indexes(n, 0, SERVER_DEAD, NULL);
854 + /* just mark it as having a successful exit status */
855 + *status = APR_PROC_EXIT;
864 +/* handle all varieties of core dumping signals */
865 +static void sig_coredump(int sig)
867 + chdir(ap_coredump_dir);
868 + apr_signal(sig, SIG_DFL);
869 + if (ap_my_pid == parent_pid) {
870 + ap_log_error(APLOG_MARK, APLOG_NOTICE,
872 + "seg fault or similar nasty error detected "
873 + "in the parent process");
875 + kill(getpid(), sig);
876 + /* At this point we've got sig blocked, because we're still inside
877 + * the signal handler. When we leave the signal handler it will
878 + * be unblocked, and we'll take the signal... and coredump or whatever
879 + * is appropriate for this particular Unix. In addition the parent
880 + * will see the real signal we received -- whereas if we called
881 + * abort() here, the parent would only see SIGABRT.
885 +/*****************************************************************
886 + * Connection structures and accounting...
889 +static void just_die(int sig)
891 +_DBG("function called");
892 + clean_child_exit(0);
895 +/* volatile just in case */
896 +static int volatile shutdown_pending;
897 +static int volatile restart_pending;
898 +static int volatile is_graceful;
899 +/* XXX static int volatile child_fatal; */
901 +static void sig_term(int sig)
903 + if (shutdown_pending == 1) {
904 + /* Um, is this _probably_ not an error, if the user has
905 + * tried to do a shutdown twice quickly, so we won't
906 + * worry about reporting it.
910 + shutdown_pending = 1;
913 +/* restart() is the signal handler for SIGHUP and AP_SIG_GRACEFUL
914 + * in the parent process, unless running in ONE_PROCESS mode
916 +static void restart(int sig)
918 + if (restart_pending == 1) {
919 + /* Probably not an error - don't bother reporting it */
922 + restart_pending = 1;
923 + is_graceful = (sig == AP_SIG_GRACEFUL);
926 +/* Sets die_now if we received a character on the pipe_of_death */
927 +static apr_status_t check_pipe_of_death
935 + char pipe_read_char;
938 + _DBG("WATCH: die_now=%d", die_now);
940 + if (die_now) return APR_SUCCESS;
942 + /* apr_thread_mutex_lock(pipe_of_death_mutex); */
943 + ret = apr_recv(lr->sd, &pipe_read_char, &n);
944 + if (APR_STATUS_IS_EAGAIN(ret))
946 + /* It lost the lottery. It must continue to suffer
947 + * through a life of servitude. */
951 + if (pipe_read_char != AP_PERUSER_CHAR_OF_DEATH)
953 + _DBG("got wrong char %c", pipe_read_char);
954 + return APR_SUCCESS;
956 + /* It won the lottery (or something else is very
957 + * wrong). Embrace death with open arms. */
959 + _DBG("WATCH: die_now=%d", die_now);
961 + /* apr_thread_mutex_unlock(pipe_of_death_mutex); */
962 + return APR_SUCCESS;
965 +static void set_signals(void)
967 +#ifndef NO_USE_SIGACTION
968 + struct sigaction sa;
970 + sigemptyset(&sa.sa_mask);
973 + if (!one_process) {
974 + sa.sa_handler = sig_coredump;
975 +#if defined(SA_ONESHOT)
976 + sa.sa_flags = SA_ONESHOT;
977 +#elif defined(SA_RESETHAND)
978 + sa.sa_flags = SA_RESETHAND;
980 + if (sigaction(SIGSEGV, &sa, NULL) < 0)
981 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
983 + if (sigaction(SIGBUS, &sa, NULL) < 0)
984 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
987 + if (sigaction(SIGABORT, &sa, NULL) < 0)
988 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABORT)");
991 + if (sigaction(SIGABRT, &sa, NULL) < 0)
992 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
995 + if (sigaction(SIGILL, &sa, NULL) < 0)
996 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
1000 + sa.sa_handler = sig_term;
1001 + if (sigaction(SIGTERM, &sa, NULL) < 0)
1002 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
1004 + if (sigaction(SIGINT, &sa, NULL) < 0)
1005 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
1008 + sa.sa_handler = SIG_DFL;
1009 + if (sigaction(SIGXCPU, &sa, NULL) < 0)
1010 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");
1013 + sa.sa_handler = SIG_IGN;
1014 + if (sigaction(SIGXFSZ, &sa, NULL) < 0)
1015 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");
1018 + sa.sa_handler = SIG_IGN;
1019 + if (sigaction(SIGPIPE, &sa, NULL) < 0)
1020 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
1023 + /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
1024 + * processing one */
1025 + sigaddset(&sa.sa_mask, SIGHUP);
1026 + sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
1027 + sa.sa_handler = restart;
1028 + if (sigaction(SIGHUP, &sa, NULL) < 0)
1029 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
1030 + if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
1031 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(" AP_SIG_GRACEFUL_STRING ")");
1033 + if (!one_process) {
1034 + apr_signal(SIGSEGV, sig_coredump);
1036 + apr_signal(SIGBUS, sig_coredump);
1037 +#endif /* SIGBUS */
1039 + apr_signal(SIGABORT, sig_coredump);
1040 +#endif /* SIGABORT */
1042 + apr_signal(SIGABRT, sig_coredump);
1043 +#endif /* SIGABRT */
1045 + apr_signal(SIGILL, sig_coredump);
1046 +#endif /* SIGILL */
1048 + apr_signal(SIGXCPU, SIG_DFL);
1049 +#endif /* SIGXCPU */
1051 + apr_signal(SIGXFSZ, SIG_DFL);
1052 +#endif /* SIGXFSZ */
1055 + apr_signal(SIGTERM, sig_term);
1057 + apr_signal(SIGHUP, restart);
1058 +#endif /* SIGHUP */
1059 +#ifdef AP_SIG_GRACEFUL
1060 + apr_signal(AP_SIG_GRACEFUL, restart);
1061 +#endif /* AP_SIG_GRACEFUL */
1063 + apr_signal(SIGPIPE, SIG_IGN);
1064 +#endif /* SIGPIPE */
1069 +/*****************************************************************
1070 + * Child process main loop.
1071 + * The following vars are static to avoid getting clobbered by longjmp();
1072 + * they are really private to child_main.
1075 +static int requests_this_child;
1076 +static int num_listensocks = 0;
1077 +static ap_listen_rec *listensocks;
1079 +int ap_graceful_stop_signalled(void)
1081 + /* not ever called anymore... */
1085 +static void process_socket(apr_pool_t *p, apr_socket_t *sock, long conn_id,
1086 + apr_bucket_alloc_t *bucket_alloc)
1088 + conn_rec *current_conn;
1091 + ap_sb_handle_t *sbh;
1093 + if ((rv = apr_os_sock_get(&sock_fd, sock)) != APR_SUCCESS)
1095 + ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "apr_os_sock_get");
1098 + _DBG("child_num=%d sock=%ld sock_fd=%d\n", my_child_num, sock, sock_fd);
1099 + _DBG("type=%s %d", child_type_string(CHILD_INFO_TABLE[my_child_num].type), my_child_num);
1101 + if (sock_fd >= FD_SETSIZE)
1103 + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1104 + "new file descriptor %d is too large; you probably need "
1105 + "to rebuild Apache with a larger FD_SETSIZE "
1107 + sock_fd, FD_SETSIZE);
1108 + apr_socket_close(sock);
1109 + _DBG("child_num=%d: exiting with error", my_child_num);
1113 + if (CHILD_INFO_TABLE[my_child_num].sock_fd < 0)
1115 + ap_sock_disable_nagle(sock);
1118 + ap_create_sb_handle(&sbh, p, conn_id, 0);
1119 + current_conn = ap_run_create_connection(p, ap_server_conf, sock, conn_id,
1120 + sbh, bucket_alloc);
1124 + ap_process_connection(current_conn, sock);
1125 + ap_lingering_close(current_conn);
1129 +static int peruser_process_connection(conn_rec *conn)
1131 + ap_filter_t *filter;
1132 + apr_bucket_brigade *bb;
1133 + core_net_rec *net;
1135 + _DBG("function entered",0);
1137 + /* -- fetch our sockets from the pool -- */
1138 + apr_pool_userdata_get((void **)&bb, "PERUSER_SOCKETS", conn->pool);
1141 + /* -- find the 'core' filter and give the socket data to it -- */
1142 + for (filter = conn->output_filters; filter != NULL; filter = filter->next)
1144 + if (!strcmp(filter->frec->name, "core")) break;
1146 + if (filter != NULL)
1148 + net = filter->ctx;
1149 + net->in_ctx = apr_palloc(conn->pool, sizeof(*net->in_ctx));
1150 + net->in_ctx->b = bb;
1152 + net->in_ctx->tmpbb = apr_brigade_create(net->in_ctx->b->p,
1153 + net->in_ctx->b->bucket_alloc);
1157 + _DBG("leaving (DECLINED)", 0);
1161 +static int total_processors(int child_num)
1165 + for(i = 0, total = 0; i < NUM_CHILDS; ++i)
1167 + if(CHILD_INFO_TABLE[i].senv == CHILD_INFO_TABLE[child_num].senv)
1174 +static int idle_processors(int child_num)
1178 + for(i = 0, total = 0; i < NUM_CHILDS; ++i)
1180 + if(CHILD_INFO_TABLE[i].senv == CHILD_INFO_TABLE[child_num].senv &&
1181 + (SCOREBOARD_STATUS(i) == SERVER_STARTING ||
1182 + SCOREBOARD_STATUS(i) == SERVER_READY))
1191 +static int pass_request(request_rec *r, child_info_t *processor)
1194 + struct msghdr msg;
1195 + struct cmsghdr *cmsg;
1198 + struct iovec iov[4];
1199 + conn_rec *c = r->connection;
1200 + apr_bucket_brigade *bb = apr_brigade_create(r->pool, c->bucket_alloc);
1201 + apr_bucket_brigade *body_bb = NULL;
1202 + apr_size_t len = 0;
1203 + apr_size_t header_len = 0;
1204 + apr_size_t body_len = 0;
1206 + apr_bucket *bucket;
1207 + const apr_array_header_t *headers_in_array;
1208 + const apr_table_entry_t *headers_in;
1211 + apr_socket_t *thesock = ap_get_module_config(r->connection->conn_config, &core_module);
1213 + if ((!r->the_request) || (!strlen(r->the_request)))
1215 + _DBG("empty request. dropping it (%ld)", r->the_request);
1221 + _DBG("server %s in child %d has no child_info associated",
1222 + r->hostname, my_child_num);
1226 + _DBG("passing request to another child. Vhost: %s, child %d %d",
1227 + apr_table_get(r->headers_in, "Host"), my_child_num, processor->senv->output);
1228 + _DBG("r->the_request=\"%s\" len=%d", r->the_request, strlen(r->the_request));
1230 + ap_get_brigade(r->connection->input_filters, bb, AP_MODE_EXHAUSTIVE, APR_NONBLOCK_READ, len);
1232 + /* Scan the brigade looking for heap-buckets */
1234 + _DBG("Scanning the brigade",0);
1235 + bucket = APR_BRIGADE_FIRST(bb);
1236 + while (bucket != APR_BRIGADE_SENTINEL(bb) &&
1237 + APR_BUCKET_IS_HEAP(bucket)) {
1238 + _DBG("HEAP BUCKET is found, length=%d", bucket->length);
1239 + bucket = APR_BUCKET_NEXT(bucket);
1240 + if (!APR_BUCKET_IS_HEAP(bucket)) {
1241 + _DBG("NON-HEAP BUCKET is found, extracting the part of brigade before it",0);
1243 + bb = apr_brigade_split(body_bb, bucket);
1244 + /* Do we need to apr_destroy_brigade(bb) here?
1245 + * Yeah, I know we do apr_pool_destroy(r->pool) before return, but
1246 + * ap_get_brigade is in non-blocking mode (however len is zero).
1248 + if (apr_brigade_pflatten(body_bb, &body, &body_len, r->pool) != APR_SUCCESS) {
1249 + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1250 + "Unable to flatten brigade, declining request");
1251 + apr_pool_destroy(r->pool);
1254 + _DBG("Brigade is flattened as body (body_len=%d)", body_len);
1257 + _DBG("Scanning is finished",0);
1259 + apr_os_sock_get(&sock_fd, thesock);
1263 + headers_in_array = apr_table_elts(r->headers_in);
1264 + headers_in = (const apr_table_entry_t *) headers_in_array->elts;
1266 + h.headers = apr_pstrcat(h.p, r->the_request, CRLF, NULL);
1267 + for (counter = 0; counter < headers_in_array->nelts; counter++) {
1268 + if (headers_in[counter].key == NULL
1269 + || headers_in[counter].val == NULL) {
1272 + h.headers = apr_pstrcat(h.p, h.headers, headers_in[counter].key, ": ",
1273 + headers_in[counter].val, CRLF, NULL);
1276 + h.headers = apr_pstrcat(h.p, h.headers, CRLF, NULL);
1277 + ap_xlate_proto_to_ascii(h.headers, strlen(h.headers));
1279 + header_len = strlen(h.headers);
1281 + iov[0].iov_base = &header_len;
1282 + iov[0].iov_len = sizeof(header_len);
1283 + iov[1].iov_base = &body_len;
1284 + iov[1].iov_len = sizeof(body_len);
1285 + iov[2].iov_base = h.headers;
1286 + iov[2].iov_len = strlen(h.headers) + 1;
1287 + iov[3].iov_base = body;
1288 + iov[3].iov_len = body_len;
1290 + msg.msg_name = NULL;
1291 + msg.msg_namelen = 0;
1292 + msg.msg_iov = iov;
1293 + msg.msg_iovlen = 4;
1295 + cmsg = apr_palloc(r->pool, sizeof(*cmsg) + sizeof(sock_fd));
1296 + cmsg->cmsg_len = sizeof(*cmsg) + sizeof(sock_fd);
1297 + cmsg->cmsg_level = SOL_SOCKET;
1298 + cmsg->cmsg_type = SCM_RIGHTS;
1300 + memcpy(CMSG_DATA(cmsg), &sock_fd, sizeof(sock_fd));
1302 + msg.msg_control = cmsg;
1303 + msg.msg_controllen = cmsg->cmsg_len;
1306 + if (processor->status == CHILD_STATUS_STANDBY)
1308 + _DBG("Activating child #%d", processor->id);
1309 + processor->status = CHILD_STATUS_STARTING;
1312 + while (processor->status != CHILD_STATUS_ACTIVE)
1314 + _DBG("Waiting for child #%d...", processor->id);
1318 + _DBG("Writing message to %d, passing sock_fd: %d", processor->senv->output, sock_fd);
1319 + _DBG("header_len=%d headers=\"%s\"", header_len, h.headers);
1320 + _DBG("body_len=%d body=\"%s\"", body_len, body);
1322 + if ((rv = sendmsg(processor->senv->output, &msg, 0)) == -1)
1324 + apr_pool_destroy(r->pool);
1325 + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1326 + "Writing message failed %d %d", rv, errno);
1330 + _DBG("Writing message succeeded %d", rv);
1332 + /* -- close the socket on our side -- */
1333 + _DBG("closing socket %d on our side", sock_fd);
1334 + apr_socket_close(thesock);
1336 + apr_pool_destroy(r->pool);
1341 +static apr_status_t receive_from_multiplexer(
1342 + void **trans_sock, /* will be filled out w/ the received socket */
1343 + ap_listen_rec *lr, /* listener to receive from */
1344 + apr_pool_t *ptrans /* transaction wide pool */
1347 + struct msghdr msg;
1348 + struct cmsghdr *cmsg;
1349 + char buff[HUGE_STRING_LEN] = "";
1350 + char headers[HUGE_STRING_LEN] = "";
1352 + apr_size_t header_len, body_len;
1353 + struct iovec iov[3];
1355 + apr_os_sock_t ctrl_sock_fd;
1356 + apr_os_sock_t trans_sock_fd;
1358 + /* -- bucket's, brigades and their allocators */
1359 + apr_bucket_alloc_t *alloc = apr_bucket_alloc_create(ptrans);
1360 + apr_bucket_brigade *bb = apr_brigade_create(ptrans, alloc);
1361 + apr_bucket *bucket;
1363 + /* prepare the buffers for receiving data from remote side */
1364 + iov[0].iov_base = &header_len;
1365 + iov[0].iov_len = sizeof(header_len);
1366 + iov[1].iov_base = &body_len;
1367 + iov[1].iov_len = sizeof(body_len);
1368 + iov[2].iov_base = (char*)&buff;
1369 + iov[2].iov_len = HUGE_STRING_LEN;
1371 + cmsg = apr_palloc(ptrans, sizeof(*cmsg) + sizeof(trans_sock_fd));
1372 + cmsg->cmsg_len = sizeof(*cmsg) + sizeof(trans_sock_fd);
1374 + msg.msg_name = NULL;
1375 + msg.msg_namelen = 0;
1376 + msg.msg_iov = iov;
1377 + msg.msg_iovlen = 3;
1378 + msg.msg_control = cmsg;
1379 + msg.msg_controllen = cmsg->cmsg_len;
1381 + /* -- receive data from socket -- */
1382 + apr_os_sock_get(&ctrl_sock_fd, lr->sd);
1383 + _DBG("receiving from sock_fd=%d", ctrl_sock_fd);
1384 + ret = recvmsg(ctrl_sock_fd, &msg, 0);
1387 + _DBG("recvmsg failed with error \"%s\"", strerror(errno));
1389 + _DBG("recvmsg returned %d", ret);
1391 + /* -- extract socket from the cmsg -- */
1392 + memcpy(&trans_sock_fd, CMSG_DATA(cmsg), sizeof(trans_sock_fd));
1393 + apr_os_sock_put((apr_socket_t **)trans_sock, &trans_sock_fd, ptrans);
1394 + apr_os_sock_get(&fd_tmp, *trans_sock);
1396 + _DBG("trans_sock=%ld fdx=%d sock_fd=%d",
1397 + *trans_sock, trans_sock_fd, fd_tmp);
1399 + apr_cpystrn(headers, buff, header_len + 1);
1400 + _DBG("header_len=%d headers=\"%s\"", header_len, headers);
1402 + /* -- store received data into an brigade and add
1403 + it to the current transaction's pool -- */
1404 + bucket = apr_bucket_eos_create(alloc);
1405 + APR_BRIGADE_INSERT_HEAD(bb, bucket);
1406 + bucket = apr_bucket_socket_create(*trans_sock, alloc);
1407 + APR_BRIGADE_INSERT_HEAD(bb, bucket);
1410 + body = (char*)&buff[header_len + 1];
1411 + _DBG("body_len=%d body=\"%s\"", body_len, body);
1413 + bucket = apr_bucket_heap_create(body, body_len, NULL, alloc);
1414 + APR_BRIGADE_INSERT_HEAD(bb, bucket);
1416 + _DBG("There is no body",0);
1419 + bucket = apr_bucket_heap_create(headers, header_len, NULL, alloc);
1421 + APR_BRIGADE_INSERT_HEAD(bb, bucket);
1422 + apr_pool_userdata_set(bb, "PERUSER_SOCKETS", NULL, ptrans);
1424 + _DBG("returning 0", 0);
1429 +/* Set group privileges.
1431 + * Note that we use the username as set in the config files, rather than
1432 + * the lookup of to uid --- the same uid may have multiple passwd entries,
1433 + * with different sets of groups for each.
1436 +static int set_group_privs(uid_t uid, gid_t gid)
1440 + struct passwd *ent;
1444 + * Set the GID before initgroups(), since on some platforms
1445 + * setgid() is known to zap the group list.
1447 + if (setgid(gid) == -1)
1449 + ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1450 + "setgid: unable to set group id to Group %u",
1455 + /* if getpwuid() fails, just skip initgroups() */
1457 + if ((ent = getpwuid(uid)) != NULL)
1459 + name = ent->pw_name;
1461 + /* Reset `groups' attributes. */
1463 + if (initgroups(name, gid) == -1)
1465 + ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1466 + "initgroups: unable to set groups for User %s "
1467 + "and Group %u", name, (unsigned)gid);
1475 +static int peruser_setup_child(int childnum)
1477 + server_env_t *senv = CHILD_INFO_TABLE[childnum].senv;
1479 + if(senv->chroot) {
1480 + _DBG("chroot to %s", senv->chroot);
1481 + if(chroot(senv->chroot)) {
1482 + _DBG("chroot failure %s", senv->chroot);
1487 + if (senv->uid == -1 && senv->gid == -1) {
1488 + return unixd_setup_child();
1490 + if (set_group_privs(senv->uid, senv->gid)) {
1493 + /* Only try to switch if we're running as root */
1497 + os_init_job_environment(ap_server_conf, unixd_config.user_name,
1498 + one_process) != 0 ||
1500 + setuid(senv->uid) == -1)) {
1501 + ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1502 + "setuid: unable to change to uid: %ld",
1503 + (long) senv->uid);
1509 +static int check_signal(int signum)
1511 + _DBG("signum=%d", signum);
1521 +/* Send a single HTTP header field to the client. Note that this function
1522 + * is used in calls to table_do(), so their interfaces are co-dependent.
1523 + * In other words, don't change this one without checking table_do in alloc.c.
1524 + * It returns true unless there was a write error of some kind.
1526 +static int peruser_header_field(peruser_header *h,
1527 + const char *fieldname, const char *fieldval)
1529 + apr_pstrcat(h->p, h->headers, fieldname, ": ", fieldval, CRLF, NULL);
1533 +static inline ap_listen_rec* listen_add(apr_pool_t* pool, apr_socket_t *sock, void* accept_func)
1535 + ap_listen_rec *lr_walk, *lr_new;
1537 + _DBG("function entered", 0);
1538 + /* -- create an new listener for this child -- */
1539 + lr_new = apr_palloc(pool, sizeof(*lr_new));
1540 + lr_new->sd = sock;
1541 + lr_new->active = 1;
1542 + lr_new->accept_func = accept_func;
1543 + lr_new->next = NULL;
1545 + /* -- add the new listener_rec into the list -- */
1546 + /* FIXME: should we somehow lock this list ? */
1547 + lr_walk = ap_listeners;
1550 + while (lr_walk->next) lr_walk = lr_walk->next;
1551 + lr_walk->next = lr_new;
1555 + ap_listeners = lr_walk = lr_new;
1557 + num_listensocks++;
1561 +static inline void listen_clear()
1563 + ap_listen_rec *lr_walk;
1565 + _DBG("function entered", 0);
1567 + /* FIXME: should we somehow lock this list ? */
1568 + while (ap_listeners)
1570 + lr_walk = ap_listeners->next;
1571 + apr_socket_close(ap_listeners->sd);
1572 + ap_listeners = lr_walk;
1574 + num_listensocks=0;
1577 +apr_status_t cleanup_child_info(void *d)
1579 + if (child_info_image == NULL) {
1580 + return APR_SUCCESS;
1583 + free(child_info_image);
1584 + child_info_image = NULL;
1585 + apr_shm_destroy(child_info_shm);
1587 + return APR_SUCCESS;
1590 +apr_status_t cleanup_server_environments(void *d)
1592 + if (server_env_image == NULL) {
1593 + return APR_SUCCESS;
1596 + free(server_env_image);
1597 + server_env_image = NULL;
1598 + apr_shm_destroy(server_env_shm);
1600 + return APR_SUCCESS;
1603 +static const char* child_clone();
1605 +static void child_main(int child_num_arg)
1607 + apr_pool_t *ptrans;
1608 + apr_allocator_t *allocator;
1609 + conn_rec *current_conn;
1610 + apr_status_t status = APR_EINIT;
1612 + ap_listen_rec *lr;
1613 + int curr_pollfd, last_pollfd = 0;
1614 + apr_pollfd_t *pollset;
1616 + ap_sb_handle_t *sbh;
1618 + apr_bucket_alloc_t *bucket_alloc;
1623 + mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
1624 + * child initializes
1627 + my_child_num = child_num_arg;
1628 + ap_my_pid = getpid();
1629 + requests_this_child = 0;
1631 + _DBG("sock_fd_in=%d sock_fd_out=%d",
1632 + CHILD_INFO_TABLE[my_child_num].senv->input,
1633 + CHILD_INFO_TABLE[my_child_num].senv->output);
1635 + /* Get a sub context for global allocations in this child, so that
1636 + * we can have cleanups occur when the child exits.
1638 + apr_allocator_create(&allocator);
1639 + apr_allocator_max_free_set(allocator, ap_max_mem_free);
1640 + apr_pool_create_ex(&pchild, pconf, NULL, allocator);
1641 + apr_allocator_owner_set(allocator, pchild);
1643 + apr_pool_create(&ptrans, pchild);
1644 + apr_pool_tag(ptrans, "transaction");
1646 + /* needs to be done before we switch UIDs so we have permissions */
1647 + ap_reopen_scoreboard(pchild, NULL, 0);
1649 + rv = apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname, pchild);
1650 + if (rv != APR_SUCCESS) {
1651 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1652 + "Couldn't initialize cross-process lock in child");
1653 + clean_child_exit(APEXIT_CHILDFATAL);
1656 + switch(CHILD_INFO_TABLE[my_child_num].type)
1658 + case CHILD_TYPE_MULTIPLEXER:
1659 + _DBG("MULTIPLEXER %d", my_child_num);
1661 + /* update status on processors that are ready to accept requests */
1662 + _DBG("updating processor stati", 0);
1663 + for(i = 0; i < NUM_CHILDS; ++i)
1665 + if(CHILD_INFO_TABLE[i].status == CHILD_STATUS_READY)
1666 + CHILD_INFO_TABLE[i].status = CHILD_STATUS_ACTIVE;
1671 + case CHILD_TYPE_PROCESSOR:
1672 + case CHILD_TYPE_WORKER:
1673 + _DBG("%s %d", child_type_string(CHILD_INFO_TABLE[my_child_num].type), my_child_num);
1675 + /* -- create new listener to receive from multiplexer -- */
1676 + apr_os_sock_put((void*)&sock, &CHILD_INFO_TABLE[my_child_num].senv->input, pconf);
1678 + listen_add(pconf, sock, receive_from_multiplexer);
1683 + _DBG("unspecified child type for %d sleeping a while ...", my_child_num);
1688 + apr_os_file_get(&fd, pipe_of_death_in);
1689 + apr_os_sock_put((void*)&pod_sock, &fd, pconf);
1690 + listen_add(pconf, pod_sock, check_pipe_of_death);
1692 + (peruser_setup_child(my_child_num) && clean_child_exit(APEXIT_CHILDFATAL));
1693 + ap_run_child_init(pchild, ap_server_conf);
1695 + ap_create_sb_handle(&sbh, pchild, my_child_num, 0);
1696 + (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
1698 + /* Set up the pollfd array */
1699 + listensocks = apr_pcalloc(pchild,
1700 + sizeof(*listensocks) * (num_listensocks));
1701 + for (lr = ap_listeners, i = 0; i < num_listensocks; lr = lr->next, i++) {
1702 + listensocks[i].accept_func = lr->accept_func;
1703 + listensocks[i].sd = lr->sd;
1706 + pollset = apr_palloc(pchild, sizeof(*pollset) * num_listensocks);
1707 + pollset[0].p = pchild;
1708 + for (i = 0; i < num_listensocks; i++) {
1709 + pollset[i].desc.s = listensocks[i].sd;
1710 + pollset[i].desc_type = APR_POLL_SOCKET;
1711 + pollset[i].reqevents = APR_POLLIN;
1714 + mpm_state = AP_MPMQ_RUNNING;
1716 + bucket_alloc = apr_bucket_alloc_create(pchild);
1718 + while (!die_now) {
1720 + * (Re)initialize this child to a pre-connection state.
1723 + current_conn = NULL;
1725 + apr_pool_clear(ptrans);
1727 + if (CHILD_INFO_TABLE[my_child_num].type != CHILD_TYPE_MULTIPLEXER
1728 + && ap_max_requests_per_child > 0
1729 + && requests_this_child++ >= ap_max_requests_per_child) {
1730 + _DBG("max requests reached, dying now", 0);
1731 + clean_child_exit(0);
1734 + (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
1737 + * Wait for an acceptable connection to arrive.
1740 + /* Lock around "accept", if necessary */
1741 + SAFE_ACCEPT(accept_mutex_on());
1743 + if (num_listensocks == 1) {
1747 + /* multiple listening sockets - need to poll */
1752 + ret = apr_poll(pollset, num_listensocks, &n, -1);
1753 + if (ret != APR_SUCCESS) {
1754 + if (APR_STATUS_IS_EINTR(ret)) {
1757 + /* Single Unix documents select as returning errnos
1758 + * EBADF, EINTR, and EINVAL... and in none of those
1759 + * cases does it make sense to continue. In fact
1760 + * on Linux 2.0.x we seem to end up with EFAULT
1761 + * occasionally, and we'd loop forever due to it.
1763 + ap_log_error(APLOG_MARK, APLOG_ERR, ret, ap_server_conf,
1764 + "apr_poll: (listen)");
1765 + clean_child_exit(1);
1767 + /* find a listener */
1768 + curr_pollfd = last_pollfd;
1771 + if (curr_pollfd >= num_listensocks) {
1774 + /* XXX: Should we check for POLLERR? */
1775 + if (pollset[curr_pollfd].rtnevents & APR_POLLIN) {
1776 + last_pollfd = curr_pollfd;
1777 + offset = curr_pollfd;
1780 + } while (curr_pollfd != last_pollfd);
1786 + _DBG("input available ... resetting socket.",0);
1787 + sock = NULL; /* important! */
1789 + /* if we accept() something we don't want to die, so we have to
1792 + status = listensocks[offset].accept_func(&sock, &listensocks[offset], ptrans);
1793 + SAFE_ACCEPT(accept_mutex_off()); /* unlock after "accept" */
1795 + if (status == APR_EGENERAL) {
1796 + /* resource shortage or should-not-occur occured */
1797 + clean_child_exit(1);
1799 + else if (status != APR_SUCCESS || die_now) {
1803 + if (CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_PROCESSOR ||
1804 + CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_WORKER)
1806 + _DBG("CHECKING IF WE SHOULD CLONE A CHILD...");
1808 + _DBG("total_processors = %d, max_processors = %d",
1809 + total_processors(my_child_num),
1810 + CHILD_INFO_TABLE[my_child_num].senv->max_processors);
1812 + _DBG("idle_processors = %d, min_free_processors = %d",
1813 + idle_processors(my_child_num),
1814 + CHILD_INFO_TABLE[my_child_num].senv->min_free_processors);
1816 + if(total_processors(my_child_num) <
1817 + CHILD_INFO_TABLE[my_child_num].senv->max_processors &&
1818 + idle_processors(my_child_num) <
1819 + CHILD_INFO_TABLE[my_child_num].senv->min_free_processors)
1821 + _DBG("CLONING CHILD");
1826 + if (!setjmp(CHILD_INFO_TABLE[my_child_num].jmpbuffer))
1828 + _DBG("marked jmpbuffer",0);
1829 + _TRACE_CALL("process_socket()",0);
1830 + process_socket(ptrans, sock, my_child_num, bucket_alloc);
1831 + _TRACE_RET("process_socket()",0);
1835 + _DBG("landed from longjmp",0);
1836 + CHILD_INFO_TABLE[my_child_num].sock_fd = AP_PERUSER_THISCHILD;
1839 + /* Check the pod and the generation number after processing a
1840 + * connection so that we'll go away if a graceful restart occurred
1841 + * while we were processing the connection or we are the lucky
1842 + * idle server process that gets to die.
1844 + if (ap_mpm_pod_check(pod) == APR_SUCCESS) { /* selected as idle? */
1845 + _DBG("ap_mpm_pod_check(pod) = APR_SUCCESS; dying now", 0);
1848 + else if (ap_my_generation !=
1849 + ap_scoreboard_image->global->running_generation) { /* restart? */
1850 + /* yeah, this could be non-graceful restart, in which case the
1851 + * parent will kill us soon enough, but why bother checking?
1853 + _DBG("ap_my_generation != ap_scoreboard_image->global->running_generation; dying now", 0);
1857 + if(CHILD_INFO_TABLE[my_child_num].status == CHILD_STATUS_RESTART)
1859 + _DBG("restarting", 0);
1864 + _DBG("clean_child_exit(0)");
1865 + clean_child_exit(0);
1868 +static server_env_t* senv_add(int uid, int gid, const char* chroot)
1873 + _DBG("Searching for matching senv...");
1875 + for(i = 0; i < NUM_SENV; i++)
1877 + if(SENV[i].uid == uid && SENV[i].gid == gid &&
1878 + (SENV[i].chroot == NULL || !strcmp(SENV[i].chroot, chroot)))
1880 + _DBG("Found existing senv: %i", i);
1885 + if(NUM_SENV >= server_limit)
1887 + _DBG("server_limit reached!");
1891 + _DBG("Creating new senv");
1893 + SENV[NUM_SENV].uid = uid;
1894 + SENV[NUM_SENV].gid = gid;
1895 + SENV[NUM_SENV].chroot = chroot;
1897 + SENV[NUM_SENV].min_processors = ap_min_processors;
1898 + SENV[NUM_SENV].min_free_processors = ap_min_free_processors;
1899 + SENV[NUM_SENV].max_processors = ap_max_processors;
1901 + socketpair(PF_UNIX, SOCK_STREAM, 0, socks);
1902 + SENV[NUM_SENV].input = socks[0];
1903 + SENV[NUM_SENV].output = socks[1];
1905 + return &SENV[server_env_image->control->num++];
1908 +static const char* child_clone()
1911 + child_info_t *this;
1912 + child_info_t *new;
1914 + if(NUM_CHILDS >= server_limit)
1916 + _DBG("Trying to use more child ID's than NumServers. "
1917 + "Increase NumServers in your config file.");
1921 + for(i = 0; i < NUM_CHILDS; i++)
1923 + if(CHILD_INFO_TABLE[i].pid == 0 &&
1924 + CHILD_INFO_TABLE[i].type == CHILD_TYPE_UNKNOWN) break;
1927 + _DBG("cloning child #%d from #%d", i, my_child_num);
1929 + this = &CHILD_INFO_TABLE[my_child_num];
1930 + new = &CHILD_INFO_TABLE[i];
1932 + new->senv = this->senv;
1933 + new->type = CHILD_TYPE_WORKER;
1934 + new->sock_fd = this->sock_fd;
1935 + new->status = CHILD_STATUS_STARTING;
1937 + if(i == NUM_CHILDS) child_info_image->control->num++;
1941 +static const char* child_add(int type, int status,
1942 + uid_t uid, gid_t gid, const char* chroot)
1944 + _DBG("adding child #%d", NUM_CHILDS);
1946 + if(NUM_CHILDS >= server_limit)
1948 + return "Trying to use more child ID's than NumServers. "
1949 + "Increase NumServers in your config file.";
1952 + CHILD_INFO_TABLE[NUM_CHILDS].senv = senv_add(uid, gid, chroot);
1954 + if(CHILD_INFO_TABLE[NUM_CHILDS].senv == NULL)
1956 + return "Trying to use more server environments than NumServers. "
1957 + "Increase NumServers in your config file.";
1960 + if(type != CHILD_TYPE_WORKER)
1961 + CHILD_INFO_TABLE[NUM_CHILDS].senv->processor_id = NUM_CHILDS;
1963 + CHILD_INFO_TABLE[NUM_CHILDS].type = type;
1964 + CHILD_INFO_TABLE[NUM_CHILDS].sock_fd = AP_PERUSER_THISCHILD;
1965 + CHILD_INFO_TABLE[NUM_CHILDS].status = status;
1967 + _DBG("[%d] uid=%d gid=%d type=%d chroot=%s",
1968 + NUM_CHILDS, uid, gid, type,
1971 + if (uid == 0 || gid == 0)
1973 + _DBG("Assigning root user/group to a child.", 0);
1976 + server_env_image->control->num++;
1977 + child_info_image->control->num++;
1982 +static int make_child(server_rec *s, int slot)
1986 + child_info_t *multiplexer;
1988 + _DBG("function entered", 0);
1990 + switch (CHILD_INFO_TABLE[slot].type)
1992 + case CHILD_TYPE_MULTIPLEXER: break;
1993 + case CHILD_TYPE_PROCESSOR: break;
1994 + case CHILD_TYPE_WORKER: break;
1997 + _DBG("no valid client in slot %d", slot);
2002 + if (slot + 1 > ap_max_daemons_limit) {
2003 + ap_max_daemons_limit = slot + 1;
2006 + if (one_process) {
2007 + apr_signal(SIGHUP, just_die);
2008 + /* Don't catch AP_SIG_GRACEFUL in ONE_PROCESS mode :) */
2009 + apr_signal(SIGINT, just_die);
2011 + apr_signal(SIGQUIT, SIG_DFL);
2013 + apr_signal(SIGTERM, just_die);
2017 + (void) ap_update_child_status_from_indexes(slot, 0, SERVER_STARTING,
2018 + (request_rec *) NULL);
2020 + CHILD_INFO_TABLE[slot].status = CHILD_STATUS_ACTIVE;
2024 + /* BS2000 requires a "special" version of fork() before a setuid() call */
2025 + if ((pid = os_fork(unixd_config.user_name)) == -1) {
2027 + if ((pid = os_fork(s, slot)) == -1) {
2029 + if ((pid = fork()) == -1) {
2031 + ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process");
2033 + /* fork didn't succeed. Fix the scoreboard or else
2034 + * it will say SERVER_STARTING forever and ever
2036 + (void) ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD,
2037 + (request_rec *) NULL);
2039 + /* In case system resources are maxxed out, we don't want
2040 + Apache running away with the CPU trying to fork over and
2041 + over and over again. */
2048 +#ifdef HAVE_BINDPROCESSOR
2049 + /* by default AIX binds to a single processor
2050 + * this bit unbinds children which will then bind to another cpu
2052 + int status = bindprocessor(BINDPROCESS, (int)getpid(),
2053 + PROCESSOR_CLASS_ANY);
2054 + if (status != OK) {
2055 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
2056 + ap_server_conf, "processor unbind failed %d", status);
2059 + RAISE_SIGSTOP(MAKE_CHILD);
2061 + /* Disable the parent's signal handlers and set up proper handling in
2064 + apr_signal(SIGHUP, just_die);
2065 + apr_signal(SIGTERM, just_die);
2066 + /* The child process doesn't do anything for AP_SIG_GRACEFUL.
2067 + * Instead, the pod is used for signalling graceful restart.
2069 + /* apr_signal(AP_SIG_GRACEFUL, restart); */
2071 + clean_child_exit(0);
2074 + ap_scoreboard_image->parent[slot].pid = pid;
2075 + CHILD_INFO_TABLE[slot].pid = pid;
2077 + ap_child_table[slot].pid = pid;
2078 + ap_child_table[slot].status = SERVER_ALIVE;
2085 + * idle_spawn_rate is the number of children that will be spawned on the
2086 + * next maintenance cycle if there aren't enough idle servers. It is
2087 + * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
2088 + * without the need to spawn.
2090 +static int idle_spawn_rate = 1;
2091 +#ifndef MAX_SPAWN_RATE
2092 +#define MAX_SPAWN_RATE (32)
2094 +static int hold_off_on_exponential_spawning;
2096 +static void perform_idle_server_maintenance(apr_pool_t *p)
2101 + /* _DBG("function entered", 0); */
2103 + now = apr_time_now();
2105 + for (i = 0; i < NUM_CHILDS; ++i)
2107 + if(CHILD_INFO_TABLE[i].pid == 0)
2109 + if(CHILD_INFO_TABLE[i].status == CHILD_STATUS_STARTING)
2110 + make_child(ap_server_conf, i);
2112 + else if(expire_timeout > 0 &&
2113 + (CHILD_INFO_TABLE[i].type == CHILD_TYPE_PROCESSOR ||
2114 + CHILD_INFO_TABLE[i].type == CHILD_TYPE_WORKER) &&
2115 + ap_scoreboard_image->parent[i].pid > 1 &&
2116 + ap_scoreboard_image->servers[i][0].status != SERVER_DEAD &&
2117 + apr_time_sec(now - ap_scoreboard_image->servers[i][0].last_used) > expire_timeout)
2119 + CHILD_INFO_TABLE[i].pid = 0;
2120 + CHILD_INFO_TABLE[i].status = CHILD_STATUS_STANDBY;
2122 + if(CHILD_INFO_TABLE[i].type == CHILD_TYPE_WORKER)
2124 + /* completely free up this slot */
2126 + CHILD_INFO_TABLE[i].senv = (server_env_t*)NULL;
2127 + CHILD_INFO_TABLE[i].type = CHILD_TYPE_UNKNOWN;
2128 + CHILD_INFO_TABLE[i].sock_fd = -3; /* -1 and -2 are taken */
2131 + if(kill(ap_scoreboard_image->parent[i].pid, SIGTERM) == -1)
2133 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
2134 + ap_server_conf, "kill SIGTERM");
2137 + ap_update_child_status_from_indexes(i, 0, SERVER_DEAD, NULL);
2143 +/*****************************************************************
2144 + * Executive routines.
2147 +int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
2152 + apr_size_t one = 1;
2153 +/* apr_socket_t *sock = NULL; */
2155 + ap_log_pid(pconf, ap_pid_fname);
2157 + first_server_limit = server_limit;
2158 + if (changed_limit_at_restart) {
2159 + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2160 + "WARNING: Attempt to change ServerLimit "
2161 + "ignored during restart");
2162 + changed_limit_at_restart = 0;
2165 + ap_server_conf = s;
2167 + /* Initialize cross-process accept lock */
2168 + ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
2169 + ap_server_root_relative(_pconf, ap_lock_fname),
2172 + rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname,
2173 + ap_accept_lock_mech, _pconf);
2174 + if (rv != APR_SUCCESS) {
2175 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
2176 + "Couldn't create accept lock");
2177 + mpm_state = AP_MPMQ_STOPPING;
2182 +#if APR_USE_SYSVSEM_SERIALIZE
2183 + if (ap_accept_lock_mech == APR_LOCK_DEFAULT ||
2184 + ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
2186 + if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
2188 + rv = unixd_set_proc_mutex_perms(accept_mutex);
2189 + if (rv != APR_SUCCESS) {
2190 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
2191 + "Couldn't set permissions on cross-process lock; "
2192 + "check User and Group directives");
2193 + mpm_state = AP_MPMQ_STOPPING;
2199 + if (!is_graceful) {
2200 + if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
2201 + mpm_state = AP_MPMQ_STOPPING;
2204 + /* fix the generation number in the global score; we just got a new,
2205 + * cleared scoreboard
2207 + ap_scoreboard_image->global->running_generation = ap_my_generation;
2210 + /* Initialize the child table */
2213 + for (i = 0; i < server_limit; i++)
2215 + ap_child_table[i].pid = 0;
2219 + /* We need to put the new listeners at the end of the ap_listeners
2220 + * list. If we don't, then the pool will be cleared before the
2221 + * open_logs phase is called for the second time, and ap_listeners
2222 + * will have only invalid data. If that happens, then the sockets
2223 + * that we opened using make_sock() will be lost, and the server
2228 + apr_os_file_get(&fd, pipe_of_death_in);
2229 + apr_os_sock_put(&sock, &fd, pconf);
2231 + listen_add(pconf, sock, check_pipe_of_death);
2235 + if (one_process) {
2239 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2240 + "%s configured -- resuming normal operations",
2241 + ap_get_server_version());
2242 + ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
2243 + "Server built: %s", ap_get_server_built());
2244 +#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
2245 + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
2246 + "AcceptMutex: %s (default: %s)",
2247 + apr_proc_mutex_name(accept_mutex),
2248 + apr_proc_mutex_defname());
2250 + restart_pending = shutdown_pending = 0;
2252 + mpm_state = AP_MPMQ_RUNNING;
2254 + _DBG("sizeof(child_info_t) = %d", sizeof(child_info_t));
2256 + while (!restart_pending && !shutdown_pending) {
2258 + apr_exit_why_e exitwhy;
2259 + int status, processed_status;
2260 + /* this is a memory leak, but I'll fix it later. */
2263 + ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
2265 + /* XXX: if it takes longer than 1 second for all our children
2266 + * to start up and get into IDLE state then we may spawn an
2269 + if (pid.pid != -1) {
2270 + processed_status = ap_process_child_status(&pid, exitwhy, status);
2271 + if (processed_status == APEXIT_CHILDFATAL) {
2272 + mpm_state = AP_MPMQ_STOPPING;
2276 + /* non-fatal death... note that it's gone in the scoreboard. */
2277 + child_slot = find_child_by_pid(&pid);
2278 + _DBG("child #%d has died ...", child_slot);
2280 + for (i = 0; i < ap_max_daemons_limit; ++i)
2282 + if (ap_child_table[i].pid == pid.pid)
2289 + if (child_slot >= 0) {
2290 + ap_child_table[child_slot].pid = 0;
2291 + _TRACE_CALL("ap_update_child_status_from_indexes", 0);
2292 + (void) ap_update_child_status_from_indexes(child_slot, 0, SERVER_DEAD,
2293 + (request_rec *) NULL);
2294 + _TRACE_RET("ap_update_child_status_from_indexes", 0);
2296 + if (processed_status == APEXIT_CHILDSICK) {
2297 + /* child detected a resource shortage (E[NM]FILE, ENOBUFS, etc)
2298 + * cut the fork rate to the minimum
2300 + _DBG("processed_status = APEXIT_CHILDSICK", 0);
2301 + idle_spawn_rate = 1;
2303 + else if (CHILD_INFO_TABLE[child_slot].status == CHILD_STATUS_STANDBY) {
2304 + _DBG("leaving child in standby state", 0);
2306 + else if (child_slot < ap_daemons_limit &&
2307 + CHILD_INFO_TABLE[child_slot].type !=
2308 + CHILD_TYPE_UNKNOWN) {
2309 + /* we're still doing a 1-for-1 replacement of dead
2310 + * children with new children
2312 + _DBG("replacing by new child ...", 0);
2313 + make_child(ap_server_conf, child_slot);
2315 +#if APR_HAS_OTHER_CHILD
2317 + else if (apr_proc_other_child_read(&pid, status) == 0) {
2318 + _DBG("APR_HAS_OTHER_CHILD, apparently", 0);
2322 + else if (is_graceful) {
2323 + /* Great, we've probably just lost a slot in the
2324 + * scoreboard. Somehow we don't know about this
2327 + _DBG("long lost child came home, whatever that means", 0);
2329 + ap_log_error(APLOG_MARK, APLOG_WARNING,
2330 + 0, ap_server_conf,
2331 + "long lost child came home! (pid %ld)", (long)pid.pid);
2333 + /* Don't perform idle maintenance when a child dies,
2334 + * only do it when there's a timeout. Remember only a
2335 + * finite number of children can die, and it's pretty
2336 + * pathological for a lot to die suddenly.
2341 + perform_idle_server_maintenance(pconf);
2343 + shutdown_pending = os_check_server(tpf_server_name);
2344 + ap_check_signals();
2349 + mpm_state = AP_MPMQ_STOPPING;
2351 + if (shutdown_pending) {
2352 + /* Time to gracefully shut down:
2353 + * Kill child processes, tell them to call child_exit, etc...
2355 + if (unixd_killpg(getpgrp(), SIGTERM) < 0) {
2356 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGTERM");
2358 + ap_reclaim_child_processes(1); /* Start with SIGTERM */
2360 + /* cleanup pid file on normal shutdown */
2362 + const char *pidfile = NULL;
2363 + pidfile = ap_server_root_relative (pconf, ap_pid_fname);
2364 + if ( pidfile != NULL && unlink(pidfile) == 0)
2365 + ap_log_error(APLOG_MARK, APLOG_INFO,
2366 + 0, ap_server_conf,
2367 + "removed PID file %s (pid=%ld)",
2368 + pidfile, (long)getpid());
2371 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2372 + "caught SIGTERM, shutting down");
2376 + /* we've been told to restart */
2377 + apr_signal(SIGHUP, SIG_IGN);
2378 + if (one_process) {
2379 + /* not worth thinking about */
2383 + /* advance to the next generation */
2384 + /* XXX: we really need to make sure this new generation number isn't in
2385 + * use by any of the children.
2387 + ++ap_my_generation;
2388 + ap_scoreboard_image->global->running_generation = ap_my_generation;
2390 + if (is_graceful) {
2391 + char char_of_death = AP_PERUSER_CHAR_OF_DEATH;
2393 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2394 + "Graceful restart requested, doing restart");
2397 + /* kill off the idle ones */
2398 + ap_mpm_pod_killpg(pod, ap_max_daemons_limit);
2400 + /* This is mostly for debugging... so that we know what is still
2401 + * gracefully dealing with existing request. This will break
2402 + * in a very nasty way if we ever have the scoreboard totally
2403 + * file-based (no shared memory)
2405 + for (i = 0; i < ap_daemons_limit; ++i) {
2406 + if (ap_scoreboard_image->servers[i][0].status != SERVER_DEAD) {
2407 + ap_scoreboard_image->servers[i][0].status = SERVER_GRACEFUL;
2412 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
2413 + ap_server_conf, AP_SIG_GRACEFUL_STRING " received. "
2414 + "Doing graceful restart");
2416 + /* This is mostly for debugging... so that we know what is still
2417 + * gracefully dealing with existing request.
2420 + for (i = 0; i < NUM_CHILDS; ++i)
2422 + ((ap_child_table[i].pid) && (ap_child_table[i].status = SERVER_DYING));
2425 + /* give the children the signal to die */
2426 + for (i = 0; i < NUM_CHILDS;)
2428 + if ((rv = apr_file_write(pipe_of_death_out, &char_of_death, &one)) != APR_SUCCESS)
2430 + if (APR_STATUS_IS_EINTR(rv)) continue;
2431 + ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
2432 + "write pipe_of_death");
2437 + for (i = 0; i < NUM_SENV; i++)
2439 + close(SENV[i].input);
2440 + close(SENV[i].output);
2444 + /* Kill 'em off */
2445 + if (unixd_killpg(getpgrp(), SIGHUP) < 0) {
2446 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGHUP");
2448 + ap_reclaim_child_processes(0); /* Not when just starting up */
2449 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2450 + "SIGHUP received. Attempting to restart");
2456 +/* == allocate an private server config structure == */
2457 +static void *peruser_create_config(apr_pool_t *p, server_rec *s)
2459 + peruser_server_conf *c = (peruser_server_conf *)
2460 + apr_pcalloc(p, sizeof(peruser_server_conf));
2464 +/* This really should be a post_config hook, but the error log is already
2465 + * redirected by that point, so we need to do this in the open_logs phase.
2467 +static int peruser_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
2472 + ap_server_conf = s;
2474 + if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
2475 + ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0,
2476 + NULL, "no listening sockets available, shutting down");
2480 + ap_log_pid(pconf, ap_pid_fname);
2482 + if ((rv = ap_mpm_pod_open(pconf, &pod))) {
2483 + ap_log_error(APLOG_MARK, APLOG_CRIT|APLOG_STARTUP, rv, NULL,
2484 + "Could not open pipe-of-death.");
2488 + if ((rv = apr_file_pipe_create(&pipe_of_death_in, &pipe_of_death_out,
2489 + pconf)) != APR_SUCCESS) {
2490 + ap_log_error(APLOG_MARK, APLOG_ERR, rv,
2491 + (const server_rec*) ap_server_conf,
2492 + "apr_file_pipe_create (pipe_of_death)");
2495 + if ((rv = apr_file_pipe_timeout_set(pipe_of_death_in, 0)) != APR_SUCCESS) {
2496 + ap_log_error(APLOG_MARK, APLOG_ERR, rv,
2497 + (const server_rec*) ap_server_conf,
2498 + "apr_file_pipe_timeout_set (pipe_of_death)");
2505 +static int peruser_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
2507 + static int restart_num = 0;
2508 + int no_detach, debug, foreground, i;
2509 + int tmp_server_limit = DEFAULT_SERVER_LIMIT;
2510 + ap_directive_t *pdir;
2512 + apr_pool_t *global_pool;
2515 + mpm_state = AP_MPMQ_STARTING;
2517 + debug = ap_exists_config_define("DEBUG");
2520 + foreground = one_process = 1;
2525 + no_detach = ap_exists_config_define("NO_DETACH");
2526 + one_process = ap_exists_config_define("ONE_PROCESS");
2527 + foreground = ap_exists_config_define("FOREGROUND");
2530 + /* sigh, want this only the second time around */
2531 + if (restart_num++ == 1) {
2534 + if (!one_process && !foreground) {
2535 + rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
2536 + : APR_PROC_DETACH_DAEMONIZE);
2537 + if (rv != APR_SUCCESS) {
2538 + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2539 + "apr_proc_detach failed");
2540 + return HTTP_INTERNAL_SERVER_ERROR;
2544 + parent_pid = ap_my_pid = getpid();
2547 + unixd_pre_config(ptemp);
2548 + ap_listen_pre_config();
2549 + ap_min_processors = DEFAULT_MIN_PROCESSORS;
2550 + ap_min_free_processors = DEFAULT_MIN_FREE_PROCESSORS;
2551 + ap_max_processors = DEFAULT_MAX_PROCESSORS;
2552 + ap_daemons_limit = server_limit;
2553 + ap_pid_fname = DEFAULT_PIDLOG;
2554 + ap_lock_fname = DEFAULT_LOCKFILE;
2555 + ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
2556 + ap_extended_status = 1;
2557 +#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
2558 + ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
2561 + apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
2563 + /* we need to know ServerLimit and ThreadLimit before we start processing
2564 + * the tree because we need to already have allocated child_info_table
2566 + for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next)
2568 + if (!strcasecmp(pdir->directive, "ServerLimit"))
2570 + if (atoi(pdir->args) > tmp_server_limit)
2572 + tmp_server_limit = atoi(pdir->args);
2573 + if (tmp_server_limit > MAX_SERVER_LIMIT)
2575 + tmp_server_limit = MAX_SERVER_LIMIT;
2581 + /* We don't want to have to recreate the scoreboard after
2582 + * restarts, so we'll create a global pool and never clean it.
2584 + rv = apr_pool_create(&global_pool, NULL);
2585 + if (rv != APR_SUCCESS) {
2586 + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2587 + "Fatal error: unable to create global pool");
2592 + _DBG("Initializing child_info_table", 0);
2593 + child_info_size = tmp_server_limit * sizeof(child_info_t) + sizeof(apr_size_t);
2595 + rv = apr_shm_create(&child_info_shm, child_info_size, NULL, global_pool);
2597 +/* if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) { */
2598 + if (rv != APR_SUCCESS) {
2599 + _DBG("shared memory creation failed", 0);
2601 + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2602 + "Unable to create shared memory segment "
2603 + "(anonymous shared memory failure)");
2605 + else if (rv == APR_ENOTIMPL) {
2606 + _DBG("anonymous shared memory not available", 0);
2607 + /* TODO: make up a filename and do name-based shmem */
2610 + if (rv || !(shmem = apr_shm_baseaddr_get(child_info_shm))) {
2611 + _DBG("apr_shm_baseaddr_get() failed", 0);
2612 + return HTTP_INTERNAL_SERVER_ERROR;
2615 + memset(shmem, 0, sizeof(child_info_size));
2616 + child_info_image = (child_info*)calloc(1, sizeof(child_info_size));
2617 + child_info_image->control = (child_info_control*)shmem;
2618 + shmem += sizeof(child_info_control*);
2619 + child_info_image->table = (child_info_t*)shmem;
2621 + child_info_image->control->num = 0;
2623 + for (i = 0; i < tmp_server_limit; i++)
2625 + CHILD_INFO_TABLE[i].pid = 0;
2626 + CHILD_INFO_TABLE[i].senv = (server_env_t*)NULL;
2627 + CHILD_INFO_TABLE[i].type = CHILD_TYPE_UNKNOWN;
2628 + CHILD_INFO_TABLE[i].status = CHILD_STATUS_STANDBY;
2629 + CHILD_INFO_TABLE[i].sock_fd = -3; /* -1 and -2 are taken */
2630 + CHILD_INFO_TABLE[i].id = i;
2633 + _DBG("Initializing server_environments_table", 0);
2634 + server_env_size = tmp_server_limit * sizeof(server_env_t) + sizeof(apr_size_t);
2636 + rv = apr_shm_create(&server_env_shm, server_env_size, NULL, global_pool);
2638 + if (rv != APR_SUCCESS) {
2639 + _DBG("shared memory creation failed", 0);
2641 + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2642 + "Unable to create shared memory segment "
2643 + "(anonymous shared memory failure)");
2645 + else if (rv == APR_ENOTIMPL) {
2646 + _DBG("anonymous shared memory not available", 0);
2647 + /* TODO: make up a filename and do name-based shmem */
2650 + if (rv || !(shmem = apr_shm_baseaddr_get(server_env_shm))) {
2651 + _DBG("apr_shm_baseaddr_get() failed", 0);
2652 + return HTTP_INTERNAL_SERVER_ERROR;
2655 + memset(shmem, 0, sizeof(server_env_size));
2656 + server_env_image = (server_env*)calloc(1, sizeof(server_env_size));
2657 + server_env_image->control = (server_env_control*)shmem;
2658 + shmem += sizeof(server_env_control*);
2659 + server_env_image->table = (server_env_t*)shmem;
2661 + server_env_image->control->num = 0;
2663 + for (i = 0; i < tmp_server_limit; i++)
2665 + SENV[i].processor_id = -1;
2668 + SENV[i].chroot = NULL;
2669 + SENV[i].input = -1;
2670 + SENV[i].output = -1;
2676 +static int peruser_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *server_list)
2678 + ap_child_table = (ap_ctable *)apr_pcalloc(p, server_limit * sizeof(ap_ctable));
2683 +static int peruser_post_read(request_rec *r)
2685 + peruser_server_conf *sconf = PERUSER_SERVER_CONF(r->server->module_config);
2686 + child_info_t *processor;
2688 + if(CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_MULTIPLEXER)
2689 + processor = &CHILD_INFO_TABLE[sconf->senv->processor_id];
2691 + processor = &CHILD_INFO_TABLE[r->connection->id];
2694 + if (!strlen(r->the_request))
2696 + _DBG("corrupt request. aborting",0);
2700 + if (processor->sock_fd != AP_PERUSER_THISCHILD)
2702 + apr_socket_t *sock = NULL;
2704 + apr_os_sock_put(&sock, &processor->sock_fd, r->connection->pool);
2705 + ap_sock_disable_nagle(sock);
2706 + ap_set_module_config(r->connection->conn_config, &core_module, sock);
2707 + _DBG("not the right socket?", 0);
2711 + switch (CHILD_INFO_TABLE[my_child_num].type)
2713 + case CHILD_TYPE_MULTIPLEXER:
2715 + _DBG("MULTIPLEXER => Determining if request should be passed. "
2716 + "Child Num: %d, dest-child: %d, hostname from server: %s r->hostname=%s r->the_request=\"%s\"",
2717 + my_child_num, processor->id, r->server->server_hostname, r->hostname, r->the_request);
2719 + if (processor->id != my_child_num)
2721 + if (processor->status == CHILD_STATUS_STANDBY)
2723 + _DBG("Activating child #%d", processor->id);
2724 + processor->status = CHILD_STATUS_STARTING;
2727 + _DBG("Passing request.",0);
2728 + if (pass_request(r, processor) == -1)
2730 + ap_log_error(APLOG_MARK, APLOG_ERR, 0,
2731 + ap_server_conf, "Could not pass request to proper " "child, request will not be honoured.");
2734 + _DBG("doing longjmp",0);
2735 + longjmp(CHILD_INFO_TABLE[my_child_num].jmpbuffer, 1);
2736 + _DBG("request declined at our site",0);
2738 + _DBG("OUH! we should never reach this point",0);
2740 + _DBG("WTF: the server is assigned to the multiplexer! ... dropping request",0);
2743 + case CHILD_TYPE_PROCESSOR:
2744 + case CHILD_TYPE_WORKER:
2746 + _DBG("%s %d", child_type_string(CHILD_INFO_TABLE[my_child_num].type), my_child_num);
2747 + _DBG("request for %s / (server %s) seems to be for us", r->hostname, r->server->server_hostname);
2753 + _DBG("unspecified child type %d in %d, dropping request",
2754 + CHILD_INFO_TABLE[my_child_num].type, my_child_num);
2759 + _DBG("THIS POINT SHOULD NOT BE REACHED!",0);
2763 +static void peruser_hooks(apr_pool_t *p)
2765 + /* The peruser open_logs phase must run before the core's, or stderr
2766 + * will be redirected to a file, and the messages won't print to the
2769 + static const char *const aszSucc[] = {"core.c", NULL};
2772 + (void) set42sig();
2775 + ap_hook_open_logs(peruser_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
2776 + ap_hook_pre_config(peruser_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
2777 + ap_hook_post_config(peruser_post_config, NULL, NULL, APR_HOOK_MIDDLE);
2779 + /* Both of these must be run absolutely first. If this request isn't for
2780 + * this server then we need to forward it to the proper child. No sense
2781 + * tying up this server running more post_read request hooks if it is
2782 + * just going to be forwarded along. The process_connection hook allows
2783 + * peruser to receive the passed request correctly, by automatically
2784 + * filling in the core_input_filter's ctx pointer.
2786 + ap_hook_post_read_request(peruser_post_read, NULL, NULL,
2787 + APR_HOOK_REALLY_FIRST);
2788 + ap_hook_process_connection(peruser_process_connection, NULL, NULL,
2789 + APR_HOOK_REALLY_FIRST);
2792 +/* we define an Processor w/ specific uid/gid */
2793 +static const char *cf_Processor(cmd_parms *cmd, void *dummy,
2794 + const char *user_name, const char *group_name, const char *chroot)
2796 + uid_t uid = ap_uname2id(user_name);
2797 + gid_t gid = ap_gname2id(group_name);
2799 + _DBG("user=%s:%d group=%s:%d chroot=%s",
2800 + user_name, uid, group_name, gid, chroot);
2802 + return child_add(CHILD_TYPE_PROCESSOR, CHILD_STATUS_STANDBY,
2803 + uid, gid, chroot);
2806 +/* we define an Multiplexer child w/ specific uid/gid */
2807 +static const char *cf_Multiplexer(cmd_parms *cmd, void *dummy,
2808 + const char *user_name, const char *group_name, const char *chroot)
2810 + uid_t uid = ap_uname2id(user_name);
2811 + gid_t gid = ap_gname2id(group_name);
2813 + _DBG("user=%s:%d group=%s:%d chroot=%s [multiplexer id %d]",
2814 + user_name, uid, group_name, gid, chroot, NUM_CHILDS);
2816 + return child_add(CHILD_TYPE_MULTIPLEXER, CHILD_STATUS_STARTING,
2817 + uid, gid, chroot);
2820 +static const char* cf_ServerEnvironment(cmd_parms *cmd, void *dummy,
2821 + const char *user_name, const char *group_name, const char *chroot)
2823 + int uid = ap_uname2id(user_name);
2824 + int gid = ap_gname2id(group_name);
2825 + peruser_server_conf *sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2827 + _DBG("function entered", 0);
2829 + sconf->senv = senv_add(uid, gid, chroot);
2831 + _DBG("user=%s:%d group=%s:%d chroot=%s numchilds=%d",
2832 + user_name, uid, group_name, gid, chroot, NUM_CHILDS);
2837 +static const char *set_min_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
2839 + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2840 + if (err != NULL) {
2844 + ap_min_free_processors = atoi(arg);
2845 + if (ap_min_free_processors <= 0) {
2846 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2847 + "WARNING: detected MinSpareServers set to non-positive.");
2848 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2849 + "Resetting to 1 to avoid almost certain Apache failure.");
2850 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2851 + "Please read the documentation.");
2852 + ap_min_free_processors = 1;
2858 +static const char *set_max_clients (cmd_parms *cmd, void *dummy, const char *arg)
2860 + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2861 + if (err != NULL) {
2865 + ap_daemons_limit = atoi(arg);
2866 + if (ap_daemons_limit > server_limit) {
2867 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2868 + "WARNING: MaxClients of %d exceeds ServerLimit value "
2869 + "of %d servers,", ap_daemons_limit, server_limit);
2870 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2871 + " lowering MaxClients to %d. To increase, please "
2872 + "see the ServerLimit", server_limit);
2873 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2875 + ap_daemons_limit = server_limit;
2877 + else if (ap_daemons_limit < 1) {
2878 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2879 + "WARNING: Require MaxClients > 0, setting to 1");
2880 + ap_daemons_limit = 1;
2885 +static const char *set_min_processors (cmd_parms *cmd, void *dummy, const char *arg)
2887 + peruser_server_conf *sconf;
2889 + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2891 + if (err != NULL) {
2895 + min_procs = atoi(arg);
2897 + if (min_procs < 1) {
2898 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2899 + "WARNING: Require MaxProcessors > 0, setting to 1");
2903 + if (ap_check_cmd_context(cmd, NOT_IN_VIRTUALHOST) != NULL) {
2904 + sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2905 + sconf->senv->min_processors = min_procs;
2908 + ap_min_processors = min_procs;
2914 +static const char *set_min_free_processors (cmd_parms *cmd, void *dummy, const char *arg)
2916 + peruser_server_conf *sconf;
2917 + int min_free_procs;
2918 + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2920 + if (err != NULL) {
2924 + min_free_procs = atoi(arg);
2926 + if (min_free_procs < 1) {
2927 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2928 + "WARNING: Require MinSpareProcessors > 0, setting to 1");
2929 + min_free_procs = 1;
2932 + if (ap_check_cmd_context(cmd, NOT_IN_VIRTUALHOST) != NULL) {
2933 + sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2934 + sconf->senv->min_free_processors = min_free_procs;
2937 + ap_min_free_processors = min_free_procs;
2943 +static const char *set_max_processors (cmd_parms *cmd, void *dummy, const char *arg)
2945 + peruser_server_conf *sconf;
2947 + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2949 + if (err != NULL) {
2953 + max_procs = atoi(arg);
2955 + if (max_procs < 1) {
2956 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2957 + "WARNING: Require MaxProcessors > 0, setting to 1");
2961 + if (ap_check_cmd_context(cmd, NOT_IN_VIRTUALHOST) != NULL) {
2962 + sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2963 + sconf->senv->max_processors = max_procs;
2966 + ap_max_processors = max_procs;
2972 +static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
2974 + int tmp_server_limit;
2976 + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2977 + if (err != NULL) {
2981 + tmp_server_limit = atoi(arg);
2982 + /* you cannot change ServerLimit across a restart; ignore
2983 + * any such attempts
2985 + if (first_server_limit &&
2986 + tmp_server_limit != server_limit) {
2987 + /* how do we log a message? the error log is a bit bucket at this
2988 + * point; we'll just have to set a flag so that ap_mpm_run()
2989 + * logs a warning later
2991 + changed_limit_at_restart = 1;
2994 + server_limit = tmp_server_limit;
2996 + if (server_limit > MAX_SERVER_LIMIT) {
2997 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2998 + "WARNING: ServerLimit of %d exceeds compile time limit "
2999 + "of %d servers,", server_limit, MAX_SERVER_LIMIT);
3000 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
3001 + " lowering ServerLimit to %d.", MAX_SERVER_LIMIT);
3002 + server_limit = MAX_SERVER_LIMIT;
3004 + else if (server_limit < 1) {
3005 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
3006 + "WARNING: Require ServerLimit > 0, setting to 1");
3012 +static const char *set_expire_timeout (cmd_parms *cmd, void *dummy, const char *arg) {
3013 + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3014 + if (err != NULL) {
3018 + expire_timeout = atoi(arg);
3023 +static const command_rec peruser_cmds[] = {
3024 +UNIX_DAEMON_COMMANDS,
3026 +AP_INIT_TAKE1("MinSpareProcessors", set_min_free_processors, NULL, RSRC_CONF,
3027 + "Minimum number of idle children, to handle request spikes"),
3028 +AP_INIT_TAKE1("MinSpareServers", set_min_free_servers, NULL, RSRC_CONF,
3029 + "Minimum number of idle children, to handle request spikes"),
3030 +AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
3031 + "Maximum number of children alive at the same time"),
3032 +AP_INIT_TAKE1("MinProcessors", set_min_processors, NULL, RSRC_CONF,
3033 + "Minimum number of processors per vhost"),
3034 +AP_INIT_TAKE1("MaxProcessors", set_max_processors, NULL, RSRC_CONF,
3035 + "Maximum number of processors per vhost"),
3036 +AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
3037 + "Maximum value of MaxClients for this run of Apache"),
3038 +AP_INIT_TAKE1("ExpireTimeout", set_expire_timeout, NULL, RSRC_CONF,
3039 + "Maximum idle time before a child is killed, 0 to disable"),
3040 +AP_INIT_TAKE23("Multiplexer", cf_Multiplexer, NULL, RSRC_CONF,
3041 + "Specify an Multiplexer Child configuration."),
3042 +AP_INIT_TAKE23("Processor", cf_Processor, NULL, RSRC_CONF,
3043 + "Specify a User and Group for a specific child process."),
3044 +AP_INIT_TAKE23("ServerEnvironment", cf_ServerEnvironment, NULL, RSRC_CONF,
3045 + "Specify the server environment for this virtual host."),
3049 +module AP_MODULE_DECLARE_DATA mpm_peruser_module = {
3050 + MPM20_MODULE_STUFF,
3051 + ap_mpm_rewrite_args, /* hook to run before apache parses args */
3052 + NULL, /* create per-directory config structure */
3053 + NULL, /* merge per-directory config structures */
3054 + peruser_create_config, /* create per-server config structure */
3055 + NULL, /* merge per-server config structures */
3056 + peruser_cmds, /* command apr_table_t */
3057 + peruser_hooks, /* register hooks */