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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20