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 static int
122 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
123 {
124 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
125 int ret;
127 ret = store_obj_init(obj, ap);
128 if (ret)
129 return ret;
131 sobj->type = va_arg(ap, int);
133 sobj->children = sdb_llist_create();
134 if (! sobj->children)
135 return -1;
136 sobj->attributes = sdb_llist_create();
137 if (! sobj->attributes)
138 return -1;
139 return 0;
140 } /* sdb_store_obj_init */
142 static void
143 sdb_store_obj_destroy(sdb_object_t *obj)
144 {
145 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
147 assert(obj);
149 store_obj_destroy(obj);
151 if (sobj->children)
152 sdb_llist_destroy(sobj->children);
153 if (sobj->attributes)
154 sdb_llist_destroy(sobj->attributes);
155 } /* sdb_store_obj_destroy */
157 static int
158 sdb_attr_init(sdb_object_t *obj, va_list ap)
159 {
160 const char *value;
161 int ret;
163 ret = store_obj_init(obj, ap);
164 if (ret)
165 return ret;
166 value = va_arg(ap, const char *);
168 if (value) {
169 SDB_ATTR(obj)->value = strdup(value);
170 if (! SDB_ATTR(obj)->value)
171 return -1;
172 }
173 return 0;
174 } /* sdb_attr_init */
176 static void
177 sdb_attr_destroy(sdb_object_t *obj)
178 {
179 assert(obj);
181 store_obj_destroy(obj);
183 if (SDB_ATTR(obj)->value)
184 free(SDB_ATTR(obj)->value);
185 } /* sdb_attr_destroy */
187 static sdb_type_t sdb_store_obj_type = {
188 sizeof(sdb_store_obj_t),
190 sdb_store_obj_init,
191 sdb_store_obj_destroy
192 };
194 static sdb_type_t sdb_attribute_type = {
195 sizeof(sdb_attribute_t),
197 sdb_attr_init,
198 sdb_attr_destroy
199 };
201 /*
202 * private helper functions
203 */
205 static sdb_store_obj_t *
206 sdb_store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
207 {
208 sdb_llist_iter_t *iter;
210 if (! l)
211 return NULL;
213 iter = sdb_llist_get_iter(l);
214 if (! iter)
215 return NULL;
217 while (sdb_llist_iter_has_next(iter)) {
218 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
219 assert(sobj);
221 if ((sobj->type == type)
222 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
223 sdb_llist_iter_destroy(iter);
224 return sobj;
225 }
227 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
228 if (sobj) {
229 sdb_llist_iter_destroy(iter);
230 return sobj;
231 }
232 }
233 sdb_llist_iter_destroy(iter);
234 return NULL;
235 } /* sdb_store_lookup_in_list */
237 static sdb_store_obj_t *
238 sdb_store_lookup(int type, const char *name)
239 {
240 return sdb_store_lookup_in_list(obj_list, type, name);
241 } /* sdb_store_lookup */
243 /* The obj_lock has to be acquired before calling this function. */
244 static int
245 store_obj(int parent_type, const char *parent_name,
246 int type, const char *name, sdb_time_t last_update,
247 store_obj_t **updated_obj)
248 {
249 char *parent_cname = NULL, *cname = NULL;
251 sdb_llist_t *parent_list;
252 store_obj_t *old;
253 int status = 0;
255 if (last_update <= 0)
256 last_update = sdb_gettime();
258 assert((parent_type == 0)
259 || (parent_type == SDB_HOST)
260 || (parent_type == SDB_SERVICE));
261 assert((type == 0)
262 || (type == SDB_HOST)
263 || (type == SDB_SERVICE)
264 || (type == SDB_ATTRIBUTE));
266 if (parent_type == SDB_HOST) {
267 parent_cname = sdb_plugin_cname(strdup(parent_name));
268 if (! parent_cname) {
269 sdb_log(SDB_LOG_ERR, "store: strdup failed");
270 return -1;
271 }
272 parent_name = parent_cname;
273 }
274 if (type == SDB_HOST) {
275 cname = sdb_plugin_cname(strdup(name));
276 if (! cname) {
277 sdb_log(SDB_LOG_ERR, "store: strdup failed");
278 return -1;
279 }
280 name = cname;
281 }
283 if (! obj_list) {
284 if (! (obj_list = sdb_llist_create())) {
285 free(parent_cname);
286 free(cname);
287 return -1;
288 }
289 }
290 parent_list = obj_list;
292 if (parent_type && parent_name) {
293 sdb_store_obj_t *parent;
295 parent = sdb_store_lookup(parent_type, parent_name);
296 if (! parent) {
297 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
298 "parent %s '%s' not found", TYPE_TO_NAME(type), name,
299 TYPE_TO_NAME(parent_type), parent_name);
300 free(parent_cname);
301 free(cname);
302 return -1;
303 }
305 if (type == SDB_ATTRIBUTE)
306 parent_list = parent->attributes;
307 else
308 parent_list = parent->children;
309 }
311 /* TODO: only look into direct children? */
312 if (type == SDB_ATTRIBUTE)
313 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
314 else
315 old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
317 if (old) {
318 if (old->last_update > last_update) {
319 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
320 "value too old (%"PRIscTIME" < %"PRIscTIME")",
321 TYPE_TO_NAME(type), name, last_update, old->last_update);
322 /* don't report an error; the object may be updated by multiple
323 * backends */
324 status = 1;
325 }
326 else {
327 old->last_update = last_update;
328 }
330 if (updated_obj)
331 *updated_obj = old;
332 }
333 else {
334 store_obj_t *new;
336 if (type == SDB_ATTRIBUTE)
337 /* the value will be updated by the caller */
338 new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
339 last_update, NULL));
340 else
341 new = STORE_OBJ(sdb_object_create(name, sdb_store_obj_type,
342 last_update, type));
344 if (! new) {
345 char errbuf[1024];
346 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
347 TYPE_TO_NAME(type), name,
348 sdb_strerror(errno, errbuf, sizeof(errbuf)));
349 free(parent_cname);
350 free(cname);
351 return -1;
352 }
354 /* TODO: insert type-aware; the current version works as long as we
355 * don't support to store hierarchical data */
356 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
357 sdb_object_cmp_by_name);
359 /* pass control to the list or destroy in case of an error */
360 sdb_object_deref(SDB_OBJ(new));
362 if (updated_obj)
363 *updated_obj = new;
364 }
365 free(parent_cname);
366 free(cname);
367 return status;
368 } /* sdb_store_obj */
370 /*
371 * public API
372 */
374 int
375 sdb_store_host(const char *name, sdb_time_t last_update)
376 {
377 int status;
379 if (! name)
380 return -1;
382 pthread_rwlock_wrlock(&obj_lock);
383 status = store_obj(/* parent = */ 0, NULL,
384 /* stored object = */ SDB_HOST, name, last_update,
385 /* updated_obj = */ NULL);
386 pthread_rwlock_unlock(&obj_lock);
387 return status;
388 } /* sdb_store_host */
390 _Bool
391 sdb_store_has_host(const char *name)
392 {
393 sdb_store_obj_t *host;
395 if (! name)
396 return NULL;
398 host = sdb_store_lookup(SDB_HOST, name);
399 return host != NULL;
400 } /* sdb_store_has_host */
402 int
403 sdb_store_attribute(const char *hostname, const char *key, const char *value,
404 sdb_time_t last_update)
405 {
406 int status;
408 store_obj_t *updated_attr = NULL;
410 if ((! hostname) || (! key))
411 return -1;
413 pthread_rwlock_wrlock(&obj_lock);
414 status = store_obj(/* parent = */ SDB_HOST, hostname,
415 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
416 &updated_attr);
418 SDB_ATTR(updated_attr)->value = strdup(value);
419 if (! SDB_ATTR(updated_attr)->value) {
420 sdb_object_deref(SDB_OBJ(updated_attr));
421 status = -1;
422 }
423 pthread_rwlock_unlock(&obj_lock);
424 return status;
425 } /* sdb_store_attribute */
427 int
428 sdb_store_service(const char *hostname, const char *name,
429 sdb_time_t last_update)
430 {
431 int status;
433 if ((! hostname) || (! name))
434 return -1;
436 pthread_rwlock_wrlock(&obj_lock);
437 status = store_obj(/* parent = */ SDB_HOST, hostname,
438 /* stored object = */ SDB_SERVICE, name, last_update,
439 /* updated obj = */ NULL);
440 pthread_rwlock_unlock(&obj_lock);
441 return status;
442 } /* sdb_store_service */
444 /* TODO: actually support hierarchical data */
445 int
446 sdb_store_dump(FILE *fh)
447 {
448 sdb_llist_iter_t *host_iter;
450 if (! fh)
451 return -1;
453 pthread_rwlock_rdlock(&obj_lock);
455 host_iter = sdb_llist_get_iter(obj_list);
456 if (! host_iter) {
457 pthread_rwlock_unlock(&obj_lock);
458 return -1;
459 }
461 while (sdb_llist_iter_has_next(host_iter)) {
462 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
463 sdb_llist_iter_t *svc_iter;
464 sdb_llist_iter_t *attr_iter;
466 char time_str[64];
468 assert(host);
470 if (! sdb_strftime(time_str, sizeof(time_str),
471 "%F %T %z", host->_last_update))
472 snprintf(time_str, sizeof(time_str), "<error>");
473 time_str[sizeof(time_str) - 1] = '\0';
475 fprintf(fh, "Host '%s' (last updated: %s):\n",
476 SDB_OBJ(host)->name, time_str);
478 attr_iter = sdb_llist_get_iter(host->attributes);
479 if (! attr_iter) {
480 char errbuf[1024];
481 fprintf(fh, "Failed to retrieve attributes: %s\n",
482 sdb_strerror(errno, errbuf, sizeof(errbuf)));
483 continue;
484 }
486 while (sdb_llist_iter_has_next(attr_iter)) {
487 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
488 assert(attr);
490 if (! sdb_strftime(time_str, sizeof(time_str),
491 "%F %T %z", attr->_last_update))
492 snprintf(time_str, sizeof(time_str), "<error>");
493 time_str[sizeof(time_str) - 1] = '\0';
495 fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
496 SDB_OBJ(attr)->name, attr->value, time_str);
497 }
499 sdb_llist_iter_destroy(attr_iter);
501 svc_iter = sdb_llist_get_iter(host->children);
502 if (! svc_iter) {
503 char errbuf[1024];
504 fprintf(fh, "Failed to retrieve services: %s\n",
505 sdb_strerror(errno, errbuf, sizeof(errbuf)));
506 continue;
507 }
509 while (sdb_llist_iter_has_next(svc_iter)) {
510 sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
511 assert(svc);
513 if (! sdb_strftime(time_str, sizeof(time_str),
514 "%F %T %z", svc->_last_update))
515 snprintf(time_str, sizeof(time_str), "<error>");
516 time_str[sizeof(time_str) - 1] = '\0';
518 fprintf(fh, "\tService '%s' (last updated: %s)\n",
519 SDB_OBJ(svc)->name, time_str);
520 }
522 sdb_llist_iter_destroy(svc_iter);
523 }
525 sdb_llist_iter_destroy(host_iter);
526 pthread_rwlock_unlock(&obj_lock);
527 return 0;
528 } /* sdb_store_dump */
530 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */