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