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_HOST)
313 /* make sure that each host is unique */
314 old = STORE_OBJ(sdb_store_lookup_in_list(obj_list, type, name));
315 else if (type == SDB_ATTRIBUTE)
316 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
317 else
318 old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
320 if (old) {
321 if (old->last_update > last_update) {
322 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
323 "value too old (%"PRIscTIME" < %"PRIscTIME")",
324 TYPE_TO_NAME(type), name, last_update, old->last_update);
325 /* don't report an error; the object may be updated by multiple
326 * backends */
327 status = 1;
328 }
329 else {
330 old->last_update = last_update;
331 }
333 if (updated_obj)
334 *updated_obj = old;
335 }
336 else {
337 store_obj_t *new;
339 if (type == SDB_ATTRIBUTE)
340 /* the value will be updated by the caller */
341 new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
342 last_update, NULL));
343 else
344 new = STORE_OBJ(sdb_object_create(name, sdb_store_obj_type,
345 last_update, type));
347 if (! new) {
348 char errbuf[1024];
349 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
350 TYPE_TO_NAME(type), name,
351 sdb_strerror(errno, errbuf, sizeof(errbuf)));
352 free(parent_cname);
353 free(cname);
354 return -1;
355 }
357 /* TODO: insert type-aware; the current version works as long as we
358 * don't support to store hierarchical data */
359 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
360 sdb_object_cmp_by_name);
362 /* pass control to the list or destroy in case of an error */
363 sdb_object_deref(SDB_OBJ(new));
365 if (updated_obj)
366 *updated_obj = new;
367 }
368 free(parent_cname);
369 free(cname);
370 return status;
371 } /* sdb_store_obj */
373 /*
374 * public API
375 */
377 int
378 sdb_store_host(const char *name, sdb_time_t last_update)
379 {
380 int status;
382 if (! name)
383 return -1;
385 pthread_rwlock_wrlock(&obj_lock);
386 status = store_obj(/* parent = */ 0, NULL,
387 /* stored object = */ SDB_HOST, name, last_update,
388 /* updated_obj = */ NULL);
389 pthread_rwlock_unlock(&obj_lock);
390 return status;
391 } /* sdb_store_host */
393 _Bool
394 sdb_store_has_host(const char *name)
395 {
396 sdb_store_obj_t *host;
398 if (! name)
399 return NULL;
401 host = sdb_store_lookup(SDB_HOST, name);
402 return host != NULL;
403 } /* sdb_store_has_host */
405 int
406 sdb_store_attribute(const char *hostname, const char *key, const char *value,
407 sdb_time_t last_update)
408 {
409 int status;
411 store_obj_t *updated_attr = NULL;
413 if ((! hostname) || (! key))
414 return -1;
416 pthread_rwlock_wrlock(&obj_lock);
417 status = store_obj(/* parent = */ SDB_HOST, hostname,
418 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
419 &updated_attr);
421 SDB_ATTR(updated_attr)->value = strdup(value);
422 if (! SDB_ATTR(updated_attr)->value) {
423 sdb_object_deref(SDB_OBJ(updated_attr));
424 status = -1;
425 }
426 pthread_rwlock_unlock(&obj_lock);
427 return status;
428 } /* sdb_store_attribute */
430 int
431 sdb_store_service(const char *hostname, const char *name,
432 sdb_time_t last_update)
433 {
434 int status;
436 if ((! hostname) || (! name))
437 return -1;
439 pthread_rwlock_wrlock(&obj_lock);
440 status = store_obj(/* parent = */ SDB_HOST, hostname,
441 /* stored object = */ SDB_SERVICE, name, last_update,
442 /* updated obj = */ NULL);
443 pthread_rwlock_unlock(&obj_lock);
444 return status;
445 } /* sdb_store_service */
447 /* TODO: actually support hierarchical data */
448 int
449 sdb_store_dump(FILE *fh)
450 {
451 sdb_llist_iter_t *host_iter;
453 if (! fh)
454 return -1;
456 pthread_rwlock_rdlock(&obj_lock);
458 host_iter = sdb_llist_get_iter(obj_list);
459 if (! host_iter) {
460 pthread_rwlock_unlock(&obj_lock);
461 return -1;
462 }
464 while (sdb_llist_iter_has_next(host_iter)) {
465 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
466 sdb_llist_iter_t *svc_iter;
467 sdb_llist_iter_t *attr_iter;
469 char time_str[64];
471 assert(host);
473 if (! sdb_strftime(time_str, sizeof(time_str),
474 "%F %T %z", host->_last_update))
475 snprintf(time_str, sizeof(time_str), "<error>");
476 time_str[sizeof(time_str) - 1] = '\0';
478 fprintf(fh, "Host '%s' (last updated: %s):\n",
479 SDB_OBJ(host)->name, time_str);
481 attr_iter = sdb_llist_get_iter(host->attributes);
482 if (! attr_iter) {
483 char errbuf[1024];
484 fprintf(fh, "Failed to retrieve attributes: %s\n",
485 sdb_strerror(errno, errbuf, sizeof(errbuf)));
486 continue;
487 }
489 while (sdb_llist_iter_has_next(attr_iter)) {
490 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
491 assert(attr);
493 if (! sdb_strftime(time_str, sizeof(time_str),
494 "%F %T %z", attr->_last_update))
495 snprintf(time_str, sizeof(time_str), "<error>");
496 time_str[sizeof(time_str) - 1] = '\0';
498 fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
499 SDB_OBJ(attr)->name, attr->value, time_str);
500 }
502 sdb_llist_iter_destroy(attr_iter);
504 svc_iter = sdb_llist_get_iter(host->children);
505 if (! svc_iter) {
506 char errbuf[1024];
507 fprintf(fh, "Failed to retrieve services: %s\n",
508 sdb_strerror(errno, errbuf, sizeof(errbuf)));
509 continue;
510 }
512 while (sdb_llist_iter_has_next(svc_iter)) {
513 sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
514 assert(svc);
516 if (! sdb_strftime(time_str, sizeof(time_str),
517 "%F %T %z", svc->_last_update))
518 snprintf(time_str, sizeof(time_str), "<error>");
519 time_str[sizeof(time_str) - 1] = '\0';
521 fprintf(fh, "\tService '%s' (last updated: %s)\n",
522 SDB_OBJ(svc)->name, time_str);
523 }
525 sdb_llist_iter_destroy(svc_iter);
526 }
528 sdb_llist_iter_destroy(host_iter);
529 pthread_rwlock_unlock(&obj_lock);
530 return 0;
531 } /* sdb_store_dump */
533 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */