Code

store: Pass the right timestamp to newly created store objects.
[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 };
92 #define TYPE_TO_NAME(t) \
93         (((t) == SDB_HOST) ? "host" \
94                 : ((t) == SDB_SERVICE) ? "service" : "unknown")
96 /* shortcuts for accessing the sdb_store_obj_t attributes
97  * of inheriting objects */
98 #define _last_update super.last_update
100 static int
101 store_obj_init(sdb_object_t *obj, va_list ap)
103         store_obj_t *sobj = STORE_OBJ(obj);
104         sobj->last_update = va_arg(ap, sdb_time_t);
106         sobj->parent = NULL;
107         return 0;
108 } /* store_obj_init */
110 static void
111 store_obj_destroy(sdb_object_t *obj)
113         const store_obj_t *sobj = STORE_OBJ(obj);
115         if (sobj->parent)
116                 sdb_object_deref(SDB_OBJ(sobj->parent));
117 } /* store_obj_destroy */
119 /* this may not be used as a type on its own but only as helper functions for
120  * the derived types; the function accepts a variadic list of arguments to
121  * pass to the sub-type's init function */
122 static sdb_object_t *
123 store_obj_clone(const sdb_object_t *obj, ...)
125         const store_obj_t *sobj = STORE_CONST_OBJ(obj);
126         store_obj_t *new;
128         va_list ap;
130         va_start(ap, obj);
132         new = STORE_OBJ(sdb_object_vcreate(obj->name, obj->type, ap));
133         va_end(ap);
134         if (! new)
135                 return NULL;
137         new->last_update = sobj->last_update;
138         sdb_object_ref(SDB_OBJ(sobj->parent));
139         new->parent = sobj->parent;
140         return SDB_OBJ(new);
141 } /* store_obj_clone */
143 static int
144 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
146         sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
147         int ret;
149         ret = store_obj_init(obj, ap);
150         if (ret)
151                 return ret;
153         sobj->type = va_arg(ap, int);
155         sobj->children = sdb_llist_create();
156         if (! sobj->children)
157                 return -1;
158         sobj->attributes = sdb_llist_create();
159         if (! sobj->attributes)
160                 return -1;
161         return 0;
162 } /* sdb_store_obj_init */
164 static void
165 sdb_store_obj_destroy(sdb_object_t *obj)
167         sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
169         assert(obj);
171         store_obj_destroy(obj);
173         if (sobj->children)
174                 sdb_llist_destroy(sobj->children);
175         if (sobj->attributes)
176                 sdb_llist_destroy(sobj->attributes);
177 } /* sdb_store_obj_destroy */
179 static sdb_object_t *
180 sdb_store_obj_clone(const sdb_object_t *obj)
182         const sdb_store_obj_t *sobj = SDB_CONST_STORE_OBJ(obj);
183         sdb_store_obj_t *new;
185         new = SDB_STORE_OBJ(store_obj_clone(obj, sobj->_last_update, sobj->type));
186         if (! new)
187                 return NULL;
189         if (sobj->children) {
190                 sdb_llist_destroy(new->children);
191                 new->children = sdb_llist_clone(sobj->children);
192                 if (! new->children) {
193                         sdb_object_deref(SDB_OBJ(new));
194                         return NULL;
195                 }
196         }
197         if (sobj->attributes) {
198                 sdb_llist_destroy(new->attributes);
199                 new->attributes = sdb_llist_clone(sobj->attributes);
200                 if (! new->attributes) {
201                         sdb_object_deref(SDB_OBJ(new));
202                         return NULL;
203                 }
204         }
206         return SDB_OBJ(new);
207 } /* sdb_store_obj_clone */
209 static int
210 sdb_attr_init(sdb_object_t *obj, va_list ap)
212         const char *value;
213         int ret;
215         ret = store_obj_init(obj, ap);
216         if (ret)
217                 return ret;
218         value = va_arg(ap, const char *);
220         SDB_ATTR(obj)->value = strdup(value);
221         if (! SDB_ATTR(obj)->value)
222                 return -1;
223         return 0;
224 } /* sdb_attr_init */
226 static void
227 sdb_attr_destroy(sdb_object_t *obj)
229         assert(obj);
231         store_obj_destroy(obj);
233         if (SDB_ATTR(obj)->value)
234                 free(SDB_ATTR(obj)->value);
235 } /* sdb_attr_destroy */
237 static sdb_object_t *
238 sdb_attr_clone(const sdb_object_t *obj)
240         const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
241         sdb_attribute_t *new;
243         new = SDB_ATTR(store_obj_clone(obj, attr->_last_update, attr->value));
244         if (! new)
245                 return NULL;
246         return SDB_OBJ(new);
247 } /* sdb_attr_clone */
249 static sdb_type_t sdb_store_obj_type = {
250         sizeof(sdb_store_obj_t),
252         sdb_store_obj_init,
253         sdb_store_obj_destroy,
254         sdb_store_obj_clone
255 };
257 static sdb_type_t sdb_attribute_type = {
258         sizeof(sdb_attribute_t),
260         sdb_attr_init,
261         sdb_attr_destroy,
262         sdb_attr_clone
263 };
265 /*
266  * private helper functions
267  */
269 static sdb_store_obj_t *
270 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
272         sdb_llist_iter_t *iter;
274         if (! l)
275                 return NULL;
277         iter = sdb_llist_get_iter(l);
278         if (! iter)
279                 return NULL;
281         while (sdb_llist_iter_has_next(iter)) {
282                 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
283                 assert(sobj);
285                 if ((sobj->type == type)
286                                 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
287                         sdb_llist_iter_destroy(iter);
288                         return sobj;
289                 }
291                 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
292                 if (sobj) {
293                         sdb_llist_iter_destroy(iter);
294                         return sobj;
295                 }
296         }
297         sdb_llist_iter_destroy(iter);
298         return NULL;
299 } /* sdb_store_lookup_in_list */
301 static sdb_store_obj_t *
302 sdb_store_lookup(int type, const char *name)
304         return sdb_store_lookup_in_list(obj_list, type, name);
305 } /* sdb_store_lookup */
307 static int
308 store_obj(int parent_type, const char *parent_name,
309                 int type, const char *name, sdb_time_t last_update)
311         sdb_llist_t *parent_list;
312         store_obj_t *old;
313         int status = 0;
315         if (! name)
316                 return -1;
318         if (last_update <= 0)
319                 last_update = sdb_gettime();
321         assert((parent_type == 0)
322                         || (parent_type = SDB_HOST)
323                         || (parent_type == SDB_SERVICE));
324         assert((type == 0) || (type = SDB_HOST) || (type == SDB_SERVICE));
326         pthread_rwlock_wrlock(&obj_lock);
328         if (! obj_list) {
329                 if (! (obj_list = sdb_llist_create())) {
330                         pthread_rwlock_unlock(&obj_lock);
331                         return -1;
332                 }
333         }
334         parent_list = obj_list;
336         if (parent_type && parent_name) {
337                 sdb_store_obj_t *parent;
339                 parent = sdb_store_lookup(parent_type, parent_name);
340                 if (! parent) {
341                         sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
342                                         "parent %s '%s' not found", TYPE_TO_NAME(type), name,
343                                         TYPE_TO_NAME(parent_type), parent_name);
344                         pthread_rwlock_unlock(&obj_lock);
345                         return -1;
346                 }
348                 parent_list = parent->children;
349         }
351         /* TODO: only look into direct children? */
352         old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
354         if (old) {
355                 if (old->last_update > last_update) {
356                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
357                                         "value too old (%"PRIscTIME" < %"PRIscTIME")",
358                                         TYPE_TO_NAME(type), name, last_update, old->last_update);
359                         /* don't report an error; the object may be updated by multiple
360                          * backends */
361                         status = 1;
362                 }
363                 else {
364                         old->last_update = last_update;
365                 }
366         }
367         else {
368                 store_obj_t *new = STORE_OBJ(sdb_object_create(name,
369                                         sdb_store_obj_type, last_update, type));
370                 if (! new) {
371                         char errbuf[1024];
372                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
373                                         TYPE_TO_NAME(type), name,
374                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
375                         pthread_rwlock_unlock(&obj_lock);
376                         return -1;
377                 }
379                 /* TODO: insert type-aware */
380                 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
381                                 sdb_object_cmp_by_name);
383                 /* pass control to the list or destroy in case of an error */
384                 sdb_object_deref(SDB_OBJ(new));
385         }
387         pthread_rwlock_unlock(&obj_lock);
388         return status;
389 } /* sdb_store_obj */
391 /*
392  * public API
393  */
395 int
396 sdb_store_host(const char *name, sdb_time_t last_update)
398         char *cname;
399         int status = 0;
401         if (! name)
402                 return -1;
404         cname = sdb_plugin_cname(strdup(name));
405         if (! cname) {
406                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
407                 return -1;
408         }
410         status = store_obj(/* parent = */ 0, NULL,
411                         /* stored object = */ SDB_HOST, cname, last_update);
412         free(cname);
413         return status;
414 } /* sdb_store_host */
416 _Bool
417 sdb_store_has_host(const char *name)
419         sdb_store_obj_t *host;
421         if (! name)
422                 return NULL;
424         host = sdb_store_lookup(SDB_HOST, name);
425         return host != NULL;
426 } /* sdb_store_has_host */
428 int
429 sdb_store_attribute(const char *hostname, const char *key, const char *value,
430                 sdb_time_t last_update)
432         sdb_store_obj_t *sobj;
433         sdb_attribute_t *old;
435         int status = 0;
437         if ((! hostname) || (! key))
438                 return -1;
440         if (last_update <= 0)
441                 last_update = sdb_gettime();
443         if (! obj_list)
444                 return -1;
446         pthread_rwlock_wrlock(&obj_lock);
448         sobj = sdb_store_lookup(SDB_HOST, hostname);
449         if (! sobj) {
450                 pthread_rwlock_unlock(&obj_lock);
451                 return -1;
452         }
454         old = SDB_ATTR(sdb_llist_search_by_name(sobj->attributes, key));
455         if (old) {
456                 if (old->_last_update > last_update) {
457                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
458                                         "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
459                                         hostname, key, last_update, old->_last_update);
460                         status = 1;
461                 }
462                 else {
463                         old->_last_update = last_update;
464                 }
465         }
466         else {
467                 sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key,
468                                         sdb_attribute_type, last_update, value));
469                 if (! new) {
470                         char errbuf[1024];
471                         sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
472                                         "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
473                         pthread_rwlock_unlock(&obj_lock);
474                         return -1;
475                 }
477                 status = sdb_llist_insert_sorted(sobj->attributes, SDB_OBJ(new),
478                                 sdb_object_cmp_by_name);
480                 /* pass control to the list or destroy in case of an error */
481                 sdb_object_deref(SDB_OBJ(new));
482         }
484         pthread_rwlock_unlock(&obj_lock);
485         return status;
486 } /* sdb_store_attribute */
488 int
489 sdb_store_service(const char *hostname, const char *name,
490                 sdb_time_t last_update)
492         char *cname;
493         int status = 0;
495         if ((! hostname) || (! name))
496                 return -1;
498         cname = sdb_plugin_cname(strdup(hostname));
499         if (! cname) {
500                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
501                 return -1;
502         }
504         status = store_obj(/* parent = */ SDB_HOST, cname,
505                         /* stored object = */ SDB_SERVICE, name, last_update);
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 : */