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 #include "sysdb.h"
29 #include "core/store.h"
30 #include "core/plugin.h"
31 #include "utils/error.h"
32 #include "utils/llist.h"
34 #include <assert.h>
36 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
42 #include <pthread.h>
44 /*
45 * private variables
46 */
48 static sdb_llist_t *obj_list = NULL;
49 static pthread_rwlock_t obj_lock = PTHREAD_RWLOCK_INITIALIZER;
51 /*
52 * private types
53 */
55 static sdb_type_t sdb_store_obj_type;
56 static sdb_type_t sdb_attribute_type;
58 struct sdb_store_base {
59 sdb_object_t super;
61 /* object type */
62 int type;
64 /* common meta information */
65 sdb_time_t last_update;
66 sdb_store_base_t *parent;
67 };
68 #define STORE_BASE(obj) ((sdb_store_base_t *)(obj))
69 #define STORE_CONST_BASE(obj) ((const sdb_store_base_t *)(obj))
71 typedef struct {
72 sdb_store_base_t super;
74 sdb_data_t value;
75 } sdb_attribute_t;
76 #define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
77 #define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
79 typedef struct {
80 sdb_store_base_t super;
82 sdb_llist_t *children;
83 sdb_llist_t *attributes;
84 } sdb_store_obj_t;
85 #define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
86 #define SDB_CONST_STORE_OBJ(obj) ((const sdb_store_obj_t *)(obj))
88 enum {
89 SDB_HOST = 1,
90 SDB_SERVICE,
91 SDB_ATTRIBUTE,
92 };
93 #define TYPE_TO_NAME(t) \
94 (((t) == SDB_HOST) ? "host" \
95 : ((t) == SDB_SERVICE) ? "service" \
96 : ((t) == SDB_ATTRIBUTE) ? "attribute" : "unknown")
98 /* shortcuts for accessing the sdb_store_obj_t attributes
99 * of inheriting objects */
100 #define _last_update super.last_update
102 static int
103 store_base_init(sdb_object_t *obj, va_list ap)
104 {
105 sdb_store_base_t *sobj = STORE_BASE(obj);
107 sobj->type = va_arg(ap, int);
109 sobj->last_update = va_arg(ap, sdb_time_t);
110 sobj->parent = NULL;
111 return 0;
112 } /* store_base_init */
114 static void
115 store_base_destroy(sdb_object_t *obj)
116 {
117 const sdb_store_base_t *sobj = STORE_CONST_BASE(obj);
119 if (sobj->parent)
120 sdb_object_deref(SDB_OBJ(sobj->parent));
121 } /* store_base_destroy */
123 static int
124 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
125 {
126 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
127 int ret;
129 /* this will consume the first argument (type) of ap */
130 ret = store_base_init(obj, ap);
131 if (ret)
132 return ret;
134 sobj->children = sdb_llist_create();
135 if (! sobj->children)
136 return -1;
137 sobj->attributes = sdb_llist_create();
138 if (! sobj->attributes)
139 return -1;
140 return 0;
141 } /* sdb_store_obj_init */
143 static void
144 sdb_store_obj_destroy(sdb_object_t *obj)
145 {
146 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
148 assert(obj);
150 store_base_destroy(obj);
152 if (sobj->children)
153 sdb_llist_destroy(sobj->children);
154 if (sobj->attributes)
155 sdb_llist_destroy(sobj->attributes);
156 } /* sdb_store_obj_destroy */
158 static int
159 sdb_attr_init(sdb_object_t *obj, va_list ap)
160 {
161 const sdb_data_t *value;
162 int ret;
164 /* this will consume the first two arguments
165 * (type and last_update) of ap */
166 ret = store_base_init(obj, ap);
167 if (ret)
168 return ret;
169 value = va_arg(ap, const sdb_data_t *);
171 if (value)
172 if (sdb_data_copy(&SDB_ATTR(obj)->value, value))
173 return -1;
174 return 0;
175 } /* sdb_attr_init */
177 static void
178 sdb_attr_destroy(sdb_object_t *obj)
179 {
180 assert(obj);
182 store_base_destroy(obj);
183 sdb_data_free_datum(&SDB_ATTR(obj)->value);
184 } /* sdb_attr_destroy */
186 static sdb_type_t sdb_store_obj_type = {
187 sizeof(sdb_store_obj_t),
189 sdb_store_obj_init,
190 sdb_store_obj_destroy
191 };
193 static sdb_type_t sdb_attribute_type = {
194 sizeof(sdb_attribute_t),
196 sdb_attr_init,
197 sdb_attr_destroy
198 };
200 /*
201 * private helper functions
202 */
204 static sdb_store_obj_t *
205 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
206 {
207 sdb_llist_iter_t *iter;
209 if (! l)
210 return NULL;
212 iter = sdb_llist_get_iter(l);
213 if (! iter)
214 return NULL;
216 while (sdb_llist_iter_has_next(iter)) {
217 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
218 assert(sobj);
220 if ((STORE_BASE(sobj)->type == type)
221 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
222 sdb_llist_iter_destroy(iter);
223 return sobj;
224 }
226 /* don't lookups non-host types from hierarchical hosts */
227 if ((type != SDB_HOST) && (STORE_BASE(sobj)->type == SDB_HOST))
228 continue;
230 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
231 if (sobj) {
232 sdb_llist_iter_destroy(iter);
233 return sobj;
234 }
235 }
236 sdb_llist_iter_destroy(iter);
237 return NULL;
238 } /* sdb_store_lookup_in_list */
240 static sdb_store_obj_t *
241 sdb_store_lookup(int type, const char *name)
242 {
243 return sdb_store_lookup_in_list(obj_list, type, name);
244 } /* sdb_store_lookup */
246 /* The obj_lock has to be acquired before calling this function. */
247 static int
248 store_obj(int parent_type, const char *parent_name,
249 int type, const char *name, sdb_time_t last_update,
250 sdb_store_base_t **updated_obj)
251 {
252 char *parent_cname = NULL, *cname = NULL;
254 sdb_llist_t *parent_list;
255 sdb_store_base_t *old;
256 int status = 0;
258 if (last_update <= 0)
259 last_update = sdb_gettime();
261 assert((parent_type == 0)
262 || (parent_type == SDB_HOST)
263 || (parent_type == SDB_SERVICE));
264 assert((type == 0)
265 || (type == SDB_HOST)
266 || (type == SDB_SERVICE)
267 || (type == SDB_ATTRIBUTE));
269 if (parent_type == SDB_HOST) {
270 parent_cname = sdb_plugin_cname(strdup(parent_name));
271 if (! parent_cname) {
272 sdb_log(SDB_LOG_ERR, "store: strdup failed");
273 return -1;
274 }
275 parent_name = parent_cname;
276 }
277 if (type == SDB_HOST) {
278 cname = sdb_plugin_cname(strdup(name));
279 if (! cname) {
280 sdb_log(SDB_LOG_ERR, "store: strdup failed");
281 return -1;
282 }
283 name = cname;
284 }
286 if (! obj_list) {
287 if (! (obj_list = sdb_llist_create())) {
288 free(parent_cname);
289 free(cname);
290 return -1;
291 }
292 }
293 parent_list = obj_list;
295 if (parent_type && parent_name) {
296 sdb_store_obj_t *parent;
298 parent = sdb_store_lookup(parent_type, parent_name);
299 if (! parent) {
300 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
301 "parent %s '%s' not found", TYPE_TO_NAME(type), name,
302 TYPE_TO_NAME(parent_type), parent_name);
303 free(parent_cname);
304 free(cname);
305 return -1;
306 }
308 if (type == SDB_ATTRIBUTE)
309 parent_list = parent->attributes;
310 else
311 parent_list = parent->children;
312 }
314 if (type == SDB_HOST)
315 /* make sure that each host is unique */
316 old = STORE_BASE(sdb_store_lookup_in_list(obj_list, type, name));
317 else if (type == SDB_ATTRIBUTE)
318 /* look into attributes of this host */
319 old = STORE_BASE(sdb_llist_search_by_name(parent_list, name));
320 else
321 /* look into services assigned to this host (sdb_store_lookup_in_list
322 * does not look up services from hierarchical hosts) */
323 old = STORE_BASE(sdb_store_lookup_in_list(parent_list, type, name));
325 if (old) {
326 if (old->last_update > last_update) {
327 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
328 "value too old (%"PRIscTIME" < %"PRIscTIME")",
329 TYPE_TO_NAME(type), name, last_update, old->last_update);
330 /* don't report an error; the object may be updated by multiple
331 * backends */
332 status = 1;
333 }
334 else {
335 old->last_update = last_update;
336 }
338 if (updated_obj)
339 *updated_obj = old;
340 }
341 else {
342 sdb_store_base_t *new;
344 if (type == SDB_ATTRIBUTE)
345 /* the value will be updated by the caller */
346 new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
347 type, last_update, NULL));
348 else
349 new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
350 type, last_update));
352 if (! new) {
353 char errbuf[1024];
354 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
355 TYPE_TO_NAME(type), name,
356 sdb_strerror(errno, errbuf, sizeof(errbuf)));
357 free(parent_cname);
358 free(cname);
359 return -1;
360 }
362 /* TODO: insert type-aware; the current version works as long as we
363 * don't support to store hierarchical data */
364 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
365 sdb_object_cmp_by_name);
367 /* pass control to the list or destroy in case of an error */
368 sdb_object_deref(SDB_OBJ(new));
370 if (updated_obj)
371 *updated_obj = new;
372 }
373 free(parent_cname);
374 free(cname);
375 return status;
376 } /* store_obj */
378 /*
379 * store_obj_tojson serializes attribute / service objects to JSON.
380 *
381 * The function never returns an error. Rather, an error message will be part
382 * of the serialized data.
383 */
384 static void
385 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
386 {
387 sdb_llist_iter_t *iter;
388 char time_str[64];
390 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
392 sdb_strbuf_append(buf, "[");
394 iter = sdb_llist_get_iter(list);
395 if (! iter) {
396 char errbuf[1024];
397 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
398 TYPE_TO_NAME(type),
399 sdb_strerror(errno, errbuf, sizeof(errbuf)));
400 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
401 TYPE_TO_NAME(type), errbuf);
402 }
404 /* has_next returns false if the iterator is NULL */
405 while (sdb_llist_iter_has_next(iter)) {
406 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
407 assert(sobj);
409 if (! sdb_strftime(time_str, sizeof(time_str),
410 "%F %T %z", sobj->last_update))
411 snprintf(time_str, sizeof(time_str), "<error>");
412 time_str[sizeof(time_str) - 1] = '\0';
414 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
415 if (type == SDB_ATTRIBUTE)
416 /* XXX: this needs to be type-dependent */
417 sdb_strbuf_append(buf, "\"value\": \"%s\", ",
418 SDB_ATTR(sobj)->value.data.string);
419 sdb_strbuf_append(buf, "\"last_update\": \"%s\"}", time_str);
421 if (sdb_llist_iter_has_next(iter))
422 sdb_strbuf_append(buf, ",");
423 }
425 sdb_llist_iter_destroy(iter);
426 sdb_strbuf_append(buf, "]");
427 } /* store_obj_tojson */
429 /*
430 * public API
431 */
433 int
434 sdb_store_host(const char *name, sdb_time_t last_update)
435 {
436 int status;
438 if (! name)
439 return -1;
441 pthread_rwlock_wrlock(&obj_lock);
442 status = store_obj(/* parent = */ 0, NULL,
443 /* stored object = */ SDB_HOST, name, last_update,
444 /* updated_obj = */ NULL);
445 pthread_rwlock_unlock(&obj_lock);
446 return status;
447 } /* sdb_store_host */
449 _Bool
450 sdb_store_has_host(const char *name)
451 {
452 sdb_store_obj_t *host;
454 if (! name)
455 return NULL;
457 host = sdb_store_lookup(SDB_HOST, name);
458 return host != NULL;
459 } /* sdb_store_has_host */
461 sdb_store_base_t *
462 sdb_store_get_host(const char *name)
463 {
464 sdb_store_obj_t *host;
466 if (! name)
467 return NULL;
469 host = sdb_store_lookup(SDB_HOST, name);
470 if (! host)
471 return NULL;
473 sdb_object_ref(SDB_OBJ(host));
474 return STORE_BASE(host);
475 } /* sdb_store_get_host */
477 int
478 sdb_store_attribute(const char *hostname,
479 const char *key, const sdb_data_t *value,
480 sdb_time_t last_update)
481 {
482 int status;
484 sdb_store_base_t *updated_attr = NULL;
486 if ((! hostname) || (! key))
487 return -1;
489 pthread_rwlock_wrlock(&obj_lock);
490 status = store_obj(/* parent = */ SDB_HOST, hostname,
491 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
492 &updated_attr);
494 if (status >= 0) {
495 assert(updated_attr);
496 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
497 sdb_object_deref(SDB_OBJ(updated_attr));
498 status = -1;
499 }
500 }
502 pthread_rwlock_unlock(&obj_lock);
503 return status;
504 } /* sdb_store_attribute */
506 int
507 sdb_store_service(const char *hostname, const char *name,
508 sdb_time_t last_update)
509 {
510 int status;
512 if ((! hostname) || (! name))
513 return -1;
515 pthread_rwlock_wrlock(&obj_lock);
516 status = store_obj(/* parent = */ SDB_HOST, hostname,
517 /* stored object = */ SDB_SERVICE, name, last_update,
518 /* updated obj = */ NULL);
519 pthread_rwlock_unlock(&obj_lock);
520 return status;
521 } /* sdb_store_service */
523 int
524 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
525 {
526 sdb_store_obj_t *host;
527 char time_str[64];
529 if ((! h) || (h->type != SDB_HOST) || (! buf))
530 return -1;
532 host = SDB_STORE_OBJ(h);
534 if (! sdb_strftime(time_str, sizeof(time_str),
535 "%F %T %z", host->_last_update))
536 snprintf(time_str, sizeof(time_str), "<error>");
537 time_str[sizeof(time_str) - 1] = '\0';
539 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
540 "\"last_update\": \"%s\"",
541 SDB_OBJ(host)->name, time_str);
543 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
544 sdb_strbuf_append(buf, ", \"attributes\": ");
545 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
546 }
548 if (! (flags & SDB_SKIP_SERVICES)) {
549 sdb_strbuf_append(buf, ", \"services\": ");
550 store_obj_tojson(host->children, SDB_SERVICE, buf);
551 }
553 sdb_strbuf_append(buf, "}");
554 return 0;
555 } /* sdb_store_host_tojson */
557 /* TODO: actually support hierarchical data */
558 int
559 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
560 {
561 sdb_llist_iter_t *host_iter;
563 if (! buf)
564 return -1;
566 pthread_rwlock_rdlock(&obj_lock);
568 host_iter = sdb_llist_get_iter(obj_list);
569 if (! host_iter) {
570 pthread_rwlock_unlock(&obj_lock);
571 return -1;
572 }
574 sdb_strbuf_append(buf, "{\"hosts\":[");
576 while (sdb_llist_iter_has_next(host_iter)) {
577 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
578 assert(host);
580 if (sdb_store_host_tojson(host, buf, flags))
581 return -1;
583 if (sdb_llist_iter_has_next(host_iter))
584 sdb_strbuf_append(buf, ",");
585 }
587 sdb_strbuf_append(buf, "]}");
589 sdb_llist_iter_destroy(host_iter);
590 pthread_rwlock_unlock(&obj_lock);
591 return 0;
592 } /* sdb_store_tojson */
594 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */