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 lookup_host(const char *name)
167 {
168 return SDB_STORE_OBJ(sdb_llist_search_by_name(host_list, name));
169 } /* lookup_host */
171 /* The obj_lock has to be acquired before calling this function. */
172 static int
173 store_obj(const char *hostname, int type, const char *name,
174 sdb_time_t last_update, sdb_store_base_t **updated_obj)
175 {
176 char *host_cname = NULL, *cname = NULL;
178 sdb_llist_t *parent_list;
179 sdb_store_base_t *old;
180 int status = 0;
182 if (last_update <= 0)
183 last_update = sdb_gettime();
185 assert((type == 0)
186 || (type == SDB_HOST)
187 || (type == SDB_SERVICE)
188 || (type == SDB_ATTRIBUTE));
190 assert(hostname || (type == SDB_HOST));
191 assert((! hostname)
192 || (type == SDB_SERVICE)
193 || (type == SDB_ATTRIBUTE));
195 if (! host_list)
196 if (! (host_list = sdb_llist_create()))
197 return -1;
198 parent_list = host_list;
200 if (type == SDB_HOST) {
201 cname = sdb_plugin_cname(strdup(name));
202 if (! cname) {
203 sdb_log(SDB_LOG_ERR, "store: strdup failed");
204 return -1;
205 }
206 name = cname;
207 }
209 if (hostname) {
210 sdb_store_obj_t *host;
212 host_cname = sdb_plugin_cname(strdup(hostname));
213 if (! host_cname) {
214 sdb_log(SDB_LOG_ERR, "store: strdup failed");
215 free(cname);
216 return -1;
217 }
218 hostname = host_cname;
220 host = lookup_host(hostname);
221 if (! host) {
222 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
223 "host '%s' not found", SDB_STORE_TYPE_TO_NAME(type),
224 name, hostname);
225 free(host_cname);
226 free(cname);
227 return -1;
228 }
230 if (type == SDB_ATTRIBUTE)
231 parent_list = host->attributes;
232 else
233 parent_list = host->services;
234 }
236 if (type == SDB_HOST)
237 old = STORE_BASE(sdb_llist_search_by_name(host_list, name));
238 else
239 old = STORE_BASE(sdb_llist_search_by_name(parent_list, name));
241 if (old) {
242 if (old->last_update > last_update) {
243 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
244 "value too old (%"PRIscTIME" < %"PRIscTIME")",
245 SDB_STORE_TYPE_TO_NAME(type), name,
246 last_update, old->last_update);
247 /* don't report an error; the object may be updated by multiple
248 * backends */
249 status = 1;
250 }
251 else {
252 sdb_time_t interval = last_update - old->last_update;
253 old->last_update = last_update;
254 if (interval) {
255 if (old->interval)
256 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
257 + (0.1 * (double)interval));
258 else
259 old->interval = interval;
260 }
261 }
263 if (updated_obj)
264 *updated_obj = old;
265 }
266 else {
267 sdb_store_base_t *new;
269 if (type == SDB_ATTRIBUTE)
270 /* the value will be updated by the caller */
271 new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
272 type, last_update, NULL));
273 else
274 new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
275 type, last_update));
277 if (! new) {
278 char errbuf[1024];
279 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
280 SDB_STORE_TYPE_TO_NAME(type), name,
281 sdb_strerror(errno, errbuf, sizeof(errbuf)));
282 free(host_cname);
283 free(cname);
284 return -1;
285 }
287 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
288 sdb_object_cmp_by_name);
290 /* pass control to the list or destroy in case of an error */
291 sdb_object_deref(SDB_OBJ(new));
293 if (updated_obj)
294 *updated_obj = new;
295 }
296 free(host_cname);
297 free(cname);
298 return status;
299 } /* store_obj */
301 /*
302 * store_obj_tojson serializes attribute / service objects to JSON.
303 *
304 * The function never returns an error. Rather, an error message will be part
305 * of the serialized data.
306 */
307 static void
308 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
309 {
310 sdb_llist_iter_t *iter;
311 char time_str[64];
312 char interval_str[64];
314 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
316 sdb_strbuf_append(buf, "[");
318 iter = sdb_llist_get_iter(list);
319 if (! iter) {
320 char errbuf[1024];
321 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
322 SDB_STORE_TYPE_TO_NAME(type),
323 sdb_strerror(errno, errbuf, sizeof(errbuf)));
324 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
325 SDB_STORE_TYPE_TO_NAME(type), errbuf);
326 }
328 /* has_next returns false if the iterator is NULL */
329 while (sdb_llist_iter_has_next(iter)) {
330 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
331 assert(sobj);
333 if (! sdb_strftime(time_str, sizeof(time_str),
334 "%F %T %z", sobj->last_update))
335 snprintf(time_str, sizeof(time_str), "<error>");
336 time_str[sizeof(time_str) - 1] = '\0';
338 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
339 sobj->interval))
340 snprintf(interval_str, sizeof(interval_str), "<error>");
341 interval_str[sizeof(interval_str) - 1] = '\0';
343 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
344 if (type == SDB_ATTRIBUTE) {
345 char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
346 sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
347 SDB_DOUBLE_QUOTED);
348 sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\", "
349 "\"update_interval\": \"%s\"}", tmp, time_str,
350 interval_str);
351 }
352 else
353 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
354 "\"update_interval\": \"%s\"}", time_str, interval_str);
356 if (sdb_llist_iter_has_next(iter))
357 sdb_strbuf_append(buf, ",");
358 }
360 sdb_llist_iter_destroy(iter);
361 sdb_strbuf_append(buf, "]");
362 } /* store_obj_tojson */
364 /*
365 * public API
366 */
368 void
369 sdb_store_clear(void)
370 {
371 sdb_llist_destroy(host_list);
372 host_list = NULL;
373 } /* sdb_store_clear */
375 int
376 sdb_store_host(const char *name, sdb_time_t last_update)
377 {
378 int status;
380 if (! name)
381 return -1;
383 pthread_rwlock_wrlock(&obj_lock);
384 status = store_obj(/* hostname = */ NULL,
385 /* stored object = */ SDB_HOST, name, last_update,
386 /* updated_obj = */ NULL);
387 pthread_rwlock_unlock(&obj_lock);
388 return status;
389 } /* sdb_store_host */
391 _Bool
392 sdb_store_has_host(const char *name)
393 {
394 sdb_store_obj_t *host;
396 if (! name)
397 return NULL;
399 host = lookup_host(name);
400 return host != NULL;
401 } /* sdb_store_has_host */
403 sdb_store_base_t *
404 sdb_store_get_host(const char *name)
405 {
406 sdb_store_obj_t *host;
408 if (! name)
409 return NULL;
411 host = lookup_host(name);
412 if (! host)
413 return NULL;
415 sdb_object_ref(SDB_OBJ(host));
416 return STORE_BASE(host);
417 } /* sdb_store_get_host */
419 int
420 sdb_store_attribute(const char *hostname,
421 const char *key, const sdb_data_t *value,
422 sdb_time_t last_update)
423 {
424 int status;
426 sdb_store_base_t *updated_attr = NULL;
428 if ((! hostname) || (! key))
429 return -1;
431 pthread_rwlock_wrlock(&obj_lock);
432 status = store_obj(hostname,
433 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
434 &updated_attr);
436 if (status >= 0) {
437 assert(updated_attr);
438 sdb_data_free_datum(&SDB_ATTR(updated_attr)->value);
439 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
440 sdb_object_deref(SDB_OBJ(updated_attr));
441 status = -1;
442 }
443 }
445 pthread_rwlock_unlock(&obj_lock);
446 return status;
447 } /* sdb_store_attribute */
449 int
450 sdb_store_service(const char *hostname, const char *name,
451 sdb_time_t last_update)
452 {
453 int status;
455 if ((! hostname) || (! name))
456 return -1;
458 pthread_rwlock_wrlock(&obj_lock);
459 status = store_obj(hostname,
460 /* stored object = */ SDB_SERVICE, name, last_update,
461 /* updated obj = */ NULL);
462 pthread_rwlock_unlock(&obj_lock);
463 return status;
464 } /* sdb_store_service */
466 int
467 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
468 {
469 sdb_store_obj_t *host;
470 char time_str[64];
471 char interval_str[64];
473 if ((! h) || (h->type != SDB_HOST) || (! buf))
474 return -1;
476 host = SDB_STORE_OBJ(h);
478 if (! sdb_strftime(time_str, sizeof(time_str),
479 "%F %T %z", host->_last_update))
480 snprintf(time_str, sizeof(time_str), "<error>");
481 time_str[sizeof(time_str) - 1] = '\0';
483 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
484 host->_interval))
485 snprintf(interval_str, sizeof(interval_str), "<error>");
486 interval_str[sizeof(interval_str) - 1] = '\0';
488 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
489 "\"last_update\": \"%s\", \"update_interval\": \"%s\"",
490 SDB_OBJ(host)->name, time_str, interval_str);
492 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
493 sdb_strbuf_append(buf, ", \"attributes\": ");
494 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
495 }
497 if (! (flags & SDB_SKIP_SERVICES)) {
498 sdb_strbuf_append(buf, ", \"services\": ");
499 store_obj_tojson(host->services, SDB_SERVICE, buf);
500 }
502 sdb_strbuf_append(buf, "}");
503 return 0;
504 } /* sdb_store_host_tojson */
506 int
507 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
508 {
509 sdb_llist_iter_t *host_iter;
511 if (! buf)
512 return -1;
514 pthread_rwlock_rdlock(&obj_lock);
516 host_iter = sdb_llist_get_iter(host_list);
517 if (! host_iter) {
518 pthread_rwlock_unlock(&obj_lock);
519 return -1;
520 }
522 sdb_strbuf_append(buf, "{\"hosts\":[");
524 while (sdb_llist_iter_has_next(host_iter)) {
525 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
526 assert(host);
528 if (sdb_store_host_tojson(host, buf, flags))
529 return -1;
531 if (sdb_llist_iter_has_next(host_iter))
532 sdb_strbuf_append(buf, ",");
533 }
535 sdb_strbuf_append(buf, "]}");
537 sdb_llist_iter_destroy(host_iter);
538 pthread_rwlock_unlock(&obj_lock);
539 return 0;
540 } /* sdb_store_tojson */
542 /* TODO: actually support hierarchical data */
543 int
544 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
545 {
546 sdb_llist_iter_t *host_iter;
547 int status = 0;
549 pthread_rwlock_rdlock(&obj_lock);
551 host_iter = sdb_llist_get_iter(host_list);
552 if (! host_iter)
553 status = -1;
555 /* has_next returns false if the iterator is NULL */
556 while (sdb_llist_iter_has_next(host_iter)) {
557 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
558 assert(host);
560 if (cb(host, user_data)) {
561 status = -1;
562 break;
563 }
564 }
566 sdb_llist_iter_destroy(host_iter);
567 pthread_rwlock_unlock(&obj_lock);
568 return status;
569 } /* sdb_store_iterate */
571 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */