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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
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
29 #include "apr_thread_proc.h"
30 #include "apr_signal.h"
31 #include "apr_strings.h"
32 #define APR_WANT_STRFUNC
34 #include "apr_getopt.h"
35 #include "apr_optional.h"
36 #include "apr_allocator.h"
39 #include "http_config.h"
41 #include "http_main.h"
43 #include "mpm_common.h"
45 #include "ap_listen.h"
46 #include "mpm_default.h"
48 #ifdef AP_MPM_WANT_SET_SCOREBOARD
49 #include "scoreboard.h"
62 #ifdef AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
64 typedef enum {DO_NOTHING, SEND_SIGTERM, SEND_SIGKILL, GIVEUP} action_t;
66 typedef struct extra_process_t {
67 struct extra_process_t *next;
71 static extra_process_t *extras;
73 void ap_register_extra_mpm_process(pid_t pid)
75 extra_process_t *p = (extra_process_t *)malloc(sizeof(extra_process_t));
82 int ap_unregister_extra_mpm_process(pid_t pid)
84 extra_process_t *cur = extras;
85 extra_process_t *prev = NULL;
87 while (cur && cur->pid != pid) {
94 prev->next = cur->next;
100 return 1; /* found */
103 /* we don't know about any such process */
108 static int reclaim_one_pid(pid_t pid, action_t action)
111 apr_status_t waitret;
113 /* Ensure pid sanity. */
119 waitret = apr_proc_wait(&proc, NULL, NULL, APR_NOWAIT);
120 if (waitret != APR_CHILD_NOTDONE) {
129 /* ok, now it's being annoying */
130 ap_log_error(APLOG_MARK, APLOG_WARNING,
132 "child process %" APR_PID_T_FMT
133 " still did not exit, "
140 ap_log_error(APLOG_MARK, APLOG_ERR,
142 "child process %" APR_PID_T_FMT
143 " still did not exit, "
149 /* sending a SIGKILL kills the entire team on BeOS, and as
150 * httpd thread is part of that team it removes any chance
151 * of ever doing a restart. To counter this I'm changing to
152 * use a kinder, gentler way of killing a specific thread
153 * that is just as effective.
160 /* gave it our best shot, but alas... If this really
161 * is a child we are trying to kill and it really hasn't
162 * exited, we will likely fail to bind to the port
165 ap_log_error(APLOG_MARK, APLOG_ERR,
167 "could not make child process %" APR_PID_T_FMT
169 "attempting to continue anyway",
177 void ap_reclaim_child_processes(int terminate)
179 apr_time_t waittime = 1024 * 16;
181 extra_process_t *cur_extra;
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
190 apr_time_t action_time;
192 {DO_NOTHING, 0}, /* dummy entry for iterations where we reap
193 * children but take no action against
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)}
202 int cur_action; /* index of action we decided to take this
205 int next_action = 1; /* index of first real action */
207 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
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
215 waittime = waittime * 4;
216 if (waittime > apr_time_from_sec(1)) {
217 waittime = apr_time_from_sec(1);
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;
226 cur_action = 0; /* nothing to do */
229 /* now see who is done */
231 for (i = 0; i < max_daemons; ++i) {
232 pid_t pid = MPM_CHILD_PID(i);
235 continue; /* not every scoreboard entry is in use */
238 if (reclaim_one_pid(pid, action_table[cur_action].action)) {
239 MPM_NOTE_CHILD_KILLED(i);
248 extra_process_t *next = cur_extra->next;
250 if (reclaim_one_pid(cur_extra->pid, action_table[cur_action].action)) {
251 AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid));
259 #if APR_HAS_OTHER_CHILD
260 apr_proc_other_child_check();
263 } while (not_dead_yet > 0 &&
264 action_table[cur_action].action != GIVEUP);
266 #endif /* AP_MPM_WANT_RECLAIM_CHILD_PROCESSES */
268 #ifdef AP_MPM_WANT_WAIT_OR_TIMEOUT
270 /* number of calls to wait_or_timeout between writable probes */
271 #ifndef INTERVAL_OF_WRITABLE_PROBES
272 #define INTERVAL_OF_WRITABLE_PROBES 10
274 static int wait_or_timeout_counter;
276 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
281 ++wait_or_timeout_counter;
282 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
283 wait_or_timeout_counter = 0;
286 rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
287 if (APR_STATUS_IS_EINTR(rv)) {
292 if (APR_STATUS_IS_CHILD_DONE(rv)) {
297 if ((ret = reap_children(exitcode, status)) > 0) {
302 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
306 #endif /* AP_MPM_WANT_WAIT_OR_TIMEOUT */
308 #ifdef AP_MPM_WANT_PROCESS_CHILD_STATUS
309 int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
312 const char *sigdesc = apr_signal_description_get(signum);
314 /* Child died... if it died due to a fatal error,
315 * we should simply bail out. The caller needs to
316 * check for bad rc from us and exit, running any
317 * appropriate cleanups.
319 * If the child died due to a resource shortage,
320 * the parent should limit the rate of forking
322 if (APR_PROC_CHECK_EXIT(why)) {
323 if (status == APEXIT_CHILDSICK) {
327 if (status == APEXIT_CHILDFATAL) {
328 ap_log_error(APLOG_MARK, APLOG_ALERT,
330 "Child %" APR_PID_T_FMT
331 " returned a Fatal error... Apache is exiting!",
333 return APEXIT_CHILDFATAL;
339 if (APR_PROC_CHECK_SIGNALED(why)) {
343 case AP_SIG_GRACEFUL:
348 if (APR_PROC_CHECK_CORE_DUMP(why)) {
349 ap_log_error(APLOG_MARK, APLOG_NOTICE,
351 "child pid %ld exit signal %s (%d), "
352 "possible coredump in %s",
353 (long)pid->pid, sigdesc, signum,
357 ap_log_error(APLOG_MARK, APLOG_NOTICE,
359 "child pid %ld exit signal %s (%d)",
360 (long)pid->pid, sigdesc, signum);
366 #endif /* AP_MPM_WANT_PROCESS_CHILD_STATUS */
368 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
369 void ap_sock_disable_nagle(apr_socket_t *s)
371 /* The Nagle algorithm says that we should delay sending partial
372 * packets in hopes of getting more data. We don't want to do
373 * this; we are not telnet. There are bad interactions between
374 * persistent connections and Nagle's algorithm that have very severe
375 * performance penalties. (Failing to disable Nagle is not much of a
376 * problem with simple HTTP.)
378 * In spite of these problems, failure here is not a shooting offense.
380 apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
382 if (status != APR_SUCCESS) {
383 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
384 "apr_socket_opt_set: (TCP_NODELAY)");
390 AP_DECLARE(uid_t) ap_uname2id(const char *name)
395 return (atoi(&name[1]));
397 if (!(ent = getpwnam(name))) {
398 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
399 "%s: bad user name %s", ap_server_argv0, name);
403 return (ent->pw_uid);
408 AP_DECLARE(gid_t) ap_gname2id(const char *name)
413 return (atoi(&name[1]));
415 if (!(ent = getgrnam(name))) {
416 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
417 "%s: bad group name %s", ap_server_argv0, name);
421 return (ent->gr_gid);
425 #ifndef HAVE_INITGROUPS
426 int initgroups(const char *name, gid_t basegid)
428 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32) || defined(NETWARE)
429 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
432 gid_t groups[NGROUPS_MAX];
438 groups[index++] = basegid;
440 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
441 if (g->gr_gid != basegid) {
444 for (names = g->gr_mem; *names != NULL; ++names) {
445 if (!strcmp(*names, name))
446 groups[index++] = g->gr_gid;
453 return setgroups(index, groups);
456 #endif /* def NEED_INITGROUPS */
458 #ifdef AP_MPM_USES_POD
460 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
464 *pod = apr_palloc(p, sizeof(**pod));
465 rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
466 if (rv != APR_SUCCESS) {
470 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
473 /* close these before exec. */
474 apr_file_unset_inherit((*pod)->pod_in);
475 apr_file_unset_inherit((*pod)->pod_out);
480 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
486 rv = apr_file_read(pod->pod_in, &c, &len);
488 if ((rv == APR_SUCCESS) && (len == 1)) {
492 if (rv != APR_SUCCESS) {
499 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
503 rv = apr_file_close(pod->pod_out);
504 if (rv != APR_SUCCESS) {
508 rv = apr_file_close(pod->pod_in);
509 if (rv != APR_SUCCESS) {
516 static apr_status_t pod_signal_internal(ap_pod_t *pod)
519 char char_of_death = '!';
522 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
523 if (rv != APR_SUCCESS) {
524 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
525 "write pipe_of_death");
531 /* This function connects to the server, then immediately closes the connection.
532 * This permits the MPM to skip the poll when there is only one listening
533 * socket, because it provides a alternate way to unblock an accept() when
536 static apr_status_t dummy_connection(ap_pod_t *pod)
542 /* create a temporary pool for the socket. pconf stays around too long */
543 rv = apr_pool_create(&p, pod->p);
544 if (rv != APR_SUCCESS) {
548 rv = apr_socket_create(&sock, ap_listeners->bind_addr->family, SOCK_STREAM, p);
549 if (rv != APR_SUCCESS) {
550 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
551 "get socket to connect to listener");
555 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
556 * queued connections before it starts blocking local connects...
557 * we need to keep from blocking too long and instead return an error,
558 * because the MPM won't want to hold up a graceful restart for a
561 rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
562 if (rv != APR_SUCCESS) {
563 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
564 "set timeout on socket to connect to listener");
565 apr_socket_close(sock);
569 rv = apr_connect(sock, ap_listeners->bind_addr);
570 if (rv != APR_SUCCESS) {
571 int log_level = APLOG_WARNING;
573 if (APR_STATUS_IS_TIMEUP(rv)) {
574 /* probably some server processes bailed out already and there
575 * is nobody around to call accept and clear out the kernel
576 * connection queue; usually this is not worth logging
578 log_level = APLOG_DEBUG;
581 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
582 "connect to listener on %pI", ap_listeners->bind_addr);
585 apr_socket_close(sock);
591 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
595 rv = pod_signal_internal(pod);
596 if (rv != APR_SUCCESS) {
600 return dummy_connection(pod);
603 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
606 apr_status_t rv = APR_SUCCESS;
608 /* we don't write anything to the pod here... we assume
609 * that the would-be reader of the pod has another way to
610 * see that it is time to die once we wake it up
612 * writing lots of things to the pod at once is very
613 * problematic... we can fill the kernel pipe buffer and
614 * be blocked until somebody consumes some bytes or
615 * we hit a timeout... if we hit a timeout we can't just
616 * keep trying because maybe we'll never successfully
617 * write again... but then maybe we'll leave would-be
618 * readers stranded (a number of them could be tied up for
619 * a while serving time-consuming requests)
621 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
622 rv = dummy_connection(pod);
625 #endif /* #ifdef AP_MPM_USES_POD */
627 /* standard mpm configuration handling */
628 #ifdef AP_MPM_WANT_SET_PIDFILE
629 const char *ap_pid_fname = NULL;
631 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
634 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
639 if (cmd->server->is_virtual) {
640 return "PidFile directive not allowed in <VirtualHost>";
648 #ifdef AP_MPM_WANT_SET_SCOREBOARD
649 const char * ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
652 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
657 ap_scoreboard_fname = arg;
662 #ifdef AP_MPM_WANT_SET_LOCKFILE
663 const char *ap_lock_fname = NULL;
665 const char *ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
668 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
678 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
679 int ap_max_requests_per_child = 0;
681 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
684 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
689 ap_max_requests_per_child = atoi(arg);
695 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
696 char ap_coredump_dir[MAX_STRING_LEN];
697 int ap_coredumpdir_configured;
699 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
705 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
710 fname = ap_server_root_relative(cmd->pool, arg);
712 return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
715 if ((rv = apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) {
716 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
717 " does not exist", NULL);
719 if (finfo.filetype != APR_DIR) {
720 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
721 " is not a directory", NULL);
723 apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
724 ap_coredumpdir_configured = 1;
729 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
730 apr_lockmech_e ap_accept_lock_mech = APR_LOCK_DEFAULT;
732 const char ap_valid_accept_mutex_string[] =
733 "Valid accept mutexes for this platform and MPM are: default"
734 #if APR_HAS_FLOCK_SERIALIZE
737 #if APR_HAS_FCNTL_SERIALIZE
740 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
743 #if APR_HAS_POSIXSEM_SERIALIZE
746 #if APR_HAS_PROC_PTHREAD_SERIALIZE
751 AP_DECLARE(const char *) ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
755 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
760 if (!strcasecmp(arg, "default")) {
761 ap_accept_lock_mech = APR_LOCK_DEFAULT;
763 #if APR_HAS_FLOCK_SERIALIZE
764 else if (!strcasecmp(arg, "flock")) {
765 ap_accept_lock_mech = APR_LOCK_FLOCK;
768 #if APR_HAS_FCNTL_SERIALIZE
769 else if (!strcasecmp(arg, "fcntl")) {
770 ap_accept_lock_mech = APR_LOCK_FCNTL;
774 /* perchild can't use SysV sems because the permissions on the accept
775 * mutex can't be set to allow all processes to use the mutex and
776 * at the same time keep all users from being able to dink with the
779 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
780 else if (!strcasecmp(arg, "sysvsem")) {
781 ap_accept_lock_mech = APR_LOCK_SYSVSEM;
784 #if APR_HAS_POSIXSEM_SERIALIZE
785 else if (!strcasecmp(arg, "posixsem")) {
786 ap_accept_lock_mech = APR_LOCK_POSIXSEM;
789 #if APR_HAS_PROC_PTHREAD_SERIALIZE
790 else if (!strcasecmp(arg, "pthread")) {
791 ap_accept_lock_mech = APR_LOCK_PROC_PTHREAD;
795 return apr_pstrcat(cmd->pool, arg, " is an invalid mutex mechanism; ",
796 ap_valid_accept_mutex_string, NULL);
803 #ifdef AP_MPM_WANT_SIGNAL_SERVER
805 static const char *dash_k_arg;
807 static int send_signal(pid_t pid, int sig)
809 if (kill(pid, sig) < 0) {
810 ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL,
811 "sending signal to server");
817 int ap_signal_server(int *exit_status, apr_pool_t *pconf)
822 int have_pid_file = 0;
827 rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
828 if (rv != APR_SUCCESS) {
829 if (rv != APR_ENOENT) {
830 ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL,
831 "Error retrieving pid file %s", ap_pid_fname);
835 status = "httpd (no pid file) not running";
839 if (kill(otherpid, 0) == 0) {
841 status = apr_psprintf(pconf,
842 "httpd (pid %" APR_PID_T_FMT ") already "
843 "running", otherpid);
846 status = apr_psprintf(pconf,
847 "httpd (pid %" APR_PID_T_FMT "?) not running",
852 if (!strcmp(dash_k_arg, "start")) {
854 printf("%s\n", status);
859 if (!strcmp(dash_k_arg, "stop")) {
861 printf("%s\n", status);
864 send_signal(otherpid, SIGTERM);
869 if (!strcmp(dash_k_arg, "restart")) {
871 printf("httpd not running, trying to start\n");
874 *exit_status = send_signal(otherpid, SIGHUP);
879 if (!strcmp(dash_k_arg, "graceful")) {
881 printf("httpd not running, trying to start\n");
884 *exit_status = send_signal(otherpid, SIGUSR1);
892 void ap_mpm_rewrite_args(process_rec *process)
894 apr_array_header_t *mpm_new_argv;
901 mpm_new_argv = apr_array_make(process->pool, process->argc,
902 sizeof(const char **));
903 *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
904 fixed_args = mpm_new_argv->nelts;
905 apr_getopt_init(&opt, process->pool, process->argc, process->argv);
908 /* option char returned by apr_getopt() will be stored in optbuf[1] */
910 while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
911 optbuf + 1, &optarg)) == APR_SUCCESS) {
915 if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
916 !strcmp(optarg, "restart") || !strcmp(optarg, "graceful")) {
922 *(const char **)apr_array_push(mpm_new_argv) =
923 apr_pstrdup(process->pool, optbuf);
925 *(const char **)apr_array_push(mpm_new_argv) = optarg;
930 /* back up to capture the bad argument */
931 if (rv == APR_BADCH || rv == APR_BADARG) {
935 while (opt->ind < opt->argc) {
936 *(const char **)apr_array_push(mpm_new_argv) =
937 apr_pstrdup(process->pool, opt->argv[opt->ind++]);
940 process->argc = mpm_new_argv->nelts;
941 process->argv = (const char * const *)mpm_new_argv->elts;
944 APR_REGISTER_OPTIONAL_FN(ap_signal_server);
948 #endif /* AP_MPM_WANT_SIGNAL_SERVER */
950 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
951 apr_uint32_t ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
953 const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
957 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
962 value = strtol(arg, NULL, 0);
963 if (value < 0 || errno == ERANGE)
964 return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
967 ap_max_mem_free = (apr_uint32_t)value * 1024;
972 #endif /* AP_MPM_WANT_SET_MAX_MEM_FREE */
974 #ifdef AP_MPM_WANT_FATAL_SIGNAL_HANDLER
976 static pid_t parent_pid, my_pid;
979 #if AP_ENABLE_EXCEPTION_HOOK
981 static int exception_hook_enabled;
983 const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
986 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
991 if (cmd->server->is_virtual) {
992 return "EnableExceptionHook directive not allowed in <VirtualHost>";
995 if (strcasecmp(arg, "on") == 0) {
996 exception_hook_enabled = 1;
998 else if (strcasecmp(arg, "off") == 0) {
999 exception_hook_enabled = 0;
1002 return "parameter must be 'on' or 'off'";
1009 APR_HOOK_LINK(fatal_exception)
1012 AP_IMPLEMENT_HOOK_RUN_ALL(int, fatal_exception,
1013 (ap_exception_info_t *ei), (ei), OK, DECLINED)
1015 static void run_fatal_exception_hook(int sig)
1017 ap_exception_info_t ei = {0};
1019 if (exception_hook_enabled &&
1021 my_pid != parent_pid) {
1024 ap_run_fatal_exception(&ei);
1027 #endif /* AP_ENABLE_EXCEPTION_HOOK */
1029 /* handle all varieties of core dumping signals */
1030 static void sig_coredump(int sig)
1032 apr_filepath_set(ap_coredump_dir, pconf);
1033 apr_signal(sig, SIG_DFL);
1034 #if AP_ENABLE_EXCEPTION_HOOK
1035 run_fatal_exception_hook(sig);
1037 /* linuxthreads issue calling getpid() here:
1038 * This comparison won't match if the crashing thread is
1039 * some module's thread that runs in the parent process.
1040 * The fallout, which is limited to linuxthreads:
1041 * The special log message won't be written when such a
1042 * thread in the parent causes the parent to crash.
1044 if (getpid() == parent_pid) {
1045 ap_log_error(APLOG_MARK, APLOG_NOTICE,
1047 "seg fault or similar nasty error detected "
1048 "in the parent process");
1049 /* XXX we can probably add some rudimentary cleanup code here,
1050 * like getting rid of the pid file. If any additional bad stuff
1051 * happens, we are protected from recursive errors taking down the
1052 * system since this function is no longer the signal handler GLA
1055 kill(getpid(), sig);
1056 /* At this point we've got sig blocked, because we're still inside
1057 * the signal handler. When we leave the signal handler it will
1058 * be unblocked, and we'll take the signal... and coredump or whatever
1059 * is appropriate for this particular Unix. In addition the parent
1060 * will see the real signal we received -- whereas if we called
1061 * abort() here, the parent would only see SIGABRT.
1065 apr_status_t ap_fatal_signal_child_setup(server_rec *s)
1071 apr_status_t ap_fatal_signal_setup(server_rec *s, apr_pool_t *in_pconf)
1073 #ifndef NO_USE_SIGACTION
1074 struct sigaction sa;
1076 sigemptyset(&sa.sa_mask);
1078 #if defined(SA_ONESHOT)
1079 sa.sa_flags = SA_ONESHOT;
1080 #elif defined(SA_RESETHAND)
1081 sa.sa_flags = SA_RESETHAND;
1086 sa.sa_handler = sig_coredump;
1087 if (sigaction(SIGSEGV, &sa, NULL) < 0)
1088 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGSEGV)");
1090 if (sigaction(SIGBUS, &sa, NULL) < 0)
1091 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGBUS)");
1094 if (sigaction(SIGABORT, &sa, NULL) < 0)
1095 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABORT)");
1098 if (sigaction(SIGABRT, &sa, NULL) < 0)
1099 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABRT)");
1102 if (sigaction(SIGILL, &sa, NULL) < 0)
1103 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGILL)");
1106 #else /* NO_USE_SIGACTION */
1108 apr_signal(SIGSEGV, sig_coredump);
1110 apr_signal(SIGBUS, sig_coredump);
1113 apr_signal(SIGABORT, sig_coredump);
1114 #endif /* SIGABORT */
1116 apr_signal(SIGABRT, sig_coredump);
1117 #endif /* SIGABRT */
1119 apr_signal(SIGILL, sig_coredump);
1122 #endif /* NO_USE_SIGACTION */
1125 parent_pid = my_pid = getpid();
1130 #endif /* AP_MPM_WANT_FATAL_SIGNAL_HANDLER */