summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: d7a3583)
raw | patch | inline | side by side (parent: d7a3583)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 24 Jul 2013 01:42:04 +0000 (18:42 -0700) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 24 Jul 2013 01:42:04 +0000 (18:42 -0700) |
Let the function accept constant arguments describing the hostname and last
update timestamp rather than a host object. This is more flexible for the
caller.
update timestamp rather than a host object. This is more flexible for the
caller.
index c9b46db4627e2f007301be16cd0cffdba31a4b5d..7e4f73a2d20bfab567f116446f5caed1dd37c33b 100644 (file)
static int
sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
{
- sdb_host_t host = SDB_HOST_INIT;
- char name[strlen(hostname) + 1];
-
int status;
- strncpy(name, hostname, sizeof(name));
-
- SDB_OBJ(&host)->name = name;
- host._last_update = last_update;
-
- status = sdb_store_host(&host);
+ status = sdb_store_host(hostname, last_update);
if (status < 0) {
sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
- "store/update host '%s'.", name);
+ "store/update host '%s'.", hostname);
return -1;
}
else if (status > 0) /* value too old */
sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
"host '%s' (last update timestamp = %"PRIscTIME").",
- name, last_update);
+ hostname, last_update);
return 0;
} /* sdb_collectd_add_host */
index dea6794080ed9a284581de5b664f42d040c077a8..da8c99c74cc04c7fae9c0ee9e586c8f01e7d228e 100644 (file)
size_t n, sdb_data_t *data,
sdb_object_t __attribute__((unused)) *user_data)
{
- char *hostname = NULL;
- sdb_time_t timestamp = 0;
-
- sdb_host_t host = SDB_HOST_INIT;
+ const char *hostname;
+ sdb_time_t timestamp;
int status;
assert((data[0].type == SDB_TYPE_STRING)
&& (data[1].type == SDB_TYPE_DATETIME));
- hostname = strdup(data[0].data.string);
+ hostname = data[0].data.string;
timestamp = data[1].data.datetime;
- SDB_OBJ(&host)->name = hostname;
- host._last_update = timestamp;
-
- status = sdb_store_host(&host);
+ status = sdb_store_host(hostname, timestamp);
if (status < 0) {
sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
"store/update host '%s'.", hostname);
- free(hostname);
return -1;
}
else if (status > 0) /* value too old */
sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
"host '%s' (last update timestamp = %"PRIscTIME").",
hostname, timestamp);
- free(hostname);
return 0;
} /* sdb_livestatus_get_host */
index bde8a0aeb1e27feed83f292d58745f5406a4a786..6f70f4940d01867645c098b762bb4416e6332066 100644 (file)
size_t n, sdb_data_t *data,
sdb_object_t __attribute__((unused)) *user_data)
{
- sdb_host_t host = SDB_HOST_INIT;
+ const char *hostname;
+ sdb_time_t timestamp;
int status;
assert((data[0].type == SDB_TYPE_STRING)
&& (data[1].type == SDB_TYPE_DATETIME));
- SDB_OBJ(&host)->name = strdup(data[0].data.string);
- host._last_update = data[1].data.datetime;
+ hostname = data[0].data.string;
+ timestamp = data[1].data.datetime;
- status = sdb_store_host(&host);
+ status = sdb_store_host(hostname, timestamp);
if (status < 0) {
sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
- "store/update host '%s'.", SDB_OBJ(&host)->name);
- free(SDB_OBJ(&host)->name);
+ "store/update host '%s'.", hostname);
return -1;
}
else if (! status)
sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: "
"Added/updated host '%s' (last update timestamp = "
- "%"PRIscTIME").", SDB_OBJ(&host)->name, host._last_update);
- free(SDB_OBJ(&host)->name);
+ "%"PRIscTIME").", hostname, timestamp);
return 0;
} /* sdb_puppet_stcfg_get_hosts */
diff --git a/src/core/store.c b/src/core/store.c
index 92f3e510b9bccd6c57076bfbbf920c09c9754fbf..a5f712ef72729636b1a4f9a3606bfc9b44b0a15a 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
*/
int
-sdb_store_host(const sdb_host_t *host)
+sdb_store_host(const char *name, sdb_time_t last_update)
{
- char *cname;
-
- sdb_time_t last_update;
sdb_host_t *old;
+ char *cname;
int status = 0;
- if ((! host) || (! SDB_CONST_OBJ(host)->name))
+ if (! name)
return -1;
- cname = sdb_plugin_cname(strdup(SDB_CONST_OBJ(host)->name));
+ cname = sdb_plugin_cname(strdup(name));
if (! cname) {
sdb_log(SDB_LOG_ERR, "store: strdup failed");
return -1;
}
- last_update = host->_last_update;
+ last_update = last_update;
if (last_update <= 0)
last_update = sdb_gettime();
}
}
else {
- sdb_host_t *new = SDB_HOST(sdb_object_clone(SDB_CONST_OBJ(host)));
+ sdb_host_t *new = SDB_HOST(sdb_object_create(name, sdb_host_type));
if (! new) {
char errbuf[1024];
sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s",
index 22b13157cfb72772501206c698aae949ce00c4c1..563bd1a1dc2cbc1c744e477525eab5d1e0c246f0 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
/*
* sdb_store_host:
- * Add/update a host in the store. If the host, identified by its name,
- * already exists, it will be updated according to the specified 'host'
- * object. Else, a new entry will be created in the store. Any memory required
- * for storing the entry will be allocated an managed by the store itself. The
- * specified host-object will not be referenced or further accessed.
+ * Add/update a host in the store. If the host, identified by its
+ * canonicalized name, already exists, it will be updated according to the
+ * specified name and timestamp. Else, a new entry will be created in the
+ * store. Any memory required for storing the entry will be allocated an
+ * managed by the store itself.
*
* Returns:
* - 0 on success
* - a negative value on error
*/
int
-sdb_store_host(const sdb_host_t *host);
+sdb_store_host(const char *name, sdb_time_t last_update);
const sdb_host_t *
sdb_store_get_host(const char *name);