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.
60 #include "apr_strings.h"
61 #include "apr_portable.h"
64 #define APR_WANT_STRFUNC
67 #if APR_HAVE_SYS_TYPES_H
68 #include <sys/types.h>
71 #include "ap_config.h"
74 #include "http_main.h"
75 #include "http_core.h"
76 #include "http_config.h"
80 #include "scoreboard.h"
82 AP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
83 AP_DECLARE_DATA const char *ap_scoreboard_fname=NULL;
84 AP_DECLARE_DATA int ap_extended_status = 0;
85 AP_DECLARE_DATA apr_time_t ap_restart_time = 0;
87 #if APR_HAS_SHARED_MEMORY
88 #include "apr_shmem.h"
89 static apr_shmem_t *scoreboard_shm = NULL;
93 * This function should be renamed to cleanup_shared
94 * and it should handle cleaning up a scoreboard shared
95 * between processes using any form of IPC (file, shared memory
96 * segment, etc.). Leave it as is now because it is being used
99 static apr_status_t ap_cleanup_shared_mem(void *d)
101 #if APR_HAS_SHARED_MEMORY
102 apr_shm_free(scoreboard_shm, ap_scoreboard_image);
103 ap_scoreboard_image = NULL;
104 apr_shm_destroy(scoreboard_shm);
109 /* ToDo: This function should be made to handle setting up
110 * a scoreboard shared between processes using any IPC technique,
111 * not just a shared memory segment
113 static void setup_shared(apr_pool_t *p)
115 #if APR_HAS_SHARED_MEMORY
121 fname = ap_server_root_relative(p, ap_scoreboard_fname);
122 rv = apr_shm_init(&scoreboard_shm, SCOREBOARD_SIZE, fname, p);
123 if (rv != APR_SUCCESS) {
124 apr_snprintf(buf, sizeof(buf), "%s: could not open(create) scoreboard: %s",
125 ap_server_argv0, apr_strerror(rv, errmsg, sizeof errmsg));
126 fprintf(stderr, "%s\n", buf);
129 ap_scoreboard_image = apr_shm_malloc(scoreboard_shm, SCOREBOARD_SIZE);
130 if (ap_scoreboard_image == NULL) {
131 apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
133 perror(buf); /* o.k. since MM sets errno */
134 apr_shm_destroy(scoreboard_shm);
137 ap_scoreboard_image->global.running_generation = 0;
141 AP_DECLARE(void) reopen_scoreboard(apr_pool_t *p)
145 /* ap_cleanup_scoreboard
148 apr_status_t ap_cleanup_scoreboard(void *d) {
149 if (ap_scoreboard_image == NULL)
151 if (ap_scoreboard_image->global.sb_type == SB_SHARED) {
152 ap_cleanup_shared_mem(NULL);
155 free(ap_scoreboard_image);
156 ap_scoreboard_image = NULL;
161 /* ap_create_scoreboard(apr_pool_t*, ap_scoreboard_e t)
163 * Create or reinit an existing scoreboard. The MPM can control whether
164 * the scoreboard is shared across multiple processes or not
166 AP_DECLARE(void) ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
169 if (ap_scoreboard_image)
170 running_gen = ap_scoreboard_image->global.running_generation;
171 if (ap_scoreboard_image == NULL) {
172 if (sb_type == SB_SHARED) {
176 /* A simple malloc will suffice */
178 ap_scoreboard_image = (scoreboard *) malloc(SCOREBOARD_SIZE);
179 if (ap_scoreboard_image == NULL) {
180 apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
182 perror(buf); /* o.k. since MM sets errno */
187 memset(ap_scoreboard_image, 0, SCOREBOARD_SIZE);
188 ap_scoreboard_image->global.sb_type = sb_type;
189 ap_scoreboard_image->global.running_generation = running_gen;
190 ap_restart_time = apr_time_now();
191 apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
194 /* Routines called to deal with the scoreboard image
195 * --- note that we do *not* need write locks, since update_child_status
196 * only updates a *single* record in place, and only one process writes to
197 * a given scoreboard slot at a time (either the child process owning that
198 * slot, or the parent, noting that the child has died).
200 * As a final note --- setting the score entry to getpid() is always safe,
201 * since when the parent is writing an entry, it's only noting SERVER_DEAD
205 void ap_sync_scoreboard_image(void)
209 AP_DECLARE(int) ap_exists_scoreboard_image(void)
211 return (ap_scoreboard_image ? 1 : 0);
214 static APR_INLINE void put_scoreboard_info(int child_num, int thread_num,
215 worker_score *new_score_rec)
217 /* XXX - needs to be fixed to account for threads */
218 #ifdef SCOREBOARD_FILE
219 lseek(scoreboard_fd, (long) child_num * sizeof(worker_score), 0);
220 force_write(scoreboard_fd, new_score_rec, sizeof(worker_score));
224 void update_scoreboard_global(void)
226 #ifdef SCOREBOARD_FILE
228 (char *) &ap_scoreboard_image->global -(char *) ap_scoreboard_image, 0);
229 force_write(scoreboard_fd, &ap_scoreboard_image->global,
230 sizeof ap_scoreboard_image->global);
234 AP_DECLARE(void) ap_increment_counts(int child_num, int thread_num, request_rec *r)
238 ws = &ap_scoreboard_image->servers[child_num][thread_num];
244 ws->my_access_count++;
246 ws->bytes_served += r->bytes_sent;
247 ws->my_bytes_served += r->bytes_sent;
248 ws->conn_bytes += r->bytes_sent;
250 put_scoreboard_info(child_num, thread_num, ws);
253 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid)
256 int max_daemons_limit;
258 ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_daemons_limit);
260 for (i = 0; i < max_daemons_limit; ++i)
261 if (ap_scoreboard_image->parent[i].pid == pid->pid)
267 int ap_update_child_status(int child_num, int thread_num, int status, request_rec *r)
276 ws = &ap_scoreboard_image->servers[child_num][thread_num];
277 old_status = ws->status;
280 ps = &ap_scoreboard_image->parent[child_num];
282 if ((status == SERVER_READY || status == SERVER_ACCEPTING)
283 && old_status == SERVER_STARTING) {
284 ws->thread_num = child_num * HARD_SERVER_LIMIT + thread_num;
285 ps->generation = ap_my_generation;
286 ps->worker_threads = ap_threads_per_child;
289 if (ap_extended_status) {
290 ws->last_used = apr_time_now();
291 if (status == SERVER_READY || status == SERVER_DEAD) {
293 * Reset individual counters
295 if (status == SERVER_DEAD) {
296 ws->my_access_count = 0L;
297 ws->my_bytes_served = 0L;
299 ws->conn_count = (unsigned short) 0;
300 ws->conn_bytes = (unsigned long) 0;
303 conn_rec *c = r->connection;
304 apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
305 REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
306 if (r->the_request == NULL) {
307 apr_cpystrn(ws->request, "NULL", sizeof(ws->request));
308 } else if (r->parsed_uri.password == NULL) {
309 apr_cpystrn(ws->request, r->the_request, sizeof(ws->request));
311 /* Don't reveal the password in the server-status view */
312 apr_cpystrn(ws->request, apr_pstrcat(r->pool, r->method, " ",
313 ap_unparse_uri_components(r->pool, &r->parsed_uri, UNP_OMITPASSWORD),
314 r->assbackwards ? NULL : " ", r->protocol, NULL),
315 sizeof(ws->request));
317 ws->vhostrec = r->server;
321 put_scoreboard_info(child_num, thread_num, ws);
325 void ap_time_process_request(int child_num, int thread_num, int status)
332 ws = &ap_scoreboard_image->servers[child_num][thread_num];
334 if (status == START_PREQUEST) {
335 ws->start_time = apr_time_now();
337 else if (status == STOP_PREQUEST) {
338 ws->stop_time = apr_time_now();
340 put_scoreboard_info(child_num, thread_num, ws);