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);
277 void ap_relieve_child_processes(void)
280 extra_process_t *cur_extra;
283 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
285 /* now see who is done */
286 for (i = 0; i < max_daemons; ++i) {
287 pid_t pid = MPM_CHILD_PID(i);
290 continue; /* not every scoreboard entry is in use */
293 if (reclaim_one_pid(pid, DO_NOTHING)) {
294 MPM_NOTE_CHILD_KILLED(i);
300 extra_process_t *next = cur_extra->next;
302 if (reclaim_one_pid(cur_extra->pid, DO_NOTHING)) {
303 AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid));
308 #endif /* AP_MPM_WANT_RECLAIM_CHILD_PROCESSES */
310 #ifdef AP_MPM_WANT_WAIT_OR_TIMEOUT
312 /* number of calls to wait_or_timeout between writable probes */
313 #ifndef INTERVAL_OF_WRITABLE_PROBES
314 #define INTERVAL_OF_WRITABLE_PROBES 10
316 static int wait_or_timeout_counter;
318 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
323 ++wait_or_timeout_counter;
324 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
325 wait_or_timeout_counter = 0;
329 rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
330 if (APR_STATUS_IS_EINTR(rv)) {
335 if (APR_STATUS_IS_CHILD_DONE(rv)) {
340 if ((ret = reap_children(exitcode, status)) > 0) {
345 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
349 #endif /* AP_MPM_WANT_WAIT_OR_TIMEOUT */
351 #ifdef AP_MPM_WANT_PROCESS_CHILD_STATUS
352 int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
355 const char *sigdesc = apr_signal_description_get(signum);
357 /* Child died... if it died due to a fatal error,
358 * we should simply bail out. The caller needs to
359 * check for bad rc from us and exit, running any
360 * appropriate cleanups.
362 * If the child died due to a resource shortage,
363 * the parent should limit the rate of forking
365 if (APR_PROC_CHECK_EXIT(why)) {
366 if (status == APEXIT_CHILDSICK) {
370 if (status == APEXIT_CHILDFATAL) {
371 ap_log_error(APLOG_MARK, APLOG_ALERT,
373 "Child %" APR_PID_T_FMT
374 " returned a Fatal error... Apache is exiting!",
376 return APEXIT_CHILDFATAL;
382 if (APR_PROC_CHECK_SIGNALED(why)) {
386 case AP_SIG_GRACEFUL:
391 if (APR_PROC_CHECK_CORE_DUMP(why)) {
392 ap_log_error(APLOG_MARK, APLOG_NOTICE,
394 "child pid %ld exit signal %s (%d), "
395 "possible coredump in %s",
396 (long)pid->pid, sigdesc, signum,
400 ap_log_error(APLOG_MARK, APLOG_NOTICE,
402 "child pid %ld exit signal %s (%d)",
403 (long)pid->pid, sigdesc, signum);
409 #endif /* AP_MPM_WANT_PROCESS_CHILD_STATUS */
411 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
412 void ap_sock_disable_nagle(apr_socket_t *s)
414 /* The Nagle algorithm says that we should delay sending partial
415 * packets in hopes of getting more data. We don't want to do
416 * this; we are not telnet. There are bad interactions between
417 * persistent connections and Nagle's algorithm that have very severe
418 * performance penalties. (Failing to disable Nagle is not much of a
419 * problem with simple HTTP.)
421 * In spite of these problems, failure here is not a shooting offense.
423 apr_status_t status = apr_socket_opt_set(s, APR_TCP_NODELAY, 1);
425 if (status != APR_SUCCESS) {
426 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
427 "apr_socket_opt_set: (TCP_NODELAY)");
433 AP_DECLARE(uid_t) ap_uname2id(const char *name)
438 return (atoi(&name[1]));
440 if (!(ent = getpwnam(name))) {
441 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
442 "%s: bad user name %s", ap_server_argv0, name);
446 return (ent->pw_uid);
451 AP_DECLARE(gid_t) ap_gname2id(const char *name)
456 return (atoi(&name[1]));
458 if (!(ent = getgrnam(name))) {
459 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
460 "%s: bad group name %s", ap_server_argv0, name);
464 return (ent->gr_gid);
468 #ifndef HAVE_INITGROUPS
469 int initgroups(const char *name, gid_t basegid)
471 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32) || defined(NETWARE)
472 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
475 gid_t groups[NGROUPS_MAX];
481 groups[index++] = basegid;
483 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
484 if (g->gr_gid != basegid) {
487 for (names = g->gr_mem; *names != NULL; ++names) {
488 if (!strcmp(*names, name))
489 groups[index++] = g->gr_gid;
496 return setgroups(index, groups);
499 #endif /* def NEED_INITGROUPS */
501 #ifdef AP_MPM_USES_POD
503 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
507 *pod = apr_palloc(p, sizeof(**pod));
508 rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
509 if (rv != APR_SUCCESS) {
513 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
516 /* close these before exec. */
517 apr_file_inherit_unset((*pod)->pod_in);
518 apr_file_inherit_unset((*pod)->pod_out);
523 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
529 rv = apr_file_read(pod->pod_in, &c, &len);
531 if ((rv == APR_SUCCESS) && (len == 1)) {
535 if (rv != APR_SUCCESS) {
542 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
546 rv = apr_file_close(pod->pod_out);
547 if (rv != APR_SUCCESS) {
551 rv = apr_file_close(pod->pod_in);
552 if (rv != APR_SUCCESS) {
559 static apr_status_t pod_signal_internal(ap_pod_t *pod)
562 char char_of_death = '!';
565 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
566 if (rv != APR_SUCCESS) {
567 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
568 "write pipe_of_death");
574 /* This function connects to the server, then immediately closes the connection.
575 * This permits the MPM to skip the poll when there is only one listening
576 * socket, because it provides a alternate way to unblock an accept() when
579 static apr_status_t dummy_connection(ap_pod_t *pod)
587 /* create a temporary pool for the socket. pconf stays around too long */
588 rv = apr_pool_create(&p, pod->p);
589 if (rv != APR_SUCCESS) {
593 rv = apr_socket_create(&sock, ap_listeners->bind_addr->family,
595 if (rv != APR_SUCCESS) {
596 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
597 "get socket to connect to listener");
602 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
603 * queued connections before it starts blocking local connects...
604 * we need to keep from blocking too long and instead return an error,
605 * because the MPM won't want to hold up a graceful restart for a
608 rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
609 if (rv != APR_SUCCESS) {
610 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
611 "set timeout on socket to connect to listener");
612 apr_socket_close(sock);
617 rv = apr_socket_connect(sock, ap_listeners->bind_addr);
618 if (rv != APR_SUCCESS) {
619 int log_level = APLOG_WARNING;
621 if (APR_STATUS_IS_TIMEUP(rv)) {
622 /* probably some server processes bailed out already and there
623 * is nobody around to call accept and clear out the kernel
624 * connection queue; usually this is not worth logging
626 log_level = APLOG_DEBUG;
629 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
630 "connect to listener on %pI", ap_listeners->bind_addr);
633 /* Create the request string. We include a User-Agent so that
634 * adminstrators can track down the cause of the odd-looking
635 * requests in their logs.
637 srequest = apr_pstrcat(p, "GET / HTTP/1.0\r\nUser-Agent: ",
638 ap_get_server_version(),
639 " (internal dummy connection)\r\n\r\n", NULL);
641 /* Since some operating systems support buffering of data or entire
642 * requests in the kernel, we send a simple request, to make sure
643 * the server pops out of a blocking accept().
645 /* XXX: This is HTTP specific. We should look at the Protocol for each
646 * listener, and send the correct type of request to trigger any Accept
649 len = strlen(srequest);
650 apr_socket_send(sock, srequest, &len);
651 apr_socket_close(sock);
657 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
661 rv = pod_signal_internal(pod);
662 if (rv != APR_SUCCESS) {
666 return dummy_connection(pod);
669 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
672 apr_status_t rv = APR_SUCCESS;
674 /* we don't write anything to the pod here... we assume
675 * that the would-be reader of the pod has another way to
676 * see that it is time to die once we wake it up
678 * writing lots of things to the pod at once is very
679 * problematic... we can fill the kernel pipe buffer and
680 * be blocked until somebody consumes some bytes or
681 * we hit a timeout... if we hit a timeout we can't just
682 * keep trying because maybe we'll never successfully
683 * write again... but then maybe we'll leave would-be
684 * readers stranded (a number of them could be tied up for
685 * a while serving time-consuming requests)
687 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
688 rv = dummy_connection(pod);
691 #endif /* #ifdef AP_MPM_USES_POD */
693 /* standard mpm configuration handling */
694 #ifdef AP_MPM_WANT_SET_PIDFILE
695 const char *ap_pid_fname = NULL;
697 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
700 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
705 if (cmd->server->is_virtual) {
706 return "PidFile directive not allowed in <VirtualHost>";
714 #ifdef AP_MPM_WANT_SET_SCOREBOARD
715 const char * ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
718 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
723 ap_scoreboard_fname = arg;
728 #ifdef AP_MPM_WANT_SET_LOCKFILE
729 const char *ap_lock_fname = NULL;
731 const char *ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
734 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
744 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
745 int ap_max_requests_per_child = 0;
747 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
750 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
755 ap_max_requests_per_child = atoi(arg);
761 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
762 char ap_coredump_dir[MAX_STRING_LEN];
763 int ap_coredumpdir_configured;
765 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
771 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
776 fname = ap_server_root_relative(cmd->pool, arg);
778 return apr_pstrcat(cmd->pool, "Invalid CoreDumpDirectory path ",
781 if ((rv = apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool)) != APR_SUCCESS) {
782 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
783 " does not exist", NULL);
785 if (finfo.filetype != APR_DIR) {
786 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
787 " is not a directory", NULL);
789 apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
790 ap_coredumpdir_configured = 1;
795 #ifdef AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
796 int ap_graceful_shutdown_timeout = 0;
798 const char * ap_mpm_set_graceful_shutdown(cmd_parms *cmd, void *dummy,
801 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
805 ap_graceful_shutdown_timeout = atoi(arg);
810 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
811 apr_lockmech_e ap_accept_lock_mech = APR_LOCK_DEFAULT;
813 const char ap_valid_accept_mutex_string[] =
814 "Valid accept mutexes for this platform and MPM are: default"
815 #if APR_HAS_FLOCK_SERIALIZE
818 #if APR_HAS_FCNTL_SERIALIZE
821 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
824 #if APR_HAS_POSIXSEM_SERIALIZE
827 #if APR_HAS_PROC_PTHREAD_SERIALIZE
832 AP_DECLARE(const char *) ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
836 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
841 if (!strcasecmp(arg, "default")) {
842 ap_accept_lock_mech = APR_LOCK_DEFAULT;
844 #if APR_HAS_FLOCK_SERIALIZE
845 else if (!strcasecmp(arg, "flock")) {
846 ap_accept_lock_mech = APR_LOCK_FLOCK;
849 #if APR_HAS_FCNTL_SERIALIZE
850 else if (!strcasecmp(arg, "fcntl")) {
851 ap_accept_lock_mech = APR_LOCK_FCNTL;
855 /* perchild can't use SysV sems because the permissions on the accept
856 * mutex can't be set to allow all processes to use the mutex and
857 * at the same time keep all users from being able to dink with the
860 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
861 else if (!strcasecmp(arg, "sysvsem")) {
862 ap_accept_lock_mech = APR_LOCK_SYSVSEM;
865 #if APR_HAS_POSIXSEM_SERIALIZE
866 else if (!strcasecmp(arg, "posixsem")) {
867 ap_accept_lock_mech = APR_LOCK_POSIXSEM;
870 #if APR_HAS_PROC_PTHREAD_SERIALIZE
871 else if (!strcasecmp(arg, "pthread")) {
872 ap_accept_lock_mech = APR_LOCK_PROC_PTHREAD;
876 return apr_pstrcat(cmd->pool, arg, " is an invalid mutex mechanism; ",
877 ap_valid_accept_mutex_string, NULL);
884 #ifdef AP_MPM_WANT_SIGNAL_SERVER
886 static const char *dash_k_arg;
888 static int send_signal(pid_t pid, int sig)
890 if (kill(pid, sig) < 0) {
891 ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL,
892 "sending signal to server");
898 int ap_signal_server(int *exit_status, apr_pool_t *pconf)
903 int have_pid_file = 0;
908 rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
909 if (rv != APR_SUCCESS) {
910 if (rv != APR_ENOENT) {
911 ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL,
912 "Error retrieving pid file %s", ap_pid_fname);
913 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
914 "Remove it before continuing if it is corrupted.");
918 status = "httpd (no pid file) not running";
922 if (kill(otherpid, 0) == 0) {
924 status = apr_psprintf(pconf,
925 "httpd (pid %" APR_PID_T_FMT ") already "
926 "running", otherpid);
929 status = apr_psprintf(pconf,
930 "httpd (pid %" APR_PID_T_FMT "?) not running",
935 if (!strcmp(dash_k_arg, "start")) {
937 printf("%s\n", status);
942 if (!strcmp(dash_k_arg, "stop")) {
944 printf("%s\n", status);
947 send_signal(otherpid, SIGTERM);
952 if (!strcmp(dash_k_arg, "restart")) {
954 printf("httpd not running, trying to start\n");
957 *exit_status = send_signal(otherpid, SIGHUP);
962 if (!strcmp(dash_k_arg, "graceful")) {
964 printf("httpd not running, trying to start\n");
967 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL);
972 if (!strcmp(dash_k_arg, "graceful-stop")) {
973 #ifdef AP_MPM_WANT_SET_GRACEFUL_SHUTDOWN
975 printf("%s\n", status);
978 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL_STOP);
981 printf("httpd MPM \"" MPM_NAME "\" does not support graceful-stop\n");
989 void ap_mpm_rewrite_args(process_rec *process)
991 apr_array_header_t *mpm_new_argv;
998 mpm_new_argv = apr_array_make(process->pool, process->argc,
999 sizeof(const char **));
1000 *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
1001 fixed_args = mpm_new_argv->nelts;
1002 apr_getopt_init(&opt, process->pool, process->argc, process->argv);
1005 /* option char returned by apr_getopt() will be stored in optbuf[1] */
1007 while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
1008 optbuf + 1, &optarg)) == APR_SUCCESS) {
1012 if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
1013 !strcmp(optarg, "restart") || !strcmp(optarg, "graceful") ||
1014 !strcmp(optarg, "graceful-stop")) {
1015 dash_k_arg = optarg;
1020 *(const char **)apr_array_push(mpm_new_argv) =
1021 apr_pstrdup(process->pool, optbuf);
1023 *(const char **)apr_array_push(mpm_new_argv) = optarg;
1028 /* back up to capture the bad argument */
1029 if (rv == APR_BADCH || rv == APR_BADARG) {
1033 while (opt->ind < opt->argc) {
1034 *(const char **)apr_array_push(mpm_new_argv) =
1035 apr_pstrdup(process->pool, opt->argv[opt->ind++]);
1038 process->argc = mpm_new_argv->nelts;
1039 process->argv = (const char * const *)mpm_new_argv->elts;
1042 APR_REGISTER_OPTIONAL_FN(ap_signal_server);
1046 #endif /* AP_MPM_WANT_SIGNAL_SERVER */
1048 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
1049 apr_uint32_t ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
1051 const char *ap_mpm_set_max_mem_free(cmd_parms *cmd, void *dummy,
1055 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1060 value = strtol(arg, NULL, 0);
1061 if (value < 0 || errno == ERANGE)
1062 return apr_pstrcat(cmd->pool, "Invalid MaxMemFree value: ",
1065 ap_max_mem_free = (apr_uint32_t)value * 1024;
1070 #endif /* AP_MPM_WANT_SET_MAX_MEM_FREE */
1072 #ifdef AP_MPM_WANT_SET_STACKSIZE
1073 apr_size_t ap_thread_stacksize = 0; /* use system default */
1075 const char *ap_mpm_set_thread_stacksize(cmd_parms *cmd, void *dummy,
1079 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1084 value = strtol(arg, NULL, 0);
1085 if (value < 0 || errno == ERANGE)
1086 return apr_pstrcat(cmd->pool, "Invalid ThreadStackSize value: ",
1089 ap_thread_stacksize = (apr_size_t)value;
1094 #endif /* AP_MPM_WANT_SET_STACKSIZE */
1096 #ifdef AP_MPM_WANT_FATAL_SIGNAL_HANDLER
1098 static pid_t parent_pid, my_pid;
1101 #if AP_ENABLE_EXCEPTION_HOOK
1103 static int exception_hook_enabled;
1105 const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
1108 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
1113 if (cmd->server->is_virtual) {
1114 return "EnableExceptionHook directive not allowed in <VirtualHost>";
1117 if (strcasecmp(arg, "on") == 0) {
1118 exception_hook_enabled = 1;
1120 else if (strcasecmp(arg, "off") == 0) {
1121 exception_hook_enabled = 0;
1124 return "parameter must be 'on' or 'off'";
1130 static void run_fatal_exception_hook(int sig)
1132 ap_exception_info_t ei = {0};
1134 if (exception_hook_enabled &&
1136 my_pid != parent_pid) {
1139 ap_run_fatal_exception(&ei);
1142 #endif /* AP_ENABLE_EXCEPTION_HOOK */
1144 /* handle all varieties of core dumping signals */
1145 static void sig_coredump(int sig)
1147 apr_filepath_set(ap_coredump_dir, pconf);
1148 apr_signal(sig, SIG_DFL);
1149 #if AP_ENABLE_EXCEPTION_HOOK
1150 run_fatal_exception_hook(sig);
1152 /* linuxthreads issue calling getpid() here:
1153 * This comparison won't match if the crashing thread is
1154 * some module's thread that runs in the parent process.
1155 * The fallout, which is limited to linuxthreads:
1156 * The special log message won't be written when such a
1157 * thread in the parent causes the parent to crash.
1159 if (getpid() == parent_pid) {
1160 ap_log_error(APLOG_MARK, APLOG_NOTICE,
1162 "seg fault or similar nasty error detected "
1163 "in the parent process");
1164 /* XXX we can probably add some rudimentary cleanup code here,
1165 * like getting rid of the pid file. If any additional bad stuff
1166 * happens, we are protected from recursive errors taking down the
1167 * system since this function is no longer the signal handler GLA
1170 kill(getpid(), sig);
1171 /* At this point we've got sig blocked, because we're still inside
1172 * the signal handler. When we leave the signal handler it will
1173 * be unblocked, and we'll take the signal... and coredump or whatever
1174 * is appropriate for this particular Unix. In addition the parent
1175 * will see the real signal we received -- whereas if we called
1176 * abort() here, the parent would only see SIGABRT.
1180 apr_status_t ap_fatal_signal_child_setup(server_rec *s)
1186 apr_status_t ap_fatal_signal_setup(server_rec *s, apr_pool_t *in_pconf)
1188 #ifndef NO_USE_SIGACTION
1189 struct sigaction sa;
1191 sigemptyset(&sa.sa_mask);
1193 #if defined(SA_ONESHOT)
1194 sa.sa_flags = SA_ONESHOT;
1195 #elif defined(SA_RESETHAND)
1196 sa.sa_flags = SA_RESETHAND;
1201 sa.sa_handler = sig_coredump;
1202 if (sigaction(SIGSEGV, &sa, NULL) < 0)
1203 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGSEGV)");
1205 if (sigaction(SIGBUS, &sa, NULL) < 0)
1206 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGBUS)");
1209 if (sigaction(SIGABORT, &sa, NULL) < 0)
1210 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABORT)");
1213 if (sigaction(SIGABRT, &sa, NULL) < 0)
1214 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABRT)");
1217 if (sigaction(SIGILL, &sa, NULL) < 0)
1218 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGILL)");
1221 #else /* NO_USE_SIGACTION */
1223 apr_signal(SIGSEGV, sig_coredump);
1225 apr_signal(SIGBUS, sig_coredump);
1228 apr_signal(SIGABORT, sig_coredump);
1229 #endif /* SIGABORT */
1231 apr_signal(SIGABRT, sig_coredump);
1232 #endif /* SIGABRT */
1234 apr_signal(SIGILL, sig_coredump);
1237 #endif /* NO_USE_SIGACTION */
1240 parent_pid = my_pid = getpid();
1245 #endif /* AP_MPM_WANT_FATAL_SIGNAL_HANDLER */