X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore.c;h=1eb22d6c5e2c9be5e1942a711abc3126ce55fdfe;hb=4fcb8750b9b34bc012b8ffa1fcf78096a8e61d6f;hp=7af662f8aa66a7ac4e6c7fd8489157c000915b8c;hpb=231b242faad442e8e7eb7dada96260edf85053c1;p=sysdb.git diff --git a/src/core/store.c b/src/core/store.c index 7af662f..1eb22d6 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -27,7 +27,8 @@ #include "sysdb.h" #include "core/store.h" -#include "utils/error.h" +#include "core/error.h" +#include "core/plugin.h" #include "utils/llist.h" #include @@ -41,134 +42,181 @@ #include /* - * private data types + * private variables */ -/* type used for looking up named objects */ -typedef struct { - sdb_object_t parent; - const char *obj_name; -} sdb_store_lookup_obj_t; -#define SDB_STORE_LOOKUP_OBJ_INIT { SDB_OBJECT_INIT, NULL } +static sdb_llist_t *obj_list = NULL; +static pthread_rwlock_t obj_lock = PTHREAD_RWLOCK_INITIALIZER; /* - * private variables + * private types */ -static sdb_llist_t *host_list = NULL; -static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER; +static sdb_type_t sdb_store_obj_type; +static sdb_type_t sdb_attribute_type; -/* - * private helper functions - */ +struct store_obj; +typedef struct store_obj store_obj_t; + +struct store_obj { + sdb_object_t super; + sdb_time_t last_update; + store_obj_t *parent; +}; +#define STORE_OBJ(obj) ((store_obj_t *)(obj)) +#define STORE_CONST_OBJ(obj) ((const store_obj_t *)(obj)) + +typedef struct { + store_obj_t super; + + char *value; +} sdb_attribute_t; +#define SDB_ATTR(obj) ((sdb_attribute_t *)(obj)) +#define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj)) + +typedef struct { + store_obj_t super; + + int type; + sdb_llist_t *children; + + sdb_llist_t *attributes; +} sdb_store_obj_t; +#define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj)) +#define SDB_CONST_STORE_OBJ(obj) ((const sdb_store_obj_t *)(obj)) + +enum { + SDB_HOST = 1, + SDB_SERVICE, +}; +#define TYPE_TO_NAME(t) \ + (((t) == SDB_HOST) ? "host" \ + : ((t) == SDB_SERVICE) ? "service" : "unknown") + +/* shortcuts for accessing the sdb_store_obj_t attributes + * of inheriting objects */ +#define _last_update super.last_update static int -sdb_store_obj_cmp_by_name(const sdb_object_t *a, const sdb_object_t *b) +store_obj_init(sdb_object_t *obj, va_list __attribute__((unused)) ap) { - const sdb_store_obj_t *h1 = (const sdb_store_obj_t *)a; - const sdb_store_obj_t *h2 = (const sdb_store_obj_t *)b; + store_obj_t *sobj = STORE_OBJ(obj); + sobj->last_update = sdb_gettime(); + /* ignore errors -> last_update will be updated later */ - assert(h1 && h2); - return strcasecmp(h1->name, h2->name); -} /* sdb_store_obj_cmp_by_name */ + sobj->parent = NULL; + return 0; +} /* store_obj_init */ -static int -sdb_cmp_store_obj_with_name(const sdb_object_t *a, const sdb_object_t *b) +static void +store_obj_destroy(sdb_object_t *obj) { - const sdb_store_obj_t *obj = (const sdb_store_obj_t *)a; - const sdb_store_lookup_obj_t *lookup = (const sdb_store_lookup_obj_t *)b; + const store_obj_t *sobj = STORE_OBJ(obj); - assert(obj && lookup); - return strcasecmp(obj->name, lookup->obj_name); -} /* sdb_cmp_store_obj_with_name */ + if (sobj->parent) + sdb_object_deref(SDB_OBJ(sobj->parent)); +} /* store_obj_destroy */ -/* - * private types - */ +static sdb_object_t * +store_obj_clone(const sdb_object_t *obj) +{ + const store_obj_t *sobj = STORE_CONST_OBJ(obj); + store_obj_t *new; + + new = STORE_OBJ(sdb_object_create(obj->name, obj->type)); + if (! new) + return NULL; + + new->last_update = sobj->last_update; + sdb_object_ref(SDB_OBJ(sobj->parent)); + new->parent = sobj->parent; + return SDB_OBJ(new); +} /* store_obj_clone */ static int -sdb_host_init(sdb_object_t *obj, va_list ap) +sdb_store_obj_init(sdb_object_t *obj, va_list ap) { - const char *name = va_arg(ap, const char *); + sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj); + int ret; - SDB_HOST(obj)->_name = strdup(name); - if (! SDB_HOST(obj)->_name) - return -1; + ret = store_obj_init(obj, ap); + if (ret) + return ret; - SDB_HOST(obj)->_last_update = sdb_gettime(); - /* ignore errors -> last_update will be updated later */ + sobj->type = va_arg(ap, int); - SDB_HOST(obj)->attributes = sdb_llist_create(); - if (! SDB_HOST(obj)->attributes) + sobj->children = sdb_llist_create(); + if (! sobj->children) return -1; - SDB_HOST(obj)->services = sdb_llist_create(); - if (! SDB_HOST(obj)->services) + sobj->attributes = sdb_llist_create(); + if (! sobj->attributes) return -1; return 0; -} /* sdb_host_init */ +} /* sdb_store_obj_init */ static void -sdb_host_destroy(sdb_object_t *obj) +sdb_store_obj_destroy(sdb_object_t *obj) { + sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj); + assert(obj); - if (SDB_HOST(obj)->_name) - free(SDB_HOST(obj)->_name); + store_obj_destroy(obj); - if (SDB_HOST(obj)->attributes) - sdb_llist_destroy(SDB_HOST(obj)->attributes); - if (SDB_HOST(obj)->services) - sdb_llist_destroy(SDB_HOST(obj)->services); -} /* sdb_host_destroy */ + if (sobj->children) + sdb_llist_destroy(sobj->children); + if (sobj->attributes) + sdb_llist_destroy(sobj->attributes); +} /* sdb_store_obj_destroy */ static sdb_object_t * -sdb_host_do_clone(const sdb_object_t *obj) +sdb_store_obj_clone(const sdb_object_t *obj) { - const sdb_host_t *host = (const sdb_host_t *)obj; - sdb_host_t *new; + const sdb_store_obj_t *sobj = SDB_CONST_STORE_OBJ(obj); + sdb_store_obj_t *new; - new = sdb_host_create(host->_name); + new = SDB_STORE_OBJ(store_obj_clone(obj)); if (! new) return NULL; + new->type = sobj->type; + /* make sure these are initialized; else sdb_object_deref() might access * arbitrary memory in case of an error */ - new->services = new->attributes = NULL; + new->children = new->attributes = NULL; - if (host->attributes) { - new->attributes = sdb_llist_clone(host->attributes); - if (! new->attributes) { + if (sobj->children) { + new->children = sdb_llist_clone(sobj->children); + if (! new->children) { sdb_object_deref(SDB_OBJ(new)); return NULL; } } - - new->_last_update = host->_last_update; - if (host->services) { - new->services = sdb_llist_clone(host->services); - if (! new->services) { + if (sobj->attributes) { + new->attributes = sdb_llist_clone(sobj->attributes); + if (! new->attributes) { sdb_object_deref(SDB_OBJ(new)); return NULL; } } + + new->_last_update = sobj->_last_update; return SDB_OBJ(new); -} /* sdb_host_do_clone */ +} /* sdb_store_obj_clone */ static int sdb_attr_init(sdb_object_t *obj, va_list ap) { - const char *hostname = va_arg(ap, const char *); - const char *name = va_arg(ap, const char *); const char *value = va_arg(ap, const char *); + int ret; - SDB_ATTR(obj)->hostname = strdup(hostname); - SDB_ATTR(obj)->_name = strdup(name); - SDB_ATTR(obj)->attr_value = strdup(value); - if ((! SDB_ATTR(obj)->hostname) - || (! SDB_ATTR(obj)->_name) || (! SDB_ATTR(obj)->attr_value)) - return -1; + ret = store_obj_init(obj, ap); + if (ret) + return ret; - SDB_ATTR(obj)->_last_update = sdb_gettime(); + SDB_ATTR(obj)->value = strdup(value); + if (! SDB_ATTR(obj)->value) + return -1; return 0; } /* sdb_attr_init */ @@ -177,12 +225,10 @@ sdb_attr_destroy(sdb_object_t *obj) { assert(obj); - if (SDB_ATTR(obj)->hostname) - free(SDB_ATTR(obj)->hostname); - if (SDB_ATTR(obj)->_name) - free(SDB_ATTR(obj)->_name); - if (SDB_ATTR(obj)->attr_value) - free(SDB_ATTR(obj)->attr_value); + store_obj_destroy(obj); + + if (SDB_ATTR(obj)->value) + free(SDB_ATTR(obj)->value); } /* sdb_attr_destroy */ static sdb_object_t * @@ -191,65 +237,28 @@ sdb_attr_clone(const sdb_object_t *obj) const sdb_attribute_t *attr = (const sdb_attribute_t *)obj; sdb_attribute_t *new; - new = sdb_attribute_create(attr->hostname, - attr->_name, attr->attr_value); + new = SDB_ATTR(store_obj_clone(obj)); if (! new) return NULL; - new->_last_update = attr->_last_update; - return SDB_OBJ(new); -} /* sdb_attr_clone */ - -static int -sdb_svc_init(sdb_object_t *obj, va_list ap) -{ - const char *hostname = va_arg(ap, const char *); - const char *name = va_arg(ap, const char *); - - SDB_SVC(obj)->hostname = strdup(hostname); - SDB_SVC(obj)->_name = strdup(name); - if ((! SDB_SVC(obj)->hostname) || (! SDB_SVC(obj)->_name)) - return -1; - - SDB_SVC(obj)->_last_update = sdb_gettime(); - /* ignore errors -> last_update will be updated later */ - return 0; -} /* sdb_svc_init */ - -static void -sdb_svc_destroy(sdb_object_t *obj) -{ - assert(obj); - - if (SDB_SVC(obj)->hostname) - free(SDB_SVC(obj)->hostname); - if (SDB_SVC(obj)->_name) - free(SDB_SVC(obj)->_name); -} /* sdb_svc_destroy */ - -static sdb_object_t * -sdb_svc_clone(const sdb_object_t *obj) -{ - const sdb_service_t *svc = (const sdb_service_t *)obj; - sdb_service_t *new; - - new = sdb_service_create(svc->hostname, svc->_name); - if (! new) + if (attr->value) + new->value = strdup(attr->value); + if (! new->value) { + sdb_object_deref(SDB_OBJ(new)); return NULL; - - new->_last_update = svc->_last_update; + } return SDB_OBJ(new); -} /* sdb_svc_clone */ +} /* sdb_attr_clone */ -static sdb_type_t sdb_host_type = { - sizeof(sdb_host_t), +static sdb_type_t sdb_store_obj_type = { + sizeof(sdb_store_obj_t), - sdb_host_init, - sdb_host_destroy, - sdb_host_do_clone + sdb_store_obj_init, + sdb_store_obj_destroy, + sdb_store_obj_clone }; -static sdb_type_t sdb_attr_type = { +static sdb_type_t sdb_attribute_type = { sizeof(sdb_attribute_t), sdb_attr_init, @@ -257,73 +266,101 @@ static sdb_type_t sdb_attr_type = { sdb_attr_clone }; -static sdb_type_t sdb_svc_type = { - sizeof(sdb_service_t), - - sdb_svc_init, - sdb_svc_destroy, - sdb_svc_clone -}; - /* - * public API + * private helper functions */ -sdb_host_t * -sdb_host_create(const char *name) +static sdb_store_obj_t * +sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name) { - sdb_object_t *obj; + sdb_llist_iter_t *iter; - if (! name) + if (! l) return NULL; - obj = sdb_object_create(sdb_host_type, name); - if (! obj) + iter = sdb_llist_get_iter(l); + if (! iter) return NULL; - return SDB_HOST(obj); -} /* sdb_host_create */ -sdb_host_t * -sdb_host_clone(const sdb_host_t *host) -{ - return SDB_HOST(sdb_host_do_clone((const sdb_object_t *)host)); -} /* sdb_host_clone */ + while (sdb_llist_iter_has_next(iter)) { + sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter)); + assert(sobj); -int -sdb_store_host(const sdb_host_t *host) + if ((sobj->type == type) + && (! strcasecmp(SDB_OBJ(sobj)->name, name))) { + sdb_llist_iter_destroy(iter); + return sobj; + } + + sobj = sdb_store_lookup_in_list(sobj->children, type, name); + if (sobj) { + sdb_llist_iter_destroy(iter); + return sobj; + } + } + sdb_llist_iter_destroy(iter); + return NULL; +} /* sdb_store_lookup_in_list */ + +static sdb_store_obj_t * +sdb_store_lookup(int type, const char *name) { - sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT; - sdb_time_t last_update; + return sdb_store_lookup_in_list(obj_list, type, name); +} /* sdb_store_lookup */ - sdb_host_t *old; +static int +store_obj(int parent_type, const char *parent_name, + int type, const char *name, sdb_time_t last_update) +{ + sdb_llist_t *parent_list; + sdb_store_obj_t *old; int status = 0; - if ((! host) || (! host->_name)) + if (! name) return -1; - last_update = host->_last_update; if (last_update <= 0) last_update = sdb_gettime(); - pthread_rwlock_wrlock(&host_lock); + assert((parent_type == 0) + || (parent_type = SDB_HOST) + || (parent_type == SDB_SERVICE)); + assert((type == 0) || (type = SDB_HOST) || (type == SDB_SERVICE)); - if (! host_list) { - if (! (host_list = sdb_llist_create())) { - pthread_rwlock_unlock(&host_lock); + pthread_rwlock_wrlock(&obj_lock); + + if (! obj_list) { + if (! (obj_list = sdb_llist_create())) { + pthread_rwlock_unlock(&obj_lock); return -1; } } + parent_list = obj_list; + + if (parent_type && parent_name) { + sdb_store_obj_t *parent; + + parent = sdb_store_lookup(parent_type, parent_name); + if (! parent) { + 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); + return -1; + } - lookup.obj_name = host->_name; - old = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup, - sdb_cmp_store_obj_with_name)); + parent_list = parent->children; + } + + /* TODO: only look into direct children? */ + old = sdb_store_lookup_in_list(parent_list, type, name); if (old) { if (old->_last_update > last_update) { - sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - " + sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - " "value too old (%"PRIscTIME" < %"PRIscTIME")", - host->_name, last_update, old->_last_update); - /* don't report an error; the host may be updated by multiple + TYPE_TO_NAME(type), name, last_update, old->_last_update); + /* don't report an error; the object may be updated by multiple * backends */ status = 1; } @@ -332,133 +369,97 @@ sdb_store_host(const sdb_host_t *host) } } else { - sdb_host_t *new = sdb_host_clone(host); + sdb_store_obj_t *new = SDB_STORE_OBJ(sdb_object_create(name, + sdb_store_obj_type, type)); if (! new) { char errbuf[1024]; - sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s", + 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(&host_lock); + pthread_rwlock_unlock(&obj_lock); return -1; } - if (! new->attributes) { - if (! (new->attributes = sdb_llist_create())) { - char errbuf[1024]; - sdb_log(SDB_LOG_ERR, "store: Failed to initialize " - "host object '%s': %s", host->_name, - sdb_strerror(errno, errbuf, sizeof(errbuf))); - sdb_object_deref(SDB_OBJ(new)); - pthread_rwlock_unlock(&host_lock); - return -1; - } - } - - if (! new->services) { - if (! (new->services = sdb_llist_create())) { - char errbuf[1024]; - sdb_log(SDB_LOG_ERR, "store: Failed to initialize " - "host object '%s': %s", host->_name, - sdb_strerror(errno, errbuf, sizeof(errbuf))); - sdb_object_deref(SDB_OBJ(new)); - pthread_rwlock_unlock(&host_lock); - return -1; - } - } - - status = sdb_llist_insert_sorted(host_list, SDB_OBJ(new), - sdb_store_obj_cmp_by_name); + status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new), + sdb_object_cmp_by_name); /* pass control to the list or destroy in case of an error */ sdb_object_deref(SDB_OBJ(new)); } - pthread_rwlock_unlock(&host_lock); + pthread_rwlock_unlock(&obj_lock); return status; -} /* sdb_store_host */ +} /* sdb_store_obj */ + +/* + * public API + */ -const sdb_host_t * -sdb_store_get_host(const char *name) +int +sdb_store_host(const char *name, sdb_time_t last_update) { - sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT; - sdb_host_t *host; + char *cname; + int status = 0; if (! name) - return NULL; + return -1; - lookup.obj_name = name; - host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup, - sdb_cmp_store_obj_with_name)); + cname = sdb_plugin_cname(strdup(name)); + if (! cname) { + sdb_log(SDB_LOG_ERR, "store: strdup failed"); + return -1; + } - if (! host) - return NULL; - return host; -} /* sdb_store_get_host */ + status = store_obj(/* parent = */ 0, NULL, + /* stored object = */ SDB_HOST, cname, last_update); + free(cname); + return status; +} /* sdb_store_host */ -sdb_attribute_t * -sdb_attribute_create(const char *hostname, - const char *name, const char *value) +_Bool +sdb_store_has_host(const char *name) { - sdb_object_t *obj; - - if ((! hostname) || (! name) || (! value)) - return NULL; + sdb_store_obj_t *host; - obj = sdb_object_create(sdb_attr_type, hostname, name, value); - if (! obj) + if (! name) return NULL; - return SDB_ATTR(obj); -} /* sdb_attribute_create */ -sdb_attribute_t * -sdb_attribute_clone(const sdb_attribute_t *attr) -{ - return SDB_ATTR(sdb_attr_clone((const sdb_object_t *)attr)); -} /* sdb_attribute_clone */ + host = sdb_store_lookup(SDB_HOST, name); + return host != NULL; +} /* sdb_store_has_host */ int -sdb_store_attribute(const sdb_attribute_t *attr) +sdb_store_attribute(const char *hostname, const char *key, const char *value, + sdb_time_t last_update) { - sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT; - sdb_host_t *host; - + sdb_store_obj_t *sobj; sdb_attribute_t *old; - sdb_time_t last_update; - int status = 0; - if (! attr) + if ((! hostname) || (! key)) return -1; - last_update = attr->_last_update; if (last_update <= 0) last_update = sdb_gettime(); - if (! host_list) + if (! obj_list) return -1; - pthread_rwlock_wrlock(&host_lock); + pthread_rwlock_wrlock(&obj_lock); - lookup.obj_name = attr->hostname; - host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup, - sdb_cmp_store_obj_with_name)); - - if (! host) { - pthread_rwlock_unlock(&host_lock); + sobj = sdb_store_lookup(SDB_HOST, hostname); + if (! sobj) { + pthread_rwlock_unlock(&obj_lock); return -1; } - lookup.obj_name = attr->_name; - old = SDB_ATTR(sdb_llist_search(host->attributes, - (const sdb_object_t *)&lookup, - sdb_cmp_store_obj_with_name)); - + old = SDB_ATTR(sdb_llist_search_by_name(sobj->attributes, key)); if (old) { if (old->_last_update > last_update) { sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute " "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")", - attr->hostname, attr->_name, last_update, - old->_last_update); + hostname, key, last_update, old->_last_update); status = 1; } else { @@ -466,135 +467,50 @@ sdb_store_attribute(const sdb_attribute_t *attr) } } else { - sdb_attribute_t *new = sdb_attribute_clone(attr); + sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key, + sdb_attribute_type, value)); if (! new) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute " "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf))); - pthread_rwlock_unlock(&host_lock); + pthread_rwlock_unlock(&obj_lock); return -1; } - status = sdb_llist_insert_sorted(host->attributes, SDB_OBJ(new), - sdb_store_obj_cmp_by_name); + status = sdb_llist_insert_sorted(sobj->attributes, SDB_OBJ(new), + sdb_object_cmp_by_name); /* pass control to the list or destroy in case of an error */ sdb_object_deref(SDB_OBJ(new)); } - pthread_rwlock_unlock(&host_lock); + pthread_rwlock_unlock(&obj_lock); return status; } /* sdb_store_attribute */ -sdb_service_t * -sdb_service_create(const char *hostname, const char *name) -{ - sdb_object_t *obj; - - if ((! hostname) || (! name)) - return NULL; - - obj = sdb_object_create(sdb_svc_type, hostname, name); - if (! obj) - return NULL; - return SDB_SVC(obj); -} /* sdb_service_create */ - -sdb_service_t * -sdb_service_clone(const sdb_service_t *svc) -{ - return SDB_SVC(sdb_svc_clone((const sdb_object_t *)svc)); -} /* sdb_service_clone */ - int -sdb_store_service(const sdb_service_t *svc) +sdb_store_service(const char *hostname, const char *name, + sdb_time_t last_update) { - sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT; - sdb_host_t *host; - - sdb_service_t *old; - - sdb_time_t last_update; - + char *cname; int status = 0; - if (! svc) + if ((! hostname) || (! name)) return -1; - last_update = svc->_last_update; - if (last_update <= 0) - last_update = sdb_gettime(); - - if (! host_list) + cname = sdb_plugin_cname(strdup(hostname)); + if (! cname) { + sdb_log(SDB_LOG_ERR, "store: strdup failed"); return -1; - - pthread_rwlock_wrlock(&host_lock); - - lookup.obj_name = svc->hostname; - host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup, - sdb_cmp_store_obj_with_name)); - - if (! host) { - pthread_rwlock_unlock(&host_lock); - return -1; - } - - lookup.obj_name = svc->_name; - old = SDB_SVC(sdb_llist_search(host->services, (const sdb_object_t *)&lookup, - sdb_cmp_store_obj_with_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, svc->_name, last_update, - old->_last_update); - status = 1; - } - else { - old->_last_update = last_update; - } } - else { - sdb_service_t *new = sdb_service_clone(svc); - if (! new) { - char errbuf[1024]; - sdb_log(SDB_LOG_ERR, "store: Failed to clone service " - "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf))); - pthread_rwlock_unlock(&host_lock); - return -1; - } - status = sdb_llist_insert_sorted(host->services, SDB_OBJ(new), - sdb_store_obj_cmp_by_name); - - /* pass control to the list or destroy in case of an error */ - sdb_object_deref(SDB_OBJ(new)); - } - - pthread_rwlock_unlock(&host_lock); + status = store_obj(/* parent = */ SDB_HOST, cname, + /* stored object = */ SDB_SERVICE, name, last_update); + free(cname); return status; } /* sdb_store_service */ -const sdb_service_t * -sdb_store_get_service(const sdb_host_t *host, const char *name) -{ - sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT; - sdb_service_t *svc; - - if ((! host) || (! name)) - return NULL; - - lookup.obj_name = name; - svc = SDB_SVC(sdb_llist_search(host->services, - (const sdb_object_t *)&lookup, - sdb_cmp_store_obj_with_name)); - - if (! svc) - return NULL; - return svc; -} /* sdb_store_get_service */ - +/* TODO: actually support hierarchical data */ int sdb_store_dump(FILE *fh) { @@ -603,16 +519,16 @@ sdb_store_dump(FILE *fh) if (! fh) return -1; - pthread_rwlock_rdlock(&host_lock); + pthread_rwlock_rdlock(&obj_lock); - host_iter = sdb_llist_get_iter(host_list); + host_iter = sdb_llist_get_iter(obj_list); if (! host_iter) { - pthread_rwlock_unlock(&host_lock); + pthread_rwlock_unlock(&obj_lock); return -1; } while (sdb_llist_iter_has_next(host_iter)) { - sdb_host_t *host = SDB_HOST(sdb_llist_iter_get_next(host_iter)); + sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter)); sdb_llist_iter_t *svc_iter; sdb_llist_iter_t *attr_iter; @@ -626,7 +542,7 @@ sdb_store_dump(FILE *fh) time_str[sizeof(time_str) - 1] = '\0'; fprintf(fh, "Host '%s' (last updated: %s):\n", - host->_name, time_str); + SDB_OBJ(host)->name, time_str); attr_iter = sdb_llist_get_iter(host->attributes); if (! attr_iter) { @@ -646,12 +562,12 @@ sdb_store_dump(FILE *fh) time_str[sizeof(time_str) - 1] = '\0'; fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n", - attr->_name, attr->attr_value, time_str); + SDB_OBJ(attr)->name, attr->value, time_str); } sdb_llist_iter_destroy(attr_iter); - svc_iter = sdb_llist_get_iter(host->services); + svc_iter = sdb_llist_get_iter(host->children); if (! svc_iter) { char errbuf[1024]; fprintf(fh, "Failed to retrieve services: %s\n", @@ -660,7 +576,7 @@ sdb_store_dump(FILE *fh) } while (sdb_llist_iter_has_next(svc_iter)) { - sdb_service_t *svc = SDB_SVC(sdb_llist_iter_get_next(svc_iter)); + sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter)); assert(svc); if (! sdb_strftime(time_str, sizeof(time_str), @@ -669,14 +585,14 @@ sdb_store_dump(FILE *fh) time_str[sizeof(time_str) - 1] = '\0'; fprintf(fh, "\tService '%s' (last updated: %s)\n", - svc->_name, time_str); + SDB_OBJ(svc)->name, time_str); } sdb_llist_iter_destroy(svc_iter); } sdb_llist_iter_destroy(host_iter); - pthread_rwlock_unlock(&host_lock); + pthread_rwlock_unlock(&obj_lock); return 0; } /* sdb_store_dump */