1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_strings.h"
19 #include "apr_portable.h"
22 #define APR_WANT_STRFUNC
25 #if APR_HAVE_SYS_TYPES_H
26 #include <sys/types.h>
29 #include "ap_config.h"
32 #include "http_main.h"
33 #include "http_core.h"
34 #include "http_config.h"
38 #include "scoreboard.h"
40 AP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
41 AP_DECLARE_DATA const char *ap_scoreboard_fname = NULL;
42 AP_DECLARE_DATA int ap_extended_status = 0;
44 #if APR_HAS_SHARED_MEMORY
49 static /* but must be exported to mpm_winnt */
51 apr_shm_t *ap_scoreboard_shm = NULL;
56 APR_HOOK_LINK(pre_mpm)
59 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_mpm,
60 (apr_pool_t *p, ap_scoreboard_e sb_type),
61 (p, sb_type),OK,DECLINED)
63 struct ap_sb_handle_t {
68 static int server_limit, thread_limit;
69 static apr_size_t scoreboard_size;
73 * This function should be renamed to cleanup_shared
74 * and it should handle cleaning up a scoreboard shared
75 * between processes using any form of IPC (file, shared memory
76 * segment, etc.). Leave it as is now because it is being used
79 static apr_status_t ap_cleanup_shared_mem(void *d)
81 #if APR_HAS_SHARED_MEMORY
82 free(ap_scoreboard_image);
83 ap_scoreboard_image = NULL;
84 apr_shm_destroy(ap_scoreboard_shm);
89 AP_DECLARE(int) ap_calc_scoreboard_size(void)
91 ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
92 ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
93 scoreboard_size = sizeof(global_score);
94 scoreboard_size += sizeof(process_score) * server_limit;
95 scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
96 return scoreboard_size;
99 void ap_init_scoreboard(void *shared_score)
104 ap_calc_scoreboard_size();
105 ap_scoreboard_image =
106 calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *));
107 more_storage = shared_score;
108 ap_scoreboard_image->global = (global_score *)more_storage;
109 more_storage += sizeof(global_score);
110 ap_scoreboard_image->parent = (process_score *)more_storage;
111 more_storage += sizeof(process_score) * server_limit;
112 ap_scoreboard_image->servers =
113 (worker_score **)((char*)ap_scoreboard_image + sizeof(scoreboard));
114 for (i = 0; i < server_limit; i++) {
115 ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
116 more_storage += thread_limit * sizeof(worker_score);
118 ap_assert(more_storage == (char*)shared_score + scoreboard_size);
119 ap_scoreboard_image->global->server_limit = server_limit;
120 ap_scoreboard_image->global->thread_limit = thread_limit;
124 * Create a name-based scoreboard in the given pool using the
127 static apr_status_t create_namebased_scoreboard(apr_pool_t *pool,
130 #if APR_HAS_SHARED_MEMORY
133 /* The shared memory file must not exist before we create the
135 apr_file_remove(fname, pool); /* ignore errors */
137 rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, fname, pool);
138 if (rv != APR_SUCCESS) {
139 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
140 "unable to create scoreboard \"%s\" "
141 "(name-based shared memory failure)", fname);
144 #endif /* APR_HAS_SHARED_MEMORY */
148 /* ToDo: This function should be made to handle setting up
149 * a scoreboard shared between processes using any IPC technique,
150 * not just a shared memory segment
152 static apr_status_t open_scoreboard(apr_pool_t *pconf)
154 #if APR_HAS_SHARED_MEMORY
157 apr_pool_t *global_pool;
159 /* We don't want to have to recreate the scoreboard after
160 * restarts, so we'll create a global pool and never clean it.
162 rv = apr_pool_create(&global_pool, NULL);
163 if (rv != APR_SUCCESS) {
164 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
165 "Fatal error: unable to create global pool "
166 "for use with by the scoreboard");
170 /* The config says to create a name-based shmem */
171 if (ap_scoreboard_fname) {
172 /* make sure it's an absolute pathname */
173 fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
175 ap_log_error(APLOG_MARK, APLOG_CRIT, APR_EBADPATH, NULL,
176 "Fatal error: Invalid Scoreboard path %s",
177 ap_scoreboard_fname);
180 return create_namebased_scoreboard(global_pool, fname);
182 else { /* config didn't specify, we get to choose shmem type */
183 rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, NULL,
184 global_pool); /* anonymous shared memory */
185 if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
186 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
187 "Unable to create scoreboard "
188 "(anonymous shared memory failure)");
191 /* Make up a filename and do name-based shmem */
192 else if (rv == APR_ENOTIMPL) {
193 /* Make sure it's an absolute pathname */
194 ap_scoreboard_fname = DEFAULT_SCOREBOARD;
195 fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
197 return create_namebased_scoreboard(global_pool, fname);
200 #endif /* APR_HAS_SHARED_MEMORY */
204 /* If detach is non-zero, this is a seperate child process,
205 * if zero, it is a forked child.
207 apr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached)
209 #if APR_HAS_SHARED_MEMORY
213 if (apr_shm_size_get(ap_scoreboard_shm) < scoreboard_size) {
214 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
215 "Fatal error: shared scoreboard too small for child!");
216 apr_shm_detach(ap_scoreboard_shm);
217 ap_scoreboard_shm = NULL;
220 /* everything will be cleared shortly */
222 *shm = ap_scoreboard_shm;
228 apr_status_t ap_cleanup_scoreboard(void *d)
230 if (ap_scoreboard_image == NULL) {
233 if (ap_scoreboard_image->global->sb_type == SB_SHARED) {
234 ap_cleanup_shared_mem(NULL);
237 free(ap_scoreboard_image->global);
238 free(ap_scoreboard_image);
239 ap_scoreboard_image = NULL;
244 /* Create or reinit an existing scoreboard. The MPM can control whether
245 * the scoreboard is shared across multiple processes or not
247 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
251 #if APR_HAS_SHARED_MEMORY
255 if (ap_scoreboard_image) {
256 running_gen = ap_scoreboard_image->global->running_generation;
257 ap_scoreboard_image->global->restart_time = apr_time_now();
258 memset(ap_scoreboard_image->parent, 0,
259 sizeof(process_score) * server_limit);
260 for (i = 0; i < server_limit; i++) {
261 memset(ap_scoreboard_image->servers[i], 0,
262 sizeof(worker_score) * thread_limit);
267 ap_calc_scoreboard_size();
268 #if APR_HAS_SHARED_MEMORY
269 if (sb_type == SB_SHARED) {
271 rv = open_scoreboard(p);
272 if (rv || !(sb_shared = apr_shm_baseaddr_get(ap_scoreboard_shm))) {
273 return HTTP_INTERNAL_SERVER_ERROR;
275 memset(sb_shared, 0, scoreboard_size);
276 ap_init_scoreboard(sb_shared);
281 /* A simple malloc will suffice */
282 void *sb_mem = calloc(1, scoreboard_size);
283 if (sb_mem == NULL) {
284 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
285 "(%d)%s: cannot allocate scoreboard",
286 errno, strerror(errno));
287 return HTTP_INTERNAL_SERVER_ERROR;
289 ap_init_scoreboard(sb_mem);
292 ap_scoreboard_image->global->sb_type = sb_type;
293 ap_scoreboard_image->global->running_generation = running_gen;
294 ap_scoreboard_image->global->restart_time = apr_time_now();
296 apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
301 /* Routines called to deal with the scoreboard image
302 * --- note that we do *not* need write locks, since update_child_status
303 * only updates a *single* record in place, and only one process writes to
304 * a given scoreboard slot at a time (either the child process owning that
305 * slot, or the parent, noting that the child has died).
307 * As a final note --- setting the score entry to getpid() is always safe,
308 * since when the parent is writing an entry, it's only noting SERVER_DEAD
312 AP_DECLARE(int) ap_exists_scoreboard_image(void)
314 return (ap_scoreboard_image ? 1 : 0);
317 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
321 ws = &ap_scoreboard_image->servers[sb->child_num][sb->thread_num];
327 ws->my_access_count++;
329 ws->bytes_served += r->bytes_sent;
330 ws->my_bytes_served += r->bytes_sent;
331 ws->conn_bytes += r->bytes_sent;
334 AP_DECLARE(int) find_child_by_pid(apr_proc_t *pid)
337 int max_daemons_limit;
339 ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_daemons_limit);
341 for (i = 0; i < max_daemons_limit; ++i) {
342 if (ap_scoreboard_image->parent[i].pid == pid->pid) {
350 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
351 int child_num, int thread_num)
353 *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
354 (*new_sbh)->child_num = child_num;
355 (*new_sbh)->thread_num = thread_num;
358 AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
371 ws = &ap_scoreboard_image->servers[child_num][thread_num];
372 old_status = ws->status;
375 ps = &ap_scoreboard_image->parent[child_num];
377 if (status == SERVER_READY
378 && old_status == SERVER_STARTING) {
379 ws->thread_num = child_num * thread_limit + thread_num;
380 ps->generation = ap_my_generation;
383 if (ap_extended_status) {
384 ws->last_used = apr_time_now();
385 if (status == SERVER_READY || status == SERVER_DEAD) {
387 * Reset individual counters
389 if (status == SERVER_DEAD) {
390 ws->my_access_count = 0L;
391 ws->my_bytes_served = 0L;
397 conn_rec *c = r->connection;
398 apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
399 REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
400 if (r->the_request == NULL) {
401 apr_cpystrn(ws->request, "NULL", sizeof(ws->request));
402 } else if (r->parsed_uri.password == NULL) {
403 apr_cpystrn(ws->request, r->the_request, sizeof(ws->request));
405 /* Don't reveal the password in the server-status view */
406 apr_cpystrn(ws->request, apr_pstrcat(r->pool, r->method, " ",
407 apr_uri_unparse(r->pool, &r->parsed_uri,
408 APR_URI_UNP_OMITPASSWORD),
409 r->assbackwards ? NULL : " ", r->protocol, NULL),
410 sizeof(ws->request));
412 apr_cpystrn(ws->vhost, r->server->server_hostname,
420 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status,
423 return ap_update_child_status_from_indexes(sbh->child_num, sbh->thread_num,
427 void ap_time_process_request(ap_sb_handle_t *sbh, int status)
431 if (sbh->child_num < 0) {
435 ws = &ap_scoreboard_image->servers[sbh->child_num][sbh->thread_num];
437 if (status == START_PREQUEST) {
438 ws->start_time = apr_time_now();
440 else if (status == STOP_PREQUEST) {
441 ws->stop_time = apr_time_now();
445 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(int x, int y)
447 if (((x < 0) || (server_limit < x)) ||
448 ((y < 0) || (thread_limit < y))) {
449 return(NULL); /* Out of range */
451 return &ap_scoreboard_image->servers[x][y];
454 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x)
456 if ((x < 0) || (server_limit < x)) {
457 return(NULL); /* Out of range */
459 return &ap_scoreboard_image->parent[x];
462 AP_DECLARE(global_score *) ap_get_scoreboard_global()
464 return ap_scoreboard_image->global;