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