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_host_type;
60 static sdb_type_t sdb_service_type;
61 static sdb_type_t sdb_attribute_type;
63 static int
64 store_base_init(sdb_object_t *obj, va_list ap)
65 {
66 sdb_store_base_t *sobj = STORE_BASE(obj);
68 sobj->type = va_arg(ap, int);
70 sobj->last_update = va_arg(ap, sdb_time_t);
71 sobj->interval = 0;
72 sobj->parent = NULL;
73 return 0;
74 } /* store_base_init */
76 static void
77 store_base_destroy(sdb_object_t *obj)
78 {
79 const sdb_store_base_t *sobj = STORE_CONST_BASE(obj);
81 if (sobj->parent)
82 sdb_object_deref(SDB_OBJ(sobj->parent));
83 } /* store_base_destroy */
85 static int
86 sdb_host_init(sdb_object_t *obj, va_list ap)
87 {
88 sdb_host_t *sobj = HOST(obj);
89 int ret;
91 /* this will consume the first argument (type) of ap */
92 ret = store_base_init(obj, ap);
93 if (ret)
94 return ret;
96 sobj->services = sdb_llist_create();
97 if (! sobj->services)
98 return -1;
99 sobj->attributes = sdb_llist_create();
100 if (! sobj->attributes)
101 return -1;
102 return 0;
103 } /* sdb_host_init */
105 static void
106 sdb_host_destroy(sdb_object_t *obj)
107 {
108 sdb_host_t *sobj = HOST(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_host_destroy */
119 static int
120 sdb_service_init(sdb_object_t *obj, va_list ap)
121 {
122 sdb_service_t *sobj = SVC(obj);
123 int ret;
125 /* this will consume the first argument (type) of ap */
126 ret = store_base_init(obj, ap);
127 if (ret)
128 return ret;
130 sobj->attributes = sdb_llist_create();
131 if (! sobj->attributes)
132 return -1;
133 return 0;
134 } /* sdb_service_init */
136 static void
137 sdb_service_destroy(sdb_object_t *obj)
138 {
139 sdb_service_t *sobj = SVC(obj);
140 assert(obj);
142 store_base_destroy(obj);
144 if (sobj->attributes)
145 sdb_llist_destroy(sobj->attributes);
146 } /* sdb_service_destroy */
148 static int
149 sdb_attr_init(sdb_object_t *obj, va_list ap)
150 {
151 const sdb_data_t *value;
152 int ret;
154 /* this will consume the first two arguments
155 * (type and last_update) of ap */
156 ret = store_base_init(obj, ap);
157 if (ret)
158 return ret;
159 value = va_arg(ap, const sdb_data_t *);
161 if (value)
162 if (sdb_data_copy(&ATTR(obj)->value, value))
163 return -1;
164 return 0;
165 } /* sdb_attr_init */
167 static void
168 sdb_attr_destroy(sdb_object_t *obj)
169 {
170 assert(obj);
172 store_base_destroy(obj);
173 sdb_data_free_datum(&ATTR(obj)->value);
174 } /* sdb_attr_destroy */
176 static sdb_type_t sdb_host_type = {
177 sizeof(sdb_host_t),
178 sdb_host_init,
179 sdb_host_destroy
180 };
182 static sdb_type_t sdb_service_type = {
183 sizeof(sdb_service_t),
184 sdb_service_init,
185 sdb_service_destroy
186 };
188 static sdb_type_t sdb_attribute_type = {
189 sizeof(sdb_attribute_t),
190 sdb_attr_init,
191 sdb_attr_destroy
192 };
194 /*
195 * private helper functions
196 */
198 static sdb_host_t *
199 lookup_host(const char *name)
200 {
201 return HOST(sdb_llist_search_by_name(host_list, name));
202 } /* lookup_host */
204 /* The obj_lock has to be acquired before calling this function. */
205 static int
206 store_obj(const char *hostname, int type, const char *name,
207 sdb_time_t last_update, sdb_store_base_t **updated_obj)
208 {
209 char *host_cname = NULL, *cname = NULL;
211 sdb_llist_t *parent_list;
212 sdb_store_base_t *old;
213 int status = 0;
215 if (last_update <= 0)
216 last_update = sdb_gettime();
218 assert((type == 0)
219 || (type == SDB_HOST)
220 || (type == SDB_SERVICE)
221 || (type == SDB_ATTRIBUTE));
223 assert(hostname || (type == SDB_HOST));
224 assert((! hostname)
225 || (type == SDB_SERVICE)
226 || (type == SDB_ATTRIBUTE));
228 if (! host_list)
229 if (! (host_list = sdb_llist_create()))
230 return -1;
231 parent_list = host_list;
233 if (type == SDB_HOST) {
234 cname = sdb_plugin_cname(strdup(name));
235 if (! cname) {
236 sdb_log(SDB_LOG_ERR, "store: strdup failed");
237 return -1;
238 }
239 name = cname;
240 }
242 if (hostname) {
243 sdb_host_t *host;
245 host_cname = sdb_plugin_cname(strdup(hostname));
246 if (! host_cname) {
247 sdb_log(SDB_LOG_ERR, "store: strdup failed");
248 free(cname);
249 return -1;
250 }
251 hostname = host_cname;
253 host = lookup_host(hostname);
254 if (! host) {
255 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
256 "host '%s' not found", SDB_STORE_TYPE_TO_NAME(type),
257 name, hostname);
258 free(host_cname);
259 free(cname);
260 return -1;
261 }
263 if (type == SDB_ATTRIBUTE)
264 parent_list = host->attributes;
265 else
266 parent_list = host->services;
267 }
269 if (type == SDB_HOST)
270 old = STORE_BASE(sdb_llist_search_by_name(host_list, name));
271 else
272 old = STORE_BASE(sdb_llist_search_by_name(parent_list, name));
274 if (old) {
275 if (old->last_update > last_update) {
276 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
277 "value too old (%"PRIscTIME" < %"PRIscTIME")",
278 SDB_STORE_TYPE_TO_NAME(type), name,
279 last_update, old->last_update);
280 /* don't report an error; the object may be updated by multiple
281 * backends */
282 status = 1;
283 }
284 else {
285 sdb_time_t interval = last_update - old->last_update;
286 old->last_update = last_update;
287 if (interval) {
288 if (old->interval)
289 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
290 + (0.1 * (double)interval));
291 else
292 old->interval = interval;
293 }
294 }
296 if (updated_obj)
297 *updated_obj = old;
298 }
299 else {
300 sdb_store_base_t *new;
302 if (type == SDB_ATTRIBUTE) {
303 /* the value will be updated by the caller */
304 new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
305 type, last_update, NULL));
306 }
307 else {
308 sdb_type_t t;
309 t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
310 new = STORE_BASE(sdb_object_create(name, t, type, last_update));
311 }
313 if (! new) {
314 char errbuf[1024];
315 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
316 SDB_STORE_TYPE_TO_NAME(type), name,
317 sdb_strerror(errno, errbuf, sizeof(errbuf)));
318 free(host_cname);
319 free(cname);
320 return -1;
321 }
323 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
324 sdb_object_cmp_by_name);
326 /* pass control to the list or destroy in case of an error */
327 sdb_object_deref(SDB_OBJ(new));
329 if (updated_obj)
330 *updated_obj = new;
331 }
332 free(host_cname);
333 free(cname);
334 return status;
335 } /* store_obj */
337 /*
338 * store_obj_tojson serializes attribute / service objects to JSON.
339 *
340 * The function never returns an error. Rather, an error message will be part
341 * of the serialized data.
342 */
343 static void
344 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
345 {
346 sdb_llist_iter_t *iter;
347 char time_str[64];
348 char interval_str[64];
350 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
352 sdb_strbuf_append(buf, "[");
354 iter = sdb_llist_get_iter(list);
355 if (! iter) {
356 char errbuf[1024];
357 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
358 SDB_STORE_TYPE_TO_NAME(type),
359 sdb_strerror(errno, errbuf, sizeof(errbuf)));
360 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
361 SDB_STORE_TYPE_TO_NAME(type), errbuf);
362 }
364 /* has_next returns false if the iterator is NULL */
365 while (sdb_llist_iter_has_next(iter)) {
366 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
367 assert(sobj);
369 if (! sdb_strftime(time_str, sizeof(time_str),
370 "%F %T %z", sobj->last_update))
371 snprintf(time_str, sizeof(time_str), "<error>");
372 time_str[sizeof(time_str) - 1] = '\0';
374 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
375 sobj->interval))
376 snprintf(interval_str, sizeof(interval_str), "<error>");
377 interval_str[sizeof(interval_str) - 1] = '\0';
379 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
380 if (type == SDB_ATTRIBUTE) {
381 char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
382 sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
383 SDB_DOUBLE_QUOTED);
384 sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
385 "\"update_interval\": \"%s\"}", tmp, time_str,
386 interval_str);
387 }
388 else
389 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
390 "\"update_interval\": \"%s\"}", time_str, interval_str);
392 if (sdb_llist_iter_has_next(iter))
393 sdb_strbuf_append(buf, ",");
394 }
396 sdb_llist_iter_destroy(iter);
397 sdb_strbuf_append(buf, "]");
398 } /* store_obj_tojson */
400 /*
401 * public API
402 */
404 void
405 sdb_store_clear(void)
406 {
407 sdb_llist_destroy(host_list);
408 host_list = NULL;
409 } /* sdb_store_clear */
411 int
412 sdb_store_host(const char *name, sdb_time_t last_update)
413 {
414 int status;
416 if (! name)
417 return -1;
419 pthread_rwlock_wrlock(&obj_lock);
420 status = store_obj(/* hostname = */ NULL,
421 /* stored object = */ SDB_HOST, name, last_update,
422 /* updated_obj = */ NULL);
423 pthread_rwlock_unlock(&obj_lock);
424 return status;
425 } /* sdb_store_host */
427 _Bool
428 sdb_store_has_host(const char *name)
429 {
430 sdb_host_t *host;
432 if (! name)
433 return NULL;
435 host = lookup_host(name);
436 return host != NULL;
437 } /* sdb_store_has_host */
439 sdb_store_base_t *
440 sdb_store_get_host(const char *name)
441 {
442 sdb_host_t *host;
444 if (! name)
445 return NULL;
447 host = lookup_host(name);
448 if (! host)
449 return NULL;
451 sdb_object_ref(SDB_OBJ(host));
452 return STORE_BASE(host);
453 } /* sdb_store_get_host */
455 int
456 sdb_store_attribute(const char *hostname,
457 const char *key, const sdb_data_t *value,
458 sdb_time_t last_update)
459 {
460 int status;
462 sdb_store_base_t *updated_attr = NULL;
464 if ((! hostname) || (! key))
465 return -1;
467 pthread_rwlock_wrlock(&obj_lock);
468 status = store_obj(hostname,
469 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
470 &updated_attr);
472 if (status >= 0) {
473 assert(updated_attr);
474 sdb_data_free_datum(&ATTR(updated_attr)->value);
475 if (sdb_data_copy(&ATTR(updated_attr)->value, value)) {
476 sdb_object_deref(SDB_OBJ(updated_attr));
477 status = -1;
478 }
479 }
481 pthread_rwlock_unlock(&obj_lock);
482 return status;
483 } /* sdb_store_attribute */
485 int
486 sdb_store_service(const char *hostname, const char *name,
487 sdb_time_t last_update)
488 {
489 int status;
491 if ((! hostname) || (! name))
492 return -1;
494 pthread_rwlock_wrlock(&obj_lock);
495 status = store_obj(hostname,
496 /* stored object = */ SDB_SERVICE, name, last_update,
497 /* updated obj = */ NULL);
498 pthread_rwlock_unlock(&obj_lock);
499 return status;
500 } /* sdb_store_service */
502 int
503 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
504 {
505 sdb_host_t *host;
506 char time_str[64];
507 char interval_str[64];
509 if ((! h) || (h->type != SDB_HOST) || (! buf))
510 return -1;
512 host = HOST(h);
514 if (! sdb_strftime(time_str, sizeof(time_str),
515 "%F %T %z", host->_last_update))
516 snprintf(time_str, sizeof(time_str), "<error>");
517 time_str[sizeof(time_str) - 1] = '\0';
519 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
520 host->_interval))
521 snprintf(interval_str, sizeof(interval_str), "<error>");
522 interval_str[sizeof(interval_str) - 1] = '\0';
524 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
525 "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
526 SDB_OBJ(host)->name, time_str, interval_str);
528 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
529 sdb_strbuf_append(buf, ", \"attributes\": ");
530 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
531 }
533 if (! (flags & SDB_SKIP_SERVICES)) {
534 sdb_strbuf_append(buf, ", \"services\": ");
535 store_obj_tojson(host->services, SDB_SERVICE, buf);
536 }
538 sdb_strbuf_append(buf, "}");
539 return 0;
540 } /* sdb_store_host_tojson */
542 int
543 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
544 {
545 sdb_llist_iter_t *host_iter;
547 if (! buf)
548 return -1;
550 pthread_rwlock_rdlock(&obj_lock);
552 host_iter = sdb_llist_get_iter(host_list);
553 if (! host_iter) {
554 pthread_rwlock_unlock(&obj_lock);
555 return -1;
556 }
558 sdb_strbuf_append(buf, "{\"hosts\":[");
560 while (sdb_llist_iter_has_next(host_iter)) {
561 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
562 assert(host);
564 if (sdb_store_host_tojson(host, buf, flags))
565 return -1;
567 if (sdb_llist_iter_has_next(host_iter))
568 sdb_strbuf_append(buf, ",");
569 }
571 sdb_strbuf_append(buf, "]}");
573 sdb_llist_iter_destroy(host_iter);
574 pthread_rwlock_unlock(&obj_lock);
575 return 0;
576 } /* sdb_store_tojson */
578 /* TODO: actually support hierarchical data */
579 int
580 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
581 {
582 sdb_llist_iter_t *host_iter;
583 int status = 0;
585 pthread_rwlock_rdlock(&obj_lock);
587 host_iter = sdb_llist_get_iter(host_list);
588 if (! host_iter)
589 status = -1;
591 /* has_next returns false if the iterator is NULL */
592 while (sdb_llist_iter_has_next(host_iter)) {
593 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
594 assert(host);
596 if (cb(host, user_data)) {
597 status = -1;
598 break;
599 }
600 }
602 sdb_llist_iter_destroy(host_iter);
603 pthread_rwlock_unlock(&obj_lock);
604 return status;
605 } /* sdb_store_iterate */
607 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */