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