From: Sebastian Harl Date: Thu, 1 Aug 2013 23:59:19 +0000 (-0700) Subject: store: Let sdb_store_service() accept const arguments. X-Git-Tag: sysdb-0.1.0~388 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=d2dfea6b174f94b1d3a76d693d9736182e4a5f33;p=sysdb.git store: Let sdb_store_service() accept const arguments. There's no need in letting the caller construct a service object. All passed in strings are treated as constant values anyway. --- diff --git a/src/backend/collectd/unixsock.c b/src/backend/collectd/unixsock.c index 762539c..c01b0f3 100644 --- a/src/backend/collectd/unixsock.c +++ b/src/backend/collectd/unixsock.c @@ -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; diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index da8c99c..d672b36 100644 --- a/src/backend/mk-livestatus.c +++ b/src/backend/mk-livestatus.c @@ -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 */ diff --git a/src/core/store.c b/src/core/store.c index e8a693c..c4caecf 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -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 " diff --git a/src/include/core/store.h b/src/include/core/store.h index 7c6eee1..af8e23c 100644 --- a/src/include/core/store.h +++ b/src/include/core/store.h @@ -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);