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