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-private.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 static int
59 store_base_init(sdb_object_t *obj, va_list ap)
60 {
61 sdb_store_base_t *sobj = STORE_BASE(obj);
63 sobj->type = va_arg(ap, int);
65 sobj->last_update = va_arg(ap, sdb_time_t);
66 sobj->parent = NULL;
67 return 0;
68 } /* store_base_init */
70 static void
71 store_base_destroy(sdb_object_t *obj)
72 {
73 const sdb_store_base_t *sobj = STORE_CONST_BASE(obj);
75 if (sobj->parent)
76 sdb_object_deref(SDB_OBJ(sobj->parent));
77 } /* store_base_destroy */
79 static int
80 sdb_store_obj_init(sdb_object_t *obj, va_list ap)
81 {
82 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
83 int ret;
85 /* this will consume the first argument (type) of ap */
86 ret = store_base_init(obj, ap);
87 if (ret)
88 return ret;
90 sobj->children = sdb_llist_create();
91 if (! sobj->children)
92 return -1;
93 sobj->attributes = sdb_llist_create();
94 if (! sobj->attributes)
95 return -1;
96 return 0;
97 } /* sdb_store_obj_init */
99 static void
100 sdb_store_obj_destroy(sdb_object_t *obj)
101 {
102 sdb_store_obj_t *sobj = SDB_STORE_OBJ(obj);
104 assert(obj);
106 store_base_destroy(obj);
108 if (sobj->children)
109 sdb_llist_destroy(sobj->children);
110 if (sobj->attributes)
111 sdb_llist_destroy(sobj->attributes);
112 } /* sdb_store_obj_destroy */
114 static int
115 sdb_attr_init(sdb_object_t *obj, va_list ap)
116 {
117 const sdb_data_t *value;
118 int ret;
120 /* this will consume the first two arguments
121 * (type and last_update) of ap */
122 ret = store_base_init(obj, ap);
123 if (ret)
124 return ret;
125 value = va_arg(ap, const sdb_data_t *);
127 if (value)
128 if (sdb_data_copy(&SDB_ATTR(obj)->value, value))
129 return -1;
130 return 0;
131 } /* sdb_attr_init */
133 static void
134 sdb_attr_destroy(sdb_object_t *obj)
135 {
136 assert(obj);
138 store_base_destroy(obj);
139 sdb_data_free_datum(&SDB_ATTR(obj)->value);
140 } /* sdb_attr_destroy */
142 static sdb_type_t sdb_store_obj_type = {
143 sizeof(sdb_store_obj_t),
145 sdb_store_obj_init,
146 sdb_store_obj_destroy
147 };
149 static sdb_type_t sdb_attribute_type = {
150 sizeof(sdb_attribute_t),
152 sdb_attr_init,
153 sdb_attr_destroy
154 };
156 /*
157 * private helper functions
158 */
160 static sdb_store_obj_t *
161 store_lookup_in_list(sdb_llist_t *l, int type, const char *name)
162 {
163 sdb_llist_iter_t *iter;
165 if (! l)
166 return NULL;
168 iter = sdb_llist_get_iter(l);
169 if (! iter)
170 return NULL;
172 while (sdb_llist_iter_has_next(iter)) {
173 sdb_store_obj_t *sobj = SDB_STORE_OBJ(sdb_llist_iter_get_next(iter));
174 assert(sobj);
176 if ((STORE_BASE(sobj)->type == type)
177 && (! strcasecmp(SDB_OBJ(sobj)->name, name))) {
178 sdb_llist_iter_destroy(iter);
179 return sobj;
180 }
182 /* don't lookups non-host types from hierarchical hosts */
183 if ((type != SDB_HOST) && (STORE_BASE(sobj)->type == SDB_HOST))
184 continue;
186 sobj = store_lookup_in_list(sobj->children, type, name);
187 if (sobj) {
188 sdb_llist_iter_destroy(iter);
189 return sobj;
190 }
191 }
192 sdb_llist_iter_destroy(iter);
193 return NULL;
194 } /* store_lookup_in_list */
196 static sdb_store_obj_t *
197 store_lookup(int type, const char *name)
198 {
199 return store_lookup_in_list(obj_list, type, name);
200 } /* store_lookup */
202 /* The obj_lock has to be acquired before calling this function. */
203 static int
204 store_obj(int parent_type, const char *parent_name,
205 int type, const char *name, sdb_time_t last_update,
206 sdb_store_base_t **updated_obj)
207 {
208 char *parent_cname = NULL, *cname = NULL;
210 sdb_llist_t *parent_list;
211 sdb_store_base_t *old;
212 int status = 0;
214 if (last_update <= 0)
215 last_update = sdb_gettime();
217 assert((parent_type == 0)
218 || (parent_type == SDB_HOST)
219 || (parent_type == SDB_SERVICE));
220 assert((type == 0)
221 || (type == SDB_HOST)
222 || (type == SDB_SERVICE)
223 || (type == SDB_ATTRIBUTE));
225 if (parent_type == SDB_HOST) {
226 parent_cname = sdb_plugin_cname(strdup(parent_name));
227 if (! parent_cname) {
228 sdb_log(SDB_LOG_ERR, "store: strdup failed");
229 return -1;
230 }
231 parent_name = parent_cname;
232 }
233 if (type == SDB_HOST) {
234 cname = sdb_plugin_cname(strdup(name));
235 if (! cname) {
236 sdb_log(SDB_LOG_ERR, "store: strdup failed");
237 return -1;
238 }
239 name = cname;
240 }
242 if (! obj_list) {
243 if (! (obj_list = sdb_llist_create())) {
244 free(parent_cname);
245 free(cname);
246 return -1;
247 }
248 }
249 parent_list = obj_list;
251 if (parent_type && parent_name) {
252 sdb_store_obj_t *parent;
254 parent = store_lookup(parent_type, parent_name);
255 if (! parent) {
256 sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
257 "parent %s '%s' not found", TYPE_TO_NAME(type), name,
258 TYPE_TO_NAME(parent_type), parent_name);
259 free(parent_cname);
260 free(cname);
261 return -1;
262 }
264 if (type == SDB_ATTRIBUTE)
265 parent_list = parent->attributes;
266 else
267 parent_list = parent->children;
268 }
270 if (type == SDB_HOST)
271 /* make sure that each host is unique */
272 old = STORE_BASE(store_lookup_in_list(obj_list, type, name));
273 else if (type == SDB_ATTRIBUTE)
274 /* look into attributes of this host */
275 old = STORE_BASE(sdb_llist_search_by_name(parent_list, name));
276 else
277 /* look into services assigned to this host (store_lookup_in_list
278 * does not look up services from hierarchical hosts) */
279 old = STORE_BASE(store_lookup_in_list(parent_list, type, name));
281 if (old) {
282 if (old->last_update > last_update) {
283 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
284 "value too old (%"PRIscTIME" < %"PRIscTIME")",
285 TYPE_TO_NAME(type), name, last_update, old->last_update);
286 /* don't report an error; the object may be updated by multiple
287 * backends */
288 status = 1;
289 }
290 else {
291 old->last_update = last_update;
292 }
294 if (updated_obj)
295 *updated_obj = old;
296 }
297 else {
298 sdb_store_base_t *new;
300 if (type == SDB_ATTRIBUTE)
301 /* the value will be updated by the caller */
302 new = STORE_BASE(sdb_object_create(name, sdb_attribute_type,
303 type, last_update, NULL));
304 else
305 new = STORE_BASE(sdb_object_create(name, sdb_store_obj_type,
306 type, last_update));
308 if (! new) {
309 char errbuf[1024];
310 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
311 TYPE_TO_NAME(type), name,
312 sdb_strerror(errno, errbuf, sizeof(errbuf)));
313 free(parent_cname);
314 free(cname);
315 return -1;
316 }
318 /* TODO: insert type-aware; the current version works as long as we
319 * don't support to store hierarchical data */
320 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
321 sdb_object_cmp_by_name);
323 /* pass control to the list or destroy in case of an error */
324 sdb_object_deref(SDB_OBJ(new));
326 if (updated_obj)
327 *updated_obj = new;
328 }
329 free(parent_cname);
330 free(cname);
331 return status;
332 } /* store_obj */
334 /*
335 * store_obj_tojson serializes attribute / service objects to JSON.
336 *
337 * The function never returns an error. Rather, an error message will be part
338 * of the serialized data.
339 */
340 static void
341 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
342 {
343 sdb_llist_iter_t *iter;
344 char time_str[64];
346 assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
348 sdb_strbuf_append(buf, "[");
350 iter = sdb_llist_get_iter(list);
351 if (! iter) {
352 char errbuf[1024];
353 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
354 TYPE_TO_NAME(type),
355 sdb_strerror(errno, errbuf, sizeof(errbuf)));
356 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
357 TYPE_TO_NAME(type), errbuf);
358 }
360 /* has_next returns false if the iterator is NULL */
361 while (sdb_llist_iter_has_next(iter)) {
362 sdb_store_base_t *sobj = STORE_BASE(sdb_llist_iter_get_next(iter));
363 assert(sobj);
365 if (! sdb_strftime(time_str, sizeof(time_str),
366 "%F %T %z", sobj->last_update))
367 snprintf(time_str, sizeof(time_str), "<error>");
368 time_str[sizeof(time_str) - 1] = '\0';
370 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
371 if (type == SDB_ATTRIBUTE) {
372 char tmp[sdb_data_strlen(&SDB_ATTR(sobj)->value) + 1];
373 sdb_data_format(&SDB_ATTR(sobj)->value, tmp, sizeof(tmp),
374 SDB_DOUBLE_QUOTED);
375 sdb_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\"}",
376 tmp, time_str);
377 }
378 else
379 sdb_strbuf_append(buf, "\"last_update\": \"%s\"}", time_str);
381 if (sdb_llist_iter_has_next(iter))
382 sdb_strbuf_append(buf, ",");
383 }
385 sdb_llist_iter_destroy(iter);
386 sdb_strbuf_append(buf, "]");
387 } /* store_obj_tojson */
389 /*
390 * public API
391 */
393 void
394 sdb_store_clear(void)
395 {
396 sdb_llist_destroy(obj_list);
397 obj_list = NULL;
398 } /* sdb_store_clear */
400 int
401 sdb_store_host(const char *name, sdb_time_t last_update)
402 {
403 int status;
405 if (! name)
406 return -1;
408 pthread_rwlock_wrlock(&obj_lock);
409 status = store_obj(/* parent = */ 0, NULL,
410 /* stored object = */ SDB_HOST, name, last_update,
411 /* updated_obj = */ NULL);
412 pthread_rwlock_unlock(&obj_lock);
413 return status;
414 } /* sdb_store_host */
416 _Bool
417 sdb_store_has_host(const char *name)
418 {
419 sdb_store_obj_t *host;
421 if (! name)
422 return NULL;
424 host = store_lookup(SDB_HOST, name);
425 return host != NULL;
426 } /* sdb_store_has_host */
428 sdb_store_base_t *
429 sdb_store_get_host(const char *name)
430 {
431 sdb_store_obj_t *host;
433 if (! name)
434 return NULL;
436 host = store_lookup(SDB_HOST, name);
437 if (! host)
438 return NULL;
440 sdb_object_ref(SDB_OBJ(host));
441 return STORE_BASE(host);
442 } /* sdb_store_get_host */
444 int
445 sdb_store_attribute(const char *hostname,
446 const char *key, const sdb_data_t *value,
447 sdb_time_t last_update)
448 {
449 int status;
451 sdb_store_base_t *updated_attr = NULL;
453 if ((! hostname) || (! key))
454 return -1;
456 pthread_rwlock_wrlock(&obj_lock);
457 status = store_obj(/* parent = */ SDB_HOST, hostname,
458 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
459 &updated_attr);
461 if (status >= 0) {
462 assert(updated_attr);
463 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
464 sdb_object_deref(SDB_OBJ(updated_attr));
465 status = -1;
466 }
467 }
469 pthread_rwlock_unlock(&obj_lock);
470 return status;
471 } /* sdb_store_attribute */
473 int
474 sdb_store_service(const char *hostname, const char *name,
475 sdb_time_t last_update)
476 {
477 int status;
479 if ((! hostname) || (! name))
480 return -1;
482 pthread_rwlock_wrlock(&obj_lock);
483 status = store_obj(/* parent = */ SDB_HOST, hostname,
484 /* stored object = */ SDB_SERVICE, name, last_update,
485 /* updated obj = */ NULL);
486 pthread_rwlock_unlock(&obj_lock);
487 return status;
488 } /* sdb_store_service */
490 int
491 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
492 {
493 sdb_store_obj_t *host;
494 char time_str[64];
496 if ((! h) || (h->type != SDB_HOST) || (! buf))
497 return -1;
499 host = SDB_STORE_OBJ(h);
501 if (! sdb_strftime(time_str, sizeof(time_str),
502 "%F %T %z", host->_last_update))
503 snprintf(time_str, sizeof(time_str), "<error>");
504 time_str[sizeof(time_str) - 1] = '\0';
506 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
507 "\"last_update\": \"%s\"",
508 SDB_OBJ(host)->name, time_str);
510 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
511 sdb_strbuf_append(buf, ", \"attributes\": ");
512 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
513 }
515 if (! (flags & SDB_SKIP_SERVICES)) {
516 sdb_strbuf_append(buf, ", \"services\": ");
517 store_obj_tojson(host->children, SDB_SERVICE, buf);
518 }
520 sdb_strbuf_append(buf, "}");
521 return 0;
522 } /* sdb_store_host_tojson */
524 /* TODO: actually support hierarchical data */
525 int
526 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
527 {
528 sdb_llist_iter_t *host_iter;
530 if (! buf)
531 return -1;
533 pthread_rwlock_rdlock(&obj_lock);
535 host_iter = sdb_llist_get_iter(obj_list);
536 if (! host_iter) {
537 pthread_rwlock_unlock(&obj_lock);
538 return -1;
539 }
541 sdb_strbuf_append(buf, "{\"hosts\":[");
543 while (sdb_llist_iter_has_next(host_iter)) {
544 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
545 assert(host);
547 if (sdb_store_host_tojson(host, buf, flags))
548 return -1;
550 if (sdb_llist_iter_has_next(host_iter))
551 sdb_strbuf_append(buf, ",");
552 }
554 sdb_strbuf_append(buf, "]}");
556 sdb_llist_iter_destroy(host_iter);
557 pthread_rwlock_unlock(&obj_lock);
558 return 0;
559 } /* sdb_store_tojson */
561 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */