/[apache]/trunk/dist/2.2/patches/20_all_peruser_0.2.0.patch
Gentoo

Contents of /trunk/dist/2.2/patches/20_all_peruser_0.2.0.patch

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20