X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fcore%2Fstore.c;h=3e9ec55eec519087f63bddf444fb6ed5df24660a;hp=888fbb973449870ae82b5c2910a386e47a2481f9;hb=8b621cd1a68f011c2935d2cb2c23e8476d624699;hpb=ad0100d18b3e1b754a92f36ef91f52382b0b6ead diff --git a/src/core/store.c b/src/core/store.c index 888fbb9..3e9ec55 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -94,6 +94,9 @@ sc_host_init(sc_object_t *obj, va_list ap) SC_HOST(obj)->host_last_update = sc_gettime(); /* ignore errors -> last_update will be updated later */ + SC_HOST(obj)->attributes = sc_llist_create(); + if (! SC_HOST(obj)->attributes) + return -1; SC_HOST(obj)->services = sc_llist_create(); if (! SC_HOST(obj)->services) return -1; @@ -108,10 +111,43 @@ sc_host_destroy(sc_object_t *obj) if (SC_HOST(obj)->host_name) free(SC_HOST(obj)->host_name); + if (SC_HOST(obj)->attributes) + sc_llist_destroy(SC_HOST(obj)->attributes); if (SC_HOST(obj)->services) sc_llist_destroy(SC_HOST(obj)->services); } /* sc_host_destroy */ +static int +sc_attr_init(sc_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 *); + + SC_ATTR(obj)->hostname = strdup(hostname); + SC_ATTR(obj)->attr_name = strdup(name); + SC_ATTR(obj)->attr_value = strdup(value); + if ((! SC_ATTR(obj)->hostname) + || (! SC_ATTR(obj)->attr_name) || (! SC_ATTR(obj)->attr_value)) + return -1; + + SC_ATTR(obj)->attr_last_update = sc_gettime(); + return 0; +} /* sc_attr_init */ + +static void +sc_attr_destroy(sc_object_t *obj) +{ + assert(obj); + + if (SC_ATTR(obj)->hostname) + free(SC_ATTR(obj)->hostname); + if (SC_ATTR(obj)->attr_name) + free(SC_ATTR(obj)->attr_name); + if (SC_ATTR(obj)->attr_value) + free(SC_ATTR(obj)->attr_value); +} /* sc_attr_destroy */ + static int sc_svc_init(sc_object_t *obj, va_list ap) { @@ -167,6 +203,18 @@ sc_host_clone(const sc_host_t *host) if (! clone) return NULL; + /* make sure these are initialized; else sc_object_deref() might access + * arbitrary memory in case of an error */ + clone->services = clone->attributes = NULL; + + if (host->attributes) { + clone->attributes = sc_llist_clone(host->attributes); + if (! clone->attributes) { + sc_object_deref(SC_OBJ(clone)); + return NULL; + } + } + clone->host_last_update = host->host_last_update; if (host->services) { clone->services = sc_llist_clone(host->services); @@ -175,8 +223,6 @@ sc_host_clone(const sc_host_t *host) return NULL; } } - else - clone->services = NULL; return clone; } /* sc_host_clone */ @@ -231,6 +277,17 @@ sc_store_host(const sc_host_t *host) return -1; } + if (! new->attributes) { + if (! (new->attributes = sc_llist_create())) { + char errbuf[1024]; + fprintf(stderr, "store: Failed to initialize " + "host object '%s': %s\n", host->host_name, + sc_strerror(errno, errbuf, sizeof(errbuf))); + sc_object_deref(SC_OBJ(new)); + return -1; + } + } + if (! new->services) { if (! (new->services = sc_llist_create())) { char errbuf[1024]; @@ -271,6 +328,104 @@ sc_store_get_host(const char *name) return host; } /* sc_store_get_host */ +sc_attribute_t * +sc_attribute_create(const char *hostname, + const char *name, const char *value) +{ + sc_object_t *obj; + + if ((! hostname) || (! name) || (! value)) + return NULL; + + obj = sc_object_create(sizeof(sc_attribute_t), sc_attr_init, + sc_attr_destroy, hostname, name, value); + if (! obj) + return NULL; + return SC_ATTR(obj); +} /* sc_attribute_create */ + +sc_attribute_t * +sc_attribute_clone(const sc_attribute_t *attr) +{ + sc_attribute_t *clone; + + clone = sc_attribute_create(attr->hostname, + attr->attr_name, attr->attr_value); + if (! clone) + return NULL; + + clone->attr_last_update = attr->attr_last_update; + return clone; +} /* sc_attribute_clone */ + +int +sc_store_attribute(const sc_attribute_t *attr) +{ + sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT; + sc_host_t *host; + + sc_attribute_t *old; + + sc_time_t last_update; + + int status = 0; + + if (! attr) + return -1; + + last_update = attr->attr_last_update; + if (last_update <= 0) + last_update = sc_gettime(); + + if (! host_list) + return -1; + + pthread_rwlock_wrlock(&host_lock); + + lookup.obj_name = attr->hostname; + host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup, + sc_cmp_store_obj_with_name)); + + if (! host) + return -1; + + lookup.obj_name = attr->attr_name; + old = SC_ATTR(sc_llist_search(host->attributes, + (const sc_object_t *)&lookup, + sc_cmp_store_obj_with_name)); + + if (old) { + if (old->host_last_update > last_update) { + fprintf(stderr, "store: Cannot update attribute '%s/%s' - " + "value too old (%"PRIscTIME" < %"PRIscTIME")\n", + attr->hostname, attr->attr_name, last_update, + old->host_last_update); + status = 1; + } + else { + old->attr_last_update = last_update; + } + } + else { + sc_attribute_t *new = sc_attribute_clone(attr); + if (! new) { + char errbuf[1024]; + fprintf(stderr, "store: Failed to clone attribute object: %s\n", + sc_strerror(errno, errbuf, sizeof(errbuf))); + return -1; + } + + status = sc_llist_insert_sorted(host->attributes, SC_OBJ(new), + sc_store_obj_cmp_by_name); + + /* pass control to the list or destroy in case of an error */ + sc_object_deref(SC_OBJ(new)); + } + + pthread_rwlock_unlock(&host_lock); + return status; +} /* sc_store_attribute */ + sc_service_t * sc_service_create(const char *hostname, const char *name) { @@ -401,6 +556,7 @@ sc_store_dump(FILE *fh) while (sc_llist_iter_has_next(host_iter)) { sc_host_t *host = SC_HOST(sc_llist_iter_get_next(host_iter)); sc_llist_iter_t *svc_iter; + sc_llist_iter_t *attr_iter; char time_str[64]; @@ -414,6 +570,29 @@ sc_store_dump(FILE *fh) fprintf(fh, "Host '%s' (last updated: %s):\n", host->host_name, time_str); + attr_iter = sc_llist_get_iter(host->attributes); + if (! attr_iter) { + char errbuf[1024]; + fprintf(fh, "Failed to retrieve attributes: %s\n", + sc_strerror(errno, errbuf, sizeof(errbuf))); + continue; + } + + while (sc_llist_iter_has_next(attr_iter)) { + sc_attribute_t *attr = SC_ATTR(sc_llist_iter_get_next(attr_iter)); + assert(attr); + + if (! sc_strftime(time_str, sizeof(time_str), + "%F %T %z", attr->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); + } + + sc_llist_iter_destroy(attr_iter); + svc_iter = sc_llist_get_iter(host->services); if (! svc_iter) { char errbuf[1024]; @@ -427,7 +606,7 @@ sc_store_dump(FILE *fh) assert(svc); if (! sc_strftime(time_str, sizeof(time_str), - "%F %T %z", host->host_last_update)) + "%F %T %z", svc->svc_last_update)) snprintf(time_str, sizeof(time_str), ""); time_str[sizeof(time_str) - 1] = '\0';