Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[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 *host_list = NULL;
53 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
55 /*
56  * private types
57  */
59 static sdb_type_t sdb_host_type;
60 static sdb_type_t sdb_service_type;
61 static sdb_type_t sdb_attribute_type;
63 static int
64 store_obj_init(sdb_object_t *obj, va_list ap)
65 {
66         sdb_store_obj_t *sobj = STORE_OBJ(obj);
68         sobj->type = va_arg(ap, int);
70         sobj->last_update = va_arg(ap, sdb_time_t);
71         sobj->interval = 0;
72         sobj->parent = NULL;
73         return 0;
74 } /* store_obj_init */
76 static void
77 store_obj_destroy(sdb_object_t *obj)
78 {
79         const sdb_store_obj_t *sobj = STORE_CONST_OBJ(obj);
81         if (sobj->parent)
82                 sdb_object_deref(SDB_OBJ(sobj->parent));
83 } /* store_obj_destroy */
85 static int
86 sdb_host_init(sdb_object_t *obj, va_list ap)
87 {
88         sdb_host_t *sobj = HOST(obj);
89         int ret;
91         /* this will consume the first argument (type) of ap */
92         ret = store_obj_init(obj, ap);
93         if (ret)
94                 return ret;
96         sobj->services = sdb_llist_create();
97         if (! sobj->services)
98                 return -1;
99         sobj->attributes = sdb_llist_create();
100         if (! sobj->attributes)
101                 return -1;
102         return 0;
103 } /* sdb_host_init */
105 static void
106 sdb_host_destroy(sdb_object_t *obj)
108         sdb_host_t *sobj = HOST(obj);
109         assert(obj);
111         store_obj_destroy(obj);
113         if (sobj->services)
114                 sdb_llist_destroy(sobj->services);
115         if (sobj->attributes)
116                 sdb_llist_destroy(sobj->attributes);
117 } /* sdb_host_destroy */
119 static int
120 sdb_service_init(sdb_object_t *obj, va_list ap)
122         sdb_service_t *sobj = SVC(obj);
123         int ret;
125         /* this will consume the first argument (type) of ap */
126         ret = store_obj_init(obj, ap);
127         if (ret)
128                 return ret;
130         sobj->attributes = sdb_llist_create();
131         if (! sobj->attributes)
132                 return -1;
133         return 0;
134 } /* sdb_service_init */
136 static void
137 sdb_service_destroy(sdb_object_t *obj)
139         sdb_service_t *sobj = SVC(obj);
140         assert(obj);
142         store_obj_destroy(obj);
144         if (sobj->attributes)
145                 sdb_llist_destroy(sobj->attributes);
146 } /* sdb_service_destroy */
148 static int
149 sdb_attr_init(sdb_object_t *obj, va_list ap)
151         const sdb_data_t *value;
152         int ret;
154         /* this will consume the first two arguments
155          * (type and last_update) of ap */
156         ret = store_obj_init(obj, ap);
157         if (ret)
158                 return ret;
159         value = va_arg(ap, const sdb_data_t *);
161         if (value)
162                 if (sdb_data_copy(&ATTR(obj)->value, value))
163                         return -1;
164         return 0;
165 } /* sdb_attr_init */
167 static void
168 sdb_attr_destroy(sdb_object_t *obj)
170         assert(obj);
172         store_obj_destroy(obj);
173         sdb_data_free_datum(&ATTR(obj)->value);
174 } /* sdb_attr_destroy */
176 static sdb_type_t sdb_host_type = {
177         sizeof(sdb_host_t),
178         sdb_host_init,
179         sdb_host_destroy
180 };
182 static sdb_type_t sdb_service_type = {
183         sizeof(sdb_service_t),
184         sdb_service_init,
185         sdb_service_destroy
186 };
188 static sdb_type_t sdb_attribute_type = {
189         sizeof(sdb_attribute_t),
190         sdb_attr_init,
191         sdb_attr_destroy
192 };
194 /*
195  * private helper functions
196  */
198 static sdb_host_t *
199 lookup_host(const char *name)
201         return HOST(sdb_llist_search_by_name(host_list, name));
202 } /* lookup_host */
204 /* The host_lock has to be acquired before calling this function. */
205 static int
206 store_obj(const char *hostname, int type, const char *name,
207                 sdb_time_t last_update, sdb_store_obj_t **updated_obj)
209         char *host_cname = NULL, *cname = NULL;
211         sdb_llist_t *parent_list;
212         sdb_store_obj_t *old;
213         int status = 0;
215         if (last_update <= 0)
216                 last_update = sdb_gettime();
218         assert((type == 0)
219                         || (type == SDB_HOST)
220                         || (type == SDB_SERVICE)
221                         || (type == SDB_ATTRIBUTE));
223         assert(hostname || (type == SDB_HOST));
224         assert((! hostname)
225                         || (type == SDB_SERVICE)
226                         || (type == SDB_ATTRIBUTE));
228         if (! host_list)
229                 if (! (host_list = sdb_llist_create()))
230                         return -1;
231         parent_list = host_list;
233         if (type == SDB_HOST) {
234                 cname = sdb_plugin_cname(strdup(name));
235                 if (! cname) {
236                         sdb_log(SDB_LOG_ERR, "store: strdup failed");
237                         return -1;
238                 }
239                 name = cname;
240         }
242         if (hostname) {
243                 sdb_host_t *host;
245                 host_cname = sdb_plugin_cname(strdup(hostname));
246                 if (! host_cname) {
247                         sdb_log(SDB_LOG_ERR, "store: strdup failed");
248                         free(cname);
249                         return -1;
250                 }
251                 hostname = host_cname;
253                 host = lookup_host(hostname);
254                 if (! host) {
255                         sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
256                                         "host '%s' not found", SDB_STORE_TYPE_TO_NAME(type),
257                                         name, hostname);
258                         free(host_cname);
259                         free(cname);
260                         return -1;
261                 }
263                 if (type == SDB_ATTRIBUTE)
264                         parent_list = host->attributes;
265                 else
266                         parent_list = host->services;
267         }
269         if (type == SDB_HOST)
270                 old = STORE_OBJ(sdb_llist_search_by_name(host_list, name));
271         else
272                 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
274         if (old) {
275                 if (old->last_update > last_update) {
276                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
277                                         "value too old (%"PRIscTIME" < %"PRIscTIME")",
278                                         SDB_STORE_TYPE_TO_NAME(type), name,
279                                         last_update, old->last_update);
280                         /* don't report an error; the object may be updated by multiple
281                          * backends */
282                         status = 1;
283                 }
284                 else {
285                         sdb_time_t interval = last_update - old->last_update;
286                         old->last_update = last_update;
287                         if (interval) {
288                                 if (old->interval)
289                                         old->interval = (sdb_time_t)((0.9 * (double)old->interval)
290                                                         + (0.1 * (double)interval));
291                                 else
292                                         old->interval = interval;
293                         }
294                 }
296                 if (updated_obj)
297                         *updated_obj = old;
298         }
299         else {
300                 sdb_store_obj_t *new;
302                 if (type == SDB_ATTRIBUTE) {
303                         /* the value will be updated by the caller */
304                         new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
305                                                 type, last_update, NULL));
306                 }
307                 else {
308                         sdb_type_t t;
309                         t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
310                         new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
311                 }
313                 if (! new) {
314                         char errbuf[1024];
315                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
316                                         SDB_STORE_TYPE_TO_NAME(type), name,
317                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
318                         free(host_cname);
319                         free(cname);
320                         return -1;
321                 }
323                 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
324                                 sdb_object_cmp_by_name);
326                 /* pass control to the list or destroy in case of an error */
327                 sdb_object_deref(SDB_OBJ(new));
329                 if (updated_obj)
330                         *updated_obj = new;
331         }
332         free(host_cname);
333         free(cname);
334         return status;
335 } /* store_obj */
337 /*
338  * store_obj_tojson serializes attribute / service objects to JSON.
339  *
340  * The function never returns an error. Rather, an error message will be part
341  * of the serialized data.
342  */
343 static void
344 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
346         sdb_llist_iter_t *iter;
347         char time_str[64];
348         char interval_str[64];
350         assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
352         sdb_strbuf_append(buf, "[");
354         iter = sdb_llist_get_iter(list);
355         if (! iter) {
356                 char errbuf[1024];
357                 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
358                                 SDB_STORE_TYPE_TO_NAME(type),
359                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
360                 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
361                                 SDB_STORE_TYPE_TO_NAME(type), errbuf);
362         }
364         /* has_next returns false if the iterator is NULL */
365         while (sdb_llist_iter_has_next(iter)) {
366                 sdb_store_obj_t *sobj = STORE_OBJ(sdb_llist_iter_get_next(iter));
367                 assert(sobj);
368                 assert(sobj->type == type);
370                 if (! sdb_strftime(time_str, sizeof(time_str),
371                                         "%F %T %z", sobj->last_update))
372                         snprintf(time_str, sizeof(time_str), "<error>");
373                 time_str[sizeof(time_str) - 1] = '\0';
375                 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
376                                         sobj->interval))
377                         snprintf(interval_str, sizeof(interval_str), "<error>");
378                 interval_str[sizeof(interval_str) - 1] = '\0';
380                 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
381                 if (sobj->type == SDB_ATTRIBUTE) {
382                         char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
383                         sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
384                                         SDB_DOUBLE_QUOTED);
385                         sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
386                                         "\"update_interval\": \"%s\"}", tmp, time_str,
387                                         interval_str);
388                 }
389                 else
390                         sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
391                                         "\"update_interval\": \"%s\"}", time_str, interval_str);
393                 if (sdb_llist_iter_has_next(iter))
394                         sdb_strbuf_append(buf, ",");
395         }
397         sdb_llist_iter_destroy(iter);
398         sdb_strbuf_append(buf, "]");
399 } /* store_obj_tojson */
401 /*
402  * public API
403  */
405 void
406 sdb_store_clear(void)
408         sdb_llist_destroy(host_list);
409         host_list = NULL;
410 } /* sdb_store_clear */
412 int
413 sdb_store_host(const char *name, sdb_time_t last_update)
415         int status;
417         if (! name)
418                 return -1;
420         pthread_rwlock_wrlock(&host_lock);
421         status = store_obj(/* hostname = */ NULL,
422                         /* stored object = */ SDB_HOST, name, last_update,
423                         /* updated_obj = */ NULL);
424         pthread_rwlock_unlock(&host_lock);
425         return status;
426 } /* sdb_store_host */
428 _Bool
429 sdb_store_has_host(const char *name)
431         sdb_host_t *host;
433         if (! name)
434                 return NULL;
436         host = lookup_host(name);
437         return host != NULL;
438 } /* sdb_store_has_host */
440 sdb_store_obj_t *
441 sdb_store_get_host(const char *name)
443         sdb_host_t *host;
445         if (! name)
446                 return NULL;
448         host = lookup_host(name);
449         if (! host)
450                 return NULL;
452         sdb_object_ref(SDB_OBJ(host));
453         return STORE_OBJ(host);
454 } /* sdb_store_get_host */
456 int
457 sdb_store_attribute(const char *hostname,
458                 const char *key, const sdb_data_t *value,
459                 sdb_time_t last_update)
461         int status;
463         sdb_store_obj_t *updated_attr = NULL;
465         if ((! hostname) || (! key))
466                 return -1;
468         pthread_rwlock_wrlock(&host_lock);
469         status = store_obj(hostname,
470                         /* stored object = */ SDB_ATTRIBUTE, key, last_update,
471                         &updated_attr);
473         if (status >= 0) {
474                 assert(updated_attr);
475                 sdb_data_free_datum(&ATTR(updated_attr)->value);
476                 if (sdb_data_copy(&ATTR(updated_attr)->value, value)) {
477                         sdb_object_deref(SDB_OBJ(updated_attr));
478                         status = -1;
479                 }
480         }
482         pthread_rwlock_unlock(&host_lock);
483         return status;
484 } /* sdb_store_attribute */
486 int
487 sdb_store_service(const char *hostname, const char *name,
488                 sdb_time_t last_update)
490         int status;
492         if ((! hostname) || (! name))
493                 return -1;
495         pthread_rwlock_wrlock(&host_lock);
496         status = store_obj(hostname,
497                         /* stored object = */ SDB_SERVICE, name, last_update,
498                         /* updated obj = */ NULL);
499         pthread_rwlock_unlock(&host_lock);
500         return status;
501 } /* sdb_store_service */
503 int
504 sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, int flags)
506         sdb_host_t *host;
507         char time_str[64];
508         char interval_str[64];
510         if ((! h) || (h->type != SDB_HOST) || (! buf))
511                 return -1;
513         host = HOST(h);
515         if (! sdb_strftime(time_str, sizeof(time_str),
516                                 "%F %T %z", host->_last_update))
517                 snprintf(time_str, sizeof(time_str), "<error>");
518         time_str[sizeof(time_str) - 1] = '\0';
520         if (! sdb_strfinterval(interval_str, sizeof(interval_str),
521                                 host->_interval))
522                 snprintf(interval_str, sizeof(interval_str), "<error>");
523         interval_str[sizeof(interval_str) - 1] = '\0';
525         sdb_strbuf_append(buf, "{\"name\": \"%s\", "
526                         "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
527                         SDB_OBJ(host)->name, time_str, interval_str);
529         if (! (flags & SDB_SKIP_ATTRIBUTES)) {
530                 sdb_strbuf_append(buf, ", \"attributes\": ");
531                 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
532         }
534         if (! (flags & SDB_SKIP_SERVICES)) {
535                 sdb_strbuf_append(buf, ", \"services\": ");
536                 store_obj_tojson(host->services, SDB_SERVICE, buf);
537         }
539         sdb_strbuf_append(buf, "}");
540         return 0;
541 } /* sdb_store_host_tojson */
543 int
544 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
546         sdb_llist_iter_t *host_iter;
548         if (! buf)
549                 return -1;
551         pthread_rwlock_rdlock(&host_lock);
553         host_iter = sdb_llist_get_iter(host_list);
554         if (! host_iter) {
555                 pthread_rwlock_unlock(&host_lock);
556                 return -1;
557         }
559         sdb_strbuf_append(buf, "{\"hosts\":[");
561         while (sdb_llist_iter_has_next(host_iter)) {
562                 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
563                 assert(host);
565                 if (sdb_store_host_tojson(host, buf, flags))
566                         return -1;
568                 if (sdb_llist_iter_has_next(host_iter))
569                         sdb_strbuf_append(buf, ",");
570         }
572         sdb_strbuf_append(buf, "]}");
574         sdb_llist_iter_destroy(host_iter);
575         pthread_rwlock_unlock(&host_lock);
576         return 0;
577 } /* sdb_store_tojson */
579 /* TODO: actually support hierarchical data */
580 int
581 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
583         sdb_llist_iter_t *host_iter;
584         int status = 0;
586         pthread_rwlock_rdlock(&host_lock);
588         host_iter = sdb_llist_get_iter(host_list);
589         if (! host_iter)
590                 status = -1;
592         /* has_next returns false if the iterator is NULL */
593         while (sdb_llist_iter_has_next(host_iter)) {
594                 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
595                 assert(host);
597                 if (cb(host, user_data)) {
598                         status = -1;
599                         break;
600                 }
601         }
603         sdb_llist_iter_destroy(host_iter);
604         pthread_rwlock_unlock(&host_lock);
605         return status;
606 } /* sdb_store_iterate */
608 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */