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 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(host_list, type, name);
205 } /* store_lookup */
207 /* The obj_lock has to be acquired before calling this function. */
208 static int
209 store_obj(const char *hostname, int type, const char *name,
210 sdb_time_t last_update, sdb_store_base_t **updated_obj)
211 {
212 char *host_cname = NULL, *cname = NULL;
214 sdb_llist_t *parent_list;
215 sdb_store_base_t *old;
216 int status = 0;
218 if (last_update <= 0)
219 last_update = sdb_gettime();
221 assert((type == 0)
222 || (type == SDB_HOST)
223 || (type == SDB_SERVICE)
224 || (type == SDB_ATTRIBUTE));
226 if (! host_list)
227 if (! (host_list = sdb_llist_create()))
228 return -1;
229 parent_list = host_list;
231 if (type == SDB_HOST) {
232 cname = sdb_plugin_cname(strdup(name));
233 if (! cname) {
234 sdb_log(SDB_LOG_ERR, "store: strdup failed");
235 return -1;
236 }
237 name = cname;
238 }
240 if (hostname) {
241 sdb_store_obj_t *parent;
243 host_cname = sdb_plugin_cname(strdup(hostname));
244 if (! host_cname) {
245 sdb_log(SDB_LOG_ERR, "store: strdup failed");
246 free(cname);
247 return -1;
248 }
249 hostname = host_cname;
251 parent = store_lookup(SDB_HOST, hostname);
252 if (! parent) {
253 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
254 "host '%s' not found", SDB_STORE_TYPE_TO_NAME(type),
255 name, hostname);
256 free(host_cname);
257 free(cname);
258 return -1;
259 }
261 if (type == SDB_ATTRIBUTE)
262 parent_list = parent->attributes;
263 else
264 parent_list = parent->services;
265 }
267 if (type == SDB_HOST)
268 /* make sure that each host is unique */
269 old = STORE_BASE(store_lookup_in_list(host_list, type, name));
270 else if (type == SDB_ATTRIBUTE)
271 /* look into attributes of this host */
272 old = STORE_BASE(sdb_llist_search_by_name(parent_list, name));
273 else
274 /* look into services assigned to this host (store_lookup_in_list
275 * does not look up services from hierarchical hosts) */
276 old = STORE_BASE(store_lookup_in_list(parent_list, type, name));
278 if (old) {
279 if (old->last_update > last_update) {
280 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
281 "value too old (%"PRIscTIME" < %"PRIscTIME")",
282 SDB_STORE_TYPE_TO_NAME(type), name,
283 last_update, old->last_update);
284 /* don't report an error; the object may be updated by multiple
285 * backends */
286 status = 1;
287 }
288 else {
289 sdb_time_t interval = last_update - old->last_update;
290 old->last_update = last_update;
291 if (interval) {
292 if (old->interval)
293 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
294 + (0.1 * (double)interval));
295 else
296 old->interval = interval;
297 }
298 }
300 if (updated_obj)
301 *updated_obj = old;
302 }
303 else {
304 sdb_store_base_t *new;
306 if (type == SDB_ATTRIBUTE)
307 /* the value will be updated by the caller */
308 new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
309 type, last_update, NULL));
310 else
311 new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
312 type, last_update));
314 if (! new) {
315 char errbuf[1024];
316 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
317 SDB_STORE_TYPE_TO_NAME(type), name,
318 sdb_strerror(errno, errbuf, sizeof(errbuf)));
319 free(host_cname);
320 free(cname);
321 return -1;
322 }
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));
330 if (updated_obj)
331 *updated_obj = new;
332 }
333 free(host_cname);
334 free(cname);
335 return status;
336 } /* store_obj */
338 /*
339 * store_obj_tojson serializes attribute / service objects to JSON.
340 *
341 * The function never returns an error. Rather, an error message will be part
342 * of the serialized data.
343 */
344 static void
345 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
346 {
347 sdb_llist_iter_t *iter;
348 char time_str[64];
349 char interval_str[64];
351 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
353 sdb_strbuf_append(buf, "[");
355 iter = sdb_llist_get_iter(list);
356 if (! iter) {
357 char errbuf[1024];
358 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
359 SDB_STORE_TYPE_TO_NAME(type),
360 sdb_strerror(errno, errbuf, sizeof(errbuf)));
361 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
362 SDB_STORE_TYPE_TO_NAME(type), errbuf);
363 }
365 /* has_next returns false if the iterator is NULL */
366 while (sdb_llist_iter_has_next(iter)) {
367 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
368 assert(sobj);
370 if (! sdb_strftime(time_str, sizeof(time_str),
371 "%F %T %z", sobj->last_update))
372 snprintf(time_str, sizeof(time_str), "<error>");
373 time_str[sizeof(time_str) - 1] = '\0';
375 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
376 sobj->interval))
377 snprintf(interval_str, sizeof(interval_str), "<error>");
378 interval_str[sizeof(interval_str) - 1] = '\0';
380 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
381 if (type == SDB_ATTRIBUTE) {
382 char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
383 sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
384 SDB_DOUBLE_QUOTED);
385 sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
386 "\"update_interval\": \"%s\"}", tmp, time_str,
387 interval_str);
388 }
389 else
390 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
391 "\"update_interval\": \"%s\"}", time_str, interval_str);
393 if (sdb_llist_iter_has_next(iter))
394 sdb_strbuf_append(buf, ",");
395 }
397 sdb_llist_iter_destroy(iter);
398 sdb_strbuf_append(buf, "]");
399 } /* store_obj_tojson */
401 /*
402 * public API
403 */
405 void
406 sdb_store_clear(void)
407 {
408 sdb_llist_destroy(host_list);
409 host_list = NULL;
410 } /* sdb_store_clear */
412 int
413 sdb_store_host(const char *name, sdb_time_t last_update)
414 {
415 int status;
417 if (! name)
418 return -1;
420 pthread_rwlock_wrlock(&obj_lock);
421 status = store_obj(/* hostname = */ NULL,
422 /* stored object = */ SDB_HOST, name, last_update,
423 /* updated_obj = */ NULL);
424 pthread_rwlock_unlock(&obj_lock);
425 return status;
426 } /* sdb_store_host */
428 _Bool
429 sdb_store_has_host(const char *name)
430 {
431 sdb_store_obj_t *host;
433 if (! name)
434 return NULL;
436 host = store_lookup(SDB_HOST, name);
437 return host != NULL;
438 } /* sdb_store_has_host */
440 sdb_store_base_t *
441 sdb_store_get_host(const char *name)
442 {
443 sdb_store_obj_t *host;
445 if (! name)
446 return NULL;
448 host = store_lookup(SDB_HOST, name);
449 if (! host)
450 return NULL;
452 sdb_object_ref(SDB_OBJ(host));
453 return STORE_BASE(host);
454 } /* sdb_store_get_host */
456 int
457 sdb_store_attribute(const char *hostname,
458 const char *key, const sdb_data_t *value,
459 sdb_time_t last_update)
460 {
461 int status;
463 sdb_store_base_t *updated_attr = NULL;
465 if ((! hostname) || (! key))
466 return -1;
468 pthread_rwlock_wrlock(&obj_lock);
469 status = store_obj(hostname,
470 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
471 &updated_attr);
473 if (status >= 0) {
474 assert(updated_attr);
475 sdb_data_free_datum(&SDB_ATTR(updated_attr)->value);
476 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
477 sdb_object_deref(SDB_OBJ(updated_attr));
478 status = -1;
479 }
480 }
482 pthread_rwlock_unlock(&obj_lock);
483 return status;
484 } /* sdb_store_attribute */
486 int
487 sdb_store_service(const char *hostname, const char *name,
488 sdb_time_t last_update)
489 {
490 int status;
492 if ((! hostname) || (! name))
493 return -1;
495 pthread_rwlock_wrlock(&obj_lock);
496 status = store_obj(hostname,
497 /* stored object = */ SDB_SERVICE, name, last_update,
498 /* updated obj = */ NULL);
499 pthread_rwlock_unlock(&obj_lock);
500 return status;
501 } /* sdb_store_service */
503 int
504 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
505 {
506 sdb_store_obj_t *host;
507 char time_str[64];
508 char interval_str[64];
510 if ((! h) || (h->type != SDB_HOST) || (! buf))
511 return -1;
513 host = SDB_STORE_OBJ(h);
515 if (! sdb_strftime(time_str, sizeof(time_str),
516 "%F %T %z", host->_last_update))
517 snprintf(time_str, sizeof(time_str), "<error>");
518 time_str[sizeof(time_str) - 1] = '\0';
520 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
521 host->_interval))
522 snprintf(interval_str, sizeof(interval_str), "<error>");
523 interval_str[sizeof(interval_str) - 1] = '\0';
525 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
526 "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
527 SDB_OBJ(host)->name, time_str, interval_str);
529 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
530 sdb_strbuf_append(buf, ", \"attributes\": ");
531 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
532 }
534 if (! (flags & SDB_SKIP_SERVICES)) {
535 sdb_strbuf_append(buf, ", \"services\": ");
536 store_obj_tojson(host->services, SDB_SERVICE, buf);
537 }
539 sdb_strbuf_append(buf, "}");
540 return 0;
541 } /* sdb_store_host_tojson */
543 int
544 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
545 {
546 sdb_llist_iter_t *host_iter;
548 if (! buf)
549 return -1;
551 pthread_rwlock_rdlock(&obj_lock);
553 host_iter = sdb_llist_get_iter(host_list);
554 if (! host_iter) {
555 pthread_rwlock_unlock(&obj_lock);
556 return -1;
557 }
559 sdb_strbuf_append(buf, "{\"hosts\":[");
561 while (sdb_llist_iter_has_next(host_iter)) {
562 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
563 assert(host);
565 if (sdb_store_host_tojson(host, buf, flags))
566 return -1;
568 if (sdb_llist_iter_has_next(host_iter))
569 sdb_strbuf_append(buf, ",");
570 }
572 sdb_strbuf_append(buf, "]}");
574 sdb_llist_iter_destroy(host_iter);
575 pthread_rwlock_unlock(&obj_lock);
576 return 0;
577 } /* sdb_store_tojson */
579 /* TODO: actually support hierarchical data */
580 int
581 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
582 {
583 sdb_llist_iter_t *host_iter;
584 int status = 0;
586 pthread_rwlock_rdlock(&obj_lock);
588 host_iter = sdb_llist_get_iter(host_list);
589 if (! host_iter)
590 status = -1;
592 /* has_next returns false if the iterator is NULL */
593 while (sdb_llist_iter_has_next(host_iter)) {
594 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
595 assert(host);
597 if (cb(host, user_data)) {
598 status = -1;
599 break;
600 }
601 }
603 sdb_llist_iter_destroy(host_iter);
604 pthread_rwlock_unlock(&obj_lock);
605 return status;
606 } /* sdb_store_iterate */
608 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */