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->services = sdb_llist_create();
96 if (! sobj->services)
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->services)
114 sdb_llist_destroy(sobj->services);
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->services, 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", SDB_STORE_TYPE_TO_NAME(type),
263 name, SDB_STORE_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->services;
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 SDB_STORE_TYPE_TO_NAME(type), name,
291 last_update, old->last_update);
292 /* don't report an error; the object may be updated by multiple
293 * backends */
294 status = 1;
295 }
296 else {
297 sdb_time_t interval = last_update - old->last_update;
298 old->last_update = last_update;
299 if (interval) {
300 if (old->interval)
301 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
302 + (0.1 * (double)interval));
303 else
304 old->interval = interval;
305 }
306 }
308 if (updated_obj)
309 *updated_obj = old;
310 }
311 else {
312 sdb_store_base_t *new;
314 if (type == SDB_ATTRIBUTE)
315 /* the value will be updated by the caller */
316 new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
317 type, last_update, NULL));
318 else
319 new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
320 type, last_update));
322 if (! new) {
323 char errbuf[1024];
324 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
325 SDB_STORE_TYPE_TO_NAME(type), name,
326 sdb_strerror(errno, errbuf, sizeof(errbuf)));
327 free(parent_cname);
328 free(cname);
329 return -1;
330 }
332 /* TODO: insert type-aware; the current version works as long as we
333 * don't support to store hierarchical data */
334 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
335 sdb_object_cmp_by_name);
337 /* pass control to the list or destroy in case of an error */
338 sdb_object_deref(SDB_OBJ(new));
340 if (updated_obj)
341 *updated_obj = new;
342 }
343 free(parent_cname);
344 free(cname);
345 return status;
346 } /* store_obj */
348 /*
349 * store_obj_tojson serializes attribute / service objects to JSON.
350 *
351 * The function never returns an error. Rather, an error message will be part
352 * of the serialized data.
353 */
354 static void
355 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
356 {
357 sdb_llist_iter_t *iter;
358 char time_str[64];
359 char interval_str[64];
361 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
363 sdb_strbuf_append(buf, "[");
365 iter = sdb_llist_get_iter(list);
366 if (! iter) {
367 char errbuf[1024];
368 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
369 SDB_STORE_TYPE_TO_NAME(type),
370 sdb_strerror(errno, errbuf, sizeof(errbuf)));
371 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
372 SDB_STORE_TYPE_TO_NAME(type), errbuf);
373 }
375 /* has_next returns false if the iterator is NULL */
376 while (sdb_llist_iter_has_next(iter)) {
377 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
378 assert(sobj);
380 if (! sdb_strftime(time_str, sizeof(time_str),
381 "%F %T %z", sobj->last_update))
382 snprintf(time_str, sizeof(time_str), "<error>");
383 time_str[sizeof(time_str) - 1] = '\0';
385 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
386 sobj->interval))
387 snprintf(interval_str, sizeof(interval_str), "<error>");
388 interval_str[sizeof(interval_str) - 1] = '\0';
390 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
391 if (type == SDB_ATTRIBUTE) {
392 char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
393 sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
394 SDB_DOUBLE_QUOTED);
395 sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
396 "\"update_interval\": \"%s\"}", tmp, time_str,
397 interval_str);
398 }
399 else
400 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
401 "\"update_interval\": \"%s\"}", time_str, interval_str);
403 if (sdb_llist_iter_has_next(iter))
404 sdb_strbuf_append(buf, ",");
405 }
407 sdb_llist_iter_destroy(iter);
408 sdb_strbuf_append(buf, "]");
409 } /* store_obj_tojson */
411 /*
412 * public API
413 */
415 void
416 sdb_store_clear(void)
417 {
418 sdb_llist_destroy(obj_list);
419 obj_list = NULL;
420 } /* sdb_store_clear */
422 int
423 sdb_store_host(const char *name, sdb_time_t last_update)
424 {
425 int status;
427 if (! name)
428 return -1;
430 pthread_rwlock_wrlock(&obj_lock);
431 status = store_obj(/* parent = */ 0, NULL,
432 /* stored object = */ SDB_HOST, name, last_update,
433 /* updated_obj = */ NULL);
434 pthread_rwlock_unlock(&obj_lock);
435 return status;
436 } /* sdb_store_host */
438 _Bool
439 sdb_store_has_host(const char *name)
440 {
441 sdb_store_obj_t *host;
443 if (! name)
444 return NULL;
446 host = store_lookup(SDB_HOST, name);
447 return host != NULL;
448 } /* sdb_store_has_host */
450 sdb_store_base_t *
451 sdb_store_get_host(const char *name)
452 {
453 sdb_store_obj_t *host;
455 if (! name)
456 return NULL;
458 host = store_lookup(SDB_HOST, name);
459 if (! host)
460 return NULL;
462 sdb_object_ref(SDB_OBJ(host));
463 return STORE_BASE(host);
464 } /* sdb_store_get_host */
466 int
467 sdb_store_attribute(const char *hostname,
468 const char *key, const sdb_data_t *value,
469 sdb_time_t last_update)
470 {
471 int status;
473 sdb_store_base_t *updated_attr = NULL;
475 if ((! hostname) || (! key))
476 return -1;
478 pthread_rwlock_wrlock(&obj_lock);
479 status = store_obj(/* parent = */ SDB_HOST, hostname,
480 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
481 &updated_attr);
483 if (status >= 0) {
484 assert(updated_attr);
485 sdb_data_free_datum(&SDB_ATTR(updated_attr)->value);
486 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
487 sdb_object_deref(SDB_OBJ(updated_attr));
488 status = -1;
489 }
490 }
492 pthread_rwlock_unlock(&obj_lock);
493 return status;
494 } /* sdb_store_attribute */
496 int
497 sdb_store_service(const char *hostname, const char *name,
498 sdb_time_t last_update)
499 {
500 int status;
502 if ((! hostname) || (! name))
503 return -1;
505 pthread_rwlock_wrlock(&obj_lock);
506 status = store_obj(/* parent = */ SDB_HOST, hostname,
507 /* stored object = */ SDB_SERVICE, name, last_update,
508 /* updated obj = */ NULL);
509 pthread_rwlock_unlock(&obj_lock);
510 return status;
511 } /* sdb_store_service */
513 int
514 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
515 {
516 sdb_store_obj_t *host;
517 char time_str[64];
518 char interval_str[64];
520 if ((! h) || (h->type != SDB_HOST) || (! buf))
521 return -1;
523 host = SDB_STORE_OBJ(h);
525 if (! sdb_strftime(time_str, sizeof(time_str),
526 "%F %T %z", host->_last_update))
527 snprintf(time_str, sizeof(time_str), "<error>");
528 time_str[sizeof(time_str) - 1] = '\0';
530 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
531 host->_interval))
532 snprintf(interval_str, sizeof(interval_str), "<error>");
533 interval_str[sizeof(interval_str) - 1] = '\0';
535 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
536 "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
537 SDB_OBJ(host)->name, time_str, interval_str);
539 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
540 sdb_strbuf_append(buf, ", \"attributes\": ");
541 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
542 }
544 if (! (flags & SDB_SKIP_SERVICES)) {
545 sdb_strbuf_append(buf, ", \"services\": ");
546 store_obj_tojson(host->services, SDB_SERVICE, buf);
547 }
549 sdb_strbuf_append(buf, "}");
550 return 0;
551 } /* sdb_store_host_tojson */
553 int
554 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
555 {
556 sdb_llist_iter_t *host_iter;
558 if (! buf)
559 return -1;
561 pthread_rwlock_rdlock(&obj_lock);
563 host_iter = sdb_llist_get_iter(obj_list);
564 if (! host_iter) {
565 pthread_rwlock_unlock(&obj_lock);
566 return -1;
567 }
569 sdb_strbuf_append(buf, "{\"hosts\":[");
571 while (sdb_llist_iter_has_next(host_iter)) {
572 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
573 assert(host);
575 if (sdb_store_host_tojson(host, buf, flags))
576 return -1;
578 if (sdb_llist_iter_has_next(host_iter))
579 sdb_strbuf_append(buf, ",");
580 }
582 sdb_strbuf_append(buf, "]}");
584 sdb_llist_iter_destroy(host_iter);
585 pthread_rwlock_unlock(&obj_lock);
586 return 0;
587 } /* sdb_store_tojson */
589 /* TODO: actually support hierarchical data */
590 int
591 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
592 {
593 sdb_llist_iter_t *host_iter;
594 int status = 0;
596 pthread_rwlock_rdlock(&obj_lock);
598 host_iter = sdb_llist_get_iter(obj_list);
599 if (! host_iter)
600 status = -1;
602 /* has_next returns false if the iterator is NULL */
603 while (sdb_llist_iter_has_next(host_iter)) {
604 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
605 assert(host);
607 if (cb(host, user_data)) {
608 status = -1;
609 break;
610 }
611 }
613 sdb_llist_iter_destroy(host_iter);
614 pthread_rwlock_unlock(&obj_lock);
615 return status;
616 } /* sdb_store_iterate */
618 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */