1 /*
2 * SysDB - src/core/store.c
3 * Copyright (C) 2012 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/error.h"
31 #include "core/plugin.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 store_obj;
59 typedef struct store_obj store_obj_t;
61 struct store_obj {
62 sdb_object_t super;
63 sdb_time_t last_update;
64 store_obj_t *parent;
65 };
66 #define STORE_OBJ(obj) ((store_obj_t *)(obj))
67 #define STORE_CONST_OBJ(obj) ((const store_obj_t *)(obj))
69 typedef struct {
70 store_obj_t super;
72 char *value;
73 } sdb_attribute_t;
74 #define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
75 #define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
77 typedef struct {
78 store_obj_t super;
80 int type;
81 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_obj_init(sdb_object_t *obj, va_list ap)
104 {
105 store_obj_t *sobj = STORE_OBJ(obj);
106 sobj->last_update = va_arg(ap, sdb_time_t);
108 sobj->parent = NULL;
109 return 0;
110 } /* store_obj_init */
112 static void
113 store_obj_destroy(sdb_object_t *obj)
114 {
115 const store_obj_t *sobj = STORE_OBJ(obj);
117 if (sobj->parent)
118 sdb_object_deref(SDB_OBJ(sobj->parent));
119 } /* store_obj_destroy */
121 /* this may not be used as a type on its own but only as helper functions for
122 * the derived types; the function accepts a variadic list of arguments to
123 * pass to the sub-type's init function */
124 static sdb_object_t *
125 store_obj_clone(const sdb_object_t *obj, ...)
126 {
127 const store_obj_t *sobj = STORE_CONST_OBJ(obj);
128 store_obj_t *new;
130 va_list ap;
132 va_start(ap, obj);
134 new = STORE_OBJ(sdb_object_vcreate(obj->name, obj->type, ap));
135 va_end(ap);
136 if (! new)
137 return NULL;
139 new->last_update = sobj->last_update;
140 sdb_object_ref(SDB_OBJ(sobj->parent));
141 new->parent = sobj->parent;
142 return SDB_OBJ(new);
143 } /* store_obj_clone */
145 static int
146 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
147 {
148 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
149 int ret;
151 ret = store_obj_init(obj, ap);
152 if (ret)
153 return ret;
155 sobj->type = va_arg(ap, int);
157 sobj->children = sdb_llist_create();
158 if (! sobj->children)
159 return -1;
160 sobj->attributes = sdb_llist_create();
161 if (! sobj->attributes)
162 return -1;
163 return 0;
164 } /* sdb_store_obj_init */
166 static void
167 sdb_store_obj_destroy(sdb_object_t *obj)
168 {
169 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
171 assert(obj);
173 store_obj_destroy(obj);
175 if (sobj->children)
176 sdb_llist_destroy(sobj->children);
177 if (sobj->attributes)
178 sdb_llist_destroy(sobj->attributes);
179 } /* sdb_store_obj_destroy */
181 static sdb_object_t *
182 sdb_store_obj_clone(const sdb_object_t *obj)
183 {
184 const sdb_store_obj_t *sobj = SDB_CONST_STORE_OBJ(obj);
185 sdb_store_obj_t *new;
187 new = SDB_STORE_OBJ(store_obj_clone(obj, sobj->_last_update, sobj->type));
188 if (! new)
189 return NULL;
191 if (sobj->children) {
192 sdb_llist_destroy(new->children);
193 new->children = sdb_llist_clone(sobj->children);
194 if (! new->children) {
195 sdb_object_deref(SDB_OBJ(new));
196 return NULL;
197 }
198 }
199 if (sobj->attributes) {
200 sdb_llist_destroy(new->attributes);
201 new->attributes = sdb_llist_clone(sobj->attributes);
202 if (! new->attributes) {
203 sdb_object_deref(SDB_OBJ(new));
204 return NULL;
205 }
206 }
208 return SDB_OBJ(new);
209 } /* sdb_store_obj_clone */
211 static int
212 sdb_attr_init(sdb_object_t *obj, va_list ap)
213 {
214 const char *value;
215 int ret;
217 ret = store_obj_init(obj, ap);
218 if (ret)
219 return ret;
220 value = va_arg(ap, const char *);
222 SDB_ATTR(obj)->value = strdup(value);
223 if (! SDB_ATTR(obj)->value)
224 return -1;
225 return 0;
226 } /* sdb_attr_init */
228 static void
229 sdb_attr_destroy(sdb_object_t *obj)
230 {
231 assert(obj);
233 store_obj_destroy(obj);
235 if (SDB_ATTR(obj)->value)
236 free(SDB_ATTR(obj)->value);
237 } /* sdb_attr_destroy */
239 static sdb_object_t *
240 sdb_attr_clone(const sdb_object_t *obj)
241 {
242 const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
243 sdb_attribute_t *new;
245 new = SDB_ATTR(store_obj_clone(obj, attr->_last_update, attr->value));
246 if (! new)
247 return NULL;
248 return SDB_OBJ(new);
249 } /* sdb_attr_clone */
251 static sdb_type_t sdb_store_obj_type = {
252 sizeof(sdb_store_obj_t),
254 sdb_store_obj_init,
255 sdb_store_obj_destroy,
256 sdb_store_obj_clone
257 };
259 static sdb_type_t sdb_attribute_type = {
260 sizeof(sdb_attribute_t),
262 sdb_attr_init,
263 sdb_attr_destroy,
264 sdb_attr_clone
265 };
267 /*
268 * private helper functions
269 */
271 static sdb_store_obj_t *
272 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
273 {
274 sdb_llist_iter_t *iter;
276 if (! l)
277 return NULL;
279 iter = sdb_llist_get_iter(l);
280 if (! iter)
281 return NULL;
283 while (sdb_llist_iter_has_next(iter)) {
284 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
285 assert(sobj);
287 if ((sobj->type == type)
288 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
289 sdb_llist_iter_destroy(iter);
290 return sobj;
291 }
293 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
294 if (sobj) {
295 sdb_llist_iter_destroy(iter);
296 return sobj;
297 }
298 }
299 sdb_llist_iter_destroy(iter);
300 return NULL;
301 } /* sdb_store_lookup_in_list */
303 static sdb_store_obj_t *
304 sdb_store_lookup(int type, const char *name)
305 {
306 return sdb_store_lookup_in_list(obj_list, type, name);
307 } /* sdb_store_lookup */
309 /* The obj_lock has to be acquired before calling this function. */
310 static int
311 store_obj(int parent_type, const char *parent_name,
312 int type, const char *name, sdb_time_t last_update,
313 store_obj_t **updated_obj)
314 {
315 sdb_llist_t *parent_list;
316 store_obj_t *old;
317 int status = 0;
319 if (! name)
320 return -1;
322 if (last_update <= 0)
323 last_update = sdb_gettime();
325 assert((parent_type == 0)
326 || (parent_type == SDB_HOST)
327 || (parent_type == SDB_SERVICE));
328 assert((type == 0)
329 || (type == SDB_HOST)
330 || (type == SDB_SERVICE)
331 || (type == SDB_ATTRIBUTE));
334 if (! obj_list) {
335 if (! (obj_list = sdb_llist_create()))
336 return -1;
337 }
338 parent_list = obj_list;
340 if (parent_type && parent_name) {
341 sdb_store_obj_t *parent;
343 parent = sdb_store_lookup(parent_type, parent_name);
344 if (! parent) {
345 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
346 "parent %s '%s' not found", TYPE_TO_NAME(type), name,
347 TYPE_TO_NAME(parent_type), parent_name);
348 return -1;
349 }
351 if (type == SDB_ATTRIBUTE)
352 parent_list = parent->attributes;
353 else
354 parent_list = parent->children;
355 }
357 /* TODO: only look into direct children? */
358 if (type == SDB_ATTRIBUTE)
359 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
360 else
361 old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
363 if (old) {
364 if (old->last_update > last_update) {
365 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
366 "value too old (%"PRIscTIME" < %"PRIscTIME")",
367 TYPE_TO_NAME(type), name, last_update, old->last_update);
368 /* don't report an error; the object may be updated by multiple
369 * backends */
370 status = 1;
371 }
372 else {
373 old->last_update = last_update;
374 }
376 if (updated_obj)
377 *updated_obj = old;
378 }
379 else {
380 store_obj_t *new;
382 if (type == SDB_ATTRIBUTE)
383 /* the value will be updated by the caller */
384 new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
385 last_update, NULL));
386 else
387 new = STORE_OBJ(sdb_object_create(name, sdb_store_obj_type,
388 last_update, type));
390 if (! new) {
391 char errbuf[1024];
392 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
393 TYPE_TO_NAME(type), name,
394 sdb_strerror(errno, errbuf, sizeof(errbuf)));
395 return -1;
396 }
398 /* TODO: insert type-aware; the current version works as long as we
399 * don't support to store hierarchical data */
400 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
401 sdb_object_cmp_by_name);
403 /* pass control to the list or destroy in case of an error */
404 sdb_object_deref(SDB_OBJ(new));
406 if (updated_obj)
407 *updated_obj = new;
408 }
409 return status;
410 } /* sdb_store_obj */
412 /*
413 * public API
414 */
416 int
417 sdb_store_host(const char *name, sdb_time_t last_update)
418 {
419 char *cname;
420 int status;
422 if (! name)
423 return -1;
425 cname = sdb_plugin_cname(strdup(name));
426 if (! cname) {
427 sdb_log(SDB_LOG_ERR, "store: strdup failed");
428 return -1;
429 }
431 pthread_rwlock_wrlock(&obj_lock);
432 status = store_obj(/* parent = */ 0, NULL,
433 /* stored object = */ SDB_HOST, cname, last_update,
434 /* updated_obj = */ NULL);
435 pthread_rwlock_unlock(&obj_lock);
436 free(cname);
437 return status;
438 } /* sdb_store_host */
440 _Bool
441 sdb_store_has_host(const char *name)
442 {
443 sdb_store_obj_t *host;
445 if (! name)
446 return NULL;
448 host = sdb_store_lookup(SDB_HOST, name);
449 return host != NULL;
450 } /* sdb_store_has_host */
452 int
453 sdb_store_attribute(const char *hostname, const char *key, const char *value,
454 sdb_time_t last_update)
455 {
456 char *cname;
457 int status;
459 store_obj_t *updated_attr = NULL;
461 if ((! hostname) || (! key))
462 return -1;
464 cname = sdb_plugin_cname(strdup(hostname));
465 if (! cname) {
466 sdb_log(SDB_LOG_ERR, "store: strdup failed");
467 return -1;
468 }
470 pthread_rwlock_wrlock(&obj_lock);
471 status = store_obj(/* parent = */ SDB_HOST, cname,
472 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
473 &updated_attr);
475 SDB_ATTR(updated_attr)->value = strdup(value);
476 if (! SDB_ATTR(updated_attr)->value) {
477 sdb_object_deref(SDB_OBJ(updated_attr));
478 status = -1;
479 }
480 pthread_rwlock_unlock(&obj_lock);
481 free(cname);
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 char *cname;
490 int status;
492 if ((! hostname) || (! name))
493 return -1;
495 cname = sdb_plugin_cname(strdup(hostname));
496 if (! cname) {
497 sdb_log(SDB_LOG_ERR, "store: strdup failed");
498 return -1;
499 }
501 pthread_rwlock_wrlock(&obj_lock);
502 status = store_obj(/* parent = */ SDB_HOST, cname,
503 /* stored object = */ SDB_SERVICE, name, last_update,
504 /* updated obj = */ NULL);
505 pthread_rwlock_unlock(&obj_lock);
506 free(cname);
507 return status;
508 } /* sdb_store_service */
510 /* TODO: actually support hierarchical data */
511 int
512 sdb_store_dump(FILE *fh)
513 {
514 sdb_llist_iter_t *host_iter;
516 if (! fh)
517 return -1;
519 pthread_rwlock_rdlock(&obj_lock);
521 host_iter = sdb_llist_get_iter(obj_list);
522 if (! host_iter) {
523 pthread_rwlock_unlock(&obj_lock);
524 return -1;
525 }
527 while (sdb_llist_iter_has_next(host_iter)) {
528 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
529 sdb_llist_iter_t *svc_iter;
530 sdb_llist_iter_t *attr_iter;
532 char time_str[64];
534 assert(host);
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 fprintf(fh, "Host '%s' (last updated: %s):\n",
542 SDB_OBJ(host)->name, time_str);
544 attr_iter = sdb_llist_get_iter(host->attributes);
545 if (! attr_iter) {
546 char errbuf[1024];
547 fprintf(fh, "Failed to retrieve attributes: %s\n",
548 sdb_strerror(errno, errbuf, sizeof(errbuf)));
549 continue;
550 }
552 while (sdb_llist_iter_has_next(attr_iter)) {
553 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
554 assert(attr);
556 if (! sdb_strftime(time_str, sizeof(time_str),
557 "%F %T %z", attr->_last_update))
558 snprintf(time_str, sizeof(time_str), "<error>");
559 time_str[sizeof(time_str) - 1] = '\0';
561 fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
562 SDB_OBJ(attr)->name, attr->value, time_str);
563 }
565 sdb_llist_iter_destroy(attr_iter);
567 svc_iter = sdb_llist_get_iter(host->children);
568 if (! svc_iter) {
569 char errbuf[1024];
570 fprintf(fh, "Failed to retrieve services: %s\n",
571 sdb_strerror(errno, errbuf, sizeof(errbuf)));
572 continue;
573 }
575 while (sdb_llist_iter_has_next(svc_iter)) {
576 sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
577 assert(svc);
579 if (! sdb_strftime(time_str, sizeof(time_str),
580 "%F %T %z", svc->_last_update))
581 snprintf(time_str, sizeof(time_str), "<error>");
582 time_str[sizeof(time_str) - 1] = '\0';
584 fprintf(fh, "\tService '%s' (last updated: %s)\n",
585 SDB_OBJ(svc)->name, time_str);
586 }
588 sdb_llist_iter_destroy(svc_iter);
589 }
591 sdb_llist_iter_destroy(host_iter);
592 pthread_rwlock_unlock(&obj_lock);
593 return 0;
594 } /* sdb_store_dump */
596 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */