1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
59 /* The purpose of this file is to store the code that MOST mpm's will need
60 * this does not mean a function only goes into this file if every MPM needs
61 * it. It means that if a function is needed by more than one MPM, and
62 * future maintenance would be served by making the code common, then the
63 * function belongs here.
65 * This is going in src/main because it is not platform specific, it is
66 * specific to multi-process servers, but NOT to Unix. Which is why it
67 * does not belong in src/os/unix
71 #include "apr_thread_proc.h"
72 #include "apr_signal.h"
73 #include "apr_strings.h"
75 #define APR_WANT_STRFUNC
79 #include "http_config.h"
81 #include "http_main.h"
83 #include "mpm_common.h"
85 #include "ap_listen.h"
87 #ifdef AP_MPM_WANT_SET_SCOREBOARD
88 #include "scoreboard.h"
98 #ifdef AP_MPM_WANT_RECLAIM_CHILD_PROCESSES
99 void ap_reclaim_child_processes(int terminate)
102 long int waittime = 1024 * 16; /* in usecs */
103 apr_status_t waitret;
108 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
109 MPM_SYNC_CHILD_TABLE();
111 for (tries = terminate ? 4 : 1; tries <= 9; ++tries) {
112 /* don't want to hold up progress any more than
113 * necessary, but we need to allow children a few moments to exit.
114 * Set delay with an exponential backoff.
116 waittime = waittime * 4;
119 /* now see who is done */
121 for (i = 0; i < max_daemons; ++i) {
122 pid_t pid = MPM_CHILD_PID(i);
129 waitret = apr_proc_wait(&proc, NULL, NULL, APR_NOWAIT);
130 if (waitret != APR_CHILD_NOTDONE) {
131 MPM_NOTE_CHILD_KILLED(i);
144 /* ok, now it's being annoying */
145 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
147 "child process %ld still did not exit, sending a SIGTERM",
153 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
155 "child process %ld still did not exit, sending a SIGKILL",
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.
170 /* gave it our best shot, but alas... If this really
171 * is a child we are trying to kill and it really hasn't
172 * exited, we will likely fail to bind to the port
175 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
177 "could not make child process %ld exit, "
178 "attempting to continue anyway", (long)pid);
182 #if APR_HAS_OTHER_CHILD
183 apr_proc_other_child_check();
186 /* nothing left to wait for */
191 #endif /* AP_MPM_WANT_RECLAIM_CHILD_PROCESSES */
193 #ifdef AP_MPM_WANT_WAIT_OR_TIMEOUT
195 /* number of calls to wait_or_timeout between writable probes */
196 #ifndef INTERVAL_OF_WRITABLE_PROBES
197 #define INTERVAL_OF_WRITABLE_PROBES 10
199 static int wait_or_timeout_counter;
201 void ap_wait_or_timeout(apr_exit_why_e *status, int *exitcode, apr_proc_t *ret,
206 ++wait_or_timeout_counter;
207 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
208 wait_or_timeout_counter = 0;
210 rv = apr_proc_wait_all_procs(ret, exitcode, status, APR_NOWAIT, p);
211 if (APR_STATUS_IS_EINTR(rv)) {
215 if (APR_STATUS_IS_CHILD_DONE(rv)) {
219 if ((ret = reap_children(exitcode, status)) > 0) {
223 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
227 #endif /* AP_MPM_WANT_WAIT_OR_TIMEOUT */
229 #ifdef AP_MPM_WANT_PROCESS_CHILD_STATUS
230 int ap_process_child_status(apr_proc_t *pid, apr_exit_why_e why, int status)
233 const char *sigdesc = apr_signal_get_description(signum);
235 /* Child died... if it died due to a fatal error,
236 * we should simply bail out. The caller needs to
237 * check for bad rc from us and exit, running any
238 * appropriate cleanups.
240 if ((APR_PROC_CHECK_EXIT(why)) &&
241 (status == APEXIT_CHILDFATAL)) {
242 ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, ap_server_conf,
243 "Child %" APR_OS_PROC_T_FMT
244 " returned a Fatal error..." APR_EOL_STR
245 "Apache is exiting!",
247 return APEXIT_CHILDFATAL;
250 if (APR_PROC_CHECK_SIGNALED(why)) {
254 case AP_SIG_GRACEFUL:
258 if (APR_PROC_CHECK_CORE_DUMP(why)) {
259 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
261 "child pid %ld exit signal %s (%d), "
262 "possible coredump in %s",
263 (long)pid->pid, sigdesc, signum,
267 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
269 "child pid %ld exit signal %s (%d)",
270 (long)pid->pid, sigdesc, signum);
276 #endif /* AP_MPM_WANT_PROCESS_CHILD_STATUS */
278 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF) && !defined(WIN32)
279 void ap_sock_disable_nagle(apr_socket_t *s)
281 /* The Nagle algorithm says that we should delay sending partial
282 * packets in hopes of getting more data. We don't want to do
283 * this; we are not telnet. There are bad interactions between
284 * persistent connections and Nagle's algorithm that have very severe
285 * performance penalties. (Failing to disable Nagle is not much of a
286 * problem with simple HTTP.)
288 * In spite of these problems, failure here is not a shooting offense.
290 apr_status_t status = apr_setsocketopt(s, APR_TCP_NODELAY, 1);
292 if (status != APR_SUCCESS) {
293 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
294 "setsockopt: (TCP_NODELAY)");
300 AP_DECLARE(uid_t) ap_uname2id(const char *name)
305 return (atoi(&name[1]));
307 if (!(ent = getpwnam(name))) {
308 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad user name %s", ap_server_argv0, name);
311 return (ent->pw_uid);
316 AP_DECLARE(gid_t) ap_gname2id(const char *name)
321 return (atoi(&name[1]));
323 if (!(ent = getgrnam(name))) {
324 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad group name %s", ap_server_argv0, name); exit(1);
326 return (ent->gr_gid);
330 #ifndef HAVE_INITGROUPS
331 int initgroups(const char *name, gid_t basegid)
333 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32) || defined(NETWARE)
334 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
337 gid_t groups[NGROUPS_MAX];
343 groups[index++] = basegid;
345 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
346 if (g->gr_gid != basegid) {
349 for (names = g->gr_mem; *names != NULL; ++names)
350 if (!strcmp(*names, name))
351 groups[index++] = g->gr_gid;
356 return setgroups(index, groups);
359 #endif /* def NEED_INITGROUPS */
361 #ifdef AP_MPM_USES_POD
363 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
367 *pod = apr_palloc(p, sizeof(**pod));
368 rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
369 if (rv != APR_SUCCESS) {
372 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
375 apr_sockaddr_info_get(&(*pod)->sa, ap_listeners->bind_addr->hostname,
376 APR_UNSPEC, ap_listeners->bind_addr->port, 0, p);
381 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
387 rv = apr_file_read(pod->pod_in, &c, &len);
389 if ((rv == APR_SUCCESS) && (len == 1)) {
392 if (rv != APR_SUCCESS) {
398 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
402 rv = apr_file_close(pod->pod_out);
403 if (rv != APR_SUCCESS) {
407 rv = apr_file_close(pod->pod_in);
408 if (rv != APR_SUCCESS) {
414 static apr_status_t pod_signal_internal(ap_pod_t *pod)
417 char char_of_death = '!';
421 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
422 } while (APR_STATUS_IS_EINTR(rv));
423 if (rv != APR_SUCCESS) {
424 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
425 "write pipe_of_death");
430 /* This function connects to the server, then immediately closes the connection.
431 * This permits the MPM to skip the poll when there is only one listening
432 * socket, because it provides a alternate way to unblock an accept() when
436 static apr_status_t dummy_connection(ap_pod_t *pod)
442 /* create a temporary pool for the socket. pconf stays around too long */
443 rv = apr_pool_create(&p, pod->p);
444 if (rv != APR_SUCCESS) {
448 rv = apr_socket_create(&sock, pod->sa->family, SOCK_STREAM, p);
449 if (rv != APR_SUCCESS) {
450 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
451 "get socket to connect to listener");
454 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
455 * queued connections before it starts blocking local connects...
456 * we need to keep from blocking too long and instead return an error,
457 * because the MPM won't want to hold up a graceful restart for a
460 rv = apr_setsocketopt(sock, APR_SO_TIMEOUT, 3 * APR_USEC_PER_SEC);
461 if (rv != APR_SUCCESS) {
462 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
463 "set timeout on socket to connect to listener");
467 rv = apr_connect(sock, pod->sa);
468 if (rv != APR_SUCCESS) {
469 int log_level = APLOG_WARNING;
471 if (APR_STATUS_IS_TIMEUP(rv)) {
472 /* probably some server processes bailed out already and there
473 * is nobody around to call accept and clear out the kernel
474 * connection queue; usually this is not worth logging
476 log_level = APLOG_DEBUG;
479 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
480 "connect to listener");
483 apr_socket_close(sock);
489 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
493 rv = pod_signal_internal(pod);
494 if (rv != APR_SUCCESS) {
497 return dummy_connection(pod);
500 void ap_mpm_pod_killpg(ap_pod_t *pod, int num)
503 apr_status_t rv = APR_SUCCESS;
505 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
506 rv = pod_signal_internal(pod);
508 if (rv == APR_SUCCESS) {
509 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
510 rv = dummy_connection(pod);
514 #endif /* #ifdef AP_MPM_USES_POD */
516 /* standard mpm configuration handling */
517 #ifdef AP_MPM_WANT_SET_PIDFILE
518 const char *ap_pid_fname = NULL;
520 const char *ap_mpm_set_pidfile(cmd_parms *cmd, void *dummy,
523 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
528 if (cmd->server->is_virtual) {
529 return "PidFile directive not allowed in <VirtualHost>";
536 #ifdef AP_MPM_WANT_SET_SCOREBOARD
537 AP_DECLARE(const char *) ap_mpm_set_scoreboard(cmd_parms *cmd, void *dummy,
540 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
545 ap_scoreboard_fname = arg;
550 #ifdef AP_MPM_WANT_SET_LOCKFILE
551 const char *ap_lock_fname = NULL;
552 AP_DECLARE(const char *) ap_mpm_set_lockfile(cmd_parms *cmd, void *dummy,
555 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
565 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
566 int ap_max_requests_per_child = 0;
567 const char *ap_mpm_set_max_requests(cmd_parms *cmd, void *dummy,
570 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
575 ap_max_requests_per_child = atoi(arg);
581 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
582 char ap_coredump_dir[MAX_STRING_LEN];
583 const char *ap_mpm_set_coredumpdir(cmd_parms *cmd, void *dummy,
588 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
593 fname = ap_server_root_relative(cmd->pool, arg);
594 if ((apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool) != APR_SUCCESS)
595 || (finfo.filetype != APR_DIR)) {
596 return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname,
597 " does not exist or is not a directory", NULL);
599 apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir));
604 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
605 apr_lockmech_e_np ap_accept_lock_mech = APR_LOCK_DEFAULT;
606 AP_DECLARE(const char *) ap_mpm_set_accept_lock_mech(cmd_parms *cmd,
610 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
615 if (!strcasecmp(arg, "default")) {
616 ap_accept_lock_mech = APR_LOCK_DEFAULT;
618 #if APR_HAS_FLOCK_SERIALIZE
619 else if (!strcasecmp(arg, "flock")) {
620 ap_accept_lock_mech = APR_LOCK_FLOCK;
623 #if APR_HAS_FCNTL_SERIALIZE
624 else if (!strcasecmp(arg, "fcntl")) {
625 ap_accept_lock_mech = APR_LOCK_FCNTL;
628 /* perchild can't use SysV sems because the permissions on the accept
629 * mutex can't be set to allow all processes to use the mutex and
630 * at the same time keep all users from being able to dink with the
633 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
634 else if (!strcasecmp(arg, "sysvsem")) {
635 ap_accept_lock_mech = APR_LOCK_SYSVSEM;
638 #if APR_HAS_PROC_PTHREAD_SERIALIZE
639 else if (!strcasecmp(arg, "pthread")) {
640 ap_accept_lock_mech = APR_LOCK_PROC_PTHREAD;
644 return apr_pstrcat(cmd->pool, arg, " is an invalid mutex mechanism; valid "
645 "ones for this platform and MPM are: default"
646 #if APR_HAS_FLOCK_SERIALIZE
649 #if APR_HAS_FCNTL_SERIALIZE
652 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
655 #if APR_HAS_PROC_PTHREAD_SERIALIZE