]> granicus.if.org Git - apache/blob - server/mpm_common.c
Remove waitpid from the config checks and all calls to waitpid from the
[apache] / server / mpm_common.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 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_thread_proc.h"
71 #include "httpd.h"
72 #include "http_config.h"
73 #include "http_log.h"
74 #include "mpm.h"
75 #include "mpm_common.h"
76
77 #if defined(DEXTER_MPM) || defined(MPMT_BEOS_MPM)
78 #define CHILD_TABLE 1
79 #define CHILD_INFO_TABLE     ap_child_table
80 #elif defined(MPMT_PTHREAD_MPM) || defined (PREFORK_MPM)
81 #define SCOREBOARD 1
82 #define CHILD_INFO_TABLE     ap_scoreboard_image->parent
83 #endif 
84
85
86 #ifdef CHILD_INFO_TABLE
87 void ap_reclaim_child_processes(int terminate)
88 {
89     int i;
90     long int waittime = 1024 * 16;      /* in usecs */
91     struct timeval tv;
92     ap_status_t waitret;
93     int tries;
94     int not_dead_yet;
95
96 #ifdef SCOREBOARD
97     ap_sync_scoreboard_image();
98 #endif
99
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.
104          */
105         tv.tv_sec = waittime / 1000000;
106         tv.tv_usec = waittime % 1000000;
107         waittime = waittime * 4;
108         ap_select(0, NULL, NULL, NULL, &tv);
109
110         /* now see who is done */
111         not_dead_yet = 0;
112         for (i = 0; i < ap_max_daemons_limit; ++i) {
113             pid_t pid = CHILD_INFO_TABLE[i].pid;
114             ap_proc_t proc;
115
116 #ifdef CHILD_TABLE
117             if (ap_child_table[i].status == SERVER_DEAD)
118 #elif defined(SCOREBOARD)
119             if (pid == ap_my_pid || pid == 0)
120 #endif
121                 continue;
122
123             proc.pid = pid;
124             waitret = ap_wait_proc(&proc, APR_NOWAIT);
125             if (waitret != APR_CHILD_NOTDONE) {
126 #ifdef CHILD_TABLE
127                 ap_child_table[i].status = SERVER_DEAD;
128 #elif defined(SCOREBOARD)
129                 ap_scoreboard_image->parent[i].pid = 0;
130 #endif
131                 continue;
132             }
133             ++not_dead_yet;
134             switch (tries) {
135             case 1:     /*  16ms */
136             case 2:     /*  82ms */
137                 break;
138             case 3:     /* 344ms */
139             case 4:     /*  16ms */
140             case 5:     /*  82ms */
141             case 6:     /* 344ms */
142             case 7:     /* 1.4sec */
143                 /* ok, now it's being annoying */
144                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
145                              0, ap_server_conf,
146                    "child process %ld still did not exit, sending a SIGTERM",
147                              (long)pid);
148                 kill(pid, SIGTERM);
149                 break;
150             case 8:     /*  6 sec */
151                 /* die child scum */
152                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
153                              0, ap_server_conf,
154                    "child process %ld still did not exit, sending a SIGKILL",
155                              (long)pid);
156                 kill(pid, SIGKILL);
157                 break;
158             case 9:     /* 14 sec */
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
162                  * after the restart.
163                  */
164                 ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
165                              0, ap_server_conf,
166                              "could not make child process %ld exit, "
167                              "attempting to continue anyway", (long)pid);
168                 break;
169             }
170         }
171         ap_check_other_child();
172         if (!not_dead_yet) {
173             /* nothing left to wait for */
174             break;
175         }
176     }
177 }
178 #endif
179
180 /* number of calls to wait_or_timeout between writable probes */
181 #ifndef INTERVAL_OF_WRITABLE_PROBES
182 #define INTERVAL_OF_WRITABLE_PROBES 10
183 #endif
184 static int wait_or_timeout_counter;
185
186 void ap_wait_or_timeout(ap_wait_t *status, ap_proc_t *ret, ap_pool_t *p)
187 {
188     struct timeval tv;
189     ap_status_t rv;
190
191     ++wait_or_timeout_counter;
192     if (wait_or_timeout_counter == INTERVAL_OF_WRITABLE_PROBES) {
193         wait_or_timeout_counter = 0;
194 #ifdef APR_HAS_OTHER_CHILD
195         ap_probe_writable_fds();
196 #endif
197     }
198     rv = ap_wait_all_procs(ret, status, APR_NOWAIT, p);
199     if (ap_canonical_error(rv) == APR_EINTR) {
200         ret->pid = -1;
201         return;
202     }
203     if (rv == APR_CHILD_DONE) {
204         return;
205     }
206 #ifdef NEED_WAITPID
207     if ((ret = reap_children(status)) > 0) {
208         return;
209     }
210 #endif
211     tv.tv_sec = SCOREBOARD_MAINTENANCE_INTERVAL / 1000000;
212     tv.tv_usec = SCOREBOARD_MAINTENANCE_INTERVAL % 1000000;
213     ap_select(0, NULL, NULL, NULL, &tv);
214     ret->pid = -1;
215     return;
216 }
217
218