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)
106 {
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)
121 {
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)
140 {
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)
167 {
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)
203 {
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)
212 {
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 (interval) {
299 if (old->interval)
300 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
301 + (0.1 * (double)interval));
302 else
303 old->interval = interval;
304 }
305 }
307 if (updated_obj)
308 *updated_obj = old;
309 }
310 else {
311 sdb_store_base_t *new;
313 if (type == SDB_ATTRIBUTE)
314 /* the value will be updated by the caller */
315 new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
316 type, last_update, NULL));
317 else
318 new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
319 type, last_update));
321 if (! new) {
322 char errbuf[1024];
323 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
324 TYPE_TO_NAME(type), name,
325 sdb_strerror(errno, errbuf, sizeof(errbuf)));
326 free(parent_cname);
327 free(cname);
328 return -1;
329 }
331 /* TODO: insert type-aware; the current version works as long as we
332 * don't support to store hierarchical data */
333 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
334 sdb_object_cmp_by_name);
336 /* pass control to the list or destroy in case of an error */
337 sdb_object_deref(SDB_OBJ(new));
339 if (updated_obj)
340 *updated_obj = new;
341 }
342 free(parent_cname);
343 free(cname);
344 return status;
345 } /* store_obj */
347 /*
348 * store_obj_tojson serializes attribute / service objects to JSON.
349 *
350 * The function never returns an error. Rather, an error message will be part
351 * of the serialized data.
352 */
353 static void
354 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
355 {
356 sdb_llist_iter_t *iter;
357 char time_str[64];
358 char interval_str[64];
360 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
362 sdb_strbuf_append(buf, "[");
364 iter = sdb_llist_get_iter(list);
365 if (! iter) {
366 char errbuf[1024];
367 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
368 TYPE_TO_NAME(type),
369 sdb_strerror(errno, errbuf, sizeof(errbuf)));
370 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
371 TYPE_TO_NAME(type), errbuf);
372 }
374 /* has_next returns false if the iterator is NULL */
375 while (sdb_llist_iter_has_next(iter)) {
376 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
377 assert(sobj);
379 if (! sdb_strftime(time_str, sizeof(time_str),
380 "%F %T %z", sobj->last_update))
381 snprintf(time_str, sizeof(time_str), "<error>");
382 time_str[sizeof(time_str) - 1] = '\0';
384 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
385 sobj->interval))
386 snprintf(interval_str, sizeof(interval_str), "<error>");
387 interval_str[sizeof(interval_str) - 1] = '\0';
389 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
390 if (type == SDB_ATTRIBUTE) {
391 char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
392 sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
393 SDB_DOUBLE_QUOTED);
394 sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
395 "\"update_interval\": \"%s\"}", tmp, time_str,
396 interval_str);
397 }
398 else
399 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
400 "\"update_interval\": \"%s\"}", time_str, interval_str);
402 if (sdb_llist_iter_has_next(iter))
403 sdb_strbuf_append(buf, ",");
404 }
406 sdb_llist_iter_destroy(iter);
407 sdb_strbuf_append(buf, "]");
408 } /* store_obj_tojson */
410 /*
411 * public API
412 */
414 void
415 sdb_store_clear(void)
416 {
417 sdb_llist_destroy(obj_list);
418 obj_list = NULL;
419 } /* sdb_store_clear */
421 int
422 sdb_store_host(const char *name, sdb_time_t last_update)
423 {
424 int status;
426 if (! name)
427 return -1;
429 pthread_rwlock_wrlock(&obj_lock);
430 status = store_obj(/* parent = */ 0, NULL,
431 /* stored object = */ SDB_HOST, name, last_update,
432 /* updated_obj = */ NULL);
433 pthread_rwlock_unlock(&obj_lock);
434 return status;
435 } /* sdb_store_host */
437 _Bool
438 sdb_store_has_host(const char *name)
439 {
440 sdb_store_obj_t *host;
442 if (! name)
443 return NULL;
445 host = store_lookup(SDB_HOST, name);
446 return host != NULL;
447 } /* sdb_store_has_host */
449 sdb_store_base_t *
450 sdb_store_get_host(const char *name)
451 {
452 sdb_store_obj_t *host;
454 if (! name)
455 return NULL;
457 host = store_lookup(SDB_HOST, name);
458 if (! host)
459 return NULL;
461 sdb_object_ref(SDB_OBJ(host));
462 return STORE_BASE(host);
463 } /* sdb_store_get_host */
465 int
466 sdb_store_attribute(const char *hostname,
467 const char *key, const sdb_data_t *value,
468 sdb_time_t last_update)
469 {
470 int status;
472 sdb_store_base_t *updated_attr = NULL;
474 if ((! hostname) || (! key))
475 return -1;
477 pthread_rwlock_wrlock(&obj_lock);
478 status = store_obj(/* parent = */ SDB_HOST, hostname,
479 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
480 &updated_attr);
482 if (status >= 0) {
483 assert(updated_attr);
484 sdb_data_free_datum(&SDB_ATTR(updated_attr)->value);
485 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
486 sdb_object_deref(SDB_OBJ(updated_attr));
487 status = -1;
488 }
489 }
491 pthread_rwlock_unlock(&obj_lock);
492 return status;
493 } /* sdb_store_attribute */
495 int
496 sdb_store_service(const char *hostname, const char *name,
497 sdb_time_t last_update)
498 {
499 int status;
501 if ((! hostname) || (! name))
502 return -1;
504 pthread_rwlock_wrlock(&obj_lock);
505 status = store_obj(/* parent = */ SDB_HOST, hostname,
506 /* stored object = */ SDB_SERVICE, name, last_update,
507 /* updated obj = */ NULL);
508 pthread_rwlock_unlock(&obj_lock);
509 return status;
510 } /* sdb_store_service */
512 int
513 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
514 {
515 sdb_store_obj_t *host;
516 char time_str[64];
517 char interval_str[64];
519 if ((! h) || (h->type != SDB_HOST) || (! buf))
520 return -1;
522 host = SDB_STORE_OBJ(h);
524 if (! sdb_strftime(time_str, sizeof(time_str),
525 "%F %T %z", host->_last_update))
526 snprintf(time_str, sizeof(time_str), "<error>");
527 time_str[sizeof(time_str) - 1] = '\0';
529 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
530 host->_interval))
531 snprintf(interval_str, sizeof(interval_str), "<error>");
532 interval_str[sizeof(interval_str) - 1] = '\0';
534 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
535 "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
536 SDB_OBJ(host)->name, time_str, interval_str);
538 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
539 sdb_strbuf_append(buf, ", \"attributes\": ");
540 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
541 }
543 if (! (flags & SDB_SKIP_SERVICES)) {
544 sdb_strbuf_append(buf, ", \"services\": ");
545 store_obj_tojson(host->children, SDB_SERVICE, buf);
546 }
548 sdb_strbuf_append(buf, "}");
549 return 0;
550 } /* sdb_store_host_tojson */
552 int
553 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
554 {
555 sdb_llist_iter_t *host_iter;
557 if (! buf)
558 return -1;
560 pthread_rwlock_rdlock(&obj_lock);
562 host_iter = sdb_llist_get_iter(obj_list);
563 if (! host_iter) {
564 pthread_rwlock_unlock(&obj_lock);
565 return -1;
566 }
568 sdb_strbuf_append(buf, "{\"hosts\":[");
570 while (sdb_llist_iter_has_next(host_iter)) {
571 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
572 assert(host);
574 if (sdb_store_host_tojson(host, buf, flags))
575 return -1;
577 if (sdb_llist_iter_has_next(host_iter))
578 sdb_strbuf_append(buf, ",");
579 }
581 sdb_strbuf_append(buf, "]}");
583 sdb_llist_iter_destroy(host_iter);
584 pthread_rwlock_unlock(&obj_lock);
585 return 0;
586 } /* sdb_store_tojson */
588 /* TODO: actually support hierarchical data */
589 int
590 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
591 {
592 sdb_llist_iter_t *host_iter;
593 int status = 0;
595 pthread_rwlock_rdlock(&obj_lock);
597 host_iter = sdb_llist_get_iter(obj_list);
598 if (! host_iter)
599 status = -1;
601 /* has_next returns false if the iterator is NULL */
602 while (sdb_llist_iter_has_next(host_iter)) {
603 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
604 assert(host);
606 if (cb(host, user_data)) {
607 status = -1;
608 break;
609 }
610 }
612 sdb_llist_iter_destroy(host_iter);
613 pthread_rwlock_unlock(&obj_lock);
614 return status;
615 } /* sdb_store_iterate */
617 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */