Code

store: Let sdb_store_service() accept const arguments.
authorSebastian Harl <sh@tokkee.org>
Thu, 1 Aug 2013 23:59:19 +0000 (16:59 -0700)
committerSebastian Harl <sh@tokkee.org>
Thu, 1 Aug 2013 23:59:19 +0000 (16:59 -0700)
There's no need in letting the caller construct a service object. All passed
in strings are treated as constant values anyway.

src/backend/collectd/unixsock.c
src/backend/mk-livestatus.c
src/core/store.c
src/include/core/store.h

index 762539c4f9cf3ed5ef41b1039c9d2e40ad21105c..c01b0f324b874ab2ac4ac2ec97a91fd5e254175e 100644 (file)
@@ -84,23 +84,15 @@ static int
 sdb_collectd_add_svc(const char *hostname, const char *plugin,
                const char *type, sdb_time_t last_update)
 {
-       sdb_service_t svc = SDB_SVC_INIT;
-       char host[strlen(hostname) + 1];
        char name[strlen(plugin) + strlen(type) + 2];
+       int  status;
 
-       int status;
-
-       strncpy(host, hostname, sizeof(host));
        snprintf(name, sizeof(name), "%s/%s", plugin, type);
 
-       svc.hostname = host;
-       SDB_OBJ(&svc)->name = name;
-       svc._last_update = last_update;
-
-       status = sdb_store_service(&svc);
+       status = sdb_store_service(hostname, name, last_update);
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
-                               "store/update service '%s/%s'.", host, name);
+                               "store/update service '%s/%s'.", hostname, name);
                return -1;
        }
        return 0;
index da8c99c74cc04c7fae9c0ee9e586c8f01e7d228e..d672b36c9ee8468c13a8e2ed5487d9c1b3a96a84 100644 (file)
@@ -85,12 +85,10 @@ sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
                size_t n, sdb_data_t *data,
                sdb_object_t __attribute__((unused)) *user_data)
 {
-       char *hostname = NULL;
-       char *svcname = NULL;
+       const char *hostname = NULL;
+       const char *svcname = NULL;
        sdb_time_t timestamp = 0;
 
-       sdb_service_t svc = SDB_SVC_INIT;
-
        int status;
 
        assert(n == 3);
@@ -98,21 +96,15 @@ sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
                        && (data[1].type == SDB_TYPE_STRING)
                        && (data[2].type == SDB_TYPE_DATETIME));
 
-       hostname  = strdup(data[0].data.string);
-       svcname   = strdup(data[1].data.string);
+       hostname  = data[0].data.string;
+       svcname   = data[1].data.string;
        timestamp = data[2].data.datetime;
 
-       svc.hostname = hostname;
-       SDB_OBJ(&svc)->name = svcname;
-       svc._last_update = timestamp;
-
-       status = sdb_store_service(&svc);
+       status = sdb_store_service(hostname, svcname, timestamp);
 
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
                                "store/update service '%s / %s'.", hostname, svcname);
-               free(hostname);
-               free(svcname);
                return -1;
        }
        else if (status > 0) /* value too old */
@@ -121,8 +113,6 @@ sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
        sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
                        "service '%s / %s' (last update timestamp = %"PRIscTIME").",
                        hostname, svcname, timestamp);
-       free(hostname);
-       free(svcname);
        return 0;
 } /* sdb_livestatus_get_svc */
 
index e8a693ce0c268b228bab54e29614a49447517e6e..c4caecfe6a49caecc36caf20c2f6836a2bf88534 100644 (file)
@@ -235,7 +235,6 @@ sdb_store_host(const char *name, sdb_time_t last_update)
                return -1;
        }
 
-       last_update = last_update;
        if (last_update <= 0)
                last_update = sdb_gettime();
 
@@ -384,18 +383,17 @@ sdb_store_attribute(const char *hostname, const char *key, const char *value,
 } /* sdb_store_attribute */
 
 int
-sdb_store_service(const sdb_service_t *svc)
+sdb_store_service(const char *hostname, const char *name,
+               sdb_time_t last_update)
 {
        sdb_host_t *host;
        sdb_service_t *old;
-       sdb_time_t last_update;
 
        int status = 0;
 
-       if (! svc)
+       if ((! hostname) || (! name))
                return -1;
 
-       last_update = svc->_last_update;
        if (last_update <= 0)
                last_update = sdb_gettime();
 
@@ -404,20 +402,18 @@ sdb_store_service(const sdb_service_t *svc)
 
        pthread_rwlock_wrlock(&host_lock);
 
-       host = SDB_HOST(sdb_llist_search_by_name(host_list, svc->hostname));
+       host = SDB_HOST(sdb_llist_search_by_name(host_list, hostname));
        if (! host) {
                pthread_rwlock_unlock(&host_lock);
                return -1;
        }
 
-       old = SDB_SVC(sdb_llist_search_by_name(host->services,
-                               SDB_CONST_OBJ(svc)->name));
+       old = SDB_SVC(sdb_llist_search_by_name(host->services, name));
        if (old) {
                if (old->_last_update > last_update) {
                        sdb_log(SDB_LOG_DEBUG, "store: Cannot update service "
                                        "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
-                                       svc->hostname, SDB_CONST_OBJ(svc)->name, last_update,
-                                       old->_last_update);
+                                       hostname, name, last_update, old->_last_update);
                        status = 1;
                }
                else {
@@ -425,7 +421,8 @@ sdb_store_service(const sdb_service_t *svc)
                }
        }
        else {
-               sdb_service_t *new = SDB_SVC(sdb_object_clone(SDB_CONST_OBJ(svc)));
+               sdb_service_t *new = SDB_SVC(sdb_object_create(name, sdb_service_type,
+                                       hostname));
                if (! new) {
                        char errbuf[1024];
                        sdb_log(SDB_LOG_ERR, "store: Failed to clone service "
index 7c6eee1c0bc801b899a8864e3baac25e6c3a1ec4..af8e23cff6a390ce74a9f354b8f38a672b70d5e1 100644 (file)
@@ -140,7 +140,8 @@ sdb_store_attribute(const char *hostname, const char *key, const char *value,
  *  - a negative value on error
  */
 int
-sdb_store_service(const sdb_service_t *svc);
+sdb_store_service(const char *hostname, const char *name,
+               sdb_time_t last_update);
 
 int
 sdb_store_dump(FILE *fh);