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 };
92 #define TYPE_TO_NAME(t) \
93 (((t) == SDB_HOST) ? "host" \
94 : ((t) == SDB_SERVICE) ? "service" : "unknown")
96 /* shortcuts for accessing the sdb_store_obj_t attributes
97 * of inheriting objects */
98 #define _last_update super.last_update
100 static int
101 store_obj_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
102 {
103 store_obj_t *sobj = STORE_OBJ(obj);
104 sobj->last_update = sdb_gettime();
105 /* ignore errors -> last_update will be updated later */
107 sobj->parent = NULL;
108 return 0;
109 } /* store_obj_init */
111 static void
112 store_obj_destroy(sdb_object_t *obj)
113 {
114 const store_obj_t *sobj = STORE_OBJ(obj);
116 if (sobj->parent)
117 sdb_object_deref(SDB_OBJ(sobj->parent));
118 } /* store_obj_destroy */
120 static sdb_object_t *
121 store_obj_clone(const sdb_object_t *obj)
122 {
123 const store_obj_t *sobj = STORE_CONST_OBJ(obj);
124 store_obj_t *new;
126 new = STORE_OBJ(sdb_object_create(obj->name, obj->type));
127 if (! new)
128 return NULL;
130 new->last_update = sobj->last_update;
131 sdb_object_ref(SDB_OBJ(sobj->parent));
132 new->parent = sobj->parent;
133 return SDB_OBJ(new);
134 } /* store_obj_clone */
136 static int
137 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
138 {
139 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
140 int ret;
142 ret = store_obj_init(obj, ap);
143 if (ret)
144 return ret;
146 sobj->type = va_arg(ap, int);
148 sobj->children = sdb_llist_create();
149 if (! sobj->children)
150 return -1;
151 sobj->attributes = sdb_llist_create();
152 if (! sobj->attributes)
153 return -1;
154 return 0;
155 } /* sdb_store_obj_init */
157 static void
158 sdb_store_obj_destroy(sdb_object_t *obj)
159 {
160 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
162 assert(obj);
164 store_obj_destroy(obj);
166 if (sobj->children)
167 sdb_llist_destroy(sobj->children);
168 if (sobj->attributes)
169 sdb_llist_destroy(sobj->attributes);
170 } /* sdb_store_obj_destroy */
172 static sdb_object_t *
173 sdb_store_obj_clone(const sdb_object_t *obj)
174 {
175 const sdb_store_obj_t *sobj = SDB_CONST_STORE_OBJ(obj);
176 sdb_store_obj_t *new;
178 new = SDB_STORE_OBJ(store_obj_clone(obj));
179 if (! new)
180 return NULL;
182 new->type = sobj->type;
184 /* make sure these are initialized; else sdb_object_deref() might access
185 * arbitrary memory in case of an error */
186 new->children = new->attributes = NULL;
188 if (sobj->children) {
189 new->children = sdb_llist_clone(sobj->children);
190 if (! new->children) {
191 sdb_object_deref(SDB_OBJ(new));
192 return NULL;
193 }
194 }
195 if (sobj->attributes) {
196 new->attributes = sdb_llist_clone(sobj->attributes);
197 if (! new->attributes) {
198 sdb_object_deref(SDB_OBJ(new));
199 return NULL;
200 }
201 }
203 new->_last_update = sobj->_last_update;
204 return SDB_OBJ(new);
205 } /* sdb_store_obj_clone */
207 static int
208 sdb_attr_init(sdb_object_t *obj, va_list ap)
209 {
210 const char *value = va_arg(ap, const char *);
211 int ret;
213 ret = store_obj_init(obj, ap);
214 if (ret)
215 return ret;
217 SDB_ATTR(obj)->value = strdup(value);
218 if (! SDB_ATTR(obj)->value)
219 return -1;
220 return 0;
221 } /* sdb_attr_init */
223 static void
224 sdb_attr_destroy(sdb_object_t *obj)
225 {
226 assert(obj);
228 store_obj_destroy(obj);
230 if (SDB_ATTR(obj)->value)
231 free(SDB_ATTR(obj)->value);
232 } /* sdb_attr_destroy */
234 static sdb_object_t *
235 sdb_attr_clone(const sdb_object_t *obj)
236 {
237 const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
238 sdb_attribute_t *new;
240 new = SDB_ATTR(store_obj_clone(obj));
241 if (! new)
242 return NULL;
244 if (attr->value)
245 new->value = strdup(attr->value);
246 if (! new->value) {
247 sdb_object_deref(SDB_OBJ(new));
248 return NULL;
249 }
250 return SDB_OBJ(new);
251 } /* sdb_attr_clone */
253 static sdb_type_t sdb_store_obj_type = {
254 sizeof(sdb_store_obj_t),
256 sdb_store_obj_init,
257 sdb_store_obj_destroy,
258 sdb_store_obj_clone
259 };
261 static sdb_type_t sdb_attribute_type = {
262 sizeof(sdb_attribute_t),
264 sdb_attr_init,
265 sdb_attr_destroy,
266 sdb_attr_clone
267 };
269 /*
270 * private helper functions
271 */
273 static sdb_store_obj_t *
274 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
275 {
276 sdb_llist_iter_t *iter;
278 if (! l)
279 return NULL;
281 iter = sdb_llist_get_iter(l);
282 if (! iter)
283 return NULL;
285 while (sdb_llist_iter_has_next(iter)) {
286 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
287 assert(sobj);
289 if ((sobj->type == type)
290 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
291 sdb_llist_iter_destroy(iter);
292 return sobj;
293 }
295 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
296 if (sobj) {
297 sdb_llist_iter_destroy(iter);
298 return sobj;
299 }
300 }
301 sdb_llist_iter_destroy(iter);
302 return NULL;
303 } /* sdb_store_lookup_in_list */
305 static sdb_store_obj_t *
306 sdb_store_lookup(int type, const char *name)
307 {
308 return sdb_store_lookup_in_list(obj_list, type, name);
309 } /* sdb_store_lookup */
311 static int
312 store_obj(int parent_type, const char *parent_name,
313 int type, const char *name, sdb_time_t last_update)
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) || (type = SDB_HOST) || (type == SDB_SERVICE));
330 pthread_rwlock_wrlock(&obj_lock);
332 if (! obj_list) {
333 if (! (obj_list = sdb_llist_create())) {
334 pthread_rwlock_unlock(&obj_lock);
335 return -1;
336 }
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 pthread_rwlock_unlock(&obj_lock);
349 return -1;
350 }
352 parent_list = parent->children;
353 }
355 /* TODO: only look into direct children? */
356 old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
358 if (old) {
359 if (old->last_update > last_update) {
360 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
361 "value too old (%"PRIscTIME" < %"PRIscTIME")",
362 TYPE_TO_NAME(type), name, last_update, old->last_update);
363 /* don't report an error; the object may be updated by multiple
364 * backends */
365 status = 1;
366 }
367 else {
368 old->last_update = last_update;
369 }
370 }
371 else {
372 store_obj_t *new = STORE_OBJ(sdb_object_create(name,
373 sdb_store_obj_type, type));
374 if (! new) {
375 char errbuf[1024];
376 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
377 TYPE_TO_NAME(type), name,
378 sdb_strerror(errno, errbuf, sizeof(errbuf)));
379 pthread_rwlock_unlock(&obj_lock);
380 return -1;
381 }
383 /* TODO: insert type-aware */
384 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
385 sdb_object_cmp_by_name);
387 /* pass control to the list or destroy in case of an error */
388 sdb_object_deref(SDB_OBJ(new));
389 }
391 pthread_rwlock_unlock(&obj_lock);
392 return status;
393 } /* sdb_store_obj */
395 /*
396 * public API
397 */
399 int
400 sdb_store_host(const char *name, sdb_time_t last_update)
401 {
402 char *cname;
403 int status = 0;
405 if (! name)
406 return -1;
408 cname = sdb_plugin_cname(strdup(name));
409 if (! cname) {
410 sdb_log(SDB_LOG_ERR, "store: strdup failed");
411 return -1;
412 }
414 status = store_obj(/* parent = */ 0, NULL,
415 /* stored object = */ SDB_HOST, cname, last_update);
416 free(cname);
417 return status;
418 } /* sdb_store_host */
420 _Bool
421 sdb_store_has_host(const char *name)
422 {
423 sdb_store_obj_t *host;
425 if (! name)
426 return NULL;
428 host = sdb_store_lookup(SDB_HOST, name);
429 return host != NULL;
430 } /* sdb_store_has_host */
432 int
433 sdb_store_attribute(const char *hostname, const char *key, const char *value,
434 sdb_time_t last_update)
435 {
436 sdb_store_obj_t *sobj;
437 sdb_attribute_t *old;
439 int status = 0;
441 if ((! hostname) || (! key))
442 return -1;
444 if (last_update <= 0)
445 last_update = sdb_gettime();
447 if (! obj_list)
448 return -1;
450 pthread_rwlock_wrlock(&obj_lock);
452 sobj = sdb_store_lookup(SDB_HOST, hostname);
453 if (! sobj) {
454 pthread_rwlock_unlock(&obj_lock);
455 return -1;
456 }
458 old = SDB_ATTR(sdb_llist_search_by_name(sobj->attributes, key));
459 if (old) {
460 if (old->_last_update > last_update) {
461 sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
462 "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
463 hostname, key, last_update, old->_last_update);
464 status = 1;
465 }
466 else {
467 old->_last_update = last_update;
468 }
469 }
470 else {
471 sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key,
472 sdb_attribute_type, value));
473 if (! new) {
474 char errbuf[1024];
475 sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
476 "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
477 pthread_rwlock_unlock(&obj_lock);
478 return -1;
479 }
481 status = sdb_llist_insert_sorted(sobj->attributes, SDB_OBJ(new),
482 sdb_object_cmp_by_name);
484 /* pass control to the list or destroy in case of an error */
485 sdb_object_deref(SDB_OBJ(new));
486 }
488 pthread_rwlock_unlock(&obj_lock);
489 return status;
490 } /* sdb_store_attribute */
492 int
493 sdb_store_service(const char *hostname, const char *name,
494 sdb_time_t last_update)
495 {
496 char *cname;
497 int status = 0;
499 if ((! hostname) || (! name))
500 return -1;
502 cname = sdb_plugin_cname(strdup(hostname));
503 if (! cname) {
504 sdb_log(SDB_LOG_ERR, "store: strdup failed");
505 return -1;
506 }
508 status = store_obj(/* parent = */ SDB_HOST, cname,
509 /* stored object = */ SDB_SERVICE, name, last_update);
510 free(cname);
511 return status;
512 } /* sdb_store_service */
514 /* TODO: actually support hierarchical data */
515 int
516 sdb_store_dump(FILE *fh)
517 {
518 sdb_llist_iter_t *host_iter;
520 if (! fh)
521 return -1;
523 pthread_rwlock_rdlock(&obj_lock);
525 host_iter = sdb_llist_get_iter(obj_list);
526 if (! host_iter) {
527 pthread_rwlock_unlock(&obj_lock);
528 return -1;
529 }
531 while (sdb_llist_iter_has_next(host_iter)) {
532 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
533 sdb_llist_iter_t *svc_iter;
534 sdb_llist_iter_t *attr_iter;
536 char time_str[64];
538 assert(host);
540 if (! sdb_strftime(time_str, sizeof(time_str),
541 "%F %T %z", host->_last_update))
542 snprintf(time_str, sizeof(time_str), "<error>");
543 time_str[sizeof(time_str) - 1] = '\0';
545 fprintf(fh, "Host '%s' (last updated: %s):\n",
546 SDB_OBJ(host)->name, time_str);
548 attr_iter = sdb_llist_get_iter(host->attributes);
549 if (! attr_iter) {
550 char errbuf[1024];
551 fprintf(fh, "Failed to retrieve attributes: %s\n",
552 sdb_strerror(errno, errbuf, sizeof(errbuf)));
553 continue;
554 }
556 while (sdb_llist_iter_has_next(attr_iter)) {
557 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
558 assert(attr);
560 if (! sdb_strftime(time_str, sizeof(time_str),
561 "%F %T %z", attr->_last_update))
562 snprintf(time_str, sizeof(time_str), "<error>");
563 time_str[sizeof(time_str) - 1] = '\0';
565 fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
566 SDB_OBJ(attr)->name, attr->value, time_str);
567 }
569 sdb_llist_iter_destroy(attr_iter);
571 svc_iter = sdb_llist_get_iter(host->children);
572 if (! svc_iter) {
573 char errbuf[1024];
574 fprintf(fh, "Failed to retrieve services: %s\n",
575 sdb_strerror(errno, errbuf, sizeof(errbuf)));
576 continue;
577 }
579 while (sdb_llist_iter_has_next(svc_iter)) {
580 sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
581 assert(svc);
583 if (! sdb_strftime(time_str, sizeof(time_str),
584 "%F %T %z", svc->_last_update))
585 snprintf(time_str, sizeof(time_str), "<error>");
586 time_str[sizeof(time_str) - 1] = '\0';
588 fprintf(fh, "\tService '%s' (last updated: %s)\n",
589 SDB_OBJ(svc)->name, time_str);
590 }
592 sdb_llist_iter_destroy(svc_iter);
593 }
595 sdb_llist_iter_destroy(host_iter);
596 pthread_rwlock_unlock(&obj_lock);
597 return 0;
598 } /* sdb_store_dump */
600 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */