]> granicus.if.org Git - apache/blob - server/mpm_common.c
Changed AP_MPMQ_MAX_DAEMONS to refer to MaxClients and
[apache] / server / mpm_common.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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.
25  *
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.
30  *
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.
34  *
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
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
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/>.
53  *
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.
57  */
58
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.
64  *
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
68  */
69
70 #include "apr.h"
71 #include "apr_thread_proc.h"
72 #include "apr_signal.h"
73
74 #include "httpd.h"
75 #include "http_config.h"
76 #include "http_log.h"
77 #include "http_main.h"
78 #include "mpm.h"
79 #include "mpm_common.h"
80 #include "ap_mpm.h"
81 #include "ap_listen.h"
82
83 #ifdef HAVE_PWD_H
84 #include <pwd.h>
85 #endif
86 #ifdef HAVE_GRP_H
87 #include <grp.h>
88 #endif
89
90 #ifdef AP_MPM_NEEDS_RECLAIM_CHILD_PROCESSES
91 void ap_reclaim_child_processes(int terminate)
92 {
93     int i;
94     long int waittime = 1024 * 16;      /* in usecs */
95     apr_status_t waitret;
96     int tries;
97     int not_dead_yet;
98     int max_daemons;
99
100     ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons);
101     MPM_SYNC_CHILD_TABLE();
102
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.
107          */
108         waittime = waittime * 4;
109         apr_sleep(waittime);
110
111         /* now see who is done */
112         not_dead_yet = 0;
113         for (i = 0; i < max_daemons; ++i) {
114             pid_t pid = MPM_CHILD_PID(i);
115             apr_proc_t proc;
116
117             if (pid == 0)
118                 continue;
119
120             proc.pid = pid;
121             waitret = apr_proc_wait(&proc, APR_NOWAIT);
122             if (waitret != APR_CHILD_NOTDONE) {
123                 MPM_NOTE_CHILD_KILLED(i);
124                 continue;
125             }
126             ++not_dead_yet;
127             switch (tries) {
128             case 1:     /*  16ms */
129             case 2:     /*  82ms */
130             case 3:     /* 344ms */
131             case 4:     /*  16ms */
132                 break;
133             case 5:     /*  82ms */
134             case 6:     /* 344ms */
135             case 7:     /* 1.4sec */
136                 /* ok, now it's being annoying */
137                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
138                              0, ap_server_conf,
139                    "child process %ld still did not exit, sending a SIGTERM",
140                              (long)pid);
141                 kill(pid, SIGTERM);
142                 break;
143             case 8:     /*  6 sec */
144                 /* die child scum */
145                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
146                              0, ap_server_conf,
147                    "child process %ld still did not exit, sending a SIGKILL",
148                              (long)pid);
149 #ifndef BEOS
150                 kill(pid, SIGKILL);
151 #else
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.
157                  */
158                 kill_thread(pid);
159 #endif
160                 break;
161             case 9:     /* 14 sec */
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
165                  * after the restart.
166                  */
167                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
168                              0, ap_server_conf,
169                              "could not make child process %ld exit, "
170                              "attempting to continue anyway", (long)pid);
171                 break;
172             }
173         }
174 #if APR_HAS_OTHER_CHILD
175         apr_proc_other_child_check();
176 #endif
177         if (!not_dead_yet) {
178             /* nothing left to wait for */
179             break;
180         }
181     }
182 }
183 #endif /* NEED_RECLAIM_CHILD_PROCESSES */
184
185 /* number of calls to wait_or_timeout between writable probes */
186 #ifndef INTERVAL_OF_WRITABLE_PROBES
187 #define INTERVAL_OF_WRITABLE_PROBES 10
188 #endif
189 static int wait_or_timeout_counter;
190
191 void ap_wait_or_timeout(apr_wait_t *status, apr_proc_t *ret, apr_pool_t *p)
192 {
193     apr_status_t rv;
194
195     ++wait_or_timeout_counter;
196     if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
197         wait_or_timeout_counter = 0;
198     }
199     rv = apr_proc_wait_all_procs(ret, status, APR_NOWAIT, p);
200     if (APR_STATUS_IS_EINTR(rv)) {
201         ret->pid = -1;
202         return;
203     }
204     if (APR_STATUS_IS_CHILD_DONE(rv)) {
205         return;
206     }
207 #ifdef NEED_WAITPID
208     if ((ret = reap_children(status)) > 0) {
209         return;
210     }
211 #endif
212     apr_sleep(SCOREBOARD_MAINTENANCE_INTERVAL);
213     ret->pid = -1;
214     return;
215 }
216
217 void ap_process_child_status(apr_proc_t *pid, apr_wait_t status)
218 {
219     int signum = WTERMSIG(status);
220     const char *sigdesc = apr_signal_get_description(signum);
221
222     /* Child died... if it died due to a fatal error,
223         * we should simply bail out.
224         */
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!",
230                         (long)pid->pid);
231         exit(APEXIT_CHILDFATAL);
232     }
233
234     if (WIFSIGNALED(status)) {
235         switch (signum) {
236         case SIGTERM:
237         case SIGHUP:
238         case SIGWINCH:
239         case SIGKILL:
240             break;
241         default:
242 #ifdef WCOREDUMP
243             if (WCOREDUMP(status)) {
244                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
245                              0, ap_server_conf,
246                              "child pid %ld exit signal %s (%d), "
247                              "possible coredump in %s",
248                              (long)pid->pid, sigdesc, signum,
249                              ap_coredump_dir);
250             }
251             else
252 #endif
253             {
254                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE,
255                              0, ap_server_conf,
256                              "child pid %ld exit signal %s (%d)",
257                              (long)pid->pid, sigdesc, signum);
258             }
259         }
260     }
261 }
262
263 #if defined(TCP_NODELAY) && !defined(MPE) && !defined(TPF)
264 void ap_sock_disable_nagle(apr_socket_t *s)
265 {
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.)
272      *
273      * In spite of these problems, failure here is not a shooting offense.
274      */
275     apr_status_t status = apr_setsocketopt(s, APR_TCP_NODELAY, 1);
276
277     if (status != APR_SUCCESS) {
278         ap_log_error(APLOG_MARK, APLOG_WARNING, status, ap_server_conf,
279                     "setsockopt: (TCP_NODELAY)");
280     }
281 }
282 #endif
283
284 AP_DECLARE(uid_t) ap_uname2id(const char *name)
285 {
286     struct passwd *ent;
287
288     if (name[0] == '#')
289         return (atoi(&name[1]));
290
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);
293         exit(1);
294     }
295     return (ent->pw_uid);
296 }
297
298 AP_DECLARE(gid_t) ap_gname2id(const char *name)
299 {
300     struct group *ent;
301
302     if (name[0] == '#')
303         return (atoi(&name[1]));
304
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);
307     }
308     return (ent->gr_gid);
309 }
310
311 #ifndef HAVE_INITGROUPS
312 int initgroups(const char *name, gid_t basegid)
313 {
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. */
316     return 0;
317 #else /* ndef QNX */
318     gid_t groups[NGROUPS_MAX];
319     struct group *g;
320     int index = 0;
321
322     setgrent();
323
324     groups[index++] = basegid;
325
326     while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
327         if (g->gr_gid != basegid) {
328             char **names;
329
330             for (names = g->gr_mem; *names != NULL; ++names)
331                 if (!strcmp(*names, name))
332                     groups[index++] = g->gr_gid;
333         }
334
335     endgrent();
336
337     return setgroups(index, groups);
338 #endif /* def QNX */
339 }
340 #endif /* def NEED_INITGROUPS */
341
342 #ifdef AP_MPM_USES_POD
343
344 AP_DECLARE(apr_status_t) ap_mpm_pod_open(apr_pool_t *p, ap_pod_t **pod)
345 {
346     apr_status_t rv;
347
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);
351     (*pod)->p = p;
352     return rv;
353 }
354
355 AP_DECLARE(apr_status_t) ap_mpm_pod_check(ap_pod_t *pod)
356 {
357     char c;
358     apr_size_t len = 1;
359     apr_status_t rv;
360
361     rv = apr_file_read(pod->pod_in, &c, &len);
362
363     if ((rv == APR_SUCCESS) && (len == 1)) {
364         return APR_SUCCESS;
365     }
366     if (rv != APR_SUCCESS) {
367         return rv;
368     }
369     return AP_NORESTART;
370 }
371
372 AP_DECLARE(apr_status_t) ap_mpm_pod_close(ap_pod_t *pod)
373 {
374     apr_status_t rv;
375
376     rv = apr_file_close(pod->pod_out);
377     if (rv != APR_SUCCESS) {
378         return rv;
379     }
380
381     rv = apr_file_close(pod->pod_in);
382     if (rv != APR_SUCCESS) {
383         return rv;
384     }
385     return rv;
386 }
387
388 AP_DECLARE(apr_status_t) ap_mpm_pod_signal(ap_pod_t *pod)
389 {
390     apr_socket_t *sock;
391     apr_sockaddr_t *sa;
392     apr_status_t rv;
393     char char_of_death = '!';
394     apr_size_t one = 1;
395
396     do {
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");
402         return rv;
403     }
404     
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");
410         return rv;
411     }
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
416      * long time
417      */
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");
422         return rv;
423     }
424     rv = apr_connect(sock, sa);    
425     if (rv != APR_SUCCESS) {
426         int log_level = APLOG_WARNING;
427
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
432              */
433             log_level = APLOG_DEBUG;
434         }
435         
436         ap_log_error(APLOG_MARK, log_level, rv, ap_server_conf,
437                      "connect to listener");
438         return rv;
439     }
440     apr_socket_close(sock);
441
442     return APR_SUCCESS;
443 }
444
445 AP_DECLARE(void) ap_mpm_pod_killpg(ap_pod_t *pod, int num)
446 {
447     int i;
448     apr_status_t rv = APR_SUCCESS;
449
450     for (i = 0; i < num && rv == APR_SUCCESS; i++) {
451         rv = ap_mpm_pod_signal(pod);
452     }
453 }
454 #endif