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"
75 #include "http_config.h"
77 #include "http_main.h"
79 #include "mpm_common.h"
81 #include "ap_listen.h"
90 #ifdef AP_MPM_NEEDS_RECLAIM_CHILD_PROCESSES
91 void ap_reclaim_child_processes(int terminate)
94 long int waittime = 1024 * 16; /* in usecs */
100 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
101 MPM_SYNC_CHILD_TABLE();
103 for (tries = terminate ? 4 : 1; tries <= 9; ++tries) {
104 /* don't want to hold up progress any more than
105 * necessary, but we need to allow children a few moments to exit.
106 * Set delay with an exponential backoff.
108 waittime = waittime * 4;
111 /* now see who is done */
113 for (i = 0; i < max_daemons; ++i) {
114 pid_t pid = MPM_CHILD_PID(i);
121 waitret = apr_proc_wait(&proc, APR_NOWAIT);
122 if (waitret != APR_CHILD_NOTDONE) {
123 MPM_NOTE_CHILD_KILLED(i);
136 /* ok, now it's being annoying */
137 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
139 "child process %ld still did not exit, sending a SIGTERM",
145 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
147 "child process %ld still did not exit, sending a SIGKILL",
152 /* sending a SIGKILL kills the entire team on BeOS, and as
153 * httpd thread is part of that team it removes any chance
154 * of ever doing a restart. To counter this I'm changing to
155 * use a kinder, gentler way of killing a specific thread
156 * that is just as effective.
162 /* gave it our best shot, but alas... If this really
163 * is a child we are trying to kill and it really hasn't
164 * exited, we will likely fail to bind to the port
167 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
169 "could not make child process %ld exit, "
170 "attempting to continue anyway", (long)pid);
174 #if APR_HAS_OTHER_CHILD
175 apr_proc_other_child_check();
178 /* nothing left to wait for */
183 #endif /* NEED_RECLAIM_CHILD_PROCESSES */
185 /* number of calls to wait_or_timeout between writable probes */
186 #ifndef INTERVAL_OF_WRITABLE_PROBES
187 #define INTERVAL_OF_WRITABLE_PROBES 10
189 static int wait_or_timeout_counter;
191 void ap_wait_or_timeout(apr_wait_t *status, apr_proc_t *ret, apr_pool_t *p)
195 ++wait_or_timeout_counter;
196 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
197 wait_or_timeout_counter = 0;
199 rv = apr_proc_wait_all_procs(ret, status, APR_NOWAIT, p);
200 if (APR_STATUS_IS_EINTR(rv)) {
204 if (APR_STATUS_IS_CHILD_DONE(rv)) {
208 if ((ret = reap_children(status)) > 0) {
212 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
217 void ap_process_child_status(apr_proc_t *pid, apr_wait_t status)
219 int signum = WTERMSIG(status);
220 const char *sigdesc = apr_signal_get_description(signum);
222 /* Child died... if it died due to a fatal error,
223 * we should simply bail out.
225 if ((WIFEXITED(status)) &&
226 WEXITSTATUS(status) == APEXIT_CHILDFATAL) {
227 ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, ap_server_conf,
228 "Child %ld returned a Fatal error..." APR_EOL_STR
229 "Apache is exiting!",
231 exit(APEXIT_CHILDFATAL);
234 if (WIFSIGNALED(status)) {
243 if (WCOREDUMP(status)) {
244 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
246 "child pid %ld exit signal %s (%d), "
247 "possible coredump in %s",
248 (long)pid->pid, sigdesc, signum,
254 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
256 "child pid %ld exit signal %s (%d)",
257 (long)pid->pid, sigdesc, signum);
263 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
264 void ap_sock_disable_nagle(apr_socket_t *s)
266 /* The Nagle algorithm says that we should delay sending partial
267 * packets in hopes of getting more data. We don't want to do
268 * this; we are not telnet. There are bad interactions between
269 * persistent connections and Nagle's algorithm that have very severe
270 * performance penalties. (Failing to disable Nagle is not much of a
271 * problem with simple HTTP.)
273 * In spite of these problems, failure here is not a shooting offense.
275 apr_status_t status = apr_setsocketopt(s, APR_TCP_NODELAY, 1);
277 if (status != APR_SUCCESS) {
278 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
279 "setsockopt: (TCP_NODELAY)");
284 AP_DECLARE(uid_t) ap_uname2id(const char *name)
289 return (atoi(&name[1]));
291 if (!(ent = getpwnam(name))) {
292 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad user name %s", ap_server_argv0, name);
295 return (ent->pw_uid);
298 AP_DECLARE(gid_t) ap_gname2id(const char *name)
303 return (atoi(&name[1]));
305 if (!(ent = getgrnam(name))) {
306 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad group name %s", ap_server_argv0, name); exit(1);
308 return (ent->gr_gid);
311 #ifndef HAVE_INITGROUPS
312 int initgroups(const char *name, gid_t basegid)
314 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32)
315 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
318 gid_t groups[NGROUPS_MAX];
324 groups[index++] = basegid;
326 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
327 if (g->gr_gid != basegid) {
330 for (names = g->gr_mem; *names != NULL; ++names)
331 if (!strcmp(*names, name))
332 groups[index++] = g->gr_gid;
337 return setgroups(index, groups);
340 #endif /* def NEED_INITGROUPS */
342 #ifdef AP_MPM_USES_POD
344 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
348 *pod = apr_palloc(p, sizeof(**pod));
349 rv = apr_file_pipe_create(&((*pod)->pod_in), &((*pod)->pod_out), p);
350 apr_file_pipe_timeout_set((*pod)->pod_in, 0);
355 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
361 rv = apr_file_read(pod->pod_in, &c, &len);
363 if ((rv == APR_SUCCESS) && (len == 1)) {
366 if (rv != APR_SUCCESS) {
372 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
376 rv = apr_file_close(pod->pod_out);
377 if (rv != APR_SUCCESS) {
381 rv = apr_file_close(pod->pod_in);
382 if (rv != APR_SUCCESS) {
388 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
393 char char_of_death = '!';
397 rv = apr_file_write(pod->pod_out, &char_of_death, &one);
398 } while (APR_STATUS_IS_EINTR(rv));
399 if (rv != APR_SUCCESS) {
400 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
401 "write pipe_of_death");
405 apr_sockaddr_info_get(&sa, "127.0.0.1", APR_UNSPEC, ap_listeners->bind_addr->port, 0, pod->p);
406 rv = apr_socket_create(&sock, sa->family, SOCK_STREAM, pod->p);
407 if (rv != APR_SUCCESS) {
408 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
409 "get socket to connect to listener");
412 /* on some platforms (e.g., FreeBSD), the kernel won't accept many
413 * queued connections before it starts blocking local connects...
414 * we need to keep from blocking too long and instead return an error,
415 * because the MPM won't want to hold up a graceful restart for a
418 rv = apr_setsocketopt(sock, APR_SO_TIMEOUT, 3 * APR_USEC_PER_SEC);
419 if (rv != APR_SUCCESS) {
420 ap_log_error(APLOG_MARK, APLOG_WARNING, rv, ap_server_conf,
421 "set timeout on socket to connect to listener");
424 rv = apr_connect(sock, sa);
425 if (rv != APR_SUCCESS) {
426 int log_level = APLOG_WARNING;
428 if (APR_STATUS_IS_TIMEUP(rv)) {
429 /* probably some server processes bailed out already and there
430 * is nobody around to call accept and clear out the kernel
431 * connection queue; usually this is not worth logging
433 log_level = APLOG_DEBUG;
436 ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
437 "connect to listener");
440 apr_socket_close(sock);
445 AP_DECLARE(void) ap_mpm_pod_killpg(ap_pod_t *pod, int num)
448 apr_status_t rv = APR_SUCCESS;
450 for (i = 0; i < num && rv == APR_SUCCESS; i++) {
451 rv = ap_mpm_pod_signal(pod);