1 diff -urN httpd-2.2.0.org/server/mpm/config.m4 httpd-2.2.0/server/mpm/config.m4
2 --- httpd-2.2.0.org/server/mpm/config.m4 2005-12-02 22:41:02.471004000 +0100
3 +++ httpd-2.2.0/server/mpm/config.m4 2005-12-02 22:42:26.900280500 +0100
6 ap_mpm_is_experimental ()
8 - if test "$apache_cv_mpm" = "event" -o "$apache_cv_mpm" = "metuxmpm"; then
9 + if test "$apache_cv_mpm" = "event" -o "$apache_cv_mpm" = "metuxmpm" -o "$apache_cv_mpm" = "peruser"; then
13 diff -urN httpd-2.2.0.org/server/mpm/experimental/peruser/config.m4 httpd-2.2.0/server/mpm/experimental/peruser/config.m4
14 --- httpd-2.2.0.org/server/mpm/experimental/peruser/config.m4 1970-01-01 01:00:00.000000000 +0100
15 +++ httpd-2.2.0/server/mpm/experimental/peruser/config.m4 2005-12-02 22:41:54.314244000 +0100
17 +if test "$MPM_NAME" = "peruser" ; then
18 + APACHE_FAST_OUTPUT(server/mpm/experimental/$MPM_NAME/Makefile)
20 diff -urN httpd-2.2.0.org/server/mpm/experimental/peruser/Makefile.in httpd-2.2.0/server/mpm/experimental/peruser/Makefile.in
21 --- httpd-2.2.0.org/server/mpm/experimental/peruser/Makefile.in 1970-01-01 01:00:00.000000000 +0100
22 +++ httpd-2.2.0/server/mpm/experimental/peruser/Makefile.in 2005-12-02 22:41:54.314244000 +0100
25 +LTLIBRARY_NAME = libperuser.la
26 +LTLIBRARY_SOURCES = peruser.c
28 +include $(top_srcdir)/build/ltlib.mk
29 diff -urN httpd-2.2.0.org/server/mpm/experimental/peruser/mpm_default.h httpd-2.2.0/server/mpm/experimental/peruser/mpm_default.h
30 --- httpd-2.2.0.org/server/mpm/experimental/peruser/mpm_default.h 1970-01-01 01:00:00.000000000 +0100
31 +++ httpd-2.2.0/server/mpm/experimental/peruser/mpm_default.h 2005-12-02 22:41:54.314244000 +0100
33 +/* ====================================================================
34 + * The Apache Software License, Version 1.1
36 + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
39 + * Redistribution and use in source and binary forms, with or without
40 + * modification, are permitted provided that the following conditions
43 + * 1. Redistributions of source code must retain the above copyright
44 + * notice, this list of conditions and the following disclaimer.
46 + * 2. Redistributions in binary form must reproduce the above copyright
47 + * notice, this list of conditions and the following disclaimer in
48 + * the documentation and/or other materials provided with the
51 + * 3. The end-user documentation included with the redistribution,
52 + * if any, must include the following acknowledgment:
53 + * "This product includes software developed by the
54 + * Apache Software Foundation (http://www.apache.org/)."
55 + * Alternately, this acknowledgment may appear in the software itself,
56 + * if and wherever such third-party acknowledgments normally appear.
58 + * 4. The names "Apache" and "Apache Software Foundation" must
59 + * not be used to endorse or promote products derived from this
60 + * software without prior written permission. For written
61 + * permission, please contact apache@apache.org.
63 + * 5. Products derived from this software may not be called "Apache",
64 + * nor may "Apache" appear in their name, without prior written
65 + * permission of the Apache Software Foundation.
67 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
68 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
69 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
70 + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
71 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
72 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
73 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
74 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
75 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
76 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
77 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
79 + * ====================================================================
81 + * This software consists of voluntary contributions made by many
82 + * individuals on behalf of the Apache Software Foundation. For more
83 + * information on the Apache Software Foundation, please see
84 + * <http://www.apache.org/>.
86 + * Portions of this software are based upon public domain software
87 + * originally written at the National Center for Supercomputing Applications,
88 + * University of Illinois, Urbana-Champaign.
91 +#ifndef APACHE_MPM_DEFAULT_H
92 +#define APACHE_MPM_DEFAULT_H
94 +/* Number of processors to spawn off for each ServerEnvironment by default */
96 +#ifndef DEFAULT_START_PROCESSORS
97 +#define DEFAULT_START_PROCESSORS 0
100 +/* Minimum number of running processors per ServerEnvironment */
102 +#ifndef DEFAULT_MIN_PROCESSORS
103 +#define DEFAULT_MIN_PROCESSORS 0
106 +/* Minimum --- fewer than this, and more will be created */
108 +#ifndef DEFAULT_MIN_FREE_PROCESSORS
109 +#define DEFAULT_MIN_FREE_PROCESSORS 2
112 +/* Maximum processors per ServerEnvironment */
114 +#ifndef DEFAULT_MAX_PROCESSORS
115 +#define DEFAULT_MAX_PROCESSORS 10
118 +/* File used for accept locking, when we use a file */
119 +#ifndef DEFAULT_LOCKFILE
120 +#define DEFAULT_LOCKFILE DEFAULT_REL_RUNTIMEDIR "/accept.lock"
123 +/* Where the main/parent process's pid is logged */
124 +#ifndef DEFAULT_PIDLOG
125 +#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
129 + * Interval, in microseconds, between scoreboard maintenance.
131 +#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
132 +#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
135 +/* Number of requests to try to handle in a single process. If <= 0,
136 + * the children don't die off.
138 +#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
139 +#define DEFAULT_MAX_REQUESTS_PER_CHILD 10000
142 +#endif /* AP_MPM_DEFAULT_H */
143 diff -urN httpd-2.2.0.org/server/mpm/experimental/peruser/mpm.h httpd-2.2.0/server/mpm/experimental/peruser/mpm.h
144 --- httpd-2.2.0.org/server/mpm/experimental/peruser/mpm.h 1970-01-01 01:00:00.000000000 +0100
145 +++ httpd-2.2.0/server/mpm/experimental/peruser/mpm.h 2005-12-02 22:41:54.314244000 +0100
147 +/* ====================================================================
148 + * The Apache Software License, Version 1.1
150 + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
153 + * Redistribution and use in source and binary forms, with or without
154 + * modification, are permitted provided that the following conditions
157 + * 1. Redistributions of source code must retain the above copyright
158 + * notice, this list of conditions and the following disclaimer.
160 + * 2. Redistributions in binary form must reproduce the above copyright
161 + * notice, this list of conditions and the following disclaimer in
162 + * the documentation and/or other materials provided with the
165 + * 3. The end-user documentation included with the redistribution,
166 + * if any, must include the following acknowledgment:
167 + * "This product includes software developed by the
168 + * Apache Software Foundation (http://www.apache.org/)."
169 + * Alternately, this acknowledgment may appear in the software itself,
170 + * if and wherever such third-party acknowledgments normally appear.
172 + * 4. The names "Apache" and "Apache Software Foundation" must
173 + * not be used to endorse or promote products derived from this
174 + * software without prior written permission. For written
175 + * permission, please contact apache@apache.org.
177 + * 5. Products derived from this software may not be called "Apache",
178 + * nor may "Apache" appear in their name, without prior written
179 + * permission of the Apache Software Foundation.
181 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
182 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
184 + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
185 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
186 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
187 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
188 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
189 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
190 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
191 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
193 + * ====================================================================
195 + * This software consists of voluntary contributions made by many
196 + * individuals on behalf of the Apache Software Foundation. For more
197 + * information on the Apache Software Foundation, please see
198 + * <http://www.apache.org/>.
200 + * Portions of this software are based upon public domain software
201 + * originally written at the National Center for Supercomputing Applications,
202 + * University of Illinois, Urbana-Champaign.
206 +#include "mpm_default.h"
207 +#include "scoreboard.h"
210 +#ifndef APACHE_MPM_PERUSER_H
211 +#define APACHE_MPM_PERUSER_H
215 +#define MPM_NAME "Peruser"
217 +#define AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
218 +#define AP_MPM_WANT_WAIT_OR_TIMEOUT
219 +#define AP_MPM_WANT_PROCESS_CHILD_STATUS
220 +#define AP_MPM_WANT_SET_PIDFILE
221 +#define AP_MPM_WANT_SET_SCOREBOARD
222 +#define AP_MPM_WANT_SET_LOCKFILE
223 +#define AP_MPM_WANT_SET_MAX_REQUESTS
224 +#define AP_MPM_WANT_SET_COREDUMPDIR
225 +#define AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
226 +#define AP_MPM_WANT_SIGNAL_SERVER
227 +#define AP_MPM_WANT_SET_MAX_MEM_FREE
228 +#define AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
230 +#define AP_MPM_USES_POD 1
231 +#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
232 +#define MPM_NOTE_CHILD_KILLED(i) (MPM_CHILD_PID(i) = 0)
233 +#define MPM_ACCEPT_FUNC unixd_accept
235 +extern int ap_threads_per_child;
236 +extern int ap_max_daemons_limit;
237 +extern server_rec *ap_server_conf;
239 +/* Table of child status */
240 +#define SERVER_DEAD 0
241 +#define SERVER_DYING 1
242 +#define SERVER_ALIVE 2
244 +typedef struct ap_ctable {
246 + unsigned char status;
249 +#endif /* APACHE_MPM_PERUSER_H */
250 diff -urN httpd-2.2.0.org/server/mpm/experimental/peruser/peruser.c httpd-2.2.0/server/mpm/experimental/peruser/peruser.c
251 --- httpd-2.2.0.org/server/mpm/experimental/peruser/peruser.c 1970-01-01 01:00:00.000000000 +0100
252 +++ httpd-2.2.0/server/mpm/experimental/peruser/peruser.c 2005-12-02 22:46:31.963596000 +0100
254 +/* ====================================================================
255 + * The Apache Software License, Version 1.1
257 + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
260 + * Redistribution and use in source and binary forms, with or without
261 + * modification, are permitted provided that the following conditions
264 + * 1. Redistributions of source code must retain the above copyright
265 + * notice, this list of conditions and the following disclaimer.
267 + * 2. Redistributions in binary form must reproduce the above copyright
268 + * notice, this list of conditions and the following disclaimer in
269 + * the documentation and/or other materials provided with the
272 + * 3. The end-user documentation included with the redistribution,
273 + * if any, must include the following acknowledgment:
274 + * "This product includes software developed by the
275 + * Apache Software Foundation (http://www.apache.org/)."
276 + * Alternately, this acknowledgment may appear in the software itself,
277 + * if and wherever such third-party acknowledgments normally appear.
279 + * 4. The names "Apache" and "Apache Software Foundation" must
280 + * not be used to endorse or promote products derived from this
281 + * software without prior written permission. For written
282 + * permission, please contact apache@apache.org.
284 + * 5. Products derived from this software may not be called "Apache",
285 + * nor may "Apache" appear in their name, without prior written
286 + * permission of the Apache Software Foundation.
288 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
289 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
290 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
291 + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
292 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
293 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
294 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
295 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
296 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
297 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
298 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
300 + * ====================================================================
302 + * This software consists of voluntary contributions made by many
303 + * individuals on behalf of the Apache Software Foundation. For more
304 + * information on the Apache Software Foundation, please see
305 + * <http://www.apache.org/>.
307 + * Portions of this software are based upon public domain software
308 + * originally written at the National Center for Supercomputing Applications,
309 + * University of Illinois, Urbana-Champaign.
312 +/* #define MPM_PERUSER_DEBUG */
315 +#include "apr_hash.h"
316 +#include "apr_pools.h"
317 +#include "apr_file_io.h"
318 +#include "apr_portable.h"
319 +#include "apr_strings.h"
320 +#include "apr_thread_proc.h"
321 +#include "apr_signal.h"
323 +#define APR_WANT_STDIO
324 +#define APR_WANT_STRFUNC
325 +#define APR_WANT_IOVEC
326 +#include "apr_want.h"
328 +#if APR_HAVE_UNISTD_H
331 +#if APR_HAVE_SYS_TYPES_H
332 +#include <sys/types.h>
335 +#define CORE_PRIVATE
337 +#include "ap_config.h"
339 +#include "mpm_default.h"
340 +#include "http_main.h"
341 +#include "http_log.h"
342 +#include "http_config.h"
343 +#include "http_core.h" /* for get_remote_host */
344 +#include "http_connection.h"
345 +#include "http_protocol.h" /* for ap_hook_post_read_request */
346 +#include "scoreboard.h"
349 +#include "mpm_common.h"
350 +#include "ap_listen.h"
352 +#include "apr_poll.h"
353 +#include "util_ebcdic.h"
355 +#ifdef HAVE_BSTRING_H
356 +#include <bstring.h> /* for IRIX, FD_SET calls bzero() */
363 +#ifdef HAVE_SYS_PROCESSOR_H
364 +#include <sys/processor.h> /* for bindprocessor() */
367 +#if APR_HAS_SHARED_MEMORY
368 +#include "apr_shm.h"
370 +#error "Peruser MPM requres shared memory support."
374 +/* should be APR-ized */
377 +#include <sys/stat.h>
382 +#include <sys/times.h>
385 +#ifdef MPM_PERUSER_DEBUG
386 +# define _DBG(text,par...) \
387 + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL, \
388 + "(peruser: pid=%d uid=%d child=%d) %s(): " text, \
389 + getpid(), getuid(), my_child_num, __FUNCTION__, ##par, 0)
391 +# define _TRACE_CALL(text,par...) _DBG("calling " text, ##par)
392 +# define _TRACE_RET(text,par...) _DBG("returned from " text, ##par)
394 +# define _DBG(text,par...)
395 +# define _TRACE_RET(text,par...)
396 +# define _TRACE_CALL(text,par...)
397 +#endif /* MPM_PERUSER_DEBUG */
399 +/* char of death - for signalling children to die */
400 +#define AP_PERUSER_CHAR_OF_DEATH '!'
402 +#define PERUSER_SERVER_CONF(cf) \
403 + ((peruser_server_conf *) ap_get_module_config(cf, &mpm_peruser_module))
405 +#define SCOREBOARD_STATUS(i) ap_scoreboard_image->servers[i][0].status
408 + * Define some magic numbers that we use for the state of the incomming
409 + * request. These must be < 0 so they don't collide with a file descriptor.
411 +#define AP_PERUSER_THISCHILD -1
412 +#define AP_PERUSER_OTHERCHILD -2
415 +/* Limit on the total --- clients will be locked out if more servers than
416 + * this are needed. It is intended solely to keep the server from crashing
417 + * when things get out of hand.
419 + * We keep a hard maximum number of servers, for two reasons --- first off,
420 + * in case something goes seriously wrong, we want to stop the fork bomb
421 + * short of actually crashing the machine we're running on by filling some
422 + * kernel table. Secondly, it keeps the size of the scoreboard file small
423 + * enough that we can read the whole thing without worrying too much about
426 +#ifndef DEFAULT_SERVER_LIMIT
427 +#define DEFAULT_SERVER_LIMIT 256
430 +/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT. We want
431 + * some sort of compile-time limit to help catch typos.
433 +#ifndef MAX_SERVER_LIMIT
434 +#define MAX_SERVER_LIMIT 20000
437 +#ifndef HARD_THREAD_LIMIT
438 +#define HARD_THREAD_LIMIT 1
441 +#define CHILD_TYPE_UNKNOWN 0
442 +#define CHILD_TYPE_MULTIPLEXER 1
443 +#define CHILD_TYPE_PROCESSOR 2
444 +#define CHILD_TYPE_WORKER 3
446 +#define CHILD_STATUS_STANDBY 0 /* wait for a request before starting */
447 +#define CHILD_STATUS_STARTING 1 /* wait for socket creation */
448 +#define CHILD_STATUS_READY 2 /* wait for mux to restart */
449 +#define CHILD_STATUS_ACTIVE 3 /* ready to take requests */
450 +#define CHILD_STATUS_RESTART 4 /* child about to die and restart */
452 +/* config globals */
454 +int ap_threads_per_child=0; /* Worker threads per child */
455 +static apr_proc_mutex_t *accept_mutex;
456 +static int ap_min_processors=DEFAULT_MIN_PROCESSORS;
457 +static int ap_min_free_processors=DEFAULT_MIN_FREE_PROCESSORS;
458 +static int ap_max_processors=DEFAULT_MAX_PROCESSORS;
459 +static int ap_daemons_limit=0; /* MaxClients */
460 +static int expire_timeout=1800;
461 +static int server_limit = DEFAULT_SERVER_LIMIT;
462 +static int first_server_limit;
463 +static int changed_limit_at_restart;
464 +static int requests_this_child;
465 +static int mpm_state = AP_MPMQ_STARTING;
466 +static ap_pod_t *pod;
468 +/* === configuration stuff === */
474 + /* security settings */
475 + uid_t uid; /* user id */
476 + gid_t gid; /* group id */
477 + const char *chroot; /* directory to chroot() to, can be null */
479 + /* resource settings */
480 + int min_processors;
481 + int min_free_processors;
482 + int max_processors;
485 + int input; /* The socket descriptor */
486 + int output; /* The socket descriptor */
492 +} server_env_control;
496 + server_env_control *control;
497 + server_env_t *table;
503 + /* identification */
504 + int id; /* index in child_info_table */
505 + pid_t pid; /* process id */
506 + int status; /* status of child */
507 + int type; /* multiplexer or processor */
508 + server_env_t *senv;
513 + /* stack context saved state */
520 +} child_info_control;
524 + child_info_control *control;
525 + child_info_t *table;
531 + server_env_t *senv;
532 +} peruser_server_conf;
535 +typedef struct peruser_header
542 +/* Tables used to determine the user and group each child process should
543 + * run as. The hash table is used to correlate a server name with a child
546 +static apr_size_t child_info_size;
547 +static child_info *child_info_image;
548 +struct ap_ctable *ap_child_table;
550 +#define NUM_CHILDS (child_info_image != NULL ? child_info_image->control->num : 0)
551 +#define CHILD_INFO_TABLE (child_info_image != NULL ? child_info_image->table : NULL)
553 +static apr_size_t server_env_size;
554 +static server_env *server_env_image;
556 +#define NUM_SENV (server_env_image != NULL ? server_env_image->control->num : 0)
557 +#define SENV (server_env_image != NULL ? server_env_image->table : NULL)
559 +#if APR_HAS_SHARED_MEMORY
561 +static /* but must be exported to mpm_winnt */
563 + apr_shm_t *child_info_shm = NULL;
564 + apr_shm_t *server_env_shm = NULL;
568 + * The max child slot ever assigned, preserved across restarts. Necessary
569 + * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts. We
570 + * use this value to optimize routines that have to scan the entire scoreboard.
572 +int ap_max_daemons_limit = -1;
573 +server_rec *ap_server_conf;
575 +module AP_MODULE_DECLARE_DATA mpm_peruser_module;
577 +/* -- replace the pipe-of-death by an control socket -- */
578 +static apr_file_t *pipe_of_death_in = NULL;
579 +static apr_file_t *pipe_of_death_out = NULL;
582 +/* one_process --- debugging mode variable; can be set from the command line
583 + * with the -X flag. If set, this gets you the child_main loop running
584 + * in the process which originally started up (no detach, no make_child),
585 + * which is a pretty nice debugging environment. (You'll get a SIGHUP
586 + * early in standalone_main; just continue through. This is the server
587 + * trying to kill off any child processes which it might have lying
588 + * around --- Apache doesn't keep track of their pids, it just sends
589 + * SIGHUP to the process group, ignoring it in the root process.
590 + * Continue through and you'll be fine.).
593 +static int one_process = 0;
595 +static apr_pool_t *pconf; /* Pool for config stuff */
596 +static apr_pool_t *pchild; /* Pool for httpd child stuff */
598 +static pid_t ap_my_pid; /* it seems silly to call getpid all the time */
599 +static pid_t parent_pid;
600 +static int my_child_num;
601 +ap_generation_t volatile ap_my_generation=0;
605 +char tpf_server_name[INETD_SERVNAME_LENGTH+1];
608 +static int die_now = 0;
612 + * change directory for gprof to plop the gmon.out file
613 + * configure in httpd.conf:
614 + * GprofDir $RuntimeDir/ -> $ServerRoot/$RuntimeDir/gmon.out
615 + * GprofDir $RuntimeDir/% -> $ServerRoot/$RuntimeDir/gprof.$pid/gmon.out
617 +static void chdir_for_gprof(void)
619 + core_server_config *sconf =
620 + ap_get_module_config(ap_server_conf->module_config, &core_module);
621 + char *dir = sconf->gprof_dir;
622 + const char *use_dir;
627 + int len = strlen(sconf->gprof_dir) - 1;
628 + if(*(dir + len) == '%') {
630 + apr_snprintf(buf, sizeof(buf), "%sgprof.%d", dir, (int)getpid());
632 + use_dir = ap_server_root_relative(pconf, buf[0] ? buf : dir);
633 + res = apr_dir_make(use_dir, 0755, pconf);
634 + if(res != APR_SUCCESS && !APR_STATUS_IS_EEXIST(res)) {
635 + ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
636 + "gprof: error creating directory %s", dir);
640 + use_dir = ap_server_root_relative(pconf, DEFAULT_REL_RUNTIMEDIR);
646 +#define chdir_for_gprof()
649 +char* child_type_string(int type)
653 + case CHILD_TYPE_MULTIPLEXER: return "MULTIPLEXER";
654 + case CHILD_TYPE_PROCESSOR: return "PROCESSOR";
655 + case CHILD_TYPE_WORKER: return "WORKER";
661 +char* child_status_string(int status)
665 + case CHILD_STATUS_STANDBY: return "STANDBY";
666 + case CHILD_STATUS_STARTING: return "STARTING";
667 + case CHILD_STATUS_READY: return "READY";
668 + case CHILD_STATUS_ACTIVE: return "ACTIVE";
669 + case CHILD_STATUS_RESTART: return "RESTART";
675 +void dump_child_table()
678 + server_env_t *senv;
680 + _DBG("%-3s %-5s %-8s %-12s %-4s %-4s %-25s %5s %6s %7s",
681 + "ID", "PID", "STATUS", "TYPE", "UID", "GID", "CHROOT", "INPUT", "OUTPUT", "SOCK_FD");
683 + for(x = 0; x < NUM_CHILDS; x++)
685 + senv = CHILD_INFO_TABLE[x].senv;
686 + _DBG("%-3d %-5d %-8s %-12s %-4d %-4d %-25s %-5d %-6d %-7d",
687 + CHILD_INFO_TABLE[x].id,
688 + CHILD_INFO_TABLE[x].pid,
689 + child_status_string(CHILD_INFO_TABLE[x].status),
690 + child_type_string(CHILD_INFO_TABLE[x].type),
691 + senv == NULL ? -1 : senv->uid,
692 + senv == NULL ? -1 : senv->gid,
693 + senv == NULL ? NULL : senv->chroot,
694 + senv == NULL ? -1 : CHILD_INFO_TABLE[x].senv->input,
695 + senv == NULL ? -1 : CHILD_INFO_TABLE[x].senv->output,
696 + CHILD_INFO_TABLE[x].sock_fd);
700 +/* XXX - I don't know if TPF will ever use this module or not, so leave
701 + * the ap_check_signals calls in but disable them - manoj */
702 +#define ap_check_signals()
704 +/* a clean exit from a child with proper cleanup */
705 +static inline int clean_child_exit(int code) __attribute__ ((noreturn));
706 +static inline int clean_child_exit(int code)
710 + mpm_state = AP_MPMQ_STOPPING;
712 + if (CHILD_INFO_TABLE[my_child_num].type != CHILD_TYPE_MULTIPLEXER &&
713 + CHILD_INFO_TABLE[my_child_num].senv)
715 + retval = close(CHILD_INFO_TABLE[my_child_num].senv->input);
716 + _DBG("close(CHILD_INFO_TABLE[%d].senv->input) = %d",
717 + my_child_num, retval);
719 + retval = close(CHILD_INFO_TABLE[my_child_num].senv->output);
720 + _DBG("close(CHILD_INFO_TABLE[%d].senv->output) = %d",
721 + my_child_num, retval);
725 + apr_pool_destroy(pchild);
727 + ap_mpm_pod_close(pod);
732 +static void accept_mutex_on(void)
734 +/* for some reason this fails if we listen on the pipe_of_death.
735 + fortunately I don't think we currently need it */
738 + apr_status_t rv = apr_proc_mutex_lock(accept_mutex);
739 + if (rv != APR_SUCCESS) {
740 + const char *msg = "couldn't grab the accept mutex";
742 + if (ap_my_generation !=
743 + ap_scoreboard_image->global->running_generation) {
744 + ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, msg);
745 + clean_child_exit(0);
748 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, msg);
749 + exit(APEXIT_CHILDFATAL);
755 +static void accept_mutex_off(void)
758 + apr_status_t rv = apr_proc_mutex_unlock(accept_mutex);
759 + if (rv != APR_SUCCESS) {
760 + const char *msg = "couldn't release the accept mutex";
762 + if (ap_my_generation !=
763 + ap_scoreboard_image->global->running_generation) {
764 + ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, msg);
765 + /* don't exit here... we have a connection to
766 + * process, after which point we'll see that the
767 + * generation changed and we'll exit cleanly
771 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, msg);
772 + exit(APEXIT_CHILDFATAL);
778 +/* On some architectures it's safe to do unserialized accept()s in the single
779 + * Listen case. But it's never safe to do it in the case where there's
780 + * multiple Listen statements. Define SINGLE_LISTEN_UNSERIALIZED_ACCEPT
781 + * when it's safe in the single Listen case.
783 +#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
784 +#define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)
786 +#define SAFE_ACCEPT(stmt) do {stmt;} while(0)
789 +AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
791 + switch(query_code){
792 + case AP_MPMQ_MAX_DAEMON_USED:
793 + *result = ap_daemons_limit;
794 + return APR_SUCCESS;
795 + case AP_MPMQ_IS_THREADED:
796 + *result = AP_MPMQ_NOT_SUPPORTED;
797 + return APR_SUCCESS;
798 + case AP_MPMQ_IS_FORKED:
799 + *result = AP_MPMQ_DYNAMIC;
800 + return APR_SUCCESS;
801 + case AP_MPMQ_HARD_LIMIT_DAEMONS:
802 + *result = server_limit;
803 + return APR_SUCCESS;
804 + case AP_MPMQ_HARD_LIMIT_THREADS:
805 + *result = HARD_THREAD_LIMIT;
806 + return APR_SUCCESS;
807 + case AP_MPMQ_MAX_THREADS:
809 + return APR_SUCCESS;
810 + case AP_MPMQ_MIN_SPARE_DAEMONS:
811 + *result = ap_min_free_processors;
812 + return APR_SUCCESS;
813 + case AP_MPMQ_MIN_SPARE_THREADS:
815 + return APR_SUCCESS;
816 + case AP_MPMQ_MAX_SPARE_THREADS:
818 + return APR_SUCCESS;
819 + case AP_MPMQ_MAX_REQUESTS_DAEMON:
820 + *result = ap_max_requests_per_child;
821 + return APR_SUCCESS;
822 + case AP_MPMQ_MAX_DAEMONS:
823 + *result = server_limit;
824 + return APR_SUCCESS;
825 + case AP_MPMQ_MPM_STATE:
826 + *result = mpm_state;
827 + return APR_SUCCESS;
829 + return APR_ENOTIMPL;
832 +#if defined(NEED_WAITPID)
834 + Systems without a real waitpid sometimes lose a child's exit while waiting
835 + for another. Search through the scoreboard for missing children.
837 +int reap_children(int *exitcode, apr_exit_why_e *status)
841 + for (n = 0; n < ap_max_daemons_limit; ++n) {
842 + if (ap_scoreboard_image->servers[n][0].status != SERVER_DEAD &&
843 + kill((pid = ap_scoreboard_image->parent[n].pid), 0) == -1) {
844 + ap_update_child_status_from_indexes(n, 0, SERVER_DEAD, NULL);
845 + /* just mark it as having a successful exit status */
846 + *status = APR_PROC_EXIT;
855 +/* handle all varieties of core dumping signals */
856 +static void sig_coredump(int sig)
858 + chdir(ap_coredump_dir);
859 + apr_signal(sig, SIG_DFL);
860 + if (ap_my_pid == parent_pid) {
861 + ap_log_error(APLOG_MARK, APLOG_NOTICE,
863 + "seg fault or similar nasty error detected "
864 + "in the parent process");
866 + kill(getpid(), sig);
867 + /* At this point we've got sig blocked, because we're still inside
868 + * the signal handler. When we leave the signal handler it will
869 + * be unblocked, and we'll take the signal... and coredump or whatever
870 + * is appropriate for this particular Unix. In addition the parent
871 + * will see the real signal we received -- whereas if we called
872 + * abort() here, the parent would only see SIGABRT.
876 +/*****************************************************************
877 + * Connection structures and accounting...
880 +static void just_die(int sig)
882 +_DBG("function called");
883 + clean_child_exit(0);
886 +/* volatile just in case */
887 +static int volatile shutdown_pending;
888 +static int volatile restart_pending;
889 +static int volatile is_graceful;
890 +/* XXX static int volatile child_fatal; */
892 +static void sig_term(int sig)
894 + if (shutdown_pending == 1) {
895 + /* Um, is this _probably_ not an error, if the user has
896 + * tried to do a shutdown twice quickly, so we won't
897 + * worry about reporting it.
901 + shutdown_pending = 1;
904 +/* restart() is the signal handler for SIGHUP and AP_SIG_GRACEFUL
905 + * in the parent process, unless running in ONE_PROCESS mode
907 +static void restart(int sig)
909 + if (restart_pending == 1) {
910 + /* Probably not an error - don't bother reporting it */
913 + restart_pending = 1;
914 + is_graceful = (sig == AP_SIG_GRACEFUL);
917 +/* Sets die_now if we received a character on the pipe_of_death */
918 +static apr_status_t check_pipe_of_death
926 + char pipe_read_char;
929 + _DBG("WATCH: die_now=%d", die_now);
931 + if (die_now) return APR_SUCCESS;
933 + /* apr_thread_mutex_lock(pipe_of_death_mutex); */
934 + ret = apr_socket_recv(lr->sd, &pipe_read_char, &n);
935 + if (APR_STATUS_IS_EAGAIN(ret))
937 + /* It lost the lottery. It must continue to suffer
938 + * through a life of servitude. */
942 + if (pipe_read_char != AP_PERUSER_CHAR_OF_DEATH)
944 + _DBG("got wrong char %c", pipe_read_char);
945 + return APR_SUCCESS;
947 + /* It won the lottery (or something else is very
948 + * wrong). Embrace death with open arms. */
950 + _DBG("WATCH: die_now=%d", die_now);
952 + /* apr_thread_mutex_unlock(pipe_of_death_mutex); */
953 + return APR_SUCCESS;
956 +static void set_signals(void)
958 +#ifndef NO_USE_SIGACTION
959 + struct sigaction sa;
961 + sigemptyset(&sa.sa_mask);
964 + if (!one_process) {
965 + sa.sa_handler = sig_coredump;
966 +#if defined(SA_ONESHOT)
967 + sa.sa_flags = SA_ONESHOT;
968 +#elif defined(SA_RESETHAND)
969 + sa.sa_flags = SA_RESETHAND;
971 + if (sigaction(SIGSEGV, &sa, NULL) < 0)
972 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
974 + if (sigaction(SIGBUS, &sa, NULL) < 0)
975 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
978 + if (sigaction(SIGABORT, &sa, NULL) < 0)
979 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABORT)");
982 + if (sigaction(SIGABRT, &sa, NULL) < 0)
983 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
986 + if (sigaction(SIGILL, &sa, NULL) < 0)
987 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
991 + sa.sa_handler = sig_term;
992 + if (sigaction(SIGTERM, &sa, NULL) < 0)
993 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
995 + if (sigaction(SIGINT, &sa, NULL) < 0)
996 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
999 + sa.sa_handler = SIG_DFL;
1000 + if (sigaction(SIGXCPU, &sa, NULL) < 0)
1001 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");
1004 + sa.sa_handler = SIG_IGN;
1005 + if (sigaction(SIGXFSZ, &sa, NULL) < 0)
1006 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");
1009 + sa.sa_handler = SIG_IGN;
1010 + if (sigaction(SIGPIPE, &sa, NULL) < 0)
1011 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
1014 + /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy
1015 + * processing one */
1016 + sigaddset(&sa.sa_mask, SIGHUP);
1017 + sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
1018 + sa.sa_handler = restart;
1019 + if (sigaction(SIGHUP, &sa, NULL) < 0)
1020 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
1021 + if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
1022 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(" AP_SIG_GRACEFUL_STRING ")");
1024 + if (!one_process) {
1025 + apr_signal(SIGSEGV, sig_coredump);
1027 + apr_signal(SIGBUS, sig_coredump);
1028 +#endif /* SIGBUS */
1030 + apr_signal(SIGABORT, sig_coredump);
1031 +#endif /* SIGABORT */
1033 + apr_signal(SIGABRT, sig_coredump);
1034 +#endif /* SIGABRT */
1036 + apr_signal(SIGILL, sig_coredump);
1037 +#endif /* SIGILL */
1039 + apr_signal(SIGXCPU, SIG_DFL);
1040 +#endif /* SIGXCPU */
1042 + apr_signal(SIGXFSZ, SIG_DFL);
1043 +#endif /* SIGXFSZ */
1046 + apr_signal(SIGTERM, sig_term);
1048 + apr_signal(SIGHUP, restart);
1049 +#endif /* SIGHUP */
1050 +#ifdef AP_SIG_GRACEFUL
1051 + apr_signal(AP_SIG_GRACEFUL, restart);
1052 +#endif /* AP_SIG_GRACEFUL */
1054 + apr_signal(SIGPIPE, SIG_IGN);
1055 +#endif /* SIGPIPE */
1060 +/*****************************************************************
1061 + * Child process main loop.
1062 + * The following vars are static to avoid getting clobbered by longjmp();
1063 + * they are really private to child_main.
1066 +static int requests_this_child;
1067 +static int num_listensocks = 0;
1068 +static ap_listen_rec *listensocks;
1070 +int ap_graceful_stop_signalled(void)
1072 + /* not ever called anymore... */
1076 +static void process_socket(apr_pool_t *p, apr_socket_t *sock, long conn_id,
1077 + apr_bucket_alloc_t *bucket_alloc)
1079 + conn_rec *current_conn;
1082 + ap_sb_handle_t *sbh;
1084 + if ((rv = apr_os_sock_get(&sock_fd, sock)) != APR_SUCCESS)
1086 + ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "apr_os_sock_get");
1089 + _DBG("child_num=%d sock=%ld sock_fd=%d\n", my_child_num, sock, sock_fd);
1090 + _DBG("type=%s %d", child_type_string(CHILD_INFO_TABLE[my_child_num].type), my_child_num);
1092 + if (sock_fd >= FD_SETSIZE)
1094 + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1095 + "new file descriptor %d is too large; you probably need "
1096 + "to rebuild Apache with a larger FD_SETSIZE "
1098 + sock_fd, FD_SETSIZE);
1099 + apr_socket_close(sock);
1100 + _DBG("child_num=%d: exiting with error", my_child_num);
1104 + if (CHILD_INFO_TABLE[my_child_num].sock_fd < 0)
1106 + ap_sock_disable_nagle(sock);
1109 + ap_create_sb_handle(&sbh, p, conn_id, 0);
1110 + current_conn = ap_run_create_connection(p, ap_server_conf, sock, conn_id,
1111 + sbh, bucket_alloc);
1115 + ap_process_connection(current_conn, sock);
1116 + ap_lingering_close(current_conn);
1120 +static int peruser_process_connection(conn_rec *conn)
1122 + ap_filter_t *filter;
1123 + apr_bucket_brigade *bb;
1124 + core_net_rec *net;
1126 + _DBG("function entered",0);
1128 + /* -- fetch our sockets from the pool -- */
1129 + apr_pool_userdata_get((void **)&bb, "PERUSER_SOCKETS", conn->pool);
1132 + /* -- find the 'core' filter and give the socket data to it -- */
1133 + for (filter = conn->output_filters; filter != NULL; filter = filter->next)
1135 + if (!strcmp(filter->frec->name, "core")) break;
1137 + if (filter != NULL)
1139 + net = filter->ctx;
1140 + net->in_ctx = apr_palloc(conn->pool, sizeof(*net->in_ctx));
1141 + net->in_ctx->b = bb;
1143 + net->in_ctx->tmpbb = apr_brigade_create(net->in_ctx->b->p,
1144 + net->in_ctx->b->bucket_alloc);
1148 + _DBG("leaving (DECLINED)", 0);
1152 +static int total_processors(int child_num)
1156 + for(i = 0, total = 0; i < NUM_CHILDS; ++i)
1158 + if(CHILD_INFO_TABLE[i].senv == CHILD_INFO_TABLE[child_num].senv)
1165 +static int idle_processors(int child_num)
1169 + for(i = 0, total = 0; i < NUM_CHILDS; ++i)
1171 + if(CHILD_INFO_TABLE[i].senv == CHILD_INFO_TABLE[child_num].senv &&
1172 + (SCOREBOARD_STATUS(i) == SERVER_STARTING ||
1173 + SCOREBOARD_STATUS(i) == SERVER_READY))
1182 +static int pass_request(request_rec *r, child_info_t *processor)
1185 + struct msghdr msg;
1186 + struct cmsghdr *cmsg;
1189 + struct iovec iov[4];
1190 + conn_rec *c = r->connection;
1191 + apr_bucket_brigade *bb = apr_brigade_create(r->pool, c->bucket_alloc);
1192 + apr_bucket_brigade *body_bb = NULL;
1193 + apr_size_t len = 0;
1194 + apr_size_t header_len = 0;
1195 + apr_size_t body_len = 0;
1197 + apr_bucket *bucket;
1198 + const apr_array_header_t *headers_in_array;
1199 + const apr_table_entry_t *headers_in;
1202 + apr_socket_t *thesock = ap_get_module_config(r->connection->conn_config, &core_module);
1204 + if ((!r->the_request) || (!strlen(r->the_request)))
1206 + _DBG("empty request. dropping it (%ld)", r->the_request);
1212 + _DBG("server %s in child %d has no child_info associated",
1213 + r->hostname, my_child_num);
1217 + _DBG("passing request to another child. Vhost: %s, child %d %d",
1218 + apr_table_get(r->headers_in, "Host"), my_child_num, processor->senv->output);
1219 + _DBG("r->the_request=\"%s\" len=%d", r->the_request, strlen(r->the_request));
1221 + ap_get_brigade(r->connection->input_filters, bb, AP_MODE_EXHAUSTIVE, APR_NONBLOCK_READ, len);
1223 + /* Scan the brigade looking for heap-buckets */
1225 + _DBG("Scanning the brigade",0);
1226 + bucket = APR_BRIGADE_FIRST(bb);
1227 + while (bucket != APR_BRIGADE_SENTINEL(bb) &&
1228 + APR_BUCKET_IS_HEAP(bucket)) {
1229 + _DBG("HEAP BUCKET is found, length=%d", bucket->length);
1230 + bucket = APR_BUCKET_NEXT(bucket);
1231 + if (!APR_BUCKET_IS_HEAP(bucket)) {
1232 + _DBG("NON-HEAP BUCKET is found, extracting the part of brigade before it",0);
1234 + bb = apr_brigade_split(body_bb, bucket);
1235 + /* Do we need to apr_destroy_brigade(bb) here?
1236 + * Yeah, I know we do apr_pool_destroy(r->pool) before return, but
1237 + * ap_get_brigade is in non-blocking mode (however len is zero).
1239 + if (apr_brigade_pflatten(body_bb, &body, &body_len, r->pool) != APR_SUCCESS) {
1240 + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1241 + "Unable to flatten brigade, declining request");
1242 + apr_pool_destroy(r->pool);
1245 + _DBG("Brigade is flattened as body (body_len=%d)", body_len);
1248 + _DBG("Scanning is finished",0);
1250 + apr_os_sock_get(&sock_fd, thesock);
1254 + headers_in_array = apr_table_elts(r->headers_in);
1255 + headers_in = (const apr_table_entry_t *) headers_in_array->elts;
1257 + h.headers = apr_pstrcat(h.p, r->the_request, CRLF, NULL);
1258 + for (counter = 0; counter < headers_in_array->nelts; counter++) {
1259 + if (headers_in[counter].key == NULL
1260 + || headers_in[counter].val == NULL) {
1263 + h.headers = apr_pstrcat(h.p, h.headers, headers_in[counter].key, ": ",
1264 + headers_in[counter].val, CRLF, NULL);
1267 + h.headers = apr_pstrcat(h.p, h.headers, CRLF, NULL);
1268 + ap_xlate_proto_to_ascii(h.headers, strlen(h.headers));
1270 + header_len = strlen(h.headers);
1272 + iov[0].iov_base = &header_len;
1273 + iov[0].iov_len = sizeof(header_len);
1274 + iov[1].iov_base = &body_len;
1275 + iov[1].iov_len = sizeof(body_len);
1276 + iov[2].iov_base = h.headers;
1277 + iov[2].iov_len = strlen(h.headers) + 1;
1278 + iov[3].iov_base = body;
1279 + iov[3].iov_len = body_len;
1281 + msg.msg_name = NULL;
1282 + msg.msg_namelen = 0;
1283 + msg.msg_iov = iov;
1284 + msg.msg_iovlen = 4;
1286 + cmsg = apr_palloc(r->pool, sizeof(*cmsg) + sizeof(sock_fd));
1287 + cmsg->cmsg_len = sizeof(*cmsg) + sizeof(sock_fd);
1288 + cmsg->cmsg_level = SOL_SOCKET;
1289 + cmsg->cmsg_type = SCM_RIGHTS;
1291 + memcpy(CMSG_DATA(cmsg), &sock_fd, sizeof(sock_fd));
1293 + msg.msg_control = cmsg;
1294 + msg.msg_controllen = cmsg->cmsg_len;
1297 + if (processor->status == CHILD_STATUS_STANDBY)
1299 + _DBG("Activating child #%d", processor->id);
1300 + processor->status = CHILD_STATUS_STARTING;
1303 + while (processor->status != CHILD_STATUS_ACTIVE)
1305 + _DBG("Waiting for child #%d...", processor->id);
1309 + _DBG("Writing message to %d, passing sock_fd: %d", processor->senv->output, sock_fd);
1310 + _DBG("header_len=%d headers=\"%s\"", header_len, h.headers);
1311 + _DBG("body_len=%d body=\"%s\"", body_len, body);
1313 + if ((rv = sendmsg(processor->senv->output, &msg, 0)) == -1)
1315 + apr_pool_destroy(r->pool);
1316 + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1317 + "Writing message failed %d %d", rv, errno);
1321 + _DBG("Writing message succeeded %d", rv);
1323 + /* -- close the socket on our side -- */
1324 + _DBG("closing socket %d on our side", sock_fd);
1325 + apr_socket_close(thesock);
1327 + apr_pool_destroy(r->pool);
1332 +static apr_status_t receive_from_multiplexer(
1333 + void **trans_sock, /* will be filled out w/ the received socket */
1334 + ap_listen_rec *lr, /* listener to receive from */
1335 + apr_pool_t *ptrans /* transaction wide pool */
1338 + struct msghdr msg;
1339 + struct cmsghdr *cmsg;
1340 + char buff[HUGE_STRING_LEN] = "";
1341 + char headers[HUGE_STRING_LEN] = "";
1343 + apr_size_t header_len, body_len;
1344 + struct iovec iov[3];
1346 + apr_os_sock_t ctrl_sock_fd;
1347 + apr_os_sock_t trans_sock_fd;
1349 + /* -- bucket's, brigades and their allocators */
1350 + apr_bucket_alloc_t *alloc = apr_bucket_alloc_create(ptrans);
1351 + apr_bucket_brigade *bb = apr_brigade_create(ptrans, alloc);
1352 + apr_bucket *bucket;
1354 + /* prepare the buffers for receiving data from remote side */
1355 + iov[0].iov_base = &header_len;
1356 + iov[0].iov_len = sizeof(header_len);
1357 + iov[1].iov_base = &body_len;
1358 + iov[1].iov_len = sizeof(body_len);
1359 + iov[2].iov_base = (char*)&buff;
1360 + iov[2].iov_len = HUGE_STRING_LEN;
1362 + cmsg = apr_palloc(ptrans, sizeof(*cmsg) + sizeof(trans_sock_fd));
1363 + cmsg->cmsg_len = sizeof(*cmsg) + sizeof(trans_sock_fd);
1365 + msg.msg_name = NULL;
1366 + msg.msg_namelen = 0;
1367 + msg.msg_iov = iov;
1368 + msg.msg_iovlen = 3;
1369 + msg.msg_control = cmsg;
1370 + msg.msg_controllen = cmsg->cmsg_len;
1372 + /* -- receive data from socket -- */
1373 + apr_os_sock_get(&ctrl_sock_fd, lr->sd);
1374 + _DBG("receiving from sock_fd=%d", ctrl_sock_fd);
1375 + ret = recvmsg(ctrl_sock_fd, &msg, 0);
1378 + _DBG("recvmsg failed with error \"%s\"", strerror(errno));
1380 + _DBG("recvmsg returned %d", ret);
1382 + /* -- extract socket from the cmsg -- */
1383 + memcpy(&trans_sock_fd, CMSG_DATA(cmsg), sizeof(trans_sock_fd));
1384 + apr_os_sock_put((apr_socket_t **)trans_sock, &trans_sock_fd, ptrans);
1385 + apr_os_sock_get(&fd_tmp, *trans_sock);
1387 + _DBG("trans_sock=%ld fdx=%d sock_fd=%d",
1388 + *trans_sock, trans_sock_fd, fd_tmp);
1390 + apr_cpystrn(headers, buff, header_len + 1);
1391 + _DBG("header_len=%d headers=\"%s\"", header_len, headers);
1393 + /* -- store received data into an brigade and add
1394 + it to the current transaction's pool -- */
1395 + bucket = apr_bucket_eos_create(alloc);
1396 + APR_BRIGADE_INSERT_HEAD(bb, bucket);
1397 + bucket = apr_bucket_socket_create(*trans_sock, alloc);
1398 + APR_BRIGADE_INSERT_HEAD(bb, bucket);
1401 + body = (char*)&buff[header_len + 1];
1402 + _DBG("body_len=%d body=\"%s\"", body_len, body);
1404 + bucket = apr_bucket_heap_create(body, body_len, NULL, alloc);
1405 + APR_BRIGADE_INSERT_HEAD(bb, bucket);
1407 + _DBG("There is no body",0);
1410 + bucket = apr_bucket_heap_create(headers, header_len, NULL, alloc);
1412 + APR_BRIGADE_INSERT_HEAD(bb, bucket);
1413 + apr_pool_userdata_set(bb, "PERUSER_SOCKETS", NULL, ptrans);
1415 + _DBG("returning 0", 0);
1420 +/* Set group privileges.
1422 + * Note that we use the username as set in the config files, rather than
1423 + * the lookup of to uid --- the same uid may have multiple passwd entries,
1424 + * with different sets of groups for each.
1427 +static int set_group_privs(uid_t uid, gid_t gid)
1431 + struct passwd *ent;
1435 + * Set the GID before initgroups(), since on some platforms
1436 + * setgid() is known to zap the group list.
1438 + if (setgid(gid) == -1)
1440 + ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1441 + "setgid: unable to set group id to Group %u",
1446 + /* if getpwuid() fails, just skip initgroups() */
1448 + if ((ent = getpwuid(uid)) != NULL)
1450 + name = ent->pw_name;
1452 + /* Reset `groups' attributes. */
1454 + if (initgroups(name, gid) == -1)
1456 + ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1457 + "initgroups: unable to set groups for User %s "
1458 + "and Group %u", name, (unsigned)gid);
1466 +static int peruser_setup_child(int childnum)
1468 + server_env_t *senv = CHILD_INFO_TABLE[childnum].senv;
1470 + if(senv->chroot) {
1471 + _DBG("chroot to %s", senv->chroot);
1472 + if(chroot(senv->chroot)) {
1473 + _DBG("chroot failure %s", senv->chroot);
1478 + if (senv->uid == -1 && senv->gid == -1) {
1479 + return unixd_setup_child();
1481 + if (set_group_privs(senv->uid, senv->gid)) {
1484 + /* Only try to switch if we're running as root */
1488 + os_init_job_environment(ap_server_conf, unixd_config.user_name,
1489 + one_process) != 0 ||
1491 + setuid(senv->uid) == -1)) {
1492 + ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1493 + "setuid: unable to change to uid: %ld",
1494 + (long) senv->uid);
1500 +static int check_signal(int signum)
1502 + _DBG("signum=%d", signum);
1512 +/* Send a single HTTP header field to the client. Note that this function
1513 + * is used in calls to table_do(), so their interfaces are co-dependent.
1514 + * In other words, don't change this one without checking table_do in alloc.c.
1515 + * It returns true unless there was a write error of some kind.
1517 +static int peruser_header_field(peruser_header *h,
1518 + const char *fieldname, const char *fieldval)
1520 + apr_pstrcat(h->p, h->headers, fieldname, ": ", fieldval, CRLF, NULL);
1524 +static inline ap_listen_rec* listen_add(apr_pool_t* pool, apr_socket_t *sock, void* accept_func)
1526 + ap_listen_rec *lr_walk, *lr_new;
1528 + _DBG("function entered", 0);
1529 + /* -- create an new listener for this child -- */
1530 + lr_new = apr_palloc(pool, sizeof(*lr_new));
1531 + lr_new->sd = sock;
1532 + lr_new->active = 1;
1533 + lr_new->accept_func = accept_func;
1534 + lr_new->next = NULL;
1536 + /* -- add the new listener_rec into the list -- */
1537 + /* FIXME: should we somehow lock this list ? */
1538 + lr_walk = ap_listeners;
1541 + while (lr_walk->next) lr_walk = lr_walk->next;
1542 + lr_walk->next = lr_new;
1546 + ap_listeners = lr_walk = lr_new;
1548 + num_listensocks++;
1552 +static inline void listen_clear()
1554 + ap_listen_rec *lr_walk;
1556 + _DBG("function entered", 0);
1558 + /* FIXME: should we somehow lock this list ? */
1559 + while (ap_listeners)
1561 + lr_walk = ap_listeners->next;
1562 + apr_socket_close(ap_listeners->sd);
1563 + ap_listeners = lr_walk;
1565 + num_listensocks=0;
1568 +apr_status_t cleanup_child_info(void *d)
1570 + if (child_info_image == NULL) {
1571 + return APR_SUCCESS;
1574 + free(child_info_image);
1575 + child_info_image = NULL;
1576 + apr_shm_destroy(child_info_shm);
1578 + return APR_SUCCESS;
1581 +apr_status_t cleanup_server_environments(void *d)
1583 + if (server_env_image == NULL) {
1584 + return APR_SUCCESS;
1587 + free(server_env_image);
1588 + server_env_image = NULL;
1589 + apr_shm_destroy(server_env_shm);
1591 + return APR_SUCCESS;
1594 +static const char* child_clone();
1596 +static void child_main(int child_num_arg)
1598 + apr_pool_t *ptrans;
1599 + apr_allocator_t *allocator;
1600 + conn_rec *current_conn;
1601 + apr_status_t status = APR_EINIT;
1603 + ap_listen_rec *lr;
1604 + int curr_pollfd, last_pollfd = 0;
1605 + apr_pollfd_t *pollset;
1607 + ap_sb_handle_t *sbh;
1609 + apr_bucket_alloc_t *bucket_alloc;
1614 + mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
1615 + * child initializes
1618 + my_child_num = child_num_arg;
1619 + ap_my_pid = getpid();
1620 + requests_this_child = 0;
1622 + _DBG("sock_fd_in=%d sock_fd_out=%d",
1623 + CHILD_INFO_TABLE[my_child_num].senv->input,
1624 + CHILD_INFO_TABLE[my_child_num].senv->output);
1626 + /* Get a sub context for global allocations in this child, so that
1627 + * we can have cleanups occur when the child exits.
1629 + apr_allocator_create(&allocator);
1630 + apr_allocator_max_free_set(allocator, ap_max_mem_free);
1631 + apr_pool_create_ex(&pchild, pconf, NULL, allocator);
1632 + apr_allocator_owner_set(allocator, pchild);
1634 + apr_pool_create(&ptrans, pchild);
1635 + apr_pool_tag(ptrans, "transaction");
1637 + /* needs to be done before we switch UIDs so we have permissions */
1638 + ap_reopen_scoreboard(pchild, NULL, 0);
1640 + rv = apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname, pchild);
1641 + if (rv != APR_SUCCESS) {
1642 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1643 + "Couldn't initialize cross-process lock in child");
1644 + clean_child_exit(APEXIT_CHILDFATAL);
1647 + switch(CHILD_INFO_TABLE[my_child_num].type)
1649 + case CHILD_TYPE_MULTIPLEXER:
1650 + _DBG("MULTIPLEXER %d", my_child_num);
1652 + /* update status on processors that are ready to accept requests */
1653 + _DBG("updating processor stati", 0);
1654 + for(i = 0; i < NUM_CHILDS; ++i)
1656 + if(CHILD_INFO_TABLE[i].status == CHILD_STATUS_READY)
1657 + CHILD_INFO_TABLE[i].status = CHILD_STATUS_ACTIVE;
1662 + case CHILD_TYPE_PROCESSOR:
1663 + case CHILD_TYPE_WORKER:
1664 + _DBG("%s %d", child_type_string(CHILD_INFO_TABLE[my_child_num].type), my_child_num);
1666 + /* -- create new listener to receive from multiplexer -- */
1667 + apr_os_sock_put((void*)&sock, &CHILD_INFO_TABLE[my_child_num].senv->input, pconf);
1669 + listen_add(pconf, sock, receive_from_multiplexer);
1674 + _DBG("unspecified child type for %d sleeping a while ...", my_child_num);
1679 + apr_os_file_get(&fd, pipe_of_death_in);
1680 + apr_os_sock_put((void*)&pod_sock, &fd, pconf);
1681 + listen_add(pconf, pod_sock, check_pipe_of_death);
1683 + (peruser_setup_child(my_child_num) && clean_child_exit(APEXIT_CHILDFATAL));
1684 + ap_run_child_init(pchild, ap_server_conf);
1686 + ap_create_sb_handle(&sbh, pchild, my_child_num, 0);
1687 + (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
1689 + /* Set up the pollfd array */
1690 + listensocks = apr_pcalloc(pchild,
1691 + sizeof(*listensocks) * (num_listensocks));
1692 + for (lr = ap_listeners, i = 0; i < num_listensocks; lr = lr->next, i++) {
1693 + listensocks[i].accept_func = lr->accept_func;
1694 + listensocks[i].sd = lr->sd;
1697 + pollset = apr_palloc(pchild, sizeof(*pollset) * num_listensocks);
1698 + pollset[0].p = pchild;
1699 + for (i = 0; i < num_listensocks; i++) {
1700 + pollset[i].desc.s = listensocks[i].sd;
1701 + pollset[i].desc_type = APR_POLL_SOCKET;
1702 + pollset[i].reqevents = APR_POLLIN;
1705 + mpm_state = AP_MPMQ_RUNNING;
1707 + bucket_alloc = apr_bucket_alloc_create(pchild);
1709 + while (!die_now) {
1711 + * (Re)initialize this child to a pre-connection state.
1714 + current_conn = NULL;
1716 + apr_pool_clear(ptrans);
1718 + if (CHILD_INFO_TABLE[my_child_num].type != CHILD_TYPE_MULTIPLEXER
1719 + && ap_max_requests_per_child > 0
1720 + && requests_this_child++ >= ap_max_requests_per_child) {
1721 + _DBG("max requests reached, dying now", 0);
1722 + clean_child_exit(0);
1725 + (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
1728 + * Wait for an acceptable connection to arrive.
1731 + /* Lock around "accept", if necessary */
1732 + SAFE_ACCEPT(accept_mutex_on());
1734 + if (num_listensocks == 1) {
1738 + /* multiple listening sockets - need to poll */
1743 + ret = apr_poll(pollset, num_listensocks, &n, -1);
1744 + if (ret != APR_SUCCESS) {
1745 + if (APR_STATUS_IS_EINTR(ret)) {
1748 + /* Single Unix documents select as returning errnos
1749 + * EBADF, EINTR, and EINVAL... and in none of those
1750 + * cases does it make sense to continue. In fact
1751 + * on Linux 2.0.x we seem to end up with EFAULT
1752 + * occasionally, and we'd loop forever due to it.
1754 + ap_log_error(APLOG_MARK, APLOG_ERR, ret, ap_server_conf,
1755 + "apr_poll: (listen)");
1756 + clean_child_exit(1);
1758 + /* find a listener */
1759 + curr_pollfd = last_pollfd;
1762 + if (curr_pollfd >= num_listensocks) {
1765 + /* XXX: Should we check for POLLERR? */
1766 + if (pollset[curr_pollfd].rtnevents & APR_POLLIN) {
1767 + last_pollfd = curr_pollfd;
1768 + offset = curr_pollfd;
1771 + } while (curr_pollfd != last_pollfd);
1777 + _DBG("input available ... resetting socket.",0);
1778 + sock = NULL; /* important! */
1780 + /* if we accept() something we don't want to die, so we have to
1783 + status = listensocks[offset].accept_func(&sock, &listensocks[offset], ptrans);
1784 + SAFE_ACCEPT(accept_mutex_off()); /* unlock after "accept" */
1786 + if (status == APR_EGENERAL) {
1787 + /* resource shortage or should-not-occur occured */
1788 + clean_child_exit(1);
1790 + else if (status != APR_SUCCESS || die_now) {
1794 + if (CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_PROCESSOR ||
1795 + CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_WORKER)
1797 + _DBG("CHECKING IF WE SHOULD CLONE A CHILD...");
1799 + _DBG("total_processors = %d, max_processors = %d",
1800 + total_processors(my_child_num),
1801 + CHILD_INFO_TABLE[my_child_num].senv->max_processors);
1803 + _DBG("idle_processors = %d, min_free_processors = %d",
1804 + idle_processors(my_child_num),
1805 + CHILD_INFO_TABLE[my_child_num].senv->min_free_processors);
1807 + if(total_processors(my_child_num) <
1808 + CHILD_INFO_TABLE[my_child_num].senv->max_processors &&
1809 + idle_processors(my_child_num) <
1810 + CHILD_INFO_TABLE[my_child_num].senv->min_free_processors)
1812 + _DBG("CLONING CHILD");
1817 + if (!setjmp(CHILD_INFO_TABLE[my_child_num].jmpbuffer))
1819 + _DBG("marked jmpbuffer",0);
1820 + _TRACE_CALL("process_socket()",0);
1821 + process_socket(ptrans, sock, my_child_num, bucket_alloc);
1822 + _TRACE_RET("process_socket()",0);
1826 + _DBG("landed from longjmp",0);
1827 + CHILD_INFO_TABLE[my_child_num].sock_fd = AP_PERUSER_THISCHILD;
1830 + /* Check the pod and the generation number after processing a
1831 + * connection so that we'll go away if a graceful restart occurred
1832 + * while we were processing the connection or we are the lucky
1833 + * idle server process that gets to die.
1835 + if (ap_mpm_pod_check(pod) == APR_SUCCESS) { /* selected as idle? */
1836 + _DBG("ap_mpm_pod_check(pod) = APR_SUCCESS; dying now", 0);
1839 + else if (ap_my_generation !=
1840 + ap_scoreboard_image->global->running_generation) { /* restart? */
1841 + /* yeah, this could be non-graceful restart, in which case the
1842 + * parent will kill us soon enough, but why bother checking?
1844 + _DBG("ap_my_generation != ap_scoreboard_image->global->running_generation; dying now", 0);
1848 + if(CHILD_INFO_TABLE[my_child_num].status == CHILD_STATUS_RESTART)
1850 + _DBG("restarting", 0);
1855 + _DBG("clean_child_exit(0)");
1856 + clean_child_exit(0);
1859 +static server_env_t* senv_add(int uid, int gid, const char* chroot)
1864 + _DBG("Searching for matching senv...");
1866 + for(i = 0; i < NUM_SENV; i++)
1868 + if(SENV[i].uid == uid && SENV[i].gid == gid &&
1869 + (SENV[i].chroot == NULL || !strcmp(SENV[i].chroot, chroot)))
1871 + _DBG("Found existing senv: %i", i);
1876 + if(NUM_SENV >= server_limit)
1878 + _DBG("server_limit reached!");
1882 + _DBG("Creating new senv");
1884 + SENV[NUM_SENV].uid = uid;
1885 + SENV[NUM_SENV].gid = gid;
1886 + SENV[NUM_SENV].chroot = chroot;
1888 + SENV[NUM_SENV].min_processors = ap_min_processors;
1889 + SENV[NUM_SENV].min_free_processors = ap_min_free_processors;
1890 + SENV[NUM_SENV].max_processors = ap_max_processors;
1892 + socketpair(PF_UNIX, SOCK_STREAM, 0, socks);
1893 + SENV[NUM_SENV].input = socks[0];
1894 + SENV[NUM_SENV].output = socks[1];
1896 + return &SENV[server_env_image->control->num++];
1899 +static const char* child_clone()
1902 + child_info_t *this;
1903 + child_info_t *new;
1905 + if(NUM_CHILDS >= server_limit)
1907 + _DBG("Trying to use more child ID's than NumServers. "
1908 + "Increase NumServers in your config file.");
1912 + for(i = 0; i < NUM_CHILDS; i++)
1914 + if(CHILD_INFO_TABLE[i].pid == 0 &&
1915 + CHILD_INFO_TABLE[i].type == CHILD_TYPE_UNKNOWN) break;
1918 + _DBG("cloning child #%d from #%d", i, my_child_num);
1920 + this = &CHILD_INFO_TABLE[my_child_num];
1921 + new = &CHILD_INFO_TABLE[i];
1923 + new->senv = this->senv;
1924 + new->type = CHILD_TYPE_WORKER;
1925 + new->sock_fd = this->sock_fd;
1926 + new->status = CHILD_STATUS_STARTING;
1928 + if(i == NUM_CHILDS) child_info_image->control->num++;
1932 +static const char* child_add(int type, int status,
1933 + uid_t uid, gid_t gid, const char* chroot)
1935 + _DBG("adding child #%d", NUM_CHILDS);
1937 + if(NUM_CHILDS >= server_limit)
1939 + return "Trying to use more child ID's than NumServers. "
1940 + "Increase NumServers in your config file.";
1943 + CHILD_INFO_TABLE[NUM_CHILDS].senv = senv_add(uid, gid, chroot);
1945 + if(CHILD_INFO_TABLE[NUM_CHILDS].senv == NULL)
1947 + return "Trying to use more server environments than NumServers. "
1948 + "Increase NumServers in your config file.";
1951 + if(type != CHILD_TYPE_WORKER)
1952 + CHILD_INFO_TABLE[NUM_CHILDS].senv->processor_id = NUM_CHILDS;
1954 + CHILD_INFO_TABLE[NUM_CHILDS].type = type;
1955 + CHILD_INFO_TABLE[NUM_CHILDS].sock_fd = AP_PERUSER_THISCHILD;
1956 + CHILD_INFO_TABLE[NUM_CHILDS].status = status;
1958 + _DBG("[%d] uid=%d gid=%d type=%d chroot=%s",
1959 + NUM_CHILDS, uid, gid, type,
1962 + if (uid == 0 || gid == 0)
1964 + _DBG("Assigning root user/group to a child.", 0);
1967 + server_env_image->control->num++;
1968 + child_info_image->control->num++;
1973 +static int make_child(server_rec *s, int slot)
1977 + child_info_t *multiplexer;
1979 + _DBG("function entered", 0);
1981 + switch (CHILD_INFO_TABLE[slot].type)
1983 + case CHILD_TYPE_MULTIPLEXER: break;
1984 + case CHILD_TYPE_PROCESSOR: break;
1985 + case CHILD_TYPE_WORKER: break;
1988 + _DBG("no valid client in slot %d", slot);
1993 + if (slot + 1 > ap_max_daemons_limit) {
1994 + ap_max_daemons_limit = slot + 1;
1997 + if (one_process) {
1998 + apr_signal(SIGHUP, just_die);
1999 + /* Don't catch AP_SIG_GRACEFUL in ONE_PROCESS mode :) */
2000 + apr_signal(SIGINT, just_die);
2002 + apr_signal(SIGQUIT, SIG_DFL);
2004 + apr_signal(SIGTERM, just_die);
2008 + (void) ap_update_child_status_from_indexes(slot, 0, SERVER_STARTING,
2009 + (request_rec *) NULL);
2011 + CHILD_INFO_TABLE[slot].status = CHILD_STATUS_ACTIVE;
2015 + /* BS2000 requires a "special" version of fork() before a setuid() call */
2016 + if ((pid = os_fork(unixd_config.user_name)) == -1) {
2018 + if ((pid = os_fork(s, slot)) == -1) {
2020 + if ((pid = fork()) == -1) {
2022 + ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process");
2024 + /* fork didn't succeed. Fix the scoreboard or else
2025 + * it will say SERVER_STARTING forever and ever
2027 + (void) ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD,
2028 + (request_rec *) NULL);
2030 + /* In case system resources are maxxed out, we don't want
2031 + Apache running away with the CPU trying to fork over and
2032 + over and over again. */
2039 +#ifdef HAVE_BINDPROCESSOR
2040 + /* by default AIX binds to a single processor
2041 + * this bit unbinds children which will then bind to another cpu
2043 + int status = bindprocessor(BINDPROCESS, (int)getpid(),
2044 + PROCESSOR_CLASS_ANY);
2045 + if (status != OK) {
2046 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
2047 + ap_server_conf, "processor unbind failed %d", status);
2050 + RAISE_SIGSTOP(MAKE_CHILD);
2052 + /* Disable the parent's signal handlers and set up proper handling in
2055 + apr_signal(SIGHUP, just_die);
2056 + apr_signal(SIGTERM, just_die);
2057 + /* The child process doesn't do anything for AP_SIG_GRACEFUL.
2058 + * Instead, the pod is used for signalling graceful restart.
2060 + /* apr_signal(AP_SIG_GRACEFUL, restart); */
2062 + clean_child_exit(0);
2065 + ap_scoreboard_image->parent[slot].pid = pid;
2066 + CHILD_INFO_TABLE[slot].pid = pid;
2068 + ap_child_table[slot].pid = pid;
2069 + ap_child_table[slot].status = SERVER_ALIVE;
2076 + * idle_spawn_rate is the number of children that will be spawned on the
2077 + * next maintenance cycle if there aren't enough idle servers. It is
2078 + * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
2079 + * without the need to spawn.
2081 +static int idle_spawn_rate = 1;
2082 +#ifndef MAX_SPAWN_RATE
2083 +#define MAX_SPAWN_RATE (32)
2085 +static int hold_off_on_exponential_spawning;
2087 +static void perform_idle_server_maintenance(apr_pool_t *p)
2092 + /* _DBG("function entered", 0); */
2094 + now = apr_time_now();
2096 + for (i = 0; i < NUM_CHILDS; ++i)
2098 + if(CHILD_INFO_TABLE[i].pid == 0)
2100 + if(CHILD_INFO_TABLE[i].status == CHILD_STATUS_STARTING)
2101 + make_child(ap_server_conf, i);
2103 + else if(expire_timeout > 0 &&
2104 + (CHILD_INFO_TABLE[i].type == CHILD_TYPE_PROCESSOR ||
2105 + CHILD_INFO_TABLE[i].type == CHILD_TYPE_WORKER) &&
2106 + ap_scoreboard_image->parent[i].pid > 1 &&
2107 + ap_scoreboard_image->servers[i][0].status != SERVER_DEAD &&
2108 + apr_time_sec(now - ap_scoreboard_image->servers[i][0].last_used) > expire_timeout)
2110 + CHILD_INFO_TABLE[i].pid = 0;
2111 + CHILD_INFO_TABLE[i].status = CHILD_STATUS_STANDBY;
2113 + if(CHILD_INFO_TABLE[i].type == CHILD_TYPE_WORKER)
2115 + /* completely free up this slot */
2117 + CHILD_INFO_TABLE[i].senv = (server_env_t*)NULL;
2118 + CHILD_INFO_TABLE[i].type = CHILD_TYPE_UNKNOWN;
2119 + CHILD_INFO_TABLE[i].sock_fd = -3; /* -1 and -2 are taken */
2122 + if(kill(ap_scoreboard_image->parent[i].pid, SIGTERM) == -1)
2124 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
2125 + ap_server_conf, "kill SIGTERM");
2128 + ap_update_child_status_from_indexes(i, 0, SERVER_DEAD, NULL);
2134 +/*****************************************************************
2135 + * Executive routines.
2138 +int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
2143 + apr_size_t one = 1;
2144 +/* apr_socket_t *sock = NULL; */
2146 + ap_log_pid(pconf, ap_pid_fname);
2148 + first_server_limit = server_limit;
2149 + if (changed_limit_at_restart) {
2150 + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2151 + "WARNING: Attempt to change ServerLimit "
2152 + "ignored during restart");
2153 + changed_limit_at_restart = 0;
2156 + ap_server_conf = s;
2158 + /* Initialize cross-process accept lock */
2159 + ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
2160 + ap_server_root_relative(_pconf, ap_lock_fname),
2163 + rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname,
2164 + ap_accept_lock_mech, _pconf);
2165 + if (rv != APR_SUCCESS) {
2166 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
2167 + "Couldn't create accept lock");
2168 + mpm_state = AP_MPMQ_STOPPING;
2173 +#if APR_USE_SYSVSEM_SERIALIZE
2174 + if (ap_accept_lock_mech == APR_LOCK_DEFAULT ||
2175 + ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
2177 + if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
2179 + rv = unixd_set_proc_mutex_perms(accept_mutex);
2180 + if (rv != APR_SUCCESS) {
2181 + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
2182 + "Couldn't set permissions on cross-process lock; "
2183 + "check User and Group directives");
2184 + mpm_state = AP_MPMQ_STOPPING;
2190 + if (!is_graceful) {
2191 + if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
2192 + mpm_state = AP_MPMQ_STOPPING;
2195 + /* fix the generation number in the global score; we just got a new,
2196 + * cleared scoreboard
2198 + ap_scoreboard_image->global->running_generation = ap_my_generation;
2201 + /* Initialize the child table */
2204 + for (i = 0; i < server_limit; i++)
2206 + ap_child_table[i].pid = 0;
2210 + /* We need to put the new listeners at the end of the ap_listeners
2211 + * list. If we don't, then the pool will be cleared before the
2212 + * open_logs phase is called for the second time, and ap_listeners
2213 + * will have only invalid data. If that happens, then the sockets
2214 + * that we opened using make_sock() will be lost, and the server
2219 + apr_os_file_get(&fd, pipe_of_death_in);
2220 + apr_os_sock_put(&sock, &fd, pconf);
2222 + listen_add(pconf, sock, check_pipe_of_death);
2226 + if (one_process) {
2230 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2231 + "%s configured -- resuming normal operations",
2232 + ap_get_server_version());
2233 + ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
2234 + "Server built: %s", ap_get_server_built());
2235 +#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
2236 + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
2237 + "AcceptMutex: %s (default: %s)",
2238 + apr_proc_mutex_name(accept_mutex),
2239 + apr_proc_mutex_defname());
2241 + restart_pending = shutdown_pending = 0;
2243 + mpm_state = AP_MPMQ_RUNNING;
2245 + _DBG("sizeof(child_info_t) = %d", sizeof(child_info_t));
2247 + while (!restart_pending && !shutdown_pending) {
2249 + apr_exit_why_e exitwhy;
2250 + int status, processed_status;
2251 + /* this is a memory leak, but I'll fix it later. */
2254 + ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
2256 + /* XXX: if it takes longer than 1 second for all our children
2257 + * to start up and get into IDLE state then we may spawn an
2260 + if (pid.pid != -1) {
2261 + processed_status = ap_process_child_status(&pid, exitwhy, status);
2262 + if (processed_status == APEXIT_CHILDFATAL) {
2263 + mpm_state = AP_MPMQ_STOPPING;
2267 + /* non-fatal death... note that it's gone in the scoreboard. */
2268 + child_slot = find_child_by_pid(&pid);
2269 + _DBG("child #%d has died ...", child_slot);
2271 + for (i = 0; i < ap_max_daemons_limit; ++i)
2273 + if (ap_child_table[i].pid == pid.pid)
2280 + if (child_slot >= 0) {
2281 + ap_child_table[child_slot].pid = 0;
2282 + _TRACE_CALL("ap_update_child_status_from_indexes", 0);
2283 + (void) ap_update_child_status_from_indexes(child_slot, 0, SERVER_DEAD,
2284 + (request_rec *) NULL);
2285 + _TRACE_RET("ap_update_child_status_from_indexes", 0);
2287 + if (processed_status == APEXIT_CHILDSICK) {
2288 + /* child detected a resource shortage (E[NM]FILE, ENOBUFS, etc)
2289 + * cut the fork rate to the minimum
2291 + _DBG("processed_status = APEXIT_CHILDSICK", 0);
2292 + idle_spawn_rate = 1;
2294 + else if (CHILD_INFO_TABLE[child_slot].status == CHILD_STATUS_STANDBY) {
2295 + _DBG("leaving child in standby state", 0);
2297 + else if (child_slot < ap_daemons_limit &&
2298 + CHILD_INFO_TABLE[child_slot].type !=
2299 + CHILD_TYPE_UNKNOWN) {
2300 + /* we're still doing a 1-for-1 replacement of dead
2301 + * children with new children
2303 + _DBG("replacing by new child ...", 0);
2304 + make_child(ap_server_conf, child_slot);
2306 +#if APR_HAS_OTHER_CHILD
2308 + else if (apr_proc_other_child_alert(&pid, APR_OC_REASON_DEATH, status) == 0) {
2309 + _DBG("APR_HAS_OTHER_CHILD, apparently", 0);
2313 + else if (is_graceful) {
2314 + /* Great, we've probably just lost a slot in the
2315 + * scoreboard. Somehow we don't know about this
2318 + _DBG("long lost child came home, whatever that means", 0);
2320 + ap_log_error(APLOG_MARK, APLOG_WARNING,
2321 + 0, ap_server_conf,
2322 + "long lost child came home! (pid %ld)", (long)pid.pid);
2324 + /* Don't perform idle maintenance when a child dies,
2325 + * only do it when there's a timeout. Remember only a
2326 + * finite number of children can die, and it's pretty
2327 + * pathological for a lot to die suddenly.
2332 + perform_idle_server_maintenance(pconf);
2334 + shutdown_pending = os_check_server(tpf_server_name);
2335 + ap_check_signals();
2340 + mpm_state = AP_MPMQ_STOPPING;
2342 + if (shutdown_pending) {
2343 + /* Time to gracefully shut down:
2344 + * Kill child processes, tell them to call child_exit, etc...
2346 + if (unixd_killpg(getpgrp(), SIGTERM) < 0) {
2347 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGTERM");
2349 + ap_reclaim_child_processes(1); /* Start with SIGTERM */
2351 + /* cleanup pid file on normal shutdown */
2353 + const char *pidfile = NULL;
2354 + pidfile = ap_server_root_relative (pconf, ap_pid_fname);
2355 + if ( pidfile != NULL && unlink(pidfile) == 0)
2356 + ap_log_error(APLOG_MARK, APLOG_INFO,
2357 + 0, ap_server_conf,
2358 + "removed PID file %s (pid=%ld)",
2359 + pidfile, (long)getpid());
2362 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2363 + "caught SIGTERM, shutting down");
2367 + /* we've been told to restart */
2368 + apr_signal(SIGHUP, SIG_IGN);
2369 + if (one_process) {
2370 + /* not worth thinking about */
2374 + /* advance to the next generation */
2375 + /* XXX: we really need to make sure this new generation number isn't in
2376 + * use by any of the children.
2378 + ++ap_my_generation;
2379 + ap_scoreboard_image->global->running_generation = ap_my_generation;
2381 + if (is_graceful) {
2382 + char char_of_death = AP_PERUSER_CHAR_OF_DEATH;
2384 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2385 + "Graceful restart requested, doing restart");
2388 + /* kill off the idle ones */
2389 + ap_mpm_pod_killpg(pod, ap_max_daemons_limit);
2391 + /* This is mostly for debugging... so that we know what is still
2392 + * gracefully dealing with existing request. This will break
2393 + * in a very nasty way if we ever have the scoreboard totally
2394 + * file-based (no shared memory)
2396 + for (i = 0; i < ap_daemons_limit; ++i) {
2397 + if (ap_scoreboard_image->servers[i][0].status != SERVER_DEAD) {
2398 + ap_scoreboard_image->servers[i][0].status = SERVER_GRACEFUL;
2403 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
2404 + ap_server_conf, AP_SIG_GRACEFUL_STRING " received. "
2405 + "Doing graceful restart");
2407 + /* This is mostly for debugging... so that we know what is still
2408 + * gracefully dealing with existing request.
2411 + for (i = 0; i < NUM_CHILDS; ++i)
2413 + ((ap_child_table[i].pid) && (ap_child_table[i].status = SERVER_DYING));
2416 + /* give the children the signal to die */
2417 + for (i = 0; i < NUM_CHILDS;)
2419 + if ((rv = apr_file_write(pipe_of_death_out, &char_of_death, &one)) != APR_SUCCESS)
2421 + if (APR_STATUS_IS_EINTR(rv)) continue;
2422 + ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
2423 + "write pipe_of_death");
2428 + for (i = 0; i < NUM_SENV; i++)
2430 + close(SENV[i].input);
2431 + close(SENV[i].output);
2435 + /* Kill 'em off */
2436 + if (unixd_killpg(getpgrp(), SIGHUP) < 0) {
2437 + ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGHUP");
2439 + ap_reclaim_child_processes(0); /* Not when just starting up */
2440 + ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2441 + "SIGHUP received. Attempting to restart");
2447 +/* == allocate an private server config structure == */
2448 +static void *peruser_create_config(apr_pool_t *p, server_rec *s)
2450 + peruser_server_conf *c = (peruser_server_conf *)
2451 + apr_pcalloc(p, sizeof(peruser_server_conf));
2455 +/* This really should be a post_config hook, but the error log is already
2456 + * redirected by that point, so we need to do this in the open_logs phase.
2458 +static int peruser_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
2463 + ap_server_conf = s;
2465 + if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
2466 + ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0,
2467 + NULL, "no listening sockets available, shutting down");
2471 + ap_log_pid(pconf, ap_pid_fname);
2473 + if ((rv = ap_mpm_pod_open(pconf, &pod))) {
2474 + ap_log_error(APLOG_MARK, APLOG_CRIT|APLOG_STARTUP, rv, NULL,
2475 + "Could not open pipe-of-death.");
2479 + if ((rv = apr_file_pipe_create(&pipe_of_death_in, &pipe_of_death_out,
2480 + pconf)) != APR_SUCCESS) {
2481 + ap_log_error(APLOG_MARK, APLOG_ERR, rv,
2482 + (const server_rec*) ap_server_conf,
2483 + "apr_file_pipe_create (pipe_of_death)");
2486 + if ((rv = apr_file_pipe_timeout_set(pipe_of_death_in, 0)) != APR_SUCCESS) {
2487 + ap_log_error(APLOG_MARK, APLOG_ERR, rv,
2488 + (const server_rec*) ap_server_conf,
2489 + "apr_file_pipe_timeout_set (pipe_of_death)");
2496 +static int peruser_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
2498 + static int restart_num = 0;
2499 + int no_detach, debug, foreground, i;
2500 + int tmp_server_limit = DEFAULT_SERVER_LIMIT;
2501 + ap_directive_t *pdir;
2503 + apr_pool_t *global_pool;
2506 + mpm_state = AP_MPMQ_STARTING;
2508 + debug = ap_exists_config_define("DEBUG");
2511 + foreground = one_process = 1;
2516 + no_detach = ap_exists_config_define("NO_DETACH");
2517 + one_process = ap_exists_config_define("ONE_PROCESS");
2518 + foreground = ap_exists_config_define("FOREGROUND");
2521 + /* sigh, want this only the second time around */
2522 + if (restart_num++ == 1) {
2525 + if (!one_process && !foreground) {
2526 + rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
2527 + : APR_PROC_DETACH_DAEMONIZE);
2528 + if (rv != APR_SUCCESS) {
2529 + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2530 + "apr_proc_detach failed");
2531 + return HTTP_INTERNAL_SERVER_ERROR;
2535 + parent_pid = ap_my_pid = getpid();
2538 + unixd_pre_config(ptemp);
2539 + ap_listen_pre_config();
2540 + ap_min_processors = DEFAULT_MIN_PROCESSORS;
2541 + ap_min_free_processors = DEFAULT_MIN_FREE_PROCESSORS;
2542 + ap_max_processors = DEFAULT_MAX_PROCESSORS;
2543 + ap_daemons_limit = server_limit;
2544 + ap_pid_fname = DEFAULT_PIDLOG;
2545 + ap_lock_fname = DEFAULT_LOCKFILE;
2546 + ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
2547 + ap_extended_status = 1;
2548 +#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
2549 + ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
2552 + apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
2554 + /* we need to know ServerLimit and ThreadLimit before we start processing
2555 + * the tree because we need to already have allocated child_info_table
2557 + for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next)
2559 + if (!strcasecmp(pdir->directive, "ServerLimit"))
2561 + if (atoi(pdir->args) > tmp_server_limit)
2563 + tmp_server_limit = atoi(pdir->args);
2564 + if (tmp_server_limit > MAX_SERVER_LIMIT)
2566 + tmp_server_limit = MAX_SERVER_LIMIT;
2572 + /* We don't want to have to recreate the scoreboard after
2573 + * restarts, so we'll create a global pool and never clean it.
2575 + rv = apr_pool_create(&global_pool, NULL);
2576 + if (rv != APR_SUCCESS) {
2577 + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2578 + "Fatal error: unable to create global pool");
2583 + _DBG("Initializing child_info_table", 0);
2584 + child_info_size = tmp_server_limit * sizeof(child_info_t) + sizeof(apr_size_t);
2586 + rv = apr_shm_create(&child_info_shm, child_info_size, NULL, global_pool);
2588 +/* if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) { */
2589 + if (rv != APR_SUCCESS) {
2590 + _DBG("shared memory creation failed", 0);
2592 + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2593 + "Unable to create shared memory segment "
2594 + "(anonymous shared memory failure)");
2596 + else if (rv == APR_ENOTIMPL) {
2597 + _DBG("anonymous shared memory not available", 0);
2598 + /* TODO: make up a filename and do name-based shmem */
2601 + if (rv || !(shmem = apr_shm_baseaddr_get(child_info_shm))) {
2602 + _DBG("apr_shm_baseaddr_get() failed", 0);
2603 + return HTTP_INTERNAL_SERVER_ERROR;
2606 + memset(shmem, 0, sizeof(child_info_size));
2607 + child_info_image = (child_info*)calloc(1, sizeof(child_info_size));
2608 + child_info_image->control = (child_info_control*)shmem;
2609 + shmem += sizeof(child_info_control*);
2610 + child_info_image->table = (child_info_t*)shmem;
2612 + child_info_image->control->num = 0;
2614 + for (i = 0; i < tmp_server_limit; i++)
2616 + CHILD_INFO_TABLE[i].pid = 0;
2617 + CHILD_INFO_TABLE[i].senv = (server_env_t*)NULL;
2618 + CHILD_INFO_TABLE[i].type = CHILD_TYPE_UNKNOWN;
2619 + CHILD_INFO_TABLE[i].status = CHILD_STATUS_STANDBY;
2620 + CHILD_INFO_TABLE[i].sock_fd = -3; /* -1 and -2 are taken */
2621 + CHILD_INFO_TABLE[i].id = i;
2624 + _DBG("Initializing server_environments_table", 0);
2625 + server_env_size = tmp_server_limit * sizeof(server_env_t) + sizeof(apr_size_t);
2627 + rv = apr_shm_create(&server_env_shm, server_env_size, NULL, global_pool);
2629 + if (rv != APR_SUCCESS) {
2630 + _DBG("shared memory creation failed", 0);
2632 + ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2633 + "Unable to create shared memory segment "
2634 + "(anonymous shared memory failure)");
2636 + else if (rv == APR_ENOTIMPL) {
2637 + _DBG("anonymous shared memory not available", 0);
2638 + /* TODO: make up a filename and do name-based shmem */
2641 + if (rv || !(shmem = apr_shm_baseaddr_get(server_env_shm))) {
2642 + _DBG("apr_shm_baseaddr_get() failed", 0);
2643 + return HTTP_INTERNAL_SERVER_ERROR;
2646 + memset(shmem, 0, sizeof(server_env_size));
2647 + server_env_image = (server_env*)calloc(1, sizeof(server_env_size));
2648 + server_env_image->control = (server_env_control*)shmem;
2649 + shmem += sizeof(server_env_control*);
2650 + server_env_image->table = (server_env_t*)shmem;
2652 + server_env_image->control->num = 0;
2654 + for (i = 0; i < tmp_server_limit; i++)
2656 + SENV[i].processor_id = -1;
2659 + SENV[i].chroot = NULL;
2660 + SENV[i].input = -1;
2661 + SENV[i].output = -1;
2667 +static int peruser_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *server_list)
2669 + ap_child_table = (ap_ctable *)apr_pcalloc(p, server_limit * sizeof(ap_ctable));
2674 +static int peruser_post_read(request_rec *r)
2676 + peruser_server_conf *sconf = PERUSER_SERVER_CONF(r->server->module_config);
2677 + child_info_t *processor;
2679 + if(CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_MULTIPLEXER)
2680 + processor = &CHILD_INFO_TABLE[sconf->senv->processor_id];
2682 + processor = &CHILD_INFO_TABLE[r->connection->id];
2685 + if (!strlen(r->the_request))
2687 + _DBG("corrupt request. aborting",0);
2691 + if (processor->sock_fd != AP_PERUSER_THISCHILD)
2693 + apr_socket_t *sock = NULL;
2695 + apr_os_sock_put(&sock, &processor->sock_fd, r->connection->pool);
2696 + ap_sock_disable_nagle(sock);
2697 + ap_set_module_config(r->connection->conn_config, &core_module, sock);
2698 + _DBG("not the right socket?", 0);
2702 + switch (CHILD_INFO_TABLE[my_child_num].type)
2704 + case CHILD_TYPE_MULTIPLEXER:
2706 + _DBG("MULTIPLEXER => Determining if request should be passed. "
2707 + "Child Num: %d, dest-child: %d, hostname from server: %s r->hostname=%s r->the_request=\"%s\"",
2708 + my_child_num, processor->id, r->server->server_hostname, r->hostname, r->the_request);
2710 + if (processor->id != my_child_num)
2712 + if (processor->status == CHILD_STATUS_STANDBY)
2714 + _DBG("Activating child #%d", processor->id);
2715 + processor->status = CHILD_STATUS_STARTING;
2718 + _DBG("Passing request.",0);
2719 + if (pass_request(r, processor) == -1)
2721 + ap_log_error(APLOG_MARK, APLOG_ERR, 0,
2722 + ap_server_conf, "Could not pass request to proper " "child, request will not be honoured.");
2725 + _DBG("doing longjmp",0);
2726 + longjmp(CHILD_INFO_TABLE[my_child_num].jmpbuffer, 1);
2727 + _DBG("request declined at our site",0);
2729 + _DBG("OUH! we should never reach this point",0);
2731 + _DBG("WTF: the server is assigned to the multiplexer! ... dropping request",0);
2734 + case CHILD_TYPE_PROCESSOR:
2735 + case CHILD_TYPE_WORKER:
2737 + _DBG("%s %d", child_type_string(CHILD_INFO_TABLE[my_child_num].type), my_child_num);
2738 + _DBG("request for %s / (server %s) seems to be for us", r->hostname, r->server->server_hostname);
2744 + _DBG("unspecified child type %d in %d, dropping request",
2745 + CHILD_INFO_TABLE[my_child_num].type, my_child_num);
2750 + _DBG("THIS POINT SHOULD NOT BE REACHED!",0);
2754 +static void peruser_hooks(apr_pool_t *p)
2756 + /* The peruser open_logs phase must run before the core's, or stderr
2757 + * will be redirected to a file, and the messages won't print to the
2760 + static const char *const aszSucc[] = {"core.c", NULL};
2763 + (void) set42sig();
2766 + ap_hook_open_logs(peruser_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
2767 + ap_hook_pre_config(peruser_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
2768 + ap_hook_post_config(peruser_post_config, NULL, NULL, APR_HOOK_MIDDLE);
2770 + /* Both of these must be run absolutely first. If this request isn't for
2771 + * this server then we need to forward it to the proper child. No sense
2772 + * tying up this server running more post_read request hooks if it is
2773 + * just going to be forwarded along. The process_connection hook allows
2774 + * peruser to receive the passed request correctly, by automatically
2775 + * filling in the core_input_filter's ctx pointer.
2777 + ap_hook_post_read_request(peruser_post_read, NULL, NULL,
2778 + APR_HOOK_REALLY_FIRST);
2779 + ap_hook_process_connection(peruser_process_connection, NULL, NULL,
2780 + APR_HOOK_REALLY_FIRST);
2783 +/* we define an Processor w/ specific uid/gid */
2784 +static const char *cf_Processor(cmd_parms *cmd, void *dummy,
2785 + const char *user_name, const char *group_name, const char *chroot)
2787 + uid_t uid = ap_uname2id(user_name);
2788 + gid_t gid = ap_gname2id(group_name);
2790 + _DBG("user=%s:%d group=%s:%d chroot=%s",
2791 + user_name, uid, group_name, gid, chroot);
2793 + return child_add(CHILD_TYPE_PROCESSOR, CHILD_STATUS_STANDBY,
2794 + uid, gid, chroot);
2797 +/* we define an Multiplexer child w/ specific uid/gid */
2798 +static const char *cf_Multiplexer(cmd_parms *cmd, void *dummy,
2799 + const char *user_name, const char *group_name, const char *chroot)
2801 + uid_t uid = ap_uname2id(user_name);
2802 + gid_t gid = ap_gname2id(group_name);
2804 + _DBG("user=%s:%d group=%s:%d chroot=%s [multiplexer id %d]",
2805 + user_name, uid, group_name, gid, chroot, NUM_CHILDS);
2807 + return child_add(CHILD_TYPE_MULTIPLEXER, CHILD_STATUS_STARTING,
2808 + uid, gid, chroot);
2811 +static const char* cf_ServerEnvironment(cmd_parms *cmd, void *dummy,
2812 + const char *user_name, const char *group_name, const char *chroot)
2814 + int uid = ap_uname2id(user_name);
2815 + int gid = ap_gname2id(group_name);
2816 + peruser_server_conf *sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2818 + _DBG("function entered", 0);
2820 + sconf->senv = senv_add(uid, gid, chroot);
2822 + _DBG("user=%s:%d group=%s:%d chroot=%s numchilds=%d",
2823 + user_name, uid, group_name, gid, chroot, NUM_CHILDS);
2828 +static const char *set_min_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
2830 + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2831 + if (err != NULL) {
2835 + ap_min_free_processors = atoi(arg);
2836 + if (ap_min_free_processors <= 0) {
2837 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2838 + "WARNING: detected MinSpareServers set to non-positive.");
2839 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2840 + "Resetting to 1 to avoid almost certain Apache failure.");
2841 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2842 + "Please read the documentation.");
2843 + ap_min_free_processors = 1;
2849 +static const char *set_max_clients (cmd_parms *cmd, void *dummy, const char *arg)
2851 + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2852 + if (err != NULL) {
2856 + ap_daemons_limit = atoi(arg);
2857 + if (ap_daemons_limit > server_limit) {
2858 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2859 + "WARNING: MaxClients of %d exceeds ServerLimit value "
2860 + "of %d servers,", ap_daemons_limit, server_limit);
2861 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2862 + " lowering MaxClients to %d. To increase, please "
2863 + "see the ServerLimit", server_limit);
2864 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2866 + ap_daemons_limit = server_limit;
2868 + else if (ap_daemons_limit < 1) {
2869 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2870 + "WARNING: Require MaxClients > 0, setting to 1");
2871 + ap_daemons_limit = 1;
2876 +static const char *set_min_processors (cmd_parms *cmd, void *dummy, const char *arg)
2878 + peruser_server_conf *sconf;
2880 + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2882 + if (err != NULL) {
2886 + min_procs = atoi(arg);
2888 + if (min_procs < 1) {
2889 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2890 + "WARNING: Require MaxProcessors > 0, setting to 1");
2894 + if (ap_check_cmd_context(cmd, NOT_IN_VIRTUALHOST) != NULL) {
2895 + sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2896 + sconf->senv->min_processors = min_procs;
2899 + ap_min_processors = min_procs;
2905 +static const char *set_min_free_processors (cmd_parms *cmd, void *dummy, const char *arg)
2907 + peruser_server_conf *sconf;
2908 + int min_free_procs;
2909 + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2911 + if (err != NULL) {
2915 + min_free_procs = atoi(arg);
2917 + if (min_free_procs < 1) {
2918 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2919 + "WARNING: Require MinSpareProcessors > 0, setting to 1");
2920 + min_free_procs = 1;
2923 + if (ap_check_cmd_context(cmd, NOT_IN_VIRTUALHOST) != NULL) {
2924 + sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2925 + sconf->senv->min_free_processors = min_free_procs;
2928 + ap_min_free_processors = min_free_procs;
2934 +static const char *set_max_processors (cmd_parms *cmd, void *dummy, const char *arg)
2936 + peruser_server_conf *sconf;
2938 + const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2940 + if (err != NULL) {
2944 + max_procs = atoi(arg);
2946 + if (max_procs < 1) {
2947 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2948 + "WARNING: Require MaxProcessors > 0, setting to 1");
2952 + if (ap_check_cmd_context(cmd, NOT_IN_VIRTUALHOST) != NULL) {
2953 + sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2954 + sconf->senv->max_processors = max_procs;
2957 + ap_max_processors = max_procs;
2963 +static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg)
2965 + int tmp_server_limit;
2967 + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2968 + if (err != NULL) {
2972 + tmp_server_limit = atoi(arg);
2973 + /* you cannot change ServerLimit across a restart; ignore
2974 + * any such attempts
2976 + if (first_server_limit &&
2977 + tmp_server_limit != server_limit) {
2978 + /* how do we log a message? the error log is a bit bucket at this
2979 + * point; we'll just have to set a flag so that ap_mpm_run()
2980 + * logs a warning later
2982 + changed_limit_at_restart = 1;
2985 + server_limit = tmp_server_limit;
2987 + if (server_limit > MAX_SERVER_LIMIT) {
2988 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2989 + "WARNING: ServerLimit of %d exceeds compile time limit "
2990 + "of %d servers,", server_limit, MAX_SERVER_LIMIT);
2991 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2992 + " lowering ServerLimit to %d.", MAX_SERVER_LIMIT);
2993 + server_limit = MAX_SERVER_LIMIT;
2995 + else if (server_limit < 1) {
2996 + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2997 + "WARNING: Require ServerLimit > 0, setting to 1");
3003 +static const char *set_expire_timeout (cmd_parms *cmd, void *dummy, const char *arg) {
3004 + const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3005 + if (err != NULL) {
3009 + expire_timeout = atoi(arg);
3014 +static const command_rec peruser_cmds[] = {
3015 +UNIX_DAEMON_COMMANDS,
3017 +AP_INIT_TAKE1("MinSpareProcessors", set_min_free_processors, NULL, RSRC_CONF,
3018 + "Minimum number of idle children, to handle request spikes"),
3019 +AP_INIT_TAKE1("MinSpareServers", set_min_free_servers, NULL, RSRC_CONF,
3020 + "Minimum number of idle children, to handle request spikes"),
3021 +AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
3022 + "Maximum number of children alive at the same time"),
3023 +AP_INIT_TAKE1("MinProcessors", set_min_processors, NULL, RSRC_CONF,
3024 + "Minimum number of processors per vhost"),
3025 +AP_INIT_TAKE1("MaxProcessors", set_max_processors, NULL, RSRC_CONF,
3026 + "Maximum number of processors per vhost"),
3027 +AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
3028 + "Maximum value of MaxClients for this run of Apache"),
3029 +AP_INIT_TAKE1("ExpireTimeout", set_expire_timeout, NULL, RSRC_CONF,
3030 + "Maximum idle time before a child is killed, 0 to disable"),
3031 +AP_INIT_TAKE23("Multiplexer", cf_Multiplexer, NULL, RSRC_CONF,
3032 + "Specify an Multiplexer Child configuration."),
3033 +AP_INIT_TAKE23("Processor", cf_Processor, NULL, RSRC_CONF,
3034 + "Specify a User and Group for a specific child process."),
3035 +AP_INIT_TAKE23("ServerEnvironment", cf_ServerEnvironment, NULL, RSRC_CONF,
3036 + "Specify the server environment for this virtual host."),
3040 +module AP_MODULE_DECLARE_DATA mpm_peruser_module = {
3041 + MPM20_MODULE_STUFF,
3042 + ap_mpm_rewrite_args, /* hook to run before apache parses args */
3043 + NULL, /* create per-directory config structure */
3044 + NULL, /* merge per-directory config structures */
3045 + peruser_create_config, /* create per-server config structure */
3046 + NULL, /* merge per-server config structures */
3047 + peruser_cmds, /* command apr_table_t */
3048 + peruser_hooks, /* register hooks */