]> granicus.if.org Git - apache/blob - server/mpm/prefork/prefork.c
merge the differences in apache-1.3 from tag apache-apr-merge-3 up
[apache] / server / mpm / prefork / prefork.c
1 /* ====================================================================
2  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the Apache Group
19  *    for use in the Apache HTTP server project (http://www.apache.org/)."
20  *
21  * 4. The names "Apache Server" and "Apache Group" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    apache@apache.org.
25  *
26  * 5. Products derived from this software may not be called "Apache"
27  *    nor may "Apache" appear in their names without prior written
28  *    permission of the Apache Group.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the Apache Group
33  *    for use in the Apache HTTP server project (http://www.apache.org/)."
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Group and was originally based
51  * on public domain software written at the National Center for
52  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53  * For more information on the Apache Group and the Apache HTTP server
54  * project, please see <http://www.apache.org/>.
55  *
56  */
57
58 /*
59  * httpd.c: simple http daemon for answering WWW file requests
60  *
61  * 
62  * 03-21-93  Rob McCool wrote original code (up to NCSA HTTPd 1.3)
63  * 
64  * 03-06-95  blong
65  *  changed server number for child-alone processes to 0 and changed name
66  *   of processes
67  *
68  * 03-10-95  blong
69  *      Added numerous speed hacks proposed by Robert S. Thau (rst@ai.mit.edu) 
70  *      including set group before fork, and call gettime before to fork
71  *      to set up libraries.
72  *
73  * 04-14-95  rst / rh
74  *      Brandon's code snarfed from NCSA 1.4, but tinkered to work with the
75  *      Apache server, and also to have child processes do accept() directly.
76  *
77  * April-July '95 rst
78  *      Extensive rework for Apache.
79  */
80
81 /* TODO: this is a cobbled together prefork MPM example... it should mostly
82  * TODO: behave like apache-1.3... here's a short list of things I think
83  * TODO: need cleaning up still:
84  * TODO: - use ralf's mm stuff for the shared mem and mutexes
85  * TODO: - clean up scoreboard stuff when we figure out how to do it in 2.0
86  */
87
88 #define CORE_PRIVATE
89
90 #include "httpd.h"
91 #include "mpm_default.h"
92 #include "http_main.h"
93 #include "http_log.h"
94 #include "http_config.h"
95 #include "http_core.h"          /* for get_remote_host */
96 #include "http_connection.h"
97 #include "scoreboard.h"
98 #include "ap_mpm.h"
99 #include "unixd.h"
100 #include "iol_socket.h"
101 #include "ap_listen.h"
102 #ifdef USE_SHMGET_SCOREBOARD
103 #include <sys/types.h>
104 #include <sys/ipc.h>
105 #include <sys/shm.h>
106 #endif
107
108 #ifdef HAVE_BSTRING_H
109 #include <bstring.h>            /* for IRIX, FD_SET calls bzero() */
110 #endif
111
112 /* config globals */
113
114 static int ap_max_requests_per_child=0;
115 static char *ap_pid_fname=NULL;
116 static char *ap_scoreboard_fname=NULL;
117 static char *ap_lock_fname;
118 static char *ap_server_argv0=NULL;
119 static int ap_daemons_to_start=0;
120 static int ap_daemons_min_free=0;
121 static int ap_daemons_max_free=0;
122 static int ap_daemons_limit=0;
123 static time_t ap_restart_time=0;
124 static int ap_extended_status = 0;
125
126 /*
127  * The max child slot ever assigned, preserved across restarts.  Necessary
128  * to deal with MaxClients changes across SIGUSR1 restarts.  We use this
129  * value to optimize routines that have to scan the entire scoreboard.
130  */
131 static int max_daemons_limit = -1;
132
133 static char ap_coredump_dir[MAX_STRING_LEN];
134
135 /* *Non*-shared http_main globals... */
136
137 static server_rec *server_conf;
138 static int sd;
139 static fd_set listenfds;
140 static int listenmaxfd;
141
142 /* one_process --- debugging mode variable; can be set from the command line
143  * with the -X flag.  If set, this gets you the child_main loop running
144  * in the process which originally started up (no detach, no make_child),
145  * which is a pretty nice debugging environment.  (You'll get a SIGHUP
146  * early in standalone_main; just continue through.  This is the server
147  * trying to kill off any child processes which it might have lying
148  * around --- Apache doesn't keep track of their pids, it just sends
149  * SIGHUP to the process group, ignoring it in the root process.
150  * Continue through and you'll be fine.).
151  */
152
153 static int one_process = 0;
154
155 #ifdef HAS_OTHER_CHILD
156 /* used to maintain list of children which aren't part of the scoreboard */
157 typedef struct other_child_rec other_child_rec;
158 struct other_child_rec {
159     other_child_rec *next;
160     int pid;
161     void (*maintenance) (int, void *, ap_wait_t);
162     void *data;
163     int write_fd;
164 };
165 static other_child_rec *other_children;
166 #endif
167
168 static pool *pconf;             /* Pool for config stuff */
169 static pool *pchild;            /* Pool for httpd child stuff */
170
171 static int my_pid;      /* it seems silly to call getpid all the time */
172 #ifndef MULTITHREAD
173 static int my_child_num;
174 #endif
175
176 #ifdef TPF
177 int tpf_child = 0;
178 char tpf_server_name[INETD_SERVNAME_LENGTH+1];
179 #endif /* TPF */
180
181 static scoreboard *ap_scoreboard_image = NULL;
182
183 #ifdef GPROF
184 /* 
185  * change directory for gprof to plop the gmon.out file
186  * configure in httpd.conf:
187  * GprofDir logs/   -> $ServerRoot/logs/gmon.out
188  * GprofDir logs/%  -> $ServerRoot/logs/gprof.$pid/gmon.out
189  */
190 static void chdir_for_gprof(void)
191 {
192     core_server_config *sconf = 
193         ap_get_module_config(server_conf->module_config, &core_module);    
194     char *dir = sconf->gprof_dir;
195
196     if(dir) {
197         char buf[512];
198         int len = strlen(sconf->gprof_dir) - 1;
199         if(*(dir + len) == '%') {
200             dir[len] = '\0';
201             ap_snprintf(buf, sizeof(buf), "%sgprof.%d", dir, (int)getpid());
202         } 
203         dir = ap_server_root_relative(pconf, buf[0] ? buf : dir);
204         if(mkdir(dir, 0755) < 0 && errno != EEXIST) {
205             ap_log_error(APLOG_MARK, APLOG_ERR, server_conf,
206                          "gprof: error creating directory %s", dir);
207         }
208     }
209     else {
210         dir = ap_server_root_relative(pconf, "logs");
211     }
212
213     chdir(dir);
214 }
215 #else
216 #define chdir_for_gprof()
217 #endif
218
219 /* a clean exit from a child with proper cleanup */
220 static void clean_child_exit(int code) __attribute__ ((noreturn));
221 static void clean_child_exit(int code)
222 {
223     if (pchild) {
224         ap_destroy_pool(pchild);
225     }
226     chdir_for_gprof();
227     exit(code);
228 }
229
230 #if defined(USE_FCNTL_SERIALIZED_ACCEPT) || defined(USE_FLOCK_SERIALIZED_ACCEPT)
231 static void expand_lock_fname(pool *p)
232 {
233     /* XXXX possibly bogus cast */
234     ap_lock_fname = ap_psprintf(p, "%s.%lu",
235         ap_server_root_relative(p, ap_lock_fname), (unsigned long)getpid());
236 }
237 #endif
238
239 #if defined (USE_USLOCK_SERIALIZED_ACCEPT)
240
241 #include <ulocks.h>
242
243 static ulock_t uslock = NULL;
244
245 #define accept_mutex_child_init(x)
246
247 static void accept_mutex_init(pool *p)
248 {
249     ptrdiff_t old;
250     usptr_t *us;
251
252
253     /* default is 8, allocate enough for all the children plus the parent */
254     if ((old = usconfig(CONF_INITUSERS, HARD_SERVER_LIMIT + 1)) == -1) {
255         perror("usconfig(CONF_INITUSERS)");
256         exit(-1);
257     }
258
259     if ((old = usconfig(CONF_LOCKTYPE, US_NODEBUG)) == -1) {
260         perror("usconfig(CONF_LOCKTYPE)");
261         exit(-1);
262     }
263     if ((old = usconfig(CONF_ARENATYPE, US_SHAREDONLY)) == -1) {
264         perror("usconfig(CONF_ARENATYPE)");
265         exit(-1);
266     }
267     if ((us = usinit("/dev/zero")) == NULL) {
268         perror("usinit");
269         exit(-1);
270     }
271
272     if ((uslock = usnewlock(us)) == NULL) {
273         perror("usnewlock");
274         exit(-1);
275     }
276 }
277
278 static void accept_mutex_on(void)
279 {
280     switch (ussetlock(uslock)) {
281     case 1:
282         /* got lock */
283         break;
284     case 0:
285         fprintf(stderr, "didn't get lock\n");
286         clean_child_exit(APEXIT_CHILDFATAL);
287     case -1:
288         perror("ussetlock");
289         clean_child_exit(APEXIT_CHILDFATAL);
290     }
291 }
292
293 static void accept_mutex_off(void)
294 {
295     if (usunsetlock(uslock) == -1) {
296         perror("usunsetlock");
297         clean_child_exit(APEXIT_CHILDFATAL);
298     }
299 }
300
301 #elif defined (USE_PTHREAD_SERIALIZED_ACCEPT)
302
303 /* This code probably only works on Solaris ... but it works really fast
304  * on Solaris.  Note that pthread mutexes are *NOT* released when a task
305  * dies ... the task has to free it itself.  So we block signals and
306  * try to be nice about releasing the mutex.
307  */
308
309 #include <pthread.h>
310
311 static pthread_mutex_t *accept_mutex = (void *)(caddr_t) -1;
312 static int have_accept_mutex;
313 static sigset_t accept_block_mask;
314 static sigset_t accept_previous_mask;
315
316 static void accept_mutex_child_cleanup(void *foo)
317 {
318     if (accept_mutex != (void *)(caddr_t)-1
319         && have_accept_mutex) {
320         pthread_mutex_unlock(accept_mutex);
321     }
322 }
323
324 static void accept_mutex_child_init(pool *p)
325 {
326     ap_register_cleanup(p, NULL, accept_mutex_child_cleanup, ap_null_cleanup);
327 }
328
329 static void accept_mutex_cleanup(void *foo)
330 {
331     if (accept_mutex != (void *)(caddr_t)-1
332         && munmap((caddr_t) accept_mutex, sizeof(*accept_mutex))) {
333         perror("munmap");
334     }
335     accept_mutex = (void *)(caddr_t)-1;
336 }
337
338 static void accept_mutex_init(pool *p)
339 {
340     pthread_mutexattr_t mattr;
341     int fd;
342
343     fd = open("/dev/zero", O_RDWR);
344     if (fd == -1) {
345         perror("open(/dev/zero)");
346         exit(APEXIT_INIT);
347     }
348     accept_mutex = (pthread_mutex_t *) mmap((caddr_t) 0, sizeof(*accept_mutex),
349                                  PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
350     if (accept_mutex == (void *) (caddr_t) - 1) {
351         perror("mmap");
352         exit(APEXIT_INIT);
353     }
354     close(fd);
355     if ((errno = pthread_mutexattr_init(&mattr))) {
356         perror("pthread_mutexattr_init");
357         exit(APEXIT_INIT);
358     }
359     if ((errno = pthread_mutexattr_setpshared(&mattr,
360                                                 PTHREAD_PROCESS_SHARED))) {
361         perror("pthread_mutexattr_setpshared");
362         exit(APEXIT_INIT);
363     }
364     if ((errno = pthread_mutex_init(accept_mutex, &mattr))) {
365         perror("pthread_mutex_init");
366         exit(APEXIT_INIT);
367     }
368     sigfillset(&accept_block_mask);
369     sigdelset(&accept_block_mask, SIGHUP);
370     sigdelset(&accept_block_mask, SIGTERM);
371     sigdelset(&accept_block_mask, SIGUSR1);
372     ap_register_cleanup(p, NULL, accept_mutex_cleanup, ap_null_cleanup);
373 }
374
375 static void accept_mutex_on(void)
376 {
377     int err;
378
379     if (sigprocmask(SIG_BLOCK, &accept_block_mask, &accept_previous_mask)) {
380         perror("sigprocmask(SIG_BLOCK)");
381         clean_child_exit(APEXIT_CHILDFATAL);
382     }
383     if ((err = pthread_mutex_lock(accept_mutex))) {
384         errno = err;
385         perror("pthread_mutex_lock");
386         clean_child_exit(APEXIT_CHILDFATAL);
387     }
388     have_accept_mutex = 1;
389 }
390
391 static void accept_mutex_off(void)
392 {
393     int err;
394
395     if ((err = pthread_mutex_unlock(accept_mutex))) {
396         errno = err;
397         perror("pthread_mutex_unlock");
398         clean_child_exit(APEXIT_CHILDFATAL);
399     }
400     /* There is a slight race condition right here... if we were to die right
401      * now, we'd do another pthread_mutex_unlock.  Now, doing that would let
402      * another process into the mutex.  pthread mutexes are designed to be
403      * fast, as such they don't have protection for things like testing if the
404      * thread owning a mutex is actually unlocking it (or even any way of
405      * testing who owns the mutex).
406      *
407      * If we were to unset have_accept_mutex prior to releasing the mutex
408      * then the race could result in the server unable to serve hits.  Doing
409      * it this way means that the server can continue, but an additional
410      * child might be in the critical section ... at least it's still serving
411      * hits.
412      */
413     have_accept_mutex = 0;
414     if (sigprocmask(SIG_SETMASK, &accept_previous_mask, NULL)) {
415         perror("sigprocmask(SIG_SETMASK)");
416         clean_child_exit(1);
417     }
418 }
419
420 #elif defined (USE_SYSVSEM_SERIALIZED_ACCEPT)
421
422 #include <sys/types.h>
423 #include <sys/ipc.h>
424 #include <sys/sem.h>
425
426 #ifdef NEED_UNION_SEMUN
427 /* it makes no sense, but this isn't defined on solaris */
428 union semun {
429     long val;
430     struct semid_ds *buf;
431     ushort *array;
432 };
433
434 #endif
435
436 static int sem_id = -1;
437 static struct sembuf op_on;
438 static struct sembuf op_off;
439
440 /* We get a random semaphore ... the lame sysv semaphore interface
441  * means we have to be sure to clean this up or else we'll leak
442  * semaphores.
443  */
444 static void accept_mutex_cleanup(void *foo)
445 {
446     union semun ick;
447
448     if (sem_id < 0)
449         return;
450     /* this is ignored anyhow */
451     ick.val = 0;
452     semctl(sem_id, 0, IPC_RMID, ick);
453 }
454
455 #define accept_mutex_child_init(x)
456
457 static void accept_mutex_init(pool *p)
458 {
459     union semun ick;
460     struct semid_ds buf;
461
462     /* acquire the semaphore */
463     sem_id = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
464     if (sem_id < 0) {
465         perror("semget");
466         exit(APEXIT_INIT);
467     }
468     ick.val = 1;
469     if (semctl(sem_id, 0, SETVAL, ick) < 0) {
470         perror("semctl(SETVAL)");
471         exit(APEXIT_INIT);
472     }
473     if (!getuid()) {
474         /* restrict it to use only by the appropriate user_id ... not that this
475          * stops CGIs from acquiring it and dinking around with it.
476          */
477         buf.sem_perm.uid = unixd_config.user_id;
478         buf.sem_perm.gid = unixd_config.group_id;
479         buf.sem_perm.mode = 0600;
480         ick.buf = &buf;
481         if (semctl(sem_id, 0, IPC_SET, ick) < 0) {
482             perror("semctl(IPC_SET)");
483             exit(APEXIT_INIT);
484         }
485     }
486     ap_register_cleanup(p, NULL, accept_mutex_cleanup, ap_null_cleanup);
487
488     /* pre-initialize these */
489     op_on.sem_num = 0;
490     op_on.sem_op = -1;
491     op_on.sem_flg = SEM_UNDO;
492     op_off.sem_num = 0;
493     op_off.sem_op = 1;
494     op_off.sem_flg = SEM_UNDO;
495 }
496
497 static void accept_mutex_on(void)
498 {
499     while (semop(sem_id, &op_on, 1) < 0) {
500         if (errno != EINTR) {
501             perror("accept_mutex_on");
502             clean_child_exit(APEXIT_CHILDFATAL);
503         }
504     }
505 }
506
507 static void accept_mutex_off(void)
508 {
509     while (semop(sem_id, &op_off, 1) < 0) {
510         if (errno != EINTR) {
511             perror("accept_mutex_off");
512             clean_child_exit(APEXIT_CHILDFATAL);
513         }
514     }
515 }
516
517 #elif defined(USE_FCNTL_SERIALIZED_ACCEPT)
518 static struct flock lock_it;
519 static struct flock unlock_it;
520
521 static int lock_fd = -1;
522
523 #define accept_mutex_child_init(x)
524
525 /*
526  * Initialize mutex lock.
527  * Must be safe to call this on a restart.
528  */
529 static void accept_mutex_init(pool *p)
530 {
531
532     lock_it.l_whence = SEEK_SET;        /* from current point */
533     lock_it.l_start = 0;                /* -"- */
534     lock_it.l_len = 0;                  /* until end of file */
535     lock_it.l_type = F_WRLCK;           /* set exclusive/write lock */
536     lock_it.l_pid = 0;                  /* pid not actually interesting */
537     unlock_it.l_whence = SEEK_SET;      /* from current point */
538     unlock_it.l_start = 0;              /* -"- */
539     unlock_it.l_len = 0;                /* until end of file */
540     unlock_it.l_type = F_UNLCK;         /* set exclusive/write lock */
541     unlock_it.l_pid = 0;                /* pid not actually interesting */
542
543     expand_lock_fname(p);
544     lock_fd = ap_popenf(p, ap_lock_fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
545     if (lock_fd == -1) {
546         perror("open");
547         fprintf(stderr, "Cannot open lock file: %s\n", ap_lock_fname);
548         exit(APEXIT_INIT);
549     }
550     unlink(ap_lock_fname);
551 }
552
553 static void accept_mutex_on(void)
554 {
555     int ret;
556
557     while ((ret = fcntl(lock_fd, F_SETLKW, &lock_it)) < 0 && errno == EINTR) {
558         /* nop */
559     }
560
561     if (ret < 0) {
562         ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
563                     "fcntl: F_SETLKW: Error getting accept lock, exiting!  "
564                     "Perhaps you need to use the LockFile directive to place "
565                     "your lock file on a local disk!");
566         clean_child_exit(APEXIT_CHILDFATAL);
567     }
568 }
569
570 static void accept_mutex_off(void)
571 {
572     int ret;
573
574     while ((ret = fcntl(lock_fd, F_SETLKW, &unlock_it)) < 0 && errno == EINTR) {
575         /* nop */
576     }
577     if (ret < 0) {
578         ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
579                     "fcntl: F_SETLKW: Error freeing accept lock, exiting!  "
580                     "Perhaps you need to use the LockFile directive to place "
581                     "your lock file on a local disk!");
582         clean_child_exit(APEXIT_CHILDFATAL);
583     }
584 }
585
586 #elif defined(USE_FLOCK_SERIALIZED_ACCEPT)
587
588 static int lock_fd = -1;
589
590 static void accept_mutex_cleanup(void *foo)
591 {
592     unlink(ap_lock_fname);
593 }
594
595 /*
596  * Initialize mutex lock.
597  * Done by each child at it's birth
598  */
599 static void accept_mutex_child_init(pool *p)
600 {
601
602     lock_fd = ap_popenf(p, ap_lock_fname, O_WRONLY, 0600);
603     if (lock_fd == -1) {
604         ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
605                     "Child cannot open lock file: %s", ap_lock_fname);
606         clean_child_exit(APEXIT_CHILDINIT);
607     }
608 }
609
610 /*
611  * Initialize mutex lock.
612  * Must be safe to call this on a restart.
613  */
614 static void accept_mutex_init(pool *p)
615 {
616     expand_lock_fname(p);
617     unlink(ap_lock_fname);
618     lock_fd = ap_popenf(p, ap_lock_fname, O_CREAT | O_WRONLY | O_EXCL, 0600);
619     if (lock_fd == -1) {
620         ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
621                     "Parent cannot open lock file: %s", ap_lock_fname);
622         exit(APEXIT_INIT);
623     }
624     ap_register_cleanup(p, NULL, accept_mutex_cleanup, ap_null_cleanup);
625 }
626
627 static void accept_mutex_on(void)
628 {
629     int ret;
630
631     while ((ret = flock(lock_fd, LOCK_EX)) < 0 && errno == EINTR)
632         continue;
633
634     if (ret < 0) {
635         ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
636                     "flock: LOCK_EX: Error getting accept lock. Exiting!");
637         clean_child_exit(APEXIT_CHILDFATAL);
638     }
639 }
640
641 static void accept_mutex_off(void)
642 {
643     if (flock(lock_fd, LOCK_UN) < 0) {
644         ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
645                     "flock: LOCK_UN: Error freeing accept lock. Exiting!");
646         clean_child_exit(APEXIT_CHILDFATAL);
647     }
648 }
649
650 #elif defined(USE_OS2SEM_SERIALIZED_ACCEPT)
651
652 static HMTX lock_sem = -1;
653
654 static void accept_mutex_cleanup(void *foo)
655 {
656     DosReleaseMutexSem(lock_sem);
657     DosCloseMutexSem(lock_sem);
658 }
659
660 /*
661  * Initialize mutex lock.
662  * Done by each child at it's birth
663  */
664 static void accept_mutex_child_init(pool *p)
665 {
666     int rc = DosOpenMutexSem(NULL, &lock_sem);
667
668     if (rc != 0) {
669         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, server_conf,
670                     "Child cannot open lock semaphore, rc=%d", rc);
671         clean_child_exit(APEXIT_CHILDINIT);
672     } else {
673         ap_register_cleanup(p, NULL, accept_mutex_cleanup, ap_null_cleanup);
674     }
675 }
676
677 /*
678  * Initialize mutex lock.
679  * Must be safe to call this on a restart.
680  */
681 static void accept_mutex_init(pool *p)
682 {
683     int rc = DosCreateMutexSem(NULL, &lock_sem, DC_SEM_SHARED, FALSE);
684
685     if (rc != 0) {
686         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, server_conf,
687                     "Parent cannot create lock semaphore, rc=%d", rc);
688         exit(APEXIT_INIT);
689     }
690
691     ap_register_cleanup(p, NULL, accept_mutex_cleanup, ap_null_cleanup);
692 }
693
694 static void accept_mutex_on(void)
695 {
696     int rc = DosRequestMutexSem(lock_sem, SEM_INDEFINITE_WAIT);
697
698     if (rc != 0) {
699         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, server_conf,
700                     "OS2SEM: Error %d getting accept lock. Exiting!", rc);
701         clean_child_exit(APEXIT_CHILDFATAL);
702     }
703 }
704
705 static void accept_mutex_off(void)
706 {
707     int rc = DosReleaseMutexSem(lock_sem);
708     
709     if (rc != 0) {
710         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, server_conf,
711                     "OS2SEM: Error %d freeing accept lock. Exiting!", rc);
712         clean_child_exit(APEXIT_CHILDFATAL);
713     }
714 }
715
716 #elif defined(USE_TPF_CORE_SERIALIZED_ACCEPT)
717
718 static int tpf_core_held;
719
720 static void accept_mutex_cleanup(void *foo)
721 {
722     if(tpf_core_held)
723         coruc(RESOURCE_KEY);
724 }
725
726 #define accept_mutex_init(x)
727
728 static void accept_mutex_child_init(pool *p)
729 {
730     ap_register_cleanup(p, NULL, accept_mutex_cleanup, ap_null_cleanup);
731     tpf_core_held = 0;
732 }
733
734 static void accept_mutex_on(void)
735 {
736     corhc(RESOURCE_KEY);
737     tpf_core_held = 1;
738     ap_check_signals();
739 }
740
741 static void accept_mutex_off(void)
742 {
743     coruc(RESOURCE_KEY);
744     tpf_core_held = 0;
745     ap_check_signals();
746 }
747
748 #else
749 /* Default --- no serialization.  Other methods *could* go here,
750  * as #elifs...
751  */
752 #if !defined(MULTITHREAD)
753 /* Multithreaded systems don't complete between processes for
754  * the sockets. */
755 #define NO_SERIALIZED_ACCEPT
756 #define accept_mutex_child_init(x)
757 #define accept_mutex_init(x)
758 #define accept_mutex_on()
759 #define accept_mutex_off()
760 #endif
761 #endif
762
763 /* On some architectures it's safe to do unserialized accept()s in the single
764  * Listen case.  But it's never safe to do it in the case where there's
765  * multiple Listen statements.  Define SINGLE_LISTEN_UNSERIALIZED_ACCEPT
766  * when it's safe in the single Listen case.
767  */
768 #ifdef SINGLE_LISTEN_UNSERIALIZED_ACCEPT
769 #define SAFE_ACCEPT(stmt) do {if (ap_listeners->next) {stmt;}} while(0)
770 #else
771 #define SAFE_ACCEPT(stmt) do {stmt;} while(0)
772 #endif
773
774
775 /*****************************************************************
776  * dealing with other children
777  */
778
779 #ifdef HAS_OTHER_CHILD
780 API_EXPORT(void) ap_register_other_child(int pid,
781                        void (*maintenance) (int reason, void *, ap_wait_t status),
782                           void *data, int write_fd)
783 {
784     other_child_rec *ocr;
785
786     ocr = ap_palloc(pconf, sizeof(*ocr));
787     ocr->pid = pid;
788     ocr->maintenance = maintenance;
789     ocr->data = data;
790     ocr->write_fd = write_fd;
791     ocr->next = other_children;
792     other_children = ocr;
793 }
794
795 /* note that since this can be called by a maintenance function while we're
796  * scanning the other_children list, all scanners should protect themself
797  * by loading ocr->next before calling any maintenance function.
798  */
799 API_EXPORT(void) ap_unregister_other_child(void *data)
800 {
801     other_child_rec **pocr, *nocr;
802
803     for (pocr = &other_children; *pocr; pocr = &(*pocr)->next) {
804         if ((*pocr)->data == data) {
805             nocr = (*pocr)->next;
806             (*(*pocr)->maintenance) (OC_REASON_UNREGISTER, (*pocr)->data, -1);
807             *pocr = nocr;
808             /* XXX: um, well we've just wasted some space in pconf ? */
809             return;
810         }
811     }
812 }
813
814 /* test to ensure that the write_fds are all still writable, otherwise
815  * invoke the maintenance functions as appropriate */
816 static void probe_writable_fds(void)
817 {
818     fd_set writable_fds;
819     int fd_max;
820     other_child_rec *ocr, *nocr;
821     struct timeval tv;
822     int rc;
823
824     if (other_children == NULL)
825         return;
826
827     fd_max = 0;
828     FD_ZERO(&writable_fds);
829     do {
830         for (ocr = other_children; ocr; ocr = ocr->next) {
831             if (ocr->write_fd == -1)
832                 continue;
833             FD_SET(ocr->write_fd, &writable_fds);
834             if (ocr->write_fd > fd_max) {
835                 fd_max = ocr->write_fd;
836             }
837         }
838         if (fd_max == 0)
839             return;
840
841         tv.tv_sec = 0;
842         tv.tv_usec = 0;
843         rc = ap_select(fd_max + 1, NULL, &writable_fds, NULL, &tv);
844     } while (rc == -1 && errno == EINTR);
845
846     if (rc == -1) {
847         /* XXX: uhh this could be really bad, we could have a bad file
848          * descriptor due to a bug in one of the maintenance routines */
849         ap_log_unixerr("probe_writable_fds", "select",
850                     "could not probe writable fds", server_conf);
851         return;
852     }
853     if (rc == 0)
854         return;
855
856     for (ocr = other_children; ocr; ocr = nocr) {
857         nocr = ocr->next;
858         if (ocr->write_fd == -1)
859             continue;
860         if (FD_ISSET(ocr->write_fd, &writable_fds))
861             continue;
862         (*ocr->maintenance) (OC_REASON_UNWRITABLE, ocr->data, -1);
863     }
864 }
865
866 /* possibly reap an other_child, return 0 if yes, -1 if not */
867 static int reap_other_child(int pid, ap_wait_t status)
868 {
869     other_child_rec *ocr, *nocr;
870
871     for (ocr = other_children; ocr; ocr = nocr) {
872         nocr = ocr->next;
873         if (ocr->pid != pid)
874             continue;
875         ocr->pid = -1;
876         (*ocr->maintenance) (OC_REASON_DEATH, ocr->data, status);
877         return 0;
878     }
879     return -1;
880 }
881 #endif
882
883 /*****************************************************************
884  *
885  * Dealing with the scoreboard... a lot of these variables are global
886  * only to avoid getting clobbered by the longjmp() that happens when
887  * a hard timeout expires...
888  *
889  * We begin with routines which deal with the file itself... 
890  */
891
892 #if defined(USE_OS2_SCOREBOARD)
893
894 /* The next two routines are used to access shared memory under OS/2.  */
895 /* This requires EMX v09c to be installed.                           */
896
897 caddr_t create_shared_heap(const char *name, size_t size)
898 {
899     ULONG rc;
900     void *mem;
901     Heap_t h;
902
903     rc = DosAllocSharedMem(&mem, name, size,
904                            PAG_COMMIT | PAG_READ | PAG_WRITE);
905     if (rc != 0)
906         return NULL;
907     h = _ucreate(mem, size, !_BLOCK_CLEAN, _HEAP_REGULAR | _HEAP_SHARED,
908                  NULL, NULL);
909     if (h == NULL)
910         DosFreeMem(mem);
911     return (caddr_t) h;
912 }
913
914 caddr_t get_shared_heap(const char *Name)
915 {
916
917     PVOID BaseAddress;          /* Pointer to the base address of
918                                    the shared memory object */
919     ULONG AttributeFlags;       /* Flags describing characteristics
920                                    of the shared memory object */
921     APIRET rc;                  /* Return code */
922
923     /* Request read and write access to */
924     /*   the shared memory object       */
925     AttributeFlags = PAG_WRITE | PAG_READ;
926
927     rc = DosGetNamedSharedMem(&BaseAddress, Name, AttributeFlags);
928
929     if (rc != 0) {
930         printf("DosGetNamedSharedMem error: return code = %ld", rc);
931         return 0;
932     }
933
934     return BaseAddress;
935 }
936
937 static void setup_shared_mem(pool *p)
938 {
939     caddr_t m;
940
941     int rc;
942
943     m = (caddr_t) create_shared_heap("\\SHAREMEM\\SCOREBOARD", SCOREBOARD_SIZE);
944     if (m == 0) {
945         fprintf(stderr, "%s: Could not create OS/2 Shared memory pool.\n",
946                 ap_server_argv0);
947         exit(APEXIT_INIT);
948     }
949
950     rc = _uopen((Heap_t) m);
951     if (rc != 0) {
952         fprintf(stderr,
953                 "%s: Could not uopen() newly created OS/2 Shared memory pool.\n",
954                 ap_server_argv0);
955     }
956     ap_scoreboard_image = (scoreboard *) m;
957     ap_scoreboard_image->global.running_generation = 0;
958 }
959
960 static void reopen_scoreboard(pool *p)
961 {
962     caddr_t m;
963     int rc;
964
965     m = (caddr_t) get_shared_heap("\\SHAREMEM\\SCOREBOARD");
966     if (m == 0) {
967         fprintf(stderr, "%s: Could not find existing OS/2 Shared memory pool.\n",
968                 ap_server_argv0);
969         exit(APEXIT_INIT);
970     }
971
972     rc = _uopen((Heap_t) m);
973     ap_scoreboard_image = (scoreboard *) m;
974 }
975
976 #elif defined(USE_POSIX_SCOREBOARD)
977 #include <sys/mman.h>
978 /* 
979  * POSIX 1003.4 style
980  *
981  * Note 1: 
982  * As of version 4.23A, shared memory in QNX must reside under /dev/shmem,
983  * where no subdirectories allowed.
984  *
985  * POSIX shm_open() and shm_unlink() will take care about this issue,
986  * but to avoid confusion, I suggest to redefine scoreboard file name
987  * in httpd.conf to cut "logs/" from it. With default setup actual name
988  * will be "/dev/shmem/logs.apache_status". 
989  * 
990  * If something went wrong and Apache did not unlinked this object upon
991  * exit, you can remove it manually, using "rm -f" command.
992  * 
993  * Note 2:
994  * <sys/mman.h> in QNX defines MAP_ANON, but current implementation 
995  * does NOT support BSD style anonymous mapping. So, the order of 
996  * conditional compilation is important: 
997  * this #ifdef section must be ABOVE the next one (BSD style).
998  *
999  * I tested this stuff and it works fine for me, but if it provides 
1000  * trouble for you, just comment out USE_MMAP_SCOREBOARD in QNX section
1001  * of ap_config.h
1002  *
1003  * June 5, 1997, 
1004  * Igor N. Kovalenko -- infoh@mail.wplus.net
1005  */
1006
1007 static void cleanup_shared_mem(void *d)
1008 {
1009     shm_unlink(ap_scoreboard_fname);
1010 }
1011
1012 static void setup_shared_mem(pool *p)
1013 {
1014     char buf[512];
1015     caddr_t m;
1016     int fd;
1017
1018     fd = shm_open(ap_scoreboard_fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
1019     if (fd == -1) {
1020         ap_snprintf(buf, sizeof(buf), "%s: could not open(create) scoreboard",
1021                     ap_server_argv0);
1022         perror(buf);
1023         exit(APEXIT_INIT);
1024     }
1025     if (ltrunc(fd, (off_t) SCOREBOARD_SIZE, SEEK_SET) == -1) {
1026         ap_snprintf(buf, sizeof(buf), "%s: could not ltrunc scoreboard",
1027                     ap_server_argv0);
1028         perror(buf);
1029         shm_unlink(ap_scoreboard_fname);
1030         exit(APEXIT_INIT);
1031     }
1032     if ((m = (caddr_t) mmap((caddr_t) 0,
1033                             (size_t) SCOREBOARD_SIZE, PROT_READ | PROT_WRITE,
1034                             MAP_SHARED, fd, (off_t) 0)) == (caddr_t) - 1) {
1035         ap_snprintf(buf, sizeof(buf), "%s: cannot mmap scoreboard",
1036                     ap_server_argv0);
1037         perror(buf);
1038         shm_unlink(ap_scoreboard_fname);
1039         exit(APEXIT_INIT);
1040     }
1041     close(fd);
1042     ap_register_cleanup(p, NULL, cleanup_shared_mem, ap_null_cleanup);
1043     ap_scoreboard_image = (scoreboard *) m;
1044     ap_scoreboard_image->global.running_generation = 0;
1045 }
1046
1047 static void reopen_scoreboard(pool *p)
1048 {
1049 }
1050
1051 #elif defined(USE_MMAP_SCOREBOARD)
1052
1053 static void setup_shared_mem(pool *p)
1054 {
1055     caddr_t m;
1056
1057 #if defined(MAP_ANON)
1058 /* BSD style */
1059 #ifdef CONVEXOS11
1060     /*
1061      * 9-Aug-97 - Jeff Venters (venters@convex.hp.com)
1062      * ConvexOS maps address space as follows:
1063      *   0x00000000 - 0x7fffffff : Kernel
1064      *   0x80000000 - 0xffffffff : User
1065      * Start mmapped area 1GB above start of text.
1066      *
1067      * Also, the length requires a pointer as the actual length is
1068      * returned (rounded up to a page boundary).
1069      */
1070     {
1071         unsigned len = SCOREBOARD_SIZE;
1072
1073         m = mmap((caddr_t) 0xC0000000, &len,
1074                  PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, NOFD, 0);
1075     }
1076 #elif defined(MAP_TMPFILE)
1077     {
1078         char mfile[] = "/tmp/apache_shmem_XXXX";
1079         int fd = mkstemp(mfile);
1080         if (fd == -1) {
1081             perror("open");
1082             fprintf(stderr, "%s: Could not open %s\n", ap_server_argv0, mfile);
1083             exit(APEXIT_INIT);
1084         }
1085         m = mmap((caddr_t) 0, SCOREBOARD_SIZE,
1086                 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1087         if (m == (caddr_t) - 1) {
1088             perror("mmap");
1089             fprintf(stderr, "%s: Could not mmap %s\n", ap_server_argv0, mfile);
1090             exit(APEXIT_INIT);
1091         }
1092         close(fd);
1093         unlink(mfile);
1094     }
1095 #else
1096     m = mmap((caddr_t) 0, SCOREBOARD_SIZE,
1097              PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
1098 #endif
1099     if (m == (caddr_t) - 1) {
1100         perror("mmap");
1101         fprintf(stderr, "%s: Could not mmap memory\n", ap_server_argv0);
1102         exit(APEXIT_INIT);
1103     }
1104 #else
1105 /* Sun style */
1106     int fd;
1107
1108     fd = open("/dev/zero", O_RDWR);
1109     if (fd == -1) {
1110         perror("open");
1111         fprintf(stderr, "%s: Could not open /dev/zero\n", ap_server_argv0);
1112         exit(APEXIT_INIT);
1113     }
1114     m = mmap((caddr_t) 0, SCOREBOARD_SIZE,
1115              PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1116     if (m == (caddr_t) - 1) {
1117         perror("mmap");
1118         fprintf(stderr, "%s: Could not mmap /dev/zero\n", ap_server_argv0);
1119         exit(APEXIT_INIT);
1120     }
1121     close(fd);
1122 #endif
1123     ap_scoreboard_image = (scoreboard *) m;
1124     ap_scoreboard_image->global.running_generation = 0;
1125 }
1126
1127 static void reopen_scoreboard(pool *p)
1128 {
1129 }
1130
1131 #elif defined(USE_SHMGET_SCOREBOARD)
1132 static key_t shmkey = IPC_PRIVATE;
1133 static int shmid = -1;
1134
1135 static void setup_shared_mem(pool *p)
1136 {
1137     struct shmid_ds shmbuf;
1138 #ifdef MOVEBREAK
1139     char *obrk;
1140 #endif
1141
1142     if ((shmid = shmget(shmkey, SCOREBOARD_SIZE, IPC_CREAT | SHM_R | SHM_W)) == -1) {
1143 #ifdef LINUX
1144         if (errno == ENOSYS) {
1145             ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_EMERG, server_conf,
1146                          "Your kernel was built without CONFIG_SYSVIPC\n"
1147                          "%s: Please consult the Apache FAQ for details",
1148                          ap_server_argv0);
1149         }
1150 #endif
1151         ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
1152                     "could not call shmget");
1153         exit(APEXIT_INIT);
1154     }
1155
1156     ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, server_conf,
1157                 "created shared memory segment #%d", shmid);
1158
1159 #ifdef MOVEBREAK
1160     /*
1161      * Some SysV systems place the shared segment WAY too close
1162      * to the dynamic memory break point (sbrk(0)). This severely
1163      * limits the use of malloc/sbrk in the program since sbrk will
1164      * refuse to move past that point.
1165      *
1166      * To get around this, we move the break point "way up there",
1167      * attach the segment and then move break back down. Ugly
1168      */
1169     if ((obrk = sbrk(MOVEBREAK)) == (char *) -1) {
1170         ap_log_error(APLOG_MARK, APLOG_ERR, server_conf,
1171             "sbrk() could not move break");
1172     }
1173 #endif
1174
1175 #define BADSHMAT        ((scoreboard *)(-1))
1176     if ((ap_scoreboard_image = (scoreboard *) shmat(shmid, 0, 0)) == BADSHMAT) {
1177         ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf, "shmat error");
1178         /*
1179          * We exit below, after we try to remove the segment
1180          */
1181     }
1182     else {                      /* only worry about permissions if we attached the segment */
1183         if (shmctl(shmid, IPC_STAT, &shmbuf) != 0) {
1184             ap_log_error(APLOG_MARK, APLOG_ERR, server_conf,
1185                 "shmctl() could not stat segment #%d", shmid);
1186         }
1187         else {
1188             shmbuf.shm_perm.uid = unixd_config.user_id;
1189             shmbuf.shm_perm.gid = unixd_config.group_id;
1190             if (shmctl(shmid, IPC_SET, &shmbuf) != 0) {
1191                 ap_log_error(APLOG_MARK, APLOG_ERR, server_conf,
1192                     "shmctl() could not set segment #%d", shmid);
1193             }
1194         }
1195     }
1196     /*
1197      * We must avoid leaving segments in the kernel's
1198      * (small) tables.
1199      */
1200     if (shmctl(shmid, IPC_RMID, NULL) != 0) {
1201         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf,
1202                 "shmctl: IPC_RMID: could not remove shared memory segment #%d",
1203                 shmid);
1204     }
1205     if (ap_scoreboard_image == BADSHMAT)        /* now bailout */
1206         exit(APEXIT_INIT);
1207
1208 #ifdef MOVEBREAK
1209     if (obrk == (char *) -1)
1210         return;                 /* nothing else to do */
1211     if (sbrk(-(MOVEBREAK)) == (char *) -1) {
1212         ap_log_error(APLOG_MARK, APLOG_ERR, server_conf,
1213             "sbrk() could not move break back");
1214     }
1215 #endif
1216     ap_scoreboard_image->global.running_generation = 0;
1217 }
1218
1219 static void reopen_scoreboard(pool *p)
1220 {
1221 }
1222
1223 #elif defined(USE_TPF_SCOREBOARD)
1224
1225 static void cleanup_scoreboard_heap()
1226 {
1227     int rv;
1228     rv = rsysc(ap_scoreboard_image, SCOREBOARD_FRAMES, SCOREBOARD_NAME);
1229     if(rv == RSYSC_ERROR) {
1230         ap_log_error(APLOG_MARK, APLOG_ERR, server_conf,
1231             "rsysc() could not release scoreboard system heap");
1232     }
1233 }
1234
1235 static void setup_shared_mem(pool *p)
1236 {
1237     cinfc(CINFC_WRITE, CINFC_CMMCTK2);
1238     ap_scoreboard_image = (scoreboard *) gsysc(SCOREBOARD_FRAMES, SCOREBOARD_NAME);
1239
1240     if (!ap_scoreboard_image) {
1241         fprintf(stderr, "httpd: Could not create scoreboard system heap storage.\n");
1242         exit(APEXIT_INIT);
1243     }
1244
1245     ap_register_cleanup(p, NULL, cleanup_scoreboard_heap, ap_null_cleanup);
1246     ap_scoreboard_image->global.running_generation = 0;
1247 }
1248
1249 static void reopen_scoreboard(pool *p)
1250 {
1251     cinfc(CINFC_WRITE, CINFC_CMMCTK2);
1252 }
1253
1254 #else
1255 #define SCOREBOARD_FILE
1256 static scoreboard _scoreboard_image;
1257 static int scoreboard_fd = -1;
1258
1259 /* XXX: things are seriously screwed if we ever have to do a partial
1260  * read or write ... we could get a corrupted scoreboard
1261  */
1262 static int force_write(int fd, void *buffer, int bufsz)
1263 {
1264     int rv, orig_sz = bufsz;
1265
1266     do {
1267         rv = write(fd, buffer, bufsz);
1268         if (rv > 0) {
1269             buffer = (char *) buffer + rv;
1270             bufsz -= rv;
1271         }
1272     } while ((rv > 0 && bufsz > 0) || (rv == -1 && errno == EINTR));
1273
1274     return rv < 0 ? rv : orig_sz - bufsz;
1275 }
1276
1277 static int force_read(int fd, void *buffer, int bufsz)
1278 {
1279     int rv, orig_sz = bufsz;
1280
1281     do {
1282         rv = read(fd, buffer, bufsz);
1283         if (rv > 0) {
1284             buffer = (char *) buffer + rv;
1285             bufsz -= rv;
1286         }
1287     } while ((rv > 0 && bufsz > 0) || (rv == -1 && errno == EINTR));
1288
1289     return rv < 0 ? rv : orig_sz - bufsz;
1290 }
1291
1292 static void cleanup_scoreboard_file(void *foo)
1293 {
1294     unlink(ap_scoreboard_fname);
1295 }
1296
1297 void reopen_scoreboard(pool *p)
1298 {
1299     if (scoreboard_fd != -1)
1300         ap_pclosef(p, scoreboard_fd);
1301
1302 #ifdef TPF
1303     ap_scoreboard_fname = ap_server_root_relative(p, ap_scoreboard_fname);
1304 #endif /* TPF */
1305     scoreboard_fd = ap_popenf(p, ap_scoreboard_fname, O_CREAT | O_BINARY | O_RDWR, 0666);
1306     if (scoreboard_fd == -1) {
1307         perror(ap_scoreboard_fname);
1308         fprintf(stderr, "Cannot open scoreboard file:\n");
1309         clean_child_exit(1);
1310     }
1311 }
1312 #endif
1313
1314 /* Called by parent process */
1315 static void reinit_scoreboard(pool *p)
1316 {
1317     int running_gen = 0;
1318     if (ap_scoreboard_image)
1319         running_gen = ap_scoreboard_image->global.running_generation;
1320
1321 #ifndef SCOREBOARD_FILE
1322     if (ap_scoreboard_image == NULL) {
1323         setup_shared_mem(p);
1324     }
1325     memset(ap_scoreboard_image, 0, SCOREBOARD_SIZE);
1326     ap_scoreboard_image->global.running_generation = running_gen;
1327 #else
1328     ap_scoreboard_image = &_scoreboard_image;
1329     ap_scoreboard_fname = ap_server_root_relative(p, ap_scoreboard_fname);
1330
1331     scoreboard_fd = ap_popenf(p, ap_scoreboard_fname, O_CREAT | O_BINARY | O_RDWR, 0644);
1332     if (scoreboard_fd == -1) {
1333         perror(ap_scoreboard_fname);
1334         fprintf(stderr, "Cannot open scoreboard file:\n");
1335         exit(APEXIT_INIT);
1336     }
1337     ap_register_cleanup(p, NULL, cleanup_scoreboard_file, ap_null_cleanup);
1338
1339     memset((char *) ap_scoreboard_image, 0, sizeof(*ap_scoreboard_image));
1340     ap_scoreboard_image->global.running_generation = running_gen;
1341     force_write(scoreboard_fd, ap_scoreboard_image, sizeof(*ap_scoreboard_image));
1342 #endif
1343 }
1344
1345 /* Routines called to deal with the scoreboard image
1346  * --- note that we do *not* need write locks, since update_child_status
1347  * only updates a *single* record in place, and only one process writes to
1348  * a given scoreboard slot at a time (either the child process owning that
1349  * slot, or the parent, noting that the child has died).
1350  *
1351  * As a final note --- setting the score entry to getpid() is always safe,
1352  * since when the parent is writing an entry, it's only noting SERVER_DEAD
1353  * anyway.
1354  */
1355
1356 ap_inline void ap_sync_scoreboard_image(void)
1357 {
1358 #ifdef SCOREBOARD_FILE
1359     lseek(scoreboard_fd, 0L, 0);
1360     force_read(scoreboard_fd, ap_scoreboard_image, sizeof(*ap_scoreboard_image));
1361 #endif
1362 }
1363
1364 API_EXPORT(int) ap_exists_scoreboard_image(void)
1365 {
1366     return (ap_scoreboard_image ? 1 : 0);
1367 }
1368
1369 static ap_inline void put_scoreboard_info(int child_num,
1370                                        short_score *new_score_rec)
1371 {
1372 #ifdef SCOREBOARD_FILE
1373     lseek(scoreboard_fd, (long) child_num * sizeof(short_score), 0);
1374     force_write(scoreboard_fd, new_score_rec, sizeof(short_score));
1375 #endif
1376 }
1377
1378 int ap_update_child_status(int child_num, int status, request_rec *r)
1379 {
1380     int old_status;
1381     short_score *ss;
1382
1383     if (child_num < 0)
1384         return -1;
1385
1386     ap_check_signals();
1387
1388     ap_sync_scoreboard_image();
1389     ss = &ap_scoreboard_image->servers[child_num];
1390     old_status = ss->status;
1391     ss->status = status;
1392
1393     if (ap_extended_status) {
1394         if (status == SERVER_READY || status == SERVER_DEAD) {
1395             /*
1396              * Reset individual counters
1397              */
1398             if (status == SERVER_DEAD) {
1399                 ss->my_access_count = 0L;
1400                 ss->my_bytes_served = 0L;
1401             }
1402             ss->conn_count = (unsigned short) 0;
1403             ss->conn_bytes = (unsigned long) 0;
1404         }
1405         if (r) {
1406             conn_rec *c = r->connection;
1407             ap_cpystrn(ss->client, ap_get_remote_host(c, r->per_dir_config,
1408                                   REMOTE_NOLOOKUP), sizeof(ss->client));
1409             if (r->the_request == NULL) {
1410                     ap_cpystrn(ss->request, "NULL", sizeof(ss->request));
1411             } else if (r->parsed_uri.password == NULL) {
1412                     ap_cpystrn(ss->request, r->the_request, sizeof(ss->request));
1413             } else {
1414                 /* Don't reveal the password in the server-status view */
1415                     ap_cpystrn(ss->request, ap_pstrcat(r->pool, r->method, " ",
1416                                                ap_unparse_uri_components(r->pool, &r->parsed_uri, UNP_OMITPASSWORD),
1417                                                r->assbackwards ? NULL : " ", r->protocol, NULL),
1418                                        sizeof(ss->request));
1419             }
1420             ss->vhostrec =  r->server;
1421         }
1422     }
1423     if (status == SERVER_STARTING && r == NULL) {
1424         /* clean up the slot's vhostrec pointer (maybe re-used)
1425          * and mark the slot as belonging to a new generation.
1426          */
1427         ss->vhostrec = NULL;
1428         ap_scoreboard_image->parent[child_num].generation = ap_my_generation;
1429 #ifdef SCOREBOARD_FILE
1430         lseek(scoreboard_fd, XtOffsetOf(scoreboard, parent[child_num]), 0);
1431         force_write(scoreboard_fd, &ap_scoreboard_image->parent[child_num],
1432             sizeof(parent_score));
1433 #endif
1434     }
1435     put_scoreboard_info(child_num, ss);
1436
1437     return old_status;
1438 }
1439
1440 static void update_scoreboard_global(void)
1441 {
1442 #ifdef SCOREBOARD_FILE
1443     lseek(scoreboard_fd,
1444           (char *) &ap_scoreboard_image->global -(char *) ap_scoreboard_image, 0);
1445     force_write(scoreboard_fd, &ap_scoreboard_image->global,
1446                 sizeof ap_scoreboard_image->global);
1447 #endif
1448 }
1449
1450 void ap_time_process_request(int child_num, int status)
1451 {
1452     short_score *ss;
1453 #if defined(NO_GETTIMEOFDAY) && !defined(NO_TIMES)
1454     struct tms tms_blk;
1455 #endif
1456
1457     if (child_num < 0)
1458         return;
1459
1460     ap_sync_scoreboard_image();
1461     ss = &ap_scoreboard_image->servers[child_num];
1462
1463     if (status == START_PREQUEST) {
1464 #if defined(NO_GETTIMEOFDAY)
1465 #ifndef NO_TIMES
1466         if ((ss->start_time = times(&tms_blk)) == -1)
1467 #endif /* NO_TIMES */
1468             ss->start_time = (clock_t) 0;
1469 #else
1470         if (gettimeofday(&ss->start_time, (struct timezone *) 0) < 0)
1471             ss->start_time.tv_sec =
1472                 ss->start_time.tv_usec = 0L;
1473 #endif
1474     }
1475     else if (status == STOP_PREQUEST) {
1476 #if defined(NO_GETTIMEOFDAY)
1477 #ifndef NO_TIMES
1478         if ((ss->stop_time = times(&tms_blk)) == -1)
1479 #endif
1480             ss->stop_time = ss->start_time = (clock_t) 0;
1481 #else
1482         if (gettimeofday(&ss->stop_time, (struct timezone *) 0) < 0)
1483             ss->stop_time.tv_sec =
1484                 ss->stop_time.tv_usec =
1485                 ss->start_time.tv_sec =
1486                 ss->start_time.tv_usec = 0L;
1487 #endif
1488
1489     }
1490
1491     put_scoreboard_info(child_num, ss);
1492 }
1493
1494 /*
1495 static void increment_counts(int child_num, request_rec *r)
1496 {
1497     long int bs = 0;
1498     short_score *ss;
1499
1500     ap_sync_scoreboard_image();
1501     ss = &ap_scoreboard_image->servers[child_num];
1502
1503     if (r->sent_bodyct)
1504         ap_bgetopt(r->connection->client, BO_BYTECT, &bs);
1505
1506 #ifndef NO_TIMES
1507     times(&ss->times);
1508 #endif
1509     ss->access_count++;
1510     ss->my_access_count++;
1511     ss->conn_count++;
1512     ss->bytes_served += (unsigned long) bs;
1513     ss->my_bytes_served += (unsigned long) bs;
1514     ss->conn_bytes += (unsigned long) bs;
1515
1516     put_scoreboard_info(child_num, ss);
1517 }
1518 */
1519
1520 static int find_child_by_pid(int pid)
1521 {
1522     int i;
1523
1524     for (i = 0; i < max_daemons_limit; ++i)
1525         if (ap_scoreboard_image->parent[i].pid == pid)
1526             return i;
1527
1528     return -1;
1529 }
1530
1531 static void reclaim_child_processes(int terminate)
1532 {
1533 #ifndef MULTITHREAD
1534     int i, status;
1535     long int waittime = 1024 * 16;      /* in usecs */
1536     struct timeval tv;
1537     int waitret, tries;
1538     int not_dead_yet;
1539 #ifdef HAS_OTHER_CHILD
1540     other_child_rec *ocr, *nocr;
1541 #endif
1542
1543     ap_sync_scoreboard_image();
1544
1545     for (tries = terminate ? 4 : 1; tries <= 9; ++tries) {
1546         /* don't want to hold up progress any more than 
1547          * necessary, but we need to allow children a few moments to exit.
1548          * Set delay with an exponential backoff.
1549          */
1550         tv.tv_sec = waittime / 1000000;
1551         tv.tv_usec = waittime % 1000000;
1552         waittime = waittime * 4;
1553         ap_select(0, NULL, NULL, NULL, &tv);
1554
1555         /* now see who is done */
1556         not_dead_yet = 0;
1557         for (i = 0; i < max_daemons_limit; ++i) {
1558             int pid = ap_scoreboard_image->parent[i].pid;
1559
1560             if (pid == my_pid || pid == 0)
1561                 continue;
1562
1563             waitret = waitpid(pid, &status, WNOHANG);
1564             if (waitret == pid || waitret == -1) {
1565                 ap_scoreboard_image->parent[i].pid = 0;
1566                 continue;
1567             }
1568             ++not_dead_yet;
1569             switch (tries) {
1570             case 1:     /*  16ms */
1571             case 2:     /*  82ms */
1572                 break;
1573             case 3:     /* 344ms */
1574                 /* perhaps it missed the SIGHUP, lets try again */
1575                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
1576                             server_conf,
1577                     "child process %d did not exit, sending another SIGHUP",
1578                             pid);
1579                 kill(pid, SIGHUP);
1580                 waittime = 1024 * 16;
1581                 break;
1582             case 4:     /*  16ms */
1583             case 5:     /*  82ms */
1584             case 6:     /* 344ms */
1585                 break;
1586             case 7:     /* 1.4sec */
1587                 /* ok, now it's being annoying */
1588                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
1589                             server_conf,
1590                    "child process %d still did not exit, sending a SIGTERM",
1591                             pid);
1592                 kill(pid, SIGTERM);
1593                 break;
1594             case 8:     /*  6 sec */
1595                 /* die child scum */
1596                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, server_conf,
1597                    "child process %d still did not exit, sending a SIGKILL",
1598                             pid);
1599                 kill(pid, SIGKILL);
1600                 break;
1601             case 9:     /* 14 sec */
1602                 /* gave it our best shot, but alas...  If this really 
1603                  * is a child we are trying to kill and it really hasn't
1604                  * exited, we will likely fail to bind to the port
1605                  * after the restart.
1606                  */
1607                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, server_conf,
1608                             "could not make child process %d exit, "
1609                             "attempting to continue anyway", pid);
1610                 break;
1611             }
1612         }
1613 #ifdef HAS_OTHER_CHILD
1614         for (ocr = other_children; ocr; ocr = nocr) {
1615             nocr = ocr->next;
1616             if (ocr->pid == -1)
1617                 continue;
1618
1619             waitret = waitpid(ocr->pid, &status, WNOHANG);
1620             if (waitret == ocr->pid) {
1621                 ocr->pid = -1;
1622                 (*ocr->maintenance) (OC_REASON_DEATH, ocr->data, status);
1623             }
1624             else if (waitret == 0) {
1625                 (*ocr->maintenance) (OC_REASON_RESTART, ocr->data, -1);
1626                 ++not_dead_yet;
1627             }
1628             else if (waitret == -1) {
1629                 /* uh what the heck? they didn't call unregister? */
1630                 ocr->pid = -1;
1631                 (*ocr->maintenance) (OC_REASON_LOST, ocr->data, -1);
1632             }
1633         }
1634 #endif
1635         if (!not_dead_yet) {
1636             /* nothing left to wait for */
1637             break;
1638         }
1639     }
1640 #endif /* ndef MULTITHREAD */
1641 }
1642
1643
1644 #if defined(NEED_WAITPID)
1645 /*
1646    Systems without a real waitpid sometimes lose a child's exit while waiting
1647    for another.  Search through the scoreboard for missing children.
1648  */
1649 int reap_children(ap_wait_t *status)
1650 {
1651     int n, pid;
1652
1653     for (n = 0; n < max_daemons_limit; ++n) {
1654         ap_sync_scoreboard_image();
1655         if (ap_scoreboard_image->servers[n].status != SERVER_DEAD &&
1656                 kill((pid = ap_scoreboard_image->parent[n].pid), 0) == -1) {
1657             ap_update_child_status(n, SERVER_DEAD, NULL);
1658             /* just mark it as having a successful exit status */
1659             bzero((char *) status, sizeof(ap_wait_t));
1660             return(pid);
1661         }
1662     }
1663     return 0;
1664 }
1665 #endif
1666
1667 /* Finally, this routine is used by the caretaker process to wait for
1668  * a while...
1669  */
1670
1671 /* number of calls to wait_or_timeout between writable probes */
1672 #ifndef INTERVAL_OF_WRITABLE_PROBES
1673 #define INTERVAL_OF_WRITABLE_PROBES 10
1674 #endif
1675 static int wait_or_timeout_counter;
1676
1677 static int wait_or_timeout(ap_wait_t *status)
1678 {
1679     struct timeval tv;
1680     int ret;
1681
1682     ++wait_or_timeout_counter;
1683     if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
1684         wait_or_timeout_counter = 0;
1685 #ifdef HAS_OTHER_CHILD
1686         probe_writable_fds();
1687 #endif
1688     }
1689     ret = waitpid(-1, status, WNOHANG);
1690     if (ret == -1 && errno == EINTR) {
1691         return -1;
1692     }
1693     if (ret > 0) {
1694         return ret;
1695     }
1696 #ifdef NEED_WAITPID
1697     if ((ret = reap_children(status)) > 0) {
1698         return ret;
1699     }
1700 #endif
1701     tv.tv_sec = SCOREBOARD_MAINTENANCE_INTERVAL / 1000000;
1702     tv.tv_usec = SCOREBOARD_MAINTENANCE_INTERVAL % 1000000;
1703     ap_select(0, NULL, NULL, NULL, &tv);
1704     return -1;
1705 }
1706
1707
1708 #if defined(NSIG)
1709 #define NumSIG NSIG
1710 #elif defined(_NSIG)
1711 #define NumSIG _NSIG
1712 #elif defined(__NSIG)
1713 #define NumSIG __NSIG
1714 #else
1715 #define NumSIG 32   /* for 1998's unixes, this is still a good assumption */
1716 #endif
1717
1718 #ifdef SYS_SIGLIST /* platform has sys_siglist[] */
1719 #define INIT_SIGLIST()  /*nothing*/
1720 #else /* platform has no sys_siglist[], define our own */
1721 #define SYS_SIGLIST ap_sys_siglist
1722 #define INIT_SIGLIST() siglist_init();
1723
1724 const char *ap_sys_siglist[NumSIG];
1725
1726 static void siglist_init(void)
1727 {
1728     int sig;
1729
1730     ap_sys_siglist[0] = "Signal 0";
1731 #ifdef SIGHUP
1732     ap_sys_siglist[SIGHUP] = "Hangup";
1733 #endif
1734 #ifdef SIGINT
1735     ap_sys_siglist[SIGINT] = "Interrupt";
1736 #endif
1737 #ifdef SIGQUIT
1738     ap_sys_siglist[SIGQUIT] = "Quit";
1739 #endif
1740 #ifdef SIGILL
1741     ap_sys_siglist[SIGILL] = "Illegal instruction";
1742 #endif
1743 #ifdef SIGTRAP
1744     ap_sys_siglist[SIGTRAP] = "Trace/BPT trap";
1745 #endif
1746 #ifdef SIGIOT
1747     ap_sys_siglist[SIGIOT] = "IOT instruction";
1748 #endif
1749 #ifdef SIGABRT
1750     ap_sys_siglist[SIGABRT] = "Abort";
1751 #endif
1752 #ifdef SIGEMT
1753     ap_sys_siglist[SIGEMT] = "Emulator trap";
1754 #endif
1755 #ifdef SIGFPE
1756     ap_sys_siglist[SIGFPE] = "Arithmetic exception";
1757 #endif
1758 #ifdef SIGKILL
1759     ap_sys_siglist[SIGKILL] = "Killed";
1760 #endif
1761 #ifdef SIGBUS
1762     ap_sys_siglist[SIGBUS] = "Bus error";
1763 #endif
1764 #ifdef SIGSEGV
1765     ap_sys_siglist[SIGSEGV] = "Segmentation fault";
1766 #endif
1767 #ifdef SIGSYS
1768     ap_sys_siglist[SIGSYS] = "Bad system call";
1769 #endif
1770 #ifdef SIGPIPE
1771     ap_sys_siglist[SIGPIPE] = "Broken pipe";
1772 #endif
1773 #ifdef SIGALRM
1774     ap_sys_siglist[SIGALRM] = "Alarm clock";
1775 #endif
1776 #ifdef SIGTERM
1777     ap_sys_siglist[SIGTERM] = "Terminated";
1778 #endif
1779 #ifdef SIGUSR1
1780     ap_sys_siglist[SIGUSR1] = "User defined signal 1";
1781 #endif
1782 #ifdef SIGUSR2
1783     ap_sys_siglist[SIGUSR2] = "User defined signal 2";
1784 #endif
1785 #ifdef SIGCLD
1786     ap_sys_siglist[SIGCLD] = "Child status change";
1787 #endif
1788 #ifdef SIGCHLD
1789     ap_sys_siglist[SIGCHLD] = "Child status change";
1790 #endif
1791 #ifdef SIGPWR
1792     ap_sys_siglist[SIGPWR] = "Power-fail restart";
1793 #endif
1794 #ifdef SIGWINCH
1795     ap_sys_siglist[SIGWINCH] = "Window changed";
1796 #endif
1797 #ifdef SIGURG
1798     ap_sys_siglist[SIGURG] = "urgent socket condition";
1799 #endif
1800 #ifdef SIGPOLL
1801     ap_sys_siglist[SIGPOLL] = "Pollable event occurred";
1802 #endif
1803 #ifdef SIGIO
1804     ap_sys_siglist[SIGIO] = "socket I/O possible";
1805 #endif
1806 #ifdef SIGSTOP
1807     ap_sys_siglist[SIGSTOP] = "Stopped (signal)";
1808 #endif
1809 #ifdef SIGTSTP
1810     ap_sys_siglist[SIGTSTP] = "Stopped";
1811 #endif
1812 #ifdef SIGCONT
1813     ap_sys_siglist[SIGCONT] = "Continued";
1814 #endif
1815 #ifdef SIGTTIN
1816     ap_sys_siglist[SIGTTIN] = "Stopped (tty input)";
1817 #endif
1818 #ifdef SIGTTOU
1819     ap_sys_siglist[SIGTTOU] = "Stopped (tty output)";
1820 #endif
1821 #ifdef SIGVTALRM
1822     ap_sys_siglist[SIGVTALRM] = "virtual timer expired";
1823 #endif
1824 #ifdef SIGPROF
1825     ap_sys_siglist[SIGPROF] = "profiling timer expired";
1826 #endif
1827 #ifdef SIGXCPU
1828     ap_sys_siglist[SIGXCPU] = "exceeded cpu limit";
1829 #endif
1830 #ifdef SIGXFSZ
1831     ap_sys_siglist[SIGXFSZ] = "exceeded file size limit";
1832 #endif
1833     for (sig=0; sig < sizeof(ap_sys_siglist)/sizeof(ap_sys_siglist[0]); ++sig)
1834         if (ap_sys_siglist[sig] == NULL)
1835             ap_sys_siglist[sig] = "";
1836 }
1837 #endif /* platform has sys_siglist[] */
1838
1839
1840 /* handle all varieties of core dumping signals */
1841 static void sig_coredump(int sig)
1842 {
1843     chdir(ap_coredump_dir);
1844     signal(sig, SIG_DFL);
1845     kill(getpid(), sig);
1846     /* At this point we've got sig blocked, because we're still inside
1847      * the signal handler.  When we leave the signal handler it will
1848      * be unblocked, and we'll take the signal... and coredump or whatever
1849      * is appropriate for this particular Unix.  In addition the parent
1850      * will see the real signal we received -- whereas if we called
1851      * abort() here, the parent would only see SIGABRT.
1852      */
1853 }
1854
1855 /*****************************************************************
1856  * Connection structures and accounting...
1857  */
1858
1859 static void just_die(int sig)
1860 {
1861     clean_child_exit(0);
1862 }
1863
1864 static int volatile deferred_die;
1865 static int volatile usr1_just_die;
1866
1867 static void usr1_handler(int sig)
1868 {
1869     if (usr1_just_die) {
1870         just_die(sig);
1871     }
1872     deferred_die = 1;
1873 }
1874
1875 /* volatile just in case */
1876 static int volatile shutdown_pending;
1877 static int volatile restart_pending;
1878 static int volatile is_graceful;
1879 ap_generation_t volatile ap_my_generation=0;
1880
1881 static void sig_term(int sig)
1882 {
1883     if (shutdown_pending == 1) {
1884         /* Um, is this _probably_ not an error, if the user has
1885          * tried to do a shutdown twice quickly, so we won't
1886          * worry about reporting it.
1887          */
1888         return;
1889     }
1890     shutdown_pending = 1;
1891 }
1892
1893 static void restart(int sig)
1894 {
1895     if (restart_pending == 1) {
1896         /* Probably not an error - don't bother reporting it */
1897         return;
1898     }
1899     restart_pending = 1;
1900     is_graceful = sig == SIGUSR1;
1901 }
1902
1903 static void set_signals(void)
1904 {
1905 #ifndef NO_USE_SIGACTION
1906     struct sigaction sa;
1907
1908     sigemptyset(&sa.sa_mask);
1909     sa.sa_flags = 0;
1910
1911     if (!one_process) {
1912         sa.sa_handler = sig_coredump;
1913 #if defined(SA_ONESHOT)
1914         sa.sa_flags = SA_ONESHOT;
1915 #elif defined(SA_RESETHAND)
1916         sa.sa_flags = SA_RESETHAND;
1917 #endif
1918         if (sigaction(SIGSEGV, &sa, NULL) < 0)
1919             ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGSEGV)");
1920 #ifdef SIGBUS
1921         if (sigaction(SIGBUS, &sa, NULL) < 0)
1922             ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGBUS)");
1923 #endif
1924 #ifdef SIGABORT
1925         if (sigaction(SIGABORT, &sa, NULL) < 0)
1926             ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGABORT)");
1927 #endif
1928 #ifdef SIGABRT
1929         if (sigaction(SIGABRT, &sa, NULL) < 0)
1930             ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGABRT)");
1931 #endif
1932 #ifdef SIGILL
1933         if (sigaction(SIGILL, &sa, NULL) < 0)
1934             ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGILL)");
1935 #endif
1936         sa.sa_flags = 0;
1937     }
1938     sa.sa_handler = sig_term;
1939     if (sigaction(SIGTERM, &sa, NULL) < 0)
1940         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGTERM)");
1941 #ifdef SIGINT
1942     if (sigaction(SIGINT, &sa, NULL) < 0)
1943         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGINT)");
1944 #endif
1945 #ifdef SIGXCPU
1946     sa.sa_handler = SIG_DFL;
1947     if (sigaction(SIGXCPU, &sa, NULL) < 0)
1948         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGXCPU)");
1949 #endif
1950 #ifdef SIGXFSZ
1951     sa.sa_handler = SIG_DFL;
1952     if (sigaction(SIGXFSZ, &sa, NULL) < 0)
1953         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGXFSZ)");
1954 #endif
1955 #ifdef SIGPIPE
1956     sa.sa_handler = SIG_IGN;
1957     if (sigaction(SIGPIPE, &sa, NULL) < 0)
1958         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGPIPE)");
1959 #endif
1960
1961     /* we want to ignore HUPs and USR1 while we're busy processing one */
1962     sigaddset(&sa.sa_mask, SIGHUP);
1963     sigaddset(&sa.sa_mask, SIGUSR1);
1964     sa.sa_handler = restart;
1965     if (sigaction(SIGHUP, &sa, NULL) < 0)
1966         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGHUP)");
1967     if (sigaction(SIGUSR1, &sa, NULL) < 0)
1968         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "sigaction(SIGUSR1)");
1969 #else
1970     if (!one_process) {
1971         signal(SIGSEGV, sig_coredump);
1972 #ifdef SIGBUS
1973         signal(SIGBUS, sig_coredump);
1974 #endif /* SIGBUS */
1975 #ifdef SIGABORT
1976         signal(SIGABORT, sig_coredump);
1977 #endif /* SIGABORT */
1978 #ifdef SIGABRT
1979         signal(SIGABRT, sig_coredump);
1980 #endif /* SIGABRT */
1981 #ifdef SIGILL
1982         signal(SIGILL, sig_coredump);
1983 #endif /* SIGILL */
1984 #ifdef SIGXCPU
1985         signal(SIGXCPU, SIG_DFL);
1986 #endif /* SIGXCPU */
1987 #ifdef SIGXFSZ
1988         signal(SIGXFSZ, SIG_DFL);
1989 #endif /* SIGXFSZ */
1990     }
1991
1992     signal(SIGTERM, sig_term);
1993 #ifdef SIGHUP
1994     signal(SIGHUP, restart);
1995 #endif /* SIGHUP */
1996 #ifdef SIGUSR1
1997     signal(SIGUSR1, restart);
1998 #endif /* SIGUSR1 */
1999 #ifdef SIGPIPE
2000     signal(SIGPIPE, SIG_IGN);
2001 #endif /* SIGPIPE */
2002
2003 #endif
2004 }
2005
2006 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
2007 static void sock_disable_nagle(int s)
2008 {
2009     /* The Nagle algorithm says that we should delay sending partial
2010      * packets in hopes of getting more data.  We don't want to do
2011      * this; we are not telnet.  There are bad interactions between
2012      * persistent connections and Nagle's algorithm that have very severe
2013      * performance penalties.  (Failing to disable Nagle is not much of a
2014      * problem with simple HTTP.)
2015      *
2016      * In spite of these problems, failure here is not a shooting offense.
2017      */
2018     int just_say_no = 1;
2019
2020     if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char *) &just_say_no,
2021                    sizeof(int)) < 0) {
2022         ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf,
2023                     "setsockopt: (TCP_NODELAY)");
2024     }
2025 }
2026
2027 #else
2028 #define sock_disable_nagle(s)   /* NOOP */
2029 #endif
2030
2031
2032 /*****************************************************************
2033  * Child process main loop.
2034  * The following vars are static to avoid getting clobbered by longjmp();
2035  * they are really private to child_main.
2036  */
2037
2038 static int srv;
2039 static int csd;
2040 static int requests_this_child;
2041 static fd_set main_fds;
2042
2043 API_EXPORT(void) ap_child_terminate(request_rec *r)
2044 {
2045     r->connection->keepalive = 0;
2046     requests_this_child = ap_max_requests_per_child = 1;
2047 }
2048
2049 int ap_graceful_stop_signalled(void)
2050 {
2051     ap_sync_scoreboard_image();
2052     if (deferred_die ||
2053         ap_scoreboard_image->global.running_generation != ap_my_generation) {
2054         return 1;
2055     }
2056     return 0;
2057 }
2058
2059 static void child_main(int child_num_arg)
2060 {
2061     NET_SIZE_T clen;
2062     struct sockaddr sa_server;
2063     struct sockaddr sa_client;
2064     ap_listen_rec *lr;
2065     ap_listen_rec *last_lr;
2066     ap_listen_rec *first_lr;
2067     pool *ptrans;
2068     conn_rec *current_conn;
2069     ap_iol *iol;
2070
2071     my_pid = getpid();
2072     csd = -1;
2073     my_child_num = child_num_arg;
2074     requests_this_child = 0;
2075     last_lr = NULL;
2076
2077     /* Get a sub pool for global allocations in this child, so that
2078      * we can have cleanups occur when the child exits.
2079      */
2080     pchild = ap_make_sub_pool(pconf);
2081
2082     ptrans = ap_make_sub_pool(pchild);
2083
2084     /* needs to be done before we switch UIDs so we have permissions */
2085     reopen_scoreboard(pchild);
2086     SAFE_ACCEPT(accept_mutex_child_init(pchild));
2087
2088     if (unixd_setup_child()) {
2089         clean_child_exit(APEXIT_CHILDFATAL);
2090     }
2091
2092     ap_child_init_hook(pchild, server_conf);
2093
2094     (void) ap_update_child_status(my_child_num, SERVER_READY, (request_rec *) NULL);
2095
2096     signal(SIGHUP, just_die);
2097     signal(SIGTERM, just_die);
2098
2099 #ifdef OS2
2100 /* Stop Ctrl-C/Ctrl-Break signals going to child processes */
2101     {
2102         unsigned long ulTimes;
2103         DosSetSignalExceptionFocus(0, &ulTimes);
2104     }
2105 #endif
2106
2107     while (!ap_graceful_stop_signalled()) {
2108         BUFF *conn_io;
2109
2110         /* Prepare to receive a SIGUSR1 due to graceful restart so that
2111          * we can exit cleanly.
2112          */
2113         usr1_just_die = 1;
2114         signal(SIGUSR1, usr1_handler);
2115
2116         /*
2117          * (Re)initialize this child to a pre-connection state.
2118          */
2119
2120         current_conn = NULL;
2121
2122         ap_clear_pool(ptrans);
2123
2124         if ((ap_max_requests_per_child > 0
2125              && requests_this_child++ >= ap_max_requests_per_child)) {
2126             clean_child_exit(0);
2127         }
2128
2129         (void) ap_update_child_status(my_child_num, SERVER_READY, (request_rec *) NULL);
2130
2131         /*
2132          * Wait for an acceptable connection to arrive.
2133          */
2134
2135         /* Lock around "accept", if necessary */
2136         SAFE_ACCEPT(accept_mutex_on());
2137
2138         for (;;) {
2139             if (ap_listeners->next) {
2140                 /* more than one socket */
2141                 memcpy(&main_fds, &listenfds, sizeof(fd_set));
2142                 srv = ap_select(listenmaxfd + 1, &main_fds, NULL, NULL, NULL);
2143
2144                 if (srv < 0 && errno != EINTR) {
2145                     /* Single Unix documents select as returning errnos
2146                      * EBADF, EINTR, and EINVAL... and in none of those
2147                      * cases does it make sense to continue.  In fact
2148                      * on Linux 2.0.x we seem to end up with EFAULT
2149                      * occasionally, and we'd loop forever due to it.
2150                      */
2151                     ap_log_error(APLOG_MARK, APLOG_ERR, server_conf, "select: (listen)");
2152                     clean_child_exit(1);
2153                 }
2154
2155                 if (srv <= 0)
2156                     continue;
2157
2158                 /* we remember the last_lr we searched last time around so that
2159                    we don't end up starving any particular listening socket */
2160                 if (last_lr == NULL) {
2161                     lr = ap_listeners;
2162                 }
2163                 else {
2164                     lr = last_lr->next;
2165                     if (!lr)
2166                         lr = ap_listeners;
2167                 }
2168                 first_lr=lr;
2169                 do {
2170                     if (FD_ISSET(lr->fd, &main_fds))
2171                         goto got_listener;
2172                     lr = lr->next;
2173                     if (!lr)
2174                         lr = ap_listeners;
2175                 }
2176                 while (lr != first_lr);
2177                 /* FIXME: if we get here, something bad has happened, and we're
2178                    probably gonna spin forever.
2179                 */
2180                 continue;
2181         got_listener:
2182                 last_lr = lr;
2183                 sd = lr->fd;
2184             }
2185             else {
2186                 /* only one socket, just pretend we did the other stuff */
2187                 sd = ap_listeners->fd;
2188             }
2189
2190             /* if we accept() something we don't want to die, so we have to
2191              * defer the exit
2192              */
2193             usr1_just_die = 0;
2194             for (;;) {
2195                 if (deferred_die) {
2196                     /* we didn't get a socket, and we were told to die */
2197                     clean_child_exit(0);
2198                 }
2199                 clen = sizeof(sa_client);
2200                 csd = ap_accept(sd, &sa_client, &clen);
2201                 if (csd >= 0 || errno != EINTR)
2202                     break;
2203             }
2204
2205             if (csd >= 0)
2206                 break;          /* We have a socket ready for reading */
2207             else {
2208
2209 /* TODO: this accept result handling stuff should be abstracted...
2210  * it's already out of date between the various unix mpms
2211  */
2212                 /* Our old behaviour here was to continue after accept()
2213                  * errors.  But this leads us into lots of troubles
2214                  * because most of the errors are quite fatal.  For
2215                  * example, EMFILE can be caused by slow descriptor
2216                  * leaks (say in a 3rd party module, or libc).  It's
2217                  * foolish for us to continue after an EMFILE.  We also
2218                  * seem to tickle kernel bugs on some platforms which
2219                  * lead to never-ending loops here.  So it seems best
2220                  * to just exit in most cases.
2221                  */
2222                 switch (errno) {
2223 #ifdef EPROTO
2224                     /* EPROTO on certain older kernels really means
2225                      * ECONNABORTED, so we need to ignore it for them.
2226                      * See discussion in new-httpd archives nh.9701
2227                      * search for EPROTO.
2228                      *
2229                      * Also see nh.9603, search for EPROTO:
2230                      * There is potentially a bug in Solaris 2.x x<6,
2231                      * and other boxes that implement tcp sockets in
2232                      * userland (i.e. on top of STREAMS).  On these
2233                      * systems, EPROTO can actually result in a fatal
2234                      * loop.  See PR#981 for example.  It's hard to
2235                      * handle both uses of EPROTO.
2236                      */
2237                 case EPROTO:
2238 #endif
2239 #ifdef ECONNABORTED
2240                 case ECONNABORTED:
2241 #endif
2242                     /* Linux generates the rest of these, other tcp
2243                      * stacks (i.e. bsd) tend to hide them behind
2244                      * getsockopt() interfaces.  They occur when
2245                      * the net goes sour or the client disconnects
2246                      * after the three-way handshake has been done
2247                      * in the kernel but before userland has picked
2248                      * up the socket.
2249                      */
2250 #ifdef ECONNRESET
2251                 case ECONNRESET:
2252 #endif
2253 #ifdef ETIMEDOUT
2254                 case ETIMEDOUT:
2255 #endif
2256 #ifdef EHOSTUNREACH
2257                 case EHOSTUNREACH:
2258 #endif
2259 #ifdef ENETUNREACH
2260                 case ENETUNREACH:
2261 #endif
2262                     break;
2263 #ifdef ENETDOWN
2264                 case ENETDOWN:
2265                      /*
2266                       * When the network layer has been shut down, there
2267                       * is not much use in simply exiting: the parent
2268                       * would simply re-create us (and we'd fail again).
2269                       * Use the CHILDFATAL code to tear the server down.
2270                       * @@@ Martin's idea for possible improvement:
2271                       * A different approach would be to define
2272                       * a new APEXIT_NETDOWN exit code, the reception
2273                       * of which would make the parent shutdown all
2274                       * children, then idle-loop until it detected that
2275                       * the network is up again, and restart the children.
2276                       * Ben Hyde noted that temporary ENETDOWN situations
2277                       * occur in mobile IP.
2278                       */
2279                     ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
2280                         "accept: giving up.");
2281                     clean_child_exit(APEXIT_CHILDFATAL);
2282 #endif /*ENETDOWN*/
2283
2284 #ifdef TPF
2285                 case EINACT:
2286                     ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
2287                         "offload device inactive");
2288                     clean_child_exit(APEXIT_CHILDFATAL);
2289                     break;
2290                 default:
2291                     ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, server_conf,
2292                         "select/accept error (%u)", errno);
2293                     clean_child_exit(APEXIT_CHILDFATAL);
2294 #else
2295                 default:
2296                     ap_log_error(APLOG_MARK, APLOG_ERR, server_conf,
2297                                 "accept: (client socket)");
2298                     clean_child_exit(1);
2299 #endif
2300                 }
2301             }
2302
2303             if (ap_graceful_stop_signalled()) {
2304                 clean_child_exit(0);
2305             }
2306             usr1_just_die = 1;
2307         }
2308
2309         SAFE_ACCEPT(accept_mutex_off());        /* unlock after "accept" */
2310
2311 #ifdef TPF
2312         if (csd == 0)                       /* 0 is invalid socket for TPF */
2313             continue;
2314 #endif
2315
2316         /* We've got a socket, let's at least process one request off the
2317          * socket before we accept a graceful restart request.  We set
2318          * the signal to ignore because we don't want to disturb any
2319          * third party code.
2320          */
2321         signal(SIGUSR1, SIG_IGN);
2322
2323         /*
2324          * We now have a connection, so set it up with the appropriate
2325          * socket options, file descriptors, and read/write buffers.
2326          */
2327
2328         clen = sizeof(sa_server);
2329         if (getsockname(csd, &sa_server, &clen) < 0) {
2330             ap_log_error(APLOG_MARK, APLOG_ERR, server_conf, "getsockname");
2331             close(csd);
2332             continue;
2333         }
2334
2335         sock_disable_nagle(csd);
2336
2337         iol = unix_attach_socket(csd);
2338         if (iol == NULL) {
2339             if (errno == EBADF) {
2340                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, NULL,
2341                     "filedescriptor (%u) larger than FD_SETSIZE (%u) "
2342                     "found, you probably need to rebuild Apache with a "
2343                     "larger FD_SETSIZE", csd, FD_SETSIZE);
2344             }
2345             else {
2346                 ap_log_error(APLOG_MARK, APLOG_WARNING, NULL,
2347                     "error attaching to socket");
2348             }
2349             close(csd);
2350             continue;
2351         }
2352
2353         (void) ap_update_child_status(my_child_num, SERVER_BUSY_READ,
2354                                    (request_rec *) NULL);
2355
2356         conn_io = ap_bcreate(ptrans, B_RDWR);
2357
2358         ap_bpush_iol(conn_io, iol);
2359
2360         current_conn = ap_new_connection(ptrans, server_conf, conn_io,
2361                                          (struct sockaddr_in *) &sa_client,
2362                                          (struct sockaddr_in *) &sa_server);
2363
2364         ap_process_connection(current_conn);
2365     }
2366 }
2367
2368
2369 static int make_child(server_rec *s, int slot, time_t now)
2370 {
2371     int pid;
2372
2373     if (slot + 1 > max_daemons_limit) {
2374         max_daemons_limit = slot + 1;
2375     }
2376
2377     if (one_process) {
2378         signal(SIGHUP, just_die);
2379         signal(SIGINT, just_die);
2380 #ifdef SIGQUIT
2381         signal(SIGQUIT, SIG_DFL);
2382 #endif
2383         signal(SIGTERM, just_die);
2384         child_main(slot);
2385     }
2386
2387     (void) ap_update_child_status(slot, SERVER_STARTING, (request_rec *) NULL);
2388
2389
2390 #ifdef _OSD_POSIX
2391     /* BS2000 requires a "special" version of fork() before a setuid() call */
2392     if ((pid = os_fork(unixd_config.user_name)) == -1) {
2393 #elif defined(TPF)
2394     if ((pid = os_fork(s, slot)) == -1) {
2395 #else
2396     if ((pid = fork()) == -1) {
2397 #endif
2398         ap_log_error(APLOG_MARK, APLOG_ERR, s, "fork: Unable to fork new process");
2399
2400         /* fork didn't succeed. Fix the scoreboard or else
2401          * it will say SERVER_STARTING forever and ever
2402          */
2403         (void) ap_update_child_status(slot, SERVER_DEAD, (request_rec *) NULL);
2404
2405         /* In case system resources are maxxed out, we don't want
2406            Apache running away with the CPU trying to fork over and
2407            over and over again. */
2408         sleep(10);
2409
2410         return -1;
2411     }
2412
2413     if (!pid) {
2414 #ifdef AIX_BIND_PROCESSOR
2415 /* by default AIX binds to a single processor
2416  * this bit unbinds children which will then bind to another cpu
2417  */
2418 #include <sys/processor.h>
2419         int status = bindprocessor(BINDPROCESS, (int)getpid(), 
2420                                    PROCESSOR_CLASS_ANY);
2421         if (status != OK) {
2422             ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, server_conf,
2423                         "processor unbind failed %d", status);
2424         }
2425 #endif
2426         RAISE_SIGSTOP(MAKE_CHILD);
2427         /* Disable the restart signal handlers and enable the just_die stuff.
2428          * Note that since restart() just notes that a restart has been
2429          * requested there's no race condition here.
2430          */
2431         signal(SIGHUP, just_die);
2432         signal(SIGUSR1, just_die);
2433         signal(SIGTERM, just_die);
2434         child_main(slot);
2435     }
2436
2437     ap_scoreboard_image->parent[slot].pid = pid;
2438 #ifdef SCOREBOARD_FILE
2439     lseek(scoreboard_fd, XtOffsetOf(scoreboard, parent[slot]), 0);
2440     force_write(scoreboard_fd, &ap_scoreboard_image->parent[slot],
2441                 sizeof(parent_score));
2442 #endif
2443
2444     return 0;
2445 }
2446
2447
2448 /* start up a bunch of children */
2449 static void startup_children(int number_to_start)
2450 {
2451     int i;
2452     time_t now = time(0);
2453
2454     for (i = 0; number_to_start && i < ap_daemons_limit; ++i) {
2455         if (ap_scoreboard_image->servers[i].status != SERVER_DEAD) {
2456             continue;
2457         }
2458         if (make_child(server_conf, i, now) < 0) {
2459             break;
2460         }
2461         --number_to_start;
2462     }
2463 }
2464
2465
2466 /*
2467  * idle_spawn_rate is the number of children that will be spawned on the
2468  * next maintenance cycle if there aren't enough idle servers.  It is
2469  * doubled up to MAX_SPAWN_RATE, and reset only when a cycle goes by
2470  * without the need to spawn.
2471  */
2472 static int idle_spawn_rate = 1;
2473 #ifndef MAX_SPAWN_RATE
2474 #define MAX_SPAWN_RATE  (32)
2475 #endif
2476 static int hold_off_on_exponential_spawning;
2477
2478 static void perform_idle_server_maintenance(void)
2479 {
2480     int i;
2481     int to_kill;
2482     int idle_count;
2483     short_score *ss;
2484     time_t now = time(0);
2485     int free_length;
2486     int free_slots[MAX_SPAWN_RATE];
2487     int last_non_dead;
2488     int total_non_dead;
2489
2490     /* initialize the free_list */
2491     free_length = 0;
2492
2493     to_kill = -1;
2494     idle_count = 0;
2495     last_non_dead = -1;
2496     total_non_dead = 0;
2497
2498     ap_sync_scoreboard_image();
2499     for (i = 0; i < ap_daemons_limit; ++i) {
2500         int status;
2501
2502         if (i >= max_daemons_limit && free_length == idle_spawn_rate)
2503             break;
2504         ss = &ap_scoreboard_image->servers[i];
2505         status = ss->status;
2506         if (status == SERVER_DEAD) {
2507             /* try to keep children numbers as low as possible */
2508             if (free_length < idle_spawn_rate) {
2509                 free_slots[free_length] = i;
2510                 ++free_length;
2511             }
2512         }
2513         else {
2514             /* We consider a starting server as idle because we started it
2515              * at least a cycle ago, and if it still hasn't finished starting
2516              * then we're just going to swamp things worse by forking more.
2517              * So we hopefully won't need to fork more if we count it.
2518              * This depends on the ordering of SERVER_READY and SERVER_STARTING.
2519              */
2520             if (status <= SERVER_READY) {
2521                 ++ idle_count;
2522                 /* always kill the highest numbered child if we have to...
2523                  * no really well thought out reason ... other than observing
2524                  * the server behaviour under linux where lower numbered children
2525                  * tend to service more hits (and hence are more likely to have
2526                  * their data in cpu caches).
2527                  */
2528                 to_kill = i;
2529             }
2530
2531             ++total_non_dead;
2532             last_non_dead = i;
2533         }
2534     }
2535     max_daemons_limit = last_non_dead + 1;
2536     if (idle_count > ap_daemons_max_free) {
2537         /* kill off one child... we use SIGUSR1 because that'll cause it to
2538          * shut down gracefully, in case it happened to pick up a request
2539          * while we were counting
2540          */
2541         kill(ap_scoreboard_image->parent[to_kill].pid, SIGUSR1);
2542         idle_spawn_rate = 1;
2543     }
2544     else if (idle_count < ap_daemons_min_free) {
2545         /* terminate the free list */
2546         if (free_length == 0) {
2547             /* only report this condition once */
2548             static int reported = 0;
2549
2550             if (!reported) {
2551                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, server_conf,
2552                             "server reached MaxClients setting, consider"
2553                             " raising the MaxClients setting");
2554                 reported = 1;
2555             }
2556             idle_spawn_rate = 1;
2557         }
2558         else {
2559             if (idle_spawn_rate >= 8) {
2560                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, server_conf,
2561                     "server seems busy, (you may need "
2562                     "to increase StartServers, or Min/MaxSpareServers), "
2563                     "spawning %d children, there are %d idle, and "
2564                     "%d total children", idle_spawn_rate,
2565                     idle_count, total_non_dead);
2566             }
2567             for (i = 0; i < free_length; ++i) {
2568 #ifdef TPF
2569         if(make_child(server_conf, free_slots[i], now) == -1) {
2570             if(free_length == 1) {
2571                 shutdown_pending = 1;
2572                 ap_log_error(APLOG_MARK, APLOG_EMERG, server_conf,
2573                 "No active child processes: shutting down");
2574             }
2575         }
2576 #else
2577                 make_child(server_conf, free_slots[i], now);
2578 #endif /* TPF */
2579             }
2580             /* the next time around we want to spawn twice as many if this
2581              * wasn't good enough, but not if we've just done a graceful
2582              */
2583             if (hold_off_on_exponential_spawning) {
2584                 --hold_off_on_exponential_spawning;
2585             }
2586             else if (idle_spawn_rate < MAX_SPAWN_RATE) {
2587                 idle_spawn_rate *= 2;
2588             }
2589         }
2590     }
2591     else {
2592         idle_spawn_rate = 1;
2593     }
2594 }
2595
2596
2597 static void process_child_status(int pid, ap_wait_t status)
2598 {
2599     /* Child died... if it died due to a fatal error,
2600         * we should simply bail out.
2601         */
2602     if ((WIFEXITED(status)) &&
2603         WEXITSTATUS(status) == APEXIT_CHILDFATAL) {
2604         ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, server_conf,
2605                         "Child %d returned a Fatal error... \n"
2606                         "Apache is exiting!",
2607                         pid);
2608         exit(APEXIT_CHILDFATAL);
2609     }
2610     if (WIFSIGNALED(status)) {
2611         switch (WTERMSIG(status)) {
2612         case SIGTERM:
2613         case SIGHUP:
2614         case SIGUSR1:
2615         case SIGKILL:
2616             break;
2617         default:
2618 #ifdef SYS_SIGLIST
2619 #ifdef WCOREDUMP
2620             if (WCOREDUMP(status)) {
2621                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
2622                              server_conf,
2623                              "child pid %d exit signal %s (%d), "
2624                              "possible coredump in %s",
2625                              pid, (WTERMSIG(status) >= NumSIG) ? "" : 
2626                              SYS_SIGLIST[WTERMSIG(status)], WTERMSIG(status),
2627                              ap_coredump_dir);
2628             }
2629             else {
2630 #endif
2631                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
2632                              server_conf,
2633                              "child pid %d exit signal %s (%d)", pid,
2634                              SYS_SIGLIST[WTERMSIG(status)], WTERMSIG(status));
2635 #ifdef WCOREDUMP
2636             }
2637 #endif
2638 #else
2639             ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
2640                          server_conf,
2641                          "child pid %d exit signal %d",
2642                          pid, WTERMSIG(status));
2643 #endif
2644         }
2645     }
2646 }
2647
2648
2649 static int setup_listeners(pool *pconf, server_rec *s)
2650 {
2651     ap_listen_rec *lr;
2652
2653     if (ap_listen_open(pconf, s->port)) {
2654         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ALERT, s,
2655                     "no listening sockets available, shutting down");
2656         return -1;
2657     }
2658
2659     listenmaxfd = -1;
2660     FD_ZERO(&listenfds);
2661     for (lr = ap_listeners; lr; lr = lr->next) {
2662         FD_SET(lr->fd, &listenfds);
2663         if (lr->fd > listenmaxfd) {
2664             listenmaxfd = lr->fd;
2665         }
2666     }
2667     return 0;
2668 }
2669
2670
2671 /*****************************************************************
2672  * Executive routines.
2673  */
2674
2675 int ap_mpm_run(pool *_pconf, pool *plog, server_rec *s)
2676 {
2677     int remaining_children_to_start;
2678
2679     pconf = _pconf;
2680
2681     server_conf = s;
2682
2683     ap_log_pid(pconf, ap_pid_fname);
2684
2685     if (setup_listeners(pconf, s)) {
2686         /* XXX: hey, what's the right way for the mpm to indicate a fatal error? */
2687         return 1;
2688     }
2689
2690     SAFE_ACCEPT(accept_mutex_init(pconf));
2691     if (!is_graceful) {
2692         reinit_scoreboard(pconf);
2693     }
2694 #ifdef SCOREBOARD_FILE
2695     else {
2696         ap_scoreboard_fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
2697         ap_note_cleanups_for_fd(pconf, scoreboard_fd);
2698     }
2699 #endif
2700
2701     set_signals();
2702
2703     if (ap_daemons_max_free < ap_daemons_min_free + 1)  /* Don't thrash... */
2704         ap_daemons_max_free = ap_daemons_min_free + 1;
2705
2706     /* If we're doing a graceful_restart then we're going to see a lot
2707         * of children exiting immediately when we get into the main loop
2708         * below (because we just sent them SIGUSR1).  This happens pretty
2709         * rapidly... and for each one that exits we'll start a new one until
2710         * we reach at least daemons_min_free.  But we may be permitted to
2711         * start more than that, so we'll just keep track of how many we're
2712         * supposed to start up without the 1 second penalty between each fork.
2713         */
2714     remaining_children_to_start = ap_daemons_to_start;
2715     if (remaining_children_to_start > ap_daemons_limit) {
2716         remaining_children_to_start = ap_daemons_limit;
2717     }
2718     if (!is_graceful) {
2719         startup_children(remaining_children_to_start);
2720         remaining_children_to_start = 0;
2721     }
2722     else {
2723         /* give the system some time to recover before kicking into
2724             * exponential mode */
2725         hold_off_on_exponential_spawning = 10;
2726     }
2727
2728     ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, server_conf,
2729                 "%s configured -- resuming normal operations",
2730                 ap_get_server_version());
2731     ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, server_conf,
2732                 "Server built: %s", ap_get_server_built());
2733     restart_pending = shutdown_pending = 0;
2734
2735     while (!restart_pending && !shutdown_pending) {
2736         int child_slot;
2737         ap_wait_t status;
2738         int pid = wait_or_timeout(&status);
2739
2740         /* XXX: if it takes longer than 1 second for all our children
2741          * to start up and get into IDLE state then we may spawn an
2742          * extra child
2743          */
2744         if (pid >= 0) {
2745             process_child_status(pid, status);
2746             /* non-fatal death... note that it's gone in the scoreboard. */
2747             ap_sync_scoreboard_image();
2748             child_slot = find_child_by_pid(pid);
2749             if (child_slot >= 0) {
2750                 (void) ap_update_child_status(child_slot, SERVER_DEAD,
2751                                             (request_rec *) NULL);
2752                 if (remaining_children_to_start
2753                     && child_slot < ap_daemons_limit) {
2754                     /* we're still doing a 1-for-1 replacement of dead
2755                         * children with new children
2756                         */
2757                     make_child(server_conf, child_slot, time(0));
2758                     --remaining_children_to_start;
2759                 }
2760 #ifdef HAS_OTHER_CHILD
2761             }
2762             else if (reap_other_child(pid, status) == 0) {
2763                 /* handled */
2764 #endif
2765             }
2766             else if (is_graceful) {
2767                 /* Great, we've probably just lost a slot in the
2768                     * scoreboard.  Somehow we don't know about this
2769                     * child.
2770                     */
2771                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, server_conf,
2772                             "long lost child came home! (pid %d)", pid);
2773             }
2774             /* Don't perform idle maintenance when a child dies,
2775                 * only do it when there's a timeout.  Remember only a
2776                 * finite number of children can die, and it's pretty
2777                 * pathological for a lot to die suddenly.
2778                 */
2779             continue;
2780         }
2781         else if (remaining_children_to_start) {
2782             /* we hit a 1 second timeout in which none of the previous
2783                 * generation of children needed to be reaped... so assume
2784                 * they're all done, and pick up the slack if any is left.
2785                 */
2786             startup_children(remaining_children_to_start);
2787             remaining_children_to_start = 0;
2788             /* In any event we really shouldn't do the code below because
2789                 * few of the servers we just started are in the IDLE state
2790                 * yet, so we'd mistakenly create an extra server.
2791                 */
2792             continue;
2793         }
2794
2795         perform_idle_server_maintenance();
2796 #ifdef TPF
2797     shutdown_pending = os_check_server(tpf_server_name);
2798     ap_check_signals();
2799     sleep(1);
2800 #endif /*TPF */
2801     }
2802
2803     if (shutdown_pending) {
2804         /* Time to gracefully shut down:
2805          * Kill child processes, tell them to call child_exit, etc...
2806          */
2807         if (ap_killpg(getpgrp(), SIGTERM) < 0) {
2808             ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "killpg SIGTERM");
2809         }
2810         reclaim_child_processes(1);             /* Start with SIGTERM */
2811
2812         /* cleanup pid file on normal shutdown */
2813         {
2814             const char *pidfile = NULL;
2815             pidfile = ap_server_root_relative (pconf, ap_pid_fname);
2816             if ( pidfile != NULL && unlink(pidfile) == 0)
2817                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO,
2818                                 server_conf,
2819                                 "removed PID file %s (pid=%ld)",
2820                                 pidfile, (long)getpid());
2821         }
2822
2823         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, server_conf,
2824                     "caught SIGTERM, shutting down");
2825         return 1;
2826     }
2827
2828     /* we've been told to restart */
2829     signal(SIGHUP, SIG_IGN);
2830     signal(SIGUSR1, SIG_IGN);
2831
2832     if (one_process) {
2833         /* not worth thinking about */
2834         return 1;
2835     }
2836
2837     /* advance to the next generation */
2838     /* XXX: we really need to make sure this new generation number isn't in
2839      * use by any of the children.
2840      */
2841     ++ap_my_generation;
2842     ap_scoreboard_image->global.running_generation = ap_my_generation;
2843     update_scoreboard_global();
2844
2845     if (is_graceful) {
2846 #ifndef SCOREBOARD_FILE
2847         int i;
2848 #endif
2849         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, server_conf,
2850                     "SIGUSR1 received.  Doing graceful restart");
2851
2852         /* kill off the idle ones */
2853         if (ap_killpg(getpgrp(), SIGUSR1) < 0) {
2854             ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "killpg SIGUSR1");
2855         }
2856 #ifndef SCOREBOARD_FILE
2857         /* This is mostly for debugging... so that we know what is still
2858             * gracefully dealing with existing request.  But we can't really
2859             * do it if we're in a SCOREBOARD_FILE because it'll cause
2860             * corruption too easily.
2861             */
2862         ap_sync_scoreboard_image();
2863         for (i = 0; i < ap_daemons_limit; ++i) {
2864             if (ap_scoreboard_image->servers[i].status != SERVER_DEAD) {
2865                 ap_scoreboard_image->servers[i].status = SERVER_GRACEFUL;
2866             }
2867         }
2868 #endif
2869     }
2870     else {
2871         /* Kill 'em off */
2872         if (ap_killpg(getpgrp(), SIGHUP) < 0) {
2873             ap_log_error(APLOG_MARK, APLOG_WARNING, server_conf, "killpg SIGHUP");
2874         }
2875         reclaim_child_processes(0);             /* Not when just starting up */
2876         ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, server_conf,
2877                     "SIGHUP received.  Attempting to restart");
2878     }
2879
2880     if (!is_graceful) {
2881         ap_restart_time = time(NULL);
2882     }
2883
2884     return 0;
2885 }
2886
2887 static void prefork_pre_config(pool *pconf, pool *plog, pool *ptemp)
2888 {
2889     static int restart_num = 0;
2890
2891     one_process = !!getenv("ONE_PROCESS");
2892
2893     /* sigh, want this only the second time around */
2894     if (restart_num++ == 1) {
2895         is_graceful = 0;
2896
2897         if (!one_process) {
2898             unixd_detach();
2899         }
2900
2901         my_pid = getpid();
2902     }
2903
2904     unixd_pre_config();
2905     ap_listen_pre_config();
2906     ap_daemons_to_start = DEFAULT_START_DAEMON;
2907     ap_daemons_min_free = DEFAULT_MIN_FREE_DAEMON;
2908     ap_daemons_max_free = DEFAULT_MAX_FREE_DAEMON;
2909     ap_daemons_limit = HARD_SERVER_LIMIT;
2910     ap_pid_fname = DEFAULT_PIDLOG;
2911     ap_scoreboard_fname = DEFAULT_SCOREBOARD;
2912     ap_lock_fname = DEFAULT_LOCKFILE;
2913     ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD;
2914     ap_extended_status = 0;
2915
2916     ap_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));
2917 }
2918
2919 static void prefork_hooks(void)
2920 {
2921     ap_hook_pre_config(prefork_pre_config,NULL,NULL,HOOK_MIDDLE);
2922     INIT_SIGLIST();
2923 #ifdef AUX3
2924     (void) set42sig();
2925 #endif
2926     /* TODO: set one_process properly */ one_process = 0;
2927 }
2928
2929 static const char *set_pidfile(cmd_parms *cmd, void *dummy, char *arg) 
2930 {
2931     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2932     if (err != NULL) {
2933         return err;
2934     }
2935
2936     if (cmd->server->is_virtual) {
2937         return "PidFile directive not allowed in <VirtualHost>";
2938     }
2939     ap_pid_fname = arg;
2940     return NULL;
2941 }
2942
2943 static const char *set_scoreboard(cmd_parms *cmd, void *dummy, char *arg) 
2944 {
2945     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2946     if (err != NULL) {
2947         return err;
2948     }
2949
2950     ap_scoreboard_fname = arg;
2951     return NULL;
2952 }
2953
2954 static const char *set_lockfile(cmd_parms *cmd, void *dummy, char *arg) 
2955 {
2956     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2957     if (err != NULL) {
2958         return err;
2959     }
2960
2961     ap_lock_fname = arg;
2962     return NULL;
2963 }
2964
2965 static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy, char *arg) 
2966 {
2967     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2968     if (err != NULL) {
2969         return err;
2970     }
2971
2972     ap_daemons_to_start = atoi(arg);
2973     return NULL;
2974 }
2975
2976 static const char *set_min_free_servers(cmd_parms *cmd, void *dummy, char *arg)
2977 {
2978     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2979     if (err != NULL) {
2980         return err;
2981     }
2982
2983     ap_daemons_min_free = atoi(arg);
2984     if (ap_daemons_min_free <= 0) {
2985        fprintf(stderr, "WARNING: detected MinSpareServers set to non-positive.\n");
2986        fprintf(stderr, "Resetting to 1 to avoid almost certain Apache failure.\n");
2987        fprintf(stderr, "Please read the documentation.\n");
2988        ap_daemons_min_free = 1;
2989     }
2990        
2991     return NULL;
2992 }
2993
2994 static const char *set_max_free_servers(cmd_parms *cmd, void *dummy, char *arg)
2995 {
2996     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2997     if (err != NULL) {
2998         return err;
2999     }
3000
3001     ap_daemons_max_free = atoi(arg);
3002     return NULL;
3003 }
3004
3005 static const char *set_server_limit (cmd_parms *cmd, void *dummy, char *arg) 
3006 {
3007     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3008     if (err != NULL) {
3009         return err;
3010     }
3011
3012     ap_daemons_limit = atoi(arg);
3013     if (ap_daemons_limit > HARD_SERVER_LIMIT) {
3014        fprintf(stderr, "WARNING: MaxClients of %d exceeds compile time limit "
3015            "of %d servers,\n", ap_daemons_limit, HARD_SERVER_LIMIT);
3016        fprintf(stderr, " lowering MaxClients to %d.  To increase, please "
3017            "see the\n", HARD_SERVER_LIMIT);
3018        fprintf(stderr, " HARD_SERVER_LIMIT define in src/include/httpd.h.\n");
3019        ap_daemons_limit = HARD_SERVER_LIMIT;
3020     } 
3021     else if (ap_daemons_limit < 1) {
3022         fprintf(stderr, "WARNING: Require MaxClients > 0, setting to 1\n");
3023         ap_daemons_limit = 1;
3024     }
3025     return NULL;
3026 }
3027
3028 static const char *set_max_requests(cmd_parms *cmd, void *dummy, char *arg) 
3029 {
3030     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3031     if (err != NULL) {
3032         return err;
3033     }
3034
3035     ap_max_requests_per_child = atoi(arg);
3036
3037     return NULL;
3038 }
3039
3040 static const char *set_coredumpdir (cmd_parms *cmd, void *dummy, char *arg) 
3041 {
3042     struct stat finfo;
3043     const char *fname;
3044     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3045     if (err != NULL) {
3046         return err;
3047     }
3048
3049     fname = ap_server_root_relative(cmd->pool, arg);
3050     /* ZZZ change this to the AP func FileInfo*/
3051     if ((stat(fname, &finfo) == -1) || !S_ISDIR(finfo.st_mode)) {
3052         return ap_pstrcat(cmd->pool, "CoreDumpDirectory ", fname, 
3053                           " does not exist or is not a directory", NULL);
3054     }
3055     ap_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
3056     return NULL;
3057 }
3058
3059 /* there are no threads in the prefork model, so the mutexes are
3060    nops. */
3061 /* TODO: make these #defines to eliminate the function call */
3062
3063 struct ap_thread_mutex {
3064     int dummy;
3065 };
3066
3067 API_EXPORT(ap_thread_mutex *) ap_thread_mutex_new(void)
3068 {
3069     return malloc(sizeof(ap_thread_mutex));
3070 }
3071
3072 API_EXPORT(void) ap_thread_mutex_lock(ap_thread_mutex *mtx)
3073 {
3074 }
3075
3076 API_EXPORT(void) ap_thread_mutex_unlock(ap_thread_mutex *mtx)
3077 {
3078 }
3079
3080 API_EXPORT(void) ap_thread_mutex_destroy(ap_thread_mutex *mtx)
3081 {
3082     free(mtx);
3083 }
3084
3085
3086 static const command_rec prefork_cmds[] = {
3087 UNIX_DAEMON_COMMANDS
3088 LISTEN_COMMANDS
3089 { "PidFile", set_pidfile, NULL, RSRC_CONF, TAKE1,
3090     "A file for logging the server process ID"},
3091 { "ScoreBoardFile", set_scoreboard, NULL, RSRC_CONF, TAKE1,
3092     "A file for Apache to maintain runtime process management information"},
3093 { "LockFile", set_lockfile, NULL, RSRC_CONF, TAKE1,
3094     "The lockfile used when Apache needs to lock the accept() call"},
3095 { "StartServers", set_daemons_to_start, NULL, RSRC_CONF, TAKE1,
3096   "Number of child processes launched at server startup" },
3097 { "MinSpareServers", set_min_free_servers, NULL, RSRC_CONF, TAKE1,
3098   "Minimum number of idle children, to handle request spikes" },
3099 { "MaxSpareServers", set_max_free_servers, NULL, RSRC_CONF, TAKE1,
3100   "Maximum number of idle children" },
3101 { "MaxClients", set_server_limit, NULL, RSRC_CONF, TAKE1,
3102   "Maximum number of children alive at the same time" },
3103 { "MaxRequestsPerChild", set_max_requests, NULL, RSRC_CONF, TAKE1,
3104   "Maximum number of requests a particular child serves before dying." },
3105 { "CoreDumpDirectory", set_coredumpdir, NULL, RSRC_CONF, TAKE1,
3106   "The location of the directory Apache changes to before dumping core" },
3107 { NULL }
3108 };
3109
3110 module MODULE_VAR_EXPORT mpm_prefork_module = {
3111     STANDARD20_MODULE_STUFF,
3112     NULL,                       /* child_init */
3113     NULL,                       /* create per-directory config structure */
3114     NULL,                       /* merge per-directory config structures */
3115     NULL,                       /* create per-server config structure */
3116     NULL,                       /* merge per-server config structures */
3117     prefork_cmds,               /* command table */
3118     NULL,                       /* handlers */
3119     NULL,                       /* check auth */
3120     NULL,                       /* check access */
3121     prefork_hooks,              /* register hooks */
3122 };