X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore.c;h=f99841f4820251f08fe86a339f1b24edf4ca6adc;hb=533dc85bfe9ef7ed1f7fead5ce612506b7fab90d;hp=3901ba45a3016b5d8ca5f911cee64cc33b6db388;hpb=848efadda124a4778f08a84a082ff2436504d22c;p=sysdb.git diff --git a/src/core/store.c b/src/core/store.c index 3901ba4..f99841f 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -29,7 +29,6 @@ #include "core/store.h" #include "utils/error.h" #include "utils/llist.h" -#include "utils/string.h" #include @@ -83,16 +82,20 @@ sdb_cmp_store_obj_with_name(const sdb_object_t *a, const sdb_object_t *b) return strcasecmp(obj->name, lookup->obj_name); } /* sdb_cmp_store_obj_with_name */ +/* + * public types + */ + static int sdb_host_init(sdb_object_t *obj, va_list ap) { const char *name = va_arg(ap, const char *); - SDB_HOST(obj)->host_name = strdup(name); - if (! SDB_HOST(obj)->host_name) + SDB_HOST(obj)->_name = strdup(name); + if (! SDB_HOST(obj)->_name) return -1; - SDB_HOST(obj)->host_last_update = sdb_gettime(); + SDB_HOST(obj)->_last_update = sdb_gettime(); /* ignore errors -> last_update will be updated later */ SDB_HOST(obj)->attributes = sdb_llist_create(); @@ -109,8 +112,8 @@ sdb_host_destroy(sdb_object_t *obj) { assert(obj); - if (SDB_HOST(obj)->host_name) - free(SDB_HOST(obj)->host_name); + if (SDB_HOST(obj)->_name) + free(SDB_HOST(obj)->_name); if (SDB_HOST(obj)->attributes) sdb_llist_destroy(SDB_HOST(obj)->attributes); @@ -118,6 +121,39 @@ sdb_host_destroy(sdb_object_t *obj) sdb_llist_destroy(SDB_HOST(obj)->services); } /* sdb_host_destroy */ +static sdb_object_t * +sdb_host_do_clone(const sdb_object_t *obj) +{ + const sdb_host_t *host = (const sdb_host_t *)obj; + sdb_host_t *new; + + new = sdb_host_create(host->_name); + if (! new) + return NULL; + + /* make sure these are initialized; else sdb_object_deref() might access + * arbitrary memory in case of an error */ + new->services = new->attributes = NULL; + + if (host->attributes) { + new->attributes = sdb_llist_clone(host->attributes); + if (! new->attributes) { + 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) { + sdb_object_deref(SDB_OBJ(new)); + return NULL; + } + } + return SDB_OBJ(new); +} /* sdb_host_do_clone */ + static int sdb_attr_init(sdb_object_t *obj, va_list ap) { @@ -126,13 +162,13 @@ sdb_attr_init(sdb_object_t *obj, va_list ap) const char *value = va_arg(ap, const char *); SDB_ATTR(obj)->hostname = strdup(hostname); - SDB_ATTR(obj)->attr_name = strdup(name); + SDB_ATTR(obj)->_name = strdup(name); SDB_ATTR(obj)->attr_value = strdup(value); if ((! SDB_ATTR(obj)->hostname) - || (! SDB_ATTR(obj)->attr_name) || (! SDB_ATTR(obj)->attr_value)) + || (! SDB_ATTR(obj)->_name) || (! SDB_ATTR(obj)->attr_value)) return -1; - SDB_ATTR(obj)->attr_last_update = sdb_gettime(); + SDB_ATTR(obj)->_last_update = sdb_gettime(); return 0; } /* sdb_attr_init */ @@ -143,12 +179,27 @@ sdb_attr_destroy(sdb_object_t *obj) if (SDB_ATTR(obj)->hostname) free(SDB_ATTR(obj)->hostname); - if (SDB_ATTR(obj)->attr_name) - free(SDB_ATTR(obj)->attr_name); + if (SDB_ATTR(obj)->_name) + free(SDB_ATTR(obj)->_name); if (SDB_ATTR(obj)->attr_value) free(SDB_ATTR(obj)->attr_value); } /* sdb_attr_destroy */ +static sdb_object_t * +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); + 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) { @@ -156,11 +207,11 @@ sdb_svc_init(sdb_object_t *obj, va_list ap) const char *name = va_arg(ap, const char *); SDB_SVC(obj)->hostname = strdup(hostname); - SDB_SVC(obj)->svc_name = strdup(name); - if ((! SDB_SVC(obj)->hostname) || (! SDB_SVC(obj)->svc_name)) + SDB_SVC(obj)->_name = strdup(name); + if ((! SDB_SVC(obj)->hostname) || (! SDB_SVC(obj)->_name)) return -1; - SDB_SVC(obj)->svc_last_update = sdb_gettime(); + SDB_SVC(obj)->_last_update = sdb_gettime(); /* ignore errors -> last_update will be updated later */ return 0; } /* sdb_svc_init */ @@ -172,10 +223,48 @@ sdb_svc_destroy(sdb_object_t *obj) if (SDB_SVC(obj)->hostname) free(SDB_SVC(obj)->hostname); - if (SDB_SVC(obj)->svc_name) - free(SDB_SVC(obj)->svc_name); + 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) + return NULL; + + new->_last_update = svc->_last_update; + return SDB_OBJ(new); +} /* sdb_svc_clone */ + +const sdb_type_t sdb_host_type = { + sizeof(sdb_host_t), + + sdb_host_init, + sdb_host_destroy, + sdb_host_do_clone +}; + +const sdb_type_t sdb_attribute_type = { + sizeof(sdb_attribute_t), + + sdb_attr_init, + sdb_attr_destroy, + sdb_attr_clone +}; + +const sdb_type_t sdb_service_type = { + sizeof(sdb_service_t), + + sdb_svc_init, + sdb_svc_destroy, + sdb_svc_clone +}; + /* * public API */ @@ -188,45 +277,12 @@ sdb_host_create(const char *name) if (! name) return NULL; - obj = sdb_object_create(sizeof(sdb_host_t), sdb_host_init, - sdb_host_destroy, name); + obj = sdb_object_create(sdb_host_type, name); if (! obj) return NULL; return SDB_HOST(obj); } /* sdb_host_create */ -sdb_host_t * -sdb_host_clone(const sdb_host_t *host) -{ - sdb_host_t *new; - - new = sdb_host_create(host->host_name); - if (! new) - return NULL; - - /* make sure these are initialized; else sdb_object_deref() might access - * arbitrary memory in case of an error */ - new->services = new->attributes = NULL; - - if (host->attributes) { - new->attributes = sdb_llist_clone(host->attributes); - if (! new->attributes) { - sdb_object_deref(SDB_OBJ(new)); - return NULL; - } - } - - new->host_last_update = host->host_last_update; - if (host->services) { - new->services = sdb_llist_clone(host->services); - if (! new->services) { - sdb_object_deref(SDB_OBJ(new)); - return NULL; - } - } - return new; -} /* sdb_host_clone */ - int sdb_store_host(const sdb_host_t *host) { @@ -236,10 +292,10 @@ sdb_store_host(const sdb_host_t *host) sdb_host_t *old; int status = 0; - if ((! host) || (! host->host_name)) + if ((! host) || (! host->_name)) return -1; - last_update = host->host_last_update; + last_update = host->_last_update; if (last_update <= 0) last_update = sdb_gettime(); @@ -252,29 +308,29 @@ sdb_store_host(const sdb_host_t *host) } } - lookup.obj_name = host->host_name; + lookup.obj_name = host->_name; old = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup, sdb_cmp_store_obj_with_name)); if (old) { - if (old->host_last_update > last_update) { + if (old->_last_update > last_update) { sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - " - "value too old (%"PRIscTIME" < %"PRIscTIME")\n", - host->host_name, last_update, old->host_last_update); + "value too old (%"PRIscTIME" < %"PRIscTIME")", + host->_name, last_update, old->_last_update); /* don't report an error; the host may be updated by multiple * backends */ status = 1; } else { - old->host_last_update = last_update; + old->_last_update = last_update; } } else { - sdb_host_t *new = sdb_host_clone(host); + sdb_host_t *new = SDB_HOST(sdb_host_do_clone(SDB_CONST_OBJ(host))); if (! new) { char errbuf[1024]; - sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: " - "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); + sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); pthread_rwlock_unlock(&host_lock); return -1; } @@ -283,7 +339,7 @@ sdb_store_host(const sdb_host_t *host) if (! (new->attributes = sdb_llist_create())) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "store: Failed to initialize " - "host object '%s': %s\n", host->host_name, + "host object '%s': %s", host->_name, sdb_strerror(errno, errbuf, sizeof(errbuf))); sdb_object_deref(SDB_OBJ(new)); pthread_rwlock_unlock(&host_lock); @@ -295,7 +351,7 @@ sdb_store_host(const sdb_host_t *host) if (! (new->services = sdb_llist_create())) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "store: Failed to initialize " - "host object '%s': %s\n", host->host_name, + "host object '%s': %s", host->_name, sdb_strerror(errno, errbuf, sizeof(errbuf))); sdb_object_deref(SDB_OBJ(new)); pthread_rwlock_unlock(&host_lock); @@ -341,27 +397,12 @@ sdb_attribute_create(const char *hostname, if ((! hostname) || (! name) || (! value)) return NULL; - obj = sdb_object_create(sizeof(sdb_attribute_t), sdb_attr_init, - sdb_attr_destroy, hostname, name, value); + obj = sdb_object_create(sdb_attribute_type, hostname, name, value); if (! obj) return NULL; return SDB_ATTR(obj); } /* sdb_attribute_create */ -sdb_attribute_t * -sdb_attribute_clone(const sdb_attribute_t *attr) -{ - sdb_attribute_t *new; - - new = sdb_attribute_create(attr->hostname, - attr->attr_name, attr->attr_value); - if (! new) - return NULL; - - new->attr_last_update = attr->attr_last_update; - return new; -} /* sdb_attribute_clone */ - int sdb_store_attribute(const sdb_attribute_t *attr) { @@ -377,7 +418,7 @@ sdb_store_attribute(const sdb_attribute_t *attr) if (! attr) return -1; - last_update = attr->attr_last_update; + last_update = attr->_last_update; if (last_update <= 0) last_update = sdb_gettime(); @@ -395,29 +436,29 @@ sdb_store_attribute(const sdb_attribute_t *attr) return -1; } - lookup.obj_name = attr->attr_name; + lookup.obj_name = attr->_name; old = SDB_ATTR(sdb_llist_search(host->attributes, (const sdb_object_t *)&lookup, sdb_cmp_store_obj_with_name)); if (old) { - if (old->host_last_update > last_update) { + if (old->_last_update > last_update) { sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute " - "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")\n", - attr->hostname, attr->attr_name, last_update, - old->host_last_update); + "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")", + attr->hostname, attr->_name, last_update, + old->_last_update); status = 1; } else { - old->attr_last_update = last_update; + old->_last_update = last_update; } } else { - sdb_attribute_t *new = sdb_attribute_clone(attr); + sdb_attribute_t *new = SDB_ATTR(sdb_attr_clone(SDB_CONST_OBJ(attr))); if (! new) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute " - "object: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); + "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf))); pthread_rwlock_unlock(&host_lock); return -1; } @@ -441,26 +482,12 @@ sdb_service_create(const char *hostname, const char *name) if ((! hostname) || (! name)) return NULL; - obj = sdb_object_create(sizeof(sdb_service_t), sdb_svc_init, - sdb_svc_destroy, hostname, name); + obj = sdb_object_create(sdb_service_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) -{ - sdb_service_t *new; - - new = sdb_service_create(svc->hostname, svc->svc_name); - if (! new) - return NULL; - - new->svc_last_update = svc->svc_last_update; - return new; -} /* sdb_service_clone */ - int sdb_store_service(const sdb_service_t *svc) { @@ -476,7 +503,7 @@ sdb_store_service(const sdb_service_t *svc) if (! svc) return -1; - last_update = svc->svc_last_update; + last_update = svc->_last_update; if (last_update <= 0) last_update = sdb_gettime(); @@ -494,28 +521,28 @@ sdb_store_service(const sdb_service_t *svc) return -1; } - lookup.obj_name = svc->svc_name; + 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->host_last_update > last_update) { + if (old->_last_update > last_update) { sdb_log(SDB_LOG_DEBUG, "store: Cannot update service " - "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")\n", - svc->hostname, svc->svc_name, last_update, - old->host_last_update); + "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")", + svc->hostname, svc->_name, last_update, + old->_last_update); status = 1; } else { - old->svc_last_update = last_update; + old->_last_update = last_update; } } else { - sdb_service_t *new = sdb_service_clone(svc); + sdb_service_t *new = SDB_SVC(sdb_svc_clone(SDB_CONST_OBJ(svc))); if (! new) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "store: Failed to clone service " - "object: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); + "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf))); pthread_rwlock_unlock(&host_lock); return -1; } @@ -576,12 +603,12 @@ sdb_store_dump(FILE *fh) assert(host); if (! sdb_strftime(time_str, sizeof(time_str), - "%F %T %z", host->host_last_update)) + "%F %T %z", host->_last_update)) snprintf(time_str, sizeof(time_str), ""); time_str[sizeof(time_str) - 1] = '\0'; fprintf(fh, "Host '%s' (last updated: %s):\n", - host->host_name, time_str); + host->_name, time_str); attr_iter = sdb_llist_get_iter(host->attributes); if (! attr_iter) { @@ -596,12 +623,12 @@ sdb_store_dump(FILE *fh) assert(attr); if (! sdb_strftime(time_str, sizeof(time_str), - "%F %T %z", attr->attr_last_update)) + "%F %T %z", attr->_last_update)) snprintf(time_str, sizeof(time_str), ""); time_str[sizeof(time_str) - 1] = '\0'; fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n", - attr->attr_name, attr->attr_value, time_str); + attr->_name, attr->attr_value, time_str); } sdb_llist_iter_destroy(attr_iter); @@ -619,12 +646,12 @@ sdb_store_dump(FILE *fh) assert(svc); if (! sdb_strftime(time_str, sizeof(time_str), - "%F %T %z", svc->svc_last_update)) + "%F %T %z", svc->_last_update)) snprintf(time_str, sizeof(time_str), ""); time_str[sizeof(time_str) - 1] = '\0'; fprintf(fh, "\tService '%s' (last updated: %s)\n", - svc->svc_name, time_str); + svc->_name, time_str); } sdb_llist_iter_destroy(svc_iter);