Code

store: Unified host and service objects into a hierarchical store object.
[sysdb.git] / src / core / store.c
1 /*
2  * SysDB - src/core/store.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "sysdb.h"
29 #include "core/store.h"
30 #include "core/error.h"
31 #include "core/plugin.h"
32 #include "utils/llist.h"
34 #include <assert.h>
36 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
42 #include <pthread.h>
44 /*
45  * private variables
46  */
48 static sdb_llist_t *obj_list = NULL;
49 static pthread_rwlock_t obj_lock = PTHREAD_RWLOCK_INITIALIZER;
51 /*
52  * private types
53  */
55 static sdb_type_t sdb_store_obj_type;
56 static sdb_type_t sdb_attribute_type;
58 struct store_obj;
59 typedef struct store_obj store_obj_t;
61 struct store_obj {
62         sdb_object_t super;
63         sdb_time_t last_update;
64         store_obj_t *parent;
65 };
66 #define STORE_OBJ(obj) ((store_obj_t *)(obj))
67 #define STORE_CONST_OBJ(obj) ((const store_obj_t *)(obj))
69 typedef struct {
70         store_obj_t super;
72         char *value;
73 } sdb_attribute_t;
74 #define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
75 #define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
77 typedef struct {
78         store_obj_t super;
80         int type;
81         sdb_llist_t *children;
83         sdb_llist_t *attributes;
84 } sdb_store_obj_t;
85 #define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
86 #define SDB_CONST_STORE_OBJ(obj) ((const sdb_store_obj_t *)(obj))
88 enum {
89         SDB_HOST = 1,
90         SDB_SERVICE,
91 };
93 /* shortcuts for accessing the sdb_store_obj_t attributes
94  * of inheriting objects */
95 #define _last_update super.last_update
97 static int
98 store_obj_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
99 {
100         store_obj_t *sobj = STORE_OBJ(obj);
101         sobj->last_update = sdb_gettime();
102         /* ignore errors -> last_update will be updated later */
104         sobj->parent = NULL;
105         return 0;
106 } /* store_obj_init */
108 static void
109 store_obj_destroy(sdb_object_t *obj)
111         const store_obj_t *sobj = STORE_OBJ(obj);
113         if (sobj->parent)
114                 sdb_object_deref(SDB_OBJ(sobj->parent));
115 } /* store_obj_destroy */
117 static sdb_object_t *
118 store_obj_clone(const sdb_object_t *obj)
120         const store_obj_t *sobj = STORE_CONST_OBJ(obj);
121         store_obj_t *new;
123         new = STORE_OBJ(sdb_object_create(obj->name, obj->type));
124         if (! new)
125                 return NULL;
127         new->last_update = sobj->last_update;
128         sdb_object_ref(SDB_OBJ(sobj->parent));
129         new->parent = sobj->parent;
130         return SDB_OBJ(new);
131 } /* store_obj_clone */
133 static int
134 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
136         sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
137         int ret;
139         ret = store_obj_init(obj, ap);
140         if (ret)
141                 return ret;
143         sobj->type = va_arg(ap, int);
145         sobj->children = sdb_llist_create();
146         if (! sobj->children)
147                 return -1;
148         sobj->attributes = sdb_llist_create();
149         if (! sobj->attributes)
150                 return -1;
151         return 0;
152 } /* sdb_store_obj_init */
154 static void
155 sdb_store_obj_destroy(sdb_object_t *obj)
157         sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
159         assert(obj);
161         store_obj_destroy(obj);
163         if (sobj->children)
164                 sdb_llist_destroy(sobj->children);
165         if (sobj->attributes)
166                 sdb_llist_destroy(sobj->attributes);
167 } /* sdb_store_obj_destroy */
169 static sdb_object_t *
170 sdb_store_obj_clone(const sdb_object_t *obj)
172         const sdb_store_obj_t *sobj = SDB_CONST_STORE_OBJ(obj);
173         sdb_store_obj_t *new;
175         new = SDB_STORE_OBJ(store_obj_clone(obj));
176         if (! new)
177                 return NULL;
179         new->type = sobj->type;
181         /* make sure these are initialized; else sdb_object_deref() might access
182          * arbitrary memory in case of an error */
183         new->children = new->attributes = NULL;
185         if (sobj->children) {
186                 new->children = sdb_llist_clone(sobj->children);
187                 if (! new->children) {
188                         sdb_object_deref(SDB_OBJ(new));
189                         return NULL;
190                 }
191         }
192         if (sobj->attributes) {
193                 new->attributes = sdb_llist_clone(sobj->attributes);
194                 if (! new->attributes) {
195                         sdb_object_deref(SDB_OBJ(new));
196                         return NULL;
197                 }
198         }
200         new->_last_update = sobj->_last_update;
201         return SDB_OBJ(new);
202 } /* sdb_store_obj_clone */
204 static int
205 sdb_attr_init(sdb_object_t *obj, va_list ap)
207         const char *value = va_arg(ap, const char *);
208         int ret;
210         ret = store_obj_init(obj, ap);
211         if (ret)
212                 return ret;
214         SDB_ATTR(obj)->value = strdup(value);
215         if (! SDB_ATTR(obj)->value)
216                 return -1;
217         return 0;
218 } /* sdb_attr_init */
220 static void
221 sdb_attr_destroy(sdb_object_t *obj)
223         assert(obj);
225         store_obj_destroy(obj);
227         if (SDB_ATTR(obj)->value)
228                 free(SDB_ATTR(obj)->value);
229 } /* sdb_attr_destroy */
231 static sdb_object_t *
232 sdb_attr_clone(const sdb_object_t *obj)
234         const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
235         sdb_attribute_t *new;
237         new = SDB_ATTR(store_obj_clone(obj));
238         if (! new)
239                 return NULL;
241         if (attr->value)
242                 new->value = strdup(attr->value);
243         if (! new->value) {
244                 sdb_object_deref(SDB_OBJ(new));
245                 return NULL;
246         }
247         return SDB_OBJ(new);
248 } /* sdb_attr_clone */
250 static sdb_type_t sdb_store_obj_type = {
251         sizeof(sdb_store_obj_t),
253         sdb_store_obj_init,
254         sdb_store_obj_destroy,
255         sdb_store_obj_clone
256 };
258 static sdb_type_t sdb_attribute_type = {
259         sizeof(sdb_attribute_t),
261         sdb_attr_init,
262         sdb_attr_destroy,
263         sdb_attr_clone
264 };
266 /*
267  * private helper functions
268  */
270 static sdb_store_obj_t *
271 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
273         sdb_llist_iter_t *iter;
275         if (! l)
276                 return NULL;
278         iter = sdb_llist_get_iter(l);
279         if (! iter)
280                 return NULL;
282         while (sdb_llist_iter_has_next(iter)) {
283                 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
284                 assert(sobj);
286                 if ((sobj->type == type)
287                                 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
288                         sdb_llist_iter_destroy(iter);
289                         return sobj;
290                 }
292                 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
293                 if (sobj) {
294                         sdb_llist_iter_destroy(iter);
295                         return sobj;
296                 }
297         }
298         sdb_llist_iter_destroy(iter);
299         return NULL;
300 } /* sdb_store_lookup_in_list */
302 static sdb_store_obj_t *
303 sdb_store_lookup(int type, const char *name)
305         return sdb_store_lookup_in_list(obj_list, type, name);
306 } /* sdb_store_lookup */
308 /*
309  * public API
310  */
312 int
313 sdb_store_host(const char *name, sdb_time_t last_update)
315         sdb_store_obj_t *old;
317         char *cname;
318         int status = 0;
320         if (! name)
321                 return -1;
323         cname = sdb_plugin_cname(strdup(name));
324         if (! cname) {
325                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
326                 return -1;
327         }
329         if (last_update <= 0)
330                 last_update = sdb_gettime();
332         pthread_rwlock_wrlock(&obj_lock);
334         if (! obj_list) {
335                 if (! (obj_list = sdb_llist_create())) {
336                         pthread_rwlock_unlock(&obj_lock);
337                         return -1;
338                 }
339         }
341         old = sdb_store_lookup(SDB_HOST, cname);
343         if (old) {
344                 if (old->_last_update > last_update) {
345                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - "
346                                         "value too old (%"PRIscTIME" < %"PRIscTIME")",
347                                         cname, last_update, old->_last_update);
348                         /* don't report an error; the host may be updated by multiple
349                          * backends */
350                         status = 1;
351                 }
352                 else {
353                         old->_last_update = last_update;
354                 }
355                 free(cname);
356         }
357         else {
358                 sdb_store_obj_t *new = SDB_STORE_OBJ(sdb_object_create(cname,
359                                         sdb_store_obj_type, SDB_HOST));
360                 if (! new) {
361                         char errbuf[1024];
362                         sdb_log(SDB_LOG_ERR, "store: Failed to create host %s: %s",
363                                         cname, sdb_strerror(errno, errbuf, sizeof(errbuf)));
364                         free(cname);
365                         pthread_rwlock_unlock(&obj_lock);
366                         return -1;
367                 }
368                 free(cname);
370                 /* TODO: add support for hierarchical inserts */
371                 status = sdb_llist_insert_sorted(obj_list, SDB_OBJ(new),
372                                 sdb_object_cmp_by_name);
374                 /* pass control to the list or destroy in case of an error */
375                 sdb_object_deref(SDB_OBJ(new));
376         }
378         pthread_rwlock_unlock(&obj_lock);
379         return status;
380 } /* sdb_store_host */
382 _Bool
383 sdb_store_has_host(const char *name)
385         sdb_store_obj_t *host;
387         if (! name)
388                 return NULL;
390         host = sdb_store_lookup(SDB_HOST, name);
391         return host != NULL;
392 } /* sdb_store_has_host */
394 int
395 sdb_store_attribute(const char *hostname, const char *key, const char *value,
396                 sdb_time_t last_update)
398         sdb_store_obj_t *sobj;
399         sdb_attribute_t *old;
401         int status = 0;
403         if ((! hostname) || (! key))
404                 return -1;
406         if (last_update <= 0)
407                 last_update = sdb_gettime();
409         if (! obj_list)
410                 return -1;
412         pthread_rwlock_wrlock(&obj_lock);
414         sobj = sdb_store_lookup(SDB_HOST, hostname);
415         if (! sobj) {
416                 pthread_rwlock_unlock(&obj_lock);
417                 return -1;
418         }
420         old = SDB_ATTR(sdb_llist_search_by_name(sobj->attributes, key));
421         if (old) {
422                 if (old->_last_update > last_update) {
423                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
424                                         "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
425                                         hostname, key, last_update, old->_last_update);
426                         status = 1;
427                 }
428                 else {
429                         old->_last_update = last_update;
430                 }
431         }
432         else {
433                 sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key,
434                                         sdb_attribute_type, value));
435                 if (! new) {
436                         char errbuf[1024];
437                         sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
438                                         "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
439                         pthread_rwlock_unlock(&obj_lock);
440                         return -1;
441                 }
443                 status = sdb_llist_insert_sorted(sobj->attributes, SDB_OBJ(new),
444                                 sdb_object_cmp_by_name);
446                 /* pass control to the list or destroy in case of an error */
447                 sdb_object_deref(SDB_OBJ(new));
448         }
450         pthread_rwlock_unlock(&obj_lock);
451         return status;
452 } /* sdb_store_attribute */
454 int
455 sdb_store_service(const char *hostname, const char *name,
456                 sdb_time_t last_update)
458         sdb_store_obj_t *host;
459         sdb_store_obj_t *old;
461         int status = 0;
463         if ((! hostname) || (! name))
464                 return -1;
466         if (last_update <= 0)
467                 last_update = sdb_gettime();
469         if (! obj_list)
470                 return -1;
472         pthread_rwlock_wrlock(&obj_lock);
474         host = sdb_store_lookup(SDB_HOST, hostname);
475         if (! host) {
476                 pthread_rwlock_unlock(&obj_lock);
477                 return -1;
478         }
480         /* TODO: only look into direct children? */
481         old = sdb_store_lookup_in_list(host->children, SDB_SERVICE, name);
482         if (old) {
483                 if (old->_last_update > last_update) {
484                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update service "
485                                         "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
486                                         hostname, name, last_update, old->_last_update);
487                         status = 1;
488                 }
489                 else {
490                         old->_last_update = last_update;
491                 }
492         }
493         else {
494                 sdb_store_obj_t *new = SDB_STORE_OBJ(sdb_object_create(name,
495                                         sdb_store_obj_type, SDB_SERVICE));
496                 if (! new) {
497                         char errbuf[1024];
498                         sdb_log(SDB_LOG_ERR, "store: Failed to clone service "
499                                         "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
500                         pthread_rwlock_unlock(&obj_lock);
501                         return -1;
502                 }
504                 status = sdb_llist_insert_sorted(host->children, SDB_OBJ(new),
505                                 sdb_object_cmp_by_name);
507                 /* pass control to the list or destroy in case of an error */
508                 sdb_object_deref(SDB_OBJ(new));
509         }
511         pthread_rwlock_unlock(&obj_lock);
512         return status;
513 } /* sdb_store_service */
515 /* TODO: actually support hierarchical data */
516 int
517 sdb_store_dump(FILE *fh)
519         sdb_llist_iter_t *host_iter;
521         if (! fh)
522                 return -1;
524         pthread_rwlock_rdlock(&obj_lock);
526         host_iter = sdb_llist_get_iter(obj_list);
527         if (! host_iter) {
528                 pthread_rwlock_unlock(&obj_lock);
529                 return -1;
530         }
532         while (sdb_llist_iter_has_next(host_iter)) {
533                 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
534                 sdb_llist_iter_t *svc_iter;
535                 sdb_llist_iter_t *attr_iter;
537                 char time_str[64];
539                 assert(host);
541                 if (! sdb_strftime(time_str, sizeof(time_str),
542                                         "%F %T %z", host->_last_update))
543                         snprintf(time_str, sizeof(time_str), "<error>");
544                 time_str[sizeof(time_str) - 1] = '\0';
546                 fprintf(fh, "Host '%s' (last updated: %s):\n",
547                                 SDB_OBJ(host)->name, time_str);
549                 attr_iter = sdb_llist_get_iter(host->attributes);
550                 if (! attr_iter) {
551                         char errbuf[1024];
552                         fprintf(fh, "Failed to retrieve attributes: %s\n",
553                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
554                         continue;
555                 }
557                 while (sdb_llist_iter_has_next(attr_iter)) {
558                         sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
559                         assert(attr);
561                         if (! sdb_strftime(time_str, sizeof(time_str),
562                                                 "%F %T %z", attr->_last_update))
563                                 snprintf(time_str, sizeof(time_str), "<error>");
564                         time_str[sizeof(time_str) - 1] = '\0';
566                         fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
567                                         SDB_OBJ(attr)->name, attr->value, time_str);
568                 }
570                 sdb_llist_iter_destroy(attr_iter);
572                 svc_iter = sdb_llist_get_iter(host->children);
573                 if (! svc_iter) {
574                         char errbuf[1024];
575                         fprintf(fh, "Failed to retrieve services: %s\n",
576                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
577                         continue;
578                 }
580                 while (sdb_llist_iter_has_next(svc_iter)) {
581                         sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
582                         assert(svc);
584                         if (! sdb_strftime(time_str, sizeof(time_str),
585                                                 "%F %T %z", svc->_last_update))
586                                 snprintf(time_str, sizeof(time_str), "<error>");
587                         time_str[sizeof(time_str) - 1] = '\0';
589                         fprintf(fh, "\tService '%s' (last updated: %s)\n",
590                                         SDB_OBJ(svc)->name, time_str);
591                 }
593                 sdb_llist_iter_destroy(svc_iter);
594         }
596         sdb_llist_iter_destroy(host_iter);
597         pthread_rwlock_unlock(&obj_lock);
598         return 0;
599 } /* sdb_store_dump */
601 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */