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 #include "apr_strings.h"
60 #include "apr_portable.h"
61 #include "ap_config.h"
64 #include "http_main.h"
65 #include "http_core.h"
66 #include "http_config.h"
69 #include "scoreboard.h"
70 #ifdef HAVE_SYS_TYPES_H
71 #include <sys/types.h>
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;
79 #if APR_HAS_SHARED_MEMORY
80 #include "apr_shmem.h"
81 static apr_shmem_t *scoreboard_shm = NULL;
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
91 static apr_status_t ap_cleanup_shared_mem(void *d)
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);
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
105 static void setup_shared(apr_pool_t *p)
107 #if APR_HAS_SHARED_MEMORY
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);
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",
125 perror(buf); /* o.k. since MM sets errno */
126 apr_shm_destroy(scoreboard_shm);
129 ap_scoreboard_image->global.running_generation = 0;
133 AP_DECLARE(void) reopen_scoreboard(apr_pool_t *p)
137 /* ap_cleanup_scoreboard
140 apr_status_t ap_cleanup_scoreboard(void *d) {
141 if (ap_scoreboard_image == NULL)
143 if (ap_scoreboard_image->global.sb_type == SB_SHARED) {
144 ap_cleanup_shared_mem(NULL);
147 free(ap_scoreboard_image);
148 ap_scoreboard_image = NULL;
153 /* ap_create_scoreboard(apr_pool_t*, ap_scoreboard_e t)
155 * Create or reinit an existing scoreboard. The MPM can control whether
156 * the scoreboard is shared across multiple processes or not
158 AP_DECLARE(void) ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
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) {
168 /* A simple malloc will suffice */
170 ap_scoreboard_image = (scoreboard *) malloc(SCOREBOARD_SIZE);
171 if (ap_scoreboard_image == NULL) {
172 apr_snprintf(buf, sizeof(buf), "%s: cannot allocate scoreboard",
174 perror(buf); /* o.k. since MM sets errno */
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);
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).
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
197 void ap_sync_scoreboard_image(void)
201 AP_DECLARE(int) ap_exists_scoreboard_image(void)
203 return (ap_scoreboard_image ? 1 : 0);
206 static apr_inline void put_scoreboard_info(int child_num, int thread_num,
207 short_score *new_score_rec)
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));
216 void update_scoreboard_global(void)
218 #ifdef SCOREBOARD_FILE
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);
226 void increment_counts(int child_num, int thread_num, request_rec *r)
230 ss = &ap_scoreboard_image->servers[child_num][thread_num];
236 ss->my_access_count++;
238 ss->bytes_served += r->bytes_sent;
239 ss->my_bytes_served += r->bytes_sent;
240 ss->conn_bytes += r->bytes_sent;
242 put_scoreboard_info(child_num, thread_num, ss);
245 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid)
248 int max_daemons_limit = ap_get_max_daemons();
250 for (i = 0; i < max_daemons_limit; ++i)
251 if (ap_scoreboard_image->parent[i].pid == pid->pid)
257 int ap_update_child_status(int child_num, int thread_num, int status, request_rec *r)
266 ss = &ap_scoreboard_image->servers[child_num][thread_num];
267 old_status = ss->status;
270 ps = &ap_scoreboard_image->parent[child_num];
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;
278 if (ap_extended_status) {
279 if (status == SERVER_READY || status == SERVER_DEAD) {
281 * Reset individual counters
283 if (status == SERVER_DEAD) {
284 ss->my_access_count = 0L;
285 ss->my_bytes_served = 0L;
287 ss->conn_count = (unsigned short) 0;
288 ss->conn_bytes = (unsigned long) 0;
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));
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));
305 ss->vhostrec = r->server;
309 put_scoreboard_info(child_num, thread_num, ss);
313 void ap_time_process_request(int child_num, int thread_num, int status)
320 ss = &ap_scoreboard_image->servers[child_num][thread_num];
322 if (status == START_PREQUEST) {
323 ss->start_time = apr_time_now();
325 else if (status == STOP_PREQUEST) {
326 ss->stop_time = apr_time_now();
328 put_scoreboard_info(child_num, thread_num, ss);