]> granicus.if.org Git - apache/blob - server/scoreboard.c
*) remove obsolete http_conf_globals.h
[apache] / server / scoreboard.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 #include "apr_strings.h"
60 #include "apr_portable.h"
61 #include "ap_config.h"
62 #include "httpd.h"
63 #include "http_log.h"
64 #include "http_main.h"
65 #include "http_core.h"
66 #include "http_config.h"
67
68 #include "mpm.h"
69 #include "scoreboard.h"
70 #ifdef HAVE_SYS_TYPES_H
71 #include <sys/types.h>
72 #endif
73
74 AP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
75 AP_DECLARE_DATA const char *ap_scoreboard_fname=NULL;
76 AP_DECLARE_DATA int ap_extended_status = 0;
77 AP_DECLARE_DATA apr_time_t ap_restart_time = 0;
78
79 #if APR_HAS_SHARED_MEMORY
80 #include "apr_shmem.h"
81 static apr_shmem_t *scoreboard_shm = NULL;
82 #endif
83 /*
84  * ToDo:
85  * This function should be renamed to cleanup_shared
86  * and it should handle cleaning up a scoreboard shared
87  * between processes using any form of IPC (file, shared memory
88  * segment, etc.). Leave it as is now because it is being used
89  * by various MPMs. 
90  */
91 static apr_status_t ap_cleanup_shared_mem(void *d)
92 {
93 #if APR_HAS_SHARED_MEMORY
94     apr_shm_free(scoreboard_shm, ap_scoreboard_image);
95     ap_scoreboard_image = NULL;
96     apr_shm_destroy(scoreboard_shm);
97 #endif
98     return APR_SUCCESS;
99 }
100
101 /* ToDo: This function should be made to handle setting up 
102  * a scoreboard shared between processes using any IPC technique, 
103  * not just a shared memory segment
104  */
105 static void setup_shared(apr_pool_t *p)
106 {
107 #if APR_HAS_SHARED_MEMORY
108     char buf[512];
109     char errmsg[120];
110     const char *fname;
111     apr_status_t rv;
112
113     fname = ap_server_root_relative(p, ap_scoreboard_fname);
114     rv = apr_shm_init(&scoreboard_shm, SCOREBOARD_SIZE, fname, p);
115     if (rv != APR_SUCCESS) {
116         apr_snprintf(buf, sizeof(buf), "%s: could not open(create) scoreboard: %s",
117                     ap_server_argv0, apr_strerror(rv, errmsg, sizeof errmsg));
118         fprintf(stderr, "%s\n", buf);
119         exit(APEXIT_INIT);
120     }
121     ap_scoreboard_image = apr_shm_malloc(scoreboard_shm, SCOREBOARD_SIZE);
122     if (ap_scoreboard_image == NULL) {
123         apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
124                     ap_server_argv0);
125         perror(buf); /* o.k. since MM sets errno */
126         apr_shm_destroy(scoreboard_shm);
127         exit(APEXIT_INIT);
128     }
129     ap_scoreboard_image->global.running_generation = 0;
130 #endif
131 }
132
133 AP_DECLARE(void) reopen_scoreboard(apr_pool_t *p)
134 {
135 }
136
137 /* ap_cleanup_scoreboard
138  * 
139  */
140 apr_status_t ap_cleanup_scoreboard(void *d) {
141     if (ap_scoreboard_image == NULL)
142         return APR_SUCCESS;
143     if (ap_scoreboard_image->global.sb_type == SB_SHARED) {
144         ap_cleanup_shared_mem(NULL);
145     }
146     else {
147         free(ap_scoreboard_image);
148         ap_scoreboard_image = NULL;
149     }
150     return APR_SUCCESS;
151 }
152
153 /* ap_create_scoreboard(apr_pool_t*, ap_scoreboard_e t)
154  *
155  * Create or reinit an existing scoreboard. The MPM can control whether
156  * the scoreboard is shared across multiple processes or not
157  */
158 AP_DECLARE(void) ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
159 {
160     int running_gen = 0;
161     if (ap_scoreboard_image)
162         running_gen = ap_scoreboard_image->global.running_generation;
163     if (ap_scoreboard_image == NULL) {
164         if (sb_type == SB_SHARED) {
165             setup_shared(p);
166         }
167         else {
168             /* A simple malloc will suffice */
169             char buf[512];
170             ap_scoreboard_image = (scoreboard *) malloc(SCOREBOARD_SIZE);
171             if (ap_scoreboard_image == NULL) {
172                 apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
173                              ap_server_argv0);
174                 perror(buf); /* o.k. since MM sets errno */
175                 exit(APEXIT_INIT);            
176             }
177         }
178     }
179     memset(ap_scoreboard_image, 0, SCOREBOARD_SIZE);
180     ap_scoreboard_image->global.sb_type = sb_type;
181     ap_scoreboard_image->global.running_generation = running_gen;
182     ap_restart_time = apr_time_now();
183     apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
184 }
185
186 /* Routines called to deal with the scoreboard image
187  * --- note that we do *not* need write locks, since update_child_status
188  * only updates a *single* record in place, and only one process writes to
189  * a given scoreboard slot at a time (either the child process owning that
190  * slot, or the parent, noting that the child has died).
191  *
192  * As a final note --- setting the score entry to getpid() is always safe,
193  * since when the parent is writing an entry, it's only noting SERVER_DEAD
194  * anyway.
195  */
196
197 void ap_sync_scoreboard_image(void)
198 {
199 }
200
201 AP_DECLARE(int) ap_exists_scoreboard_image(void)
202 {
203     return (ap_scoreboard_image ? 1 : 0);
204 }
205
206 static apr_inline void put_scoreboard_info(int child_num, int thread_num, 
207                                        short_score *new_score_rec)
208 {
209     /* XXX - needs to be fixed to account for threads */
210 #ifdef SCOREBOARD_FILE
211     lseek(scoreboard_fd, (long) child_num * sizeof(short_score), 0);
212     force_write(scoreboard_fd, new_score_rec, sizeof(short_score));
213 #endif
214 }
215
216 void update_scoreboard_global(void)
217 {
218 #ifdef SCOREBOARD_FILE
219     lseek(scoreboard_fd,
220           (char *) &ap_scoreboard_image->global -(char *) ap_scoreboard_image, 0);
221     force_write(scoreboard_fd, &ap_scoreboard_image->global,
222                 sizeof ap_scoreboard_image->global);
223 #endif
224 }
225
226 void increment_counts(int child_num, int thread_num, request_rec *r)
227 {
228     short_score *ss;
229
230     ss = &ap_scoreboard_image->servers[child_num][thread_num];
231
232 #ifdef HAVE_TIMES
233     times(&ss->times);
234 #endif
235     ss->access_count++;
236     ss->my_access_count++;
237     ss->conn_count++;
238     ss->bytes_served += r->bytes_sent;
239     ss->my_bytes_served += r->bytes_sent;
240     ss->conn_bytes += r->bytes_sent;
241
242     put_scoreboard_info(child_num, thread_num, ss);
243 }
244
245 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid)
246 {
247     int i;
248     int max_daemons_limit = ap_get_max_daemons();
249
250     for (i = 0; i < max_daemons_limit; ++i)
251         if (ap_scoreboard_image->parent[i].pid == pid->pid)
252             return i;
253
254     return -1;
255 }
256
257 int ap_update_child_status(int child_num, int thread_num, int status, request_rec *r)
258 {
259     int old_status;
260     short_score *ss;
261     parent_score *ps;
262
263     if (child_num < 0)
264         return -1;
265
266     ss = &ap_scoreboard_image->servers[child_num][thread_num];
267     old_status = ss->status;
268     ss->status = status;
269
270     ps = &ap_scoreboard_image->parent[child_num];
271     
272     if ((status == SERVER_READY  || status == SERVER_ACCEPTING)
273         && old_status == SERVER_STARTING) {
274         ss->thread_num = child_num * HARD_SERVER_LIMIT + thread_num;
275         ps->worker_threads = ap_threads_per_child;
276     }
277
278     if (ap_extended_status) {
279         if (status == SERVER_READY || status == SERVER_DEAD) {
280             /*
281              * Reset individual counters
282              */
283             if (status == SERVER_DEAD) {
284                 ss->my_access_count = 0L;
285                 ss->my_bytes_served = 0L;
286             }
287             ss->conn_count = (unsigned short) 0;
288             ss->conn_bytes = (unsigned long) 0;
289         }
290         if (r) {
291             conn_rec *c = r->connection;
292             apr_cpystrn(ss->client, ap_get_remote_host(c, r->per_dir_config,
293                                   REMOTE_NOLOOKUP), sizeof(ss->client));
294             if (r->the_request == NULL) {
295                     apr_cpystrn(ss->request, "NULL", sizeof(ss->request));
296             } else if (r->parsed_uri.password == NULL) {
297                     apr_cpystrn(ss->request, r->the_request, sizeof(ss->request));
298             } else {
299                 /* Don't reveal the password in the server-status view */
300                     apr_cpystrn(ss->request, apr_pstrcat(r->pool, r->method, " ",
301                                                ap_unparse_uri_components(r->pool, &r->parsed_uri, UNP_OMITPASSWORD),
302                                                r->assbackwards ? NULL : " ", r->protocol, NULL),
303                                        sizeof(ss->request));
304             }
305             ss->vhostrec =  r->server;
306         }
307     }
308     
309     put_scoreboard_info(child_num, thread_num, ss);
310     return old_status;
311 }
312
313 void ap_time_process_request(int child_num, int thread_num, int status)
314 {
315     short_score *ss;
316
317     if (child_num < 0)
318         return;
319
320     ss = &ap_scoreboard_image->servers[child_num][thread_num];
321
322     if (status == START_PREQUEST) {
323         ss->start_time = apr_time_now(); 
324     }
325     else if (status == STOP_PREQUEST) {
326         ss->stop_time = apr_time_now(); 
327     }
328     put_scoreboard_info(child_num, thread_num, ss);
329 }
330