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->parent = NULL;
73 return 0;
74 } /* store_obj_init */
76 static void
77 store_obj_destroy(sdb_object_t *obj)
78 {
79 const sdb_store_obj_t *sobj = STORE_CONST_OBJ(obj);
81 if (sobj->parent)
82 sdb_object_deref(SDB_OBJ(sobj->parent));
83 } /* store_obj_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_obj_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_obj_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_obj_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_obj_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_obj_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_obj_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 host_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_obj_t **updated_obj)
208 {
209 char *host_cname = NULL, *cname = NULL;
211 sdb_llist_t *parent_list;
212 sdb_store_obj_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_OBJ(sdb_llist_search_by_name(host_list, name));
271 else
272 old = STORE_OBJ(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_obj_t *new;
302 if (type == SDB_ATTRIBUTE) {
303 /* the value will be updated by the caller */
304 new = STORE_OBJ(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_OBJ(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_common_tojson serializes common object attributes 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_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
345 {
346 char time_str[64];
347 char interval_str[64];
349 if (! sdb_strftime(time_str, sizeof(time_str),
350 "%F %T %z", obj->last_update))
351 snprintf(time_str, sizeof(time_str), "<error>");
352 time_str[sizeof(time_str) - 1] = '\0';
354 if (! sdb_strfinterval(interval_str, sizeof(interval_str),
355 obj->interval))
356 snprintf(interval_str, sizeof(interval_str), "<error>");
357 interval_str[sizeof(interval_str) - 1] = '\0';
359 sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
360 "\"update_interval\": \"%s\"", time_str, interval_str);
361 } /* store_common_tojson */
363 /*
364 * store_obj_tojson serializes attribute / service objects 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_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
371 {
372 sdb_llist_iter_t *iter;
374 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
376 sdb_strbuf_append(buf, "[");
377 iter = sdb_llist_get_iter(list);
378 if (! iter) {
379 char errbuf[1024];
380 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
381 SDB_STORE_TYPE_TO_NAME(type),
382 sdb_strerror(errno, errbuf, sizeof(errbuf)));
383 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
384 SDB_STORE_TYPE_TO_NAME(type), errbuf);
385 }
387 /* has_next returns false if the iterator is NULL */
388 while (sdb_llist_iter_has_next(iter)) {
389 sdb_store_obj_t *sobj = STORE_OBJ(sdb_llist_iter_get_next(iter));
390 assert(sobj);
391 assert(sobj->type == type);
393 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
394 if (sobj->type == SDB_ATTRIBUTE) {
395 char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
396 sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
397 SDB_DOUBLE_QUOTED);
398 sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
399 }
401 store_common_tojson(sobj, buf);
402 sdb_strbuf_append(buf, "}");
404 if (sdb_llist_iter_has_next(iter))
405 sdb_strbuf_append(buf, ",");
406 }
408 sdb_llist_iter_destroy(iter);
409 sdb_strbuf_append(buf, "]");
410 } /* store_obj_tojson */
412 /*
413 * public API
414 */
416 void
417 sdb_store_clear(void)
418 {
419 sdb_llist_destroy(host_list);
420 host_list = NULL;
421 } /* sdb_store_clear */
423 int
424 sdb_store_host(const char *name, sdb_time_t last_update)
425 {
426 int status;
428 if (! name)
429 return -1;
431 pthread_rwlock_wrlock(&host_lock);
432 status = store_obj(/* hostname = */ NULL,
433 /* stored object = */ SDB_HOST, name, last_update,
434 /* updated_obj = */ NULL);
435 pthread_rwlock_unlock(&host_lock);
436 return status;
437 } /* sdb_store_host */
439 _Bool
440 sdb_store_has_host(const char *name)
441 {
442 sdb_host_t *host;
444 if (! name)
445 return NULL;
447 host = lookup_host(name);
448 return host != NULL;
449 } /* sdb_store_has_host */
451 sdb_store_obj_t *
452 sdb_store_get_host(const char *name)
453 {
454 sdb_host_t *host;
456 if (! name)
457 return NULL;
459 host = lookup_host(name);
460 if (! host)
461 return NULL;
463 sdb_object_ref(SDB_OBJ(host));
464 return STORE_OBJ(host);
465 } /* sdb_store_get_host */
467 int
468 sdb_store_attribute(const char *hostname,
469 const char *key, const sdb_data_t *value,
470 sdb_time_t last_update)
471 {
472 int status;
474 sdb_store_obj_t *updated_attr = NULL;
476 if ((! hostname) || (! key))
477 return -1;
479 pthread_rwlock_wrlock(&host_lock);
480 status = store_obj(hostname,
481 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
482 &updated_attr);
484 if (status >= 0) {
485 assert(updated_attr);
486 sdb_data_free_datum(&ATTR(updated_attr)->value);
487 if (sdb_data_copy(&ATTR(updated_attr)->value, value)) {
488 sdb_object_deref(SDB_OBJ(updated_attr));
489 status = -1;
490 }
491 }
493 pthread_rwlock_unlock(&host_lock);
494 return status;
495 } /* sdb_store_attribute */
497 int
498 sdb_store_service(const char *hostname, const char *name,
499 sdb_time_t last_update)
500 {
501 int status;
503 if ((! hostname) || (! name))
504 return -1;
506 pthread_rwlock_wrlock(&host_lock);
507 status = store_obj(hostname,
508 /* stored object = */ SDB_SERVICE, name, last_update,
509 /* updated obj = */ NULL);
510 pthread_rwlock_unlock(&host_lock);
511 return status;
512 } /* sdb_store_service */
514 int
515 sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, int flags)
516 {
517 sdb_host_t *host;
519 if ((! h) || (h->type != SDB_HOST) || (! buf))
520 return -1;
522 host = HOST(h);
524 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(host)->name);
525 store_common_tojson(h, buf);
527 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
528 sdb_strbuf_append(buf, ", \"attributes\": ");
529 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
530 }
532 if (! (flags & SDB_SKIP_SERVICES)) {
533 sdb_strbuf_append(buf, ", \"services\": ");
534 store_obj_tojson(host->services, SDB_SERVICE, buf);
535 }
537 sdb_strbuf_append(buf, "}");
538 return 0;
539 } /* sdb_store_host_tojson */
541 int
542 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
543 {
544 sdb_llist_iter_t *host_iter;
546 if (! buf)
547 return -1;
549 pthread_rwlock_rdlock(&host_lock);
551 host_iter = sdb_llist_get_iter(host_list);
552 if (! host_iter) {
553 pthread_rwlock_unlock(&host_lock);
554 return -1;
555 }
557 sdb_strbuf_append(buf, "{\"hosts\":[");
559 while (sdb_llist_iter_has_next(host_iter)) {
560 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
561 assert(host);
563 if (sdb_store_host_tojson(host, buf, flags))
564 return -1;
566 if (sdb_llist_iter_has_next(host_iter))
567 sdb_strbuf_append(buf, ",");
568 }
570 sdb_strbuf_append(buf, "]}");
572 sdb_llist_iter_destroy(host_iter);
573 pthread_rwlock_unlock(&host_lock);
574 return 0;
575 } /* sdb_store_tojson */
577 /* TODO: actually support hierarchical data */
578 int
579 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
580 {
581 sdb_llist_iter_t *host_iter;
582 int status = 0;
584 pthread_rwlock_rdlock(&host_lock);
586 host_iter = sdb_llist_get_iter(host_list);
587 if (! host_iter)
588 status = -1;
590 /* has_next returns false if the iterator is NULL */
591 while (sdb_llist_iter_has_next(host_iter)) {
592 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
593 assert(host);
595 if (cb(host, user_data)) {
596 status = -1;
597 break;
598 }
599 }
601 sdb_llist_iter_destroy(host_iter);
602 pthread_rwlock_unlock(&host_lock);
603 return status;
604 } /* sdb_store_iterate */
606 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */