]> granicus.if.org Git - apache/blobdiff - server/scoreboard.c
Help doc writer to spot places where:
[apache] / server / scoreboard.c
index bbf233767cee1bab594ff4d500a2a4ed16694f2f..a028447ffbd0f31ba3f72da53c390d064d627006 100644 (file)
 #include "http_main.h"
 #include "http_core.h"
 #include "http_config.h"
+#include "http_protocol.h"
 #include "ap_mpm.h"
 
 #include "scoreboard.h"
 
-APLOG_USE_MODULE(core);
+/* we know core's module_index is 0 */
+#undef APLOG_MODULE_INDEX
+#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
 
 AP_DECLARE_DATA scoreboard *ap_scoreboard_image = NULL;
 AP_DECLARE_DATA const char *ap_scoreboard_fname = NULL;
+static ap_scoreboard_e scoreboard_type;
 
 const char * ap_set_scoreboard(cmd_parms *cmd, void *dummy,
                                const char *arg)
@@ -126,38 +130,46 @@ static apr_status_t ap_cleanup_shared_mem(void *d)
     return APR_SUCCESS;
 }
 
+#define SIZE_OF_scoreboard    APR_ALIGN_DEFAULT(sizeof(scoreboard))
+#define SIZE_OF_global_score  APR_ALIGN_DEFAULT(sizeof(global_score))
+#define SIZE_OF_process_score APR_ALIGN_DEFAULT(sizeof(process_score))
+#define SIZE_OF_worker_score  APR_ALIGN_DEFAULT(sizeof(worker_score))
+
 AP_DECLARE(int) ap_calc_scoreboard_size(void)
 {
     ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &thread_limit);
     ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &server_limit);
 
-    scoreboard_size = sizeof(global_score);
-    scoreboard_size += sizeof(process_score) * server_limit;
-    scoreboard_size += sizeof(worker_score) * server_limit * thread_limit;
-
-    pfn_ap_logio_get_last_bytes = APR_RETRIEVE_OPTIONAL_FN(ap_logio_get_last_bytes);
+    scoreboard_size  = SIZE_OF_global_score;
+    scoreboard_size += SIZE_OF_process_score * server_limit;
+    scoreboard_size += SIZE_OF_worker_score * server_limit * thread_limit;
 
     return scoreboard_size;
 }
 
-void ap_init_scoreboard(void *shared_score)
+AP_DECLARE(void) ap_init_scoreboard(void *shared_score)
 {
     char *more_storage;
     int i;
 
+    pfn_ap_logio_get_last_bytes = APR_RETRIEVE_OPTIONAL_FN(ap_logio_get_last_bytes);
+    if (!shared_score) {
+        return;
+    }
+    
     ap_calc_scoreboard_size();
     ap_scoreboard_image =
-        calloc(1, sizeof(scoreboard) + server_limit * sizeof(worker_score *));
+        ap_calloc(1, SIZE_OF_scoreboard + server_limit * sizeof(worker_score *));
     more_storage = shared_score;
     ap_scoreboard_image->global = (global_score *)more_storage;
-    more_storage += sizeof(global_score);
+    more_storage += SIZE_OF_global_score;
     ap_scoreboard_image->parent = (process_score *)more_storage;
-    more_storage += sizeof(process_score) * server_limit;
+    more_storage += SIZE_OF_process_score * server_limit;
     ap_scoreboard_image->servers =
-        (worker_score **)((char*)ap_scoreboard_image + sizeof(scoreboard));
+        (worker_score **)((char*)ap_scoreboard_image + SIZE_OF_scoreboard);
     for (i = 0; i < server_limit; i++) {
         ap_scoreboard_image->servers[i] = (worker_score *)more_storage;
-        more_storage += thread_limit * sizeof(worker_score);
+        more_storage += thread_limit * SIZE_OF_worker_score;
     }
     ap_assert(more_storage == (char*)shared_score + scoreboard_size);
     ap_scoreboard_image->global->server_limit = server_limit;
@@ -180,7 +192,7 @@ static apr_status_t create_namebased_scoreboard(apr_pool_t *pool,
 
     rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, fname, pool);
     if (rv != APR_SUCCESS) {
-        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00001)
                      "unable to create or access scoreboard \"%s\" "
                      "(name-based shared memory failure)", fname);
         return rv;
