Code

6420190a319b532ed0db1b2248e4aafd6ee77784
[sysdb.git] / src / core / store.c
1 /*
2  * SysDB - src/core/store.c
3  * Copyright (C) 2012-2013 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 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/store-private.h"
34 #include "core/plugin.h"
35 #include "utils/error.h"
36 #include "utils/llist.h"
38 #include <assert.h>
40 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #include <pthread.h>
48 /*
49  * private variables
50  */
52 static sdb_llist_t *obj_list = NULL;
53 static pthread_rwlock_t obj_lock = PTHREAD_RWLOCK_INITIALIZER;
55 /*
56  * private types
57  */
59 static sdb_type_t sdb_store_obj_type;
60 static sdb_type_t sdb_attribute_type;
62 static int
63 store_base_init(sdb_object_t *obj, va_list ap)
64 {
65         sdb_store_base_t *sobj = STORE_BASE(obj);
67         sobj->type = va_arg(ap, int);
69         sobj->last_update = va_arg(ap, sdb_time_t);
70         sobj->interval = 0;
71         sobj->parent = NULL;
72         return 0;
73 } /* store_base_init */
75 static void
76 store_base_destroy(sdb_object_t *obj)
77 {
78         const sdb_store_base_t *sobj = STORE_CONST_BASE(obj);
80         if (sobj->parent)
81                 sdb_object_deref(SDB_OBJ(sobj->parent));
82 } /* store_base_destroy */
84 static int
85 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
86 {
87         sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
88         int ret;
90         /* this will consume the first argument (type) of ap */
91         ret = store_base_init(obj, ap);
92         if (ret)
93                 return ret;
95         sobj->children = sdb_llist_create();
96         if (! sobj->children)
97                 return -1;
98         sobj->attributes = sdb_llist_create();
99         if (! sobj->attributes)
100                 return -1;
101         return 0;
102 } /* sdb_store_obj_init */
104 static void
105 sdb_store_obj_destroy(sdb_object_t *obj)
107         sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
109         assert(obj);
111         store_base_destroy(obj);
113         if (sobj->children)
114                 sdb_llist_destroy(sobj->children);
115         if (sobj->attributes)
116                 sdb_llist_destroy(sobj->attributes);
117 } /* sdb_store_obj_destroy */
119 static int
120 sdb_attr_init(sdb_object_t *obj, va_list ap)
122         const sdb_data_t *value;
123         int ret;
125         /* this will consume the first two arguments
126          * (type and last_update) of ap */
127         ret = store_base_init(obj, ap);
128         if (ret)
129                 return ret;
130         value = va_arg(ap, const sdb_data_t *);
132         if (value)
133                 if (sdb_data_copy(&SDB_ATTR(obj)->value, value))
134                         return -1;
135         return 0;
136 } /* sdb_attr_init */
138 static void
139 sdb_attr_destroy(sdb_object_t *obj)
141         assert(obj);
143         store_base_destroy(obj);
144         sdb_data_free_datum(&SDB_ATTR(obj)->value);
145 } /* sdb_attr_destroy */
147 static sdb_type_t sdb_store_obj_type = {
148         sizeof(sdb_store_obj_t),
150         sdb_store_obj_init,
151         sdb_store_obj_destroy
152 };
154 static sdb_type_t sdb_attribute_type = {
155         sizeof(sdb_attribute_t),
157         sdb_attr_init,
158         sdb_attr_destroy
159 };
161 /*
162  * private helper functions
163  */
165 static sdb_store_obj_t *
166 store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
168         sdb_llist_iter_t *iter;
170         if (! l)
171                 return NULL;
173         iter = sdb_llist_get_iter(l);
174         if (! iter)
175                 return NULL;
177         while (sdb_llist_iter_has_next(iter)) {
178                 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
179                 assert(sobj);
181                 if ((STORE_BASE(sobj)->type == type)
182                                 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
183                         sdb_llist_iter_destroy(iter);
184                         return sobj;
185                 }
187                 /* don't lookups non-host types from hierarchical hosts */
188                 if ((type != SDB_HOST) && (STORE_BASE(sobj)->type == SDB_HOST))
189                         continue;
191                 sobj = store_lookup_in_list(sobj->children, type, name);
192                 if (sobj) {
193                         sdb_llist_iter_destroy(iter);
194                         return sobj;
195                 }
196         }
197         sdb_llist_iter_destroy(iter);
198         return NULL;
199 } /* store_lookup_in_list */
201 static sdb_store_obj_t *
202 store_lookup(int type, const char *name)
204         return store_lookup_in_list(obj_list, type, name);
205 } /* store_lookup */
207 /* The obj_lock has to be acquired before calling this function. */
208 static int
209 store_obj(int parent_type, const char *parent_name,
210                 int type, const char *name, sdb_time_t last_update,
211                 sdb_store_base_t **updated_obj)
213         char *parent_cname = NULL, *cname = NULL;
215         sdb_llist_t *parent_list;
216         sdb_store_base_t *old;
217         int status = 0;
219         if (last_update <= 0)
220                 last_update = sdb_gettime();
222         assert((parent_type == 0)
223                         || (parent_type == SDB_HOST)
224                         || (parent_type == SDB_SERVICE));
225         assert((type == 0)
226                         || (type == SDB_HOST)
227                         || (type == SDB_SERVICE)
228                         || (type == SDB_ATTRIBUTE));
230         if (parent_type == SDB_HOST) {
231                 parent_cname = sdb_plugin_cname(strdup(parent_name));
232                 if (! parent_cname) {
233                         sdb_log(SDB_LOG_ERR, "store: strdup failed");
234                         return -1;
235                 }
236                 parent_name = parent_cname;
237         }
238         if (type == SDB_HOST) {
239                 cname = sdb_plugin_cname(strdup(name));
240                 if (! cname) {
241                         sdb_log(SDB_LOG_ERR, "store: strdup failed");
242                         return -1;
243                 }
244                 name = cname;
245         }
247         if (! obj_list) {
248                 if (! (obj_list = sdb_llist_create())) {
249                         free(parent_cname);
250                         free(cname);
251                         return -1;
252                 }
253         }
254         parent_list = obj_list;
256         if (parent_type && parent_name) {
257                 sdb_store_obj_t *parent;
259                 parent = store_lookup(parent_type, parent_name);
260                 if (! parent) {
261                         sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
262                                         "parent %s '%s' not found", TYPE_TO_NAME(type), name,
263                                         TYPE_TO_NAME(parent_type), parent_name);
264                         free(parent_cname);
265                         free(cname);
266                         return -1;
267                 }
269                 if (type == SDB_ATTRIBUTE)
270                         parent_list = parent->attributes;
271                 else
272                         parent_list = parent->children;
273         }
275         if (type == SDB_HOST)
276                 /* make sure that each host is unique */
277                 old = STORE_BASE(store_lookup_in_list(obj_list, type, name));
278         else if (type == SDB_ATTRIBUTE)
279                 /* look into attributes of this host */
280                 old = STORE_BASE(sdb_llist_search_by_name(parent_list, name));
281         else
282                 /* look into services assigned to this host (store_lookup_in_list
283                  * does not look up services from hierarchical hosts) */
284                 old = STORE_BASE(store_lookup_in_list(parent_list, type, name));
286         if (old) {
287                 if (old->last_update > last_update) {
288                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
289                                         "value too old (%"PRIscTIME" < %"PRIscTIME")",
290                                         TYPE_TO_NAME(type), name, last_update, old->last_update);
291                         /* don't report an error; the object may be updated by multiple
292                          * backends */
293                         status = 1;
294                 }
295                 else {
296                         sdb_time_t interval = last_update - old->last_update;
297                         old->last_update = last_update;
298                         if (old->interval)
299                                 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
300                                                 + (0.1 * interval));
301                         else
302                                 old->interval = interval;
303                 }
305                 if (updated_obj)
306                         *updated_obj = old;
307         }
308         else {
309                 sdb_store_base_t *new;
311                 if (type == SDB_ATTRIBUTE)
312                         /* the value will be updated by the caller */
313                         new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
314                                                 type, last_update, NULL));
315                 else
316                         new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
317                                                 type, last_update));
319                 if (! new) {
320                         char errbuf[1024];
321                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
322                                         TYPE_TO_NAME(type), name,
323                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
324                         free(parent_cname);
325                         free(cname);
326                         return -1;
327                 }
329                 /* TODO: insert type-aware; the current version works as long as we
330                  * don't support to store hierarchical data */
331                 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
332                                 sdb_object_cmp_by_name);
334                 /* pass control to the list or destroy in case of an error */
335                 sdb_object_deref(SDB_OBJ(new));
337                 if (updated_obj)
338                         *updated_obj = new;
339         }
340         free(parent_cname);
341         free(cname);
342         return status;
343 } /* store_obj */
345 /*
346  * store_obj_tojson serializes attribute / service objects to JSON.
347  *
348  * The function never returns an error. Rather, an error message will be part
349  * of the serialized data.
350  */
351 static void
352 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
354         sdb_llist_iter_t *iter;
355         char time_str[64];
357         assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
359         sdb_strbuf_append(buf, "[");
361         iter = sdb_llist_get_iter(list);
362         if (! iter) {
363                 char errbuf[1024];
364                 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
365                                 TYPE_TO_NAME(type),
366                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
367                 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
368                                 TYPE_TO_NAME(type), errbuf);
369         }
371         /* has_next returns false if the iterator is NULL */
372         while (sdb_llist_iter_has_next(iter)) {
373                 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
374                 assert(sobj);
376                 if (! sdb_strftime(time_str, sizeof(time_str),
377                                         "%F %T %z", sobj->last_update))
378                         snprintf(time_str, sizeof(time_str), "<error>");
379                 time_str[sizeof(time_str) - 1] = '\0';
381                 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
382                 if (type == SDB_ATTRIBUTE) {
383                         char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
384                         sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
385                                         SDB_DOUBLE_QUOTED);
386                         sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\"}",
387                                         tmp, time_str);
388                 }
389                 else
390                         sdb_strbuf_append(buf, "\"last_update\": \"%s\"}", time_str);
392                 if (sdb_llist_iter_has_next(iter))
393                         sdb_strbuf_append(buf, ",");
394         }
396         sdb_llist_iter_destroy(iter);
397         sdb_strbuf_append(buf, "]");
398 } /* store_obj_tojson */
400 /*
401  * public API
402  */
404 void
405 sdb_store_clear(void)
407         sdb_llist_destroy(obj_list);
408         obj_list = NULL;
409 } /* sdb_store_clear */
411 int
412 sdb_store_host(const char *name, sdb_time_t last_update)
414         int status;
416         if (! name)
417                 return -1;
419         pthread_rwlock_wrlock(&obj_lock);
420         status = store_obj(/* parent = */ 0, NULL,
421                         /* stored object = */ SDB_HOST, name, last_update,
422                         /* updated_obj = */ NULL);
423         pthread_rwlock_unlock(&obj_lock);
424         return status;
425 } /* sdb_store_host */
427 _Bool
428 sdb_store_has_host(const char *name)
430         sdb_store_obj_t *host;
432         if (! name)
433                 return NULL;
435         host = store_lookup(SDB_HOST, name);
436         return host != NULL;
437 } /* sdb_store_has_host */
439 sdb_store_base_t *
440 sdb_store_get_host(const char *name)
442         sdb_store_obj_t *host;
444         if (! name)
445                 return NULL;
447         host = store_lookup(SDB_HOST, name);
448         if (! host)
449                 return NULL;
451         sdb_object_ref(SDB_OBJ(host));
452         return STORE_BASE(host);
453 } /* sdb_store_get_host */
455 int
456 sdb_store_attribute(const char *hostname,
457                 const char *key, const sdb_data_t *value,
458                 sdb_time_t last_update)
460         int status;
462         sdb_store_base_t *updated_attr = NULL;
464         if ((! hostname) || (! key))
465                 return -1;
467         pthread_rwlock_wrlock(&obj_lock);
468         status = store_obj(/* parent = */ SDB_HOST, hostname,
469                         /* stored object = */ SDB_ATTRIBUTE, key, last_update,
470                         &updated_attr);
472         if (status >= 0) {
473                 assert(updated_attr);
474                 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
475                         sdb_object_deref(SDB_OBJ(updated_attr));
476                         status = -1;
477                 }
478         }
480         pthread_rwlock_unlock(&obj_lock);
481         return status;
482 } /* sdb_store_attribute */
484 int
485 sdb_store_service(const char *hostname, const char *name,
486                 sdb_time_t last_update)
488         int status;
490         if ((! hostname) || (! name))
491                 return -1;
493         pthread_rwlock_wrlock(&obj_lock);
494         status = store_obj(/* parent = */ SDB_HOST, hostname,
495                         /* stored object = */ SDB_SERVICE, name, last_update,
496                         /* updated obj = */ NULL);
497         pthread_rwlock_unlock(&obj_lock);
498         return status;
499 } /* sdb_store_service */
501 int
502 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
504         sdb_store_obj_t *host;
505         char time_str[64];
507         if ((! h) || (h->type != SDB_HOST) || (! buf))
508                 return -1;
510         host = SDB_STORE_OBJ(h);
512         if (! sdb_strftime(time_str, sizeof(time_str),
513                                 "%F %T %z", host->_last_update))
514                 snprintf(time_str, sizeof(time_str), "<error>");
515         time_str[sizeof(time_str) - 1] = '\0';
517         sdb_strbuf_append(buf, "{\"name\": \"%s\", "
518                         "\"last_update\": \"%s\"",
519                         SDB_OBJ(host)->name, time_str);
521         if (! (flags & SDB_SKIP_ATTRIBUTES)) {
522                 sdb_strbuf_append(buf, ", \"attributes\": ");
523                 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
524         }
526         if (! (flags & SDB_SKIP_SERVICES)) {
527                 sdb_strbuf_append(buf, ", \"services\": ");
528                 store_obj_tojson(host->children, SDB_SERVICE, buf);
529         }
531         sdb_strbuf_append(buf, "}");
532         return 0;
533 } /* sdb_store_host_tojson */
535 int
536 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
538         sdb_llist_iter_t *host_iter;
540         if (! buf)
541                 return -1;
543         pthread_rwlock_rdlock(&obj_lock);
545         host_iter = sdb_llist_get_iter(obj_list);
546         if (! host_iter) {
547                 pthread_rwlock_unlock(&obj_lock);
548                 return -1;
549         }
551         sdb_strbuf_append(buf, "{\"hosts\":[");
553         while (sdb_llist_iter_has_next(host_iter)) {
554                 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
555                 assert(host);
557                 if (sdb_store_host_tojson(host, buf, flags))
558                         return -1;
560                 if (sdb_llist_iter_has_next(host_iter))
561                         sdb_strbuf_append(buf, ",");
562         }
564         sdb_strbuf_append(buf, "]}");
566         sdb_llist_iter_destroy(host_iter);
567         pthread_rwlock_unlock(&obj_lock);
568         return 0;
569 } /* sdb_store_tojson */
571 /* TODO: actually support hierarchical data */
572 int
573 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
575         sdb_llist_iter_t *host_iter;
576         int status = 0;
578         pthread_rwlock_rdlock(&obj_lock);
580         host_iter = sdb_llist_get_iter(obj_list);
581         if (! host_iter)
582                 status = -1;
584         /* has_next returns false if the iterator is NULL */
585         while (sdb_llist_iter_has_next(host_iter)) {
586                 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
587                 assert(host);
589                 if (cb(host, user_data)) {
590                         status = -1;
591                         break;
592                 }
593         }
595         sdb_llist_iter_destroy(host_iter);
596         pthread_rwlock_unlock(&obj_lock);
597         return status;
598 } /* sdb_store_iterate */
600 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */