866661a59ee963758db185ab5f3db8b2d7a54710
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 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
252 if (old) {
253 if (old->last_update > last_update) {
254 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
255 "value too old (%"PRIscTIME" < %"PRIscTIME")",
256 SDB_STORE_TYPE_TO_NAME(type), name,
257 last_update, old->last_update);
258 /* don't report an error; the object may be updated by multiple
259 * backends */
260 status = 1;
261 }
262 else {
263 sdb_time_t interval = last_update - old->last_update;
264 old->last_update = last_update;
265 if (interval) {
266 if (old->interval)
267 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
268 + (0.1 * (double)interval));
269 else
270 old->interval = interval;
271 }
272 }
274 new = old;
275 }
276 else {
277 if (type == SDB_ATTRIBUTE) {
278 /* the value will be updated by the caller */
279 new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
280 type, last_update, NULL));
281 }
282 else {
283 sdb_type_t t;
284 t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
285 new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
286 }
288 if (new) {
289 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
290 sdb_object_cmp_by_name);
292 /* pass control to the list or destroy in case of an error */
293 sdb_object_deref(SDB_OBJ(new));
294 }
295 else {
296 char errbuf[1024];
297 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
298 SDB_STORE_TYPE_TO_NAME(type), name,
299 sdb_strerror(errno, errbuf, sizeof(errbuf)));
300 status = -1;
301 }
302 }
304 if (status < 0)
305 return status;
306 assert(new);
308 if (updated_obj)
309 *updated_obj = new;
311 if (record_backend(new))
312 return -1;
313 return status;
314 } /* store_obj */
316 /* The host_lock has to be acquired before calling this function. */
317 static int
318 store_host_obj(const char *hostname, int type, const char *name,
319 sdb_time_t last_update, sdb_store_obj_t **updated_obj)
320 {
321 char *cname = NULL;
322 sdb_host_t *host;
323 sdb_llist_t *parent_list;
325 int status = 0;
327 if (last_update <= 0)
328 last_update = sdb_gettime();
330 assert(hostname);
331 assert((type == SDB_SERVICE) || (type == SDB_ATTRIBUTE));
333 if (! host_list)
334 return -1;
336 cname = sdb_plugin_cname(strdup(hostname));
337 if (! cname) {
338 sdb_log(SDB_LOG_ERR, "store: strdup failed");
339 return -1;
340 }
342 host = lookup_host(cname);
343 if (! host) {
344 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
345 "host '%s' not found", SDB_STORE_TYPE_TO_NAME(type),
346 name, hostname);
347 free(cname);
348 return -1;
349 }
351 if (type == SDB_ATTRIBUTE)
352 parent_list = host->attributes;
353 else
354 parent_list = host->services;
356 status = store_obj(parent_list, type, name,
357 last_update, updated_obj);
359 free(cname);
360 return status;
361 } /* store_host_obj */
363 /*
364 * store_common_tojson serializes common object attributes to JSON.
365 *
366 * The function never returns an error. Rather, an error message will be part
367 * of the serialized data.
368 */
369 static void
370 store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
371 {
372 char time_str[64];
373 char interval_str[64];
374 size_t i;
376 if (! sdb_strftime(time_str, sizeof(time_str),
377 "%F %T %z", obj->last_update))
378 snprintf(time_str, sizeof(time_str), "<error>");
379 time_str[sizeof(time_str) - 1] = '\0';
381 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
382 obj->interval))
383 snprintf(interval_str, sizeof(interval_str), "<error>");
384 interval_str[sizeof(interval_str) - 1] = '\0';
386 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
387 "\"update_interval\": \"%s\", \"backends\": [",
388 time_str, interval_str);
390 for (i = 0; i < obj->backends_num; ++i) {
391 sdb_strbuf_append(buf, "\"%s\"", obj->backends[i]);
392 if (i < obj->backends_num - 1)
393 sdb_strbuf_append(buf, ",");
394 }
395 sdb_strbuf_append(buf, "]");
396 } /* store_common_tojson */
398 /*
399 * store_obj_tojson serializes attribute / service objects to JSON.
400 *
401 * The function never returns an error. Rather, an error message will be part
402 * of the serialized data.
403 */
404 static void
405 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
406 {
407 sdb_llist_iter_t *iter;
409 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
411 sdb_strbuf_append(buf, "[");
412 iter = sdb_llist_get_iter(list);
413 if (! iter) {
414 char errbuf[1024];
415 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
416 SDB_STORE_TYPE_TO_NAME(type),
417 sdb_strerror(errno, errbuf, sizeof(errbuf)));
418 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
419 SDB_STORE_TYPE_TO_NAME(type), errbuf);
420 }
422 /* has_next returns false if the iterator is NULL */
423 while (sdb_llist_iter_has_next(iter)) {
424 sdb_store_obj_t *sobj = STORE_OBJ(sdb_llist_iter_get_next(iter));
425 assert(sobj);
426 assert(sobj->type == type);
428 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
429 if (sobj->type == SDB_ATTRIBUTE) {
430 char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
431 sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
432 SDB_DOUBLE_QUOTED);
433 sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
434 }
436 store_common_tojson(sobj, buf);
437 sdb_strbuf_append(buf, "}");
439 if (sdb_llist_iter_has_next(iter))
440 sdb_strbuf_append(buf, ",");
441 }
443 sdb_llist_iter_destroy(iter);
444 sdb_strbuf_append(buf, "]");
445 } /* store_obj_tojson */
447 /*
448 * public API
449 */
451 void
452 sdb_store_clear(void)
453 {
454 sdb_llist_destroy(host_list);
455 host_list = NULL;
456 } /* sdb_store_clear */
458 int
459 sdb_store_host(const char *name, sdb_time_t last_update)
460 {
461 char *cname = NULL;
462 int status = 0;
464 if (! name)
465 return -1;
467 if (last_update <= 0)
468 last_update = sdb_gettime();
470 cname = sdb_plugin_cname(strdup(name));
471 if (! cname) {
472 sdb_log(SDB_LOG_ERR, "store: strdup failed");
473 return -1;
474 }
476 pthread_rwlock_wrlock(&host_lock);
477 if (! host_list)
478 if (! (host_list = sdb_llist_create()))
479 status = -1;
481 if (! status)
482 status = store_obj(host_list, SDB_HOST, cname, last_update, NULL);
483 pthread_rwlock_unlock(&host_lock);
485 free(cname);
486 return status;
487 } /* sdb_store_host */
489 _Bool
490 sdb_store_has_host(const char *name)
491 {
492 sdb_host_t *host;
494 if (! name)
495 return NULL;
497 host = lookup_host(name);
498 return host != NULL;
499 } /* sdb_store_has_host */
501 sdb_store_obj_t *
502 sdb_store_get_host(const char *name)
503 {
504 sdb_host_t *host;
506 if (! name)
507 return NULL;
509 host = lookup_host(name);
510 if (! host)
511 return NULL;
513 sdb_object_ref(SDB_OBJ(host));
514 return STORE_OBJ(host);
515 } /* sdb_store_get_host */
517 int
518 sdb_store_attribute(const char *hostname,
519 const char *key, const sdb_data_t *value,
520 sdb_time_t last_update)
521 {
522 int status;
524 sdb_store_obj_t *updated_attr = NULL;
526 if ((! hostname) || (! key))
527 return -1;
529 pthread_rwlock_wrlock(&host_lock);
530 status = store_host_obj(hostname, SDB_ATTRIBUTE, key, last_update,
531 &updated_attr);
533 if (status >= 0) {
534 assert(updated_attr);
535 sdb_data_free_datum(&ATTR(updated_attr)->value);
536 if (sdb_data_copy(&ATTR(updated_attr)->value, value)) {
537 sdb_object_deref(SDB_OBJ(updated_attr));
538 status = -1;
539 }
540 }
542 pthread_rwlock_unlock(&host_lock);
543 return status;
544 } /* sdb_store_attribute */
546 int
547 sdb_store_service(const char *hostname, const char *name,
548 sdb_time_t last_update)
549 {
550 int status;
552 if ((! hostname) || (! name))
553 return -1;
555 pthread_rwlock_wrlock(&host_lock);
556 status = store_host_obj(hostname, SDB_SERVICE, name, last_update, NULL);
557 pthread_rwlock_unlock(&host_lock);
558 return status;
559 } /* sdb_store_service */
561 int
562 sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, int flags)
563 {
564 sdb_host_t *host;
566 if ((! h) || (h->type != SDB_HOST) || (! buf))
567 return -1;
569 host = HOST(h);
571 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(host)->name);
572 store_common_tojson(h, buf);
574 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
575 sdb_strbuf_append(buf, ", \"attributes\": ");
576 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
577 }
579 if (! (flags & SDB_SKIP_SERVICES)) {
580 sdb_strbuf_append(buf, ", \"services\": ");
581 store_obj_tojson(host->services, SDB_SERVICE, buf);
582 }
584 sdb_strbuf_append(buf, "}");
585 return 0;
586 } /* sdb_store_host_tojson */
588 int
589 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
590 {
591 sdb_llist_iter_t *host_iter;
593 if (! buf)
594 return -1;
596 pthread_rwlock_rdlock(&host_lock);
598 host_iter = sdb_llist_get_iter(host_list);
599 if (! host_iter) {
600 pthread_rwlock_unlock(&host_lock);
601 return -1;
602 }
604 sdb_strbuf_append(buf, "{\"hosts\":[");
606 while (sdb_llist_iter_has_next(host_iter)) {
607 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
608 assert(host);
610 if (sdb_store_host_tojson(host, buf, flags))
611 return -1;
613 if (sdb_llist_iter_has_next(host_iter))
614 sdb_strbuf_append(buf, ",");
615 }
617 sdb_strbuf_append(buf, "]}");
619 sdb_llist_iter_destroy(host_iter);
620 pthread_rwlock_unlock(&host_lock);
621 return 0;
622 } /* sdb_store_tojson */
624 /* TODO: actually support hierarchical data */
625 int
626 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
627 {
628 sdb_llist_iter_t *host_iter;
629 int status = 0;
631 pthread_rwlock_rdlock(&host_lock);
633 host_iter = sdb_llist_get_iter(host_list);
634 if (! host_iter)
635 status = -1;
637 /* has_next returns false if the iterator is NULL */
638 while (sdb_llist_iter_has_next(host_iter)) {
639 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
640 assert(host);
642 if (cb(host, user_data)) {
643 status = -1;
644 break;
645 }
646 }
648 sdb_llist_iter_destroy(host_iter);
649 pthread_rwlock_unlock(&host_lock);
650 return status;
651 } /* sdb_store_iterate */
653 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */