]> git.pld-linux.org Git - packages/apache.git/blob - httpd-2.0.52-peruser-0.1.6.patch
- main package doesn't provide mod_autoindex
[packages/apache.git] / httpd-2.0.52-peruser-0.1.6.patch
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
4 @@ -1,7 +1,7 @@
5  AC_MSG_CHECKING(which MPM to use)
6  AC_ARG_WITH(mpm,
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}),[
10    APACHE_MPM=$withval
11  ],[
12    if test "x$APACHE_MPM" = "x"; then
13 @@ -26,7 +26,7 @@
14  APACHE_FAST_OUTPUT(server/mpm/Makefile)
15  
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
21  else
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
25 @@ -0,0 +1,5 @@
26 +
27 +LTLIBRARY_NAME    = libperuser.la
28 +LTLIBRARY_SOURCES = peruser.c
29 +
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
34 @@ -0,0 +1,3 @@
35 +if test "$MPM_NAME" = "peruser" ; then
36 +    APACHE_FAST_OUTPUT(server/mpm/experimental/$MPM_NAME/Makefile)
37 +fi
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
41 @@ -0,0 +1,103 @@
42 +/* ====================================================================
43 + * The Apache Software License, Version 1.1
44 + *
45 + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
46 + * reserved.
47 + *
48 + * Redistribution and use in source and binary forms, with or without
49 + * modification, are permitted provided that the following conditions
50 + * are met:
51 + *
52 + * 1. Redistributions of source code must retain the above copyright
53 + *    notice, this list of conditions and the following disclaimer.
54 + *
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
58 + *    distribution.
59 + *
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.
66 + *
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.
71 + *
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.
75 + *
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
87 + * SUCH DAMAGE.
88 + * ====================================================================
89 + *
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/>.
94 + *
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.
98 + */
99 +
100 +#include "httpd.h"
101 +#include "mpm_default.h"
102 +#include "scoreboard.h"
103 +#include "unixd.h"
104 +
105 +#ifndef APACHE_MPM_PERUSER_H
106 +#define APACHE_MPM_PERUSER_H
107 +
108 +#define PERUSER_MPM
109 +
110 +#define MPM_NAME "Peruser"
111 +
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
124 +
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
129 +
130 +extern int ap_threads_per_child;
131 +extern int ap_max_daemons_limit;
132 +extern server_rec *ap_server_conf;
133 +
134 +/* Table of child status */
135 +#define SERVER_DEAD 0
136 +#define SERVER_DYING 1
137 +#define SERVER_ALIVE 2
138 +
139 +typedef struct ap_ctable {
140 +    pid_t pid;
141 +    unsigned char status;
142 +} ap_ctable;
143 +
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
148 @@ -0,0 +1,110 @@
149 +/* ====================================================================
150 + * The Apache Software License, Version 1.1
151 + *
152 + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
153 + * reserved.
154 + *
155 + * Redistribution and use in source and binary forms, with or without
156 + * modification, are permitted provided that the following conditions
157 + * are met:
158 + *
159 + * 1. Redistributions of source code must retain the above copyright
160 + *    notice, this list of conditions and the following disclaimer.
161 + *
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
165 + *    distribution.
166 + *
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.
173 + *
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.
178 + *
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.
182 + *
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
194 + * SUCH DAMAGE.
195 + * ====================================================================
196 + *
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/>.
201 + *
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.
205 + */
206 +
207 +#ifndef APACHE_MPM_DEFAULT_H
208 +#define APACHE_MPM_DEFAULT_H
209 +
210 +/* Number of processors to spawn off for each ServerEnvironment by default */
211 +
212 +#ifndef DEFAULT_START_PROCESSORS
213 +#define DEFAULT_START_PROCESSORS 0
214 +#endif
215 +
216 +/* Minimum number of running processors per ServerEnvironment */
217 +
218 +#ifndef DEFAULT_MIN_PROCESSORS
219 +#define DEFAULT_MIN_PROCESSORS 0
220 +#endif
221 +
222 +/* Minimum --- fewer than this, and more will be created */
223 +
224 +#ifndef DEFAULT_MIN_FREE_PROCESSORS
225 +#define DEFAULT_MIN_FREE_PROCESSORS 2
226 +#endif
227 +
228 +/* Maximum processors per ServerEnvironment */
229 +
230 +#ifndef DEFAULT_MAX_PROCESSORS
231 +#define DEFAULT_MAX_PROCESSORS 10
232 +#endif
233 +
234 +/* File used for accept locking, when we use a file */
235 +#ifndef DEFAULT_LOCKFILE
236 +#define DEFAULT_LOCKFILE DEFAULT_REL_RUNTIMEDIR "/accept.lock"
237 +#endif
238 +
239 +/* Where the main/parent process's pid is logged */
240 +#ifndef DEFAULT_PIDLOG
241 +#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
242 +#endif
243 +
244 +/*
245 + * Interval, in microseconds, between scoreboard maintenance.
246 + */
247 +#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
248 +#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
249 +#endif
250 +
251 +/* Number of requests to try to handle in a single process.  If <= 0,
252 + * the children don't die off.
253 + */
254 +#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
255 +#define DEFAULT_MAX_REQUESTS_PER_CHILD 10000
256 +#endif
257 +
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
262 @@ -0,0 +1,2796 @@
263 +/* ====================================================================
264 + * The Apache Software License, Version 1.1
265 + *
266 + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
267 + * reserved.
268 + *
269 + * Redistribution and use in source and binary forms, with or without
270 + * modification, are permitted provided that the following conditions
271 + * are met:
272 + *
273 + * 1. Redistributions of source code must retain the above copyright
274 + *    notice, this list of conditions and the following disclaimer.
275 + *
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
279 + *    distribution.
280 + *
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.
287 + *
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.
292 + *
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.
296 + *
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
308 + * SUCH DAMAGE.
309 + * ====================================================================
310 + *
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/>.
315 + *
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.
319 + */
320 +
321 +/* #define MPM_PERUSER_DEBUG */
322 +
323 +#include "apr.h"
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"
331 +
332 +#define APR_WANT_STDIO
333 +#define APR_WANT_STRFUNC
334 +#define APR_WANT_IOVEC
335 +#include "apr_want.h"
336 +
337 +#if APR_HAVE_UNISTD_H
338 +#include <unistd.h>
339 +#endif
340 +#if APR_HAVE_SYS_TYPES_H
341 +#include <sys/types.h>
342 +#endif
343 +
344 +#define CORE_PRIVATE
345 +
346 +#include "ap_config.h"
347 +#include "httpd.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"
356 +#include "ap_mpm.h"
357 +#include "unixd.h"
358 +#include "mpm_common.h"
359 +#include "ap_listen.h"
360 +#include "ap_mmn.h"
361 +#include "apr_poll.h"
362 +#include "util_ebcdic.h"
363 +
364 +#ifdef HAVE_BSTRING_H
365 +#include <bstring.h>           /* for IRIX, FD_SET calls bzero() */
366 +#endif
367 +
368 +#ifdef HAVE_TIME_H
369 +#include <time.h>
370 +#endif
371 +
372 +#ifdef HAVE_SYS_PROCESSOR_H
373 +#include <sys/processor.h> /* for bindprocessor() */
374 +#endif
375 +
376 +#if APR_HAS_SHARED_MEMORY
377 +#include "apr_shm.h"
378 +#else
379 +#error "Peruser MPM requres shared memory support."
380 +#endif
381 +
382 +
383 +/* should be APR-ized */
384 +#include <grp.h>
385 +#include <pwd.h>
386 +#include <sys/stat.h>
387 +#include <sys/un.h>
388 +#include <setjmp.h>
389 +
390 +#include <signal.h>
391 +#include <sys/times.h>
392 +
393 +
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)
399 +
400 +# define _TRACE_CALL(text,par...) _DBG("calling " text, ##par)
401 +# define _TRACE_RET(text,par...) _DBG("returned from " text, ##par)
402 +#else
403 +# define _DBG(text,par...)
404 +# define _TRACE_RET(text,par...)
405 +# define _TRACE_CALL(text,par...)
406 +#endif /* MPM_PERUSER_DEBUG */
407 +
408 +/* char of death - for signalling children to die */
409 +#define AP_PERUSER_CHAR_OF_DEATH        '!'
410 +
411 +#define PERUSER_SERVER_CONF(cf)        \
412 +    ((peruser_server_conf *) ap_get_module_config(cf, &mpm_peruser_module))
413 +
414 +#define SCOREBOARD_STATUS(i)   ap_scoreboard_image->servers[i][0].status
415 +
416 +/*
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.
419 + */
420 +#define AP_PERUSER_THISCHILD -1
421 +#define AP_PERUSER_OTHERCHILD -2
422 +
423 +
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.
427 + *
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
433 + * the overhead.
434 + */
435 +#ifndef DEFAULT_SERVER_LIMIT
436 +#define DEFAULT_SERVER_LIMIT 256
437 +#endif
438 +
439 +/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT.  We want
440 + * some sort of compile-time limit to help catch typos.
441 + */
442 +#ifndef MAX_SERVER_LIMIT
443 +#define MAX_SERVER_LIMIT 20000
444 +#endif
445 +
446 +#ifndef HARD_THREAD_LIMIT
447 +#define HARD_THREAD_LIMIT 1
448 +#endif
449 +
450 +#define CHILD_TYPE_UNKNOWN      0
451 +#define CHILD_TYPE_MULTIPLEXER  1
452 +#define CHILD_TYPE_PROCESSOR    2
453 +#define CHILD_TYPE_WORKER       3
454 +
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 */
460 +
461 +/* config globals */
462 +
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;
476 +
477 +/* === configuration stuff === */
478 +
479 +typedef struct
480 +{
481 +    int processor_id;
482 +
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 */
487 +
488 +    /* resource settings */
489 +    int min_processors;
490 +    int min_free_processors;
491 +    int max_processors;
492 +
493 +    /* sockets */
494 +    int input;          /* The socket descriptor */
495 +    int output;         /* The socket descriptor */
496 +} server_env_t;
497 +
498 +typedef struct
499 +{
500 +    apr_size_t num;
501 +} server_env_control;
502 +
503 +typedef struct
504 +{
505 +    server_env_control *control;
506 +    server_env_t *table;
507 +} server_env;
508 +
509 +
510 +typedef struct
511 +{
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;
518 +
519 +    /* sockets */
520 +    int sock_fd;
521 +
522 +    /* stack context saved state */
523 +    jmp_buf jmpbuffer;
524 +} child_info_t;
525 +
526 +typedef struct
527 +{
528 +    apr_size_t num;
529 +} child_info_control;
530 +
531 +typedef struct
532 +{
533 +    child_info_control *control;
534 +    child_info_t *table;
535 +} child_info;
536 +
537 +
538 +typedef struct
539 +{
540 +    server_env_t *senv;
541 +} peruser_server_conf;
542 +
543 +
544 +typedef struct peruser_header
545 +{
546 +    char *headers;
547 +    apr_pool_t *p;
548 +} peruser_header;
549 +
550 +
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
553 + * process.
554 + */
555 +static apr_size_t child_info_size;
556 +static child_info *child_info_image;
557 +struct ap_ctable *ap_child_table;
558 +
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)
561 +
562 +static apr_size_t server_env_size;
563 +static server_env *server_env_image;
564 +
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)
567 +
568 +#if APR_HAS_SHARED_MEMORY
569 +#ifndef WIN32
570 +static /* but must be exported to mpm_winnt */
571 +#endif
572 +        apr_shm_t *child_info_shm = NULL;
573 +        apr_shm_t *server_env_shm = NULL;
574 +#endif
575 +
576 +/*
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.
580 + */
581 +int ap_max_daemons_limit = -1;
582 +server_rec *ap_server_conf;
583 +
584 +module AP_MODULE_DECLARE_DATA mpm_peruser_module;
585 +
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;
589 +
590 +
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.).
600 + */
601 +
602 +static int one_process = 0;
603 +
604 +static apr_pool_t *pconf;              /* Pool for config stuff */
605 +static apr_pool_t *pchild;             /* Pool for httpd child stuff */
606 +
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;
611 +
612 +#ifdef TPF
613 +int tpf_child = 0;
614 +char tpf_server_name[INETD_SERVNAME_LENGTH+1];
615 +#endif /* TPF */
616 +
617 +static int die_now = 0;
618 +
619 +#ifdef GPROF
620 +/* 
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
625 + */
626 +static void chdir_for_gprof(void)
627 +{
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;
632 +
633 +    if(dir) {
634 +        apr_status_t res;
635 +       char buf[512];
636 +       int len = strlen(sconf->gprof_dir) - 1;
637 +       if(*(dir + len) == '%') {
638 +           dir[len] = '\0';
639 +           apr_snprintf(buf, sizeof(buf), "%sgprof.%d", dir, (int)getpid());
640 +       } 
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);
646 +       }
647 +    }
648 +    else {
649 +       use_dir = ap_server_root_relative(pconf, DEFAULT_REL_RUNTIMEDIR);
650 +    }
651 +
652 +    chdir(use_dir);
653 +}
654 +#else
655 +#define chdir_for_gprof()
656 +#endif
657 +
658 +char* child_type_string(int type)
659 +{
660 +    switch(type)
661 +    {
662 +        case CHILD_TYPE_MULTIPLEXER: return "MULTIPLEXER";
663 +        case CHILD_TYPE_PROCESSOR:   return "PROCESSOR";
664 +        case CHILD_TYPE_WORKER:      return "WORKER";
665 +    }
666 +
667 +    return "UNKNOWN";
668 +}
669 +
670 +char* child_status_string(int status)
671 +{
672 +    switch(status)
673 +    {
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";
679 +    }
680 +
681 +    return "UNKNOWN";
682 +}
683 +
684 +void dump_child_table()
685 +{
686 +  int x;
687 +  server_env_t *senv;
688 +
689 +  _DBG("%-3s %-5s %-8s %-12s %-4s %-4s %-25s %5s %6s %7s",
690 +    "ID", "PID", "STATUS", "TYPE", "UID", "GID", "CHROOT", "INPUT", "OUTPUT", "SOCK_FD");
691 +
692 +  for(x = 0; x < NUM_CHILDS; x++)
693 +  {
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);
706 +  }
707 +}
708 +
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() 
712 +
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)
716 +{
717 +    int retval;
718 +
719 +    mpm_state = AP_MPMQ_STOPPING;
720 +
721 +    if (CHILD_INFO_TABLE[my_child_num].type != CHILD_TYPE_MULTIPLEXER &&
722 +        CHILD_INFO_TABLE[my_child_num].senv)
723 +    {
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);
727 +
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);
731 +    }
732 +
733 +    if (pchild) {
734 +       apr_pool_destroy(pchild);
735 +    }
736 +    ap_mpm_pod_close(pod);
737 +    chdir_for_gprof();
738 +    exit(code);
739 +}
740 +
741 +static void accept_mutex_on(void)
742 +{
743 +/* for some reason this fails if we listen on the pipe_of_death.
744 +   fortunately I don't think we currently need it */
745 +
746 +#if 0
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";
750 +
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);
755 +        }
756 +        else {
757 +            ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, msg);
758 +            exit(APEXIT_CHILDFATAL);
759 +        }
760 +    }
761 +#endif
762 +}
763 +
764 +static void accept_mutex_off(void)
765 +{
766 +#if 0
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";
770 +
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
777 +             */
778 +        }
779 +        else {
780 +            ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, msg);
781 +            exit(APEXIT_CHILDFATAL);
782 +        }
783 +    }
784 +#endif
785 +}
786 +
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.
791 + */
792 +#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
793 +#define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)
794 +#else
795 +#define SAFE_ACCEPT(stmt) do {stmt;} while(0)
796 +#endif
797 +
798 +AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
799 +{
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:
817 +            *result = 0;
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:
823 +            *result = 0;
824 +            return APR_SUCCESS;
825 +        case AP_MPMQ_MAX_SPARE_THREADS:
826 +            *result = 0;
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;
837 +    }
838 +    return APR_ENOTIMPL;
839 +}
840 +
841 +#if defined(NEED_WAITPID)
842 +/*
843 +   Systems without a real waitpid sometimes lose a child's exit while waiting
844 +   for another.  Search through the scoreboard for missing children.
845 + */
846 +int reap_children(int *exitcode, apr_exit_why_e *status)
847 +{
848 +    int n, pid;
849 +
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;
856 +            *exitcode = 0;
857 +           return(pid);
858 +       }
859 +    }
860 +    return 0;
861 +}
862 +#endif
863 +
864 +/* handle all varieties of core dumping signals */
865 +static void sig_coredump(int sig)
866 +{
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,
871 +                         0, ap_server_conf,
872 +                         "seg fault or similar nasty error detected "
873 +                         "in the parent process");
874 +    }
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.
882 +     */
883 +}
884 +
885 +/*****************************************************************
886 + * Connection structures and accounting...
887 + */
888 +
889 +static void just_die(int sig)
890 +{
891 +_DBG("function called");
892 +    clean_child_exit(0);
893 +}
894 +
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; */
900 +
901 +static void sig_term(int sig)
902 +{
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.
907 +        */
908 +       return;
909 +    }
910 +    shutdown_pending = 1;
911 +}
912 +
913 +/* restart() is the signal handler for SIGHUP and AP_SIG_GRACEFUL
914 + * in the parent process, unless running in ONE_PROCESS mode
915 + */
916 +static void restart(int sig)
917 +{
918 +    if (restart_pending == 1) {
919 +       /* Probably not an error - don't bother reporting it */
920 +       return;
921 +    }
922 +    restart_pending = 1;
923 +    is_graceful = (sig == AP_SIG_GRACEFUL);
924 +}
925 +
926 +/* Sets die_now if we received a character on the pipe_of_death */
927 +static apr_status_t check_pipe_of_death
928 +(
929 +    void **csd,
930 +    ap_listen_rec *lr,
931 +    apr_pool_t *ptrans
932 +)
933 +{
934 +    int ret;
935 +    char pipe_read_char;
936 +    apr_size_t n = 1;
937 +
938 +    _DBG("WATCH: die_now=%d", die_now);
939 +
940 +    if (die_now) return APR_SUCCESS;
941 +
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))
945 +    {
946 +            /* It lost the lottery. It must continue to suffer
947 +             * through a life of servitude. */
948 +    }
949 +    else
950 +    {
951 +       if (pipe_read_char != AP_PERUSER_CHAR_OF_DEATH)
952 +       {
953 +           _DBG("got wrong char %c", pipe_read_char);
954 +           return APR_SUCCESS;
955 +       }
956 +        /* It won the lottery (or something else is very
957 +         * wrong). Embrace death with open arms. */
958 +        die_now = 1;
959 +        _DBG("WATCH: die_now=%d", die_now);
960 +    }
961 +    /* apr_thread_mutex_unlock(pipe_of_death_mutex); */
962 +    return APR_SUCCESS;
963 +}
964 +
965 +static void set_signals(void)
966 +{
967 +#ifndef NO_USE_SIGACTION
968 +    struct sigaction sa;
969 +
970 +    sigemptyset(&sa.sa_mask);
971 +    sa.sa_flags = 0;
972 +
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;
979 +#endif
980 +       if (sigaction(SIGSEGV, &sa, NULL) < 0)
981 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
982 +#ifdef SIGBUS
983 +       if (sigaction(SIGBUS, &sa, NULL) < 0)
984 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
985 +#endif
986 +#ifdef SIGABORT
987 +       if (sigaction(SIGABORT, &sa, NULL) < 0)
988 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABORT)");
989 +#endif
990 +#ifdef SIGABRT
991 +       if (sigaction(SIGABRT, &sa, NULL) < 0)
992 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
993 +#endif
994 +#ifdef SIGILL
995 +       if (sigaction(SIGILL, &sa, NULL) < 0)
996 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
997 +#endif
998 +       sa.sa_flags = 0;
999 +    }
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)");
1003 +#ifdef SIGINT
1004 +    if (sigaction(SIGINT, &sa, NULL) < 0)
1005 +        ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
1006 +#endif
1007 +#ifdef SIGXCPU
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)");
1011 +#endif
1012 +#ifdef SIGXFSZ
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)");
1016 +#endif
1017 +#ifdef SIGPIPE
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)");
1021 +#endif
1022 +
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 ")");
1032 +#else
1033 +    if (!one_process) {
1034 +       apr_signal(SIGSEGV, sig_coredump);
1035 +#ifdef SIGBUS
1036 +       apr_signal(SIGBUS, sig_coredump);
1037 +#endif /* SIGBUS */
1038 +#ifdef SIGABORT
1039 +       apr_signal(SIGABORT, sig_coredump);
1040 +#endif /* SIGABORT */
1041 +#ifdef SIGABRT
1042 +       apr_signal(SIGABRT, sig_coredump);
1043 +#endif /* SIGABRT */
1044 +#ifdef SIGILL
1045 +       apr_signal(SIGILL, sig_coredump);
1046 +#endif /* SIGILL */
1047 +#ifdef SIGXCPU
1048 +       apr_signal(SIGXCPU, SIG_DFL);
1049 +#endif /* SIGXCPU */
1050 +#ifdef SIGXFSZ
1051 +       apr_signal(SIGXFSZ, SIG_DFL);
1052 +#endif /* SIGXFSZ */
1053 +    }
1054 +
1055 +    apr_signal(SIGTERM, sig_term);
1056 +#ifdef SIGHUP
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 */
1062 +#ifdef SIGPIPE
1063 +    apr_signal(SIGPIPE, SIG_IGN);
1064 +#endif /* SIGPIPE */
1065 +
1066 +#endif
1067 +}
1068 +
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.
1073 + */
1074 +
1075 +static int requests_this_child;
1076 +static int num_listensocks = 0;
1077 +static ap_listen_rec *listensocks;
1078 +
1079 +int ap_graceful_stop_signalled(void)
1080 +{
1081 +    /* not ever called anymore... */
1082 +    return 0;
1083 +}
1084 +
1085 +static void process_socket(apr_pool_t *p, apr_socket_t *sock, long conn_id,
1086 +                           apr_bucket_alloc_t *bucket_alloc)
1087 +{
1088 +    conn_rec *current_conn;
1089 +    int sock_fd;
1090 +    apr_status_t rv;
1091 +    ap_sb_handle_t *sbh;
1092 +
1093 +    if ((rv = apr_os_sock_get(&sock_fd, sock)) != APR_SUCCESS)
1094 +    {
1095 +        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "apr_os_sock_get");
1096 +    }
1097 +
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);
1100 +
1101 +    if (sock_fd >= FD_SETSIZE)
1102 +    {
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 "
1106 +                     "(currently %d)",
1107 +                     sock_fd, FD_SETSIZE);
1108 +        apr_socket_close(sock);
1109 +        _DBG("child_num=%d: exiting with error", my_child_num);
1110 +        return;
1111 +    }
1112 +
1113 +    if (CHILD_INFO_TABLE[my_child_num].sock_fd < 0)
1114 +    {
1115 +        ap_sock_disable_nagle(sock);
1116 +    }
1117 +
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);
1121 +
1122 +    if (current_conn)
1123 +    {
1124 +        ap_process_connection(current_conn, sock);
1125 +        ap_lingering_close(current_conn);
1126 +    }
1127 +}
1128 +
1129 +static int peruser_process_connection(conn_rec *conn)
1130 +{
1131 +    ap_filter_t *filter;
1132 +    apr_bucket_brigade *bb;
1133 +    core_net_rec *net;
1134 +
1135 +    _DBG("function entered",0);
1136 +
1137 +    /* -- fetch our sockets from the pool -- */
1138 +    apr_pool_userdata_get((void **)&bb, "PERUSER_SOCKETS", conn->pool);
1139 +    if (bb != NULL)
1140 +    {
1141 +        /* -- find the 'core' filter and give the socket data to it -- */
1142 +        for (filter = conn->output_filters; filter != NULL; filter = filter->next)
1143 +        {
1144 +            if (!strcmp(filter->frec->name, "core")) break;
1145 +        }
1146 +        if (filter != NULL)
1147 +        {
1148 +            net = filter->ctx;
1149 +            net->in_ctx = apr_palloc(conn->pool, sizeof(*net->in_ctx));
1150 +            net->in_ctx->b = bb;
1151 +
1152 +            net->in_ctx->tmpbb = apr_brigade_create(net->in_ctx->b->p, 
1153 +                net->in_ctx->b->bucket_alloc);
1154 +
1155 +        }
1156 +    }
1157 +    _DBG("leaving (DECLINED)", 0);
1158 +    return DECLINED;
1159 +}
1160 +
1161 +static int total_processors(int child_num)
1162 +{
1163 +    int i, total;
1164 +
1165 +    for(i = 0, total = 0; i < NUM_CHILDS; ++i)
1166 +    {
1167 +        if(CHILD_INFO_TABLE[i].senv == CHILD_INFO_TABLE[child_num].senv)
1168 +            total++;
1169 +    }
1170 +
1171 +    return total;
1172 +}
1173 +
1174 +static int idle_processors(int child_num)
1175 +{
1176 +    int i, total;
1177 +
1178 +    for(i = 0, total = 0; i < NUM_CHILDS; ++i)
1179 +    {
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))
1183 +        {
1184 +            total++;
1185 +        }
1186 +    }
1187 +
1188 +    return total;
1189 +}
1190 +
1191 +static int pass_request(request_rec *r, child_info_t *processor)
1192 +{
1193 +    int rv;
1194 +    struct msghdr msg;
1195 +    struct cmsghdr *cmsg;
1196 +    int sock_fd;
1197 +    char *body = "";
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;
1205 +    peruser_header h;
1206 +    apr_bucket *bucket;
1207 +    const apr_array_header_t *headers_in_array;
1208 +    const apr_table_entry_t *headers_in;
1209 +    int counter;
1210 +
1211 +    apr_socket_t *thesock = ap_get_module_config(r->connection->conn_config, &core_module);
1212 +
1213 +    if ((!r->the_request) || (!strlen(r->the_request)))
1214 +    {
1215 +        _DBG("empty request. dropping it (%ld)", r->the_request);
1216 +        return -1;
1217 +    }
1218 +
1219 +    if (!processor)
1220 +    {
1221 +        _DBG("server %s in child %d has no child_info associated",
1222 +                r->hostname, my_child_num);
1223 +        return -1;
1224 +    }
1225 +
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));
1229 +
1230 +    ap_get_brigade(r->connection->input_filters, bb, AP_MODE_EXHAUSTIVE, APR_NONBLOCK_READ, len);
1231 +
1232 +    /* Scan the brigade looking for heap-buckets */
1233 +
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);
1242 +            body_bb = bb;
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).
1247 +             */
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);
1252 +                return DECLINED;
1253 +            }
1254 +            _DBG("Brigade is flattened as body (body_len=%d)", body_len);
1255 +        }
1256 +    }
1257 +    _DBG("Scanning is finished",0);
1258 +
1259 +    apr_os_sock_get(&sock_fd, thesock);
1260 +
1261 +    h.p = r->pool;
1262 +
1263 +    headers_in_array = apr_table_elts(r->headers_in);
1264 +    headers_in = (const apr_table_entry_t *) headers_in_array->elts;
1265 +
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) {
1270 +             continue;
1271 +         }
1272 +         h.headers = apr_pstrcat(h.p, h.headers, headers_in[counter].key, ": ",
1273 +                                 headers_in[counter].val, CRLF, NULL);
1274 +
1275 +    }
1276 +    h.headers = apr_pstrcat(h.p, h.headers, CRLF, NULL);
1277 +    ap_xlate_proto_to_ascii(h.headers, strlen(h.headers));
1278 +
1279 +    header_len = strlen(h.headers);
1280 +
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;
1289 +
1290 +    msg.msg_name    = NULL;
1291 +    msg.msg_namelen = 0;
1292 +    msg.msg_iov     = iov;
1293 +    msg.msg_iovlen  = 4;
1294 +
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;
1299 +
1300 +    memcpy(CMSG_DATA(cmsg), &sock_fd, sizeof(sock_fd));
1301 +
1302 +    msg.msg_control    = cmsg;
1303 +    msg.msg_controllen = cmsg->cmsg_len;
1304 +
1305 +
1306 +    if (processor->status == CHILD_STATUS_STANDBY)
1307 +    {
1308 +        _DBG("Activating child #%d", processor->id);
1309 +        processor->status = CHILD_STATUS_STARTING;
1310 +    }
1311 +
1312 +    while (processor->status != CHILD_STATUS_ACTIVE)
1313 +    {
1314 +        _DBG("Waiting for child #%d...", processor->id);
1315 +        sleep(1);
1316 +    }
1317 +
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);
1321 +
1322 +    if ((rv = sendmsg(processor->senv->output, &msg, 0)) == -1)
1323 +    {
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);
1327 +        return -1;
1328 +    }
1329 +
1330 +    _DBG("Writing message succeeded %d", rv);
1331 +
1332 +    /* -- close the socket on our side -- */
1333 +    _DBG("closing socket %d on our side", sock_fd);
1334 +    apr_socket_close(thesock);
1335 +
1336 +    apr_pool_destroy(r->pool);
1337 +    return 1;
1338 +}
1339 +
1340 +
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 */
1345 +)
1346 +{
1347 +    struct msghdr msg;
1348 +    struct cmsghdr *cmsg;
1349 +    char buff[HUGE_STRING_LEN] = "";
1350 +    char headers[HUGE_STRING_LEN] = "";
1351 +    char *body = "";
1352 +    apr_size_t header_len, body_len;
1353 +    struct iovec iov[3];
1354 +    int ret, fd_tmp;
1355 +    apr_os_sock_t ctrl_sock_fd;
1356 +    apr_os_sock_t trans_sock_fd;
1357 +
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;
1362 +
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;
1370 +
1371 +    cmsg = apr_palloc(ptrans, sizeof(*cmsg) + sizeof(trans_sock_fd));
1372 +    cmsg->cmsg_len = sizeof(*cmsg) + sizeof(trans_sock_fd);
1373 +
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;
1380 +
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);
1385 +
1386 +    if(ret == -1)
1387 +      _DBG("recvmsg failed with error \"%s\"", strerror(errno));
1388 +    else
1389 +      _DBG("recvmsg returned %d", ret);
1390 +
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);
1395 +
1396 +    _DBG("trans_sock=%ld fdx=%d sock_fd=%d",
1397 +         *trans_sock, trans_sock_fd, fd_tmp);
1398 +
1399 +    apr_cpystrn(headers, buff, header_len + 1);
1400 +    _DBG("header_len=%d headers=\"%s\"", header_len, headers);
1401 +
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);
1408 +
1409 +    if (body_len) {
1410 +        body = (char*)&buff[header_len + 1];
1411 +        _DBG("body_len=%d body=\"%s\"", body_len, body);
1412 +
1413 +        bucket = apr_bucket_heap_create(body, body_len, NULL, alloc);
1414 +        APR_BRIGADE_INSERT_HEAD(bb, bucket);
1415 +    } else {
1416 +        _DBG("There is no body",0);
1417 +    }
1418 +
1419 +    bucket = apr_bucket_heap_create(headers, header_len, NULL, alloc);
1420 +
1421 +    APR_BRIGADE_INSERT_HEAD(bb, bucket);
1422 +    apr_pool_userdata_set(bb, "PERUSER_SOCKETS", NULL, ptrans);
1423 +
1424 +    _DBG("returning 0", 0);
1425 +    return 0;
1426 +}
1427 +
1428 +
1429 +/* Set group privileges.
1430 + *
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.
1434 + */
1435 +
1436 +static int set_group_privs(uid_t uid, gid_t gid)
1437 +{
1438 +    if (!geteuid())
1439 +    {
1440 +        struct passwd *ent;
1441 +        const char *name;
1442 +
1443 +        /*
1444 +         * Set the GID before initgroups(), since on some platforms
1445 +         * setgid() is known to zap the group list.
1446 +         */
1447 +        if (setgid(gid) == -1)
1448 +        {
1449 +            ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1450 +                         "setgid: unable to set group id to Group %u",
1451 +                         (unsigned)gid);
1452 +            return -1;
1453 +        }
1454 +
1455 +        /* if getpwuid() fails, just skip initgroups() */
1456 +
1457 +        if ((ent = getpwuid(uid)) != NULL)
1458 +        {
1459 +            name = ent->pw_name;
1460 +
1461 +            /* Reset `groups' attributes. */
1462 +
1463 +            if (initgroups(name, gid) == -1)
1464 +            {
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);
1468 +                return -1;
1469 +            }
1470 +        }
1471 +    }
1472 +    return 0;
1473 +}
1474 +
1475 +static int peruser_setup_child(int childnum)
1476 +{
1477 +    server_env_t *senv = CHILD_INFO_TABLE[childnum].senv;
1478 +
1479 +    if(senv->chroot) {
1480 +      _DBG("chroot to %s", senv->chroot);
1481 +      if(chroot(senv->chroot)) {
1482 +        _DBG("chroot failure %s", senv->chroot);
1483 +        return;
1484 +      }
1485 +    }
1486 +
1487 +    if (senv->uid == -1 && senv->gid == -1) {
1488 +        return unixd_setup_child();
1489 +    }
1490 +    if (set_group_privs(senv->uid, senv->gid)) {
1491 +        return -1;
1492 +    }
1493 +    /* Only try to switch if we're running as root */
1494 +    if (!geteuid()
1495 +        && (
1496 +#ifdef _OSD_POSIX
1497 +            os_init_job_environment(ap_server_conf, unixd_config.user_name,
1498 +                                    one_process) != 0 ||
1499 +#endif
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);
1504 +        return -1;
1505 +    }
1506 +    return 0;
1507 +}
1508 +
1509 +static int check_signal(int signum)
1510 +{
1511 +    _DBG("signum=%d", signum);
1512 +    switch (signum) {
1513 +    case SIGTERM:
1514 +    case SIGINT:
1515 +        just_die(signum);
1516 +        return 1;
1517 +    }
1518 +    return 0;
1519 +}
1520 +
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.
1525 + */
1526 +static int peruser_header_field(peruser_header *h,
1527 +                             const char *fieldname, const char *fieldval)
1528 +{
1529 +    apr_pstrcat(h->p, h->headers, fieldname, ": ", fieldval, CRLF, NULL);
1530 +    return 1;
1531 +}
1532 +
1533 +static inline ap_listen_rec* listen_add(apr_pool_t* pool, apr_socket_t *sock, void* accept_func)
1534 +{
1535 +    ap_listen_rec *lr_walk, *lr_new;
1536 +
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;
1544 +
1545 +    /* -- add the new listener_rec into the list -- */
1546 +    /* FIXME: should we somehow lock this list ? */
1547 +    lr_walk = ap_listeners;
1548 +    if (lr_walk)
1549 +    {
1550 +        while (lr_walk->next) lr_walk = lr_walk->next;
1551 +        lr_walk->next = lr_new;
1552 +    }
1553 +    else
1554 +    {
1555 +        ap_listeners = lr_walk = lr_new;
1556 +    }
1557 +    num_listensocks++;
1558 +    return lr_new;
1559 +}
1560 +
1561 +static inline void listen_clear()
1562 +{
1563 +    ap_listen_rec *lr_walk;
1564 +
1565 +    _DBG("function entered", 0);
1566 +
1567 +    /* FIXME: should we somehow lock this list ? */
1568 +    while (ap_listeners)
1569 +    {
1570 +        lr_walk = ap_listeners->next;
1571 +        apr_socket_close(ap_listeners->sd);
1572 +        ap_listeners = lr_walk;
1573 +    }
1574 +    num_listensocks=0;
1575 +}
1576 +
1577 +apr_status_t cleanup_child_info(void *d)
1578 +{
1579 +    if (child_info_image == NULL) {
1580 +        return APR_SUCCESS;
1581 +    }
1582 +
1583 +    free(child_info_image);
1584 +    child_info_image = NULL;
1585 +    apr_shm_destroy(child_info_shm);
1586 +
1587 +    return APR_SUCCESS;
1588 +}
1589 +
1590 +apr_status_t cleanup_server_environments(void *d)
1591 +{
1592 +    if (server_env_image == NULL) {
1593 +        return APR_SUCCESS;
1594 +    }
1595 +
1596 +    free(server_env_image);
1597 +    server_env_image = NULL;
1598 +    apr_shm_destroy(server_env_shm);
1599 +
1600 +    return APR_SUCCESS;
1601 +}
1602 +
1603 +static const char* child_clone();
1604 +
1605 +static void child_main(int child_num_arg)
1606 +{
1607 +    apr_pool_t *ptrans;
1608 +    apr_allocator_t *allocator;
1609 +    conn_rec *current_conn;
1610 +    apr_status_t status = APR_EINIT;
1611 +    int i;
1612 +    ap_listen_rec *lr;
1613 +    int curr_pollfd, last_pollfd = 0;
1614 +    apr_pollfd_t *pollset;
1615 +    int offset;
1616 +    ap_sb_handle_t *sbh;
1617 +    apr_status_t rv;
1618 +    apr_bucket_alloc_t *bucket_alloc;
1619 +    int fd;
1620 +    void* sock;
1621 +    void* pod_sock;
1622 +
1623 +    mpm_state = AP_MPMQ_STARTING; /* for benefit of any hooks that run as this
1624 +                                  * child initializes
1625 +                                  */
1626 +
1627 +    my_child_num = child_num_arg;
1628 +    ap_my_pid = getpid();
1629 +    requests_this_child = 0;
1630 +
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);
1634 +
1635 +    /* Get a sub context for global allocations in this child, so that
1636 +     * we can have cleanups occur when the child exits.
1637 +     */
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);
1642 +
1643 +    apr_pool_create(&ptrans, pchild);
1644 +    apr_pool_tag(ptrans, "transaction");
1645 +
1646 +    /* needs to be done before we switch UIDs so we have permissions */
1647 +    ap_reopen_scoreboard(pchild, NULL, 0);
1648 +
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);
1654 +    }
1655 +
1656 +    switch(CHILD_INFO_TABLE[my_child_num].type)
1657 +    {
1658 +        case CHILD_TYPE_MULTIPLEXER:
1659 +            _DBG("MULTIPLEXER %d", my_child_num);
1660 +
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)
1664 +            {
1665 +                if(CHILD_INFO_TABLE[i].status == CHILD_STATUS_READY)
1666 +                    CHILD_INFO_TABLE[i].status = CHILD_STATUS_ACTIVE;
1667 +            }
1668 +
1669 +            break;
1670 +
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);
1674 +
1675 +            /* -- create new listener to receive from multiplexer -- */
1676 +            apr_os_sock_put((void*)&sock, &CHILD_INFO_TABLE[my_child_num].senv->input, pconf);
1677 +            listen_clear();
1678 +            listen_add(pconf, sock, receive_from_multiplexer);
1679 +
1680 +            break;
1681 +
1682 +        default:
1683 +            _DBG("unspecified child type for %d sleeping a while ...", my_child_num);
1684 +            sleep(5);
1685 +            return;
1686 +    }
1687 +
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);
1691 +
1692 +    (peruser_setup_child(my_child_num) && clean_child_exit(APEXIT_CHILDFATAL));
1693 +    ap_run_child_init(pchild, ap_server_conf);
1694 +
1695 +    ap_create_sb_handle(&sbh, pchild, my_child_num, 0);
1696 +    (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
1697 +
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;
1704 +    }
1705 +
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;
1712 +    }
1713 +
1714 +    mpm_state = AP_MPMQ_RUNNING;
1715 +
1716 +    bucket_alloc = apr_bucket_alloc_create(pchild);
1717 +
1718 +    while (!die_now) {
1719 +       /*
1720 +        * (Re)initialize this child to a pre-connection state.
1721 +        */
1722 +
1723 +       current_conn = NULL;
1724 +
1725 +       apr_pool_clear(ptrans);
1726 +
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);
1732 +       }
1733 +
1734 +       (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
1735 +
1736 +       /*
1737 +        * Wait for an acceptable connection to arrive.
1738 +        */
1739 +
1740 +       /* Lock around "accept", if necessary */
1741 +       SAFE_ACCEPT(accept_mutex_on());
1742 +
1743 +        if (num_listensocks == 1) {
1744 +            offset = 0;
1745 +        }
1746 +        else {
1747 +            /* multiple listening sockets - need to poll */
1748 +           for (;;) {
1749 +                apr_status_t ret;
1750 +                apr_int32_t n;
1751 +
1752 +                ret = apr_poll(pollset, num_listensocks, &n, -1);
1753 +                if (ret != APR_SUCCESS) {
1754 +                    if (APR_STATUS_IS_EINTR(ret)) {
1755 +                        continue;
1756 +                    }
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.
1762 +                    */
1763 +                   ap_log_error(APLOG_MARK, APLOG_ERR, ret, ap_server_conf,
1764 +                             "apr_poll: (listen)");
1765 +                   clean_child_exit(1);
1766 +                }
1767 +                /* find a listener */
1768 +                curr_pollfd = last_pollfd;
1769 +                do {
1770 +                    curr_pollfd++;
1771 +                    if (curr_pollfd >= num_listensocks) {
1772 +                        curr_pollfd = 0;
1773 +                    }
1774 +                    /* XXX: Should we check for POLLERR? */
1775 +                    if (pollset[curr_pollfd].rtnevents & APR_POLLIN) {
1776 +                        last_pollfd = curr_pollfd;
1777 +                        offset = curr_pollfd;
1778 +                        goto got_fd;
1779 +                    }
1780 +                } while (curr_pollfd != last_pollfd);
1781 +
1782 +                continue;
1783 +            }
1784 +        }
1785 +    got_fd:
1786 +        _DBG("input available ... resetting socket.",0);
1787 +        sock = NULL;    /* important! */
1788 +
1789 +       /* if we accept() something we don't want to die, so we have to
1790 +        * defer the exit
1791 +        */
1792 +        status = listensocks[offset].accept_func(&sock, &listensocks[offset], ptrans);
1793 +        SAFE_ACCEPT(accept_mutex_off());       /* unlock after "accept" */
1794 +
1795 +        if (status == APR_EGENERAL) {
1796 +            /* resource shortage or should-not-occur occured */
1797 +            clean_child_exit(1);
1798 +        }
1799 +        else if (status != APR_SUCCESS || die_now) {
1800 +            continue;
1801 +        }
1802 +
1803 +        if (CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_PROCESSOR ||
1804 +            CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_WORKER)
1805 +        {
1806 +          _DBG("CHECKING IF WE SHOULD CLONE A CHILD...");
1807 +
1808 +          _DBG("total_processors = %d, max_processors = %d",
1809 +            total_processors(my_child_num),
1810 +            CHILD_INFO_TABLE[my_child_num].senv->max_processors);
1811 +
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);
1815 +
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)
1820 +          {
1821 +              _DBG("CLONING CHILD");
1822 +              child_clone();
1823 +          }
1824 +        }
1825 +
1826 +        if (!setjmp(CHILD_INFO_TABLE[my_child_num].jmpbuffer))
1827 +        {
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);
1832 +        }
1833 +        else
1834 +        {
1835 +            _DBG("landed from longjmp",0);
1836 +            CHILD_INFO_TABLE[my_child_num].sock_fd = AP_PERUSER_THISCHILD;
1837 +        }
1838 +
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.
1843 +         */
1844 +        if (ap_mpm_pod_check(pod) == APR_SUCCESS) { /* selected as idle? */
1845 +            _DBG("ap_mpm_pod_check(pod) = APR_SUCCESS; dying now", 0);
1846 +            die_now = 1;
1847 +        }
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?
1852 +             */
1853 +            _DBG("ap_my_generation != ap_scoreboard_image->global->running_generation; dying now", 0);
1854 +            die_now = 1;
1855 +        }
1856 +
1857 +        if(CHILD_INFO_TABLE[my_child_num].status == CHILD_STATUS_RESTART)
1858 +        {
1859 +            _DBG("restarting", 0);
1860 +            die_now = 1;
1861 +        }
1862 +    }
1863 +
1864 +    _DBG("clean_child_exit(0)");
1865 +    clean_child_exit(0);
1866 +}
1867 +
1868 +static server_env_t* senv_add(int uid, int gid, const char* chroot)
1869 +{
1870 +    int i;
1871 +    int socks[2];
1872 +
1873 +    _DBG("Searching for matching senv...");
1874 +
1875 +    for(i = 0; i < NUM_SENV; i++)
1876 +    {
1877 +      if(SENV[i].uid == uid && SENV[i].gid == gid &&
1878 +         (SENV[i].chroot == NULL || !strcmp(SENV[i].chroot, chroot)))
1879 +      {
1880 +          _DBG("Found existing senv: %i", i);
1881 +          return &SENV[i];
1882 +      }
1883 +    }
1884 +
1885 +    if(NUM_SENV >= server_limit)
1886 +    {
1887 +      _DBG("server_limit reached!");
1888 +      return NULL;
1889 +    }
1890 +
1891 +    _DBG("Creating new senv");
1892 +
1893 +    SENV[NUM_SENV].uid = uid;
1894 +    SENV[NUM_SENV].gid = gid;
1895 +    SENV[NUM_SENV].chroot = chroot;
1896 +
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;
1900 +
1901 +    socketpair(PF_UNIX, SOCK_STREAM, 0, socks);
1902 +    SENV[NUM_SENV].input  = socks[0];
1903 +    SENV[NUM_SENV].output = socks[1];
1904 +
1905 +    return &SENV[server_env_image->control->num++];
1906 +}
1907 +
1908 +static const char* child_clone()
1909 +{
1910 +    int i;
1911 +    child_info_t *this;
1912 +    child_info_t *new;
1913 +
1914 +    if(NUM_CHILDS >= server_limit)
1915 +    {
1916 +        _DBG("Trying to use more child ID's than NumServers.  "
1917 +               "Increase NumServers in your config file.");
1918 +        return NULL;
1919 +    }
1920 +
1921 +    for(i = 0; i < NUM_CHILDS; i++)
1922 +    {
1923 +      if(CHILD_INFO_TABLE[i].pid == 0 &&
1924 +         CHILD_INFO_TABLE[i].type == CHILD_TYPE_UNKNOWN) break;
1925 +    }
1926 +
1927 +    _DBG("cloning child #%d from #%d", i, my_child_num);
1928 +
1929 +    this = &CHILD_INFO_TABLE[my_child_num];
1930 +    new = &CHILD_INFO_TABLE[i];
1931 +
1932 +    new->senv = this->senv;
1933 +    new->type = CHILD_TYPE_WORKER;
1934 +    new->sock_fd = this->sock_fd;
1935 +    new->status = CHILD_STATUS_STARTING;
1936 +
1937 +    if(i == NUM_CHILDS) child_info_image->control->num++;
1938 +    return NULL;
1939 +}
1940 +
1941 +static const char* child_add(int type, int status,
1942 +                             uid_t uid, gid_t gid, const char* chroot)
1943 +{
1944 +    _DBG("adding child #%d", NUM_CHILDS);
1945 +
1946 +    if(NUM_CHILDS >= server_limit)
1947 +    {
1948 +        return "Trying to use more child ID's than NumServers.  "
1949 +               "Increase NumServers in your config file.";
1950 +    }
1951 +
1952 +    CHILD_INFO_TABLE[NUM_CHILDS].senv = senv_add(uid, gid, chroot);
1953 +
1954 +    if(CHILD_INFO_TABLE[NUM_CHILDS].senv == NULL)
1955 +    {
1956 +        return "Trying to use more server environments than NumServers.  "
1957 +               "Increase NumServers in your config file.";
1958 +    }
1959 +
1960 +    if(type != CHILD_TYPE_WORKER)
1961 +        CHILD_INFO_TABLE[NUM_CHILDS].senv->processor_id = NUM_CHILDS;
1962 +
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;
1966 +
1967 +    _DBG("[%d] uid=%d gid=%d type=%d chroot=%s",
1968 +         NUM_CHILDS, uid, gid, type,
1969 +         chroot);
1970 +
1971 +    if (uid == 0 || gid == 0)
1972 +    {
1973 +        _DBG("Assigning root user/group to a child.", 0);
1974 +    }
1975 +
1976 +    server_env_image->control->num++;
1977 +    child_info_image->control->num++;
1978 +
1979 +    return NULL;
1980 +}
1981 +
1982 +static int make_child(server_rec *s, int slot)
1983 +{
1984 +    int pid;
1985 +    int socks[2];
1986 +    child_info_t *multiplexer;
1987 +
1988 +    _DBG("function entered", 0);
1989 +
1990 +    switch (CHILD_INFO_TABLE[slot].type)
1991 +    {
1992 +        case CHILD_TYPE_MULTIPLEXER: break;
1993 +        case CHILD_TYPE_PROCESSOR: break;
1994 +        case CHILD_TYPE_WORKER: break;
1995 +
1996 +        default:
1997 +            _DBG("no valid client in slot %d", slot);
1998 +            /* sleep(1); */
1999 +            return 0;
2000 +    }
2001 +
2002 +    if (slot + 1 > ap_max_daemons_limit) {
2003 +       ap_max_daemons_limit = slot + 1;
2004 +    }
2005 +
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);
2010 +#ifdef SIGQUIT
2011 +       apr_signal(SIGQUIT, SIG_DFL);
2012 +#endif
2013 +       apr_signal(SIGTERM, just_die);
2014 +       child_main(slot);
2015 +    }
2016 +
2017 +    (void) ap_update_child_status_from_indexes(slot, 0, SERVER_STARTING,
2018 +                                               (request_rec *) NULL);
2019 +
2020 +    CHILD_INFO_TABLE[slot].status = CHILD_STATUS_ACTIVE;
2021 +
2022 +
2023 +#ifdef _OSD_POSIX
2024 +    /* BS2000 requires a "special" version of fork() before a setuid() call */
2025 +    if ((pid = os_fork(unixd_config.user_name)) == -1) {
2026 +#elif defined(TPF)
2027 +    if ((pid = os_fork(s, slot)) == -1) {
2028 +#else
2029 +    if ((pid = fork()) == -1) {
2030 +#endif
2031 +       ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process");
2032 +
2033 +       /* fork didn't succeed. Fix the scoreboard or else
2034 +        * it will say SERVER_STARTING forever and ever
2035 +        */
2036 +       (void) ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD,
2037 +                                                   (request_rec *) NULL);
2038 +
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. */
2042 +       sleep(10);
2043 +
2044 +       return -1;
2045 +    }
2046 +
2047 +    if (!pid) {
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
2051 +         */
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);
2057 +       }
2058 +#endif
2059 +       RAISE_SIGSTOP(MAKE_CHILD);
2060 +        AP_MONCONTROL(1);
2061 +        /* Disable the parent's signal handlers and set up proper handling in
2062 +         * the child.
2063 +        */
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.
2068 +         */
2069 +        /* apr_signal(AP_SIG_GRACEFUL, restart); */
2070 +       child_main(slot);
2071 +        clean_child_exit(0);
2072 +    }
2073 +
2074 +    ap_scoreboard_image->parent[slot].pid = pid;
2075 +    CHILD_INFO_TABLE[slot].pid    = pid;
2076 +
2077 +    ap_child_table[slot].pid    = pid;
2078 +    ap_child_table[slot].status = SERVER_ALIVE;
2079 +
2080 +    return 0;
2081 +}
2082 +
2083 +
2084 +/*
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.
2089 + */
2090 +static int idle_spawn_rate = 1;
2091 +#ifndef MAX_SPAWN_RATE
2092 +#define MAX_SPAWN_RATE (32)
2093 +#endif
2094 +static int hold_off_on_exponential_spawning;
2095 +
2096 +static void perform_idle_server_maintenance(apr_pool_t *p)
2097 +{
2098 +    int i;
2099 +    apr_time_t now;
2100 +
2101 +    /* _DBG("function entered", 0); */
2102 +
2103 +    now = apr_time_now();
2104 +
2105 +    for (i = 0; i < NUM_CHILDS; ++i)
2106 +    {
2107 +      if(CHILD_INFO_TABLE[i].pid == 0)
2108 +      {
2109 +        if(CHILD_INFO_TABLE[i].status == CHILD_STATUS_STARTING)
2110 +          make_child(ap_server_conf, i);
2111 +      }
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)
2118 +      {
2119 +        CHILD_INFO_TABLE[i].pid = 0;
2120 +        CHILD_INFO_TABLE[i].status = CHILD_STATUS_STANDBY;
2121 +
2122 +        if(CHILD_INFO_TABLE[i].type == CHILD_TYPE_WORKER)
2123 +        {
2124 +          /* completely free up this slot */
2125 +
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 */
2129 +        }
2130 +
2131 +        if(kill(ap_scoreboard_image->parent[i].pid, SIGTERM) == -1)
2132 +        {
2133 +          ap_log_error(APLOG_MARK, APLOG_WARNING, errno,
2134 +            ap_server_conf, "kill SIGTERM");
2135 +        }
2136 +
2137 +        ap_update_child_status_from_indexes(i, 0, SERVER_DEAD, NULL);
2138 +      }
2139 +    }
2140 +}
2141 +
2142 +
2143 +/*****************************************************************
2144 + * Executive routines.
2145 + */
2146 +
2147 +int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
2148 +{
2149 +    int i;
2150 +/*    int fd; */
2151 +    apr_status_t rv;
2152 +    apr_size_t one = 1;
2153 +/*    apr_socket_t *sock = NULL; */
2154 +
2155 +    ap_log_pid(pconf, ap_pid_fname);
2156 +
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;
2163 +    }
2164 +
2165 +    ap_server_conf = s;
2166 +
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),
2170 +                                 ap_my_pid);
2171 +
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;
2178 +        return 1;
2179 +    }
2180 +
2181 +#if 0
2182 +#if APR_USE_SYSVSEM_SERIALIZE
2183 +    if (ap_accept_lock_mech == APR_LOCK_DEFAULT || 
2184 +        ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
2185 +#else
2186 +    if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
2187 +#endif
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;
2194 +            return 1;
2195 +        }
2196 +    }
2197 +#endif
2198 +
2199 +    if (!is_graceful) {
2200 +        if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
2201 +            mpm_state = AP_MPMQ_STOPPING;
2202 +            return 1;
2203 +        }
2204 +        /* fix the generation number in the global score; we just got a new,
2205 +         * cleared scoreboard
2206 +         */
2207 +        ap_scoreboard_image->global->running_generation = ap_my_generation;
2208 +    }
2209 +
2210 +    /* Initialize the child table */
2211 +    if (!is_graceful)
2212 +    {
2213 +        for (i = 0; i < server_limit; i++)
2214 +        {
2215 +            ap_child_table[i].pid = 0;
2216 +        }
2217 +    }
2218 +
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
2224 +     * won't start.
2225 +     */
2226 +
2227 +/*
2228 +    apr_os_file_get(&fd, pipe_of_death_in);
2229 +    apr_os_sock_put(&sock, &fd, pconf);
2230 +
2231 +    listen_add(pconf, sock, check_pipe_of_death);
2232 +*/
2233 +    set_signals();
2234 +
2235 +    if (one_process) {
2236 +        AP_MONCONTROL(1);
2237 +    }
2238 +
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());
2249 +#endif
2250 +    restart_pending = shutdown_pending = 0;
2251 +
2252 +    mpm_state = AP_MPMQ_RUNNING;
2253 +
2254 +    _DBG("sizeof(child_info_t) = %d", sizeof(child_info_t));
2255 +
2256 +    while (!restart_pending && !shutdown_pending) {
2257 +       int child_slot;
2258 +        apr_exit_why_e exitwhy;
2259 +       int status, processed_status;
2260 +        /* this is a memory leak, but I'll fix it later. */
2261 +       apr_proc_t pid;
2262 +
2263 +        ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
2264 +
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
2267 +        * extra child
2268 +        */
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;
2273 +                return 1;
2274 +            }
2275 +
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);
2279 +
2280 +            for (i = 0; i < ap_max_daemons_limit; ++i)
2281 +            {
2282 +                if (ap_child_table[i].pid == pid.pid)
2283 +                {
2284 +                    child_slot = i;
2285 +                    break;
2286 +                }
2287 +            }
2288 +
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);
2295 +
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 
2299 +                     */
2300 +                    _DBG("processed_status = APEXIT_CHILDSICK", 0);
2301 +                    idle_spawn_rate = 1; 
2302 +                }
2303 +                else if (CHILD_INFO_TABLE[child_slot].status == CHILD_STATUS_STANDBY) {
2304 +                    _DBG("leaving child in standby state", 0);
2305 +                }
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
2311 +                       */
2312 +                    _DBG("replacing by new child ...", 0);
2313 +                   make_child(ap_server_conf, child_slot);
2314 +               }
2315 +#if APR_HAS_OTHER_CHILD
2316 +           }
2317 +           else if (apr_proc_other_child_read(&pid, status) == 0) {
2318 +                _DBG("APR_HAS_OTHER_CHILD, apparently", 0);
2319 +               /* handled */
2320 +#endif
2321 +           }
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
2325 +                   * child.
2326 +                   */
2327 +                _DBG("long lost child came home, whatever that means", 0);
2328 +
2329 +               ap_log_error(APLOG_MARK, APLOG_WARNING, 
2330 +                            0, ap_server_conf,
2331 +                           "long lost child came home! (pid %ld)", (long)pid.pid);
2332 +           }
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.
2337 +               */
2338 +           continue;
2339 +       }
2340 +
2341 +       perform_idle_server_maintenance(pconf);
2342 +#ifdef TPF
2343 +        shutdown_pending = os_check_server(tpf_server_name);
2344 +        ap_check_signals();
2345 +        sleep(1);
2346 +#endif /*TPF */
2347 +    }
2348 +
2349 +    mpm_state = AP_MPMQ_STOPPING;
2350 +
2351 +    if (shutdown_pending) {
2352 +       /* Time to gracefully shut down:
2353 +        * Kill child processes, tell them to call child_exit, etc...
2354 +        */
2355 +       if (unixd_killpg(getpgrp(), SIGTERM) < 0) {
2356 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGTERM");
2357 +       }
2358 +       ap_reclaim_child_processes(1);          /* Start with SIGTERM */
2359 +
2360 +       /* cleanup pid file on normal shutdown */
2361 +       {
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());
2369 +       }
2370 +
2371 +       ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2372 +                   "caught SIGTERM, shutting down");
2373 +       return 1;
2374 +    }
2375 +
2376 +    /* we've been told to restart */
2377 +    apr_signal(SIGHUP, SIG_IGN);
2378 +    if (one_process) {
2379 +       /* not worth thinking about */
2380 +       return 1;
2381 +    }
2382 +
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.
2386 +     */
2387 +    ++ap_my_generation;
2388 +    ap_scoreboard_image->global->running_generation = ap_my_generation;
2389 +    
2390 +    if (is_graceful) {
2391 +        char char_of_death = AP_PERUSER_CHAR_OF_DEATH;
2392 +
2393 +       ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2394 +                   "Graceful restart requested, doing restart");
2395 +
2396 +#if 0
2397 +       /* kill off the idle ones */
2398 +        ap_mpm_pod_killpg(pod, ap_max_daemons_limit);
2399 +
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)
2404 +           */
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;
2408 +           }
2409 +       }
2410 +#endif
2411 +
2412 +        ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
2413 +                     ap_server_conf, AP_SIG_GRACEFUL_STRING " received.  "
2414 +                     "Doing graceful restart");
2415 +
2416 +        /* This is mostly for debugging... so that we know what is still
2417 +         * gracefully dealing with existing request.
2418 +         */
2419 +
2420 +        for (i = 0; i < NUM_CHILDS; ++i)
2421 +        {
2422 +            ((ap_child_table[i].pid) && (ap_child_table[i].status = SERVER_DYING));
2423 +        }
2424 +
2425 +        /* give the children the signal to die */
2426 +        for (i = 0; i < NUM_CHILDS;)
2427 +        {
2428 +            if ((rv = apr_file_write(pipe_of_death_out, &char_of_death, &one)) != APR_SUCCESS)
2429 +            {
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");
2433 +            }
2434 +            i++;
2435 +        }
2436 +
2437 +        for (i = 0; i < NUM_SENV; i++)
2438 +        {
2439 +            close(SENV[i].input);
2440 +            close(SENV[i].output);
2441 +        }
2442 +    }
2443 +    else {
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");
2447 +       }
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");
2451 +    }
2452 +
2453 +    return 0;
2454 +}
2455 +
2456 +/* == allocate an private server config structure == */
2457 +static void *peruser_create_config(apr_pool_t *p, server_rec *s)
2458 +{
2459 +    peruser_server_conf *c = (peruser_server_conf *)
2460 +                                  apr_pcalloc(p, sizeof(peruser_server_conf));
2461 +    return c;
2462 +}
2463 +
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.
2466 + */
2467 +static int peruser_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
2468 +{
2469 +    apr_status_t rv;
2470 +
2471 +    pconf = p;
2472 +    ap_server_conf = s;
2473 +
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");
2477 +       return DONE;
2478 +    }
2479 +
2480 +    ap_log_pid(pconf, ap_pid_fname);
2481 +
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.");
2485 +        return DONE;
2486 +    }
2487 +
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)");
2493 +        exit(1);
2494 +    }
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)");
2499 +        exit(1);
2500 +    }
2501 +
2502 +    return OK;
2503 +}
2504 +
2505 +static int peruser_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
2506 +{
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;
2511 +    apr_status_t rv;
2512 +    apr_pool_t *global_pool;
2513 +    void *shmem;
2514 +
2515 +    mpm_state = AP_MPMQ_STARTING;
2516 +
2517 +    debug = ap_exists_config_define("DEBUG");
2518 +
2519 +    if (debug) {
2520 +        foreground = one_process = 1;
2521 +        no_detach = 0;
2522 +    }
2523 +    else
2524 +    {
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");
2528 +    }
2529 +
2530 +    /* sigh, want this only the second time around */
2531 +    if (restart_num++ == 1) {
2532 +       is_graceful = 0;
2533 +
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;
2541 +            }
2542 +        }
2543 +
2544 +       parent_pid = ap_my_pid = getpid();
2545 +    }
2546 +
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;
2559 +#endif
2560 +
2561 +    apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
2562 +
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
2565 +     */
2566 +    for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next)
2567 +    {
2568 +        if (!strcasecmp(pdir->directive, "ServerLimit"))
2569 +        {
2570 +            if (atoi(pdir->args) > tmp_server_limit)
2571 +            {
2572 +                tmp_server_limit = atoi(pdir->args);
2573 +                if (tmp_server_limit > MAX_SERVER_LIMIT)
2574 +                {
2575 +                    tmp_server_limit = MAX_SERVER_LIMIT;
2576 +                }
2577 +            }
2578 +        }
2579 +    }
2580 +
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.
2583 +     */
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");
2588 +        return rv;
2589 +    }
2590 +
2591 +
2592 +    _DBG("Initializing child_info_table", 0);
2593 +    child_info_size = tmp_server_limit * sizeof(child_info_t) + sizeof(apr_size_t);
2594 +
2595 +    rv = apr_shm_create(&child_info_shm, child_info_size, NULL, global_pool);
2596 +
2597 +/*  if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) { */
2598 +    if (rv != APR_SUCCESS) {
2599 +        _DBG("shared memory creation failed", 0);
2600 +
2601 +        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2602 +                     "Unable to create shared memory segment "
2603 +                     "(anonymous shared memory failure)");
2604 +    }
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 */
2608 +    }
2609 +
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;
2613 +    }
2614 +
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;
2620 +
2621 +    child_info_image->control->num = 0;
2622 +
2623 +    for (i = 0; i < tmp_server_limit; i++)
2624 +    {
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;
2631 +    }
2632 +
2633 +    _DBG("Initializing server_environments_table", 0);
2634 +    server_env_size = tmp_server_limit * sizeof(server_env_t) + sizeof(apr_size_t);
2635 +
2636 +    rv = apr_shm_create(&server_env_shm, server_env_size, NULL, global_pool);
2637 +
2638 +    if (rv != APR_SUCCESS) {
2639 +        _DBG("shared memory creation failed", 0);
2640 +
2641 +        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2642 +                     "Unable to create shared memory segment "
2643 +                     "(anonymous shared memory failure)");
2644 +    }
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 */
2648 +    }
2649 +
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;
2653 +    }
2654 +
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;
2660 +
2661 +    server_env_image->control->num = 0;
2662 +
2663 +    for (i = 0; i < tmp_server_limit; i++)
2664 +    {
2665 +        SENV[i].processor_id = -1;
2666 +        SENV[i].uid          = -1;
2667 +        SENV[i].gid          = -1;
2668 +        SENV[i].chroot       = NULL;
2669 +        SENV[i].input        = -1;
2670 +        SENV[i].output       = -1;
2671 +    }
2672 +
2673 +    return OK;
2674 +}
2675 +
2676 +static int peruser_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *server_list)
2677 +{
2678 +    ap_child_table = (ap_ctable *)apr_pcalloc(p, server_limit * sizeof(ap_ctable));
2679 +
2680 +    return OK;
2681 +}
2682 +
2683 +static int peruser_post_read(request_rec *r)
2684 +{
2685 +    peruser_server_conf *sconf = PERUSER_SERVER_CONF(r->server->module_config);
2686 +    child_info_t *processor;
2687 +
2688 +    if(CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_MULTIPLEXER)
2689 +        processor = &CHILD_INFO_TABLE[sconf->senv->processor_id];
2690 +    else
2691 +        processor = &CHILD_INFO_TABLE[r->connection->id];
2692 +
2693 +
2694 +    if (!strlen(r->the_request))
2695 +    {
2696 +        _DBG("corrupt request. aborting",0);
2697 +        return DECLINED;
2698 +    }
2699 +
2700 +    if (processor->sock_fd != AP_PERUSER_THISCHILD)
2701 +    {
2702 +        apr_socket_t *sock = NULL;
2703 +
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);
2708 +        return OK;
2709 +    }
2710 +
2711 +    switch (CHILD_INFO_TABLE[my_child_num].type)
2712 +    {
2713 +        case CHILD_TYPE_MULTIPLEXER:
2714 +        {
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);
2718 +
2719 +            if (processor->id != my_child_num)
2720 +            {
2721 +                if (processor->status == CHILD_STATUS_STANDBY)
2722 +                {
2723 +                    _DBG("Activating child #%d", processor->id);
2724 +                    processor->status = CHILD_STATUS_STARTING;
2725 +                }
2726 +
2727 +                _DBG("Passing request.",0);
2728 +                if (pass_request(r, processor) == -1)
2729 +                {
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.");
2732 +                    return DECLINED;
2733 +                }
2734 +                _DBG("doing longjmp",0);
2735 +                longjmp(CHILD_INFO_TABLE[my_child_num].jmpbuffer, 1);
2736 +                _DBG("request declined at our site",0);
2737 +                return DECLINED;
2738 +                _DBG("OUH! we should never reach this point",0);
2739 +            }
2740 +            _DBG("WTF: the server is assigned to the multiplexer! ... dropping request",0);
2741 +            return DECLINED;
2742 +        }
2743 +        case CHILD_TYPE_PROCESSOR:
2744 +        case CHILD_TYPE_WORKER:
2745 +        {
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);
2748 +
2749 +            return OK;
2750 +        }
2751 +        default:
2752 +        {
2753 +            _DBG("unspecified child type %d in %d, dropping request",
2754 +                 CHILD_INFO_TABLE[my_child_num].type, my_child_num);
2755 +            return DECLINED;
2756 +        }
2757 +    }
2758 +
2759 +    _DBG("THIS POINT SHOULD NOT BE REACHED!",0);
2760 +    return OK;
2761 +}
2762 +
2763 +static void peruser_hooks(apr_pool_t *p)
2764 +{
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
2767 +     * console.
2768 +     */
2769 +    static const char *const aszSucc[] = {"core.c", NULL};
2770 +
2771 +#ifdef AUX3
2772 +    (void) set42sig();
2773 +#endif
2774 +
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);
2778 +
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.
2785 +     */
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);
2790 +}
2791 +
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)
2795 +{
2796 +    uid_t uid = ap_uname2id(user_name);
2797 +    gid_t gid = ap_gname2id(group_name);
2798 +
2799 +    _DBG("user=%s:%d group=%s:%d chroot=%s",
2800 +        user_name, uid, group_name, gid, chroot);
2801 +
2802 +    return child_add(CHILD_TYPE_PROCESSOR, CHILD_STATUS_STANDBY,
2803 +                     uid, gid, chroot);
2804 +}
2805 +
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)
2809 +{
2810 +    uid_t uid = ap_uname2id(user_name);
2811 +    gid_t gid = ap_gname2id(group_name);
2812 +
2813 +    _DBG("user=%s:%d group=%s:%d chroot=%s [multiplexer id %d]",
2814 +        user_name, uid, group_name, gid, chroot, NUM_CHILDS);
2815 +
2816 +    return child_add(CHILD_TYPE_MULTIPLEXER, CHILD_STATUS_STARTING,
2817 +                     uid, gid, chroot);
2818 +}
2819 +
2820 +static const char* cf_ServerEnvironment(cmd_parms *cmd, void *dummy,
2821 +    const char *user_name, const char *group_name, const char *chroot)
2822 +{
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);
2826 +
2827 +    _DBG("function entered", 0);
2828 +
2829 +    sconf->senv = senv_add(uid, gid, chroot);
2830 +
2831 +    _DBG("user=%s:%d group=%s:%d chroot=%s numchilds=%d",
2832 +        user_name, uid, group_name, gid, chroot, NUM_CHILDS);
2833 +
2834 +    return NULL;
2835 +}
2836 +
2837 +static const char *set_min_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
2838 +{
2839 +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2840 +    if (err != NULL) {
2841 +        return err;
2842 +    }
2843 +
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;
2853 +    }
2854 +       
2855 +    return NULL;
2856 +}
2857 +
2858 +static const char *set_max_clients (cmd_parms *cmd, void *dummy, const char *arg) 
2859 +{
2860 +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2861 +    if (err != NULL) {
2862 +        return err;
2863 +    }
2864 +
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,
2874 +                    " directive.");
2875 +       ap_daemons_limit = server_limit;
2876 +    } 
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;
2881 +    }
2882 +    return NULL;
2883 +}
2884 +
2885 +static const char *set_min_processors (cmd_parms *cmd, void *dummy, const char *arg)
2886 +{
2887 +    peruser_server_conf *sconf;
2888 +    int min_procs;
2889 +    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2890 +
2891 +    if (err != NULL) {
2892 +        return err;
2893 +    }
2894 +
2895 +    min_procs = atoi(arg);
2896 +
2897 +    if (min_procs < 1) {
2898 +        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2899 +                     "WARNING: Require MaxProcessors > 0, setting to 1");
2900 +        min_procs = 1;
2901 +    }
2902 +
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;
2906 +    }
2907 +    else {
2908 +        ap_min_processors = min_procs;
2909 +    }
2910 +
2911 +    return NULL;
2912 +}
2913 +
2914 +static const char *set_min_free_processors (cmd_parms *cmd, void *dummy, const char *arg)
2915 +{
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);
2919 +
2920 +    if (err != NULL) {
2921 +        return err;
2922 +    }
2923 +
2924 +    min_free_procs = atoi(arg);
2925 +
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;
2930 +    }
2931 +
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;
2935 +    }
2936 +    else {
2937 +        ap_min_free_processors = min_free_procs;
2938 +    }
2939 +
2940 +    return NULL;
2941 +}
2942 +
2943 +static const char *set_max_processors (cmd_parms *cmd, void *dummy, const char *arg)
2944 +{
2945 +    peruser_server_conf *sconf;
2946 +    int max_procs;
2947 +    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2948 +
2949 +    if (err != NULL) {
2950 +        return err;
2951 +    }
2952 +
2953 +    max_procs = atoi(arg);
2954 +
2955 +    if (max_procs < 1) {
2956 +        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2957 +                     "WARNING: Require MaxProcessors > 0, setting to 1");
2958 +        max_procs = 1;
2959 +    }
2960 +
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;
2964 +    }
2965 +    else {
2966 +        ap_max_processors = max_procs;
2967 +    }
2968 +
2969 +    return NULL;
2970 +}
2971 +
2972 +static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg) 
2973 +{
2974 +    int tmp_server_limit;
2975 +    
2976 +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2977 +    if (err != NULL) {
2978 +        return err;
2979 +    }
2980 +
2981 +    tmp_server_limit = atoi(arg);
2982 +    /* you cannot change ServerLimit across a restart; ignore
2983 +     * any such attempts
2984 +     */
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
2990 +         */
2991 +        changed_limit_at_restart = 1;
2992 +        return NULL;
2993 +    }
2994 +    server_limit = tmp_server_limit;
2995 +    
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;
3003 +    } 
3004 +    else if (server_limit < 1) {
3005 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
3006 +                     "WARNING: Require ServerLimit > 0, setting to 1");
3007 +       server_limit = 1;
3008 +    }
3009 +    return NULL;
3010 +}
3011 +
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) {
3015 +        return err;
3016 +    }
3017 +
3018 +    expire_timeout = atoi(arg);
3019 +
3020 +    return NULL;
3021 +}
3022 +
3023 +static const command_rec peruser_cmds[] = {
3024 +UNIX_DAEMON_COMMANDS,
3025 +LISTEN_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."),
3046 +{ NULL }
3047 +};
3048 +
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 */
3058 +};
This page took 0.260823 seconds and 3 git commands to generate.