diff --git a/src/core/store.c b/src/core/store.c
index 5a3cc4c357b70b429cdb0203711185a909c3eb36..dba88cd2a03a70e126bf0b409e9d68b6b85e6b25 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
return sdb_store_lookup_in_list(obj_list, type, name);
} /* sdb_store_lookup */
+/* The obj_lock has to be acquired before calling this function. */
static int
store_obj(int parent_type, const char *parent_name,
int type, const char *name, sdb_time_t last_update,
store_obj_t **updated_obj)
{
+ char *parent_cname = NULL, *cname = NULL;
+
sdb_llist_t *parent_list;
store_obj_t *old;
int status = 0;
- if (! name)
- return -1;
-
if (last_update <= 0)
last_update = sdb_gettime();
|| (type == SDB_SERVICE)
|| (type == SDB_ATTRIBUTE));
- pthread_rwlock_wrlock(&obj_lock);
+ if (parent_type == SDB_HOST) {
+ parent_cname = sdb_plugin_cname(strdup(parent_name));
+ if (! parent_cname) {
+ sdb_log(SDB_LOG_ERR, "store: strdup failed");
+ return -1;
+ }
+ parent_name = parent_cname;
+ }
+ if (type == SDB_HOST) {
+ cname = sdb_plugin_cname(strdup(name));
+ if (! cname) {
+ sdb_log(SDB_LOG_ERR, "store: strdup failed");
+ return -1;
+ }
+ name = cname;
+ }
if (! obj_list) {
if (! (obj_list = sdb_llist_create())) {
- pthread_rwlock_unlock(&obj_lock);
+ free(parent_cname);
+ free(cname);
return -1;
}
}
sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
"parent %s '%s' not found", TYPE_TO_NAME(type), name,
TYPE_TO_NAME(parent_type), parent_name);
- pthread_rwlock_unlock(&obj_lock);
+ free(parent_cname);
+ free(cname);
return -1;
}
sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
TYPE_TO_NAME(type), name,
sdb_strerror(errno, errbuf, sizeof(errbuf)));
- pthread_rwlock_unlock(&obj_lock);
+ free(parent_cname);
+ free(cname);
return -1;
}
if (updated_obj)
*updated_obj = new;
}
-
- pthread_rwlock_unlock(&obj_lock);
+ free(parent_cname);
+ free(cname);
return status;
} /* sdb_store_obj */
int
sdb_store_host(const char *name, sdb_time_t last_update)
{
- char *cname;
int status;
if (! name)
return -1;
- cname = sdb_plugin_cname(strdup(name));
- if (! cname) {
- sdb_log(SDB_LOG_ERR, "store: strdup failed");
- return -1;
- }
-
+ pthread_rwlock_wrlock(&obj_lock);
status = store_obj(/* parent = */ 0, NULL,
- /* stored object = */ SDB_HOST, cname, last_update,
+ /* stored object = */ SDB_HOST, name, last_update,
/* updated_obj = */ NULL);
- free(cname);
+ pthread_rwlock_unlock(&obj_lock);
return status;
} /* sdb_store_host */
sdb_store_attribute(const char *hostname, const char *key, const char *value,
sdb_time_t last_update)
{
- char *cname;
int status;
store_obj_t *updated_attr = NULL;
if ((! hostname) || (! key))
return -1;
- cname = sdb_plugin_cname(strdup(hostname));
- if (! cname) {
- sdb_log(SDB_LOG_ERR, "store: strdup failed");
- return -1;
- }
-
- status = store_obj(/* parent = */ SDB_HOST, cname,
+ pthread_rwlock_wrlock(&obj_lock);
+ status = store_obj(/* parent = */ SDB_HOST, hostname,
/* stored object = */ SDB_ATTRIBUTE, key, last_update,
&updated_attr);
- free(cname);
SDB_ATTR(updated_attr)->value = strdup(value);
if (! SDB_ATTR(updated_attr)->value) {
sdb_object_deref(SDB_OBJ(updated_attr));
status = -1;
}
+ pthread_rwlock_unlock(&obj_lock);
return status;
} /* sdb_store_attribute */
sdb_store_service(const char *hostname, const char *name,
sdb_time_t last_update)
{
- char *cname;
int status;
if ((! hostname) || (! name))
return -1;
- cname = sdb_plugin_cname(strdup(hostname));
- if (! cname) {
- sdb_log(SDB_LOG_ERR, "store: strdup failed");
- return -1;
- }
-
- status = store_obj(/* parent = */ SDB_HOST, cname,
+ pthread_rwlock_wrlock(&obj_lock);
+ status = store_obj(/* parent = */ SDB_HOST, hostname,
/* stored object = */ SDB_SERVICE, name, last_update,
/* updated obj = */ NULL);
- free(cname);
+ pthread_rwlock_unlock(&obj_lock);
return status;
} /* sdb_store_service */