]> git.pld-linux.org Git - packages/apache.git/blob - httpd-2.0.50-peruser-r3.patch
- removed copy/paste bug. tnx twittner.
[packages/apache.git] / httpd-2.0.50-peruser-r3.patch
1 diff -Nur httpd-2.0.50/server/mpm/config.m4 httpd-2.0.50-new/server/mpm/config.m4
2 --- httpd-2.0.50/server/mpm/config.m4   2003-03-11 00:07:52.000000000 -0700
3 +++ httpd-2.0.50-new/server/mpm/config.m4       2004-08-01 21:06:05.773211880 -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|leader|threadpool|metuxmpm|peruser}),[
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.50/server/mpm/experimental/peruser/config.m4 httpd-2.0.50-new/server/mpm/experimental/peruser/config.m4
23 --- httpd-2.0.50/server/mpm/experimental/peruser/config.m4      1969-12-31 17:00:00.000000000 -0700
24 +++ httpd-2.0.50-new/server/mpm/experimental/peruser/config.m4  2004-08-01 21:06:05.777211272 -0600
25 @@ -0,0 +1,3 @@
26 +if test "$MPM_NAME" = "peruser" ; then
27 +    APACHE_FAST_OUTPUT(server/mpm/experimental/$MPM_NAME/Makefile)
28 +fi
29 diff -Nur httpd-2.0.50/server/mpm/experimental/peruser/Makefile.in httpd-2.0.50-new/server/mpm/experimental/peruser/Makefile.in
30 --- httpd-2.0.50/server/mpm/experimental/peruser/Makefile.in    1969-12-31 17:00:00.000000000 -0700
31 +++ httpd-2.0.50-new/server/mpm/experimental/peruser/Makefile.in        2004-08-01 21:06:05.775211576 -0600
32 @@ -0,0 +1,5 @@
33 +
34 +LTLIBRARY_NAME    = libperuser.la
35 +LTLIBRARY_SOURCES = peruser.c
36 +
37 +include $(top_srcdir)/build/ltlib.mk
38 diff -Nur httpd-2.0.50/server/mpm/experimental/peruser/mpm_default.h httpd-2.0.50-new/server/mpm/experimental/peruser/mpm_default.h
39 --- httpd-2.0.50/server/mpm/experimental/peruser/mpm_default.h  1969-12-31 17:00:00.000000000 -0700
40 +++ httpd-2.0.50-new/server/mpm/experimental/peruser/mpm_default.h      2004-08-01 21:06:05.781210664 -0600
41 @@ -0,0 +1,113 @@
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 +#ifndef APACHE_MPM_DEFAULT_H
101 +#define APACHE_MPM_DEFAULT_H
102 +
103 +/* Number of servers to spawn off by default --- also, if fewer than
104 + * this free when the caretaker checks, it will spawn more.
105 + */
106 +#ifndef DEFAULT_START_DAEMON
107 +#define DEFAULT_START_DAEMON 5
108 +#endif
109 +
110 +/* Maximum number of *free* server processes --- more than this, and
111 + * they will die off.
112 + */
113 +
114 +#ifndef DEFAULT_MAX_FREE_DAEMON
115 +#define DEFAULT_MAX_FREE_DAEMON 10
116 +#endif
117 +
118 +/* Minimum --- fewer than this, and more will be created */
119 +
120 +#ifndef DEFAULT_MIN_FREE_DAEMON
121 +#define DEFAULT_MIN_FREE_DAEMON 5
122 +#endif
123 +
124 +/* Maximum processors per ServerEnvironment */
125 +
126 +#ifndef DEFAULT_MAX_PROCESSORS
127 +#define DEFAULT_MAX_PROCESSORS 10
128 +#endif
129 +
130 +/* File used for accept locking, when we use a file */
131 +#ifndef DEFAULT_LOCKFILE
132 +#define DEFAULT_LOCKFILE DEFAULT_REL_RUNTIMEDIR "/accept.lock"
133 +#endif
134 +
135 +/* Where the main/parent process's pid is logged */
136 +#ifndef DEFAULT_PIDLOG
137 +#define DEFAULT_PIDLOG DEFAULT_REL_RUNTIMEDIR "/httpd.pid"
138 +#endif
139 +
140 +/*
141 + * Interval, in microseconds, between scoreboard maintenance.
142 + */
143 +#ifndef SCOREBOARD_MAINTENANCE_INTERVAL
144 +#define SCOREBOARD_MAINTENANCE_INTERVAL 1000000
145 +#endif
146 +
147 +/* Number of requests to try to handle in a single process.  If <= 0,
148 + * the children don't die off.
149 + */
150 +#ifndef DEFAULT_MAX_REQUESTS_PER_CHILD
151 +#define DEFAULT_MAX_REQUESTS_PER_CHILD 10000
152 +#endif
153 +
154 +#endif /* AP_MPM_DEFAULT_H */
155 diff -Nur httpd-2.0.50/server/mpm/experimental/peruser/mpm.h httpd-2.0.50-new/server/mpm/experimental/peruser/mpm.h
156 --- httpd-2.0.50/server/mpm/experimental/peruser/mpm.h  1969-12-31 17:00:00.000000000 -0700
157 +++ httpd-2.0.50-new/server/mpm/experimental/peruser/mpm.h      2004-08-01 21:13:56.178699376 -0600
158 @@ -0,0 +1,103 @@
159 +/* ====================================================================
160 + * The Apache Software License, Version 1.1
161 + *
162 + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
163 + * reserved.
164 + *
165 + * Redistribution and use in source and binary forms, with or without
166 + * modification, are permitted provided that the following conditions
167 + * are met:
168 + *
169 + * 1. Redistributions of source code must retain the above copyright
170 + *    notice, this list of conditions and the following disclaimer.
171 + *
172 + * 2. Redistributions in binary form must reproduce the above copyright
173 + *    notice, this list of conditions and the following disclaimer in
174 + *    the documentation and/or other materials provided with the
175 + *    distribution.
176 + *
177 + * 3. The end-user documentation included with the redistribution,
178 + *    if any, must include the following acknowledgment:
179 + *       "This product includes software developed by the
180 + *        Apache Software Foundation (http://www.apache.org/)."
181 + *    Alternately, this acknowledgment may appear in the software itself,
182 + *    if and wherever such third-party acknowledgments normally appear.
183 + *
184 + * 4. The names "Apache" and "Apache Software Foundation" must
185 + *    not be used to endorse or promote products derived from this
186 + *    software without prior written permission. For written
187 + *    permission, please contact apache@apache.org.
188 + *
189 + * 5. Products derived from this software may not be called "Apache",
190 + *    nor may "Apache" appear in their name, without prior written
191 + *    permission of the Apache Software Foundation.
192 + *
193 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
194 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
195 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
196 + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
197 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
198 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
199 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
200 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
201 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
202 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
203 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
204 + * SUCH DAMAGE.
205 + * ====================================================================
206 + *
207 + * This software consists of voluntary contributions made by many
208 + * individuals on behalf of the Apache Software Foundation.  For more
209 + * information on the Apache Software Foundation, please see
210 + * <http://www.apache.org/>.
211 + *
212 + * Portions of this software are based upon public domain software
213 + * originally written at the National Center for Supercomputing Applications,
214 + * University of Illinois, Urbana-Champaign.
215 + */
216 +
217 +#include "httpd.h"
218 +#include "mpm_default.h"
219 +#include "scoreboard.h"
220 +#include "unixd.h"
221 +
222 +#ifndef APACHE_MPM_PERUSER_H
223 +#define APACHE_MPM_PERUSER_H
224 +
225 +#define PERUSER_MPM
226 +
227 +#define MPM_NAME "Peruser"
228 +
229 +#define AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
230 +#define AP_MPM_WANT_WAIT_OR_TIMEOUT
231 +#define AP_MPM_WANT_PROCESS_CHILD_STATUS
232 +#define AP_MPM_WANT_SET_PIDFILE
233 +#define AP_MPM_WANT_SET_SCOREBOARD
234 +#define AP_MPM_WANT_SET_LOCKFILE
235 +#define AP_MPM_WANT_SET_MAX_REQUESTS
236 +#define AP_MPM_WANT_SET_COREDUMPDIR
237 +#define AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
238 +#define AP_MPM_WANT_SIGNAL_SERVER
239 +#define AP_MPM_WANT_SET_MAX_MEM_FREE
240 +#define AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
241 +
242 +#define AP_MPM_USES_POD 1
243 +#define MPM_CHILD_PID(i) (ap_scoreboard_image->parent[i].pid)
244 +#define MPM_NOTE_CHILD_KILLED(i) (MPM_CHILD_PID(i) = 0)
245 +#define MPM_ACCEPT_FUNC unixd_accept
246 +
247 +extern int ap_threads_per_child;
248 +extern int ap_max_daemons_limit;
249 +extern server_rec *ap_server_conf;
250 +
251 +/* Table of child status */
252 +#define SERVER_DEAD 0
253 +#define SERVER_DYING 1
254 +#define SERVER_ALIVE 2
255 +
256 +typedef struct ap_ctable {
257 +    pid_t pid;
258 +    unsigned char status;
259 +} ap_ctable;
260 +
261 +#endif /* APACHE_MPM_PERUSER_H */
262 diff -Nur httpd-2.0.50/server/mpm/experimental/peruser/peruser.c httpd-2.0.50-new/server/mpm/experimental/peruser/peruser.c
263 --- httpd-2.0.50/server/mpm/experimental/peruser/peruser.c      1969-12-31 17:00:00.000000000 -0700
264 +++ httpd-2.0.50-new/server/mpm/experimental/peruser/peruser.c  2004-08-01 21:07:09.480526896 -0600
265 @@ -0,0 +1,2629 @@
266 +/* ====================================================================
267 + * The Apache Software License, Version 1.1
268 + *
269 + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
270 + * reserved.
271 + *
272 + * Redistribution and use in source and binary forms, with or without
273 + * modification, are permitted provided that the following conditions
274 + * are met:
275 + *
276 + * 1. Redistributions of source code must retain the above copyright
277 + *    notice, this list of conditions and the following disclaimer.
278 + *
279 + * 2. Redistributions in binary form must reproduce the above copyright
280 + *    notice, this list of conditions and the following disclaimer in
281 + *    the documentation and/or other materials provided with the
282 + *    distribution.
283 + *
284 + * 3. The end-user documentation included with the redistribution,
285 + *    if any, must include the following acknowledgment:
286 + *       "This product includes software developed by the
287 + *        Apache Software Foundation (http://www.apache.org/)."
288 + *    Alternately, this acknowledgment may appear in the software itself,
289 + *    if and wherever such third-party acknowledgments normally appear.
290 + *
291 + * 4. The names "Apache" and "Apache Software Foundation" must
292 + *    not be used to endorse or promote products derived from this
293 + *    software without prior written permission. For written
294 + *    permission, please contact apache@apache.org.
295 + *
296 + * 5. Products derived from this software may not be called "Apache",
297 + *    nor may "Apache" appear in their name, without prior written
298 + *    permission of the Apache Software Foundation.
299 + *
300 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
301 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
302 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
303 + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
304 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
305 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
306 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
307 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
308 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
309 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
310 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311 + * SUCH DAMAGE.
312 + * ====================================================================
313 + *
314 + * This software consists of voluntary contributions made by many
315 + * individuals on behalf of the Apache Software Foundation.  For more
316 + * information on the Apache Software Foundation, please see
317 + * <http://www.apache.org/>.
318 + *
319 + * Portions of this software are based upon public domain software
320 + * originally written at the National Center for Supercomputing Applications,
321 + * University of Illinois, Urbana-Champaign.
322 + */
323 +
324 +/* #define MPM_PERUSER_DEBUG */
325 +
326 +#include "apr.h"
327 +#include "apr_hash.h"
328 +#include "apr_pools.h"
329 +#include "apr_file_io.h"
330 +#include "apr_portable.h"
331 +#include "apr_strings.h"
332 +#include "apr_thread_proc.h"
333 +#include "apr_signal.h"
334 +
335 +#define APR_WANT_STDIO
336 +#define APR_WANT_STRFUNC
337 +#define APR_WANT_IOVEC
338 +#include "apr_want.h"
339 +
340 +#if APR_HAVE_UNISTD_H
341 +#include <unistd.h>
342 +#endif
343 +#if APR_HAVE_SYS_TYPES_H
344 +#include <sys/types.h>
345 +#endif
346 +
347 +#define CORE_PRIVATE
348 +
349 +#include "ap_config.h"
350 +#include "httpd.h"
351 +#include "mpm_default.h"
352 +#include "http_main.h"
353 +#include "http_log.h"
354 +#include "http_config.h"
355 +#include "http_core.h"         /* for get_remote_host */
356 +#include "http_connection.h"
357 +#include "http_protocol.h"  /* for ap_hook_post_read_request */
358 +#include "scoreboard.h"
359 +#include "ap_mpm.h"
360 +#include "unixd.h"
361 +#include "mpm_common.h"
362 +#include "ap_listen.h"
363 +#include "ap_mmn.h"
364 +#include "apr_poll.h"
365 +#include "util_ebcdic.h"
366 +
367 +#ifdef HAVE_BSTRING_H
368 +#include <bstring.h>           /* for IRIX, FD_SET calls bzero() */
369 +#endif
370 +
371 +#ifdef HAVE_TIME_H
372 +#include <time.h>
373 +#endif
374 +
375 +#ifdef HAVE_SYS_PROCESSOR_H
376 +#include <sys/processor.h> /* for bindprocessor() */
377 +#endif
378 +
379 +#if APR_HAS_SHARED_MEMORY
380 +#include "apr_shm.h"
381 +#else
382 +#error "Peruser MPM requres shared memory support."
383 +#endif
384 +
385 +
386 +/* should be APR-ized */
387 +#include <grp.h>
388 +#include <pwd.h>
389 +#include <sys/stat.h>
390 +#include <sys/un.h>
391 +#include <setjmp.h>
392 +
393 +#include <signal.h>
394 +#include <sys/times.h>
395 +
396 +
397 +#ifdef MPM_PERUSER_DEBUG
398 +# define _DBG(text,par...) \
399 +    ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL, \
400 +                 "(peruser: pid=%d uid=%d child=%d) %s(): " text, \
401 +                 getpid(), getuid(), my_child_num, __FUNCTION__, ##par, 0)
402 +
403 +# define _TRACE_CALL(text,par...) _DBG("calling " text, ##par)
404 +# define _TRACE_RET(text,par...) _DBG("returned from " text, ##par)
405 +#else
406 +# define _DBG(text,par...)
407 +# define _TRACE_RET(text,par...)
408 +# define _TRACE_CALL(text,par...)
409 +#endif /* MPM_PERUSER_DEBUG */
410 +
411 +/* char of death - for signalling children to die */
412 +#define AP_PERUSER_CHAR_OF_DEATH        '!'
413 +
414 +#define PERUSER_SERVER_CONF(cf)        \
415 +    ((peruser_server_conf *) ap_get_module_config(cf, &mpm_peruser_module))
416 +
417 +/*
418 + * Define some magic numbers that we use for the state of the incomming
419 + * request. These must be < 0 so they don't collide with a file descriptor.
420 + */
421 +#define AP_PERUSER_THISCHILD -1
422 +#define AP_PERUSER_OTHERCHILD -2
423 +
424 +
425 +/* Limit on the total --- clients will be locked out if more servers than
426 + * this are needed.  It is intended solely to keep the server from crashing
427 + * when things get out of hand.
428 + *
429 + * We keep a hard maximum number of servers, for two reasons --- first off,
430 + * in case something goes seriously wrong, we want to stop the fork bomb
431 + * short of actually crashing the machine we're running on by filling some
432 + * kernel table.  Secondly, it keeps the size of the scoreboard file small
433 + * enough that we can read the whole thing without worrying too much about
434 + * the overhead.
435 + */
436 +#ifndef DEFAULT_SERVER_LIMIT
437 +#define DEFAULT_SERVER_LIMIT 256
438 +#endif
439 +
440 +/* Admin can't tune ServerLimit beyond MAX_SERVER_LIMIT.  We want
441 + * some sort of compile-time limit to help catch typos.
442 + */
443 +#ifndef MAX_SERVER_LIMIT
444 +#define MAX_SERVER_LIMIT 20000
445 +#endif
446 +
447 +#ifndef HARD_THREAD_LIMIT
448 +#define HARD_THREAD_LIMIT 1
449 +#endif
450 +
451 +#define CHILD_TYPE_MULTIPLEXER  1
452 +#define CHILD_TYPE_PROCESSOR    2
453 +
454 +#define CHILD_STATUS_STANDBY  0  /* wait for a request before starting */
455 +#define CHILD_STATUS_STARTING 1  /* wait for socket creation */
456 +#define CHILD_STATUS_READY    2  /* wait for mux to restart */
457 +#define CHILD_STATUS_ACTIVE   3  /* ready to take requests */
458 +#define CHILD_STATUS_RESTART  4  /* child about to die and restart */
459 +
460 +/* config globals */
461 +
462 +int ap_threads_per_child=0;         /* Worker threads per child */
463 +static apr_proc_mutex_t *accept_mutex;
464 +static int ap_daemons_min_free=0;
465 +static int ap_max_processors=0;
466 +static int ap_daemons_limit=0;      /* MaxClients */
467 +static int server_limit = DEFAULT_SERVER_LIMIT;
468 +static int first_server_limit;
469 +static int changed_limit_at_restart;
470 +
471 +static int requests_this_child;
472 +static ap_pod_t *pod;
473 +
474 +/* === configuration stuff === */
475 +/* the server limit is also the size of the server table */
476 +static int cf_server_limit = DEFAULT_SERVER_LIMIT;
477 +
478 +/* -- server process information -- */
479 +typedef struct
480 +{
481 +    /* identification */
482 +    int id;            /* index in child_info_table */
483 +    pid_t pid;         /* process id */
484 +    int status;                /* status of child */
485 +    int type;           /* multiplexer or processor */
486 +
487 +    /* security settings */
488 +    uid_t uid;         /* user id */
489 +    gid_t gid;         /* group id */
490 +    const char *chroot;        /* directory to chroot() to, can be null */
491 +
492 +    /* sockets */
493 +    int input;         /* The socket descriptor */
494 +    int output;                /* The socket descriptor */
495 +    int sock_fd;
496 +
497 +    /* stack context saved state */
498 +    jmp_buf jmpbuffer;
499 +} child_info_t;
500 +
501 +typedef struct
502 +{
503 +    apr_size_t num_childs;
504 +} child_info_control;
505 +
506 +typedef struct
507 +{
508 +    child_info_control *control;
509 +    child_info_t *table;
510 +} child_info;
511 +
512 +typedef struct
513 +{
514 +    const char *sockname;       /* The base name for the socket */
515 +    const char *fullsockname;   /* socket base name + extension */
516 +    uid_t uid;
517 +    gid_t gid;
518 +    const char *chroot;
519 +    int max_processors;
520 +    int last_child;
521 +} peruser_server_conf;
522 +
523 +typedef struct peruser_header
524 +{
525 +    char *headers;
526 +    apr_pool_t *p;
527 +} peruser_header;
528 +
529 +
530 +/* Tables used to determine the user and group each child process should
531 + * run as.  The hash table is used to correlate a server name with a child
532 + * process.
533 + */
534 +static apr_size_t child_info_size;
535 +static child_info *child_info_image;
536 +struct ap_ctable *ap_child_table;
537 +
538 +#define NUM_CHILDS (child_info_image != NULL ? child_info_image->control->num_childs : 0)
539 +#define CHILD_INFO_TABLE (child_info_image != NULL ? child_info_image->table : NULL)
540 +
541 +#if APR_HAS_SHARED_MEMORY
542 +#ifndef WIN32
543 +static /* but must be exported to mpm_winnt */
544 +#endif
545 +        apr_shm_t *child_info_shm = NULL;
546 +#endif
547 +
548 +/*
549 + * The max child slot ever assigned, preserved across restarts.  Necessary
550 + * to deal with MaxClients changes across AP_SIG_GRACEFUL restarts.  We 
551 + * use this value to optimize routines that have to scan the entire scoreboard.
552 + */
553 +int ap_max_daemons_limit = -1;
554 +server_rec *ap_server_conf;
555 +
556 +module AP_MODULE_DECLARE_DATA mpm_peruser_module;
557 +
558 +/* -- replace the pipe-of-death by an control socket -- */
559 +static apr_file_t *pipe_of_death_in = NULL;
560 +static apr_file_t *pipe_of_death_out = NULL;
561 +
562 +
563 +/* one_process --- debugging mode variable; can be set from the command line
564 + * with the -X flag.  If set, this gets you the child_main loop running
565 + * in the process which originally started up (no detach, no make_child),
566 + * which is a pretty nice debugging environment.  (You'll get a SIGHUP
567 + * early in standalone_main; just continue through.  This is the server
568 + * trying to kill off any child processes which it might have lying
569 + * around --- Apache doesn't keep track of their pids, it just sends
570 + * SIGHUP to the process group, ignoring it in the root process.
571 + * Continue through and you'll be fine.).
572 + */
573 +
574 +static int one_process = 0;
575 +
576 +static apr_pool_t *pconf;              /* Pool for config stuff */
577 +static apr_pool_t *pchild;             /* Pool for httpd child stuff */
578 +
579 +static pid_t ap_my_pid;        /* it seems silly to call getpid all the time */
580 +static pid_t parent_pid;
581 +static int my_child_num;
582 +ap_generation_t volatile ap_my_generation=0;
583 +
584 +#ifdef TPF
585 +int tpf_child = 0;
586 +char tpf_server_name[INETD_SERVNAME_LENGTH+1];
587 +#endif /* TPF */
588 +
589 +static int die_now = 0;
590 +
591 +#ifdef GPROF
592 +/* 
593 + * change directory for gprof to plop the gmon.out file
594 + * configure in httpd.conf:
595 + * GprofDir $RuntimeDir/   -> $ServerRoot/$RuntimeDir/gmon.out
596 + * GprofDir $RuntimeDir/%  -> $ServerRoot/$RuntimeDir/gprof.$pid/gmon.out
597 + */
598 +static void chdir_for_gprof(void)
599 +{
600 +    core_server_config *sconf = 
601 +       ap_get_module_config(ap_server_conf->module_config, &core_module);    
602 +    char *dir = sconf->gprof_dir;
603 +    const char *use_dir;
604 +
605 +    if(dir) {
606 +        apr_status_t res;
607 +       char buf[512];
608 +       int len = strlen(sconf->gprof_dir) - 1;
609 +       if(*(dir + len) == '%') {
610 +           dir[len] = '\0';
611 +           apr_snprintf(buf, sizeof(buf), "%sgprof.%d", dir, (int)getpid());
612 +       } 
613 +       use_dir = ap_server_root_relative(pconf, buf[0] ? buf : dir);
614 +       res = apr_dir_make(use_dir, 0755, pconf);
615 +       if(res != APR_SUCCESS && !APR_STATUS_IS_EEXIST(res)) {
616 +           ap_log_error(APLOG_MARK, APLOG_ERR, errno, ap_server_conf,
617 +                        "gprof: error creating directory %s", dir);
618 +       }
619 +    }
620 +    else {
621 +       use_dir = ap_server_root_relative(pconf, DEFAULT_REL_RUNTIMEDIR);
622 +    }
623 +
624 +    chdir(use_dir);
625 +}
626 +#else
627 +#define chdir_for_gprof()
628 +#endif
629 +
630 +char* child_type_string(int type)
631 +{
632 +    switch(type)
633 +    {
634 +        case CHILD_TYPE_MULTIPLEXER: return "MULTIPLEXER";
635 +        case CHILD_TYPE_PROCESSOR:   return "PROCESSOR";
636 +    }
637 +
638 +    return "UNKNOWN";
639 +}
640 +
641 +char* child_status_string(int status)
642 +{
643 +    switch(status)
644 +    {
645 +        case CHILD_STATUS_STANDBY:  return "STANDBY";
646 +        case CHILD_STATUS_STARTING: return "STARTING";
647 +        case CHILD_STATUS_READY:    return "READY";
648 +        case CHILD_STATUS_ACTIVE:   return "ACTIVE";
649 +        case CHILD_STATUS_RESTART:  return "RESTART";
650 +    }
651 +
652 +    return "UNKNOWN";
653 +}
654 +
655 +void dump_child_table()
656 +{
657 +  int x;
658 +
659 +  _DBG("%-3s %-5s %-8s %-12s %-4s %-4s %-25s %5s %6s %7s",
660 +    "ID", "PID", "STATUS", "TYPE", "UID", "GID", "CHROOT", "INPUT", "OUTPUT", "SOCK_FD");
661 +
662 +  for(x = 0; x < NUM_CHILDS; x++)
663 +  {
664 +    _DBG("%-3d %-5d %-8s %-12s %-4d %-4d %-25s %-5d %-6d %-7d",
665 +      CHILD_INFO_TABLE[x].id,
666 +      CHILD_INFO_TABLE[x].pid,
667 +      child_status_string(CHILD_INFO_TABLE[x].status),
668 +      child_type_string(CHILD_INFO_TABLE[x].type),
669 +      CHILD_INFO_TABLE[x].uid,
670 +      CHILD_INFO_TABLE[x].gid,
671 +      CHILD_INFO_TABLE[x].chroot,
672 +      CHILD_INFO_TABLE[x].input,
673 +      CHILD_INFO_TABLE[x].output,
674 +      CHILD_INFO_TABLE[x].sock_fd);
675 +  }
676 +}
677 +
678 +/* XXX - I don't know if TPF will ever use this module or not, so leave
679 + * the ap_check_signals calls in but disable them - manoj */
680 +#define ap_check_signals() 
681 +
682 +/* a clean exit from a child with proper cleanup */
683 +static inline int clean_child_exit(int code) __attribute__ ((noreturn));
684 +static inline int clean_child_exit(int code)
685 +{
686 +    if (pchild) {
687 +       apr_pool_destroy(pchild);
688 +    }
689 +    ap_mpm_pod_close(pod);
690 +    chdir_for_gprof();
691 +    exit(code);
692 +}
693 +
694 +static void accept_mutex_on(void)
695 +{
696 +/* for some reason this fails if we listen on the pipe_of_death.
697 +   fortunately I don't think we currently need it */
698 +
699 +#if 0
700 +    apr_status_t rv = apr_proc_mutex_lock(accept_mutex);
701 +    if (rv != APR_SUCCESS) {
702 +        const char *msg = "couldn't grab the accept mutex";
703 +
704 +        if (ap_my_generation != 
705 +            ap_scoreboard_image->global->running_generation) {
706 +            ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, msg);
707 +            clean_child_exit(0);
708 +        }
709 +        else {
710 +            ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, msg);
711 +            exit(APEXIT_CHILDFATAL);
712 +        }
713 +    }
714 +#endif
715 +}
716 +
717 +static void accept_mutex_off(void)
718 +{
719 +#if 0
720 +    apr_status_t rv = apr_proc_mutex_unlock(accept_mutex);
721 +    if (rv != APR_SUCCESS) {
722 +        const char *msg = "couldn't release the accept mutex";
723 +
724 +        if (ap_my_generation != 
725 +            ap_scoreboard_image->global->running_generation) {
726 +            ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, NULL, msg);
727 +            /* don't exit here... we have a connection to
728 +             * process, after which point we'll see that the
729 +             * generation changed and we'll exit cleanly
730 +             */
731 +        }
732 +        else {
733 +            ap_log_error(APLOG_MARK, APLOG_EMERG, rv, NULL, msg);
734 +            exit(APEXIT_CHILDFATAL);
735 +        }
736 +    }
737 +#endif
738 +}
739 +
740 +/* On some architectures it's safe to do unserialized accept()s in the single
741 + * Listen case.  But it's never safe to do it in the case where there's
742 + * multiple Listen statements.  Define SINGLE_LISTEN_UNSERIALIZED_ACCEPT
743 + * when it's safe in the single Listen case.
744 + */
745 +#ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
746 +#define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)
747 +#else
748 +#define SAFE_ACCEPT(stmt) do {stmt;} while(0)
749 +#endif
750 +
751 +AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result)
752 +{
753 +    switch(query_code){
754 +        case AP_MPMQ_MAX_DAEMON_USED:
755 +            *result = ap_daemons_limit;
756 +            return APR_SUCCESS;
757 +        case AP_MPMQ_IS_THREADED:
758 +            *result = AP_MPMQ_NOT_SUPPORTED;
759 +            return APR_SUCCESS;
760 +        case AP_MPMQ_IS_FORKED:
761 +            *result = AP_MPMQ_DYNAMIC;
762 +            return APR_SUCCESS;
763 +        case AP_MPMQ_HARD_LIMIT_DAEMONS:
764 +            *result = server_limit;
765 +            return APR_SUCCESS;
766 +        case AP_MPMQ_HARD_LIMIT_THREADS:
767 +            *result = HARD_THREAD_LIMIT;
768 +            return APR_SUCCESS;
769 +        case AP_MPMQ_MAX_THREADS:
770 +            *result = 0;
771 +            return APR_SUCCESS;
772 +        case AP_MPMQ_MIN_SPARE_DAEMONS:
773 +            *result = ap_daemons_min_free;
774 +            return APR_SUCCESS;
775 +        case AP_MPMQ_MIN_SPARE_THREADS:
776 +            *result = 0;
777 +            return APR_SUCCESS;
778 +        case AP_MPMQ_MAX_SPARE_THREADS:
779 +            *result = 0;
780 +            return APR_SUCCESS;
781 +        case AP_MPMQ_MAX_REQUESTS_DAEMON:
782 +            *result = ap_max_requests_per_child;
783 +            return APR_SUCCESS;
784 +        case AP_MPMQ_MAX_DAEMONS:
785 +            *result = server_limit;
786 +            return APR_SUCCESS;
787 +    }
788 +    return APR_ENOTIMPL;
789 +}
790 +
791 +#if defined(NEED_WAITPID)
792 +/*
793 +   Systems without a real waitpid sometimes lose a child's exit while waiting
794 +   for another.  Search through the scoreboard for missing children.
795 + */
796 +int reap_children(int *exitcode, apr_exit_why_e *status)
797 +{
798 +    int n, pid;
799 +
800 +    for (n = 0; n < ap_max_daemons_limit; ++n) {
801 +       if (ap_scoreboard_image->servers[n][0].status != SERVER_DEAD &&
802 +               kill((pid = ap_scoreboard_image->parent[n].pid), 0) == -1) {
803 +           ap_update_child_status_from_indexes(n, 0, SERVER_DEAD, NULL);
804 +           /* just mark it as having a successful exit status */
805 +            *status = APR_PROC_EXIT;
806 +            *exitcode = 0;
807 +           return(pid);
808 +       }
809 +    }
810 +    return 0;
811 +}
812 +#endif
813 +
814 +/* handle all varieties of core dumping signals */
815 +static void sig_coredump(int sig)
816 +{
817 +    chdir(ap_coredump_dir);
818 +    apr_signal(sig, SIG_DFL);
819 +    if (ap_my_pid == parent_pid) {
820 +            ap_log_error(APLOG_MARK, APLOG_NOTICE,
821 +                         0, ap_server_conf,
822 +                         "seg fault or similar nasty error detected "
823 +                         "in the parent process");
824 +    }
825 +    kill(getpid(), sig);
826 +    /* At this point we've got sig blocked, because we're still inside
827 +     * the signal handler.  When we leave the signal handler it will
828 +     * be unblocked, and we'll take the signal... and coredump or whatever
829 +     * is appropriate for this particular Unix.  In addition the parent
830 +     * will see the real signal we received -- whereas if we called
831 +     * abort() here, the parent would only see SIGABRT.
832 +     */
833 +}
834 +
835 +/*****************************************************************
836 + * Connection structures and accounting...
837 + */
838 +
839 +static void just_die(int sig)
840 +{
841 +_DBG("function called");
842 +    clean_child_exit(0);
843 +}
844 +
845 +/* volatile just in case */
846 +static int volatile shutdown_pending;
847 +static int volatile restart_pending;
848 +static int volatile is_graceful;
849 +/* XXX static int volatile child_fatal; */
850 +
851 +static void sig_term(int sig)
852 +{
853 +    if (shutdown_pending == 1) {
854 +       /* Um, is this _probably_ not an error, if the user has
855 +        * tried to do a shutdown twice quickly, so we won't
856 +        * worry about reporting it.
857 +        */
858 +       return;
859 +    }
860 +    shutdown_pending = 1;
861 +}
862 +
863 +/* restart() is the signal handler for SIGHUP and AP_SIG_GRACEFUL
864 + * in the parent process, unless running in ONE_PROCESS mode
865 + */
866 +static void restart(int sig)
867 +{
868 +    if (restart_pending == 1) {
869 +       /* Probably not an error - don't bother reporting it */
870 +       return;
871 +    }
872 +    restart_pending = 1;
873 +    is_graceful = (sig == AP_SIG_GRACEFUL);
874 +}
875 +
876 +/* Sets die_now if we received a character on the pipe_of_death */
877 +static apr_status_t check_pipe_of_death
878 +(
879 +    void **csd,
880 +    ap_listen_rec *lr,
881 +    apr_pool_t *ptrans
882 +)
883 +{
884 +    int ret;
885 +    char pipe_read_char;
886 +    apr_size_t n = 1;
887 +
888 +    _DBG("WATCH: die_now=%d", die_now);
889 +
890 +    if (die_now) return APR_SUCCESS;
891 +
892 +    /* apr_thread_mutex_lock(pipe_of_death_mutex); */
893 +    ret = apr_recv(lr->sd, &pipe_read_char, &n);
894 +    if (APR_STATUS_IS_EAGAIN(ret))
895 +    {
896 +            /* It lost the lottery. It must continue to suffer
897 +             * through a life of servitude. */
898 +    }
899 +    else
900 +    {
901 +       if (pipe_read_char != AP_PERUSER_CHAR_OF_DEATH)
902 +       {
903 +           _DBG("got wrong char %c", pipe_read_char);
904 +           return APR_SUCCESS;
905 +       }
906 +        /* It won the lottery (or something else is very
907 +         * wrong). Embrace death with open arms. */
908 +        die_now = 1;
909 +        _DBG("WATCH: die_now=%d", die_now);
910 +    }
911 +    /* apr_thread_mutex_unlock(pipe_of_death_mutex); */
912 +    return APR_SUCCESS;
913 +}
914 +
915 +static void set_signals(void)
916 +{
917 +#ifndef NO_USE_SIGACTION
918 +    struct sigaction sa;
919 +
920 +    sigemptyset(&sa.sa_mask);
921 +    sa.sa_flags = 0;
922 +
923 +    if (!one_process) {
924 +       sa.sa_handler = sig_coredump;
925 +#if defined(SA_ONESHOT)
926 +       sa.sa_flags = SA_ONESHOT;
927 +#elif defined(SA_RESETHAND)
928 +       sa.sa_flags = SA_RESETHAND;
929 +#endif
930 +       if (sigaction(SIGSEGV, &sa, NULL) < 0)
931 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGSEGV)");
932 +#ifdef SIGBUS
933 +       if (sigaction(SIGBUS, &sa, NULL) < 0)
934 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGBUS)");
935 +#endif
936 +#ifdef SIGABORT
937 +       if (sigaction(SIGABORT, &sa, NULL) < 0)
938 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABORT)");
939 +#endif
940 +#ifdef SIGABRT
941 +       if (sigaction(SIGABRT, &sa, NULL) < 0)
942 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGABRT)");
943 +#endif
944 +#ifdef SIGILL
945 +       if (sigaction(SIGILL, &sa, NULL) < 0)
946 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGILL)");
947 +#endif
948 +       sa.sa_flags = 0;
949 +    }
950 +    sa.sa_handler = sig_term;
951 +    if (sigaction(SIGTERM, &sa, NULL) < 0)
952 +       ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGTERM)");
953 +#ifdef SIGINT
954 +    if (sigaction(SIGINT, &sa, NULL) < 0)
955 +        ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGINT)");
956 +#endif
957 +#ifdef SIGXCPU
958 +    sa.sa_handler = SIG_DFL;
959 +    if (sigaction(SIGXCPU, &sa, NULL) < 0)
960 +       ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXCPU)");
961 +#endif
962 +#ifdef SIGXFSZ
963 +    sa.sa_handler = SIG_IGN;
964 +    if (sigaction(SIGXFSZ, &sa, NULL) < 0)
965 +       ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGXFSZ)");
966 +#endif
967 +#ifdef SIGPIPE
968 +    sa.sa_handler = SIG_IGN;
969 +    if (sigaction(SIGPIPE, &sa, NULL) < 0)
970 +       ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGPIPE)");
971 +#endif
972 +
973 +    /* we want to ignore HUPs and AP_SIG_GRACEFUL while we're busy 
974 +     * processing one */
975 +    sigaddset(&sa.sa_mask, SIGHUP);
976 +    sigaddset(&sa.sa_mask, AP_SIG_GRACEFUL);
977 +    sa.sa_handler = restart;
978 +    if (sigaction(SIGHUP, &sa, NULL) < 0)
979 +       ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(SIGHUP)");
980 +    if (sigaction(AP_SIG_GRACEFUL, &sa, NULL) < 0)
981 +        ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "sigaction(" AP_SIG_GRACEFUL_STRING ")");
982 +#else
983 +    if (!one_process) {
984 +       apr_signal(SIGSEGV, sig_coredump);
985 +#ifdef SIGBUS
986 +       apr_signal(SIGBUS, sig_coredump);
987 +#endif /* SIGBUS */
988 +#ifdef SIGABORT
989 +       apr_signal(SIGABORT, sig_coredump);
990 +#endif /* SIGABORT */
991 +#ifdef SIGABRT
992 +       apr_signal(SIGABRT, sig_coredump);
993 +#endif /* SIGABRT */
994 +#ifdef SIGILL
995 +       apr_signal(SIGILL, sig_coredump);
996 +#endif /* SIGILL */
997 +#ifdef SIGXCPU
998 +       apr_signal(SIGXCPU, SIG_DFL);
999 +#endif /* SIGXCPU */
1000 +#ifdef SIGXFSZ
1001 +       apr_signal(SIGXFSZ, SIG_DFL);
1002 +#endif /* SIGXFSZ */
1003 +    }
1004 +
1005 +    apr_signal(SIGTERM, sig_term);
1006 +#ifdef SIGHUP
1007 +    apr_signal(SIGHUP, restart);
1008 +#endif /* SIGHUP */
1009 +#ifdef AP_SIG_GRACEFUL
1010 +    apr_signal(AP_SIG_GRACEFUL, restart);
1011 +#endif /* AP_SIG_GRACEFUL */
1012 +#ifdef SIGPIPE
1013 +    apr_signal(SIGPIPE, SIG_IGN);
1014 +#endif /* SIGPIPE */
1015 +
1016 +#endif
1017 +}
1018 +
1019 +/*****************************************************************
1020 + * Child process main loop.
1021 + * The following vars are static to avoid getting clobbered by longjmp();
1022 + * they are really private to child_main.
1023 + */
1024 +
1025 +static int requests_this_child;
1026 +static int num_listensocks = 0;
1027 +static ap_listen_rec *listensocks;
1028 +
1029 +int ap_graceful_stop_signalled(void)
1030 +{
1031 +    /* not ever called anymore... */
1032 +    return 0;
1033 +}
1034 +
1035 +static void process_socket(apr_pool_t *p, apr_socket_t *sock, long conn_id,
1036 +                           apr_bucket_alloc_t *bucket_alloc)
1037 +{
1038 +    conn_rec *current_conn;
1039 +    int sock_fd;
1040 +    apr_status_t rv;
1041 +    ap_sb_handle_t *sbh;
1042 +
1043 +    if ((rv = apr_os_sock_get(&sock_fd, sock)) != APR_SUCCESS)
1044 +    {
1045 +        ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, "apr_os_sock_get");
1046 +    }
1047 +
1048 +    _DBG("child_num=%d sock=%ld sock_fd=%d\n", my_child_num, sock, sock_fd);
1049 +
1050 +    switch (CHILD_INFO_TABLE[my_child_num].type)
1051 +    {
1052 +        case CHILD_TYPE_MULTIPLEXER:
1053 +            _DBG("type=MULTIPLEXER %d", my_child_num);
1054 +            break;
1055 +        case CHILD_TYPE_PROCESSOR:
1056 +            _DBG("type=PROCESSOR %d", my_child_num);
1057 +            break;
1058 +        default:
1059 +            _DBG("type UNKNOWN! %d", my_child_num);
1060 +    }
1061 +
1062 +    if (sock_fd >= FD_SETSIZE)
1063 +    {
1064 +        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL,
1065 +                     "new file descriptor %d is too large; you probably need "
1066 +                     "to rebuild Apache with a larger FD_SETSIZE "
1067 +                     "(currently %d)",
1068 +                     sock_fd, FD_SETSIZE);
1069 +        apr_socket_close(sock);
1070 +        _DBG("child_num=%d: exiting with error", my_child_num);
1071 +        return;
1072 +    }
1073 +
1074 +    if (CHILD_INFO_TABLE[my_child_num].sock_fd < 0)
1075 +    {
1076 +        ap_sock_disable_nagle(sock);
1077 +    }
1078 +
1079 +    ap_create_sb_handle(&sbh, p, conn_id, 0);
1080 +    current_conn = ap_run_create_connection(p, ap_server_conf, sock, conn_id,
1081 +                                            sbh, bucket_alloc);
1082 +
1083 +    if (current_conn)
1084 +    {
1085 +        ap_process_connection(current_conn, sock);
1086 +        ap_lingering_close(current_conn);
1087 +    }
1088 +}
1089 +
1090 +static int peruser_process_connection(conn_rec *conn)
1091 +{
1092 +    ap_filter_t *filter;
1093 +    apr_bucket_brigade *bb;
1094 +    core_net_rec *net;
1095 +
1096 +    _DBG("function entered",0);
1097 +
1098 +    /* -- fetch our sockets from the pool -- */
1099 +    apr_pool_userdata_get((void **)&bb, "PERUSER_SOCKETS", conn->pool);
1100 +    if (bb != NULL)
1101 +    {
1102 +        /* -- find the 'core' filter and give the socket data to it -- */
1103 +        for (filter = conn->output_filters; filter != NULL; filter = filter->next)
1104 +        {
1105 +            if (!strcmp(filter->frec->name, "core")) break;
1106 +        }
1107 +        if (filter != NULL)
1108 +        {
1109 +            net = filter->ctx;
1110 +            net->in_ctx = apr_palloc(conn->pool, sizeof(*net->in_ctx));
1111 +            net->in_ctx->b = bb;
1112 +        }
1113 +    }
1114 +    _DBG("leaving (DECLINED)", 0);
1115 +    return DECLINED;
1116 +}
1117 +
1118 +static int total_processors(peruser_server_conf* sconf)
1119 +{
1120 +    int i, total;
1121 +
1122 +    for(i = 0, total = 0; i < NUM_CHILDS; ++i)
1123 +    {
1124 +        if(CHILD_INFO_TABLE[i].uid == sconf->uid &&
1125 +           CHILD_INFO_TABLE[i].gid == sconf->gid &&
1126 +           (!sconf->chroot || !strcmp(CHILD_INFO_TABLE[i].chroot, sconf->chroot)))
1127 +        {
1128 +            total++;
1129 +        }
1130 +    }
1131 +
1132 +    return total;
1133 +}
1134 +
1135 +static int idle_processors(peruser_server_conf* sconf)
1136 +{
1137 +    int i, total;
1138 +
1139 +    for(i = 0, total = 0; i < NUM_CHILDS; ++i)
1140 +    {
1141 +        if(CHILD_INFO_TABLE[i].uid == sconf->uid &&
1142 +           CHILD_INFO_TABLE[i].gid == sconf->gid &&
1143 +           (!sconf->chroot || !strcmp(CHILD_INFO_TABLE[i].chroot, sconf->chroot)) &&
1144 +           ap_scoreboard_image->servers[i][0].status == SERVER_READY)
1145 +        {
1146 +            total++;
1147 +        }
1148 +    }
1149 +
1150 +    return total;
1151 +}
1152 +
1153 +static child_info_t* find_next_multiplexer()
1154 +{
1155 +    int i;
1156 +
1157 +    _DBG("function entered", 0);
1158 +
1159 +    for(i = 0; i < NUM_CHILDS; ++i)
1160 +    {
1161 +        if(CHILD_INFO_TABLE[i].type == CHILD_TYPE_MULTIPLEXER)
1162 +            return &CHILD_INFO_TABLE[i];
1163 +    }
1164 +
1165 +    return NULL;
1166 +}
1167 +
1168 +static child_info_t* find_next_processor(peruser_server_conf* sconf)
1169 +{
1170 +    int i, match;
1171 +
1172 +    _DBG("function entered", 0);
1173 +    _DBG("uid=%d gid=%d chroot=%s", sconf->uid, sconf->gid, sconf->chroot);
1174 +
1175 +    match = -1;
1176 +
1177 +    for(i = 0; i < NUM_CHILDS; i++)
1178 +    {
1179 +        if(CHILD_INFO_TABLE[i].type == CHILD_TYPE_PROCESSOR &&
1180 +           CHILD_INFO_TABLE[i].uid == sconf->uid &&
1181 +           CHILD_INFO_TABLE[i].gid == sconf->gid &&
1182 +           (!sconf->chroot || !strcmp(CHILD_INFO_TABLE[i].chroot, sconf->chroot)) &&
1183 +           (CHILD_INFO_TABLE[i].status == CHILD_STATUS_STANDBY ||
1184 +           CHILD_INFO_TABLE[i].status == CHILD_STATUS_ACTIVE))
1185 +        {
1186 +            _DBG("checking child #%d (%d, %d, %s)", i,
1187 +                CHILD_INFO_TABLE[i].uid,
1188 +                CHILD_INFO_TABLE[i].gid,
1189 +                CHILD_INFO_TABLE[i].chroot);
1190 +
1191 +            if(CHILD_INFO_TABLE[i].status == CHILD_STATUS_STANDBY)
1192 +                return &CHILD_INFO_TABLE[i];
1193 +
1194 +            if(match == -1)
1195 +            {
1196 +                match = i;
1197 +                continue;
1198 +            }
1199 +
1200 +            /* the plan: pick the processor that's been idle the longest.
1201 +               prefer ones in the SERVER_READY state */
1202 +
1203 +            if(ap_scoreboard_image->servers[match][0].status != SERVER_READY && 
1204 +               ap_scoreboard_image->servers[i][0].status == SERVER_READY)
1205 +                match = i;
1206 +
1207 +            if(ap_scoreboard_image->servers[i][0].status ==
1208 +               ap_scoreboard_image->servers[match][0].status &&
1209 +               ap_scoreboard_image->servers[i][0].last_used <
1210 +               ap_scoreboard_image->servers[match][0].last_used)
1211 +                match = i;
1212 +        }
1213 +    }
1214 +
1215 +    if(match == -1) return NULL;
1216 +    return &CHILD_INFO_TABLE[match];
1217 +}
1218 +
1219 +static int pass_request(request_rec *r, child_info_t *processor)
1220 +{
1221 +    int rv;
1222 +    struct msghdr msg;
1223 +    struct cmsghdr *cmsg;
1224 +    int sock_fd;
1225 +    char *bodypart = NULL;
1226 +    struct iovec iov[2];
1227 +    conn_rec *c = r->connection;
1228 +    apr_bucket_brigade *bb = apr_brigade_create(r->pool, c->bucket_alloc);
1229 +    apr_bucket_brigade *bodypart_bb = NULL;
1230 +    apr_size_t len = 0;
1231 +    apr_size_t bodypart_len = 0;
1232 +    peruser_header h;
1233 +    apr_bucket *bucket;
1234 +    const apr_array_header_t *headers_in_array;
1235 +    const apr_table_entry_t *headers_in;
1236 +    int counter;
1237 +
1238 +    apr_socket_t *thesock = ap_get_module_config(r->connection->conn_config, &core_module);
1239 +    peruser_server_conf *sconf = PERUSER_SERVER_CONF(r->server->module_config);
1240 +
1241 +    if ((!r->the_request) || (!strlen(r->the_request)))
1242 +    {
1243 +        _DBG("empty request. dropping it (%ld)", r->the_request);
1244 +        return -1;
1245 +    }
1246 +
1247 +    if (!processor)
1248 +    {
1249 +        _DBG("server %s in child %d has no child_info associated",
1250 +                r->hostname, my_child_num);
1251 +        return -1;
1252 +    }
1253 +
1254 +    _DBG("passing request to another child.  Vhost: %s, child %d %d",
1255 +      apr_table_get(r->headers_in, "Host"), my_child_num, processor->output);
1256 +    _DBG("r->the_request=\"%s\" len=%d", r->the_request, strlen(r->the_request));
1257 +
1258 +    ap_get_brigade(r->connection->input_filters, bb, AP_MODE_EXHAUSTIVE, APR_NONBLOCK_READ, len);
1259 +
1260 +    /* Scan the brigade looking for heap-buckets */
1261 +
1262 +    _DBG("Scanning the brigade",0);
1263 +    bucket = APR_BRIGADE_FIRST(bb);
1264 +    while (bucket != APR_BRIGADE_SENTINEL(bb) &&
1265 +           APR_BUCKET_IS_HEAP(bucket)) {
1266 +       _DBG("HEAP BUCKET is found, length=%d", bucket->length);
1267 +        bucket = APR_BUCKET_NEXT(bucket);
1268 +        if (!APR_BUCKET_IS_HEAP(bucket)) {
1269 +            _DBG("NON-HEAP BUCKET is found, extracting the part of brigade before it",0);
1270 +            bodypart_bb = bb;
1271 +            bb = apr_brigade_split(bodypart_bb, bucket);
1272 +            /* Do we need to apr_destroy_brigade(bb) here?
1273 +             * Yeah, I know we do apr_pool_destroy(r->pool) before return, but
1274 +             * ap_get_brigade is in non-blocking mode (however len is zero).
1275 +             */
1276 +            if (apr_brigade_pflatten(bodypart_bb, &bodypart, &bodypart_len, r->pool) != APR_SUCCESS) {
1277 +                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1278 +                     "Unable to flatten brigade, declining request");
1279 +                apr_pool_destroy(r->pool);
1280 +                return DECLINED;
1281 +            }
1282 +            _DBG("Brigade is flattened as bodypart (bodypart_len=%d)", bodypart_len);
1283 +        }
1284 +    }
1285 +    _DBG("Scanning is finished",0);
1286 +
1287 +    apr_os_sock_get(&sock_fd, thesock);
1288 +
1289 +    h.p = r->pool;
1290 +
1291 +    headers_in_array = apr_table_elts(r->headers_in);
1292 +    headers_in = (const apr_table_entry_t *) headers_in_array->elts;
1293 +
1294 +    h.headers = apr_pstrcat(h.p, r->the_request, CRLF, NULL);
1295 +    for (counter = 0; counter < headers_in_array->nelts; counter++) {
1296 +        if (headers_in[counter].key == NULL
1297 +         || headers_in[counter].val == NULL) {
1298 +             continue;
1299 +         }
1300 +         h.headers = apr_pstrcat(h.p, h.headers, headers_in[counter].key, ": ",
1301 +                                 headers_in[counter].val, CRLF, NULL);
1302 +
1303 +    }
1304 +    h.headers = apr_pstrcat(h.p, h.headers, CRLF, NULL);
1305 +    ap_xlate_proto_to_ascii(h.headers, strlen(h.headers));
1306 +
1307 +    iov[0].iov_base = &bodypart_len;
1308 +    iov[0].iov_len  = sizeof(bodypart_len);
1309 +    iov[1].iov_base = h.headers;
1310 +    iov[1].iov_len  = strlen(h.headers) + 1;
1311 +
1312 +    msg.msg_name    = NULL;
1313 +    msg.msg_namelen = 0;
1314 +    msg.msg_iov     = iov;
1315 +    msg.msg_iovlen  = 2;
1316 +
1317 +    cmsg = apr_palloc(r->pool, sizeof(*cmsg) + sizeof(sock_fd));
1318 +    cmsg->cmsg_len   = sizeof(*cmsg) + sizeof(sock_fd);
1319 +    cmsg->cmsg_level = SOL_SOCKET;
1320 +    cmsg->cmsg_type  = SCM_RIGHTS;
1321 +
1322 +    memcpy(CMSG_DATA(cmsg), &sock_fd, sizeof(sock_fd));
1323 +
1324 +    msg.msg_control    = cmsg;
1325 +    msg.msg_controllen = cmsg->cmsg_len;
1326 +
1327 +
1328 +    if (processor->status == CHILD_STATUS_STANDBY)
1329 +    {
1330 +        _DBG("Activating child #%d", processor->id);
1331 +        processor->status = CHILD_STATUS_STARTING;
1332 +    }
1333 +
1334 +    while (processor->status != CHILD_STATUS_ACTIVE)
1335 +    {
1336 +        _DBG("Waiting for child #%d...", processor->id);
1337 +        sleep(1);
1338 +    }
1339 +
1340 +    _DBG("Writing message to %d, passing sock_fd:  %d", processor->output, sock_fd);
1341 +    _DBG("headers=\"%s\"", h.headers);
1342 +    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1343 +                 "Writing message to %d, passing sd:  %d", processor->output, sock_fd);
1344 +
1345 +    if ((rv = sendmsg(processor->output, &msg, 0)) == -1)
1346 +    {
1347 +        apr_pool_destroy(r->pool);
1348 +        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1349 +                     "Writing message failed %d %d", rv, errno);
1350 +        return -1;
1351 +    }
1352 +
1353 +    _DBG("Writing message succeeded %d", rv);
1354 +    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1355 +                 "Writing message succeeded %d", rv);
1356 +
1357 +    if (bodypart_len) {
1358 +        _DBG("Passing bodypart (bodypart_len=%d) \"%s\"", bodypart_len, bodypart);
1359 +         if ((rv = send(processor->output, bodypart, bodypart_len, 0)) == -1) {
1360 +             apr_pool_destroy(r->pool);
1361 +             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1362 +                          "Writing message failed %d %d", rv, errno);
1363 +             return -1;
1364 +         }
1365 +        _DBG("Writing message succeeded %d", rv);
1366 +        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
1367 +                     "Writing message succeeded %d", rv);
1368 +    } else {
1369 +        _DBG("No bodypart",0);
1370 +    }
1371 +
1372 +    /* -- close the socket at our site -- */
1373 +    apr_socket_close(thesock);
1374 +    /* DS: What is the purpose of this?
1375 +     *     apr_socket_close(processor->output) seems more reasonable
1376 +     */
1377 +    _DBG("closing socket %d at our side", sock_fd);
1378 +    apr_pool_destroy(r->pool);
1379 +    return 1;
1380 +}
1381 +
1382 +
1383 +static apr_status_t receive_from_multiplexer(
1384 +    void **trans_sock,  /* will be filled out w/ the received socket */
1385 +    ap_listen_rec *lr,  /* listener to receive from */
1386 +    apr_pool_t *ptrans  /* transaction wide pool */
1387 +)
1388 +{
1389 +    struct msghdr msg;
1390 +    struct cmsghdr *cmsg;
1391 +    char headers[HUGE_STRING_LEN];
1392 +    char *bodypart;
1393 +    apr_size_t bodypart_len;
1394 +    struct iovec iov[2];
1395 +    int ret, fd_tmp;
1396 +    apr_os_sock_t ctrl_sock_fd;
1397 +    apr_os_sock_t trans_sock_fd;
1398 +
1399 +    /* -- bucket's, brigades and their allocators */
1400 +    apr_bucket_alloc_t *alloc = apr_bucket_alloc_create(ptrans);
1401 +    apr_bucket_brigade *bb    = apr_brigade_create(ptrans, alloc);
1402 +    apr_bucket         *bucket;
1403 +
1404 +    /* prepare the buffers for receiving data from remote side */
1405 +    iov[0].iov_base = &bodypart_len;
1406 +    iov[0].iov_len  = sizeof(bodypart_len);
1407 +    iov[1].iov_base = headers;
1408 +    iov[1].iov_len  = HUGE_STRING_LEN;
1409 +
1410 +    cmsg = apr_palloc(ptrans, sizeof(*cmsg) + sizeof(trans_sock_fd));
1411 +    cmsg->cmsg_len = sizeof(*cmsg) + sizeof(trans_sock_fd);
1412 +
1413 +    msg.msg_name       = NULL;
1414 +    msg.msg_namelen    = 0;
1415 +    msg.msg_iov        = iov;
1416 +    msg.msg_iovlen     = 2;
1417 +    msg.msg_control    = cmsg;
1418 +    msg.msg_controllen = cmsg->cmsg_len;
1419 +
1420 +    /* -- receive data from socket -- */
1421 +    apr_os_sock_get(&ctrl_sock_fd, lr->sd);
1422 +    _DBG("receiving from sock_fd=%d", ctrl_sock_fd);
1423 +    ret = recvmsg(ctrl_sock_fd, &msg, 0);
1424 +
1425 +    /* -- extract socket from the cmsg -- */
1426 +    memcpy(&trans_sock_fd, CMSG_DATA(cmsg), sizeof(trans_sock_fd));
1427 +    apr_os_sock_put((apr_socket_t **)trans_sock, &trans_sock_fd, ptrans);
1428 +    apr_os_sock_get(&fd_tmp, *trans_sock);
1429 +
1430 +
1431 +    _DBG("trans_sock=%ld fdx=%d sock_fd=%d",
1432 +         *trans_sock, trans_sock_fd, fd_tmp);
1433 +
1434 +    _DBG("headers=\"%s\"", headers);
1435 +
1436 +    /* -- store received data into an brigade and add
1437 +          it to the current transaction's pool -- */
1438 +    bucket = apr_bucket_eos_create(alloc);
1439 +    APR_BRIGADE_INSERT_HEAD(bb, bucket);
1440 +    bucket = apr_bucket_socket_create(*trans_sock, alloc);
1441 +    APR_BRIGADE_INSERT_HEAD(bb, bucket);
1442 +
1443 +    /* Receive the bodypart (if there is one) and insert it in the brigade */
1444 +    if (bodypart_len) {
1445 +        _DBG("Receiving the bodypart (bodypart_len=%d)", bodypart_len);
1446 +        bodypart = apr_palloc(ptrans, bodypart_len);
1447 +        ret = recv(ctrl_sock_fd, bodypart, HUGE_STRING_LEN, 0);
1448 +
1449 +        _DBG("bodypart=\"%s\"", bodypart);
1450 +        bucket = apr_bucket_heap_create(bodypart,
1451 +                                        bodypart_len, NULL, alloc);
1452 +        APR_BRIGADE_INSERT_HEAD(bb, bucket);
1453 +    } else {
1454 +        _DBG("There is no bodypart",0);
1455 +    }
1456 +
1457 +    bucket = apr_bucket_heap_create(headers,
1458 +                                    strlen(headers), NULL, alloc);
1459 +    /* DS: I really don't like that strlen here */
1460 +
1461 +    APR_BRIGADE_INSERT_HEAD(bb, bucket);
1462 +    apr_pool_userdata_set(bb, "PERUSER_SOCKETS", NULL, ptrans);
1463 +
1464 +    _DBG("returning 0", 0);
1465 +    return 0;
1466 +}
1467 +
1468 +
1469 +/* Set group privileges.
1470 + *
1471 + * Note that we use the username as set in the config files, rather than
1472 + * the lookup of to uid --- the same uid may have multiple passwd entries,
1473 + * with different sets of groups for each.
1474 + */
1475 +
1476 +static int set_group_privs(uid_t uid, gid_t gid)
1477 +{
1478 +    if (!geteuid())
1479 +    {
1480 +        const char *name;
1481 +
1482 +        /* Get username if passed as a uid */
1483 +
1484 +        struct passwd *ent;
1485 +
1486 +        if ((ent = getpwuid(uid)) == NULL)
1487 +        {
1488 +            ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1489 +                         "getpwuid: couldn't determine user name from uid %u, "
1490 +                         "you probably need to modify the User directive",
1491 +                         (unsigned)uid);
1492 +            return -1;
1493 +        }
1494 +
1495 +        name = ent->pw_name;
1496 +
1497 +        /*
1498 +         * Set the GID before initgroups(), since on some platforms
1499 +         * setgid() is known to zap the group list.
1500 +         */
1501 +        if (setgid(gid) == -1)
1502 +        {
1503 +            ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1504 +                         "setgid: unable to set group id to Group %u",
1505 +                         (unsigned)gid);
1506 +            return -1;
1507 +        }
1508 +
1509 +        /* Reset `groups' attributes. */
1510 +
1511 +        if (initgroups(name, gid) == -1)
1512 +        {
1513 +                    ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1514 +                         "initgroups: unable to set groups for User %s "
1515 +                         "and Group %u", name, (unsigned)gid);
1516 +            return -1;
1517 +        }
1518 +    }
1519 +    return 0;
1520 +}
1521 +
1522 +static int peruser_setup_child(int childnum)
1523 +{
1524 +    child_info_t *ug = &CHILD_INFO_TABLE[childnum];
1525 +
1526 +    if (ug->uid == -1 && ug->gid == -1) {
1527 +        return unixd_setup_child();
1528 +    }
1529 +    if (set_group_privs(ug->uid, ug->gid)) {
1530 +        return -1;
1531 +    }
1532 +    /* Only try to switch if we're running as root */
1533 +    if (!geteuid()
1534 +        && (
1535 +#ifdef _OSD_POSIX
1536 +            os_init_job_environment(ap_server_conf, unixd_config.user_name,
1537 +                                    one_process) != 0 ||
1538 +#endif
1539 +            setuid(ug->uid) == -1)) {
1540 +        ap_log_error(APLOG_MARK, APLOG_ALERT, errno, NULL,
1541 +                     "setuid: unable to change to uid: %ld",
1542 +                     (long) ug->uid);
1543 +        return -1;
1544 +    }
1545 +    return 0;
1546 +}
1547 +
1548 +static int check_signal(int signum)
1549 +{
1550 +    _DBG("signum=%d", signum);
1551 +    switch (signum) {
1552 +    case SIGTERM:
1553 +    case SIGINT:
1554 +        just_die(signum);
1555 +        return 1;
1556 +    }
1557 +    return 0;
1558 +}
1559 +
1560 +/* Send a single HTTP header field to the client.  Note that this function
1561 + * is used in calls to table_do(), so their interfaces are co-dependent.
1562 + * In other words, don't change this one without checking table_do in alloc.c.
1563 + * It returns true unless there was a write error of some kind.
1564 + */
1565 +static int peruser_header_field(peruser_header *h,
1566 +                             const char *fieldname, const char *fieldval)
1567 +{
1568 +    apr_pstrcat(h->p, h->headers, fieldname, ": ", fieldval, CRLF, NULL);
1569 +    return 1;
1570 +}
1571 +
1572 +static inline ap_listen_rec* listen_add(apr_pool_t* pool, apr_socket_t *sock, void* accept_func)
1573 +{
1574 +    ap_listen_rec *lr_walk, *lr_new;
1575 +
1576 +    _DBG("function entered", 0);
1577 +    /* -- create an new listener for this child -- */
1578 +    lr_new = apr_palloc(pool, sizeof(*lr_new));
1579 +    lr_new->sd          = sock;
1580 +    lr_new->active      = 1;
1581 +    lr_new->accept_func = accept_func;
1582 +    lr_new->next        = NULL;
1583 +
1584 +    /* -- add the new listener_rec into the list -- */
1585 +    /* FIXME: should we somehow lock this list ? */
1586 +    if ((lr_walk = ap_listeners))
1587 +    {
1588 +        while (lr_walk->next) lr_walk = lr_walk->next;
1589 +        lr_walk->next = lr_new;
1590 +    }
1591 +    else
1592 +    {
1593 +        ap_listeners = lr_walk = lr_new;
1594 +    }
1595 +    num_listensocks++;
1596 +    return lr_new;
1597 +}
1598 +
1599 +static inline void listen_clear()
1600 +{
1601 +    ap_listen_rec *lr_walk;
1602 +
1603 +    _DBG("function entered", 0);
1604 +
1605 +    /* FIXME: should we somehow lock this list ? */
1606 +    while (ap_listeners)
1607 +    {
1608 +        lr_walk = ap_listeners->next;
1609 +        apr_socket_close(ap_listeners->sd);
1610 +        ap_listeners = lr_walk;
1611 +    }
1612 +    num_listensocks=0;
1613 +}
1614 +
1615 +apr_status_t cleanup_child_info(void *d)
1616 +{
1617 +    if (child_info_image == NULL) {
1618 +        return APR_SUCCESS;
1619 +    }
1620 +
1621 +    free(child_info_image);
1622 +    child_info_image = NULL;
1623 +    apr_shm_destroy(child_info_shm);
1624 +
1625 +    return APR_SUCCESS;
1626 +}
1627 +
1628 +static void child_main(int child_num_arg)
1629 +{
1630 +    apr_pool_t *ptrans;
1631 +    apr_allocator_t *allocator;
1632 +    conn_rec *current_conn;
1633 +    apr_status_t status = APR_EINIT;
1634 +    int i;
1635 +    ap_listen_rec *lr;
1636 +    int curr_pollfd, last_pollfd = 0;
1637 +    apr_pollfd_t *pollset;
1638 +    int offset;
1639 +    ap_sb_handle_t *sbh;
1640 +    apr_status_t rv;
1641 +    apr_bucket_alloc_t *bucket_alloc;
1642 +    int fd;
1643 +    void* sock;
1644 +    void* pod_sock;
1645 +    void* shmem;
1646 +
1647 +    my_child_num = child_num_arg;
1648 +    ap_my_pid = getpid();
1649 +    requests_this_child = 0;
1650 +
1651 +    _DBG("sock_fd_in=%d sock_fd_out=%d",
1652 +         CHILD_INFO_TABLE[my_child_num].input,
1653 +         CHILD_INFO_TABLE[my_child_num].output);
1654 +
1655 +    /* Get a sub context for global allocations in this child, so that
1656 +     * we can have cleanups occur when the child exits.
1657 +     */
1658 +    apr_allocator_create(&allocator);
1659 +    apr_allocator_max_free_set(allocator, ap_max_mem_free);
1660 +    apr_pool_create_ex(&pchild, pconf, NULL, allocator);
1661 +    apr_allocator_owner_set(allocator, pchild);
1662 +
1663 +    apr_pool_create(&ptrans, pchild);
1664 +    apr_pool_tag(ptrans, "transaction");
1665 +
1666 +    /* needs to be done before we switch UIDs so we have permissions */
1667 +    ap_reopen_scoreboard(pchild, NULL, 0);
1668 +
1669 +    rv = apr_proc_mutex_child_init(&accept_mutex, ap_lock_fname, pchild);
1670 +    if (rv != APR_SUCCESS) {
1671 +        ap_log_error(APLOG_MARK, APLOG_EMERG, rv, ap_server_conf,
1672 +                     "Couldn't initialize cross-process lock in child");
1673 +        clean_child_exit(APEXIT_CHILDFATAL);
1674 +    }
1675 +
1676 +    if(CHILD_INFO_TABLE[my_child_num].chroot) {
1677 +      _DBG("chroot to %s", CHILD_INFO_TABLE[my_child_num].chroot);
1678 +      if(chroot(CHILD_INFO_TABLE[my_child_num].chroot)) {
1679 +        _DBG("chroot failure %s", CHILD_INFO_TABLE[my_child_num].chroot);
1680 +        return;
1681 +      }
1682 +    }
1683 +
1684 +    switch(CHILD_INFO_TABLE[my_child_num].type)
1685 +    {
1686 +        case CHILD_TYPE_MULTIPLEXER:
1687 +            _DBG("MULTIPLEXER %d", my_child_num);
1688 +
1689 +            /* update status on processors that are ready to accept requests */
1690 +            _DBG("updating processor stati", 0);
1691 +            for(i = 0; i < NUM_CHILDS; ++i)
1692 +            {
1693 +                if(CHILD_INFO_TABLE[i].status == CHILD_STATUS_READY)
1694 +                    CHILD_INFO_TABLE[i].status = CHILD_STATUS_ACTIVE;
1695 +            }
1696 +
1697 +            break;
1698 +
1699 +        case CHILD_TYPE_PROCESSOR:
1700 +            _DBG("PROCESSOR %d", my_child_num);
1701 +
1702 +            /* -- create new listener to receive from multiplexer -- */
1703 +            apr_os_sock_put((void*)&sock, &CHILD_INFO_TABLE[my_child_num].input, pconf);
1704 +            listen_clear();
1705 +            listen_add(pconf, sock, receive_from_multiplexer);
1706 +
1707 +            break;
1708 +
1709 +        default:
1710 +            _DBG("unspecified child type for %d sleeping a while ...", my_child_num);
1711 +            sleep(5);
1712 +            return;
1713 +    }
1714 +
1715 +    apr_os_file_get(&fd, pipe_of_death_in);
1716 +    apr_os_sock_put((void*)&pod_sock, &fd, pconf);
1717 +    listen_add(pconf, pod_sock, check_pipe_of_death);
1718 +
1719 +    (peruser_setup_child(my_child_num) && clean_child_exit(APEXIT_CHILDFATAL));
1720 +    ap_run_child_init(pchild, ap_server_conf);
1721 +
1722 +    ap_create_sb_handle(&sbh, pchild, my_child_num, 0);
1723 +    (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
1724 +
1725 +    /* Set up the pollfd array */
1726 +    listensocks = apr_pcalloc(pchild,
1727 +                            sizeof(*listensocks) * (num_listensocks));
1728 +    for (lr = ap_listeners, i = 0; i < num_listensocks; lr = lr->next, i++) {
1729 +        listensocks[i].accept_func = lr->accept_func;
1730 +        listensocks[i].sd = lr->sd;
1731 +    }
1732 +
1733 +    pollset = apr_palloc(pchild, sizeof(*pollset) * num_listensocks);
1734 +    pollset[0].p = pchild;
1735 +    for (i = 0; i < num_listensocks; i++) {
1736 +        pollset[i].desc.s = listensocks[i].sd;
1737 +        pollset[i].desc_type = APR_POLL_SOCKET;
1738 +        pollset[i].reqevents = APR_POLLIN;
1739 +    }
1740 +
1741 +    bucket_alloc = apr_bucket_alloc_create(pchild);
1742 +
1743 +    while (!die_now) {
1744 +       /*
1745 +        * (Re)initialize this child to a pre-connection state.
1746 +        */
1747 +
1748 +       current_conn = NULL;
1749 +
1750 +       apr_pool_clear(ptrans);
1751 +
1752 +       if ((ap_max_requests_per_child > 0
1753 +            && requests_this_child++ >= ap_max_requests_per_child)) {
1754 +            _DBG("max requests reached, dying now", 0);
1755 +           clean_child_exit(0);
1756 +       }
1757 +
1758 +       (void) ap_update_child_status(sbh, SERVER_READY, (request_rec *) NULL);
1759 +
1760 +       /*
1761 +        * Wait for an acceptable connection to arrive.
1762 +        */
1763 +
1764 +       /* Lock around "accept", if necessary */
1765 +       SAFE_ACCEPT(accept_mutex_on());
1766 +
1767 +        if (num_listensocks == 1) {
1768 +            offset = 0;
1769 +        }
1770 +        else {
1771 +            /* multiple listening sockets - need to poll */
1772 +           for (;;) {
1773 +                apr_status_t ret;
1774 +                apr_int32_t n;
1775 +
1776 +                ret = apr_poll(pollset, num_listensocks, &n, -1);
1777 +                if (ret != APR_SUCCESS) {
1778 +                    if (APR_STATUS_IS_EINTR(ret)) {
1779 +                        continue;
1780 +                    }
1781 +                   /* Single Unix documents select as returning errnos
1782 +                    * EBADF, EINTR, and EINVAL... and in none of those
1783 +                    * cases does it make sense to continue.  In fact
1784 +                    * on Linux 2.0.x we seem to end up with EFAULT
1785 +                    * occasionally, and we'd loop forever due to it.
1786 +                    */
1787 +                   ap_log_error(APLOG_MARK, APLOG_ERR, ret, ap_server_conf,
1788 +                             "apr_poll: (listen)");
1789 +                   clean_child_exit(1);
1790 +                }
1791 +                /* find a listener */
1792 +                curr_pollfd = last_pollfd;
1793 +                do {
1794 +                    curr_pollfd++;
1795 +                    if (curr_pollfd >= num_listensocks) {
1796 +                        curr_pollfd = 0;
1797 +                    }
1798 +                    /* XXX: Should we check for POLLERR? */
1799 +                    if (pollset[curr_pollfd].rtnevents & APR_POLLIN) {
1800 +                        last_pollfd = curr_pollfd;
1801 +                        offset = curr_pollfd;
1802 +                        goto got_fd;
1803 +                    }
1804 +                } while (curr_pollfd != last_pollfd);
1805 +
1806 +                continue;
1807 +            }
1808 +        }
1809 +    got_fd:
1810 +        _DBG("input available ... resetting socket.",0);
1811 +        sock = NULL;    /* important! */
1812 +
1813 +       /* if we accept() something we don't want to die, so we have to
1814 +        * defer the exit
1815 +        */
1816 +        status = listensocks[offset].accept_func(&sock, &listensocks[offset], ptrans);
1817 +        SAFE_ACCEPT(accept_mutex_off());       /* unlock after "accept" */
1818 +
1819 +        if (status == APR_EGENERAL) {
1820 +            /* resource shortage or should-not-occur occured */
1821 +            clean_child_exit(1);
1822 +        }
1823 +        else if (status != APR_SUCCESS) {
1824 +            continue;
1825 +        }
1826 +
1827 +        if (!setjmp(CHILD_INFO_TABLE[my_child_num].jmpbuffer))
1828 +        {
1829 +            _DBG("marked jmpbuffer",0);
1830 +            _TRACE_CALL("process_socket()",0);
1831 +            process_socket(ptrans, sock, my_child_num, bucket_alloc);
1832 +            _TRACE_RET("process_socket()",0);
1833 +        }
1834 +        else
1835 +        {
1836 +            _DBG("landed from longjmp",0);
1837 +            CHILD_INFO_TABLE[my_child_num].sock_fd = AP_PERUSER_THISCHILD;
1838 +        }
1839 +
1840 +        /* Check the pod and the generation number after processing a
1841 +         * connection so that we'll go away if a graceful restart occurred
1842 +         * while we were processing the connection or we are the lucky
1843 +         * idle server process that gets to die.
1844 +         */
1845 +        if (ap_mpm_pod_check(pod) == APR_SUCCESS) { /* selected as idle? */
1846 +            _DBG("ap_mpm_pod_check(pod) = APR_SUCCESS; dying now", 0);
1847 +            die_now = 1;
1848 +        }
1849 +        else if (ap_my_generation !=
1850 +                 ap_scoreboard_image->global->running_generation) { /* restart? */
1851 +            /* yeah, this could be non-graceful restart, in which case the
1852 +             * parent will kill us soon enough, but why bother checking?
1853 +             */
1854 +            _DBG("ap_my_generation != ap_scoreboard_image->global->running_generation; dying now", 0);
1855 +            die_now = 1;
1856 +        }
1857 +
1858 +        if(CHILD_INFO_TABLE[my_child_num].status == CHILD_STATUS_RESTART)
1859 +        {
1860 +            _DBG("Multiplexer: restarting", 0);
1861 +            die_now = 1;
1862 +        }
1863 +    }
1864 +    clean_child_exit(0);
1865 +}
1866 +
1867 +static const char* child_clone()
1868 +{
1869 +    child_info_t *this;
1870 +    child_info_t *new;
1871 +
1872 +    _DBG("cloning child #%d from #%d", NUM_CHILDS, my_child_num);
1873 +
1874 +    if(NUM_CHILDS >= server_limit)
1875 +    {
1876 +        return "Trying to use more child ID's than NumServers.  "
1877 +               "Increase NumServers in your config file.";
1878 +    }
1879 +
1880 +    this = &CHILD_INFO_TABLE[my_child_num];
1881 +    new = &CHILD_INFO_TABLE[NUM_CHILDS];
1882 +
1883 +    new->uid = this->uid;
1884 +    new->gid = this->gid;
1885 +    new->type = this->type;
1886 +    new->chroot = this->chroot;
1887 +    new->sock_fd = this->sock_fd;
1888 +    new->status = CHILD_STATUS_STARTING;
1889 +    new->input = -1;
1890 +    new->output = -1;
1891 +
1892 +    child_info_image->control->num_childs++;
1893 +    return NULL;
1894 +}
1895 +
1896 +static const char* child_add(int type, int status,
1897 +                             uid_t uid, gid_t gid, const char* chroot)
1898 +{
1899 +    int socks[2];
1900 +    int l;
1901 +
1902 +    _DBG("adding child #%d", NUM_CHILDS);
1903 +
1904 +    if(NUM_CHILDS >= server_limit)
1905 +    {
1906 +        return "Trying to use more child ID's than NumServers.  "
1907 +               "Increase NumServers in your config file.";
1908 +    }
1909 +
1910 +    CHILD_INFO_TABLE[NUM_CHILDS].uid  = uid;
1911 +    CHILD_INFO_TABLE[NUM_CHILDS].gid  = gid;
1912 +    CHILD_INFO_TABLE[NUM_CHILDS].type = type;
1913 +    CHILD_INFO_TABLE[NUM_CHILDS].chroot = chroot;
1914 +    CHILD_INFO_TABLE[NUM_CHILDS].sock_fd = AP_PERUSER_THISCHILD;
1915 +    CHILD_INFO_TABLE[NUM_CHILDS].status = status;
1916 +
1917 +    socketpair(PF_UNIX, SOCK_STREAM, 0, socks);
1918 +    CHILD_INFO_TABLE[NUM_CHILDS].input  = socks[0];
1919 +    CHILD_INFO_TABLE[NUM_CHILDS].output = socks[1];
1920 +
1921 +    _DBG("[%d] uid=%d gid=%d type=%d fd_in=%d fd_out=%d chroot=%s",
1922 +         NUM_CHILDS,
1923 +         CHILD_INFO_TABLE[NUM_CHILDS].uid,
1924 +         CHILD_INFO_TABLE[NUM_CHILDS].gid,
1925 +         CHILD_INFO_TABLE[NUM_CHILDS].type,
1926 +         CHILD_INFO_TABLE[NUM_CHILDS].input,
1927 +         CHILD_INFO_TABLE[NUM_CHILDS].output,
1928 +         CHILD_INFO_TABLE[NUM_CHILDS].chroot);
1929 +
1930 +    if (CHILD_INFO_TABLE[NUM_CHILDS].uid == 0 ||
1931 +        CHILD_INFO_TABLE[NUM_CHILDS].gid == 0)
1932 +    {
1933 +        _DBG("Assigning root user/group to a child.",0);
1934 +    }
1935 +
1936 +    child_info_image->control->num_childs++;
1937 +    return NULL;
1938 +}
1939 +
1940 +static int make_child(server_rec *s, int slot)
1941 +{
1942 +    int pid;
1943 +    int socks[2];
1944 +    child_info_t *multiplexer;
1945 +
1946 +    _DBG("function entered", 0);
1947 +
1948 +    switch (CHILD_INFO_TABLE[slot].type)
1949 +    {
1950 +        case CHILD_TYPE_MULTIPLEXER:            break;
1951 +        case CHILD_TYPE_PROCESSOR:              break;
1952 +        default:
1953 +            _DBG("no valid client in slot %d", slot);
1954 +            sleep(1);
1955 +            return 0;
1956 +    }
1957 +
1958 +    if (slot + 1 > ap_max_daemons_limit) {
1959 +       ap_max_daemons_limit = slot + 1;
1960 +    }
1961 +
1962 +    if (one_process) {
1963 +       apr_signal(SIGHUP, just_die);
1964 +        /* Don't catch AP_SIG_GRACEFUL in ONE_PROCESS mode :) */
1965 +       apr_signal(SIGINT, just_die);
1966 +#ifdef SIGQUIT
1967 +       apr_signal(SIGQUIT, SIG_DFL);
1968 +#endif
1969 +       apr_signal(SIGTERM, just_die);
1970 +       child_main(slot);
1971 +    }
1972 +
1973 +    (void) ap_update_child_status_from_indexes(slot, 0, SERVER_STARTING,
1974 +                                               (request_rec *) NULL);
1975 +
1976 +    if(CHILD_INFO_TABLE[slot].input == -1 &&
1977 +       CHILD_INFO_TABLE[slot].output == -1)
1978 +    {
1979 +        socketpair(PF_UNIX, SOCK_STREAM, 0, socks);
1980 +        CHILD_INFO_TABLE[slot].input  = socks[0];
1981 +        CHILD_INFO_TABLE[slot].output = socks[1];
1982 +
1983 +        if(!(multiplexer = find_next_multiplexer()))
1984 +        {
1985 +            _DBG("no multiplexer found!", 0);
1986 +            sleep(1);
1987 +            return 0;
1988 +        }
1989 +
1990 +        CHILD_INFO_TABLE[slot].status = CHILD_STATUS_READY;
1991 +        multiplexer->status = CHILD_STATUS_RESTART;
1992 +
1993 +        _DBG("New processor added: signaling multiplexer to restart", 0);
1994 +    }
1995 +    else
1996 +    {
1997 +        CHILD_INFO_TABLE[slot].status = CHILD_STATUS_ACTIVE;
1998 +    }
1999 +
2000 +
2001 +#ifdef _OSD_POSIX
2002 +    /* BS2000 requires a "special" version of fork() before a setuid() call */
2003 +    if ((pid = os_fork(unixd_config.user_name)) == -1) {
2004 +#elif defined(TPF)
2005 +    if ((pid = os_fork(s, slot)) == -1) {
2006 +#else
2007 +    if ((pid = fork()) == -1) {
2008 +#endif
2009 +       ap_log_error(APLOG_MARK, APLOG_ERR, errno, s, "fork: Unable to fork new process");
2010 +
2011 +       /* fork didn't succeed. Fix the scoreboard or else
2012 +        * it will say SERVER_STARTING forever and ever
2013 +        */
2014 +       (void) ap_update_child_status_from_indexes(slot, 0, SERVER_DEAD,
2015 +                                                   (request_rec *) NULL);
2016 +
2017 +       /* In case system resources are maxxed out, we don't want
2018 +          Apache running away with the CPU trying to fork over and
2019 +          over and over again. */
2020 +       sleep(10);
2021 +
2022 +       return -1;
2023 +    }
2024 +
2025 +    if (!pid) {
2026 +#ifdef HAVE_BINDPROCESSOR
2027 +        /* by default AIX binds to a single processor
2028 +         * this bit unbinds children which will then bind to another cpu
2029 +         */
2030 +       int status = bindprocessor(BINDPROCESS, (int)getpid(), 
2031 +                                  PROCESSOR_CLASS_ANY);
2032 +       if (status != OK) {
2033 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, 
2034 +                         ap_server_conf, "processor unbind failed %d", status);
2035 +       }
2036 +#endif
2037 +       RAISE_SIGSTOP(MAKE_CHILD);
2038 +        AP_MONCONTROL(1);
2039 +        /* Disable the parent's signal handlers and set up proper handling in
2040 +         * the child.
2041 +        */
2042 +       apr_signal(SIGHUP, just_die);
2043 +       apr_signal(SIGTERM, just_die);
2044 +        /* The child process doesn't do anything for AP_SIG_GRACEFUL.  
2045 +         * Instead, the pod is used for signalling graceful restart.
2046 +         */
2047 +        /* apr_signal(AP_SIG_GRACEFUL, restart); */
2048 +       child_main(slot);
2049 +        clean_child_exit(0);
2050 +    }
2051 +
2052 +    ap_scoreboard_image->parent[slot].pid = pid;
2053 +    CHILD_INFO_TABLE[slot].pid    = pid;
2054 +
2055 +    ap_child_table[slot].pid    = pid;
2056 +    ap_child_table[slot].status = SERVER_ALIVE;
2057 +
2058 +    return 0;
2059 +}
2060 +
2061 +
2062 +/*
2063 + * idle_spawn_rate is the number of children that will be spawned on the
2064 + * next maintenance cycle if there aren't enough idle servers.  It is
2065 + * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
2066 + * without the need to spawn.
2067 + */
2068 +static int idle_spawn_rate = 1;
2069 +#ifndef MAX_SPAWN_RATE
2070 +#define MAX_SPAWN_RATE (32)
2071 +#endif
2072 +static int hold_off_on_exponential_spawning;
2073 +
2074 +static void perform_idle_server_maintenance(apr_pool_t *p)
2075 +{
2076 +    int i;
2077 +
2078 +    /* _DBG("function entered", 0); */
2079 +
2080 +    for (i = 0; i < NUM_CHILDS; ++i)
2081 +    {
2082 +      if(CHILD_INFO_TABLE[i].pid == 0 &&
2083 +         CHILD_INFO_TABLE[i].status == CHILD_STATUS_STARTING)
2084 +          make_child(ap_server_conf, i);
2085 +    }
2086 +}
2087 +
2088 +
2089 +/*****************************************************************
2090 + * Executive routines.
2091 + */
2092 +
2093 +int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
2094 +{
2095 +    int remaining_children_to_start;
2096 +    int i;
2097 +    int fd;
2098 +    apr_status_t rv;
2099 +    apr_size_t one = 1;
2100 +    apr_socket_t *sock = NULL;
2101 +
2102 +    ap_log_pid(pconf, ap_pid_fname);
2103 +
2104 +    first_server_limit = server_limit;
2105 +    if (changed_limit_at_restart) {
2106 +        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
2107 +                     "WARNING: Attempt to change ServerLimit "
2108 +                     "ignored during restart");
2109 +        changed_limit_at_restart = 0;
2110 +    }
2111 +
2112 +    ap_server_conf = s;
2113 +
2114 +    /* Initialize cross-process accept lock */
2115 +    ap_lock_fname = apr_psprintf(_pconf, "%s.%" APR_PID_T_FMT,
2116 +                                 ap_server_root_relative(_pconf, ap_lock_fname),
2117 +                                 ap_my_pid);
2118 +
2119 +    rv = apr_proc_mutex_create(&accept_mutex, ap_lock_fname, 
2120 +                               ap_accept_lock_mech, _pconf);
2121 +    if (rv != APR_SUCCESS) {
2122 +        ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
2123 +                     "Couldn't create accept lock");
2124 +        return 1;
2125 +    }
2126 +
2127 +#if 0
2128 +#if APR_USE_SYSVSEM_SERIALIZE
2129 +    if (ap_accept_lock_mech == APR_LOCK_DEFAULT || 
2130 +        ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
2131 +#else
2132 +    if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
2133 +#endif
2134 +        rv = unixd_set_proc_mutex_perms(accept_mutex);
2135 +        if (rv != APR_SUCCESS) {
2136 +            ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
2137 +                         "Couldn't set permissions on cross-process lock; "
2138 +                         "check User and Group directives");
2139 +            return 1;
2140 +        }
2141 +    }
2142 +#endif
2143 +
2144 +    if (!is_graceful) {
2145 +        if (ap_run_pre_mpm(s->process->pool, SB_SHARED) != OK) {
2146 +            return 1;
2147 +        }
2148 +        /* fix the generation number in the global score; we just got a new,
2149 +         * cleared scoreboard
2150 +         */
2151 +        ap_scoreboard_image->global->running_generation = ap_my_generation;
2152 +    }
2153 +
2154 +    /* Initialize the child table */
2155 +    if (!is_graceful)
2156 +    {
2157 +        for (i = 0; i < server_limit; i++)
2158 +       {
2159 +            ap_child_table[i].pid = 0;
2160 +        }
2161 +    }
2162 +
2163 +    /* We need to put the new listeners at the end of the ap_listeners
2164 +     * list.  If we don't, then the pool will be cleared before the
2165 +     * open_logs phase is called for the second time, and ap_listeners
2166 +     * will have only invalid data.  If that happens, then the sockets
2167 +     * that we opened using make_sock() will be lost, and the server
2168 +     * won't start.
2169 +     */
2170 +
2171 +/*
2172 +    apr_os_file_get(&fd, pipe_of_death_in);
2173 +    apr_os_sock_put(&sock, &fd, pconf);
2174 +
2175 +    listen_add(pconf, sock, check_pipe_of_death);
2176 +*/
2177 +    set_signals();
2178 +
2179 +    if (one_process) {
2180 +        AP_MONCONTROL(1);
2181 +    }
2182 +
2183 +    ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2184 +               "%s configured -- resuming normal operations",
2185 +               ap_get_server_version());
2186 +    ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf,
2187 +               "Server built: %s", ap_get_server_built());
2188 +#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
2189 +    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf,
2190 +               "AcceptMutex: %s (default: %s)",
2191 +               apr_proc_mutex_name(accept_mutex),
2192 +               apr_proc_mutex_defname());
2193 +#endif
2194 +    restart_pending = shutdown_pending = 0;
2195 +
2196 +    _DBG("sizeof(child_info_t) = %d", sizeof(child_info_t));
2197 +
2198 +    while (!restart_pending && !shutdown_pending) {
2199 +       int child_slot;
2200 +        apr_exit_why_e exitwhy;
2201 +       int status, processed_status;
2202 +        /* this is a memory leak, but I'll fix it later. */
2203 +       apr_proc_t pid;
2204 +
2205 +        ap_wait_or_timeout(&exitwhy, &status, &pid, pconf);
2206 +
2207 +       /* XXX: if it takes longer than 1 second for all our children
2208 +        * to start up and get into IDLE state then we may spawn an
2209 +        * extra child
2210 +        */
2211 +       if (pid.pid != -1) {
2212 +            processed_status = ap_process_child_status(&pid, exitwhy, status);
2213 +            if (processed_status == APEXIT_CHILDFATAL) {
2214 +                return 1;
2215 +            }
2216 +
2217 +            /* non-fatal death... note that it's gone in the scoreboard. */
2218 +            child_slot = find_child_by_pid(&pid);
2219 +            _DBG("child #%d has died ...", child_slot);
2220 +
2221 +            for (i = 0; i < ap_max_daemons_limit; ++i)
2222 +            {
2223 +                if (ap_child_table[i].pid == pid.pid)
2224 +                {
2225 +                    child_slot = i;
2226 +                    break;
2227 +                }
2228 +            }
2229 +
2230 +           if (child_slot >= 0) {
2231 +                ap_child_table[child_slot].pid = 0;
2232 +                _TRACE_CALL("ap_update_child_status_from_indexes", 0);
2233 +               (void) ap_update_child_status_from_indexes(child_slot, 0, SERVER_DEAD,
2234 +                                                           (request_rec *) NULL);
2235 +                _TRACE_RET("ap_update_child_status_from_indexes", 0);
2236 +
2237 +                if (processed_status == APEXIT_CHILDSICK) {
2238 +                    /* child detected a resource shortage (E[NM]FILE, ENOBUFS, etc)
2239 +                     * cut the fork rate to the minimum 
2240 +                     */
2241 +                    _DBG("processed_status = APEXIT_CHILDSICK", 0);
2242 +                    idle_spawn_rate = 1; 
2243 +                }
2244 +                else if(child_slot < ap_daemons_limit) {
2245 +                   /* we're still doing a 1-for-1 replacement of dead
2246 +                       * children with new children
2247 +                       */
2248 +                    _DBG("replacing by new child ...", 0);
2249 +                   make_child(ap_server_conf, child_slot);
2250 +               }
2251 +#if APR_HAS_OTHER_CHILD
2252 +           }
2253 +           else if (apr_proc_other_child_read(&pid, status) == 0) {
2254 +                _DBG("APR_HAS_OTHER_CHILD, apparently", 0);
2255 +               /* handled */
2256 +#endif
2257 +           }
2258 +           else if (is_graceful) {
2259 +               /* Great, we've probably just lost a slot in the
2260 +                   * scoreboard.  Somehow we don't know about this
2261 +                   * child.
2262 +                   */
2263 +                _DBG("long lost child came home, whatever that means", 0);
2264 +
2265 +               ap_log_error(APLOG_MARK, APLOG_WARNING, 
2266 +                            0, ap_server_conf,
2267 +                           "long lost child came home! (pid %ld)", (long)pid.pid);
2268 +           }
2269 +           /* Don't perform idle maintenance when a child dies,
2270 +               * only do it when there's a timeout.  Remember only a
2271 +               * finite number of children can die, and it's pretty
2272 +               * pathological for a lot to die suddenly.
2273 +               */
2274 +           continue;
2275 +       }
2276 +
2277 +       perform_idle_server_maintenance(pconf);
2278 +#ifdef TPF
2279 +        shutdown_pending = os_check_server(tpf_server_name);
2280 +        ap_check_signals();
2281 +        sleep(1);
2282 +#endif /*TPF */
2283 +    }
2284 +
2285 +    if (shutdown_pending) {
2286 +       /* Time to gracefully shut down:
2287 +        * Kill child processes, tell them to call child_exit, etc...
2288 +        */
2289 +       if (unixd_killpg(getpgrp(), SIGTERM) < 0) {
2290 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGTERM");
2291 +       }
2292 +       ap_reclaim_child_processes(1);          /* Start with SIGTERM */
2293 +
2294 +       /* cleanup pid file on normal shutdown */
2295 +       {
2296 +           const char *pidfile = NULL;
2297 +           pidfile = ap_server_root_relative (pconf, ap_pid_fname);
2298 +           if ( pidfile != NULL && unlink(pidfile) == 0)
2299 +               ap_log_error(APLOG_MARK, APLOG_INFO,
2300 +                               0, ap_server_conf,
2301 +                               "removed PID file %s (pid=%ld)",
2302 +                               pidfile, (long)getpid());
2303 +       }
2304 +
2305 +       ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2306 +                   "caught SIGTERM, shutting down");
2307 +       return 1;
2308 +    }
2309 +
2310 +    /* we've been told to restart */
2311 +    apr_signal(SIGHUP, SIG_IGN);
2312 +    if (one_process) {
2313 +       /* not worth thinking about */
2314 +       return 1;
2315 +    }
2316 +
2317 +    /* advance to the next generation */
2318 +    /* XXX: we really need to make sure this new generation number isn't in
2319 +     * use by any of the children.
2320 +     */
2321 +    ++ap_my_generation;
2322 +    ap_scoreboard_image->global->running_generation = ap_my_generation;
2323 +    
2324 +    if (is_graceful) {
2325 +        char char_of_death = AP_PERUSER_CHAR_OF_DEATH;
2326 +
2327 +       ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2328 +                   "Graceful restart requested, doing restart");
2329 +
2330 +#if 0
2331 +       /* kill off the idle ones */
2332 +        ap_mpm_pod_killpg(pod, ap_max_daemons_limit);
2333 +
2334 +       /* This is mostly for debugging... so that we know what is still
2335 +           * gracefully dealing with existing request.  This will break
2336 +           * in a very nasty way if we ever have the scoreboard totally
2337 +           * file-based (no shared memory)
2338 +           */
2339 +       for (i = 0; i < ap_daemons_limit; ++i) {
2340 +           if (ap_scoreboard_image->servers[i][0].status != SERVER_DEAD) {
2341 +               ap_scoreboard_image->servers[i][0].status = SERVER_GRACEFUL;
2342 +           }
2343 +       }
2344 +#endif
2345 +
2346 +        ap_log_error(APLOG_MARK, APLOG_NOTICE, 0,
2347 +                     ap_server_conf, AP_SIG_GRACEFUL_STRING " received.  "
2348 +                     "Doing graceful restart");
2349 +
2350 +        /* This is mostly for debugging... so that we know what is still
2351 +         * gracefully dealing with existing request.
2352 +         */
2353 +
2354 +        for (i = 0; i < NUM_CHILDS; ++i)
2355 +        {
2356 +            ((ap_child_table[i].pid) && (ap_child_table[i].status = SERVER_DYING));
2357 +        }
2358 +
2359 +        /* give the children the signal to die */
2360 +        for (i = 0; i < NUM_CHILDS;)
2361 +        {
2362 +            if ((rv = apr_file_write(pipe_of_death_out, &char_of_death, &one)) != APR_SUCCESS)
2363 +            {
2364 +                if (APR_STATUS_IS_EINTR(rv)) continue;
2365 +                ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
2366 +                             "write pipe_of_death");
2367 +            }
2368 +            i++;
2369 +        }
2370 +    }
2371 +    else {
2372 +       /* Kill 'em off */
2373 +       if (unixd_killpg(getpgrp(), SIGHUP) < 0) {
2374 +           ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGHUP");
2375 +       }
2376 +       ap_reclaim_child_processes(0);          /* Not when just starting up */
2377 +       ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf,
2378 +                   "SIGHUP received.  Attempting to restart");
2379 +    }
2380 +
2381 +    return 0;
2382 +}
2383 +
2384 +/* == allocate an private server config structure == */
2385 +static void *peruser_create_config(apr_pool_t *p, server_rec *s)
2386 +{
2387 +    peruser_server_conf *c = (peruser_server_conf *)
2388 +                                  apr_pcalloc(p, sizeof(peruser_server_conf));
2389 +    c->max_processors = 5;
2390 +    c->last_child = 0;
2391 +    return c;
2392 +}
2393 +
2394 +/* This really should be a post_config hook, but the error log is already
2395 + * redirected by that point, so we need to do this in the open_logs phase.
2396 + */
2397 +static int peruser_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
2398 +{
2399 +    apr_status_t rv;
2400 +
2401 +    pconf = p;
2402 +    ap_server_conf = s;
2403 +
2404 +    if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) {
2405 +        ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0, 
2406 +                     NULL, "no listening sockets available, shutting down");
2407 +       return DONE;
2408 +    }
2409 +
2410 +    ap_log_pid(pconf, ap_pid_fname);
2411 +
2412 +    if ((rv = ap_mpm_pod_open(pconf, &pod))) {
2413 +        ap_log_error(APLOG_MARK, APLOG_CRIT|APLOG_STARTUP, rv, NULL,
2414 +               "Could not open pipe-of-death.");
2415 +        return DONE;
2416 +    }
2417 +
2418 +    if ((rv = apr_file_pipe_create(&pipe_of_death_in, &pipe_of_death_out,
2419 +                                   pconf)) != APR_SUCCESS) {
2420 +        ap_log_error(APLOG_MARK, APLOG_ERR, rv,
2421 +                     (const server_rec*) ap_server_conf,
2422 +                     "apr_file_pipe_create (pipe_of_death)");
2423 +        exit(1);
2424 +    }
2425 +    if ((rv = apr_file_pipe_timeout_set(pipe_of_death_in, 0)) != APR_SUCCESS) {
2426 +        ap_log_error(APLOG_MARK, APLOG_ERR, rv,
2427 +                     (const server_rec*) ap_server_conf,
2428 +                     "apr_file_pipe_timeout_set (pipe_of_death)");
2429 +        exit(1);
2430 +    }
2431 +
2432 +    return OK;
2433 +}
2434 +
2435 +static int peruser_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
2436 +{
2437 +    static int restart_num = 0;
2438 +    int no_detach, debug, foreground, i;
2439 +    int tmp_server_limit = DEFAULT_SERVER_LIMIT;
2440 +    ap_directive_t *pdir;
2441 +    apr_status_t rv;
2442 +    apr_pool_t *global_pool;
2443 +    void *shmem;
2444 +
2445 +    debug = ap_exists_config_define("DEBUG");
2446 +
2447 +    if (debug) {
2448 +        foreground = one_process = 1;
2449 +        no_detach = 0;
2450 +    }
2451 +    else
2452 +    {
2453 +        no_detach = ap_exists_config_define("NO_DETACH");
2454 +        one_process = ap_exists_config_define("ONE_PROCESS");
2455 +        foreground = ap_exists_config_define("FOREGROUND");
2456 +    }
2457 +
2458 +    /* sigh, want this only the second time around */
2459 +    if (restart_num++ == 1) {
2460 +       is_graceful = 0;
2461 +
2462 +        if (!one_process && !foreground) {
2463 +            rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND
2464 +                                           : APR_PROC_DETACH_DAEMONIZE);
2465 +            if (rv != APR_SUCCESS) {
2466 +                ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2467 +                             "apr_proc_detach failed");
2468 +                return HTTP_INTERNAL_SERVER_ERROR;
2469 +            }
2470 +        }
2471 +
2472 +       parent_pid = ap_my_pid = getpid();
2473 +    }
2474 +
2475 +    unixd_pre_config(ptemp);
2476 +    ap_listen_pre_config();
2477 +    ap_daemons_min_free = DEFAULT_MIN_FREE_DAEMON;
2478 +    ap_max_processors = DEFAULT_MAX_PROCESSORS;
2479 +    ap_daemons_limit = server_limit;
2480 +    ap_pid_fname = DEFAULT_PIDLOG;
2481 +    ap_lock_fname = DEFAULT_LOCKFILE;
2482 +    ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
2483 +    ap_extended_status = 0;
2484 +#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
2485 +       ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
2486 +#endif
2487 +
2488 +    apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
2489 +
2490 +    /* we need to know ServerLimit and ThreadLimit before we start processing
2491 +     * the tree because we need to already have allocated child_info_table
2492 +     */
2493 +    for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next)
2494 +    {
2495 +        if (!strcasecmp(pdir->directive, "ServerLimit"))
2496 +        {
2497 +            if (atoi(pdir->args) > tmp_server_limit)
2498 +            {
2499 +                tmp_server_limit = atoi(pdir->args);
2500 +                if (tmp_server_limit > MAX_SERVER_LIMIT)
2501 +                {
2502 +                    tmp_server_limit = MAX_SERVER_LIMIT;
2503 +                }
2504 +            }
2505 +        }
2506 +    }
2507 +
2508 +    _DBG("Initializing child_info_table", 0);
2509 +    child_info_size = tmp_server_limit * sizeof(child_info_t) + sizeof(apr_size_t);
2510 +
2511 +    /* We don't want to have to recreate the scoreboard after
2512 +     * restarts, so we'll create a global pool and never clean it.
2513 +     */
2514 +    rv = apr_pool_create(&global_pool, NULL);
2515 +    if (rv != APR_SUCCESS) {
2516 +        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2517 +                     "Fatal error: unable to create global pool");
2518 +        return rv;
2519 +    }
2520 +
2521 +    rv = apr_shm_create(&child_info_shm, child_info_size, NULL, global_pool);
2522 +
2523 +/*  if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) { */
2524 +    if (rv != APR_SUCCESS) {
2525 +        _DBG("shared memory creation failed", 0);
2526 +
2527 +        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
2528 +                     "Unable to create shared memory segment "
2529 +                     "(anonymous shared memory failure)");
2530 +    }
2531 +    else if (rv == APR_ENOTIMPL) {
2532 +        _DBG("anonymous shared memory not available", 0);
2533 +        /* TODO: make up a filename and do name-based shmem */
2534 +    }
2535 +
2536 +    if (rv || !(shmem = apr_shm_baseaddr_get(child_info_shm))) {
2537 +        _DBG("apr_shm_baseaddr_get() failed", 0);
2538 +        return HTTP_INTERNAL_SERVER_ERROR;
2539 +    }
2540 +
2541 +    memset(shmem, 0, sizeof(child_info_size));
2542 +    child_info_image = (child_info*)calloc(1, sizeof(child_info_size));
2543 +    child_info_image->control = (child_info_control*)shmem;
2544 +    shmem += sizeof(child_info_control*);
2545 +    child_info_image->table = (child_info_t*)shmem;
2546 +
2547 +    child_info_image->control->num_childs = 0;
2548 +
2549 +    for (i = 0; i < tmp_server_limit; i++)
2550 +    {
2551 +        CHILD_INFO_TABLE[i].uid     = -1;
2552 +        CHILD_INFO_TABLE[i].gid     = -1;
2553 +        CHILD_INFO_TABLE[i].chroot  = NULL;
2554 +        CHILD_INFO_TABLE[i].input   = -1;
2555 +        CHILD_INFO_TABLE[i].output  = -1;
2556 +        CHILD_INFO_TABLE[i].type    = -1;
2557 +        CHILD_INFO_TABLE[i].status  = CHILD_STATUS_STANDBY;
2558 +        CHILD_INFO_TABLE[i].sock_fd = -3; /* -1 and -2 are taken */
2559 +        CHILD_INFO_TABLE[i].id      = i;
2560 +    }
2561 +
2562 +    return OK;
2563 +}
2564 +
2565 +static int peruser_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *server_list)
2566 +{
2567 +    ap_child_table = (ap_ctable *)apr_pcalloc(p, server_limit * sizeof(ap_ctable));
2568 +
2569 +    return OK;
2570 +}
2571 +
2572 +static int peruser_post_read(request_rec *r)
2573 +{
2574 +    peruser_server_conf *sconf = PERUSER_SERVER_CONF(r->server->module_config);
2575 +    child_info_t *processor;
2576 +
2577 +    if(CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_MULTIPLEXER)
2578 +        processor = find_next_processor(sconf);
2579 +    else if(CHILD_INFO_TABLE[my_child_num].type == CHILD_TYPE_PROCESSOR)
2580 +        processor = &CHILD_INFO_TABLE[r->connection->id];
2581 +
2582 +    if(!processor)
2583 +    {
2584 +      _DBG("processor not found");
2585 +      processor = &CHILD_INFO_TABLE[r->connection->id];
2586 +    }
2587 +
2588 +    if (!strlen(r->the_request))
2589 +    {
2590 +        _DBG("corrupt request. aborting",0);
2591 +        return DECLINED;
2592 +    }
2593 +
2594 +    if (processor->sock_fd != AP_PERUSER_THISCHILD)
2595 +    {
2596 +        apr_socket_t *sock = NULL;
2597 +
2598 +        apr_os_sock_put(&sock, &processor->sock_fd, r->connection->pool);
2599 +        ap_sock_disable_nagle(sock);
2600 +        ap_set_module_config(r->connection->conn_config, &core_module, sock);
2601 +        _DBG("not the right socket?", 0);
2602 +        return OK;
2603 +    }
2604 +
2605 +    switch (CHILD_INFO_TABLE[my_child_num].type)
2606 +    {
2607 +        case CHILD_TYPE_MULTIPLEXER:
2608 +        {
2609 +            _DBG("MULTIPLEXER => Determining if request should be passed. "
2610 +                 "Child Num: %d, dest-child: %d, hostname from server: %s r->hostname=%s r->the_request=\"%s\"",
2611 +                my_child_num, processor->id, r->server->server_hostname, r->hostname, r->the_request);
2612 +
2613 +            if (processor->id != my_child_num)
2614 +            {
2615 +                if (processor->status == CHILD_STATUS_STANDBY)
2616 +                {
2617 +                    int sock[2];
2618 +                    _DBG("Activating child #%d", processor->id);
2619 +                    processor->status = CHILD_STATUS_STARTING;
2620 +                }
2621 +
2622 +                _DBG("Passing request.",0);
2623 +                if (pass_request(r, processor) == -1)
2624 +                {
2625 +                    ap_log_error(APLOG_MARK, APLOG_ERR, 0,
2626 +                             ap_server_conf, "Could not pass request to proper "                             "child, request will not be honoured.");
2627 +                    return DECLINED;
2628 +                }
2629 +                _DBG("doing longjmp",0);
2630 +                longjmp(CHILD_INFO_TABLE[my_child_num].jmpbuffer, 1);
2631 +                _DBG("request declined at our site",0);
2632 +                return DECLINED;
2633 +                _DBG("OUH! we should never reach this point",0);
2634 +            }
2635 +            _DBG("WTF: the server is assigned to the multiplexer! ... dropping request",0);
2636 +            return DECLINED;
2637 +        }
2638 +        case CHILD_TYPE_PROCESSOR:
2639 +        {
2640 +            _DBG("PROCESSOR %d", my_child_num);
2641 +            if (processor->id != my_child_num)
2642 +            {
2643 +                _DBG("WTF: this server is not assigned to us! what has the mux been smoking?!",0);
2644 +            }
2645 +            _DBG("request for %s / (server %s) seems to be for us", r->hostname, r->server->server_hostname);
2646 +
2647 +            /* TODO: add configuration for max processors per server */
2648 +            if (total_processors(sconf) > ap_max_processors) return DECLINED;
2649 +            if (idle_processors(sconf) < ap_daemons_min_free) child_clone();
2650 +
2651 +            return OK;
2652 +        }
2653 +        default:
2654 +        {
2655 +            _DBG("unspecified child type %d in %d, dropping request",
2656 +                 CHILD_INFO_TABLE[my_child_num].type, my_child_num);
2657 +            return OK;
2658 +        }
2659 +    }
2660 +
2661 +    _DBG("THIS POINT SHOULD NOT BE REACHED!",0);
2662 +    return OK;
2663 +}
2664 +
2665 +static void peruser_hooks(apr_pool_t *p)
2666 +{
2667 +    /* The peruser open_logs phase must run before the core's, or stderr
2668 +     * will be redirected to a file, and the messages won't print to the
2669 +     * console.
2670 +     */
2671 +    static const char *const aszSucc[] = {"core.c", NULL};
2672 +
2673 +#ifdef AUX3
2674 +    (void) set42sig();
2675 +#endif
2676 +
2677 +    ap_hook_open_logs(peruser_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE);
2678 +    ap_hook_pre_config(peruser_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
2679 +    ap_hook_post_config(peruser_post_config, NULL, NULL, APR_HOOK_MIDDLE);
2680 +
2681 +    /* Both of these must be run absolutely first.  If this request isn't for
2682 +     * this server then we need to forward it to the proper child.  No sense
2683 +     * tying up this server running more post_read request hooks if it is
2684 +     * just going to be forwarded along.  The process_connection hook allows
2685 +     * peruser to receive the passed request correctly, by automatically
2686 +     * filling in the core_input_filter's ctx pointer.
2687 +     */
2688 +    ap_hook_post_read_request(peruser_post_read, NULL, NULL,
2689 +                              APR_HOOK_REALLY_FIRST);
2690 +    ap_hook_process_connection(peruser_process_connection, NULL, NULL,
2691 +                               APR_HOOK_REALLY_FIRST);
2692 +}
2693 +
2694 +/* we define an Processor w/ specific uid/gid */
2695 +static const char *cf_Processor(cmd_parms *cmd, void *dummy,
2696 +    const char *user_name, const char *group_name, const char *chroot)
2697 +{
2698 +    uid_t uid = ap_uname2id(user_name);
2699 +    gid_t gid = ap_gname2id(group_name);
2700 +
2701 +    _DBG("user=%s:%d group=%s:%d chroot=%s",
2702 +        user_name, uid, group_name, gid, chroot);
2703 +
2704 +    return child_add(CHILD_TYPE_PROCESSOR, CHILD_STATUS_STANDBY,
2705 +                     uid, gid, chroot);
2706 +}
2707 +
2708 +/* we define an Multiplexer child w/ specific uid/gid */
2709 +static const char *cf_Multiplexer(cmd_parms *cmd, void *dummy,
2710 +    const char *user_name, const char *group_name, const char *chroot)
2711 +{
2712 +    uid_t uid = ap_uname2id(user_name);
2713 +    gid_t gid = ap_gname2id(group_name);
2714 +
2715 +    _DBG("user=%s:%d group=%s:%d chroot=%s [multiplexer id %d]",
2716 +        user_name, uid, group_name, gid, chroot, NUM_CHILDS);
2717 +
2718 +    return child_add(CHILD_TYPE_MULTIPLEXER, CHILD_STATUS_STARTING,
2719 +                     uid, gid, chroot);
2720 +}
2721 +
2722 +static const char* cf_ServerEnvironment(cmd_parms *cmd, void *dummy,
2723 +    const char *user_name, const char *group_name, const char *chroot)
2724 +{
2725 +    int i;
2726 +    int match = 0;
2727 +    int uid = ap_uname2id(user_name);
2728 +    int gid = ap_gname2id(group_name);
2729 +    const char *errstr;
2730 +    peruser_server_conf *sconf;
2731 +
2732 +    _DBG("function entered", 0);
2733 +
2734 +    sconf = PERUSER_SERVER_CONF(cmd->server->module_config);
2735 +
2736 +    sconf->fullsockname = apr_pstrcat(cmd->pool, sconf->sockname, ".",
2737 +        user_name,":", group_name, NULL);
2738 +
2739 +    sconf->uid = uid;
2740 +    sconf->gid = gid;
2741 +    sconf->chroot = chroot;
2742 +
2743 +    _DBG("user=%s:%d group=%s:%d chroot=%s numchilds=%d",
2744 +        user_name, uid, group_name, gid, chroot, NUM_CHILDS);
2745 +
2746 +    return NULL;
2747 +}
2748 +
2749 +/*
2750 +static const char *cf_MaxProcessorsPerServer(cmd_parms *cmd, void *dummy, const char *arg)
2751 +{
2752 +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2753 +    if (err != NULL) return err;
2754 +
2755 +    max_processors = atoi(arg);
2756 +    return NULL;
2757 +}
2758 +*/
2759 +
2760 +static const char *set_min_free_servers(cmd_parms *cmd, void *dummy, const char *arg)
2761 +{
2762 +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2763 +    if (err != NULL) {
2764 +        return err;
2765 +    }
2766 +
2767 +    ap_daemons_min_free = atoi(arg);
2768 +    if (ap_daemons_min_free <= 0) {
2769 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2770 +                    "WARNING: detected MinSpareServers set to non-positive.");
2771 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2772 +                    "Resetting to 1 to avoid almost certain Apache failure.");
2773 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2774 +                    "Please read the documentation.");
2775 +       ap_daemons_min_free = 1;
2776 +    }
2777 +       
2778 +    return NULL;
2779 +}
2780 +
2781 +static const char *set_max_clients (cmd_parms *cmd, void *dummy, const char *arg) 
2782 +{
2783 +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2784 +    if (err != NULL) {
2785 +        return err;
2786 +    }
2787 +
2788 +    ap_daemons_limit = atoi(arg);
2789 +    if (ap_daemons_limit > server_limit) {
2790 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2791 +                    "WARNING: MaxClients of %d exceeds ServerLimit value "
2792 +                    "of %d servers,", ap_daemons_limit, server_limit);
2793 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2794 +                    " lowering MaxClients to %d.  To increase, please "
2795 +                    "see the ServerLimit", server_limit);
2796 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2797 +                    " directive.");
2798 +       ap_daemons_limit = server_limit;
2799 +    } 
2800 +    else if (ap_daemons_limit < 1) {
2801 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2802 +                     "WARNING: Require MaxClients > 0, setting to 1");
2803 +       ap_daemons_limit = 1;
2804 +    }
2805 +    return NULL;
2806 +}
2807 +
2808 +static const char *set_max_processors (cmd_parms *cmd, void *dummy, const char *arg)
2809 +{
2810 +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2811 +    if (err != NULL) {
2812 +        return err;
2813 +    }
2814 +
2815 +    ap_max_processors = atoi(arg);
2816 +
2817 +    if (ap_max_processors < 1) {
2818 +        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
2819 +                     "WARNING: Require MaxProcessors > 0, setting to 1");
2820 +        ap_max_processors = 1;
2821 +    }
2822 +    return NULL;
2823 +}
2824 +
2825 +static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg) 
2826 +{
2827 +    int tmp_server_limit;
2828 +    
2829 +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2830 +    if (err != NULL) {
2831 +        return err;
2832 +    }
2833 +
2834 +    tmp_server_limit = atoi(arg);
2835 +    /* you cannot change ServerLimit across a restart; ignore
2836 +     * any such attempts
2837 +     */
2838 +    if (first_server_limit &&
2839 +        tmp_server_limit != server_limit) {
2840 +        /* how do we log a message?  the error log is a bit bucket at this
2841 +         * point; we'll just have to set a flag so that ap_mpm_run()
2842 +         * logs a warning later
2843 +         */
2844 +        changed_limit_at_restart = 1;
2845 +        return NULL;
2846 +    }
2847 +    server_limit = tmp_server_limit;
2848 +    
2849 +    if (server_limit > MAX_SERVER_LIMIT) {
2850 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2851 +                    "WARNING: ServerLimit of %d exceeds compile time limit "
2852 +                    "of %d servers,", server_limit, MAX_SERVER_LIMIT);
2853 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2854 +                    " lowering ServerLimit to %d.", MAX_SERVER_LIMIT);
2855 +       server_limit = MAX_SERVER_LIMIT;
2856 +    } 
2857 +    else if (server_limit < 1) {
2858 +       ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, 
2859 +                     "WARNING: Require ServerLimit > 0, setting to 1");
2860 +       server_limit = 1;
2861 +    }
2862 +    return NULL;
2863 +}
2864 +
2865 +static const command_rec peruser_cmds[] = {
2866 +UNIX_DAEMON_COMMANDS,
2867 +LISTEN_COMMANDS,
2868 +AP_INIT_TAKE1("MinSpareServers", set_min_free_servers, NULL, RSRC_CONF,
2869 +              "Minimum number of idle children, to handle request spikes"),
2870 +AP_INIT_TAKE1("MaxClients", set_max_clients, NULL, RSRC_CONF,
2871 +              "Maximum number of children alive at the same time"),
2872 +AP_INIT_TAKE1("MaxProcessors", set_max_processors, NULL, RSRC_CONF,
2873 +              "Maximum number of processors per vhost"),
2874 +AP_INIT_TAKE1("ServerLimit", set_server_limit, NULL, RSRC_CONF,
2875 +              "Maximum value of MaxClients for this run of Apache"),
2876 +AP_INIT_TAKE23("Multiplexer", cf_Multiplexer, NULL, RSRC_CONF,
2877 +              "Specify an Multiplexer Child configuration."),
2878 +AP_INIT_TAKE23("Processor", cf_Processor, NULL, RSRC_CONF,
2879 +              "Specify a User and Group for a specific child process."),
2880 +AP_INIT_TAKE23("ServerEnvironment", cf_ServerEnvironment, NULL, RSRC_CONF,
2881 +              "Specify the server environment for this virtual host."),
2882 +{ NULL }
2883 +};
2884 +
2885 +module AP_MODULE_DECLARE_DATA mpm_peruser_module = {
2886 +    MPM20_MODULE_STUFF,
2887 +    ap_mpm_rewrite_args,        /* hook to run before apache parses args */
2888 +    NULL,                      /* create per-directory config structure */
2889 +    NULL,                      /* merge per-directory config structures */
2890 +    peruser_create_config,     /* create per-server config structure */
2891 +    NULL,                      /* merge per-server config structures */
2892 +    peruser_cmds,              /* command apr_table_t */
2893 +    peruser_hooks,             /* register hooks */
2894 +};
This page took 0.227839 seconds and 3 git commands to generate.