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 (old->interval)
299 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
300 + (0.1 * (double)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)
353 {
354 sdb_llist_iter_t *iter;
355 char time_str[64];
356 char interval_str[64];
358 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
360 sdb_strbuf_append(buf, "[");
362 iter = sdb_llist_get_iter(list);
363 if (! iter) {
364 char errbuf[1024];
365 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
366 TYPE_TO_NAME(type),
367 sdb_strerror(errno, errbuf, sizeof(errbuf)));
368 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
369 TYPE_TO_NAME(type), errbuf);
370 }
372 /* has_next returns false if the iterator is NULL */
373 while (sdb_llist_iter_has_next(iter)) {
374 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
375 assert(sobj);
377 if (! sdb_strftime(time_str, sizeof(time_str),
378 "%F %T %z", sobj->last_update))
379 snprintf(time_str, sizeof(time_str), "<error>");
380 time_str[sizeof(time_str) - 1] = '\0';
382 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
383 sobj->interval))
384 snprintf(interval_str, sizeof(interval_str), "<error>");
385 interval_str[sizeof(interval_str) - 1] = '\0';
387 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
388 if (type == SDB_ATTRIBUTE) {
389 char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
390 sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
391 SDB_DOUBLE_QUOTED);
392 sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
393 "\"update_interval\": \"%s\"}", tmp, time_str,
394 interval_str);
395 }
396 else
397 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
398 "\"update_interval\": \"%s\"}", time_str, interval_str);
400 if (sdb_llist_iter_has_next(iter))
401 sdb_strbuf_append(buf, ",");
402 }
404 sdb_llist_iter_destroy(iter);
405 sdb_strbuf_append(buf, "]");
406 } /* store_obj_tojson */
408 /*
409 * public API
410 */
412 void
413 sdb_store_clear(void)
414 {
415 sdb_llist_destroy(obj_list);
416 obj_list = NULL;
417 } /* sdb_store_clear */
419 int
420 sdb_store_host(const char *name, sdb_time_t last_update)
421 {
422 int status;
424 if (! name)
425 return -1;
427 pthread_rwlock_wrlock(&obj_lock);
428 status = store_obj(/* parent = */ 0, NULL,
429 /* stored object = */ SDB_HOST, name, last_update,
430 /* updated_obj = */ NULL);
431 pthread_rwlock_unlock(&obj_lock);
432 return status;
433 } /* sdb_store_host */
435 _Bool
436 sdb_store_has_host(const char *name)
437 {
438 sdb_store_obj_t *host;
440 if (! name)
441 return NULL;
443 host = store_lookup(SDB_HOST, name);
444 return host != NULL;
445 } /* sdb_store_has_host */
447 sdb_store_base_t *
448 sdb_store_get_host(const char *name)
449 {
450 sdb_store_obj_t *host;
452 if (! name)
453 return NULL;
455 host = store_lookup(SDB_HOST, name);
456 if (! host)
457 return NULL;
459 sdb_object_ref(SDB_OBJ(host));
460 return STORE_BASE(host);
461 } /* sdb_store_get_host */
463 int
464 sdb_store_attribute(const char *hostname,
465 const char *key, const sdb_data_t *value,
466 sdb_time_t last_update)
467 {
468 int status;
470 sdb_store_base_t *updated_attr = NULL;
472 if ((! hostname) || (! key))
473 return -1;
475 pthread_rwlock_wrlock(&obj_lock);
476 status = store_obj(/* parent = */ SDB_HOST, hostname,
477 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
478 &updated_attr);
480 if (status >= 0) {
481 assert(updated_attr);
482 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
483 sdb_object_deref(SDB_OBJ(updated_attr));
484 status = -1;
485 }
486 }
488 pthread_rwlock_unlock(&obj_lock);
489 return status;
490 } /* sdb_store_attribute */
492 int
493 sdb_store_service(const char *hostname, const char *name,
494 sdb_time_t last_update)
495 {
496 int status;
498 if ((! hostname) || (! name))
499 return -1;
501 pthread_rwlock_wrlock(&obj_lock);
502 status = store_obj(/* parent = */ SDB_HOST, hostname,
503 /* stored object = */ SDB_SERVICE, name, last_update,
504 /* updated obj = */ NULL);
505 pthread_rwlock_unlock(&obj_lock);
506 return status;
507 } /* sdb_store_service */
509 int
510 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
511 {
512 sdb_store_obj_t *host;
513 char time_str[64];
514 char interval_str[64];
516 if ((! h) || (h->type != SDB_HOST) || (! buf))
517 return -1;
519 host = SDB_STORE_OBJ(h);
521 if (! sdb_strftime(time_str, sizeof(time_str),
522 "%F %T %z", host->_last_update))
523 snprintf(time_str, sizeof(time_str), "<error>");
524 time_str[sizeof(time_str) - 1] = '\0';
526 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
527 host->_interval))
528 snprintf(interval_str, sizeof(interval_str), "<error>");
529 interval_str[sizeof(interval_str) - 1] = '\0';
531 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
532 "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
533 SDB_OBJ(host)->name, time_str, interval_str);
535 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
536 sdb_strbuf_append(buf, ", \"attributes\": ");
537 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
538 }
540 if (! (flags & SDB_SKIP_SERVICES)) {
541 sdb_strbuf_append(buf, ", \"services\": ");
542 store_obj_tojson(host->children, SDB_SERVICE, buf);
543 }
545 sdb_strbuf_append(buf, "}");
546 return 0;
547 } /* sdb_store_host_tojson */
549 int
550 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
551 {
552 sdb_llist_iter_t *host_iter;
554 if (! buf)
555 return -1;
557 pthread_rwlock_rdlock(&obj_lock);
559 host_iter = sdb_llist_get_iter(obj_list);
560 if (! host_iter) {
561 pthread_rwlock_unlock(&obj_lock);
562 return -1;
563 }
565 sdb_strbuf_append(buf, "{\"hosts\":[");
567 while (sdb_llist_iter_has_next(host_iter)) {
568 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
569 assert(host);
571 if (sdb_store_host_tojson(host, buf, flags))
572 return -1;
574 if (sdb_llist_iter_has_next(host_iter))
575 sdb_strbuf_append(buf, ",");
576 }
578 sdb_strbuf_append(buf, "]}");
580 sdb_llist_iter_destroy(host_iter);
581 pthread_rwlock_unlock(&obj_lock);
582 return 0;
583 } /* sdb_store_tojson */
585 /* TODO: actually support hierarchical data */
586 int
587 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
588 {
589 sdb_llist_iter_t *host_iter;
590 int status = 0;
592 pthread_rwlock_rdlock(&obj_lock);
594 host_iter = sdb_llist_get_iter(obj_list);
595 if (! host_iter)
596 status = -1;
598 /* has_next returns false if the iterator is NULL */
599 while (sdb_llist_iter_has_next(host_iter)) {
600 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
601 assert(host);
603 if (cb(host, user_data)) {
604 status = -1;
605 break;
606 }
607 }
609 sdb_llist_iter_destroy(host_iter);
610 pthread_rwlock_unlock(&obj_lock);
611 return status;
612 } /* sdb_store_iterate */
614 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */