Code

store: Use generic store function also to store attributes.
[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         SDB_ATTRIBUTE,
92 };
93 #define TYPE_TO_NAME(t) \
94         (((t) == SDB_HOST) ? "host" \
95                 : ((t) == SDB_SERVICE) ? "service" \
96                 : ((t) == SDB_ATTRIBUTE) ? "attribute" : "unknown")
98 /* shortcuts for accessing the sdb_store_obj_t attributes
99  * of inheriting objects */
100 #define _last_update super.last_update
102 static int
103 store_obj_init(sdb_object_t *obj, va_list ap)
105         store_obj_t *sobj = STORE_OBJ(obj);
106         sobj->last_update = va_arg(ap, sdb_time_t);
108         sobj->parent = NULL;
109         return 0;
110 } /* store_obj_init */
112 static void
113 store_obj_destroy(sdb_object_t *obj)
115         const store_obj_t *sobj = STORE_OBJ(obj);
117         if (sobj->parent)
118                 sdb_object_deref(SDB_OBJ(sobj->parent));
119 } /* store_obj_destroy */
121 /* this may not be used as a type on its own but only as helper functions for
122  * the derived types; the function accepts a variadic list of arguments to
123  * pass to the sub-type's init function */
124 static sdb_object_t *
125 store_obj_clone(const sdb_object_t *obj, ...)
127         const store_obj_t *sobj = STORE_CONST_OBJ(obj);
128         store_obj_t *new;
130         va_list ap;
132         va_start(ap, obj);
134         new = STORE_OBJ(sdb_object_vcreate(obj->name, obj->type, ap));
135         va_end(ap);
136         if (! new)
137                 return NULL;
139         new->last_update = sobj->last_update;
140         sdb_object_ref(SDB_OBJ(sobj->parent));
141         new->parent = sobj->parent;
142         return SDB_OBJ(new);
143 } /* store_obj_clone */
145 static int
146 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
148         sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
149         int ret;
151         ret = store_obj_init(obj, ap);
152         if (ret)
153                 return ret;
155         sobj->type = va_arg(ap, int);
157         sobj->children = sdb_llist_create();
158         if (! sobj->children)
159                 return -1;
160         sobj->attributes = sdb_llist_create();
161         if (! sobj->attributes)
162                 return -1;
163         return 0;
164 } /* sdb_store_obj_init */
166 static void
167 sdb_store_obj_destroy(sdb_object_t *obj)
169         sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
171         assert(obj);
173         store_obj_destroy(obj);
175         if (sobj->children)
176                 sdb_llist_destroy(sobj->children);
177         if (sobj->attributes)
178                 sdb_llist_destroy(sobj->attributes);
179 } /* sdb_store_obj_destroy */
181 static sdb_object_t *
182 sdb_store_obj_clone(const sdb_object_t *obj)
184         const sdb_store_obj_t *sobj = SDB_CONST_STORE_OBJ(obj);
185         sdb_store_obj_t *new;
187         new = SDB_STORE_OBJ(store_obj_clone(obj, sobj->_last_update, sobj->type));
188         if (! new)
189                 return NULL;
191         if (sobj->children) {
192                 sdb_llist_destroy(new->children);
193                 new->children = sdb_llist_clone(sobj->children);
194                 if (! new->children) {
195                         sdb_object_deref(SDB_OBJ(new));
196                         return NULL;
197                 }
198         }
199         if (sobj->attributes) {
200                 sdb_llist_destroy(new->attributes);
201                 new->attributes = sdb_llist_clone(sobj->attributes);
202                 if (! new->attributes) {
203                         sdb_object_deref(SDB_OBJ(new));
204                         return NULL;
205                 }
206         }
208         return SDB_OBJ(new);
209 } /* sdb_store_obj_clone */
211 static int
212 sdb_attr_init(sdb_object_t *obj, va_list ap)
214         const char *value;
215         int ret;
217         ret = store_obj_init(obj, ap);
218         if (ret)
219                 return ret;
220         value = va_arg(ap, const char *);
222         SDB_ATTR(obj)->value = strdup(value);
223         if (! SDB_ATTR(obj)->value)
224                 return -1;
225         return 0;
226 } /* sdb_attr_init */
228 static void
229 sdb_attr_destroy(sdb_object_t *obj)
231         assert(obj);
233         store_obj_destroy(obj);
235         if (SDB_ATTR(obj)->value)
236                 free(SDB_ATTR(obj)->value);
237 } /* sdb_attr_destroy */
239 static sdb_object_t *
240 sdb_attr_clone(const sdb_object_t *obj)
242         const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
243         sdb_attribute_t *new;
245         new = SDB_ATTR(store_obj_clone(obj, attr->_last_update, attr->value));
246         if (! new)
247                 return NULL;
248         return SDB_OBJ(new);
249 } /* sdb_attr_clone */
251 static sdb_type_t sdb_store_obj_type = {
252         sizeof(sdb_store_obj_t),
254         sdb_store_obj_init,
255         sdb_store_obj_destroy,
256         sdb_store_obj_clone
257 };
259 static sdb_type_t sdb_attribute_type = {
260         sizeof(sdb_attribute_t),
262         sdb_attr_init,
263         sdb_attr_destroy,
264         sdb_attr_clone
265 };
267 /*
268  * private helper functions
269  */
271 static sdb_store_obj_t *
272 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
274         sdb_llist_iter_t *iter;
276         if (! l)
277                 return NULL;
279         iter = sdb_llist_get_iter(l);
280         if (! iter)
281                 return NULL;
283         while (sdb_llist_iter_has_next(iter)) {
284                 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
285                 assert(sobj);
287                 if ((sobj->type == type)
288                                 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
289                         sdb_llist_iter_destroy(iter);
290                         return sobj;
291                 }
293                 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
294                 if (sobj) {
295                         sdb_llist_iter_destroy(iter);
296                         return sobj;
297                 }
298         }
299         sdb_llist_iter_destroy(iter);
300         return NULL;
301 } /* sdb_store_lookup_in_list */
303 static sdb_store_obj_t *
304 sdb_store_lookup(int type, const char *name)
306         return sdb_store_lookup_in_list(obj_list, type, name);
307 } /* sdb_store_lookup */
309 static int
310 store_obj(int parent_type, const char *parent_name,
311                 int type, const char *name, sdb_time_t last_update,
312                 store_obj_t **updated_obj)
314         sdb_llist_t *parent_list;
315         store_obj_t *old;
316         int status = 0;
318         if (! name)
319                 return -1;
321         if (last_update <= 0)
322                 last_update = sdb_gettime();
324         assert((parent_type == 0)
325                         || (parent_type == SDB_HOST)
326                         || (parent_type == SDB_SERVICE));
327         assert((type == 0)
328                         || (type == SDB_HOST)
329                         || (type == SDB_SERVICE)
330                         || (type == SDB_ATTRIBUTE));
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         }
340         parent_list = obj_list;
342         if (parent_type && parent_name) {
343                 sdb_store_obj_t *parent;
345                 parent = sdb_store_lookup(parent_type, parent_name);
346                 if (! parent) {
347                         sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
348                                         "parent %s '%s' not found", TYPE_TO_NAME(type), name,
349                                         TYPE_TO_NAME(parent_type), parent_name);
350                         pthread_rwlock_unlock(&obj_lock);
351                         return -1;
352                 }
354                 if (type == SDB_ATTRIBUTE)
355                         parent_list = parent->attributes;
356                 else
357                         parent_list = parent->children;
358         }
360         /* TODO: only look into direct children? */
361         if (type == SDB_ATTRIBUTE)
362                 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
363         else
364                 old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
366         if (old) {
367                 if (old->last_update > last_update) {
368                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
369                                         "value too old (%"PRIscTIME" < %"PRIscTIME")",
370                                         TYPE_TO_NAME(type), name, last_update, old->last_update);
371                         /* don't report an error; the object may be updated by multiple
372                          * backends */
373                         status = 1;
374                 }
375                 else {
376                         old->last_update = last_update;
377                 }
379                 if (updated_obj)
380                         *updated_obj = old;
381         }
382         else {
383                 store_obj_t *new;
385                 if (type == SDB_ATTRIBUTE)
386                         /* the value will be updated by the caller */
387                         new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
388                                                 last_update, NULL));
389                 else
390                         new = STORE_OBJ(sdb_object_create(name, sdb_store_obj_type,
391                                                 last_update, type));
393                 if (! new) {
394                         char errbuf[1024];
395                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
396                                         TYPE_TO_NAME(type), name,
397                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
398                         pthread_rwlock_unlock(&obj_lock);
399                         return -1;
400                 }
402                 /* TODO: insert type-aware; the current version works as long as we
403                  * don't support to store hierarchical data */
404                 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
405                                 sdb_object_cmp_by_name);
407                 /* pass control to the list or destroy in case of an error */
408                 sdb_object_deref(SDB_OBJ(new));
410                 if (updated_obj)
411                         *updated_obj = new;
412         }
414         pthread_rwlock_unlock(&obj_lock);
415         return status;
416 } /* sdb_store_obj */
418 /*
419  * public API
420  */
422 int
423 sdb_store_host(const char *name, sdb_time_t last_update)
425         char *cname;
426         int status;
428         if (! name)
429                 return -1;
431         cname = sdb_plugin_cname(strdup(name));
432         if (! cname) {
433                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
434                 return -1;
435         }
437         status = store_obj(/* parent = */ 0, NULL,
438                         /* stored object = */ SDB_HOST, cname, last_update,
439                         /* updated_obj = */ NULL);
440         free(cname);
441         return status;
442 } /* sdb_store_host */
444 _Bool
445 sdb_store_has_host(const char *name)
447         sdb_store_obj_t *host;
449         if (! name)
450                 return NULL;
452         host = sdb_store_lookup(SDB_HOST, name);
453         return host != NULL;
454 } /* sdb_store_has_host */
456 int
457 sdb_store_attribute(const char *hostname, const char *key, const char *value,
458                 sdb_time_t last_update)
460         char *cname;
461         int status;
463         store_obj_t *updated_attr = NULL;
465         if ((! hostname) || (! key))
466                 return -1;
468         cname = sdb_plugin_cname(strdup(hostname));
469         if (! cname) {
470                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
471                 return -1;
472         }
474         status = store_obj(/* parent = */ SDB_HOST, cname,
475                         /* stored object = */ SDB_ATTRIBUTE, key, last_update,
476                         &updated_attr);
477         free(cname);
479         SDB_ATTR(updated_attr)->value = strdup(value);
480         if (! SDB_ATTR(updated_attr)->value) {
481                 sdb_object_deref(SDB_OBJ(updated_attr));
482                 status = -1;
483         }
484         return status;
485 } /* sdb_store_attribute */
487 int
488 sdb_store_service(const char *hostname, const char *name,
489                 sdb_time_t last_update)
491         char *cname;
492         int status;
494         if ((! hostname) || (! name))
495                 return -1;
497         cname = sdb_plugin_cname(strdup(hostname));
498         if (! cname) {
499                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
500                 return -1;
501         }
503         status = store_obj(/* parent = */ SDB_HOST, cname,
504                         /* stored object = */ SDB_SERVICE, name, last_update,
505                         /* updated obj = */ NULL);
506         free(cname);
507         return status;
508 } /* sdb_store_service */
510 /* TODO: actually support hierarchical data */
511 int
512 sdb_store_dump(FILE *fh)
514         sdb_llist_iter_t *host_iter;
516         if (! fh)
517                 return -1;
519         pthread_rwlock_rdlock(&obj_lock);
521         host_iter = sdb_llist_get_iter(obj_list);
522         if (! host_iter) {
523                 pthread_rwlock_unlock(&obj_lock);
524                 return -1;
525         }
527         while (sdb_llist_iter_has_next(host_iter)) {
528                 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
529                 sdb_llist_iter_t *svc_iter;
530                 sdb_llist_iter_t *attr_iter;
532                 char time_str[64];
534                 assert(host);
536                 if (! sdb_strftime(time_str, sizeof(time_str),
537                                         "%F %T %z", host->_last_update))
538                         snprintf(time_str, sizeof(time_str), "<error>");
539                 time_str[sizeof(time_str) - 1] = '\0';
541                 fprintf(fh, "Host '%s' (last updated: %s):\n",
542                                 SDB_OBJ(host)->name, time_str);
544                 attr_iter = sdb_llist_get_iter(host->attributes);
545                 if (! attr_iter) {
546                         char errbuf[1024];
547                         fprintf(fh, "Failed to retrieve attributes: %s\n",
548                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
549                         continue;
550                 }
552                 while (sdb_llist_iter_has_next(attr_iter)) {
553                         sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
554                         assert(attr);
556                         if (! sdb_strftime(time_str, sizeof(time_str),
557                                                 "%F %T %z", attr->_last_update))
558                                 snprintf(time_str, sizeof(time_str), "<error>");
559                         time_str[sizeof(time_str) - 1] = '\0';
561                         fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
562                                         SDB_OBJ(attr)->name, attr->value, time_str);
563                 }
565                 sdb_llist_iter_destroy(attr_iter);
567                 svc_iter = sdb_llist_get_iter(host->children);
568                 if (! svc_iter) {
569                         char errbuf[1024];
570                         fprintf(fh, "Failed to retrieve services: %s\n",
571                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
572                         continue;
573                 }
575                 while (sdb_llist_iter_has_next(svc_iter)) {
576                         sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
577                         assert(svc);
579                         if (! sdb_strftime(time_str, sizeof(time_str),
580                                                 "%F %T %z", svc->_last_update))
581                                 snprintf(time_str, sizeof(time_str), "<error>");
582                         time_str[sizeof(time_str) - 1] = '\0';
584                         fprintf(fh, "\tService '%s' (last updated: %s)\n",
585                                         SDB_OBJ(svc)->name, time_str);
586                 }
588                 sdb_llist_iter_destroy(svc_iter);
589         }
591         sdb_llist_iter_destroy(host_iter);
592         pthread_rwlock_unlock(&obj_lock);
593         return 0;
594 } /* sdb_store_dump */
596 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */