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"
35 #include "http_protocol.h"
38 #include "scoreboard.h"
40 /* we know core's module_index is 0 */
41 #undef APLOG_MODULE_INDEX
42 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
44 AP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
45 AP_DECLARE_DATA const char *ap_scoreboard_fname = NULL;
46 static ap_scoreboard_e scoreboard_type;
48 const char * ap_set_scoreboard(cmd_parms *cmd, void *dummy,
51 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
56 ap_scoreboard_fname = arg;
60 /* Default to false when mod_status is not loaded */
61 AP_DECLARE_DATA int ap_extended_status = 0;
63 const char *ap_set_extended_status(cmd_parms *cmd, void *dummy, int arg)
65 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
69 ap_extended_status = arg;
73 AP_DECLARE_DATA int ap_mod_status_reqtail = 0;
75 const char *ap_set_reqtail(cmd_parms *cmd, void *dummy, int arg)
77 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
81 ap_mod_status_reqtail = arg;
85 #if APR_HAS_SHARED_MEMORY
90 static /* but must be exported to mpm_winnt */
92 apr_shm_t *ap_scoreboard_shm = NULL;
97 APR_HOOK_LINK(pre_mpm)
100 AP_IMPLEMENT_HOOK_RUN_ALL(int,pre_mpm,
101 (apr_pool_t *p, ap_scoreboard_e sb_type),
102 (p, sb_type),OK,DECLINED)
104 static APR_OPTIONAL_FN_TYPE(ap_logio_get_last_bytes)
105 *pfn_ap_logio_get_last_bytes;
107 struct ap_sb_handle_t {
112 static int server_limit, thread_limit;
113 static apr_size_t scoreboard_size;
117 * This function should be renamed to cleanup_shared
118 * and it should handle cleaning up a scoreboard shared
119 * between processes using any form of IPC (file, shared memory
120 * segment, etc.). Leave it as is now because it is being used
123 static apr_status_t ap_cleanup_shared_mem(void *d)
125 #if APR_HAS_SHARED_MEMORY
126 free(ap_scoreboard_image);
127 ap_scoreboard_image = NULL;
128 apr_shm_destroy(ap_scoreboard_shm);
133 #define SIZE_OF_scoreboard APR_ALIGN_DEFAULT(sizeof(scoreboard))
134 #define SIZE_OF_global_score APR_ALIGN_DEFAULT(sizeof(global_score))
135 #define SIZE_OF_process_score APR_ALIGN_DEFAULT(sizeof(process_score))
136 #define SIZE_OF_worker_score APR_ALIGN_DEFAULT(sizeof(worker_score))
138 AP_DECLARE(int) ap_calc_scoreboard_size(void)
140 ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
141 ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
143 scoreboard_size = SIZE_OF_global_score;
144 scoreboard_size += SIZE_OF_process_score * server_limit;
145 scoreboard_size += SIZE_OF_worker_score * server_limit * thread_limit;
147 return scoreboard_size;
150 AP_DECLARE(void) ap_init_scoreboard(void *shared_score)
155 pfn_ap_logio_get_last_bytes = APR_RETRIEVE_OPTIONAL_FN(ap_logio_get_last_bytes);
160 ap_calc_scoreboard_size();
161 ap_scoreboard_image =
162 ap_calloc(1, SIZE_OF_scoreboard + server_limit * sizeof(worker_score *));
163 more_storage = shared_score;
164 ap_scoreboard_image->global = (global_score *)more_storage;
165 more_storage += SIZE_OF_global_score;
166 ap_scoreboard_image->parent = (process_score *)more_storage;
167 more_storage += SIZE_OF_process_score * server_limit;
168 ap_scoreboard_image->servers =
169 (worker_score **)((char*)ap_scoreboard_image + SIZE_OF_scoreboard);
170 for (i = 0; i < server_limit; i++) {
171 ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
172 more_storage += thread_limit * SIZE_OF_worker_score;
174 ap_assert(more_storage == (char*)shared_score + scoreboard_size);
175 ap_scoreboard_image->global->server_limit = server_limit;
176 ap_scoreboard_image->global->thread_limit = thread_limit;
180 * Create a name-based scoreboard in the given pool using the
183 static apr_status_t create_namebased_scoreboard(apr_pool_t *pool,
186 #if APR_HAS_SHARED_MEMORY
189 /* The shared memory file must not exist before we create the
191 apr_shm_remove(fname, pool); /* ignore errors */
193 rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, fname, pool);
194 if (rv != APR_SUCCESS) {
195 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00001)
196 "unable to create or access scoreboard \"%s\" "
197 "(name-based shared memory failure)", fname);
200 #endif /* APR_HAS_SHARED_MEMORY */
204 /* ToDo: This function should be made to handle setting up
205 * a scoreboard shared between processes using any IPC technique,
206 * not just a shared memory segment
208 static apr_status_t open_scoreboard(apr_pool_t *pconf)
210 #if APR_HAS_SHARED_MEMORY
213 apr_pool_t *global_pool;
215 /* We don't want to have to recreate the scoreboard after
216 * restarts, so we'll create a global pool and never clean it.
218 rv = apr_pool_create(&global_pool, NULL);
219 if (rv != APR_SUCCESS) {
220 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00002)
221 "Fatal error: unable to create global pool "
222 "for use by the scoreboard");
226 /* The config says to create a name-based shmem */
227 if (ap_scoreboard_fname) {
228 /* make sure it's an absolute pathname */
229 fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
231 ap_log_error(APLOG_MARK, APLOG_CRIT, APR_EBADPATH, ap_server_conf, APLOGNO(00003)
232 "Fatal error: Invalid Scoreboard path %s",
233 ap_scoreboard_fname);
236 return create_namebased_scoreboard(global_pool, fname);
238 else { /* config didn't specify, we get to choose shmem type */
239 rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, NULL,
240 global_pool); /* anonymous shared memory */
241 if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
242 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00004)
243 "Unable to create or access scoreboard "
244 "(anonymous shared memory failure)");
247 /* Make up a filename and do name-based shmem */
248 else if (rv == APR_ENOTIMPL) {
249 /* Make sure it's an absolute pathname */
250 ap_scoreboard_fname = DEFAULT_SCOREBOARD;
251 fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
253 return create_namebased_scoreboard(global_pool, fname);
256 #endif /* APR_HAS_SHARED_MEMORY */
260 /* If detach is non-zero, this is a separate child process,
261 * if zero, it is a forked child.
263 AP_DECLARE(apr_status_t) ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm,
266 #if APR_HAS_SHARED_MEMORY
270 if (apr_shm_size_get(ap_scoreboard_shm) < scoreboard_size) {
271 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ap_server_conf, APLOGNO(00005)
272 "Fatal error: shared scoreboard too small for child!");
273 apr_shm_detach(ap_scoreboard_shm);
274 ap_scoreboard_shm = NULL;
277 /* everything will be cleared shortly */
279 *shm = ap_scoreboard_shm;
285 apr_status_t ap_cleanup_scoreboard(void *d)
287 if (ap_scoreboard_image == NULL) {
290 if (scoreboard_type == SB_SHARED) {
291 ap_cleanup_shared_mem(NULL);
294 free(ap_scoreboard_image->global);
295 free(ap_scoreboard_image);
296 ap_scoreboard_image = NULL;
301 /* Create or reinit an existing scoreboard. The MPM can control whether
302 * the scoreboard is shared across multiple processes or not
304 int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
307 #if APR_HAS_SHARED_MEMORY
311 if (ap_scoreboard_image) {
312 ap_scoreboard_image->global->restart_time = apr_time_now();
313 memset(ap_scoreboard_image->parent, 0,
314 SIZE_OF_process_score * server_limit);
315 for (i = 0; i < server_limit; i++) {
316 memset(ap_scoreboard_image->servers[i], 0,
317 SIZE_OF_worker_score * thread_limit);
319 ap_init_scoreboard(NULL);
323 ap_calc_scoreboard_size();
324 #if APR_HAS_SHARED_MEMORY
325 if (sb_type == SB_SHARED) {
327 rv = open_scoreboard(p);
328 if (rv || !(sb_shared = apr_shm_baseaddr_get(ap_scoreboard_shm))) {
329 return HTTP_INTERNAL_SERVER_ERROR;
331 memset(sb_shared, 0, scoreboard_size);
332 ap_init_scoreboard(sb_shared);
337 /* A simple malloc will suffice */
338 void *sb_mem = ap_calloc(1, scoreboard_size);
339 ap_init_scoreboard(sb_mem);
342 scoreboard_type = sb_type;
343 ap_scoreboard_image->global->running_generation = 0;
344 ap_scoreboard_image->global->restart_time = apr_time_now();
346 apr_pool_cleanup_register(p, NULL, ap_cleanup_scoreboard, apr_pool_cleanup_null);
351 /* Routines called to deal with the scoreboard image
352 * --- note that we do *not* need write locks, since update_child_status
353 * only updates a *single* record in place, and only one process writes to
354 * a given scoreboard slot at a time (either the child process owning that
355 * slot, or the parent, noting that the child has died).
357 * As a final note --- setting the score entry to getpid() is always safe,
358 * since when the parent is writing an entry, it's only noting SERVER_DEAD
362 AP_DECLARE(int) ap_exists_scoreboard_image(void)
364 return (ap_scoreboard_image ? 1 : 0);
367 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
375 ws = &ap_scoreboard_image->servers[sb->child_num][sb->thread_num];
376 if (pfn_ap_logio_get_last_bytes != NULL) {
377 bytes = pfn_ap_logio_get_last_bytes(r->connection);
379 else if (r->method_number == M_GET && r->method[0] == 'H') {
383 bytes = r->bytes_sent;
390 ws->my_access_count++;
392 ws->bytes_served += bytes;
393 ws->my_bytes_served += bytes;
394 ws->conn_bytes += bytes;
397 AP_DECLARE(int) ap_find_child_by_pid(apr_proc_t *pid)
400 int max_daemons_limit = 0;
402 ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons_limit);
404 for (i = 0; i < max_daemons_limit; ++i) {
405 if (ap_scoreboard_image->parent[i].pid == pid->pid) {
413 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
414 int child_num, int thread_num)
416 *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
417 (*new_sbh)->child_num = child_num;
418 (*new_sbh)->thread_num = thread_num;
421 static void copy_request(char *rbuf, apr_size_t rbuflen, request_rec *r)
425 if (r->the_request == NULL) {
426 apr_cpystrn(rbuf, "NULL", rbuflen);
427 return; /* short circuit below */
430 if (r->parsed_uri.password == NULL) {
434 /* Don't reveal the password in the server-status view */
435 p = apr_pstrcat(r->pool, r->method, " ",
436 apr_uri_unparse(r->pool, &r->parsed_uri,
437 APR_URI_UNP_OMITPASSWORD),
438 r->assbackwards ? NULL : " ", r->protocol, NULL);
441 /* now figure out if we copy over the 1st rbuflen chars or the last */
442 if (!ap_mod_status_reqtail) {
443 apr_cpystrn(rbuf, p, rbuflen);
446 apr_size_t slen = strlen(p);
447 if (slen < rbuflen) {
448 /* it all fits anyway */
449 apr_cpystrn(rbuf, p, rbuflen);
452 apr_cpystrn(rbuf, p+(slen-rbuflen+1), rbuflen);
457 static int update_child_status_internal(int child_num,
469 ws = &ap_scoreboard_image->servers[child_num][thread_num];
470 old_status = ws->status;
473 if (status == SERVER_READY
474 && old_status == SERVER_STARTING) {
475 process_score *ps = &ap_scoreboard_image->parent[child_num];
476 ws->thread_num = child_num * thread_limit + thread_num;
477 ap_mpm_query(AP_MPMQ_GENERATION, &mpm_generation);
478 ps->generation = mpm_generation;
481 if (ap_extended_status) {
484 if (status == SERVER_READY || status == SERVER_DEAD) {
486 * Reset individual counters
488 if (status == SERVER_DEAD) {
489 ws->my_access_count = 0L;
490 ws->my_bytes_served = 0L;
494 ws->last_used = apr_time_now();
498 apr_cpystrn(ws->request, descr, sizeof(ws->request));
501 copy_request(ws->request, sizeof(ws->request), r);
507 if (r && r->useragent_ip) {
508 if (!(val = ap_get_useragent_host(r, REMOTE_NOLOOKUP, NULL)))
509 apr_cpystrn(ws->client, r->useragent_ip, sizeof(ws->client));
511 apr_cpystrn(ws->client, val, sizeof(ws->client));
514 if (!(val = ap_get_remote_host(c, c->base_server->lookup_defaults,
515 REMOTE_NOLOOKUP, NULL)))
516 apr_cpystrn(ws->client, c->client_ip, sizeof(ws->client));
518 apr_cpystrn(ws->client, val, sizeof(ws->client));
523 apr_snprintf(ws->vhost, sizeof(ws->vhost), "%s:%d",
524 s->server_hostname, c->local_addr->port);
527 apr_cpystrn(ws->vhost, s->server_hostname, sizeof(ws->vhost));
535 val = ap_get_protocol(c);
536 apr_cpystrn(ws->protocol, val, sizeof(ws->protocol));
543 AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
552 return update_child_status_internal(child_num, thread_num, status,
553 r ? r->connection : NULL,
554 r ? r->server : NULL,
558 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status,
561 if (!sbh || (sbh->child_num < 0))
564 return update_child_status_internal(sbh->child_num, sbh->thread_num,
566 r ? r->connection : NULL,
567 r ? r->server : NULL,
571 AP_DECLARE(int) ap_update_child_status_from_conn(ap_sb_handle_t *sbh, int status,
574 if (!sbh || (sbh->child_num < 0))
577 return update_child_status_internal(sbh->child_num, sbh->thread_num,
578 status, c, NULL, NULL, NULL);
581 AP_DECLARE(int) ap_update_child_status_from_server(ap_sb_handle_t *sbh, int status,
582 conn_rec *c, server_rec *s)
584 if (!sbh || (sbh->child_num < 0))
587 return update_child_status_internal(sbh->child_num, sbh->thread_num,
588 status, c, s, NULL, NULL);
591 AP_DECLARE(int) ap_update_child_status_descr(ap_sb_handle_t *sbh, int status, const char *descr)
593 if (!sbh || (sbh->child_num < 0))
596 return update_child_status_internal(sbh->child_num, sbh->thread_num,
597 status, NULL, NULL, NULL, descr);
600 AP_DECLARE(void) ap_time_process_request(ap_sb_handle_t *sbh, int status)
607 if (sbh->child_num < 0) {
611 ws = &ap_scoreboard_image->servers[sbh->child_num][sbh->thread_num];
613 if (status == START_PREQUEST) {
614 ws->start_time = ws->last_used = apr_time_now();
616 else if (status == STOP_PREQUEST) {
617 ws->stop_time = ws->last_used = apr_time_now();
621 AP_DECLARE(worker_score *) ap_get_scoreboard_worker_from_indexes(int x, int y)
623 if (((x < 0) || (x >= server_limit)) ||
624 ((y < 0) || (y >= thread_limit))) {
625 return(NULL); /* Out of range */
627 return &ap_scoreboard_image->servers[x][y];
630 AP_DECLARE(worker_score *) ap_get_scoreboard_worker(ap_sb_handle_t *sbh)
635 return ap_get_scoreboard_worker_from_indexes(sbh->child_num,
639 AP_DECLARE(void) ap_copy_scoreboard_worker(worker_score *dest,
643 worker_score *ws = ap_get_scoreboard_worker_from_indexes(child_num, thread_num);
645 memcpy(dest, ws, sizeof *ws);
647 /* For extra safety, NUL-terminate the strings returned, though it
648 * should be true those last bytes are always zero anyway. */
649 dest->client[sizeof(dest->client) - 1] = '\0';
650 dest->request[sizeof(dest->request) - 1] = '\0';
651 dest->vhost[sizeof(dest->vhost) - 1] = '\0';
652 dest->protocol[sizeof(dest->protocol) - 1] = '\0';
655 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x)
657 if ((x < 0) || (x >= server_limit)) {
658 return(NULL); /* Out of range */
660 return &ap_scoreboard_image->parent[x];
663 AP_DECLARE(global_score *) ap_get_scoreboard_global()
665 return ap_scoreboard_image->global;