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"
47 #include "util_mutex.h"
49 #ifdef AP_MPM_WANT_SET_SCOREBOARD
50 #include "scoreboard.h"
63 #if AP_ENABLE_EXCEPTION_HOOK
65 APR_HOOK_LINK(fatal_exception)
66 APR_HOOK_LINK(monitor)
68 AP_IMPLEMENT_HOOK_RUN_ALL(int, fatal_exception,
69 (ap_exception_info_t *ei), (ei), OK, DECLINED)
72 APR_HOOK_LINK(monitor)
75 AP_IMPLEMENT_HOOK_RUN_ALL(int, monitor,
76 (apr_pool_t *p), (p), OK, DECLINED)
79 #ifdef AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
81 typedef enum {DO_NOTHING, SEND_SIGTERM, SEND_SIGKILL, GIVEUP} action_t;
83 typedef struct extra_process_t {
84 struct extra_process_t *next;
88 static extra_process_t *extras;
90 void ap_register_extra_mpm_process(pid_t pid)
92 extra_process_t *p = (extra_process_t *)malloc(sizeof(extra_process_t));
99 int ap_unregister_extra_mpm_process(pid_t pid)
101 extra_process_t *cur = extras;
102 extra_process_t *prev = NULL;
104 while (cur && cur->pid != pid) {
111 prev->next = cur->next;
117 return 1; /* found */
120 /* we don't know about any such process */
125 static int reclaim_one_pid(pid_t pid, action_t action)
128 apr_status_t waitret;
131 waitret = apr_proc_wait(&proc, NULL, NULL, APR_NOWAIT);
132 if (waitret != APR_CHILD_NOTDONE) {
141 /* ok, now it's being annoying */
142 ap_log_error(APLOG_MARK, APLOG_WARNING,
144 "child process %" APR_PID_T_FMT
145 " still did not exit, "
152 ap_log_error(APLOG_MARK, APLOG_ERR,
154 "child process %" APR_PID_T_FMT
155 " still did not exit, "
161 /* sending a SIGKILL kills the entire team on BeOS, and as
162 * httpd thread is part of that team it removes any chance
163 * of ever doing a restart. To counter this I'm changing to
164 * use a kinder, gentler way of killing a specific thread
165 * that is just as effective.
172 /* gave it our best shot, but alas... If this really
173 * is a child we are trying to kill and it really hasn't
174 * exited, we will likely fail to bind to the port
177 ap_log_error(APLOG_MARK, APLOG_ERR,
179 "could not make child process %" APR_PID_T_FMT
181 "attempting to continue anyway",
189 void ap_reclaim_child_processes(int terminate)
191 apr_time_t waittime = 1024 * 16;
193 extra_process_t *cur_extra;
196 apr_time_t starttime = apr_time_now();
197 /* this table of actions and elapsed times tells what action is taken
198 * at which elapsed time from starting the reclaim
202 apr_time_t action_time;
204 {DO_NOTHING, 0}, /* dummy entry for iterations where we reap
205 * children but take no action against
208 {SEND_SIGTERM, apr_time_from_sec(3)},
209 {SEND_SIGTERM, apr_time_from_sec(5)},
210 {SEND_SIGTERM, apr_time_from_sec(7)},
211 {SEND_SIGKILL, apr_time_from_sec(9)},
212 {GIVEUP, apr_time_from_sec(10)}
214 int cur_action; /* index of action we decided to take this
217 int next_action = 1; /* index of first real action */
219 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
223 /* don't let waittime get longer than 1 second; otherwise, we don't
224 * react quickly to the last child exiting, and taking action can
227 waittime = waittime * 4;
228 if (waittime > apr_time_from_sec(1)) {
229 waittime = apr_time_from_sec(1);
232 /* see what action to take, if any */
233 if (action_table[next_action].action_time <= apr_time_now() - starttime) {
234 cur_action = next_action;
238 cur_action = 0; /* nothing to do */
241 /* now see who is done */
243 for (i = 0; i < max_daemons; ++i) {
244 pid_t pid = MPM_CHILD_PID(i);
247 continue; /* not every scoreboard entry is in use */
250 if (reclaim_one_pid(pid, action_table[cur_action].action)) {
251 MPM_NOTE_CHILD_KILLED(i);
260 extra_process_t *next = cur_extra->next;
262 if (reclaim_one_pid(cur_extra->pid, action_table[cur_action].action)) {
263 AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid));
270 #if APR_HAS_OTHER_CHILD
271 apr_proc_other_child_refresh_all(APR_OC_REASON_RESTART);
274 } while (not_dead_yet > 0 &&
275 action_table[cur_action].action != GIVEUP);
278 void ap_relieve_child_processes(void)
281 extra_process_t *cur_extra;
284 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
286 /* now see who is done */
287 for (i = 0; i < max_daemons; ++i) {
288 pid_t pid = MPM_CHILD_PID(i);
291 continue; /* not every scoreboard entry is in use */
294 if (reclaim_one_pid(pid, DO_NOTHING)) {
295 MPM_NOTE_CHILD_KILLED(i);
301 extra_process_t *next = cur_extra->next;
303 if (reclaim_one_pid(cur_extra->pid, DO_NOTHING)) {
304 AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid));
309 #endif /* AP_MPM_WANT_RECLAIM_CHILD_PROCESSES */
311 #ifdef AP_MPM_WANT_WAIT_OR_TIMEOUT
313 /* number of calls to wait_or_timeout between writable probes */
314 #ifndef INTERVAL_OF_WRITABLE_PROBES
315 #define INTERVAL_OF_WRITABLE_PROBES 10
317 static int wait_or_timeout_counter;
319 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
324 ++wait_or_timeout_counter;
325 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
326 wait_or_timeout_counter = 0;
330 rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
331 if (APR_STATUS_IS_EINTR(rv)) {
336 if (APR_STATUS_IS_CHILD_DONE(rv)) {
341 if ((ret = reap_children(exitcode, status)) > 0) {
346 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
350 #endif /* AP_MPM_WANT_WAIT_OR_TIMEOUT */
352 #ifdef AP_MPM_WANT_PROCESS_CHILD_STATUS
353 int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
356 const char *sigdesc = apr_signal_description_get(signum);
358 /* Child died... if it died due to a fatal error,
359 * we should simply bail out. The caller needs to
360 * check for bad rc from us and exit, running any
361 * appropriate cleanups.
363 * If the child died due to a resource shortage,
364 * the parent should limit the rate of forking
366 if (APR_PROC_CHECK_EXIT(why)) {
367 if (status == APEXIT_CHILDSICK) {
371 if (status == APEXIT_CHILDFATAL) {
372 ap_log_error(APLOG_MARK, APLOG_ALERT,
374 "Child %" APR_PID_T_FMT
375 " returned a Fatal error... Apache is exiting!",
377 return APEXIT_CHILDFATAL;
383 if (APR_PROC_CHECK_SIGNALED(why)) {
387 case AP_SIG_GRACEFUL:
392 if (APR_PROC_CHECK_CORE_DUMP(why)) {
393 ap_log_error(APLOG_MARK, APLOG_NOTICE,
395 "child pid %ld exit signal %s (%d), "
396 "possible coredump in %s",
397 (long)pid->pid, sigdesc, signum,
401 ap_log_error(APLOG_MARK, APLOG_NOTICE,
403 "child pid %ld exit signal %s (%d)",
404 (long)pid->pid, sigdesc, signum);
410 #endif /* AP_MPM_WANT_PROCESS_CHILD_STATUS */
412 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
413 void ap_sock_disable_nagle(apr_socket_t *s)
415 /* The Nagle algorithm says that we should delay sending partial
416 * packets in hopes of getting more data. We don't want to do
417 * this; we are not telnet. There are bad interactions between
418 * persistent connections and Nagle's algorithm that have very severe
419 * performance penalties. (Failing to disable Nagle is not much of a
420 * problem with simple HTTP.)
422 * In spite of these problems, failure here is not a shooting offense.
424 apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
426 if (status != APR_SUCCESS) {
427 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
428 "apr_socket_opt_set: (TCP_NODELAY)");
434 AP_DECLARE(uid_t) ap_uname2id(const char *name)
439 return (atoi(&name[1]));
441 if (!(ent = getpwnam(name))) {
442 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
443 "%s: bad user name %s", ap_server_argv0, name);
447 return (ent->pw_uid);
452 AP_DECLARE(gid_t) ap_gname2id(const char *name)
457 return (atoi(&name[1]));
459 if (!(ent = getgrnam(name))) {
460 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
461 "%s: bad group name %s", ap_server_argv0, name);
465 return (ent->gr_gid);
469 #ifndef HAVE_INITGROUPS
470 int initgroups(const char *name, gid_t basegid)
472 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32) || defined(NETWARE)
473 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
476 gid_t groups[NGROUPS_MAX];
482 groups[index++] = basegid;
484 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
485 if (g->gr_gid != basegid) {
488 for (names = g->gr_mem; *names != NULL; ++names) {
489 if (!strcmp(*names, name))
490 groups[index++] = g->gr_gid;
497 return setgroups(index, groups);
500 #endif /* def NEED_INITGROUPS */
502 #ifdef AP_MPM_USES_POD
504 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
508 *pod = apr_palloc(p, sizeof(**pod));
509 rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
510 if (rv != APR_SUCCESS) {
514 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
517 /* close these before exec. */
518 apr_file_inherit_unset((*pod)->pod_in);
519 apr_file_inherit_unset((*pod)->pod_out);
524 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
530 rv = apr_file_read(pod->pod_in, &c, &len);
532 if ((rv == APR_SUCCESS) && (len == 1)) {
536 if (rv != APR_SUCCESS) {
543 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
547 rv = apr_file_close(pod->pod_out);
548 if (rv != APR_SUCCESS) {
552 rv = apr_file_close(pod->pod_in);
553 if (rv != APR_SUCCESS) {
560 static apr_status_t pod_signal_internal(ap_pod_t *pod)
563 char char_of_death = '!';
566 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
567 if (rv != APR_SUCCESS) {
568 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
569 "write pipe_of_death");
575 /* This function connects to the server, then immediately closes the connection.
576 * This permits the MPM to skip the poll when there is only one listening
577 * socket, because it provides a alternate way to unblock an accept() when
580 static apr_status_t dummy_connection(ap_pod_t *pod)
588 /* create a temporary pool for the socket. pconf stays around too long */
589 rv = apr_pool_create(&p, pod->p);
590 if (rv != APR_SUCCESS) {
594 rv = apr_socket_create(&sock, ap_listeners->bind_addr->family,
596 if (rv != APR_SUCCESS) {
597 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
598 "get socket to connect to listener");
603 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
604 * queued connections before it starts blocking local connects...
605 * we need to keep from blocking too long and instead return an error,
606 * because the MPM won't want to hold up a graceful restart for a
609 rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
610 if (rv != APR_SUCCESS) {
611 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
612 "set timeout on socket to connect to listener");
613 apr_socket_close(sock);
618 rv = apr_socket_connect(sock, ap_listeners->bind_addr);
619 if (rv != APR_SUCCESS) {
620 int log_level = APLOG_WARNING;
622 if (APR_STATUS_IS_TIMEUP(rv)) {
623 /* probably some server processes bailed out already and there
624 * is nobody around to call accept and clear out the kernel
625 * connection queue; usually this is not worth logging
627 log_level = APLOG_DEBUG;
630 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
631 "connect to listener on %pI", ap_listeners->bind_addr);
634 /* Create the request string. We include a User-Agent so that
635 * adminstrators can track down the cause of the odd-looking
636 * requests in their logs.
638 srequest = apr_pstrcat(p, "GET / HTTP/1.0\r\nUser-Agent: ",
639 ap_get_server_banner(),
640 " (internal dummy connection)\r\n\r\n", NULL);
642 /* Since some operating systems support buffering of data or entire
643 * requests in the kernel, we send a simple request, to make sure
644 * the server pops out of a blocking accept().
646 /* XXX: This is HTTP specific. We should look at the Protocol for each
647 * listener, and send the correct type of request to trigger any Accept
650 len = strlen(srequest);
651 apr_socket_send(sock, srequest, &len);
652 apr_socket_close(sock);
658 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
662 rv = pod_signal_internal(pod);
663 if (rv != APR_SUCCESS) {
667 return dummy_connection(pod);
670 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
673 apr_status_t rv = APR_SUCCESS;
675 /* we don't write anything to the pod here... we assume
676 * that the would-be reader of the pod has another way to
677 * see that it is time to die once we wake it up
679 * writing lots of things to the pod at once is very
680 * problematic... we can fill the kernel pipe buffer and
681 * be blocked until somebody consumes some bytes or
682 * we hit a timeout... if we hit a timeout we can't just
683 * keep trying because maybe we'll never successfully
684 * write again... but then maybe we'll leave would-be
685 * readers stranded (a number of them could be tied up for
686 * a while serving time-consuming requests)
688 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
689 rv = dummy_connection(pod);
692 #endif /* #ifdef AP_MPM_USES_POD */
694 /* standard mpm configuration handling */
695 #ifdef AP_MPM_WANT_SET_PIDFILE
696 const char *ap_pid_fname = NULL;
698 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
701 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
706 if (cmd->server->is_virtual) {
707 return "PidFile directive not allowed in <VirtualHost>";
715 #ifdef AP_MPM_WANT_SET_SCOREBOARD
716 const char * ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
719 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
724 ap_scoreboard_fname = arg;
729 #ifdef AP_MPM_WANT_SET_LOCKFILE
730 const char *ap_lock_fname = NULL;
732 const char *ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
735 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
745 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
746 int ap_max_requests_per_child = 0;
748 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
751 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
756 ap_max_requests_per_child = atoi(arg);
762 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
763 char ap_coredump_dir[MAX_STRING_LEN];
764 int ap_coredumpdir_configured;
766 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
772 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
777 fname = ap_server_root_relative(cmd->pool, arg);
779 return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
782 if ((rv = apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) {
783 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
784 " does not exist", NULL);
786 if (finfo.filetype != APR_DIR) {
787 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
788 " is not a directory", NULL);
790 apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
791 ap_coredumpdir_configured = 1;
796 #ifdef AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
797 int ap_graceful_shutdown_timeout = 0;
799 const char * ap_mpm_set_graceful_shutdown(cmd_parms *cmd, void *dummy,
802 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
806 ap_graceful_shutdown_timeout = atoi(arg);
811 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
812 apr_lockmech_e ap_accept_lock_mech = APR_LOCK_DEFAULT;
814 AP_DECLARE(const char *) ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
819 const char *lockfile;
820 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
825 rv = ap_parse_mutex(arg, cmd->server->process->pool,
826 &ap_accept_lock_mech, &lockfile);
828 if ((rv == APR_ENOTIMPL) || (rv == APR_ENOLOCK)) {
829 return apr_pstrcat(cmd->pool, "Invalid AcceptMutex argument ", arg,
830 " (", ap_available_mutexes_string, ")", NULL);
831 } else if (rv == APR_BADARG) {
832 return apr_pstrcat(cmd->pool, "Invalid AcceptMutex filepath ",
836 /* perchild can't use SysV sems because the permissions on the accept
837 * mutex can't be set to allow all processes to use the mutex and
838 * at the same time keep all users from being able to dink with the
841 #if defined(PERCHILD_MPM)
842 if (ap_accept_lock_mech == APR_LOCK_SYSVSEM) {
843 return apr_pstrcat(cmd->pool, "Invalid AcceptMutex argument ", arg,
844 " (", ap_available_mutexes_string, ")", NULL);
847 if (lockfile && !ap_lock_fname)
848 ap_lock_fname = lockfile;
854 #ifdef AP_MPM_WANT_SIGNAL_SERVER
856 static const char *dash_k_arg;
858 static int send_signal(pid_t pid, int sig)
860 if (kill(pid, sig) < 0) {
861 ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL,
862 "sending signal to server");
868 int ap_signal_server(int *exit_status, apr_pool_t *pconf)
873 int have_pid_file = 0;
878 rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
879 if (rv != APR_SUCCESS) {
880 if (rv != APR_ENOENT) {
881 ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL,
882 "Error retrieving pid file %s", ap_pid_fname);
883 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
884 "Remove it before continuing if it is corrupted.");
888 status = "httpd (no pid file) not running";
892 if (kill(otherpid, 0) == 0) {
894 status = apr_psprintf(pconf,
895 "httpd (pid %" APR_PID_T_FMT ") already "
896 "running", otherpid);
899 status = apr_psprintf(pconf,
900 "httpd (pid %" APR_PID_T_FMT "?) not running",
905 if (!strcmp(dash_k_arg, "start")) {
907 printf("%s\n", status);
912 if (!strcmp(dash_k_arg, "stop")) {
914 printf("%s\n", status);
917 send_signal(otherpid, SIGTERM);
922 if (!strcmp(dash_k_arg, "restart")) {
924 printf("httpd not running, trying to start\n");
927 *exit_status = send_signal(otherpid, SIGHUP);
932 if (!strcmp(dash_k_arg, "graceful")) {
934 printf("httpd not running, trying to start\n");
937 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL);
942 if (!strcmp(dash_k_arg, "graceful-stop")) {
943 #ifdef AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
945 printf("%s\n", status);
948 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL_STOP);
951 printf("httpd MPM \"" MPM_NAME "\" does not support graceful-stop\n");
959 void ap_mpm_rewrite_args(process_rec *process)
961 apr_array_header_t *mpm_new_argv;
968 mpm_new_argv = apr_array_make(process->pool, process->argc,
969 sizeof(const char **));
970 *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
971 fixed_args = mpm_new_argv->nelts;
972 apr_getopt_init(&opt, process->pool, process->argc, process->argv);
975 /* option char returned by apr_getopt() will be stored in optbuf[1] */
977 while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
978 optbuf + 1, &optarg)) == APR_SUCCESS) {
982 if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
983 !strcmp(optarg, "restart") || !strcmp(optarg, "graceful") ||
984 !strcmp(optarg, "graceful-stop")) {
990 *(const char **)apr_array_push(mpm_new_argv) =
991 apr_pstrdup(process->pool, optbuf);
993 *(const char **)apr_array_push(mpm_new_argv) = optarg;
998 /* back up to capture the bad argument */
999 if (rv == APR_BADCH || rv == APR_BADARG) {
1003 while (opt->ind < opt->argc) {
1004 *(const char **)apr_array_push(mpm_new_argv) =
1005 apr_pstrdup(process->pool, opt->argv[opt->ind++]);
1008 process->argc = mpm_new_argv->nelts;
1009 process->argv = (const char * const *)mpm_new_argv->elts;
1012 APR_REGISTER_OPTIONAL_FN(ap_signal_server);
1016 #endif /* AP_MPM_WANT_SIGNAL_SERVER */
1018 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
1019 apr_uint32_t ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
1021 const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
1025 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1030 value = strtol(arg, NULL, 0);
1031 if (value < 0 || errno == ERANGE)
1032 return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
1035 ap_max_mem_free = (apr_uint32_t)value * 1024;
1040 #endif /* AP_MPM_WANT_SET_MAX_MEM_FREE */
1042 #ifdef AP_MPM_WANT_SET_STACKSIZE
1043 apr_size_t ap_thread_stacksize = 0; /* use system default */
1045 const char *ap_mpm_set_thread_stacksize(cmd_parms *cmd, void *dummy,
1049 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1054 value = strtol(arg, NULL, 0);
1055 if (value < 0 || errno == ERANGE)
1056 return apr_pstrcat(cmd->pool, "Invalid ThreadStackSize value: ",
1059 ap_thread_stacksize = (apr_size_t)value;
1064 #endif /* AP_MPM_WANT_SET_STACKSIZE */
1066 #ifdef AP_MPM_WANT_FATAL_SIGNAL_HANDLER
1068 static pid_t parent_pid, my_pid;
1071 #if AP_ENABLE_EXCEPTION_HOOK
1073 static int exception_hook_enabled;
1075 const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
1078 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1083 if (cmd->server->is_virtual) {
1084 return "EnableExceptionHook directive not allowed in <VirtualHost>";
1087 if (strcasecmp(arg, "on") == 0) {
1088 exception_hook_enabled = 1;
1090 else if (strcasecmp(arg, "off") == 0) {
1091 exception_hook_enabled = 0;
1094 return "parameter must be 'on' or 'off'";
1100 static void run_fatal_exception_hook(int sig)
1102 ap_exception_info_t ei = {0};
1104 if (exception_hook_enabled &&
1106 my_pid != parent_pid) {
1109 ap_run_fatal_exception(&ei);
1112 #endif /* AP_ENABLE_EXCEPTION_HOOK */
1114 /* handle all varieties of core dumping signals */
1115 static void sig_coredump(int sig)
1117 apr_filepath_set(ap_coredump_dir, pconf);
1118 apr_signal(sig, SIG_DFL);
1119 #if AP_ENABLE_EXCEPTION_HOOK
1120 run_fatal_exception_hook(sig);
1122 /* linuxthreads issue calling getpid() here:
1123 * This comparison won't match if the crashing thread is
1124 * some module's thread that runs in the parent process.
1125 * The fallout, which is limited to linuxthreads:
1126 * The special log message won't be written when such a
1127 * thread in the parent causes the parent to crash.
1129 if (getpid() == parent_pid) {
1130 ap_log_error(APLOG_MARK, APLOG_NOTICE,
1132 "seg fault or similar nasty error detected "
1133 "in the parent process");
1134 /* XXX we can probably add some rudimentary cleanup code here,
1135 * like getting rid of the pid file. If any additional bad stuff
1136 * happens, we are protected from recursive errors taking down the
1137 * system since this function is no longer the signal handler GLA
1140 kill(getpid(), sig);
1141 /* At this point we've got sig blocked, because we're still inside
1142 * the signal handler. When we leave the signal handler it will
1143 * be unblocked, and we'll take the signal... and coredump or whatever
1144 * is appropriate for this particular Unix. In addition the parent
1145 * will see the real signal we received -- whereas if we called
1146 * abort() here, the parent would only see SIGABRT.
1150 apr_status_t ap_fatal_signal_child_setup(server_rec *s)
1156 apr_status_t ap_fatal_signal_setup(server_rec *s, apr_pool_t *in_pconf)
1158 #ifndef NO_USE_SIGACTION
1159 struct sigaction sa;
1161 sigemptyset(&sa.sa_mask);
1163 #if defined(SA_ONESHOT)
1164 sa.sa_flags = SA_ONESHOT;
1165 #elif defined(SA_RESETHAND)
1166 sa.sa_flags = SA_RESETHAND;
1171 sa.sa_handler = sig_coredump;
1172 if (sigaction(SIGSEGV, &sa, NULL) < 0)
1173 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGSEGV)");
1175 if (sigaction(SIGBUS, &sa, NULL) < 0)
1176 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGBUS)");
1179 if (sigaction(SIGABORT, &sa, NULL) < 0)
1180 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABORT)");
1183 if (sigaction(SIGABRT, &sa, NULL) < 0)
1184 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABRT)");
1187 if (sigaction(SIGILL, &sa, NULL) < 0)
1188 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGILL)");
1191 #else /* NO_USE_SIGACTION */
1193 apr_signal(SIGSEGV, sig_coredump);
1195 apr_signal(SIGBUS, sig_coredump);
1198 apr_signal(SIGABORT, sig_coredump);
1199 #endif /* SIGABORT */
1201 apr_signal(SIGABRT, sig_coredump);
1202 #endif /* SIGABRT */
1204 apr_signal(SIGILL, sig_coredump);
1207 #endif /* NO_USE_SIGACTION */
1210 parent_pid = my_pid = getpid();
1215 #endif /* AP_MPM_WANT_FATAL_SIGNAL_HANDLER */