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 /* this may not be used as a type on its own but only as helper functions for
121 * the derived types; the function accepts a variadic list of arguments to
122 * pass to the sub-type's init function */
123 static sdb_object_t *
124 store_obj_clone(const sdb_object_t *obj, ...)
125 {
126 const store_obj_t *sobj = STORE_CONST_OBJ(obj);
127 store_obj_t *new;
129 va_list ap;
131 va_start(ap, obj);
133 new = STORE_OBJ(sdb_object_vcreate(obj->name, obj->type, ap));
134 va_end(ap);
135 if (! new)
136 return NULL;
138 new->last_update = sobj->last_update;
139 sdb_object_ref(SDB_OBJ(sobj->parent));
140 new->parent = sobj->parent;
141 return SDB_OBJ(new);
142 } /* store_obj_clone */
144 static int
145 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
146 {
147 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
148 int ret;
150 ret = store_obj_init(obj, ap);
151 if (ret)
152 return ret;
154 sobj->type = va_arg(ap, int);
156 sobj->children = sdb_llist_create();
157 if (! sobj->children)
158 return -1;
159 sobj->attributes = sdb_llist_create();
160 if (! sobj->attributes)
161 return -1;
162 return 0;
163 } /* sdb_store_obj_init */
165 static void
166 sdb_store_obj_destroy(sdb_object_t *obj)
167 {
168 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
170 assert(obj);
172 store_obj_destroy(obj);
174 if (sobj->children)
175 sdb_llist_destroy(sobj->children);
176 if (sobj->attributes)
177 sdb_llist_destroy(sobj->attributes);
178 } /* sdb_store_obj_destroy */
180 static sdb_object_t *
181 sdb_store_obj_clone(const sdb_object_t *obj)
182 {
183 const sdb_store_obj_t *sobj = SDB_CONST_STORE_OBJ(obj);
184 sdb_store_obj_t *new;
186 new = SDB_STORE_OBJ(store_obj_clone(obj, sobj->type));
187 if (! new)
188 return NULL;
190 new->type = sobj->type;
192 /* make sure these are initialized; else sdb_object_deref() might access
193 * arbitrary memory in case of an error */
194 new->children = new->attributes = NULL;
196 if (sobj->children) {
197 new->children = sdb_llist_clone(sobj->children);
198 if (! new->children) {
199 sdb_object_deref(SDB_OBJ(new));
200 return NULL;
201 }
202 }
203 if (sobj->attributes) {
204 new->attributes = sdb_llist_clone(sobj->attributes);
205 if (! new->attributes) {
206 sdb_object_deref(SDB_OBJ(new));
207 return NULL;
208 }
209 }
211 new->_last_update = sobj->_last_update;
212 return SDB_OBJ(new);
213 } /* sdb_store_obj_clone */
215 static int
216 sdb_attr_init(sdb_object_t *obj, va_list ap)
217 {
218 const char *value = va_arg(ap, const char *);
219 int ret;
221 ret = store_obj_init(obj, ap);
222 if (ret)
223 return ret;
225 SDB_ATTR(obj)->value = strdup(value);
226 if (! SDB_ATTR(obj)->value)
227 return -1;
228 return 0;
229 } /* sdb_attr_init */
231 static void
232 sdb_attr_destroy(sdb_object_t *obj)
233 {
234 assert(obj);
236 store_obj_destroy(obj);
238 if (SDB_ATTR(obj)->value)
239 free(SDB_ATTR(obj)->value);
240 } /* sdb_attr_destroy */
242 static sdb_object_t *
243 sdb_attr_clone(const sdb_object_t *obj)
244 {
245 const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
246 sdb_attribute_t *new;
248 new = SDB_ATTR(store_obj_clone(obj, attr->value));
249 if (! new)
250 return NULL;
251 return SDB_OBJ(new);
252 } /* sdb_attr_clone */
254 static sdb_type_t sdb_store_obj_type = {
255 sizeof(sdb_store_obj_t),
257 sdb_store_obj_init,
258 sdb_store_obj_destroy,
259 sdb_store_obj_clone
260 };
262 static sdb_type_t sdb_attribute_type = {
263 sizeof(sdb_attribute_t),
265 sdb_attr_init,
266 sdb_attr_destroy,
267 sdb_attr_clone
268 };
270 /*
271 * private helper functions
272 */
274 static sdb_store_obj_t *
275 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
276 {
277 sdb_llist_iter_t *iter;
279 if (! l)
280 return NULL;
282 iter = sdb_llist_get_iter(l);
283 if (! iter)
284 return NULL;
286 while (sdb_llist_iter_has_next(iter)) {
287 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
288 assert(sobj);
290 if ((sobj->type == type)
291 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
292 sdb_llist_iter_destroy(iter);
293 return sobj;
294 }
296 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
297 if (sobj) {
298 sdb_llist_iter_destroy(iter);
299 return sobj;
300 }
301 }
302 sdb_llist_iter_destroy(iter);
303 return NULL;
304 } /* sdb_store_lookup_in_list */
306 static sdb_store_obj_t *
307 sdb_store_lookup(int type, const char *name)
308 {
309 return sdb_store_lookup_in_list(obj_list, type, name);
310 } /* sdb_store_lookup */
312 static int
313 store_obj(int parent_type, const char *parent_name,
314 int type, const char *name, sdb_time_t last_update)
315 {
316 sdb_llist_t *parent_list;
317 store_obj_t *old;
318 int status = 0;
320 if (! name)
321 return -1;
323 if (last_update <= 0)
324 last_update = sdb_gettime();
326 assert((parent_type == 0)
327 || (parent_type = SDB_HOST)
328 || (parent_type == SDB_SERVICE));
329 assert((type == 0) || (type = SDB_HOST) || (type == SDB_SERVICE));
331 pthread_rwlock_wrlock(&obj_lock);
333 if (! obj_list) {
334 if (! (obj_list = sdb_llist_create())) {
335 pthread_rwlock_unlock(&obj_lock);
336 return -1;
337 }
338 }
339 parent_list = obj_list;
341 if (parent_type && parent_name) {
342 sdb_store_obj_t *parent;
344 parent = sdb_store_lookup(parent_type, parent_name);
345 if (! parent) {
346 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
347 "parent %s '%s' not found", TYPE_TO_NAME(type), name,
348 TYPE_TO_NAME(parent_type), parent_name);
349 pthread_rwlock_unlock(&obj_lock);
350 return -1;
351 }
353 parent_list = parent->children;
354 }
356 /* TODO: only look into direct children? */
357 old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
359 if (old) {
360 if (old->last_update > last_update) {
361 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
362 "value too old (%"PRIscTIME" < %"PRIscTIME")",
363 TYPE_TO_NAME(type), name, last_update, old->last_update);
364 /* don't report an error; the object may be updated by multiple
365 * backends */
366 status = 1;
367 }
368 else {
369 old->last_update = last_update;
370 }
371 }
372 else {
373 store_obj_t *new = STORE_OBJ(sdb_object_create(name,
374 sdb_store_obj_type, type));
375 if (! new) {
376 char errbuf[1024];
377 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
378 TYPE_TO_NAME(type), name,
379 sdb_strerror(errno, errbuf, sizeof(errbuf)));
380 pthread_rwlock_unlock(&obj_lock);
381 return -1;
382 }
384 /* TODO: insert type-aware */
385 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
386 sdb_object_cmp_by_name);
388 /* pass control to the list or destroy in case of an error */
389 sdb_object_deref(SDB_OBJ(new));
390 }
392 pthread_rwlock_unlock(&obj_lock);
393 return status;
394 } /* sdb_store_obj */
396 /*
397 * public API
398 */
400 int
401 sdb_store_host(const char *name, sdb_time_t last_update)
402 {
403 char *cname;
404 int status = 0;
406 if (! name)
407 return -1;
409 cname = sdb_plugin_cname(strdup(name));
410 if (! cname) {
411 sdb_log(SDB_LOG_ERR, "store: strdup failed");
412 return -1;
413 }
415 status = store_obj(/* parent = */ 0, NULL,
416 /* stored object = */ SDB_HOST, cname, last_update);
417 free(cname);
418 return status;
419 } /* sdb_store_host */
421 _Bool
422 sdb_store_has_host(const char *name)
423 {
424 sdb_store_obj_t *host;
426 if (! name)
427 return NULL;
429 host = sdb_store_lookup(SDB_HOST, name);
430 return host != NULL;
431 } /* sdb_store_has_host */
433 int
434 sdb_store_attribute(const char *hostname, const char *key, const char *value,
435 sdb_time_t last_update)
436 {
437 sdb_store_obj_t *sobj;
438 sdb_attribute_t *old;
440 int status = 0;
442 if ((! hostname) || (! key))
443 return -1;
445 if (last_update <= 0)
446 last_update = sdb_gettime();
448 if (! obj_list)
449 return -1;
451 pthread_rwlock_wrlock(&obj_lock);
453 sobj = sdb_store_lookup(SDB_HOST, hostname);
454 if (! sobj) {
455 pthread_rwlock_unlock(&obj_lock);
456 return -1;
457 }
459 old = SDB_ATTR(sdb_llist_search_by_name(sobj->attributes, key));
460 if (old) {
461 if (old->_last_update > last_update) {
462 sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
463 "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
464 hostname, key, last_update, old->_last_update);
465 status = 1;
466 }
467 else {
468 old->_last_update = last_update;
469 }
470 }
471 else {
472 sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key,
473 sdb_attribute_type, value));
474 if (! new) {
475 char errbuf[1024];
476 sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
477 "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
478 pthread_rwlock_unlock(&obj_lock);
479 return -1;
480 }
482 status = sdb_llist_insert_sorted(sobj->attributes, SDB_OBJ(new),
483 sdb_object_cmp_by_name);
485 /* pass control to the list or destroy in case of an error */
486 sdb_object_deref(SDB_OBJ(new));
487 }
489 pthread_rwlock_unlock(&obj_lock);
490 return status;
491 } /* sdb_store_attribute */
493 int
494 sdb_store_service(const char *hostname, const char *name,
495 sdb_time_t last_update)
496 {
497 char *cname;
498 int status = 0;
500 if ((! hostname) || (! name))
501 return -1;
503 cname = sdb_plugin_cname(strdup(hostname));
504 if (! cname) {
505 sdb_log(SDB_LOG_ERR, "store: strdup failed");
506 return -1;
507 }
509 status = store_obj(/* parent = */ SDB_HOST, cname,
510 /* stored object = */ SDB_SERVICE, name, last_update);
511 free(cname);
512 return status;
513 } /* sdb_store_service */
515 /* TODO: actually support hierarchical data */
516 int
517 sdb_store_dump(FILE *fh)
518 {
519 sdb_llist_iter_t *host_iter;
521 if (! fh)
522 return -1;
524 pthread_rwlock_rdlock(&obj_lock);
526 host_iter = sdb_llist_get_iter(obj_list);
527 if (! host_iter) {
528 pthread_rwlock_unlock(&obj_lock);
529 return -1;
530 }
532 while (sdb_llist_iter_has_next(host_iter)) {
533 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
534 sdb_llist_iter_t *svc_iter;
535 sdb_llist_iter_t *attr_iter;
537 char time_str[64];
539 assert(host);
541 if (! sdb_strftime(time_str, sizeof(time_str),
542 "%F %T %z", host->_last_update))
543 snprintf(time_str, sizeof(time_str), "<error>");
544 time_str[sizeof(time_str) - 1] = '\0';
546 fprintf(fh, "Host '%s' (last updated: %s):\n",
547 SDB_OBJ(host)->name, time_str);
549 attr_iter = sdb_llist_get_iter(host->attributes);
550 if (! attr_iter) {
551 char errbuf[1024];
552 fprintf(fh, "Failed to retrieve attributes: %s\n",
553 sdb_strerror(errno, errbuf, sizeof(errbuf)));
554 continue;
555 }
557 while (sdb_llist_iter_has_next(attr_iter)) {
558 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
559 assert(attr);
561 if (! sdb_strftime(time_str, sizeof(time_str),
562 "%F %T %z", attr->_last_update))
563 snprintf(time_str, sizeof(time_str), "<error>");
564 time_str[sizeof(time_str) - 1] = '\0';
566 fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
567 SDB_OBJ(attr)->name, attr->value, time_str);
568 }
570 sdb_llist_iter_destroy(attr_iter);
572 svc_iter = sdb_llist_get_iter(host->children);
573 if (! svc_iter) {
574 char errbuf[1024];
575 fprintf(fh, "Failed to retrieve services: %s\n",
576 sdb_strerror(errno, errbuf, sizeof(errbuf)));
577 continue;
578 }
580 while (sdb_llist_iter_has_next(svc_iter)) {
581 sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
582 assert(svc);
584 if (! sdb_strftime(time_str, sizeof(time_str),
585 "%F %T %z", svc->_last_update))
586 snprintf(time_str, sizeof(time_str), "<error>");
587 time_str[sizeof(time_str) - 1] = '\0';
589 fprintf(fh, "\tService '%s' (last updated: %s)\n",
590 SDB_OBJ(svc)->name, time_str);
591 }
593 sdb_llist_iter_destroy(svc_iter);
594 }
596 sdb_llist_iter_destroy(host_iter);
597 pthread_rwlock_unlock(&obj_lock);
598 return 0;
599 } /* sdb_store_dump */
601 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */