1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 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"
88 #ifdef MPM_NEEDS_RECLAIM_CHILD_PROCESSES
89 void ap_reclaim_child_processes(int terminate)
92 long int waittime = 1024 * 16; /* in usecs */
96 int max_daemons = ap_get_max_daemons();
98 MPM_SYNC_CHILD_TABLE();
100 for (tries = terminate ? 4 : 1; tries <= 9; ++tries) {
101 /* don't want to hold up progress any more than
102 * necessary, but we need to allow children a few moments to exit.
103 * Set delay with an exponential backoff.
105 waittime = waittime * 4;
108 /* now see who is done */
110 for (i = 0; i < max_daemons; ++i) {
111 pid_t pid = MPM_CHILD_PID(i);
118 waitret = apr_proc_wait(&proc, APR_NOWAIT);
119 if (waitret != APR_CHILD_NOTDONE) {
120 MPM_NOTE_CHILD_KILLED(i);
133 /* ok, now it's being annoying */
134 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
136 "child process %ld still did not exit, sending a SIGTERM",
142 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
144 "child process %ld still did not exit, sending a SIGKILL",
149 /* sending a SIGKILL kills the entire team on BeOS, and as
150 * httpd thread is part of that team it removes any chance
151 * of ever doing a restart. To counter this I'm changing to
152 * use a kinder, gentler way of killing a specific thread
153 * that is just as effective.
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_NOERRNO|APLOG_ERR,
166 "could not make child process %ld exit, "
167 "attempting to continue anyway", (long)pid);
171 apr_proc_other_child_check();
173 /* nothing left to wait for */
178 #endif /* NEED_RECLAIM_CHILD_PROCESSES */
180 /* number of calls to wait_or_timeout between writable probes */
181 #ifndef INTERVAL_OF_WRITABLE_PROBES
182 #define INTERVAL_OF_WRITABLE_PROBES 10
184 static int wait_or_timeout_counter;
186 void ap_wait_or_timeout(apr_wait_t *status, apr_proc_t *ret, apr_pool_t *p)
190 ++wait_or_timeout_counter;
191 if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
192 wait_or_timeout_counter = 0;
193 #if APR_HAS_OTHER_CHILD
194 apr_proc_probe_writable_fds();
197 rv = apr_proc_wait_all_procs(ret, status, APR_NOWAIT, p);
198 if (APR_STATUS_IS_EINTR(rv)) {
202 if (APR_STATUS_IS_CHILD_DONE(rv)) {
206 if ((ret = reap_children(status)) > 0) {
210 apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
215 void ap_process_child_status(apr_proc_t *pid, apr_wait_t status)
217 int signum = WTERMSIG(status);
218 const char *sigdesc = apr_signal_get_description(signum);
220 /* Child died... if it died due to a fatal error,
221 * we should simply bail out.
223 if ((WIFEXITED(status)) &&
224 WEXITSTATUS(status) == APEXIT_CHILDFATAL) {
225 ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, ap_server_conf,
226 "Child %ld returned a Fatal error..." APR_EOL_STR
227 "Apache is exiting!",
229 exit(APEXIT_CHILDFATAL);
232 if (WIFSIGNALED(status)) {
241 if (WCOREDUMP(status)) {
242 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
244 "child pid %ld exit signal %s (%d), "
245 "possible coredump in %s",
246 (long)pid->pid, sigdesc, signum,
252 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
254 "child pid %ld exit signal %s (%d)",
255 (long)pid->pid, sigdesc, signum);
261 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
262 void ap_sock_disable_nagle(apr_socket_t *s)
264 /* The Nagle algorithm says that we should delay sending partial
265 * packets in hopes of getting more data. We don't want to do
266 * this; we are not telnet. There are bad interactions between
267 * persistent connections and Nagle's algorithm that have very severe
268 * performance penalties. (Failing to disable Nagle is not much of a
269 * problem with simple HTTP.)
271 * In spite of these problems, failure here is not a shooting offense.
273 apr_status_t status = apr_setsocketopt(s, APR_TCP_NODELAY, 1);
275 if (status != APR_SUCCESS) {
276 ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
277 "setsockopt: (TCP_NODELAY)");
282 AP_DECLARE(uid_t) ap_uname2id(const char *name)
287 return (atoi(&name[1]));
289 if (!(ent = getpwnam(name))) { ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad user name %s", ap_server_argv0, name);
292 return (ent->pw_uid);
295 AP_DECLARE(gid_t) ap_gname2id(const char *name)
300 return (atoi(&name[1]));
302 if (!(ent = getgrnam(name))) {
303 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad group name %s", ap_server_argv0, name); exit(1);
305 return (ent->gr_gid);
308 #ifndef HAVE_INITGROUPS
309 int initgroups(const char *name, gid_t basegid)
311 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32)
312 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
315 gid_t groups[NGROUPS_MAX];
321 groups[index++] = basegid;
323 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
324 if (g->gr_gid != basegid) {
327 for (names = g->gr_mem; *names != NULL; ++names)
328 if (!strcmp(*names, name))
329 groups[index++] = g->gr_gid;
334 return setgroups(index, groups);
337 #endif /* def NEED_INITGROUPS */