diff --git a/src/core/store.c b/src/core/store.c
index f80c49e15d8964c606a32dad0470755cef9efed3..3e9ec55eec519087f63bddf444fb6ed5df24660a 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
#include <pthread.h>
+/*
+ * private data types
+ */
+
+/* type used for looking up named objects */
+typedef struct {
+ sc_object_t parent;
+ const char *obj_name;
+} sc_store_lookup_obj_t;
+#define SC_STORE_LOOKUP_OBJ_INIT { SC_OBJECT_INIT, NULL }
+
/*
* private variables
*/
return strcasecmp(h1->name, h2->name);
} /* sc_store_obj_cmp_by_name */
+static int
+sc_cmp_store_obj_with_name(const sc_object_t *a, const sc_object_t *b)
+{
+ const sc_store_obj_t *obj = (const sc_store_obj_t *)a;
+ const sc_store_lookup_obj_t *lookup = (const sc_store_lookup_obj_t *)b;
+
+ assert(obj && lookup);
+ return strcasecmp(obj->name, lookup->obj_name);
+} /* sc_cmp_store_obj_with_name */
+
static int
sc_host_init(sc_object_t *obj, va_list ap)
{
- char *name = va_arg(ap, char *);
+ const char *name = va_arg(ap, const char *);
SC_HOST(obj)->host_name = strdup(name);
if (! SC_HOST(obj)->host_name)
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;
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)
{
- char *hostname = va_arg(ap, char *);
- char *name = va_arg(ap, char *);
+ const char *hostname = va_arg(ap, const char *);
+ const char *name = va_arg(ap, const char *);
SC_SVC(obj)->hostname = strdup(hostname);
SC_SVC(obj)->svc_name = strdup(name);
*/
sc_host_t *
-sc_host_create(char *name)
+sc_host_create(const char *name)
{
sc_object_t *obj;
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);
return NULL;
}
}
- else
- clone->services = NULL;
return clone;
} /* sc_host_clone */
int
sc_store_host(const sc_host_t *host)
{
+ sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
sc_time_t last_update;
sc_host_t *old;
}
}
- old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)host,
- sc_store_obj_cmp_by_name));
+ lookup.obj_name = host->host_name;
+ old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
+ sc_cmp_store_obj_with_name));
if (old) {
if (old->host_last_update > last_update) {
host->host_name, last_update, old->host_last_update);
/* don't report an error; the host may be updated by multiple
* backends */
- status = 0;
+ status = 1;
}
else {
old->host_last_update = last_update;
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];
} /* sc_store_host */
const sc_host_t *
-sc_store_get_host(char *name)
+sc_store_get_host(const char *name)
{
- sc_host_t tmp = SC_HOST_INIT;
+ sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
sc_host_t *host;
if (! name)
return NULL;
- tmp.host_name = name;
- host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp,
- sc_store_obj_cmp_by_name));
+ lookup.obj_name = name;
+ host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
+ sc_cmp_store_obj_with_name));
if (! host)
return NULL;
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(char *hostname, char *name)
+sc_service_create(const char *hostname, const char *name)
{
sc_object_t *obj;
int
sc_store_service(const sc_service_t *svc)
{
- sc_host_t tmp = SC_HOST_INIT;
+ sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
sc_host_t *host;
sc_service_t *old;
pthread_rwlock_wrlock(&host_lock);
- tmp.host_name = svc->hostname;
- host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp,
- sc_store_obj_cmp_by_name));
+ lookup.obj_name = svc->hostname;
+ host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
+ sc_cmp_store_obj_with_name));
if (! host)
return -1;
- old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)svc,
- sc_store_obj_cmp_by_name));
+ lookup.obj_name = svc->svc_name;
+ old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup,
+ sc_cmp_store_obj_with_name));
if (old) {
if (old->host_last_update > last_update) {
"value too old (%"PRIscTIME" < %"PRIscTIME")\n",
svc->hostname, svc->svc_name, last_update,
old->host_last_update);
- status = -1;
+ status = 1;
}
else {
old->svc_last_update = last_update;
} /* sc_store_service */
const sc_service_t *
-sc_store_get_service(const sc_host_t *host, char *name)
+sc_store_get_service(const sc_host_t *host, const char *name)
{
- sc_service_t tmp = SC_SVC_INIT;
+ sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
sc_service_t *svc;
if ((! host) || (! name))
return NULL;
- tmp.svc_name = name;
- svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&tmp,
- sc_store_obj_cmp_by_name));
+ lookup.obj_name = name;
+ svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup,
+ sc_cmp_store_obj_with_name));
if (! svc)
return NULL;
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];
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), "<error>");
+ 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];
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), "<error>");
time_str[sizeof(time_str) - 1] = '\0';