1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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 #if AP_ENABLE_EXCEPTION_HOOK
64 APR_HOOK_LINK(fatal_exception)
65 APR_HOOK_LINK(monitor)
67 AP_IMPLEMENT_HOOK_RUN_ALL(int, fatal_exception,
68 (ap_exception_info_t *ei), (ei), OK, DECLINED)
71 APR_HOOK_LINK(monitor)
74 AP_IMPLEMENT_HOOK_RUN_ALL(int, monitor,
75 (apr_pool_t *p), (p), OK, DECLINED)
78 #ifdef AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
80 typedef enum {DO_NOTHING, SEND_SIGTERM, SEND_SIGKILL, GIVEUP} action_t;
82 typedef struct extra_process_t {
83 struct extra_process_t *next;
87 static extra_process_t *extras;
89 void ap_register_extra_mpm_process(pid_t pid)
91 extra_process_t *p = (extra_process_t *)malloc(sizeof(extra_process_t));
98 int ap_unregister_extra_mpm_process(pid_t pid)
100 extra_process_t *cur = extras;
101 extra_process_t *prev = NULL;
103 while (cur && cur->pid != pid) {
110 prev->next = cur->next;
116 return 1; /* found */
119 /* we don't know about any such process */
124 static int reclaim_one_pid(pid_t pid, action_t action)
127 apr_status_t waitret;
130 waitret = apr_proc_wait(&proc, NULL, NULL, APR_NOWAIT);
131 if (waitret != APR_CHILD_NOTDONE) {
140 /* ok, now it's being annoying */
141 ap_log_error(APLOG_MARK, APLOG_WARNING,
143 "child process %" APR_PID_T_FMT
144 " still did not exit, "
151 ap_log_error(APLOG_MARK, APLOG_ERR,
153 "child process %" APR_PID_T_FMT
154 " still did not exit, "
160 /* sending a SIGKILL kills the entire team on BeOS, and as
161 * httpd thread is part of that team it removes any chance
162 * of ever doing a restart. To counter this I'm changing to
163 * use a kinder, gentler way of killing a specific thread
164 * that is just as effective.
171 /* gave it our best shot, but alas... If this really
172 * is a child we are trying to kill and it really hasn't
173 * exited, we will likely fail to bind to the port
176 ap_log_error(APLOG_MARK, APLOG_ERR,
178 "could not make child process %" APR_PID_T_FMT
180 "attempting to continue anyway",
188 void ap_reclaim_child_processes(int terminate)
190 apr_time_t waittime = 1024 * 16;
192 extra_process_t *cur_extra;
195 apr_time_t starttime = apr_time_now();
196 /* this table of actions and elapsed times tells what action is taken
197 * at which elapsed time from starting the reclaim
201 apr_time_t action_time;
203 {DO_NOTHING, 0}, /* dummy entry for iterations where we reap
204 * children but take no action against
207 {SEND_SIGTERM, apr_time_from_sec(3)},
208 {SEND_SIGTERM, apr_time_from_sec(5)},
209 {SEND_SIGTERM, apr_time_from_sec(7)},
210 {SEND_SIGKILL, apr_time_from_sec(9)},
211 {GIVEUP, apr_time_from_sec(10)}
213 int cur_action; /* index of action we decided to take this
216 int next_action = 1; /* index of first real action */
218 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
222 /* don't let waittime get longer than 1 second; otherwise, we don't
223 * react quickly to the last child exiting, and taking action can
226 waittime = waittime * 4;
227 if (waittime > apr_time_from_sec(1)) {
228 waittime = apr_time_from_sec(1);
231 /* see what action to take, if any */
232 if (action_table[next_action].action_time <= apr_time_now() - starttime) {
233 cur_action = next_action;
237 cur_action = 0; /* nothing to do */
240 /* now see who is done */
242 for (i = 0; i < max_daemons; ++i) {
243 pid_t pid = MPM_CHILD_PID(i);
246 continue; /* not every scoreboard entry is in use */
249 if (reclaim_one_pid(pid, action_table[cur_action].action)) {
250 MPM_NOTE_CHILD_KILLED(i);
259 extra_process_t *next = cur_extra->next;
261 if (reclaim_one_pid(cur_extra->pid, action_table[cur_action].action)) {
262 AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid));
269 #if APR_HAS_OTHER_CHILD
270 apr_proc_other_child_refresh_all(APR_OC_REASON_RESTART);
273 } while (not_dead_yet > 0 &&
274 action_table[cur_action].action != GIVEUP);
276 #endif /* AP_MPM_WANT_RECLAIM_CHILD_PROCESSES */
278 #ifdef AP_MPM_WANT_WAIT_OR_TIMEOUT
280 /* number of calls to wait_or_timeout between writable probes */
281 #ifndef INTERVAL_OF_WRITABLE_PROBES
282 #define INTERVAL_OF_WRITABLE_PROBES 10
284 static int wait_or_timeout_counter;
286 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
291 ++wait_or_timeout_counter;
292 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
293 wait_or_timeout_counter = 0;
297 rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
298 if (APR_STATUS_IS_EINTR(rv)) {
303 if (APR_STATUS_IS_CHILD_DONE(rv)) {
308 if ((ret = reap_children(exitcode, status)) > 0) {
313 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
317 #endif /* AP_MPM_WANT_WAIT_OR_TIMEOUT */
319 #ifdef AP_MPM_WANT_PROCESS_CHILD_STATUS
320 int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
323 const char *sigdesc = apr_signal_description_get(signum);
325 /* Child died... if it died due to a fatal error,
326 * we should simply bail out. The caller needs to
327 * check for bad rc from us and exit, running any
328 * appropriate cleanups.
330 * If the child died due to a resource shortage,
331 * the parent should limit the rate of forking
333 if (APR_PROC_CHECK_EXIT(why)) {
334 if (status == APEXIT_CHILDSICK) {
338 if (status == APEXIT_CHILDFATAL) {
339 ap_log_error(APLOG_MARK, APLOG_ALERT,
341 "Child %" APR_PID_T_FMT
342 " returned a Fatal error... Apache is exiting!",
344 return APEXIT_CHILDFATAL;
350 if (APR_PROC_CHECK_SIGNALED(why)) {
354 case AP_SIG_GRACEFUL:
359 if (APR_PROC_CHECK_CORE_DUMP(why)) {
360 ap_log_error(APLOG_MARK, APLOG_NOTICE,
362 "child pid %ld exit signal %s (%d), "
363 "possible coredump in %s",
364 (long)pid->pid, sigdesc, signum,
368 ap_log_error(APLOG_MARK, APLOG_NOTICE,
370 "child pid %ld exit signal %s (%d)",
371 (long)pid->pid, sigdesc, signum);
377 #endif /* AP_MPM_WANT_PROCESS_CHILD_STATUS */
379 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
380 void ap_sock_disable_nagle(apr_socket_t *s)
382 /* The Nagle algorithm says that we should delay sending partial
383 * packets in hopes of getting more data. We don't want to do
384 * this; we are not telnet. There are bad interactions between
385 * persistent connections and Nagle's algorithm that have very severe
386 * performance penalties. (Failing to disable Nagle is not much of a
387 * problem with simple HTTP.)
389 * In spite of these problems, failure here is not a shooting offense.
391 apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
393 if (status != APR_SUCCESS) {
394 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
395 "apr_socket_opt_set: (TCP_NODELAY)");
401 AP_DECLARE(uid_t) ap_uname2id(const char *name)
406 return (atoi(&name[1]));
408 if (!(ent = getpwnam(name))) {
409 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
410 "%s: bad user name %s", ap_server_argv0, name);
414 return (ent->pw_uid);
419 AP_DECLARE(gid_t) ap_gname2id(const char *name)
424 return (atoi(&name[1]));
426 if (!(ent = getgrnam(name))) {
427 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
428 "%s: bad group name %s", ap_server_argv0, name);
432 return (ent->gr_gid);
436 #ifndef HAVE_INITGROUPS
437 int initgroups(const char *name, gid_t basegid)
439 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32) || defined(NETWARE)
440 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
443 gid_t groups[NGROUPS_MAX];
449 groups[index++] = basegid;
451 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
452 if (g->gr_gid != basegid) {
455 for (names = g->gr_mem; *names != NULL; ++names) {
456 if (!strcmp(*names, name))
457 groups[index++] = g->gr_gid;
464 return setgroups(index, groups);
467 #endif /* def NEED_INITGROUPS */
469 #ifdef AP_MPM_USES_POD
471 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
475 *pod = apr_palloc(p, sizeof(**pod));
476 rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
477 if (rv != APR_SUCCESS) {
481 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
484 /* close these before exec. */
485 apr_file_inherit_unset((*pod)->pod_in);
486 apr_file_inherit_unset((*pod)->pod_out);
491 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
497 rv = apr_file_read(pod->pod_in, &c, &len);
499 if ((rv == APR_SUCCESS) && (len == 1)) {
503 if (rv != APR_SUCCESS) {
510 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
514 rv = apr_file_close(pod->pod_out);
515 if (rv != APR_SUCCESS) {
519 rv = apr_file_close(pod->pod_in);
520 if (rv != APR_SUCCESS) {
527 static apr_status_t pod_signal_internal(ap_pod_t *pod)
530 char char_of_death = '!';
533 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
534 if (rv != APR_SUCCESS) {
535 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
536 "write pipe_of_death");
542 /* This function connects to the server, then immediately closes the connection.
543 * This permits the MPM to skip the poll when there is only one listening
544 * socket, because it provides a alternate way to unblock an accept() when
547 static apr_status_t dummy_connection(ap_pod_t *pod)
555 /* create a temporary pool for the socket. pconf stays around too long */
556 rv = apr_pool_create(&p, pod->p);
557 if (rv != APR_SUCCESS) {
561 rv = apr_socket_create(&sock, ap_listeners->bind_addr->family,
563 if (rv != APR_SUCCESS) {
564 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
565 "get socket to connect to listener");
570 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
571 * queued connections before it starts blocking local connects...
572 * we need to keep from blocking too long and instead return an error,
573 * because the MPM won't want to hold up a graceful restart for a
576 rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
577 if (rv != APR_SUCCESS) {
578 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
579 "set timeout on socket to connect to listener");
580 apr_socket_close(sock);
585 rv = apr_socket_connect(sock, ap_listeners->bind_addr);
586 if (rv != APR_SUCCESS) {
587 int log_level = APLOG_WARNING;
589 if (APR_STATUS_IS_TIMEUP(rv)) {
590 /* probably some server processes bailed out already and there
591 * is nobody around to call accept and clear out the kernel
592 * connection queue; usually this is not worth logging
594 log_level = APLOG_DEBUG;
597 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
598 "connect to listener on %pI", ap_listeners->bind_addr);
601 /* Create the request string. We include a User-Agent so that
602 * adminstrators can track down the cause of the odd-looking
603 * requests in their logs.
605 srequest = apr_pstrcat(p, "GET / HTTP/1.0\r\nUser-Agent: ",
606 ap_get_server_version(),
607 " (internal dummy connection)\r\n\r\n", NULL);
609 /* Since some operating systems support buffering of data or entire
610 * requests in the kernel, we send a simple request, to make sure
611 * the server pops out of a blocking accept().
613 /* XXX: This is HTTP specific. We should look at the Protocol for each
614 * listener, and send the correct type of request to trigger any Accept
617 len = strlen(srequest);
618 apr_socket_send(sock, srequest, &len);
619 apr_socket_close(sock);
625 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
629 rv = pod_signal_internal(pod);
630 if (rv != APR_SUCCESS) {
634 return dummy_connection(pod);
637 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
640 apr_status_t rv = APR_SUCCESS;
642 /* we don't write anything to the pod here... we assume
643 * that the would-be reader of the pod has another way to
644 * see that it is time to die once we wake it up
646 * writing lots of things to the pod at once is very
647 * problematic... we can fill the kernel pipe buffer and
648 * be blocked until somebody consumes some bytes or
649 * we hit a timeout... if we hit a timeout we can't just
650 * keep trying because maybe we'll never successfully
651 * write again... but then maybe we'll leave would-be
652 * readers stranded (a number of them could be tied up for
653 * a while serving time-consuming requests)
655 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
656 rv = dummy_connection(pod);
659 #endif /* #ifdef AP_MPM_USES_POD */
661 /* standard mpm configuration handling */
662 #ifdef AP_MPM_WANT_SET_PIDFILE
663 const char *ap_pid_fname = NULL;
665 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
668 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
673 if (cmd->server->is_virtual) {
674 return "PidFile directive not allowed in <VirtualHost>";
682 #ifdef AP_MPM_WANT_SET_SCOREBOARD
683 const char * ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
686 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
691 ap_scoreboard_fname = arg;
696 #ifdef AP_MPM_WANT_SET_LOCKFILE
697 const char *ap_lock_fname = NULL;
699 const char *ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
702 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
712 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
713 int ap_max_requests_per_child = 0;
715 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
718 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
723 ap_max_requests_per_child = atoi(arg);
729 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
730 char ap_coredump_dir[MAX_STRING_LEN];
731 int ap_coredumpdir_configured;
733 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
739 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
744 fname = ap_server_root_relative(cmd->pool, arg);
746 return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
749 if ((rv = apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) {
750 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
751 " does not exist", NULL);
753 if (finfo.filetype != APR_DIR) {
754 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
755 " is not a directory", NULL);
757 apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
758 ap_coredumpdir_configured = 1;
763 #ifdef AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
764 int ap_graceful_shutdown_timeout = 0;
766 const char * ap_mpm_set_graceful_shutdown(cmd_parms *cmd, void *dummy,
769 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
773 ap_graceful_shutdown_timeout = atoi(arg);
778 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
779 apr_lockmech_e ap_accept_lock_mech = APR_LOCK_DEFAULT;
781 const char ap_valid_accept_mutex_string[] =
782 "Valid accept mutexes for this platform and MPM are: default"
783 #if APR_HAS_FLOCK_SERIALIZE
786 #if APR_HAS_FCNTL_SERIALIZE
789 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
792 #if APR_HAS_POSIXSEM_SERIALIZE
795 #if APR_HAS_PROC_PTHREAD_SERIALIZE
800 AP_DECLARE(const char *) ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
804 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
809 if (!strcasecmp(arg, "default")) {
810 ap_accept_lock_mech = APR_LOCK_DEFAULT;
812 #if APR_HAS_FLOCK_SERIALIZE
813 else if (!strcasecmp(arg, "flock")) {
814 ap_accept_lock_mech = APR_LOCK_FLOCK;
817 #if APR_HAS_FCNTL_SERIALIZE
818 else if (!strcasecmp(arg, "fcntl")) {
819 ap_accept_lock_mech = APR_LOCK_FCNTL;
823 /* perchild can't use SysV sems because the permissions on the accept
824 * mutex can't be set to allow all processes to use the mutex and
825 * at the same time keep all users from being able to dink with the
828 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
829 else if (!strcasecmp(arg, "sysvsem")) {
830 ap_accept_lock_mech = APR_LOCK_SYSVSEM;
833 #if APR_HAS_POSIXSEM_SERIALIZE
834 else if (!strcasecmp(arg, "posixsem")) {
835 ap_accept_lock_mech = APR_LOCK_POSIXSEM;
838 #if APR_HAS_PROC_PTHREAD_SERIALIZE
839 else if (!strcasecmp(arg, "pthread")) {
840 ap_accept_lock_mech = APR_LOCK_PROC_PTHREAD;
844 return apr_pstrcat(cmd->pool, arg, " is an invalid mutex mechanism; ",
845 ap_valid_accept_mutex_string, NULL);
852 #ifdef AP_MPM_WANT_SIGNAL_SERVER
854 static const char *dash_k_arg;
856 static int send_signal(pid_t pid, int sig)
858 if (kill(pid, sig) < 0) {
859 ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL,
860 "sending signal to server");
866 int ap_signal_server(int *exit_status, apr_pool_t *pconf)
871 int have_pid_file = 0;
876 rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
877 if (rv != APR_SUCCESS) {
878 if (rv != APR_ENOENT) {
879 ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL,
880 "Error retrieving pid file %s", ap_pid_fname);
881 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
882 "Remove it before continuing if it is corrupted.");
886 status = "httpd (no pid file) not running";
890 if (kill(otherpid, 0) == 0) {
892 status = apr_psprintf(pconf,
893 "httpd (pid %" APR_PID_T_FMT ") already "
894 "running", otherpid);
897 status = apr_psprintf(pconf,
898 "httpd (pid %" APR_PID_T_FMT "?) not running",
903 if (!strcmp(dash_k_arg, "start")) {
905 printf("%s\n", status);
910 if (!strcmp(dash_k_arg, "stop")) {
912 printf("%s\n", status);
915 send_signal(otherpid, SIGTERM);
920 if (!strcmp(dash_k_arg, "restart")) {
922 printf("httpd not running, trying to start\n");
925 *exit_status = send_signal(otherpid, SIGHUP);
930 if (!strcmp(dash_k_arg, "graceful")) {
932 printf("httpd not running, trying to start\n");
935 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL);
940 if (!strcmp(dash_k_arg, "graceful-stop")) {
941 #ifdef AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
943 printf("%s\n", status);
946 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL_STOP);
949 printf("httpd MPM \"" MPM_NAME "\" does not support graceful-stop\n");
957 void ap_mpm_rewrite_args(process_rec *process)
959 apr_array_header_t *mpm_new_argv;
966 mpm_new_argv = apr_array_make(process->pool, process->argc,
967 sizeof(const char **));
968 *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
969 fixed_args = mpm_new_argv->nelts;
970 apr_getopt_init(&opt, process->pool, process->argc, process->argv);
973 /* option char returned by apr_getopt() will be stored in optbuf[1] */
975 while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
976 optbuf + 1, &optarg)) == APR_SUCCESS) {
980 if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
981 !strcmp(optarg, "restart") || !strcmp(optarg, "graceful") ||
982 !strcmp(optarg, "graceful-stop")) {
988 *(const char **)apr_array_push(mpm_new_argv) =
989 apr_pstrdup(process->pool, optbuf);
991 *(const char **)apr_array_push(mpm_new_argv) = optarg;
996 /* back up to capture the bad argument */
997 if (rv == APR_BADCH || rv == APR_BADARG) {
1001 while (opt->ind < opt->argc) {
1002 *(const char **)apr_array_push(mpm_new_argv) =
1003 apr_pstrdup(process->pool, opt->argv[opt->ind++]);
1006 process->argc = mpm_new_argv->nelts;
1007 process->argv = (const char * const *)mpm_new_argv->elts;
1010 APR_REGISTER_OPTIONAL_FN(ap_signal_server);
1014 #endif /* AP_MPM_WANT_SIGNAL_SERVER */
1016 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
1017 apr_uint32_t ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
1019 const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
1023 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1028 value = strtol(arg, NULL, 0);
1029 if (value < 0 || errno == ERANGE)
1030 return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
1033 ap_max_mem_free = (apr_uint32_t)value * 1024;
1038 #endif /* AP_MPM_WANT_SET_MAX_MEM_FREE */
1040 #ifdef AP_MPM_WANT_SET_STACKSIZE
1041 apr_size_t ap_thread_stacksize = 0; /* use system default */
1043 const char *ap_mpm_set_thread_stacksize(cmd_parms *cmd, void *dummy,
1047 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1052 value = strtol(arg, NULL, 0);
1053 if (value < 0 || errno == ERANGE)
1054 return apr_pstrcat(cmd->pool, "Invalid ThreadStackSize value: ",
1057 ap_thread_stacksize = (apr_size_t)value;
1062 #endif /* AP_MPM_WANT_SET_STACKSIZE */
1064 #ifdef AP_MPM_WANT_FATAL_SIGNAL_HANDLER
1066 static pid_t parent_pid, my_pid;
1069 #if AP_ENABLE_EXCEPTION_HOOK
1071 static int exception_hook_enabled;
1073 const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
1076 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1081 if (cmd->server->is_virtual) {
1082 return "EnableExceptionHook directive not allowed in <VirtualHost>";
1085 if (strcasecmp(arg, "on") == 0) {
1086 exception_hook_enabled = 1;
1088 else if (strcasecmp(arg, "off") == 0) {
1089 exception_hook_enabled = 0;
1092 return "parameter must be 'on' or 'off'";
1098 static void run_fatal_exception_hook(int sig)
1100 ap_exception_info_t ei = {0};
1102 if (exception_hook_enabled &&
1104 my_pid != parent_pid) {
1107 ap_run_fatal_exception(&ei);
1110 #endif /* AP_ENABLE_EXCEPTION_HOOK */
1112 /* handle all varieties of core dumping signals */
1113 static void sig_coredump(int sig)
1115 apr_filepath_set(ap_coredump_dir, pconf);
1116 apr_signal(sig, SIG_DFL);
1117 #if AP_ENABLE_EXCEPTION_HOOK
1118 run_fatal_exception_hook(sig);
1120 /* linuxthreads issue calling getpid() here:
1121 * This comparison won't match if the crashing thread is
1122 * some module's thread that runs in the parent process.
1123 * The fallout, which is limited to linuxthreads:
1124 * The special log message won't be written when such a
1125 * thread in the parent causes the parent to crash.
1127 if (getpid() == parent_pid) {
1128 ap_log_error(APLOG_MARK, APLOG_NOTICE,
1130 "seg fault or similar nasty error detected "
1131 "in the parent process");
1132 /* XXX we can probably add some rudimentary cleanup code here,
1133 * like getting rid of the pid file. If any additional bad stuff
1134 * happens, we are protected from recursive errors taking down the
1135 * system since this function is no longer the signal handler GLA
1138 kill(getpid(), sig);
1139 /* At this point we've got sig blocked, because we're still inside
1140 * the signal handler. When we leave the signal handler it will
1141 * be unblocked, and we'll take the signal... and coredump or whatever
1142 * is appropriate for this particular Unix. In addition the parent
1143 * will see the real signal we received -- whereas if we called
1144 * abort() here, the parent would only see SIGABRT.
1148 apr_status_t ap_fatal_signal_child_setup(server_rec *s)
1154 apr_status_t ap_fatal_signal_setup(server_rec *s, apr_pool_t *in_pconf)
1156 #ifndef NO_USE_SIGACTION
1157 struct sigaction sa;
1159 sigemptyset(&sa.sa_mask);
1161 #if defined(SA_ONESHOT)
1162 sa.sa_flags = SA_ONESHOT;
1163 #elif defined(SA_RESETHAND)
1164 sa.sa_flags = SA_RESETHAND;
1169 sa.sa_handler = sig_coredump;
1170 if (sigaction(SIGSEGV, &sa, NULL) < 0)
1171 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGSEGV)");
1173 if (sigaction(SIGBUS, &sa, NULL) < 0)
1174 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGBUS)");
1177 if (sigaction(SIGABORT, &sa, NULL) < 0)
1178 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABORT)");
1181 if (sigaction(SIGABRT, &sa, NULL) < 0)
1182 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABRT)");
1185 if (sigaction(SIGILL, &sa, NULL) < 0)
1186 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGILL)");
1189 #else /* NO_USE_SIGACTION */
1191 apr_signal(SIGSEGV, sig_coredump);
1193 apr_signal(SIGBUS, sig_coredump);
1196 apr_signal(SIGABORT, sig_coredump);
1197 #endif /* SIGABORT */
1199 apr_signal(SIGABRT, sig_coredump);
1200 #endif /* SIGABRT */
1202 apr_signal(SIGILL, sig_coredump);
1205 #endif /* NO_USE_SIGACTION */
1208 parent_pid = my_pid = getpid();
1213 #endif /* AP_MPM_WANT_FATAL_SIGNAL_HANDLER */