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