]> granicus.if.org Git - apache/blob - server/mpm_unix.c
Correct build order. Seems mod_session was misimplemented to require
[apache] / server / mpm_unix.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* The purpose of this file is to store the code that MOST mpm's will need
18  * this does not mean a function only goes into this file if every MPM needs
19  * it.  It means that if a function is needed by more than one MPM, and
20  * future maintenance would be served by making the code common, then the
21  * function belongs here.
22  *
23  * This is going in src/main because it is not platform specific, it is
24  * specific to multi-process servers, but NOT to Unix.  Which is why it
25  * does not belong in src/os/unix
26  */
27
28 #include "apr.h"
29 #include "apr_thread_proc.h"
30 #include "apr_signal.h"
31 #include "apr_strings.h"
32 #define APR_WANT_STRFUNC
33 #include "apr_want.h"
34 #include "apr_getopt.h"
35 #include "apr_optional.h"
36 #include "apr_allocator.h"
37
38 #include "httpd.h"
39 #include "http_config.h"
40 #include "http_log.h"
41 #include "http_main.h"
42 #include "mpm_common.h"
43 #include "ap_mpm.h"
44 #include "ap_listen.h"
45 #include "scoreboard.h"
46 #include "util_mutex.h"
47
48 #ifdef HAVE_PWD_H
49 #include <pwd.h>
50 #endif
51 #ifdef HAVE_GRP_H
52 #include <grp.h>
53 #endif
54 #if APR_HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57
58
59 APLOG_USE_MODULE(core);
60
61 typedef enum {DO_NOTHING, SEND_SIGTERM, SEND_SIGKILL, GIVEUP} action_t;
62
63 typedef struct extra_process_t {
64     struct extra_process_t *next;
65     pid_t pid;
66 } extra_process_t;
67
68 static extra_process_t *extras;
69
70 void ap_register_extra_mpm_process(pid_t pid)
71 {
72     extra_process_t *p = (extra_process_t *)malloc(sizeof(extra_process_t));
73
74     p->next = extras;
75     p->pid = pid;
76     extras = p;
77 }
78
79 int ap_unregister_extra_mpm_process(pid_t pid)
80 {
81     extra_process_t *cur = extras;
82     extra_process_t *prev = NULL;
83
84     while (cur && cur->pid != pid) {
85         prev = cur;
86         cur = cur->next;
87     }
88
89     if (cur) {
90         if (prev) {
91             prev->next = cur->next;
92         }
93         else {
94             extras = cur->next;
95         }
96         free(cur);
97         return 1; /* found */
98     }
99     else {
100         /* we don't know about any such process */
101         return 0;
102     }
103 }
104
105 static int reclaim_one_pid(pid_t pid, action_t action)
106 {
107     apr_proc_t proc;
108     apr_status_t waitret;
109     apr_exit_why_e why;
110     int status;
111
112     /* Ensure pid sanity. */
113     if (pid < 1) {
114         return 1;
115     }        
116
117     proc.pid = pid;
118     waitret = apr_proc_wait(&proc, &status, &why, APR_NOWAIT);
119     if (waitret != APR_CHILD_NOTDONE) {
120         if (waitret == APR_CHILD_DONE)
121             ap_process_child_status(&proc, why, status);
122         return 1;
123     }
124
125     switch(action) {
126     case DO_NOTHING:
127         break;
128
129     case SEND_SIGTERM:
130         /* ok, now it's being annoying */
131         ap_log_error(APLOG_MARK, APLOG_WARNING,
132                      0, ap_server_conf,
133                      "child process %" APR_PID_T_FMT
134                      " still did not exit, "
135                      "sending a SIGTERM",
136                      pid);
137         kill(pid, SIGTERM);
138         break;
139
140     case SEND_SIGKILL:
141         ap_log_error(APLOG_MARK, APLOG_ERR,
142                      0, ap_server_conf,
143                      "child process %" APR_PID_T_FMT
144                      " still did not exit, "
145                      "sending a SIGKILL",
146                      pid);
147         kill(pid, SIGKILL);
148         break;
149
150     case GIVEUP:
151         /* gave it our best shot, but alas...  If this really
152          * is a child we are trying to kill and it really hasn't
153          * exited, we will likely fail to bind to the port
154          * after the restart.
155          */
156         ap_log_error(APLOG_MARK, APLOG_ERR,
157                      0, ap_server_conf,
158                      "could not make child process %" APR_PID_T_FMT
159                      " exit, "
160                      "attempting to continue anyway",
161                      pid);
162         break;
163     }
164
165     return 0;
166 }
167
168 void ap_reclaim_child_processes(int terminate)
169 {
170     apr_time_t waittime = 1024 * 16;
171     int i;
172     extra_process_t *cur_extra;
173     int not_dead_yet;
174     int max_daemons;
175     apr_time_t starttime = apr_time_now();
176     /* this table of actions and elapsed times tells what action is taken
177      * at which elapsed time from starting the reclaim
178      */
179     struct {
180         action_t action;
181         apr_time_t action_time;
182     } action_table[] = {
183         {DO_NOTHING, 0}, /* dummy entry for iterations where we reap
184                           * children but take no action against
185                           * stragglers
186                           */
187         {SEND_SIGTERM, apr_time_from_sec(3)},
188         {SEND_SIGTERM, apr_time_from_sec(5)},
189         {SEND_SIGTERM, apr_time_from_sec(7)},
190         {SEND_SIGKILL, apr_time_from_sec(9)},
191         {GIVEUP,       apr_time_from_sec(10)}
192     };
193     int cur_action;      /* index of action we decided to take this
194                           * iteration
195                           */
196     int next_action = 1; /* index of first real action */
197
198     ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
199
200     do {
201         apr_sleep(waittime);
202         /* don't let waittime get longer than 1 second; otherwise, we don't
203          * react quickly to the last child exiting, and taking action can
204          * be delayed
205          */
206         waittime = waittime * 4;
207         if (waittime > apr_time_from_sec(1)) {
208             waittime = apr_time_from_sec(1);
209         }
210
211         /* see what action to take, if any */
212         if (action_table[next_action].action_time <= apr_time_now() - starttime) {
213             cur_action = next_action;
214             ++next_action;
215         }
216         else {
217             cur_action = 0; /* nothing to do */
218         }
219
220         /* now see who is done */
221         not_dead_yet = 0;
222         for (i = 0; i < max_daemons; ++i) {
223             process_score *ps = ap_get_scoreboard_process(i);
224             pid_t pid = ps->pid;
225
226             if (pid == 0) {
227                 continue; /* not every scoreboard entry is in use */
228             }
229
230             if (reclaim_one_pid(pid, action_table[cur_action].action)) {
231                 ap_mpm_note_child_killed(i);
232             }
233             else {
234                 ++not_dead_yet;
235             }
236         }
237
238         cur_extra = extras;
239         while (cur_extra) {
240             extra_process_t *next = cur_extra->next;
241
242             if (reclaim_one_pid(cur_extra->pid, action_table[cur_action].action)) {
243                 AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid));
244             }
245             else {
246                 ++not_dead_yet;
247             }
248             cur_extra = next;
249         }
250 #if APR_HAS_OTHER_CHILD
251         apr_proc_other_child_refresh_all(APR_OC_REASON_RESTART);
252 #endif
253
254     } while (not_dead_yet > 0 &&
255              action_table[cur_action].action != GIVEUP);
256 }
257
258 void ap_relieve_child_processes(void)
259 {
260     int i;
261     extra_process_t *cur_extra;
262     int max_daemons;
263
264     ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
265
266     /* now see who is done */
267     for (i = 0; i < max_daemons; ++i) {
268         process_score *ps = ap_get_scoreboard_process(i);
269         pid_t pid = ps->pid;
270
271         if (pid == 0) {
272             continue; /* not every scoreboard entry is in use */
273         }
274
275         if (reclaim_one_pid(pid, DO_NOTHING)) {
276             ap_mpm_note_child_killed(i);
277         }
278     }
279
280     cur_extra = extras;
281     while (cur_extra) {
282         extra_process_t *next = cur_extra->next;
283
284         if (reclaim_one_pid(cur_extra->pid, DO_NOTHING)) {
285             AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid));
286         }
287         cur_extra = next;
288     }
289 }
290
291 /* Before sending the signal to the pid this function verifies that
292  * the pid is a member of the current process group; either using
293  * apr_proc_wait(), where waitpid() guarantees to fail for non-child
294  * processes; or by using getpgid() directly, if available. */
295 apr_status_t ap_mpm_safe_kill(pid_t pid, int sig)
296 {
297 #ifndef HAVE_GETPGID
298     apr_proc_t proc;
299     apr_status_t rv;
300     apr_exit_why_e why;
301     int status;
302
303     /* Ensure pid sanity */
304     if (pid < 1) {
305         return APR_EINVAL;
306     }
307
308     proc.pid = pid;
309     rv = apr_proc_wait(&proc, &status, &why, APR_NOWAIT);
310     if (rv == APR_CHILD_DONE) {
311         /* The child already died - log the termination status if
312          * necessary: */
313         ap_process_child_status(&proc, why, status);
314         return APR_EINVAL;
315     }
316     else if (rv != APR_CHILD_NOTDONE) {
317         /* The child is already dead and reaped, or was a bogus pid -
318          * log this either way. */
319         ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, ap_server_conf,
320                      "cannot send signal %d to pid %ld (non-child or "
321                      "already dead)", sig, (long)pid);
322         return APR_EINVAL;
323     }
324 #else
325     pid_t pg;
326
327     /* Ensure pid sanity. */
328     if (pid < 1) {
329         return APR_EINVAL;
330     }
331
332     pg = getpgid(pid);    
333     if (pg == -1) {
334         /* Process already dead... */
335         return errno;
336     }
337
338     if (pg != getpgrp()) {
339         ap_log_error(APLOG_MARK, APLOG_ALERT, 0, ap_server_conf,
340                      "refusing to send signal %d to pid %ld outside "
341                      "process group", sig, (long)pid);
342         return APR_EINVAL;
343     }
344 #endif        
345
346     return kill(pid, sig) ? errno : APR_SUCCESS;
347 }
348
349
350 int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
351 {
352     int signum = status;
353     const char *sigdesc;
354
355     /* Child died... if it died due to a fatal error,
356      * we should simply bail out.  The caller needs to
357      * check for bad rc from us and exit, running any
358      * appropriate cleanups.
359      *
360      * If the child died due to a resource shortage,
361      * the parent should limit the rate of forking
362      */
363     if (APR_PROC_CHECK_EXIT(why)) {
364         if (status == APEXIT_CHILDSICK) {
365             return status;
366         }
367
368         if (status == APEXIT_CHILDFATAL) {
369             ap_log_error(APLOG_MARK, APLOG_ALERT,
370                          0, ap_server_conf,
371                          "Child %" APR_PID_T_FMT
372                          " returned a Fatal error... Apache is exiting!",
373                          pid->pid);
374             return APEXIT_CHILDFATAL;
375         }
376
377         return 0;
378     }
379
380     if (APR_PROC_CHECK_SIGNALED(why)) {
381         sigdesc = apr_signal_description_get(signum);
382
383         switch (signum) {
384         case SIGTERM:
385         case SIGHUP:
386         case AP_SIG_GRACEFUL:
387         case SIGKILL:
388             break;
389
390         default:
391             if (APR_PROC_CHECK_CORE_DUMP(why)) {
392                 ap_log_error(APLOG_MARK, APLOG_NOTICE,
393                              0, ap_server_conf,
394                              "child pid %ld exit signal %s (%d), "
395                              "possible coredump in %s",
396                              (long)pid->pid, sigdesc, signum,
397                              ap_coredump_dir);
398             }
399             else {
400                 ap_log_error(APLOG_MARK, APLOG_NOTICE,
401                              0, ap_server_conf,
402                              "child pid %ld exit signal %s (%d)",
403                              (long)pid->pid, sigdesc, signum);
404             }
405         }
406     }
407     return 0;
408 }
409
410 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
411 {
412     apr_status_t rv;
413
414     *pod = apr_palloc(p, sizeof(**pod));
415     rv = apr_file_pipe_create_ex(&((*pod)->pod_in), &((*pod)->pod_out),
416                                  APR_WRITE_BLOCK, p);
417     if (rv != APR_SUCCESS) {
418         return rv;
419     }
420
421     apr_file_pipe_timeout_set((*pod)->pod_in, 0);
422     (*pod)->p = p;
423
424     /* close these before exec. */
425     apr_file_inherit_unset((*pod)->pod_in);
426     apr_file_inherit_unset((*pod)->pod_out);
427
428     return APR_SUCCESS;
429 }
430
431 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
432 {
433     char c;
434     apr_size_t len = 1;
435     apr_status_t rv;
436
437     rv = apr_file_read(pod->pod_in, &c, &len);
438
439     if ((rv == APR_SUCCESS) && (len == 1)) {
440         return APR_SUCCESS;
441     }
442
443     if (rv != APR_SUCCESS) {
444         return rv;
445     }
446
447     return AP_NORESTART;
448 }
449
450 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
451 {
452     apr_status_t rv;
453
454     rv = apr_file_close(pod->pod_out);
455     if (rv != APR_SUCCESS) {
456         return rv;
457     }
458
459     rv = apr_file_close(pod->pod_in);
460     if (rv != APR_SUCCESS) {
461         return rv;
462     }
463
464     return APR_SUCCESS;
465 }
466
467 static apr_status_t pod_signal_internal(ap_pod_t *pod)
468 {
469     apr_status_t rv;
470     char char_of_death = '!';
471     apr_size_t one = 1;
472
473     rv = apr_file_write(pod->pod_out, &char_of_death, &one);
474     if (rv != APR_SUCCESS) {
475         ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
476                      "write pipe_of_death");
477     }
478
479     return rv;
480 }
481
482 /* This function connects to the server, then immediately closes the connection.
483  * This permits the MPM to skip the poll when there is only one listening
484  * socket, because it provides a alternate way to unblock an accept() when
485  * the pod is used.
486  */
487 static apr_status_t dummy_connection(ap_pod_t *pod)
488 {
489     char *srequest;
490     apr_status_t rv;
491     apr_socket_t *sock;
492     apr_pool_t *p;
493     apr_size_t len;
494     ap_listen_rec *lp;
495
496     /* create a temporary pool for the socket.  pconf stays around too long */
497     rv = apr_pool_create(&p, pod->p);
498     if (rv != APR_SUCCESS) {
499         return rv;
500     }
501
502     /* If possible, find a listener which is configured for
503      * plain-HTTP, not SSL; using an SSL port would either be
504      * expensive to do correctly (performing a complete SSL handshake)
505      * or cause log spam by doing incorrectly (simply sending EOF). */
506     lp = ap_listeners;
507     while (lp && lp->protocol && strcasecmp(lp->protocol, "http") != 0) {
508         lp = lp->next;
509     }
510     if (!lp) {
511         lp = ap_listeners;
512     }
513
514     rv = apr_socket_create(&sock, lp->bind_addr->family, SOCK_STREAM, 0, p);
515     if (rv != APR_SUCCESS) {
516         ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
517                      "get socket to connect to listener");
518         apr_pool_destroy(p);
519         return rv;
520     }
521
522     /* on some platforms (e.g., FreeBSD), the kernel won't accept many
523      * queued connections before it starts blocking local connects...
524      * we need to keep from blocking too long and instead return an error,
525      * because the MPM won't want to hold up a graceful restart for a
526      * long time
527      */
528     rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
529     if (rv != APR_SUCCESS) {
530         ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
531                      "set timeout on socket to connect to listener");
532         apr_socket_close(sock);
533         apr_pool_destroy(p);
534         return rv;
535     }
536
537     rv = apr_socket_connect(sock, lp->bind_addr);
538     if (rv != APR_SUCCESS) {
539         int log_level = APLOG_WARNING;
540
541         if (APR_STATUS_IS_TIMEUP(rv)) {
542             /* probably some server processes bailed out already and there
543              * is nobody around to call accept and clear out the kernel
544              * connection queue; usually this is not worth logging
545              */
546             log_level = APLOG_DEBUG;
547         }
548
549         ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
550                      "connect to listener on %pI", lp->bind_addr);
551     }
552
553     /* Create the request string. We include a User-Agent so that
554      * adminstrators can track down the cause of the odd-looking
555      * requests in their logs.
556      */
557     srequest = apr_pstrcat(p, "OPTIONS * HTTP/1.0\r\nUser-Agent: ",
558                            ap_get_server_description(),
559                            " (internal dummy connection)\r\n\r\n", NULL);
560
561     /* Since some operating systems support buffering of data or entire
562      * requests in the kernel, we send a simple request, to make sure
563      * the server pops out of a blocking accept().
564      */
565     /* XXX: This is HTTP specific. We should look at the Protocol for each
566      * listener, and send the correct type of request to trigger any Accept
567      * Filters.
568      */
569     len = strlen(srequest);
570     apr_socket_send(sock, srequest, &len);
571     apr_socket_close(sock);
572     apr_pool_destroy(p);
573
574     return rv;
575 }
576
577 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
578 {
579     apr_status_t rv;
580
581     rv = pod_signal_internal(pod);
582     if (rv != APR_SUCCESS) {
583         return rv;
584     }
585
586     return dummy_connection(pod);
587 }
588
589 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
590 {
591     int i;
592     apr_status_t rv = APR_SUCCESS;
593
594     /* we don't write anything to the pod here...  we assume
595      * that the would-be reader of the pod has another way to
596      * see that it is time to die once we wake it up
597      *
598      * writing lots of things to the pod at once is very
599      * problematic... we can fill the kernel pipe buffer and
600      * be blocked until somebody consumes some bytes or
601      * we hit a timeout...  if we hit a timeout we can't just
602      * keep trying because maybe we'll never successfully
603      * write again...  but then maybe we'll leave would-be
604      * readers stranded (a number of them could be tied up for
605      * a while serving time-consuming requests)
606      */
607     for (i = 0; i < num && rv == APR_SUCCESS; i++) {
608         rv = dummy_connection(pod);
609     }
610 }
611
612 static const char *dash_k_arg = NULL;
613 static const char *dash_k_arg_noarg = "noarg";
614
615 static int send_signal(pid_t pid, int sig)
616 {
617     if (kill(pid, sig) < 0) {
618         ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL,
619                      "sending signal to server");
620         return 1;
621     }
622     return 0;
623 }
624
625 int ap_signal_server(int *exit_status, apr_pool_t *pconf)
626 {
627     apr_status_t rv;
628     pid_t otherpid;
629     int running = 0;
630     const char *status;
631
632     *exit_status = 0;
633
634     rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
635     if (rv != APR_SUCCESS) {
636         if (rv != APR_ENOENT) {
637             ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL,
638                          "Error retrieving pid file %s", ap_pid_fname);
639             ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
640                          "Remove it before continuing if it is corrupted.");
641             *exit_status = 1;
642             return 1;
643         }
644         status = "httpd (no pid file) not running";
645     }
646     else {
647         if (kill(otherpid, 0) == 0) {
648             running = 1;
649             status = apr_psprintf(pconf,
650                                   "httpd (pid %" APR_PID_T_FMT ") already "
651                                   "running", otherpid);
652         }
653         else {
654             status = apr_psprintf(pconf,
655                                   "httpd (pid %" APR_PID_T_FMT "?) not running",
656                                   otherpid);
657         }
658     }
659
660     if (!strcmp(dash_k_arg, "start") || dash_k_arg == dash_k_arg_noarg) {
661         if (running) {
662             printf("%s\n", status);
663             return 1;
664         }
665     }
666
667     if (!strcmp(dash_k_arg, "stop")) {
668         if (!running) {
669             printf("%s\n", status);
670         }
671         else {
672             send_signal(otherpid, SIGTERM);
673         }
674         return 1;
675     }
676
677     if (!strcmp(dash_k_arg, "restart")) {
678         if (!running) {
679             printf("httpd not running, trying to start\n");
680         }
681         else {
682             *exit_status = send_signal(otherpid, SIGHUP);
683             return 1;
684         }
685     }
686
687     if (!strcmp(dash_k_arg, "graceful")) {
688         if (!running) {
689             printf("httpd not running, trying to start\n");
690         }
691         else {
692             *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL);
693             return 1;
694         }
695     }
696
697     if (!strcmp(dash_k_arg, "graceful-stop")) {
698         if (!running) {
699             printf("%s\n", status);
700         }
701         else {
702             *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL_STOP);
703         }
704         return 1;
705     }
706
707     return 0;
708 }
709
710 void ap_mpm_rewrite_args(process_rec *process)
711 {
712     apr_array_header_t *mpm_new_argv;
713     apr_status_t rv;
714     apr_getopt_t *opt;
715     char optbuf[3];
716     const char *optarg;
717
718     mpm_new_argv = apr_array_make(process->pool, process->argc,
719                                   sizeof(const char **));
720     *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
721     apr_getopt_init(&opt, process->pool, process->argc, process->argv);
722     opt->errfn = NULL;
723     optbuf[0] = '-';
724     /* option char returned by apr_getopt() will be stored in optbuf[1] */
725     optbuf[2] = '\0';
726     while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
727                             optbuf + 1, &optarg)) == APR_SUCCESS) {
728         switch(optbuf[1]) {
729         case 'k':
730             if (!dash_k_arg) {
731                 if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
732                     !strcmp(optarg, "restart") || !strcmp(optarg, "graceful") ||
733                     !strcmp(optarg, "graceful-stop")) {
734                     dash_k_arg = optarg;
735                     break;
736                 }
737             }
738         default:
739             *(const char **)apr_array_push(mpm_new_argv) =
740                 apr_pstrdup(process->pool, optbuf);
741             if (optarg) {
742                 *(const char **)apr_array_push(mpm_new_argv) = optarg;
743             }
744         }
745     }
746
747     /* back up to capture the bad argument */
748     if (rv == APR_BADCH || rv == APR_BADARG) {
749         opt->ind--;
750     }
751
752     while (opt->ind < opt->argc) {
753         *(const char **)apr_array_push(mpm_new_argv) =
754             apr_pstrdup(process->pool, opt->argv[opt->ind++]);
755     }
756
757     process->argc = mpm_new_argv->nelts;
758     process->argv = (const char * const *)mpm_new_argv->elts;
759   
760     if (NULL == dash_k_arg) { 
761         dash_k_arg = dash_k_arg_noarg;
762     }
763
764     APR_REGISTER_OPTIONAL_FN(ap_signal_server);
765 }
766
767 static pid_t parent_pid, my_pid;
768 static apr_pool_t *pconf;
769
770 #if AP_ENABLE_EXCEPTION_HOOK
771
772 static int exception_hook_enabled;
773
774 const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
775                                       const char *arg)
776 {
777     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
778     if (err != NULL) {
779         return err;
780     }
781
782     if (cmd->server->is_virtual) {
783         return "EnableExceptionHook directive not allowed in <VirtualHost>";
784     }
785
786     if (strcasecmp(arg, "on") == 0) {
787         exception_hook_enabled = 1;
788     }
789     else if (strcasecmp(arg, "off") == 0) {
790         exception_hook_enabled = 0;
791     }
792     else {
793         return "parameter must be 'on' or 'off'";
794     }
795
796     return NULL;
797 }
798
799 static void run_fatal_exception_hook(int sig)
800 {
801     ap_exception_info_t ei = {0};
802
803     if (exception_hook_enabled &&
804         geteuid() != 0 &&
805         my_pid != parent_pid) {
806         ei.sig = sig;
807         ei.pid = my_pid;
808         ap_run_fatal_exception(&ei);
809     }
810 }
811 #endif /* AP_ENABLE_EXCEPTION_HOOK */
812
813 /* handle all varieties of core dumping signals */
814 static void sig_coredump(int sig)
815 {
816     apr_filepath_set(ap_coredump_dir, pconf);
817     apr_signal(sig, SIG_DFL);
818 #if AP_ENABLE_EXCEPTION_HOOK
819     run_fatal_exception_hook(sig);
820 #endif
821     /* linuxthreads issue calling getpid() here:
822      *   This comparison won't match if the crashing thread is
823      *   some module's thread that runs in the parent process.
824      *   The fallout, which is limited to linuxthreads:
825      *   The special log message won't be written when such a
826      *   thread in the parent causes the parent to crash.
827      */
828     if (getpid() == parent_pid) {
829         ap_log_error(APLOG_MARK, APLOG_NOTICE,
830                      0, ap_server_conf,
831                      "seg fault or similar nasty error detected "
832                      "in the parent process");
833         /* XXX we can probably add some rudimentary cleanup code here,
834          * like getting rid of the pid file.  If any additional bad stuff
835          * happens, we are protected from recursive errors taking down the
836          * system since this function is no longer the signal handler   GLA
837          */
838     }
839     kill(getpid(), sig);
840     /* At this point we've got sig blocked, because we're still inside
841      * the signal handler.  When we leave the signal handler it will
842      * be unblocked, and we'll take the signal... and coredump or whatever
843      * is appropriate for this particular Unix.  In addition the parent
844      * will see the real signal we received -- whereas if we called
845      * abort() here, the parent would only see SIGABRT.
846      */
847 }
848
849 apr_status_t ap_fatal_signal_child_setup(server_rec *s)
850 {
851     my_pid = getpid();
852     return APR_SUCCESS;
853 }
854
855 apr_status_t ap_fatal_signal_setup(server_rec *s, apr_pool_t *in_pconf)
856 {
857 #ifndef NO_USE_SIGACTION
858     struct sigaction sa;
859
860     sigemptyset(&sa.sa_mask);
861
862 #if defined(SA_ONESHOT)
863     sa.sa_flags = SA_ONESHOT;
864 #elif defined(SA_RESETHAND)
865     sa.sa_flags = SA_RESETHAND;
866 #else
867     sa.sa_flags = 0;
868 #endif
869
870     sa.sa_handler = sig_coredump;
871     if (sigaction(SIGSEGV, &sa, NULL) < 0)
872         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGSEGV)");
873 #ifdef SIGBUS
874     if (sigaction(SIGBUS, &sa, NULL) < 0)
875         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGBUS)");
876 #endif
877 #ifdef SIGABORT
878     if (sigaction(SIGABORT, &sa, NULL) < 0)
879         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABORT)");
880 #endif
881 #ifdef SIGABRT
882     if (sigaction(SIGABRT, &sa, NULL) < 0)
883         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABRT)");
884 #endif
885 #ifdef SIGILL
886     if (sigaction(SIGILL, &sa, NULL) < 0)
887         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGILL)");
888 #endif
889 #ifdef SIGFPE
890     if (sigaction(SIGFPE, &sa, NULL) < 0)
891         ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGFPE)");
892 #endif
893
894 #else /* NO_USE_SIGACTION */
895
896     apr_signal(SIGSEGV, sig_coredump);
897 #ifdef SIGBUS
898     apr_signal(SIGBUS, sig_coredump);
899 #endif /* SIGBUS */
900 #ifdef SIGABORT
901     apr_signal(SIGABORT, sig_coredump);
902 #endif /* SIGABORT */
903 #ifdef SIGABRT
904     apr_signal(SIGABRT, sig_coredump);
905 #endif /* SIGABRT */
906 #ifdef SIGILL
907     apr_signal(SIGILL, sig_coredump);
908 #endif /* SIGILL */
909 #ifdef SIGFPE
910     apr_signal(SIGFPE, sig_coredump);
911 #endif /* SIGFPE */
912
913 #endif /* NO_USE_SIGACTION */
914
915     pconf = in_pconf;
916     parent_pid = my_pid = getpid();
917
918     return APR_SUCCESS;
919 }