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"
42 #include "http_core.h"
44 #include "http_main.h"
45 #include "mpm_common.h"
47 #include "ap_listen.h"
48 #include "scoreboard.h"
49 #include "util_mutex.h"
62 /* we know core's module_index is 0 */
63 #undef APLOG_MODULE_INDEX
64 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
66 typedef enum {DO_NOTHING, SEND_SIGTERM, SEND_SIGKILL, GIVEUP} action_t;
68 typedef struct extra_process_t {
69 struct extra_process_t *next;
74 static extra_process_t *extras;
76 AP_DECLARE(void) ap_register_extra_mpm_process(pid_t pid, ap_generation_t gen)
78 extra_process_t *p = (extra_process_t *)ap_malloc(sizeof(extra_process_t));
86 AP_DECLARE(int) ap_unregister_extra_mpm_process(pid_t pid, ap_generation_t *old_gen)
88 extra_process_t *cur = extras;
89 extra_process_t *prev = NULL;
91 while (cur && cur->pid != pid) {
98 prev->next = cur->next;
105 return 1; /* found */
108 /* we don't know about any such process */
113 static int reclaim_one_pid(pid_t pid, action_t action)
116 apr_status_t waitret;
120 /* Ensure pid sanity. */
126 waitret = apr_proc_wait(&proc, &status, &why, APR_NOWAIT);
127 if (waitret != APR_CHILD_NOTDONE) {
128 if (waitret == APR_CHILD_DONE)
129 ap_process_child_status(&proc, why, status);
138 /* ok, now it's being annoying */
139 ap_log_error(APLOG_MARK, APLOG_WARNING,
140 0, ap_server_conf, APLOGNO(00045)
141 "child process %" APR_PID_T_FMT
142 " still did not exit, "
149 ap_log_error(APLOG_MARK, APLOG_ERR,
150 0, ap_server_conf, APLOGNO(00046)
151 "child process %" APR_PID_T_FMT
152 " still did not exit, "
159 /* gave it our best shot, but alas... If this really
160 * is a child we are trying to kill and it really hasn't
161 * exited, we will likely fail to bind to the port
164 ap_log_error(APLOG_MARK, APLOG_ERR,
165 0, ap_server_conf, APLOGNO(00047)
166 "could not make child process %" APR_PID_T_FMT
168 "attempting to continue anyway",
176 AP_DECLARE(void) ap_reclaim_child_processes(int terminate,
177 ap_reclaim_callback_fn_t *mpm_callback)
179 apr_time_t waittime = 1024 * 16;
181 extra_process_t *cur_extra;
184 apr_time_t starttime = apr_time_now();
185 /* this table of actions and elapsed times tells what action is taken
186 * at which elapsed time from starting the reclaim
190 apr_time_t action_time;
192 {DO_NOTHING, 0}, /* dummy entry for iterations where we reap
193 * children but take no action against
196 {SEND_SIGTERM, apr_time_from_sec(3)},
197 {SEND_SIGTERM, apr_time_from_sec(5)},
198 {SEND_SIGTERM, apr_time_from_sec(7)},
199 {SEND_SIGKILL, apr_time_from_sec(9)},
200 {GIVEUP, apr_time_from_sec(10)}
202 int cur_action; /* index of action we decided to take this
205 int next_action = 1; /* index of first real action */
207 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
211 /* don't let waittime get longer than 1 second; otherwise, we don't
212 * react quickly to the last child exiting, and taking action can
215 waittime = waittime * 4;
216 if (waittime > apr_time_from_sec(1)) {
217 waittime = apr_time_from_sec(1);
220 /* see what action to take, if any */
221 if (action_table[next_action].action_time <= apr_time_now() - starttime) {
222 cur_action = next_action;
226 cur_action = 0; /* nothing to do */
229 /* now see who is done */
231 for (i = 0; i < max_daemons; ++i) {
232 process_score *ps = ap_get_scoreboard_process(i);
236 continue; /* not every scoreboard entry is in use */
239 if (reclaim_one_pid(pid, action_table[cur_action].action)) {
240 mpm_callback(i, 0, 0);
249 ap_generation_t old_gen;
250 extra_process_t *next = cur_extra->next;
252 if (reclaim_one_pid(cur_extra->pid, action_table[cur_action].action)) {
253 if (ap_unregister_extra_mpm_process(cur_extra->pid, &old_gen) == 1) {
254 mpm_callback(-1, cur_extra->pid, old_gen);
257 AP_DEBUG_ASSERT(1 == 0);
265 #if APR_HAS_OTHER_CHILD
266 apr_proc_other_child_refresh_all(APR_OC_REASON_RESTART);
269 } while (not_dead_yet > 0 &&
270 action_table[cur_action].action != GIVEUP);
273 AP_DECLARE(void) ap_relieve_child_processes(ap_reclaim_callback_fn_t *mpm_callback)
276 extra_process_t *cur_extra;
279 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
281 /* now see who is done */
282 for (i = 0; i < max_daemons; ++i) {
283 process_score *ps = ap_get_scoreboard_process(i);
287 continue; /* not every scoreboard entry is in use */
290 if (reclaim_one_pid(pid, DO_NOTHING)) {
291 mpm_callback(i, 0, 0);
297 ap_generation_t old_gen;
298 extra_process_t *next = cur_extra->next;
300 if (reclaim_one_pid(cur_extra->pid, DO_NOTHING)) {
301 if (ap_unregister_extra_mpm_process(cur_extra->pid, &old_gen) == 1) {
302 mpm_callback(-1, cur_extra->pid, old_gen);
305 AP_DEBUG_ASSERT(1 == 0);
312 /* Before sending the signal to the pid this function verifies that
313 * the pid is a member of the current process group; either using
314 * apr_proc_wait(), where waitpid() guarantees to fail for non-child
315 * processes; or by using getpgid() directly, if available. */
316 AP_DECLARE(apr_status_t) ap_mpm_safe_kill(pid_t pid, int sig)
324 /* Ensure pid sanity */
330 rv = apr_proc_wait(&proc, &status, &why, APR_NOWAIT);
331 if (rv == APR_CHILD_DONE) {
332 /* The child already died - log the termination status if
334 ap_process_child_status(&proc, why, status);
337 else if (rv != APR_CHILD_NOTDONE) {
338 /* The child is already dead and reaped, or was a bogus pid -
339 * log this either way. */
340 ap_log_error(APLOG_MARK, APLOG_NOTICE, rv, ap_server_conf, APLOGNO(00048)
341 "cannot send signal %d to pid %ld (non-child or "
342 "already dead)", sig, (long)pid);
348 /* Ensure pid sanity. */
355 /* Process already dead... */
359 if (pg != getpgrp()) {
360 ap_log_error(APLOG_MARK, APLOG_ALERT, 0, ap_server_conf, APLOGNO(00049)
361 "refusing to send signal %d to pid %ld outside "
362 "process group", sig, (long)pid);
367 return kill(pid, sig) ? errno : APR_SUCCESS;
371 AP_DECLARE(int) ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why,
377 /* Child died... if it died due to a fatal error,
378 * we should simply bail out. The caller needs to
379 * check for bad rc from us and exit, running any
380 * appropriate cleanups.
382 * If the child died due to a resource shortage,
383 * the parent should limit the rate of forking
385 if (APR_PROC_CHECK_EXIT(why)) {
386 if (status == APEXIT_CHILDSICK) {
390 if (status == APEXIT_CHILDFATAL) {
391 ap_log_error(APLOG_MARK, APLOG_ALERT,
392 0, ap_server_conf, APLOGNO(00050)
393 "Child %" APR_PID_T_FMT
394 " returned a Fatal error... Apache is exiting!",
396 return APEXIT_CHILDFATAL;
402 if (APR_PROC_CHECK_SIGNALED(why)) {
403 sigdesc = apr_signal_description_get(signum);
408 case AP_SIG_GRACEFUL:
413 if (APR_PROC_CHECK_CORE_DUMP(why)) {
414 ap_log_error(APLOG_MARK, APLOG_NOTICE,
415 0, ap_server_conf, APLOGNO(00051)
416 "child pid %ld exit signal %s (%d), "
417 "possible coredump in %s",
418 (long)pid->pid, sigdesc, signum,
422 ap_log_error(APLOG_MARK, APLOG_NOTICE,
423 0, ap_server_conf, APLOGNO(00052)
424 "child pid %ld exit signal %s (%d)",
425 (long)pid->pid, sigdesc, signum);
432 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
436 *pod = apr_palloc(p, sizeof(**pod));
437 rv = apr_file_pipe_create_ex(&((*pod)->pod_in), &((*pod)->pod_out),
439 if (rv != APR_SUCCESS) {
443 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
446 /* close these before exec. */
447 apr_file_inherit_unset((*pod)->pod_in);
448 apr_file_inherit_unset((*pod)->pod_out);
453 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
459 rv = apr_file_read(pod->pod_in, &c, &len);
461 if ((rv == APR_SUCCESS) && (len == 1)) {
465 if (rv != APR_SUCCESS) {
472 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
476 rv = apr_file_close(pod->pod_out);
477 if (rv != APR_SUCCESS) {
481 rv = apr_file_close(pod->pod_in);
482 if (rv != APR_SUCCESS) {
489 static apr_status_t pod_signal_internal(ap_pod_t *pod)
492 char char_of_death = '!';
495 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
496 if (rv != APR_SUCCESS) {
497 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf, APLOGNO(00053)
498 "write pipe_of_death");
504 /* This function connects to the server, then immediately closes the connection.
505 * This permits the MPM to skip the poll when there is only one listening
506 * socket, because it provides a alternate way to unblock an accept() when
509 static apr_status_t dummy_connection(ap_pod_t *pod)
518 /* create a temporary pool for the socket. pconf stays around too long */
519 rv = apr_pool_create(&p, pod->p);
520 if (rv != APR_SUCCESS) {
524 /* If possible, find a listener which is configured for
525 * plain-HTTP, not SSL; using an SSL port would either be
526 * expensive to do correctly (performing a complete SSL handshake)
527 * or cause log spam by doing incorrectly (simply sending EOF). */
529 while (lp && lp->protocol && strcasecmp(lp->protocol, "http") != 0) {
536 rv = apr_socket_create(&sock, lp->bind_addr->family, SOCK_STREAM, 0, p);
537 if (rv != APR_SUCCESS) {
538 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf, APLOGNO(00054)
539 "get socket to connect to listener");
544 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
545 * queued connections before it starts blocking local connects...
546 * we need to keep from blocking too long and instead return an error,
547 * because the MPM won't want to hold up a graceful restart for a
550 rv = apr_socket_timeout_set(sock, apr_time_from_sec(3));
551 if (rv != APR_SUCCESS) {
552 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf, APLOGNO(00055)
553 "set timeout on socket to connect to listener");
554 apr_socket_close(sock);
559 rv = apr_socket_connect(sock, lp->bind_addr);
560 if (rv != APR_SUCCESS) {
561 int log_level = APLOG_WARNING;
563 if (APR_STATUS_IS_TIMEUP(rv)) {
564 /* probably some server processes bailed out already and there
565 * is nobody around to call accept and clear out the kernel
566 * connection queue; usually this is not worth logging
568 log_level = APLOG_DEBUG;
571 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf, APLOGNO(00056)
572 "connect to listener on %pI", lp->bind_addr);
577 /* Create the request string. We include a User-Agent so that
578 * adminstrators can track down the cause of the odd-looking
579 * requests in their logs.
581 srequest = apr_pstrcat(p, "OPTIONS * HTTP/1.0\r\nUser-Agent: ",
582 ap_get_server_description(),
583 " (internal dummy connection)\r\n\r\n", NULL);
585 /* Since some operating systems support buffering of data or entire
586 * requests in the kernel, we send a simple request, to make sure
587 * the server pops out of a blocking accept().
589 /* XXX: This is HTTP specific. We should look at the Protocol for each
590 * listener, and send the correct type of request to trigger any Accept
593 len = strlen(srequest);
594 apr_socket_send(sock, srequest, &len);
595 apr_socket_close(sock);
601 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
605 rv = pod_signal_internal(pod);
606 if (rv != APR_SUCCESS) {
610 return dummy_connection(pod);
613 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
616 apr_status_t rv = APR_SUCCESS;
618 /* we don't write anything to the pod here... we assume
619 * that the would-be reader of the pod has another way to
620 * see that it is time to die once we wake it up
622 * writing lots of things to the pod at once is very
623 * problematic... we can fill the kernel pipe buffer and
624 * be blocked until somebody consumes some bytes or
625 * we hit a timeout... if we hit a timeout we can't just
626 * keep trying because maybe we'll never successfully
627 * write again... but then maybe we'll leave would-be
628 * readers stranded (a number of them could be tied up for
629 * a while serving time-consuming requests)
631 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
632 rv = dummy_connection(pod);
636 static const char *dash_k_arg = NULL;
637 static const char *dash_k_arg_noarg = "noarg";
639 static int send_signal(pid_t pid, int sig)
641 if (kill(pid, sig) < 0) {
642 ap_log_error(APLOG_MARK, APLOG_STARTUP, errno, NULL, APLOGNO(00057)
643 "sending signal to server");
649 int ap_signal_server(int *exit_status, apr_pool_t *pconf)
658 rv = ap_read_pid(pconf, ap_pid_fname, &otherpid);
659 if (rv != APR_SUCCESS) {
660 if (!APR_STATUS_IS_ENOENT(rv)) {
661 ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, NULL, APLOGNO(00058)
662 "Error retrieving pid file %s", ap_pid_fname);
663 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, APLOGNO(00059)
664 "Remove it before continuing if it is corrupted.");
668 status = "httpd (no pid file) not running";
671 if (kill(otherpid, 0) == 0) {
673 status = apr_psprintf(pconf,
674 "httpd (pid %" APR_PID_T_FMT ") already "
675 "running", otherpid);
678 status = apr_psprintf(pconf,
679 "httpd (pid %" APR_PID_T_FMT "?) not running",
684 if (!strcmp(dash_k_arg, "start") || dash_k_arg == dash_k_arg_noarg) {
686 printf("%s\n", status);
691 if (!strcmp(dash_k_arg, "stop")) {
693 printf("%s\n", status);
696 send_signal(otherpid, SIGTERM);
701 if (!strcmp(dash_k_arg, "restart")) {
703 printf("httpd not running, trying to start\n");
706 *exit_status = send_signal(otherpid, SIGHUP);
711 if (!strcmp(dash_k_arg, "graceful")) {
713 printf("httpd not running, trying to start\n");
716 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL);
721 if (!strcmp(dash_k_arg, "graceful-stop")) {
723 printf("%s\n", status);
726 *exit_status = send_signal(otherpid, AP_SIG_GRACEFUL_STOP);
734 void ap_mpm_rewrite_args(process_rec *process)
736 apr_array_header_t *mpm_new_argv;
742 mpm_new_argv = apr_array_make(process->pool, process->argc,
743 sizeof(const char **));
744 *(const char **)apr_array_push(mpm_new_argv) = process->argv[0];
745 apr_getopt_init(&opt, process->pool, process->argc, process->argv);
748 /* option char returned by apr_getopt() will be stored in optbuf[1] */
750 while ((rv = apr_getopt(opt, "k:" AP_SERVER_BASEARGS,
751 optbuf + 1, &optarg)) == APR_SUCCESS) {
755 if (!strcmp(optarg, "start") || !strcmp(optarg, "stop") ||
756 !strcmp(optarg, "restart") || !strcmp(optarg, "graceful") ||
757 !strcmp(optarg, "graceful-stop")) {
763 *(const char **)apr_array_push(mpm_new_argv) =
764 apr_pstrdup(process->pool, optbuf);
766 *(const char **)apr_array_push(mpm_new_argv) = optarg;
771 /* back up to capture the bad argument */
772 if (rv == APR_BADCH || rv == APR_BADARG) {
776 while (opt->ind < opt->argc) {
777 *(const char **)apr_array_push(mpm_new_argv) =
778 apr_pstrdup(process->pool, opt->argv[opt->ind++]);
781 process->argc = mpm_new_argv->nelts;
782 process->argv = (const char * const *)mpm_new_argv->elts;
784 if (NULL == dash_k_arg) {
785 dash_k_arg = dash_k_arg_noarg;
788 APR_REGISTER_OPTIONAL_FN(ap_signal_server);
791 static pid_t parent_pid, my_pid;
792 static apr_pool_t *pconf;
794 #if AP_ENABLE_EXCEPTION_HOOK
796 static int exception_hook_enabled;
798 const char *ap_mpm_set_exception_hook(cmd_parms *cmd, void *dummy,
801 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
806 if (cmd->server->is_virtual) {
807 return "EnableExceptionHook directive not allowed in <VirtualHost>";
810 if (strcasecmp(arg, "on") == 0) {
811 exception_hook_enabled = 1;
813 else if (strcasecmp(arg, "off") == 0) {
814 exception_hook_enabled = 0;
817 return "parameter must be 'on' or 'off'";
823 static void run_fatal_exception_hook(int sig)
825 ap_exception_info_t ei = {0};
827 if (exception_hook_enabled &&
829 my_pid != parent_pid) {
832 ap_run_fatal_exception(&ei);
835 #endif /* AP_ENABLE_EXCEPTION_HOOK */
837 /* handle all varieties of core dumping signals */
838 static void sig_coredump(int sig)
840 apr_filepath_set(ap_coredump_dir, pconf);
841 apr_signal(sig, SIG_DFL);
842 #if AP_ENABLE_EXCEPTION_HOOK
843 run_fatal_exception_hook(sig);
845 /* linuxthreads issue calling getpid() here:
846 * This comparison won't match if the crashing thread is
847 * some module's thread that runs in the parent process.
848 * The fallout, which is limited to linuxthreads:
849 * The special log message won't be written when such a
850 * thread in the parent causes the parent to crash.
852 if (getpid() == parent_pid) {
853 ap_log_error(APLOG_MARK, APLOG_NOTICE,
854 0, ap_server_conf, APLOGNO(00060)
855 "seg fault or similar nasty error detected "
856 "in the parent process");
857 /* XXX we can probably add some rudimentary cleanup code here,
858 * like getting rid of the pid file. If any additional bad stuff
859 * happens, we are protected from recursive errors taking down the
860 * system since this function is no longer the signal handler GLA
864 /* At this point we've got sig blocked, because we're still inside
865 * the signal handler. When we leave the signal handler it will
866 * be unblocked, and we'll take the signal... and coredump or whatever
867 * is appropriate for this particular Unix. In addition the parent
868 * will see the real signal we received -- whereas if we called
869 * abort() here, the parent would only see SIGABRT.
873 AP_DECLARE(apr_status_t) ap_fatal_signal_child_setup(server_rec *s)
879 AP_DECLARE(apr_status_t) ap_fatal_signal_setup(server_rec *s,
880 apr_pool_t *in_pconf)
882 #ifndef NO_USE_SIGACTION
885 sigemptyset(&sa.sa_mask);
887 #if defined(SA_ONESHOT)
888 sa.sa_flags = SA_ONESHOT;
889 #elif defined(SA_RESETHAND)
890 sa.sa_flags = SA_RESETHAND;
895 sa.sa_handler = sig_coredump;
896 if (sigaction(SIGSEGV, &sa, NULL) < 0)
897 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, APLOGNO(00061) "sigaction(SIGSEGV)");
899 if (sigaction(SIGBUS, &sa, NULL) < 0)
900 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, APLOGNO(00062) "sigaction(SIGBUS)");
903 if (sigaction(SIGABORT, &sa, NULL) < 0)
904 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, APLOGNO(00063) "sigaction(SIGABORT)");
907 if (sigaction(SIGABRT, &sa, NULL) < 0)
908 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, APLOGNO(00064) "sigaction(SIGABRT)");
911 if (sigaction(SIGILL, &sa, NULL) < 0)
912 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, APLOGNO(00065) "sigaction(SIGILL)");
915 if (sigaction(SIGFPE, &sa, NULL) < 0)
916 ap_log_error(APLOG_MARK, APLOG_WARNING, errno, s, APLOGNO(00066) "sigaction(SIGFPE)");
919 #else /* NO_USE_SIGACTION */
921 apr_signal(SIGSEGV, sig_coredump);
923 apr_signal(SIGBUS, sig_coredump);
926 apr_signal(SIGABORT, sig_coredump);
927 #endif /* SIGABORT */
929 apr_signal(SIGABRT, sig_coredump);
932 apr_signal(SIGILL, sig_coredump);
935 apr_signal(SIGFPE, sig_coredump);
938 #endif /* NO_USE_SIGACTION */
941 parent_pid = my_pid = getpid();