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 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 = 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 } /* store_lookup_in_list */
240 static sdb_store_obj_t *
241 store_lookup(int type, const char *name)
242 {
243 return store_lookup_in_list(obj_list, type, name);
244 } /* 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 = 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(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 (store_lookup_in_list
322 * does not look up services from hierarchical hosts) */
323 old = STORE_BASE(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 sdb_strbuf_append(buf, "\"value\": ");
417 sdb_data_format(&SDB_ATTR(sobj)->value, buf);
418 sdb_strbuf_append(buf, ", \"last_update\": \"%s\"}", time_str);
419 }
420 else
421 sdb_strbuf_append(buf, "\"last_update\": \"%s\"}", time_str);
423 if (sdb_llist_iter_has_next(iter))
424 sdb_strbuf_append(buf, ",");
425 }
427 sdb_llist_iter_destroy(iter);
428 sdb_strbuf_append(buf, "]");
429 } /* store_obj_tojson */
431 /*
432 * public API
433 */
435 int
436 sdb_store_host(const char *name, sdb_time_t last_update)
437 {
438 int status;
440 if (! name)
441 return -1;
443 pthread_rwlock_wrlock(&obj_lock);
444 status = store_obj(/* parent = */ 0, NULL,
445 /* stored object = */ SDB_HOST, name, last_update,
446 /* updated_obj = */ NULL);
447 pthread_rwlock_unlock(&obj_lock);
448 return status;
449 } /* sdb_store_host */
451 _Bool
452 sdb_store_has_host(const char *name)
453 {
454 sdb_store_obj_t *host;
456 if (! name)
457 return NULL;
459 host = store_lookup(SDB_HOST, name);
460 return host != NULL;
461 } /* sdb_store_has_host */
463 sdb_store_base_t *
464 sdb_store_get_host(const char *name)
465 {
466 sdb_store_obj_t *host;
468 if (! name)
469 return NULL;
471 host = store_lookup(SDB_HOST, name);
472 if (! host)
473 return NULL;
475 sdb_object_ref(SDB_OBJ(host));
476 return STORE_BASE(host);
477 } /* sdb_store_get_host */
479 int
480 sdb_store_attribute(const char *hostname,
481 const char *key, const sdb_data_t *value,
482 sdb_time_t last_update)
483 {
484 int status;
486 sdb_store_base_t *updated_attr = NULL;
488 if ((! hostname) || (! key))
489 return -1;
491 pthread_rwlock_wrlock(&obj_lock);
492 status = store_obj(/* parent = */ SDB_HOST, hostname,
493 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
494 &updated_attr);
496 if (status >= 0) {
497 assert(updated_attr);
498 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
499 sdb_object_deref(SDB_OBJ(updated_attr));
500 status = -1;
501 }
502 }
504 pthread_rwlock_unlock(&obj_lock);
505 return status;
506 } /* sdb_store_attribute */
508 int
509 sdb_store_service(const char *hostname, const char *name,
510 sdb_time_t last_update)
511 {
512 int status;
514 if ((! hostname) || (! name))
515 return -1;
517 pthread_rwlock_wrlock(&obj_lock);
518 status = store_obj(/* parent = */ SDB_HOST, hostname,
519 /* stored object = */ SDB_SERVICE, name, last_update,
520 /* updated obj = */ NULL);
521 pthread_rwlock_unlock(&obj_lock);
522 return status;
523 } /* sdb_store_service */
525 int
526 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
527 {
528 sdb_store_obj_t *host;
529 char time_str[64];
531 if ((! h) || (h->type != SDB_HOST) || (! buf))
532 return -1;
534 host = SDB_STORE_OBJ(h);
536 if (! sdb_strftime(time_str, sizeof(time_str),
537 "%F %T %z", host->_last_update))
538 snprintf(time_str, sizeof(time_str), "<error>");
539 time_str[sizeof(time_str) - 1] = '\0';
541 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
542 "\"last_update\": \"%s\"",
543 SDB_OBJ(host)->name, time_str);
545 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
546 sdb_strbuf_append(buf, ", \"attributes\": ");
547 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
548 }
550 if (! (flags & SDB_SKIP_SERVICES)) {
551 sdb_strbuf_append(buf, ", \"services\": ");
552 store_obj_tojson(host->children, SDB_SERVICE, buf);
553 }
555 sdb_strbuf_append(buf, "}");
556 return 0;
557 } /* sdb_store_host_tojson */
559 /* TODO: actually support hierarchical data */
560 int
561 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
562 {
563 sdb_llist_iter_t *host_iter;
565 if (! buf)
566 return -1;
568 pthread_rwlock_rdlock(&obj_lock);
570 host_iter = sdb_llist_get_iter(obj_list);
571 if (! host_iter) {
572 pthread_rwlock_unlock(&obj_lock);
573 return -1;
574 }
576 sdb_strbuf_append(buf, "{\"hosts\":[");
578 while (sdb_llist_iter_has_next(host_iter)) {
579 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
580 assert(host);
582 if (sdb_store_host_tojson(host, buf, flags))
583 return -1;
585 if (sdb_llist_iter_has_next(host_iter))
586 sdb_strbuf_append(buf, ",");
587 }
589 sdb_strbuf_append(buf, "]}");
591 sdb_llist_iter_destroy(host_iter);
592 pthread_rwlock_unlock(&obj_lock);
593 return 0;
594 } /* sdb_store_tojson */
596 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */