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->backends = NULL;
73 sobj->backends_num = 0;
74 sobj->parent = NULL;
75 return 0;
76 } /* store_obj_init */
78 static void
79 store_obj_destroy(sdb_object_t *obj)
80 {
81 sdb_store_obj_t *sobj = STORE_OBJ(obj);
82 size_t i;
84 for (i = 0; i < sobj->backends_num; ++i)
85 free(sobj->backends[i]);
86 free(sobj->backends);
87 sobj->backends = NULL;
88 sobj->backends_num = 0;
90 if (sobj->parent)
91 sdb_object_deref(SDB_OBJ(sobj->parent));
92 } /* store_obj_destroy */
94 static int
95 sdb_host_init(sdb_object_t *obj, va_list ap)
96 {
97 sdb_host_t *sobj = HOST(obj);
98 int ret;
100 /* this will consume the first argument (type) of ap */
101 ret = store_obj_init(obj, ap);
102 if (ret)
103 return ret;
105 sobj->services = sdb_llist_create();
106 if (! sobj->services)
107 return -1;
108 sobj->attributes = sdb_llist_create();
109 if (! sobj->attributes)
110 return -1;
111 return 0;
112 } /* sdb_host_init */
114 static void
115 sdb_host_destroy(sdb_object_t *obj)
116 {
117 sdb_host_t *sobj = HOST(obj);
118 assert(obj);
120 store_obj_destroy(obj);
122 if (sobj->services)
123 sdb_llist_destroy(sobj->services);
124 if (sobj->attributes)
125 sdb_llist_destroy(sobj->attributes);
126 } /* sdb_host_destroy */
128 static int
129 sdb_service_init(sdb_object_t *obj, va_list ap)
130 {
131 sdb_service_t *sobj = SVC(obj);
132 int ret;
134 /* this will consume the first argument (type) of ap */
135 ret = store_obj_init(obj, ap);
136 if (ret)
137 return ret;
139 sobj->attributes = sdb_llist_create();
140 if (! sobj->attributes)
141 return -1;
142 return 0;
143 } /* sdb_service_init */
145 static void
146 sdb_service_destroy(sdb_object_t *obj)
147 {
148 sdb_service_t *sobj = SVC(obj);
149 assert(obj);
151 store_obj_destroy(obj);
153 if (sobj->attributes)
154 sdb_llist_destroy(sobj->attributes);
155 } /* sdb_service_destroy */
157 static int
158 sdb_attr_init(sdb_object_t *obj, va_list ap)
159 {
160 const sdb_data_t *value;
161 int ret;
163 /* this will consume the first two arguments
164 * (type and last_update) of ap */
165 ret = store_obj_init(obj, ap);
166 if (ret)
167 return ret;
168 value = va_arg(ap, const sdb_data_t *);
170 if (value)
171 if (sdb_data_copy(&ATTR(obj)->value, value))
172 return -1;
173 return 0;
174 } /* sdb_attr_init */
176 static void
177 sdb_attr_destroy(sdb_object_t *obj)
178 {
179 assert(obj);
181 store_obj_destroy(obj);
182 sdb_data_free_datum(&ATTR(obj)->value);
183 } /* sdb_attr_destroy */
185 static sdb_type_t sdb_host_type = {
186 sizeof(sdb_host_t),
187 sdb_host_init,
188 sdb_host_destroy
189 };
191 static sdb_type_t sdb_service_type = {
192 sizeof(sdb_service_t),
193 sdb_service_init,
194 sdb_service_destroy
195 };
197 static sdb_type_t sdb_attribute_type = {
198 sizeof(sdb_attribute_t),
199 sdb_attr_init,
200 sdb_attr_destroy
201 };
203 /*
204 * private helper functions
205 */
207 static sdb_host_t *
208 lookup_host(const char *name)
209 {
210 return HOST(sdb_llist_search_by_name(host_list, name));
211 } /* lookup_host */
213 static int
214 record_backend(sdb_store_obj_t *obj)
215 {
216 const sdb_plugin_info_t *info;
217 char **tmp;
218 size_t i;
220 info = sdb_plugin_current();
221 if (! info)
222 return 0;
224 for (i = 0; i < obj->backends_num; ++i)
225 if (!strcasecmp(obj->backends[i], info->plugin_name))
226 return 0;
228 tmp = realloc(obj->backends,
229 (obj->backends_num + 1) * sizeof(*obj->backends));
230 if (! tmp)
231 return -1;
233 obj->backends = tmp;
234 obj->backends[obj->backends_num] = strdup(info->plugin_name);
235 if (! obj->backends[obj->backends_num])
236 return -1;
238 ++obj->backends_num;
239 return 0;
240 } /* record_backend */
242 static int
243 store_obj(sdb_llist_t *parent_list, int type, const char *name,
244 sdb_time_t last_update, sdb_store_obj_t **updated_obj)
245 {
246 sdb_store_obj_t *old, *new;
247 int status = 0;
249 assert(parent_list);
251 if (last_update <= 0)
252 last_update = sdb_gettime();
254 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
255 if (old) {
256 if (old->last_update >= last_update) {
257 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
258 "value too old (%"PRIscTIME" < %"PRIscTIME")",
259 SDB_STORE_TYPE_TO_NAME(type), name,
260 last_update, old->last_update);
261 /* don't report an error; the object may be updated by multiple
262 * backends */
263 status = 1;
264 }
265 else {
266 sdb_time_t interval = last_update - old->last_update;
267 old->last_update = last_update;
268 if (interval) {
269 if (old->interval)
270 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
271 + (0.1 * (double)interval));
272 else
273 old->interval = interval;
274 }
275 }
277 new = old;
278 }
279 else {
280 if (type == SDB_ATTRIBUTE) {
281 /* the value will be updated by the caller */
282 new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
283 type, last_update, NULL));
284 }
285 else {
286 sdb_type_t t;
287 t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
288 new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
289 }
291 if (new) {
292 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
293 sdb_object_cmp_by_name);
295 /* pass control to the list or destroy in case of an error */
296 sdb_object_deref(SDB_OBJ(new));
297 }
298 else {
299 char errbuf[1024];
300 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
301 SDB_STORE_TYPE_TO_NAME(type), name,
302 sdb_strerror(errno, errbuf, sizeof(errbuf)));
303 status = -1;
304 }
305 }
307 if (status < 0)
308 return status;
309 assert(new);
311 if (updated_obj)
312 *updated_obj = new;
314 if (record_backend(new))
315 return -1;
316 return status;
317 } /* store_obj */
319 static int
320 store_attr(sdb_llist_t *attributes, const char *key, const sdb_data_t *value,
321 sdb_time_t last_update)
322 {
323 sdb_store_obj_t *attr = NULL;
324 int status;
326 status = store_obj(attributes, SDB_ATTRIBUTE, key, last_update, &attr);
327 if (status)
328 return status;
330 /* don't update unchanged values */
331 if (! sdb_data_cmp(&ATTR(attr)->value, value))
332 return status;
334 assert(attr);
335 if (sdb_data_copy(&ATTR(attr)->value, value))
336 return -1;
337 return status;
338 } /* store_attr */
340 /* The host_lock has to be acquired before calling this function. */
341 static sdb_llist_t *
342 get_host_children(const char *hostname, int type)
343 {
344 char *cname = NULL;
345 sdb_host_t *host;
347 assert(hostname);
348 assert((type == SDB_SERVICE) || (type == SDB_ATTRIBUTE));
350 if (! host_list)
351 return NULL;
353 cname = sdb_plugin_cname(strdup(hostname));
354 if (! cname) {
355 sdb_log(SDB_LOG_ERR, "store: strdup failed");
356 return NULL;
357 }
359 host = lookup_host(cname);
360 free(cname);
361 if (! host)
362 return NULL;
364 if (type == SDB_ATTRIBUTE)
365 return host->attributes;
366 else
367 return host->services;
368 } /* get_host_children */
370 /*
371 * store_common_tojson serializes common object attributes to JSON.
372 *
373 * The function never returns an error. Rather, an error message will be part
374 * of the serialized data.
375 */
376 static void
377 store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
378 {
379 char time_str[64];
380 char interval_str[64];
381 size_t i;
383 if (! sdb_strftime(time_str, sizeof(time_str),
384 "%F %T %z", obj->last_update))
385 snprintf(time_str, sizeof(time_str), "<error>");
386 time_str[sizeof(time_str) - 1] = '\0';
388 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
389 obj->interval))
390 snprintf(interval_str, sizeof(interval_str), "<error>");
391 interval_str[sizeof(interval_str) - 1] = '\0';
393 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
394 "\"update_interval\": \"%s\", \"backends\": [",
395 time_str, interval_str);
397 for (i = 0; i < obj->backends_num; ++i) {
398 sdb_strbuf_append(buf, "\"%s\"", obj->backends[i]);
399 if (i < obj->backends_num - 1)
400 sdb_strbuf_append(buf, ",");
401 }
402 sdb_strbuf_append(buf, "]");
403 } /* store_common_tojson */
405 /*
406 * store_obj_tojson serializes attribute / service objects to JSON.
407 *
408 * The function never returns an error. Rather, an error message will be part
409 * of the serialized data.
410 */
411 static void
412 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf, int flags)
413 {
414 sdb_llist_iter_t *iter;
416 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
418 sdb_strbuf_append(buf, "[");
419 iter = sdb_llist_get_iter(list);
420 if (! iter) {
421 char errbuf[1024];
422 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
423 SDB_STORE_TYPE_TO_NAME(type),
424 sdb_strerror(errno, errbuf, sizeof(errbuf)));
425 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
426 SDB_STORE_TYPE_TO_NAME(type), errbuf);
427 }
429 /* has_next returns false if the iterator is NULL */
430 while (sdb_llist_iter_has_next(iter)) {
431 sdb_store_obj_t *sobj = STORE_OBJ(sdb_llist_iter_get_next(iter));
432 assert(sobj);
433 assert(sobj->type == type);
435 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
436 if (sobj->type == SDB_ATTRIBUTE) {
437 char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
438 sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
439 SDB_DOUBLE_QUOTED);
440 sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
441 }
442 store_common_tojson(sobj, buf);
444 if ((sobj->type == SDB_SERVICE)
445 && (! (flags & SDB_SKIP_ATTRIBUTES))) {
446 sdb_strbuf_append(buf, ", \"attributes\": ");
447 store_obj_tojson(SVC(sobj)->attributes, SDB_ATTRIBUTE,
448 buf, flags);
449 }
450 sdb_strbuf_append(buf, "}");
452 if (sdb_llist_iter_has_next(iter))
453 sdb_strbuf_append(buf, ",");
454 }
456 sdb_llist_iter_destroy(iter);
457 sdb_strbuf_append(buf, "]");
458 } /* store_obj_tojson */
460 /*
461 * public API
462 */
464 void
465 sdb_store_clear(void)
466 {
467 sdb_llist_destroy(host_list);
468 host_list = NULL;
469 } /* sdb_store_clear */
471 int
472 sdb_store_host(const char *name, sdb_time_t last_update)
473 {
474 char *cname = NULL;
475 int status = 0;
477 if (! name)
478 return -1;
480 cname = sdb_plugin_cname(strdup(name));
481 if (! cname) {
482 sdb_log(SDB_LOG_ERR, "store: strdup failed");
483 return -1;
484 }
486 pthread_rwlock_wrlock(&host_lock);
487 if (! host_list)
488 if (! (host_list = sdb_llist_create()))
489 status = -1;
491 if (! status)
492 status = store_obj(host_list, SDB_HOST, cname, last_update, NULL);
493 pthread_rwlock_unlock(&host_lock);
495 free(cname);
496 return status;
497 } /* sdb_store_host */
499 _Bool
500 sdb_store_has_host(const char *name)
501 {
502 sdb_host_t *host;
504 if (! name)
505 return NULL;
507 host = lookup_host(name);
508 return host != NULL;
509 } /* sdb_store_has_host */
511 sdb_store_obj_t *
512 sdb_store_get_host(const char *name)
513 {
514 sdb_host_t *host;
516 if (! name)
517 return NULL;
519 host = lookup_host(name);
520 if (! host)
521 return NULL;
523 sdb_object_ref(SDB_OBJ(host));
524 return STORE_OBJ(host);
525 } /* sdb_store_get_host */
527 int
528 sdb_store_attribute(const char *hostname,
529 const char *key, const sdb_data_t *value,
530 sdb_time_t last_update)
531 {
532 sdb_llist_t *attrs;
533 int status = 0;
535 if ((! hostname) || (! key))
536 return -1;
538 pthread_rwlock_wrlock(&host_lock);
539 attrs = get_host_children(hostname, SDB_ATTRIBUTE);
540 if (! attrs) {
541 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
542 "host '%s' not found", key, hostname);
543 status = -1;
544 }
546 if (! status)
547 status = store_attr(attrs, key, value, last_update);
549 pthread_rwlock_unlock(&host_lock);
550 return status;
551 } /* sdb_store_attribute */
553 int
554 sdb_store_service(const char *hostname, const char *name,
555 sdb_time_t last_update)
556 {
557 sdb_llist_t *services;
559 int status = 0;
561 if ((! hostname) || (! name))
562 return -1;
564 pthread_rwlock_wrlock(&host_lock);
565 services = get_host_children(hostname, SDB_SERVICE);
566 if (! services) {
567 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
568 "host '%s' not found", name, hostname);
569 status = -1;
570 }
572 if (! status)
573 status = store_obj(services, SDB_SERVICE, name, last_update, NULL);
574 pthread_rwlock_unlock(&host_lock);
575 return status;
576 } /* sdb_store_service */
578 int
579 sdb_store_service_attr(const char *hostname, const char *service,
580 const char *key, const sdb_data_t *value, sdb_time_t last_update)
581 {
582 sdb_llist_t *services;
583 sdb_service_t *svc;
584 int status = 0;
586 if ((! hostname) || (! service) || (! key))
587 return -1;
589 pthread_rwlock_wrlock(&host_lock);
590 services = get_host_children(hostname, SDB_SERVICE);
591 if (! services) {
592 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
593 "for service '%s' - host '%ss' not found",
594 key, service, hostname);
595 pthread_rwlock_unlock(&host_lock);
596 return -1;
597 }
599 svc = SVC(sdb_llist_search_by_name(services, service));
600 if (! svc) {
601 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
602 "service '%s/%s' not found", key, hostname, service);
603 status = -1;
604 }
606 if (! status)
607 status = store_attr(svc->attributes, key, value, last_update);
609 pthread_rwlock_unlock(&host_lock);
610 return status;
611 } /* sdb_store_service_attr */
613 int
614 sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, int flags)
615 {
616 sdb_host_t *host;
618 if ((! h) || (h->type != SDB_HOST) || (! buf))
619 return -1;
621 host = HOST(h);
623 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(host)->name);
624 store_common_tojson(h, buf);
626 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
627 sdb_strbuf_append(buf, ", \"attributes\": ");
628 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf, flags);
629 }
631 if (! (flags & SDB_SKIP_SERVICES)) {
632 sdb_strbuf_append(buf, ", \"services\": ");
633 store_obj_tojson(host->services, SDB_SERVICE, buf, flags);
634 }
636 sdb_strbuf_append(buf, "}");
637 return 0;
638 } /* sdb_store_host_tojson */
640 int
641 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
642 {
643 sdb_llist_iter_t *host_iter;
645 if (! buf)
646 return -1;
648 pthread_rwlock_rdlock(&host_lock);
650 host_iter = sdb_llist_get_iter(host_list);
651 if (! host_iter) {
652 pthread_rwlock_unlock(&host_lock);
653 return -1;
654 }
656 sdb_strbuf_append(buf, "{\"hosts\":[");
658 while (sdb_llist_iter_has_next(host_iter)) {
659 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
660 assert(host);
662 if (sdb_store_host_tojson(host, buf, flags))
663 return -1;
665 if (sdb_llist_iter_has_next(host_iter))
666 sdb_strbuf_append(buf, ",");
667 }
669 sdb_strbuf_append(buf, "]}");
671 sdb_llist_iter_destroy(host_iter);
672 pthread_rwlock_unlock(&host_lock);
673 return 0;
674 } /* sdb_store_tojson */
676 /* TODO: actually support hierarchical data */
677 int
678 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
679 {
680 sdb_llist_iter_t *host_iter;
681 int status = 0;
683 pthread_rwlock_rdlock(&host_lock);
685 host_iter = sdb_llist_get_iter(host_list);
686 if (! host_iter)
687 status = -1;
689 /* has_next returns false if the iterator is NULL */
690 while (sdb_llist_iter_has_next(host_iter)) {
691 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
692 assert(host);
694 if (cb(host, user_data)) {
695 status = -1;
696 break;
697 }
698 }
700 sdb_llist_iter_destroy(host_iter);
701 pthread_rwlock_unlock(&host_lock);
702 return status;
703 } /* sdb_store_iterate */
705 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */