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 sdb_strbuf_append(buf, "\"value\": ");
373 sdb_data_format(&SDB_ATTR(sobj)->value, buf);
374 sdb_strbuf_append(buf, ", \"last_update\": \"%s\"}", time_str);
375 }
376 else
377 sdb_strbuf_append(buf, "\"last_update\": \"%s\"}", time_str);
379 if (sdb_llist_iter_has_next(iter))
380 sdb_strbuf_append(buf, ",");
381 }
383 sdb_llist_iter_destroy(iter);
384 sdb_strbuf_append(buf, "]");
385 } /* store_obj_tojson */
387 /*
388 * public API
389 */
391 int
392 sdb_store_host(const char *name, sdb_time_t last_update)
393 {
394 int status;
396 if (! name)
397 return -1;
399 pthread_rwlock_wrlock(&obj_lock);
400 status = store_obj(/* parent = */ 0, NULL,
401 /* stored object = */ SDB_HOST, name, last_update,
402 /* updated_obj = */ NULL);
403 pthread_rwlock_unlock(&obj_lock);
404 return status;
405 } /* sdb_store_host */
407 _Bool
408 sdb_store_has_host(const char *name)
409 {
410 sdb_store_obj_t *host;
412 if (! name)
413 return NULL;
415 host = store_lookup(SDB_HOST, name);
416 return host != NULL;
417 } /* sdb_store_has_host */
419 sdb_store_base_t *
420 sdb_store_get_host(const char *name)
421 {
422 sdb_store_obj_t *host;
424 if (! name)
425 return NULL;
427 host = store_lookup(SDB_HOST, name);
428 if (! host)
429 return NULL;
431 sdb_object_ref(SDB_OBJ(host));
432 return STORE_BASE(host);
433 } /* sdb_store_get_host */
435 int
436 sdb_store_attribute(const char *hostname,
437 const char *key, const sdb_data_t *value,
438 sdb_time_t last_update)
439 {
440 int status;
442 sdb_store_base_t *updated_attr = NULL;
444 if ((! hostname) || (! key))
445 return -1;
447 pthread_rwlock_wrlock(&obj_lock);
448 status = store_obj(/* parent = */ SDB_HOST, hostname,
449 /* stored object = */ SDB_ATTRIBUTE, key, last_update,
450 &updated_attr);
452 if (status >= 0) {
453 assert(updated_attr);
454 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
455 sdb_object_deref(SDB_OBJ(updated_attr));
456 status = -1;
457 }
458 }
460 pthread_rwlock_unlock(&obj_lock);
461 return status;
462 } /* sdb_store_attribute */
464 int
465 sdb_store_service(const char *hostname, const char *name,
466 sdb_time_t last_update)
467 {
468 int status;
470 if ((! hostname) || (! name))
471 return -1;
473 pthread_rwlock_wrlock(&obj_lock);
474 status = store_obj(/* parent = */ SDB_HOST, hostname,
475 /* stored object = */ SDB_SERVICE, name, last_update,
476 /* updated obj = */ NULL);
477 pthread_rwlock_unlock(&obj_lock);
478 return status;
479 } /* sdb_store_service */
481 int
482 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
483 {
484 sdb_store_obj_t *host;
485 char time_str[64];
487 if ((! h) || (h->type != SDB_HOST) || (! buf))
488 return -1;
490 host = SDB_STORE_OBJ(h);
492 if (! sdb_strftime(time_str, sizeof(time_str),
493 "%F %T %z", host->_last_update))
494 snprintf(time_str, sizeof(time_str), "<error>");
495 time_str[sizeof(time_str) - 1] = '\0';
497 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
498 "\"last_update\": \"%s\"",
499 SDB_OBJ(host)->name, time_str);
501 if (! (flags & SDB_SKIP_ATTRIBUTES)) {
502 sdb_strbuf_append(buf, ", \"attributes\": ");
503 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
504 }
506 if (! (flags & SDB_SKIP_SERVICES)) {
507 sdb_strbuf_append(buf, ", \"services\": ");
508 store_obj_tojson(host->children, SDB_SERVICE, buf);
509 }
511 sdb_strbuf_append(buf, "}");
512 return 0;
513 } /* sdb_store_host_tojson */
515 /* TODO: actually support hierarchical data */
516 int
517 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
518 {
519 sdb_llist_iter_t *host_iter;
521 if (! buf)
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 sdb_strbuf_append(buf, "{\"hosts\":[");
534 while (sdb_llist_iter_has_next(host_iter)) {
535 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
536 assert(host);
538 if (sdb_store_host_tojson(host, buf, flags))
539 return -1;
541 if (sdb_llist_iter_has_next(host_iter))
542 sdb_strbuf_append(buf, ",");
543 }
545 sdb_strbuf_append(buf, "]}");
547 sdb_llist_iter_destroy(host_iter);
548 pthread_rwlock_unlock(&obj_lock);
549 return 0;
550 } /* sdb_store_tojson */
552 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */