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 /* The host_lock has to be acquired before calling this function. */
214 static int
215 store_obj(const char *hostname, int type, const char *name,
216 sdb_time_t last_update, sdb_store_obj_t **updated_obj)
217 {
218 char *host_cname = NULL, *cname = NULL;
219 char **tmp;
221 sdb_llist_t *parent_list;
222 sdb_store_obj_t *old, *new;
223 const sdb_plugin_info_t *info;
225 int status = 0;
226 size_t i;
228 if (last_update <= 0)
229 last_update = sdb_gettime();
231 assert((type == 0)
232 || (type == SDB_HOST)
233 || (type == SDB_SERVICE)
234 || (type == SDB_ATTRIBUTE));
236 assert(hostname || (type == SDB_HOST));
237 assert((! hostname)
238 || (type == SDB_SERVICE)
239 || (type == SDB_ATTRIBUTE));
241 if (! host_list)
242 if (! (host_list = sdb_llist_create()))
243 return -1;
244 parent_list = host_list;
246 if (type == SDB_HOST) {
247 cname = sdb_plugin_cname(strdup(name));
248 if (! cname) {
249 sdb_log(SDB_LOG_ERR, "store: strdup failed");
250 return -1;
251 }
252 name = cname;
253 }
255 if (hostname) {
256 sdb_host_t *host;
258 host_cname = sdb_plugin_cname(strdup(hostname));
259 if (! host_cname) {
260 sdb_log(SDB_LOG_ERR, "store: strdup failed");
261 free(cname);
262 return -1;
263 }
264 hostname = host_cname;
266 host = lookup_host(hostname);
267 if (! host) {
268 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
269 "host '%s' not found", SDB_STORE_TYPE_TO_NAME(type),
270 name, hostname);
271 free(host_cname);
272 free(cname);
273 return -1;
274 }
276 if (type == SDB_ATTRIBUTE)
277 parent_list = host->attributes;
278 else
279 parent_list = host->services;
280 }
282 if (type == SDB_HOST)
283 old = STORE_OBJ(sdb_llist_search_by_name(host_list, name));
284 else
285 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
287 if (old) {
288 if (old->last_update > last_update) {
289 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
290 "value too old (%"PRIscTIME" < %"PRIscTIME")",
291 SDB_STORE_TYPE_TO_NAME(type), name,
292 last_update, old->last_update);
293 /* don't report an error; the object may be updated by multiple
294 * backends */
295 status = 1;
296 }
297 else {
298 sdb_time_t interval = last_update - old->last_update;
299 old->last_update = last_update;
300 if (interval) {
301 if (old->interval)
302 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
303 + (0.1 * (double)interval));
304 else
305 old->interval = interval;
306 }
307 }
309 new = old;
310 }
311 else {
312 if (type == SDB_ATTRIBUTE) {
313 /* the value will be updated by the caller */
314 new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
315 type, last_update, NULL));
316 }
317 else {
318 sdb_type_t t;
319 t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
320 new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
321 }
323 if (new) {
324 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
325 sdb_object_cmp_by_name);
327 /* pass control to the list or destroy in case of an error */
328 sdb_object_deref(SDB_OBJ(new));
329 }
330 else {
331 char errbuf[1024];
332 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
333 SDB_STORE_TYPE_TO_NAME(type), name,
334 sdb_strerror(errno, errbuf, sizeof(errbuf)));
335 status = -1;
336 }
337 }
339 free(host_cname);
340 free(cname);
342 if (status < 0)
343 return status;
344 assert(new);
346 if (updated_obj)
347 *updated_obj = new;
349 info = sdb_plugin_current();
350 if (! info)
351 return status;
353 for (i = 0; i < new->backends_num; ++i)
354 if (!strcasecmp(new->backends[i], info->plugin_name))
355 return status;
357 tmp = realloc(new->backends,
358 (new->backends_num + 1) * sizeof(*new->backends));
359 if (! tmp)
360 return -1;
362 new->backends = tmp;
363 new->backends[new->backends_num] = strdup(info->plugin_name);
364 if (! new->backends[new->backends_num])
365 return -1;
367 ++new->backends_num;
368 return status;
369 } /* store_obj */
371 /*
372 * store_common_tojson serializes common object attributes to JSON.
373 *
374 * The function never returns an error. Rather, an error message will be part
375 * of the serialized data.
376 */
377 static void
378 store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
379 {
380 char time_str[64];
381 char interval_str[64];
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\"", time_str, interval_str);
395 } /* store_common_tojson */
397 /*
398 * store_obj_tojson serializes attribute / service objects to JSON.
399 *
400 * The function never returns an error. Rather, an error message will be part
401 * of the serialized data.
402 */
403 static void
404 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
405 {
406 sdb_llist_iter_t *iter;
408 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
410 sdb_strbuf_append(buf, "[");
411 iter = sdb_llist_get_iter(list);
412 if (! iter) {
413 char errbuf[1024];
414 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
415 SDB_STORE_TYPE_TO_NAME(type),
416 sdb_strerror(errno, errbuf, sizeof(errbuf)));
417 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
418 SDB_STORE_TYPE_TO_NAME(type), errbuf);
419 }
421 /* has_next returns false if the iterator is NULL */
422 while (sdb_llist_iter_has_next(iter)) {
423 sdb_store_obj_t *sobj = STORE_OBJ(sdb_llist_iter_get_next(iter));
424 assert(sobj);
425 assert(sobj->type == type);
427 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
428 if (sobj->type == SDB_ATTRIBUTE) {
429 char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
430 sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
431 SDB_DOUBLE_QUOTED);
432 sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
433 }
435 store_common_tojson(sobj, buf);
436 sdb_strbuf_append(buf, "}");
438 if (sdb_llist_iter_has_next(iter))
439 sdb_strbuf_append(buf, ",");
440 }
442 sdb_llist_iter_destroy(iter);
443 sdb_strbuf_append(buf, "]");
444 } /* store_obj_tojson */
446 /*
447 * public API
448 */
450 void
451 sdb_store_clear(void)
452 {
453 sdb_llist_destroy(host_list);
454 host_list = NULL;
455 } /* sdb_store_clear */
457 int
458 sdb_store_host(const char *name, sdb_time_t last_update)
459 {
460 int status;
462 if (! name)
463 return -1;
465 pthread_rwlock_wrlock(&host_lock);
466 status = store_obj(/* hostname = */ NULL,
467 /* stored object = */ SDB_HOST, name, last_update,
468 /* updated_obj = */ NULL);
469 pthread_rwlock_unlock(&host_lock);
470 return status;
471 } /* sdb_store_host */
473 _Bool
474 sdb_store_has_host(const char *name)
475 {
476 sdb_host_t *host;
478 if (! name)
479 return NULL;
481 host = lookup_host(name);
482 return host != NULL;
483 } /* sdb_store_has_host */
485 sdb_store_obj_t *
486 sdb_store_get_host(const char *name)
487 {
488 sdb_host_t *host;
490 if (! name)
491 return NULL;
493 host = lookup_host(name);
494 if (! host)
495 return NULL;
497 sdb_object_ref(SDB_OBJ(host));
498 return STORE_OBJ(host);
499 } /* sdb_store_get_host */
501 int
502 sdb_store_attribute(const char *hostname,
503 const char *key, const sdb_data_t *value,
504 sdb_time_t last_update)
505 {
506 int status;
508 sdb_store_obj_t *updated_attr = NULL;
510 if ((! hostname) || (! key))
511 return -1;
513 pthread_rwlock_wrlock(&host_lock);
514 status = store_obj(hostname,
515 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
516 &updated_attr);
518 if (status >= 0) {
519 assert(updated_attr);
520 sdb_data_free_datum(&ATTR(updated_attr)->value);
521 if (sdb_data_copy(&ATTR(updated_attr)->value, value)) {
522 sdb_object_deref(SDB_OBJ(updated_attr));
523 status = -1;
524 }
525 }
527 pthread_rwlock_unlock(&host_lock);
528 return status;
529 } /* sdb_store_attribute */
531 int
532 sdb_store_service(const char *hostname, const char *name,
533 sdb_time_t last_update)
534 {
535 int status;
537 if ((! hostname) || (! name))
538 return -1;
540 pthread_rwlock_wrlock(&host_lock);
541 status = store_obj(hostname,
542 /* stored object = */ SDB_SERVICE, name, last_update,
543 /* updated obj = */ NULL);
544 pthread_rwlock_unlock(&host_lock);
545 return status;
546 } /* sdb_store_service */
548 int
549 sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, int flags)
550 {
551 sdb_host_t *host;
553 if ((! h) || (h->type != SDB_HOST) || (! buf))
554 return -1;
556 host = HOST(h);
558 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(host)->name);
559 store_common_tojson(h, buf);
561 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
562 sdb_strbuf_append(buf, ", \"attributes\": ");
563 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
564 }
566 if (! (flags & SDB_SKIP_SERVICES)) {
567 sdb_strbuf_append(buf, ", \"services\": ");
568 store_obj_tojson(host->services, SDB_SERVICE, buf);
569 }
571 sdb_strbuf_append(buf, "}");
572 return 0;
573 } /* sdb_store_host_tojson */
575 int
576 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
577 {
578 sdb_llist_iter_t *host_iter;
580 if (! buf)
581 return -1;
583 pthread_rwlock_rdlock(&host_lock);
585 host_iter = sdb_llist_get_iter(host_list);
586 if (! host_iter) {
587 pthread_rwlock_unlock(&host_lock);
588 return -1;
589 }
591 sdb_strbuf_append(buf, "{\"hosts\":[");
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 (sdb_store_host_tojson(host, buf, flags))
598 return -1;
600 if (sdb_llist_iter_has_next(host_iter))
601 sdb_strbuf_append(buf, ",");
602 }
604 sdb_strbuf_append(buf, "]}");
606 sdb_llist_iter_destroy(host_iter);
607 pthread_rwlock_unlock(&host_lock);
608 return 0;
609 } /* sdb_store_tojson */
611 /* TODO: actually support hierarchical data */
612 int
613 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
614 {
615 sdb_llist_iter_t *host_iter;
616 int status = 0;
618 pthread_rwlock_rdlock(&host_lock);
620 host_iter = sdb_llist_get_iter(host_list);
621 if (! host_iter)
622 status = -1;
624 /* has_next returns false if the iterator is NULL */
625 while (sdb_llist_iter_has_next(host_iter)) {
626 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
627 assert(host);
629 if (cb(host, user_data)) {
630 status = -1;
631 break;
632 }
633 }
635 sdb_llist_iter_destroy(host_iter);
636 pthread_rwlock_unlock(&host_lock);
637 return status;
638 } /* sdb_store_iterate */
640 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */