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
31 #include "apr_thread_proc.h"
32 #include "apr_signal.h"
33 #include "apr_strings.h"
34 #define APR_WANT_STRFUNC
36 #include "apr_getopt.h"
37 #include "apr_optional.h"
38 #include "apr_allocator.h"
41 #include "http_config.h"
43 #include "http_main.h"
44 #include "mpm_common.h"
46 #include "ap_listen.h"
47 #include "scoreboard.h"
48 #include "util_mutex.h"
61 APLOG_USE_MODULE(core);
63 typedef enum {DO_NOTHING, SEND_SIGTERM, SEND_SIGKILL, GIVEUP} action_t;
65 typedef struct extra_process_t {
66 struct extra_process_t *next;
71 static extra_process_t *extras;
73 void ap_register_extra_mpm_process(pid_t pid, ap_generation_t gen)
75 extra_process_t *p = (extra_process_t *)malloc(sizeof(extra_process_t));
83 int ap_unregister_extra_mpm_process(pid_t pid, ap_generation_t *gen)
85 extra_process_t *cur = extras;
86 extra_process_t *prev = NULL;
88 while (cur && cur->pid != pid) {
95 prev->next = cur->next;
102 return 1; /* found */
105 /* we don't know about any such process */
110 static int reclaim_one_pid(pid_t pid, action_t action)
113 apr_status_t waitret;
117 /* Ensure pid sanity. */
123 waitret = apr_proc_wait(&proc, &status, &why, APR_NOWAIT);
124 if (waitret != APR_CHILD_NOTDONE) {
125 if (waitret == APR_CHILD_DONE)
126 ap_process_child_status(&proc, why, status);
135 /* ok, now it's being annoying */
136 ap_log_error(APLOG_MARK, APLOG_WARNING,
138 "child process %" APR_PID_T_FMT
139 " still did not exit, "
146 ap_log_error(APLOG_MARK, APLOG_ERR,
148 "child process %" APR_PID_T_FMT
149 " still did not exit, "
156 /* gave it our best shot, but alas... If this really
157 * is a child we are trying to kill and it really hasn't
158 * exited, we will likely fail to bind to the port
161 ap_log_error(APLOG_MARK, APLOG_ERR,
163 "could not make child process %" APR_PID_T_FMT
165 "attempting to continue anyway",
173 void ap_reclaim_child_processes(int terminate,
174 ap_reclaim_callback_fn_t *mpm_callback)
176 apr_time_t waittime = 1024 * 16;
178 extra_process_t *cur_extra;
181 apr_time_t starttime = apr_time_now();
182 /* this table of actions and elapsed times tells what action is taken
183 * at which elapsed time from starting the reclaim
187 apr_time_t action_time;
189 {DO_NOTHING, 0}, /* dummy entry for iterations where we reap
190 * children but take no action against
193 {SEND_SIGTERM, apr_time_from_sec(3)},
194 {SEND_SIGTERM, apr_time_from_sec(5)},
195 {SEND_SIGTERM, apr_time_from_sec(7)},
196 {SEND_SIGKILL, apr_time_from_sec(9)},
197 {GIVEUP, apr_time_from_sec(10)}
199 int cur_action; /* index of action we decided to take this
202 int next_action = 1; /* index of first real action */
204 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
208 /* don't let waittime get longer than 1 second; otherwise, we don't
209 * react quickly to the last child exiting, and taking action can
212 waittime = waittime * 4;
213 if (waittime > apr_time_from_sec(1)) {
214 waittime = apr_time_from_sec(1);
217 /* see what action to take, if any */
218 if (action_table[next_action].action_time <= apr_time_now() - starttime) {
219 cur_action = next_action;
223 cur_action = 0; /* nothing to do */
226 /* now see who is done */
228 for (i = 0; i < max_daemons; ++i) {
229 process_score *ps = ap_get_scoreboard_process(i);
233 continue; /* not every scoreboard entry is in use */
236 if (reclaim_one_pid(pid, action_table[cur_action].action)) {
237 mpm_callback(i, 0, 0);
246 ap_generation_t old_gen;
247 extra_process_t *next = cur_extra->next;
249 if (reclaim_one_pid(cur_extra->pid, action_table[cur_action].action)) {
250 AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid, &old_gen));
251 mpm_callback(-1, cur_extra->pid, old_gen);
258 #if APR_HAS_OTHER_CHILD
259 apr_proc_other_child_refresh_all(APR_OC_REASON_RESTART);
262 } while (not_dead_yet > 0 &&
263 action_table[cur_action].action != GIVEUP);
266 void ap_relieve_child_processes(ap_reclaim_callback_fn_t *mpm_callback)
269 extra_process_t *cur_extra;
272 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
274 /* now see who is done */
275 for (i = 0; i < max_daemons; ++i) {
276 process_score *ps = ap_get_scoreboard_process(i);
280 continue; /* not every scoreboard entry is in use */
283 if (reclaim_one_pid(pid, DO_NOTHING)) {
284 mpm_callback(i, 0, 0);
290 ap_generation_t old_gen;
291 extra_process_t *next = cur_extra->next;
293 if (reclaim_one_pid(cur_extra->pid, DO_NOTHING)) {
294 AP_DEBUG_ASSERT(1 == ap_unregister_extra_mpm_process(cur_extra->pid, &old_gen));
295 mpm_callback(-1, cur_extra->pid, old_gen);
301 /* Before sending the signal to the pid this function verifies that
302 * the pid is a member of the current process group; either using
303 * apr_proc_wait(), where waitpid() guarantees to fail for non-child
304 * processes; or by using getpgid() directly, if available. */
305 apr_status_t ap_mpm_safe_kill(pid_t pid, int sig)
313 /* Ensure pid sanity */
319 rv = apr_proc_wait(&proc, &status, &why, APR_NOWAIT);
320 if (rv == APR_CHILD_DONE) {
321 /* The child already died - log the termination status if
323 ap_process_child_status(&proc, why, status);
326 else if (rv != APR_CHILD_NOTDONE) {
327 /* The child is already dead and reaped, or was a bogus pid -
328 * log this either way. */
329 ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, ap_server_conf,
330 "cannot send signal %d to pid %ld (non-child or "
331 "already dead)", sig, (long)pid);
337 /* Ensure pid sanity. */
344 /* Process already dead... */
348 if (pg != getpgrp()) {
349 ap_log_error(APLOG_MARK, APLOG_ALERT, 0, ap_server_conf,
350 "refusing to send signal %d to pid %ld outside "
351 "process group", sig, (long)pid);
356 return kill(pid, sig) ? errno : APR_SUCCESS;
360 int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
365 /* Child died... if it died due to a fatal error,
366 * we should simply bail out. The caller needs to
367 * check for bad rc from us and exit, running any
368 * appropriate cleanups.
370 * If the child died due to a resource shortage,
371 * the parent should limit the rate of forking
373 if (APR_PROC_CHECK_EXIT(why)) {
374 if (status == APEXIT_CHILDSICK) {
378 if (status == APEXIT_CHILDFATAL) {
379 ap_log_error(APLOG_MARK, APLOG_ALERT,
381 "Child %" APR_PID_T_FMT
382 " returned a Fatal error... Apache is exiting!",
384 return APEXIT_CHILDFATAL;
390 if (APR_PROC_CHECK_SIGNALED(why)) {
391 sigdesc = apr_signal_description_get(signum);
396 case AP_SIG_GRACEFUL:
401 if (APR_PROC_CHECK_CORE_DUMP(why)) {
402 ap_log_error(APLOG_MARK, APLOG_NOTICE,
404 "child pid %ld exit signal %s (%d), "
405 "possible coredump in %s",
406 (long)pid->pid, sigdesc, signum,
410 ap_log_error(APLOG_MARK, APLOG_NOTICE,
412 "child pid %ld exit signal %s (%d)",
413 (long)pid->pid, sigdesc, signum);
420 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
424 *pod = apr_palloc(p, sizeof(**pod));
425 rv = apr_file_pipe_create_ex(&((*pod)->pod_in), &((*pod)->pod_out),
427 if (rv != APR_SUCCESS) {
431 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
434 /* close these before exec. */
435 apr_file_inherit_unset((*pod)->pod_in);
436 apr_file_inherit_unset((*pod)->pod_out);
441 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
447 rv = apr_file_read(pod->pod_in, &c, &len);
449 if ((rv == APR_SUCCESS) && (len == 1)) {
453 if (rv != APR_SUCCESS) {
460 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
464 rv = apr_file_close(pod->pod_out);
465 if (rv != APR_SUCCESS) {
469 rv = apr_file_close(pod->pod_in);
470 if (rv != APR_SUCCESS) {
477 static apr_status_t pod_signal_internal(ap_pod_t *pod)
480 char char_of_death = '!';
483 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
484 if (rv != APR_SUCCESS) {
485 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
486 "write pipe_of_death");
492 /* This function connects to the server, then immediately closes the connection.
493 * This permits the MPM to skip the poll when there is only one listening
494 * socket, because it provides a alternate way to unblock an accept() when
497 static apr_status_t dummy_connection(ap_pod_t *pod)
506 /* create a temporary pool for the socket. pconf stays around too long */
507 rv = apr_pool_create(&p, pod->p);
508 if (rv != APR_SUCCESS) {
512 /* If possible, find a listener which is configured for
513 * plain-HTTP, not SSL; using an SSL port would either be
514 * expensive to do correctly (performing a complete SSL handshake)
515 * or cause log spam by doing incorrectly (simply sending EOF). */
517 while (lp && lp->protocol && strcasecmp(lp->protocol, "http") != 0) {
524 rv = apr_socket_create(&sock, lp->bind_addr->family, SOCK_STREAM, 0, p);
525 if (rv != APR_SUCCESS) {
526 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
527 "get socket to connect to listener");
532 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
533 * queued connections before it starts blocking local connects...
534 * we need to keep from blocking too long and instead return an error,
535 * because the MPM won't want to hold up a graceful restart for a
538 rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
539 if (rv != APR_SUCCESS) {
540 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
541 "set timeout on socket to connect to listener");
542 apr_socket_close(sock);
547 rv = apr_socket_connect(sock, lp->bind_addr);
548 if (rv != APR_SUCCESS) {
549 int log_level = APLOG_WARNING;
551 if (APR_STATUS_IS_TIMEUP(rv)) {
552 /* probably some server processes bailed out already and there
553 * is nobody around to call accept and clear out the kernel
554 * connection queue; usually this is not worth logging
556 log_level = APLOG_DEBUG;
559 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
560 "connect to listener on %pI", lp->bind_addr);
563 /* Create the request string. We include a User-Agent so that
564 * adminstrators can track down the cause of the odd-looking
565 * requests in their logs.
567 srequest = apr_pstrcat(p, "OPTIONS * HTTP/1.0\r\nUser-Agent: ",
568 ap_get_server_description(),
569 " (internal dummy connection)\r\n\r\n", NULL);
571 /* Since some operating systems support buffering of data or entire
572 * requests in the kernel, we send a simple request, to make sure
573 * the server pops out of a blocking accept().
575 /* XXX: This is HTTP specific. We should look at the Protocol for each
576 * listener, and send the correct type of request to trigger any Accept
579 len = strlen(srequest);
580 apr_socket_send(sock, srequest, &len);
581 apr_socket_close(sock);
587 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
591 rv = pod_signal_internal(pod);
592 if (rv != APR_SUCCESS) {
596 return dummy_connection(pod);
599 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
602 apr_status_t rv = APR_SUCCESS;
604 /* we don't write anything to the pod here... we assume
605 * that the would-be reader of the pod has another way to
606 * see that it is time to die once we wake it up
608 * writing lots of things to the pod at once is very
609 * problematic... we can fill the kernel pipe buffer and
610 * be blocked until somebody consumes some bytes or
611 * we hit a timeout... if we hit a timeout we can't just
612 * keep trying because maybe we'll never successfully
613 * write again... but then maybe we'll leave would-be
614 * readers stranded (a number of them could be tied up for
615 * a while serving time-consuming requests)
617 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
618 rv = dummy_connection(pod);
622 static const char *dash_k_arg = NULL;
623 static const char *dash_k_arg_noarg = "noarg";
625 static int send_signal(pid_t pid, int sig)
627 if (kill(pid, sig) < 0) {
628 ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL,
629 "sending signal to server");
635 int ap_signal_server(int *exit_status, apr_pool_t *pconf)
644 rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
645 if (rv != APR_SUCCESS) {
646 if (rv != APR_ENOENT) {
647 ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL,
648 "Error retrieving pid file %s", ap_pid_fname);
649 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL,
650 "Remove it before continuing if it is corrupted.");
654 status = "httpd (no pid file) not running";
657 if (kill(otherpid, 0) == 0) {
659 status = apr_psprintf(pconf,
660 "httpd (pid %" APR_PID_T_FMT ") already "
661 "running", otherpid);
664 status = apr_psprintf(pconf,
665 "httpd (pid %" APR_PID_T_FMT "?) not running",
670 if (!strcmp(dash_k_arg, "start") || dash_k_arg == dash_k_arg_noarg) {
672 printf("%s\n", status);
677 if (!strcmp(dash_k_arg, "stop")) {
679 printf("%s\n", status);
682 send_signal(otherpid, SIGTERM);
687 if (!strcmp(dash_k_arg, "restart")) {
689 printf("httpd not running, trying to start\n");
692 *exit_status = send_signal(otherpid, SIGHUP);
697 if (!strcmp(dash_k_arg, "graceful")) {
699 printf("httpd not running, trying to start\n");
702 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL);
707 if (!strcmp(dash_k_arg, "graceful-stop")) {
709 printf("%s\n", status);
712 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL_STOP);
720 void ap_mpm_rewrite_args(process_rec *process)
722 apr_array_header_t *mpm_new_argv;
728 mpm_new_argv = apr_array_make(process->pool, process->argc,
729 sizeof(const char **));
730 *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
731 apr_getopt_init(&opt, process->pool, process->argc, process->argv);
734 /* option char returned by apr_getopt() will be stored in optbuf[1] */
736 while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
737 optbuf + 1, &optarg)) == APR_SUCCESS) {
741 if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
742 !strcmp(optarg, "restart") || !strcmp(optarg, "graceful") ||
743 !strcmp(optarg, "graceful-stop")) {
749 *(const char **)apr_array_push(mpm_new_argv) =
750 apr_pstrdup(process->pool, optbuf);
752 *(const char **)apr_array_push(mpm_new_argv) = optarg;
757 /* back up to capture the bad argument */
758 if (rv == APR_BADCH || rv == APR_BADARG) {
762 while (opt->ind < opt->argc) {
763 *(const char **)apr_array_push(mpm_new_argv) =
764 apr_pstrdup(process->pool, opt->argv[opt->ind++]);
767 process->argc = mpm_new_argv->nelts;
768 process->argv = (const char * const *)mpm_new_argv->elts;
770 if (NULL == dash_k_arg) {
771 dash_k_arg = dash_k_arg_noarg;
774 APR_REGISTER_OPTIONAL_FN(ap_signal_server);
777 static pid_t parent_pid, my_pid;
778 static apr_pool_t *pconf;
780 #if AP_ENABLE_EXCEPTION_HOOK
782 static int exception_hook_enabled;
784 const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
787 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
792 if (cmd->server->is_virtual) {
793 return "EnableExceptionHook directive not allowed in <VirtualHost>";
796 if (strcasecmp(arg, "on") == 0) {
797 exception_hook_enabled = 1;
799 else if (strcasecmp(arg, "off") == 0) {
800 exception_hook_enabled = 0;
803 return "parameter must be 'on' or 'off'";
809 static void run_fatal_exception_hook(int sig)
811 ap_exception_info_t ei = {0};
813 if (exception_hook_enabled &&
815 my_pid != parent_pid) {
818 ap_run_fatal_exception(&ei);
821 #endif /* AP_ENABLE_EXCEPTION_HOOK */
823 /* handle all varieties of core dumping signals */
824 static void sig_coredump(int sig)
826 apr_filepath_set(ap_coredump_dir, pconf);
827 apr_signal(sig, SIG_DFL);
828 #if AP_ENABLE_EXCEPTION_HOOK
829 run_fatal_exception_hook(sig);
831 /* linuxthreads issue calling getpid() here:
832 * This comparison won't match if the crashing thread is
833 * some module's thread that runs in the parent process.
834 * The fallout, which is limited to linuxthreads:
835 * The special log message won't be written when such a
836 * thread in the parent causes the parent to crash.
838 if (getpid() == parent_pid) {
839 ap_log_error(APLOG_MARK, APLOG_NOTICE,
841 "seg fault or similar nasty error detected "
842 "in the parent process");
843 /* XXX we can probably add some rudimentary cleanup code here,
844 * like getting rid of the pid file. If any additional bad stuff
845 * happens, we are protected from recursive errors taking down the
846 * system since this function is no longer the signal handler GLA
850 /* At this point we've got sig blocked, because we're still inside
851 * the signal handler. When we leave the signal handler it will
852 * be unblocked, and we'll take the signal... and coredump or whatever
853 * is appropriate for this particular Unix. In addition the parent
854 * will see the real signal we received -- whereas if we called
855 * abort() here, the parent would only see SIGABRT.
859 apr_status_t ap_fatal_signal_child_setup(server_rec *s)
865 apr_status_t ap_fatal_signal_setup(server_rec *s, apr_pool_t *in_pconf)
867 #ifndef NO_USE_SIGACTION
870 sigemptyset(&sa.sa_mask);
872 #if defined(SA_ONESHOT)
873 sa.sa_flags = SA_ONESHOT;
874 #elif defined(SA_RESETHAND)
875 sa.sa_flags = SA_RESETHAND;
880 sa.sa_handler = sig_coredump;
881 if (sigaction(SIGSEGV, &sa, NULL) < 0)
882 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGSEGV)");
884 if (sigaction(SIGBUS, &sa, NULL) < 0)
885 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGBUS)");
888 if (sigaction(SIGABORT, &sa, NULL) < 0)
889 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABORT)");
892 if (sigaction(SIGABRT, &sa, NULL) < 0)
893 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGABRT)");
896 if (sigaction(SIGILL, &sa, NULL) < 0)
897 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGILL)");
900 if (sigaction(SIGFPE, &sa, NULL) < 0)
901 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, "sigaction(SIGFPE)");
904 #else /* NO_USE_SIGACTION */
906 apr_signal(SIGSEGV, sig_coredump);
908 apr_signal(SIGBUS, sig_coredump);
911 apr_signal(SIGABORT, sig_coredump);
912 #endif /* SIGABORT */
914 apr_signal(SIGABRT, sig_coredump);
917 apr_signal(SIGILL, sig_coredump);
920 apr_signal(SIGFPE, sig_coredump);
923 #endif /* NO_USE_SIGACTION */
926 parent_pid = my_pid = getpid();