@@ -198,25 +210,18 @@ static apr_status_t open_scoreboard(apr_pool_t *pconf)
 #if APR_HAS_SHARED_MEMORY
     apr_status_t rv;
     char *fname = NULL;
-    apr_pool_t *global_pool;
-
-    /* We don't want to have to recreate the scoreboard after
-     * restarts, so we'll create a global pool and never clean it.
-     */
-    rv = apr_pool_create(&global_pool, NULL);
-    if (rv != APR_SUCCESS) {
-        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
-                     "Fatal error: unable to create global pool "
-                     "for use by the scoreboard");
-        return rv;
-    }
+    apr_pool_t *global_pool = apr_pool_parent_get(pconf);
 
+    /* If this is not passed pconf, or pconf is no longer a direct
+     * child of a global pool, this should change... */
+    AP_DEBUG_ASSERT(apr_pool_parent_get(global_pool) == NULL);
+    
     /* The config says to create a name-based shmem */
     if (ap_scoreboard_fname) {
         /* make sure it's an absolute pathname */
-        fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
+        fname = ap_runtime_dir_relative(pconf, ap_scoreboard_fname);
         if (!fname) {
-            ap_log_error(APLOG_MARK, APLOG_CRIT, APR_EBADPATH, NULL,
+            ap_log_error(APLOG_MARK, APLOG_CRIT, APR_EBADPATH, ap_server_conf, APLOGNO(00003)
                          "Fatal error: Invalid Scoreboard path %s",
                          ap_scoreboard_fname);
             return APR_EBADPATH;
@@ -227,7 +232,7 @@ static apr_status_t open_scoreboard(apr_pool_t *pconf)
         rv = apr_shm_create(&ap_scoreboard_shm, scoreboard_size, NULL,
                             global_pool); /* anonymous shared memory */
         if ((rv != APR_SUCCESS) && (rv != APR_ENOTIMPL)) {
-            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
+            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, ap_server_conf, APLOGNO(00004)
                          "Unable to create or access scoreboard "
                          "(anonymous shared memory failure)");
             return rv;
@@ -236,7 +241,7 @@ static apr_status_t open_scoreboard(apr_pool_t *pconf)
         else if (rv == APR_ENOTIMPL) {
             /* Make sure it's an absolute pathname */
             ap_scoreboard_fname = DEFAULT_SCOREBOARD;
-            fname = ap_server_root_relative(pconf, ap_scoreboard_fname);
+            fname = ap_runtime_dir_relative(pconf, ap_scoreboard_fname);
 
             return create_namebased_scoreboard(global_pool, fname);
         }
@@ -245,17 +250,18 @@ static apr_status_t open_scoreboard(apr_pool_t *pconf)
     return APR_SUCCESS;
 }
 
-/* If detach is non-zero, this is a seperate child process,
+/* If detach is non-zero, this is a separate child process,
  * if zero, it is a forked child.
  */
-apr_status_t ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm, int detached)
+AP_DECLARE(apr_status_t) ap_reopen_scoreboard(apr_pool_t *p, apr_shm_t **shm,
+                                              int detached)
 {
 #if APR_HAS_SHARED_MEMORY
     if (!detached) {
         return APR_SUCCESS;
     }
     if (apr_shm_size_get(ap_scoreboard_shm) < scoreboard_size) {
-        ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
+        ap_log_error(APLOG_MARK, APLOG_CRIT, 0, ap_server_conf, APLOGNO(00005)
                      "Fatal error: shared scoreboard too small for child!");
         apr_shm_detach(ap_scoreboard_shm);
         ap_scoreboard_shm = NULL;
@@ -274,7 +280,7 @@ apr_status_t ap_cleanup_scoreboard(void *d)
     if (ap_scoreboard_image == NULL) {
         return APR_SUCCESS;
     }
-    if (ap_scoreboard_image->global->sb_type == SB_SHARED) {
+    if (scoreboard_type == SB_SHARED) {
         ap_cleanup_shared_mem(NULL);
     }
     else {
@@ -295,16 +301,15 @@ int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
     apr_status_t rv;
 #endif
 
-    pfn_ap_logio_get_last_bytes = APR_RETRIEVE_OPTIONAL_FN(ap_logio_get_last_bytes);
-
     if (ap_scoreboard_image) {
         ap_scoreboard_image->global->restart_time = apr_time_now();
         memset(ap_scoreboard_image->parent, 0,
-               sizeof(process_score) * server_limit);
+               SIZE_OF_process_score * server_limit);
         for (i = 0; i < server_limit; i++) {
             memset(ap_scoreboard_image->servers[i], 0,
-                   sizeof(worker_score) * thread_limit);
+                   SIZE_OF_worker_score * thread_limit);
         }
+        ap_init_scoreboard(NULL);
         return OK;
     }
 
@@ -323,17 +328,11 @@ int ap_create_scoreboard(apr_pool_t *p, ap_scoreboard_e sb_type)
 #endif
     {
         /* A simple malloc will suffice */
-        void *sb_mem = calloc(1, scoreboard_size);
-        if (sb_mem == NULL) {
-            ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
-                         "(%d)%s: cannot allocate scoreboard",
-                         errno, strerror(errno));
-            return HTTP_INTERNAL_SERVER_ERROR;
-        }
+        void *sb_mem = ap_calloc(1, scoreboard_size);
         ap_init_scoreboard(sb_mem);
     }
 
-    ap_scoreboard_image->global->sb_type = sb_type;
+    scoreboard_type = sb_type;
     ap_scoreboard_image->global->running_generation = 0;
     ap_scoreboard_image->global->restart_time = apr_time_now();
 
@@ -358,6 +357,18 @@ AP_DECLARE(int) ap_exists_scoreboard_image(void)
     return (ap_scoreboard_image ? 1 : 0);
 }
 
+AP_DECLARE(void) ap_set_conn_count(ap_sb_handle_t *sb, request_rec *r, 
+                                   unsigned short conn_count)
+{
+    worker_score *ws;
+
+    if (!sb)
+        return;
+
+    ws = &ap_scoreboard_image->servers[sb->child_num][sb->thread_num];
+    ws->conn_count = conn_count;
+}
+
 AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
 {
     worker_score *ws;
@@ -391,9 +402,9 @@ AP_DECLARE(void) ap_increment_counts(ap_sb_handle_t *sb, request_rec *r)
 AP_DECLARE(int) ap_find_child_by_pid(apr_proc_t *pid)
 {
     int i;
-    int max_daemons_limit;
+    int max_daemons_limit = 0;
 
-    ap_mpm_query(AP_MPMQ_MAX_DAEMONS, &max_daemons_limit);
+    ap_mpm_query(AP_MPMQ_MAX_DAEMON_USED, &max_daemons_limit);
 
     for (i = 0; i < max_daemons_limit; ++i) {
         if (ap_scoreboard_image->parent[i].pid == pid->pid) {
@@ -404,12 +415,18 @@ AP_DECLARE(int) ap_find_child_by_pid(apr_proc_t *pid)
     return -1;
 }
 
+AP_DECLARE(void) ap_update_sb_handle(ap_sb_handle_t *sbh,
+                                     int child_num, int thread_num)
+{
+    sbh->child_num = child_num;
+    sbh->thread_num = thread_num;
+}
+
 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
                                      int child_num, int thread_num)
 {
     *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
-    (*new_sbh)->child_num = child_num;
-    (*new_sbh)->thread_num = thread_num;
+    ap_update_sb_handle(*new_sbh, child_num, thread_num);
 }
 
 static void copy_request(char *rbuf, apr_size_t rbuflen, request_rec *r)
@@ -452,28 +469,29 @@ static int update_child_status_internal(int child_num,
                                         int thread_num,
                                         int status,
                                         conn_rec *c,
-                                        request_rec *r)
+                                        server_rec *s,
+                                        request_rec *r,
+                                        const char *descr)
 {
     int old_status;
     worker_score *ws;
-    process_score *ps;
     int mpm_generation;
 
     ws = &ap_scoreboard_image->servers[child_num][thread_num];
     old_status = ws->status;
     ws->status = status;
-
-    ps = &ap_scoreboard_image->parent[child_num];
-
+    
     if (status == SERVER_READY
         && old_status == SERVER_STARTING) {
+        process_score *ps = &ap_scoreboard_image->parent[child_num];
         ws->thread_num = child_num * thread_limit + thread_num;
         ap_mpm_query(AP_MPMQ_GENERATION, &mpm_generation);
         ps->generation = mpm_generation;
     }
 
     if (ap_extended_status) {
-        ws->last_used = apr_time_now();
+        const char *val;
+        
         if (status == SERVER_READY || status == SERVER_DEAD) {
             /*
              * Reset individual counters
@@ -481,25 +499,67 @@ static int update_child_status_internal(int child_num,
             if (status == SERVER_DEAD) {
                 ws->my_access_count = 0L;
                 ws->my_bytes_served = 0L;
+#ifdef HAVE_TIMES
+                ws->times.tms_utime = 0;
+                ws->times.tms_stime = 0;
+                ws->times.tms_cutime = 0;
+                ws->times.tms_cstime = 0;
+#endif
             }
             ws->conn_count = 0;
             ws->conn_bytes = 0;
+            ws->last_used = apr_time_now();
         }
-        if (r) {
-            apr_cpystrn(ws->client, ap_get_remote_host(c, r->per_dir_config,
-                        REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
+
+        if (descr) {
+            apr_cpystrn(ws->request, descr, sizeof(ws->request));
+        }
+        else if (r) {
             copy_request(ws->request, sizeof(ws->request), r);
-            if (r->server) {
-               apr_cpystrn(ws->vhost, r->server->server_hostname,
-                            sizeof(ws->vhost));
-            }
         }
         else if (c) {
-            apr_cpystrn(ws->client, ap_get_remote_host(c, NULL,
-                        REMOTE_NOLOOKUP, NULL), sizeof(ws->client));
             ws->request[0]='\0';
+        }
+
+        if (r && r->useragent_ip) {
+            if (!(val = ap_get_useragent_host(r, REMOTE_NOLOOKUP, NULL))) {
+                apr_cpystrn(ws->client, r->useragent_ip, sizeof(ws->client)); /* DEPRECATE */
+                apr_cpystrn(ws->client64, r->useragent_ip, sizeof(ws->client64));
+            }
+            else {
+                apr_cpystrn(ws->client, val, sizeof(ws->client)); /* DEPRECATE */
+                apr_cpystrn(ws->client64, val, sizeof(ws->client64));
+            }
+        }
+        else if (c) {
+            if (!(val = ap_get_remote_host(c, c->base_server->lookup_defaults,
+                                           REMOTE_NOLOOKUP, NULL))) {
+                apr_cpystrn(ws->client, c->client_ip, sizeof(ws->client)); /* DEPRECATE */
+                apr_cpystrn(ws->client64, c->client_ip, sizeof(ws->client64));
+            }
+            else {
+                apr_cpystrn(ws->client, val, sizeof(ws->client)); /* DEPRECATE */
+                apr_cpystrn(ws->client64, val, sizeof(ws->client64));
+            }
+        }
+
+        if (s) {
+            if (c) {
+                apr_snprintf(ws->vhost, sizeof(ws->vhost), "%s:%d",
+                             s->server_hostname, c->local_addr->port);
+            }
+            else {
+                apr_cpystrn(ws->vhost, s->server_hostname, sizeof(ws->vhost));
+            }
+        }
+        else if (c) {
             ws->vhost[0]='\0';
         }
+
+        if (c) {
+            val = ap_get_protocol(c);
+            apr_cpystrn(ws->protocol, val, sizeof(ws->protocol));
+        }
     }
 
     return old_status;
@@ -516,29 +576,50 @@ AP_DECLARE(int) ap_update_child_status_from_indexes(int child_num,
 
     return update_child_status_internal(child_num, thread_num, status,
                                         r ? r->connection : NULL,
-                                        r);
+                                        r ? r->server : NULL,
+                                        r, NULL);
 }
 
 AP_DECLARE(int) ap_update_child_status(ap_sb_handle_t *sbh, int status,
                                       request_rec *r)
 {
-    if (!sbh)
+    if (!sbh || (sbh->child_num < 0))
         return -1;
 
     return update_child_status_internal(sbh->child_num, sbh->thread_num,
                                         status,
                                         r ? r->connection : NULL,
-                                        r);
+                                        r ? r->server : NULL,
+                                        r, NULL);
 }
 
 AP_DECLARE(int) ap_update_child_status_from_conn(ap_sb_handle_t *sbh, int status,
-                                       conn_rec *c)
+                                                 conn_rec *c)
 {
-    if (!sbh)
+    if (!sbh || (sbh->child_num < 0))
         return -1;
-    
+
+    return update_child_status_internal(sbh->child_num, sbh->thread_num,
+                                        status, c, NULL, NULL, NULL);
+}
+
+AP_DECLARE(int) ap_update_child_status_from_server(ap_sb_handle_t *sbh, int status, 
+                                                   conn_rec *c, server_rec *s)
+{
+    if (!sbh || (sbh->child_num < 0))
+        return -1;
+
     return update_child_status_internal(sbh->child_num, sbh->thread_num,
-                                        status, c, NULL);
+                                        status, c, s, NULL, NULL);
+}
+
+AP_DECLARE(int) ap_update_child_status_descr(ap_sb_handle_t *sbh, int status, const char *descr)
+{
+    if (!sbh || (sbh->child_num < 0))
+        return -1;
+
+    return update_child_status_internal(sbh->child_num, sbh->thread_num,
+                                        status, NULL, NULL, NULL, descr);
 }
 
 AP_DECLARE(void) ap_time_process_request(ap_sb_handle_t *sbh, int status)
@@ -555,13 +636,27 @@ AP_DECLARE(void) ap_time_process_request(ap_sb_handle_t *sbh, int status)
     ws = &ap_scoreboard_image->servers[sbh->child_num][sbh->thread_num];
 
     if (status == START_PREQUEST) {
-        ws->start_time = apr_time_now();
+        ws->start_time = ws->last_used = apr_time_now();
     }
     else if (status == STOP_PREQUEST) {
-        ws->stop_time = apr_time_now();
+        ws->stop_time = ws->last_used = apr_time_now();
+        if (ap_extended_status) {
+            ws->duration += ws->stop_time - ws->start_time;
+        }
     }
 }
 
+AP_DECLARE(int) ap_update_global_status()
+{
+#ifdef HAVE_TIMES
+    if (ap_scoreboard_image == NULL) {
+        return APR_SUCCESS;
+    }
+    times(&ap_scoreboard_image->global->times);
+#endif
+    return APR_SUCCESS;
+}
+
 AP_DECLARE(worker_score *) ap_get_scoreboard_worker_from_indexes(int x, int y)
 {
     if (((x < 0) || (x >= server_limit)) ||
@@ -580,6 +675,23 @@ AP_DECLARE(worker_score *) ap_get_scoreboard_worker(ap_sb_handle_t *sbh)
                                                  sbh->thread_num);
 }
 
+AP_DECLARE(void) ap_copy_scoreboard_worker(worker_score *dest, 
+                                           int child_num,
+                                           int thread_num)
+{
+    worker_score *ws = ap_get_scoreboard_worker_from_indexes(child_num, thread_num);
+
+    memcpy(dest, ws, sizeof *ws);
+
+    /* For extra safety, NUL-terminate the strings returned, though it
+     * should be true those last bytes are always zero anyway. */
+    dest->client[sizeof(dest->client) - 1] = '\0';
+    dest->client64[sizeof(dest->client64) - 1] = '\0';
+    dest->request[sizeof(dest->request) - 1] = '\0';
+    dest->vhost[sizeof(dest->vhost) - 1] = '\0';
+    dest->protocol[sizeof(dest->protocol) - 1] = '\0';
+}
+
 AP_DECLARE(process_score *) ap_get_scoreboard_process(int x)
 {
     if ((x < 0) || (x >= server_limit)) {