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;
60 sdb_time_t last_update;
61 sdb_store_base_t *parent;
62 };
63 #define STORE_BASE(obj) ((sdb_store_base_t *)(obj))
64 #define STORE_CONST_BASE(obj) ((const sdb_store_base_t *)(obj))
66 typedef struct {
67 sdb_store_base_t super;
69 char *value;
70 } sdb_attribute_t;
71 #define SDB_ATTR(obj) ((sdb_attribute_t *)(obj))
72 #define SDB_CONST_ATTR(obj) ((const sdb_attribute_t *)(obj))
74 typedef struct {
75 sdb_store_base_t super;
77 int type;
78 sdb_llist_t *children;
80 sdb_llist_t *attributes;
81 } sdb_store_obj_t;
82 #define SDB_STORE_OBJ(obj) ((sdb_store_obj_t *)(obj))
83 #define SDB_CONST_STORE_OBJ(obj) ((const sdb_store_obj_t *)(obj))
85 enum {
86 SDB_HOST = 1,
87 SDB_SERVICE,
88 SDB_ATTRIBUTE,
89 };
90 #define TYPE_TO_NAME(t) \
91 (((t) == SDB_HOST) ? "host" \
92 : ((t) == SDB_SERVICE) ? "service" \
93 : ((t) == SDB_ATTRIBUTE) ? "attribute" : "unknown")
95 /* shortcuts for accessing the sdb_store_obj_t attributes
96 * of inheriting objects */
97 #define _last_update super.last_update
99 static int
100 store_base_init(sdb_object_t *obj, va_list ap)
101 {
102 sdb_store_base_t *sobj = STORE_BASE(obj);
103 sobj->last_update = va_arg(ap, sdb_time_t);
105 sobj->parent = NULL;
106 return 0;
107 } /* store_base_init */
109 static void
110 store_base_destroy(sdb_object_t *obj)
111 {
112 const sdb_store_base_t *sobj = STORE_CONST_BASE(obj);
114 if (sobj->parent)
115 sdb_object_deref(SDB_OBJ(sobj->parent));
116 } /* store_base_destroy */
118 static int
119 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
120 {
121 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
122 int ret;
124 ret = store_base_init(obj, ap);
125 if (ret)
126 return ret;
128 sobj->type = va_arg(ap, int);
130 sobj->children = sdb_llist_create();
131 if (! sobj->children)
132 return -1;
133 sobj->attributes = sdb_llist_create();
134 if (! sobj->attributes)
135 return -1;
136 return 0;
137 } /* sdb_store_obj_init */
139 static void
140 sdb_store_obj_destroy(sdb_object_t *obj)
141 {
142 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
144 assert(obj);
146 store_base_destroy(obj);
148 if (sobj->children)
149 sdb_llist_destroy(sobj->children);
150 if (sobj->attributes)
151 sdb_llist_destroy(sobj->attributes);
152 } /* sdb_store_obj_destroy */
154 static int
155 sdb_attr_init(sdb_object_t *obj, va_list ap)
156 {
157 const char *value;
158 int ret;
160 ret = store_base_init(obj, ap);
161 if (ret)
162 return ret;
163 value = va_arg(ap, const char *);
165 if (value) {
166 SDB_ATTR(obj)->value = strdup(value);
167 if (! SDB_ATTR(obj)->value)
168 return -1;
169 }
170 return 0;
171 } /* sdb_attr_init */
173 static void
174 sdb_attr_destroy(sdb_object_t *obj)
175 {
176 assert(obj);
178 store_base_destroy(obj);
180 if (SDB_ATTR(obj)->value)
181 free(SDB_ATTR(obj)->value);
182 } /* sdb_attr_destroy */
184 static sdb_type_t sdb_store_obj_type = {
185 sizeof(sdb_store_obj_t),
187 sdb_store_obj_init,
188 sdb_store_obj_destroy
189 };
191 static sdb_type_t sdb_attribute_type = {
192 sizeof(sdb_attribute_t),
194 sdb_attr_init,
195 sdb_attr_destroy
196 };
198 /*
199 * private helper functions
200 */
202 static sdb_store_obj_t *
203 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
204 {
205 sdb_llist_iter_t *iter;
207 if (! l)
208 return NULL;
210 iter = sdb_llist_get_iter(l);
211 if (! iter)
212 return NULL;
214 while (sdb_llist_iter_has_next(iter)) {
215 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
216 assert(sobj);
218 if ((sobj->type == type)
219 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
220 sdb_llist_iter_destroy(iter);
221 return sobj;
222 }
224 /* don't lookups non-host types from hierarchical hosts */
225 if ((type != SDB_HOST) && (sobj->type == SDB_HOST))
226 continue;
228 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
229 if (sobj) {
230 sdb_llist_iter_destroy(iter);
231 return sobj;
232 }
233 }
234 sdb_llist_iter_destroy(iter);
235 return NULL;
236 } /* sdb_store_lookup_in_list */
238 static sdb_store_obj_t *
239 sdb_store_lookup(int type, const char *name)
240 {
241 return sdb_store_lookup_in_list(obj_list, type, name);
242 } /* sdb_store_lookup */
244 /* The obj_lock has to be acquired before calling this function. */
245 static int
246 store_obj(int parent_type, const char *parent_name,
247 int type, const char *name, sdb_time_t last_update,
248 sdb_store_base_t **updated_obj)
249 {
250 char *parent_cname = NULL, *cname = NULL;
252 sdb_llist_t *parent_list;
253 sdb_store_base_t *old;
254 int status = 0;
256 if (last_update <= 0)
257 last_update = sdb_gettime();
259 assert((parent_type == 0)
260 || (parent_type == SDB_HOST)
261 || (parent_type == SDB_SERVICE));
262 assert((type == 0)
263 || (type == SDB_HOST)
264 || (type == SDB_SERVICE)
265 || (type == SDB_ATTRIBUTE));
267 if (parent_type == SDB_HOST) {
268 parent_cname = sdb_plugin_cname(strdup(parent_name));
269 if (! parent_cname) {
270 sdb_log(SDB_LOG_ERR, "store: strdup failed");
271 return -1;
272 }
273 parent_name = parent_cname;
274 }
275 if (type == SDB_HOST) {
276 cname = sdb_plugin_cname(strdup(name));
277 if (! cname) {
278 sdb_log(SDB_LOG_ERR, "store: strdup failed");
279 return -1;
280 }
281 name = cname;
282 }
284 if (! obj_list) {
285 if (! (obj_list = sdb_llist_create())) {
286 free(parent_cname);
287 free(cname);
288 return -1;
289 }
290 }
291 parent_list = obj_list;
293 if (parent_type && parent_name) {
294 sdb_store_obj_t *parent;
296 parent = sdb_store_lookup(parent_type, parent_name);
297 if (! parent) {
298 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
299 "parent %s '%s' not found", TYPE_TO_NAME(type), name,
300 TYPE_TO_NAME(parent_type), parent_name);
301 free(parent_cname);
302 free(cname);
303 return -1;
304 }
306 if (type == SDB_ATTRIBUTE)
307 parent_list = parent->attributes;
308 else
309 parent_list = parent->children;
310 }
312 if (type == SDB_HOST)
313 /* make sure that each host is unique */
314 old = STORE_BASE(sdb_store_lookup_in_list(obj_list, type, name));
315 else if (type == SDB_ATTRIBUTE)
316 /* look into attributes of this host */
317 old = STORE_BASE(sdb_llist_search_by_name(parent_list, name));
318 else
319 /* look into services assigned to this host (sdb_store_lookup_in_list
320 * does not look up services from hierarchical hosts) */
321 old = STORE_BASE(sdb_store_lookup_in_list(parent_list, type, name));
323 if (old) {
324 if (old->last_update > last_update) {
325 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
326 "value too old (%"PRIscTIME" < %"PRIscTIME")",
327 TYPE_TO_NAME(type), name, last_update, old->last_update);
328 /* don't report an error; the object may be updated by multiple
329 * backends */
330 status = 1;
331 }
332 else {
333 old->last_update = last_update;
334 }
336 if (updated_obj)
337 *updated_obj = old;
338 }
339 else {
340 sdb_store_base_t *new;
342 if (type == SDB_ATTRIBUTE)
343 /* the value will be updated by the caller */
344 new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
345 last_update, NULL));
346 else
347 new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
348 last_update, type));
350 if (! new) {
351 char errbuf[1024];
352 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
353 TYPE_TO_NAME(type), name,
354 sdb_strerror(errno, errbuf, sizeof(errbuf)));
355 free(parent_cname);
356 free(cname);
357 return -1;
358 }
360 /* TODO: insert type-aware; the current version works as long as we
361 * don't support to store hierarchical data */
362 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
363 sdb_object_cmp_by_name);
365 /* pass control to the list or destroy in case of an error */
366 sdb_object_deref(SDB_OBJ(new));
368 if (updated_obj)
369 *updated_obj = new;
370 }
371 free(parent_cname);
372 free(cname);
373 return status;
374 } /* sdb_store_obj */
376 /*
377 * public API
378 */
380 int
381 sdb_store_host(const char *name, sdb_time_t last_update)
382 {
383 int status;
385 if (! name)
386 return -1;
388 pthread_rwlock_wrlock(&obj_lock);
389 status = store_obj(/* parent = */ 0, NULL,
390 /* stored object = */ SDB_HOST, name, last_update,
391 /* updated_obj = */ NULL);
392 pthread_rwlock_unlock(&obj_lock);
393 return status;
394 } /* sdb_store_host */
396 _Bool
397 sdb_store_has_host(const char *name)
398 {
399 sdb_store_obj_t *host;
401 if (! name)
402 return NULL;
404 host = sdb_store_lookup(SDB_HOST, name);
405 return host != NULL;
406 } /* sdb_store_has_host */
408 int
409 sdb_store_attribute(const char *hostname, const char *key, const char *value,
410 sdb_time_t last_update)
411 {
412 int status;
414 sdb_store_base_t *updated_attr = NULL;
416 if ((! hostname) || (! key))
417 return -1;
419 pthread_rwlock_wrlock(&obj_lock);
420 status = store_obj(/* parent = */ SDB_HOST, hostname,
421 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
422 &updated_attr);
424 if (status >= 0) {
425 assert(updated_attr);
426 SDB_ATTR(updated_attr)->value = strdup(value);
427 if (! SDB_ATTR(updated_attr)->value) {
428 sdb_object_deref(SDB_OBJ(updated_attr));
429 status = -1;
430 }
431 }
433 pthread_rwlock_unlock(&obj_lock);
434 return status;
435 } /* sdb_store_attribute */
437 int
438 sdb_store_service(const char *hostname, const char *name,
439 sdb_time_t last_update)
440 {
441 int status;
443 if ((! hostname) || (! name))
444 return -1;
446 pthread_rwlock_wrlock(&obj_lock);
447 status = store_obj(/* parent = */ SDB_HOST, hostname,
448 /* stored object = */ SDB_SERVICE, name, last_update,
449 /* updated obj = */ NULL);
450 pthread_rwlock_unlock(&obj_lock);
451 return status;
452 } /* sdb_store_service */
454 /* TODO: actually support hierarchical data */
455 int
456 sdb_store_tojson(sdb_strbuf_t *buf)
457 {
458 sdb_llist_iter_t *host_iter;
460 if (! buf)
461 return -1;
463 pthread_rwlock_rdlock(&obj_lock);
465 host_iter = sdb_llist_get_iter(obj_list);
466 if (! host_iter) {
467 pthread_rwlock_unlock(&obj_lock);
468 return -1;
469 }
471 sdb_strbuf_append(buf, "{\"hosts\":[");
473 while (sdb_llist_iter_has_next(host_iter)) {
474 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
475 sdb_llist_iter_t *svc_iter;
476 sdb_llist_iter_t *attr_iter;
478 char time_str[64];
480 assert(host);
482 if (! sdb_strftime(time_str, sizeof(time_str),
483 "%F %T %z", host->_last_update))
484 snprintf(time_str, sizeof(time_str), "<error>");
485 time_str[sizeof(time_str) - 1] = '\0';
487 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
488 "\"last_update\": \"%s\", "
489 "\"attributes\": [",
490 SDB_OBJ(host)->name, time_str);
492 attr_iter = sdb_llist_get_iter(host->attributes);
493 if (! attr_iter) {
494 char errbuf[1024];
495 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve attributes: %s\n",
496 sdb_strerror(errno, errbuf, sizeof(errbuf)));
497 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve "
498 "attributes: %s\"}", errbuf);
499 }
501 /* has_next returns false if the iterator is NULL */
502 while (sdb_llist_iter_has_next(attr_iter)) {
503 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
504 assert(attr);
506 if (! sdb_strftime(time_str, sizeof(time_str),
507 "%F %T %z", attr->_last_update))
508 snprintf(time_str, sizeof(time_str), "<error>");
509 time_str[sizeof(time_str) - 1] = '\0';
511 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
512 "\"value\": \"%s\", \"last_update\": \"%s\"},",
513 SDB_OBJ(attr)->name, attr->value, time_str);
514 }
516 sdb_llist_iter_destroy(attr_iter);
517 sdb_strbuf_append(buf, "], \"services\": [");
519 svc_iter = sdb_llist_get_iter(host->children);
520 if (! svc_iter) {
521 char errbuf[1024];
522 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve services: %s\n",
523 sdb_strerror(errno, errbuf, sizeof(errbuf)));
524 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve "
525 "services: %s\"}", errbuf);
526 }
528 while (sdb_llist_iter_has_next(svc_iter)) {
529 sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
530 assert(svc);
532 if (! sdb_strftime(time_str, sizeof(time_str),
533 "%F %T %z", svc->_last_update))
534 snprintf(time_str, sizeof(time_str), "<error>");
535 time_str[sizeof(time_str) - 1] = '\0';
537 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
538 "\"last_update\": \"%s\"},",
539 SDB_OBJ(svc)->name, time_str);
540 }
542 sdb_llist_iter_destroy(svc_iter);
543 sdb_strbuf_append(buf, "]},");
544 }
546 sdb_strbuf_append(buf, "]}");
548 sdb_llist_iter_destroy(host_iter);
549 pthread_rwlock_unlock(&obj_lock);
550 return 0;
551 } /* sdb_store_tojson */
553 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */