Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / src / core / store.c
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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         if (status >= 0) {
422                 assert(updated_attr);
423                 SDB_ATTR(updated_attr)->value = strdup(value);
424                 if (! SDB_ATTR(updated_attr)->value) {
425                         sdb_object_deref(SDB_OBJ(updated_attr));
426                         status = -1;
427                 }
428         }
430         pthread_rwlock_unlock(&obj_lock);
431         return status;
432 } /* sdb_store_attribute */
434 int
435 sdb_store_service(const char *hostname, const char *name,
436                 sdb_time_t last_update)
438         int status;
440         if ((! hostname) || (! name))
441                 return -1;
443         pthread_rwlock_wrlock(&obj_lock);
444         status = store_obj(/* parent = */ SDB_HOST, hostname,
445                         /* stored object = */ SDB_SERVICE, name, last_update,
446                         /* updated obj = */ NULL);
447         pthread_rwlock_unlock(&obj_lock);
448         return status;
449 } /* sdb_store_service */
451 /* TODO: actually support hierarchical data */
452 int
453 sdb_store_tojson(sdb_strbuf_t *buf)
455         sdb_llist_iter_t *host_iter;
457         if (! buf)
458                 return -1;
460         pthread_rwlock_rdlock(&obj_lock);
462         host_iter = sdb_llist_get_iter(obj_list);
463         if (! host_iter) {
464                 pthread_rwlock_unlock(&obj_lock);
465                 return -1;
466         }
468         sdb_strbuf_append(buf, "{\"hosts\":[");
470         while (sdb_llist_iter_has_next(host_iter)) {
471                 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
472                 sdb_llist_iter_t *svc_iter;
473                 sdb_llist_iter_t *attr_iter;
475                 char time_str[64];
477                 assert(host);
479                 if (! sdb_strftime(time_str, sizeof(time_str),
480                                         "%F %T %z", host->_last_update))
481                         snprintf(time_str, sizeof(time_str), "<error>");
482                 time_str[sizeof(time_str) - 1] = '\0';
484                 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
485                                 "\"last_update\": \"%s\", "
486                                 "\"attributes\": [",
487                                 SDB_OBJ(host)->name, time_str);
489                 attr_iter = sdb_llist_get_iter(host->attributes);
490                 if (! attr_iter) {
491                         char errbuf[1024];
492                         sdb_log(SDB_LOG_ERR, "store: Failed to retrieve attributes: %s\n",
493                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
494                         sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve "
495                                         "attributes: %s\"}", errbuf);
496                 }
498                 /* has_next returns false if the iterator is NULL */
499                 while (sdb_llist_iter_has_next(attr_iter)) {
500                         sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
501                         assert(attr);
503                         if (! sdb_strftime(time_str, sizeof(time_str),
504                                                 "%F %T %z", attr->_last_update))
505                                 snprintf(time_str, sizeof(time_str), "<error>");
506                         time_str[sizeof(time_str) - 1] = '\0';
508                         sdb_strbuf_append(buf, "{\"name\": \"%s\", "
509                                         "\"value\": \"%s\", \"last_update\": \"%s\"},",
510                                         SDB_OBJ(attr)->name, attr->value, time_str);
511                 }
513                 sdb_llist_iter_destroy(attr_iter);
514                 sdb_strbuf_append(buf, "], \"services\": [");
516                 svc_iter = sdb_llist_get_iter(host->children);
517                 if (! svc_iter) {
518                         char errbuf[1024];
519                         sdb_log(SDB_LOG_ERR, "store: Failed to retrieve services: %s\n",
520                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
521                         sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve "
522                                         "services: %s\"}", errbuf);
523                 }
525                 while (sdb_llist_iter_has_next(svc_iter)) {
526                         sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
527                         assert(svc);
529                         if (! sdb_strftime(time_str, sizeof(time_str),
530                                                 "%F %T %z", svc->_last_update))
531                                 snprintf(time_str, sizeof(time_str), "<error>");
532                         time_str[sizeof(time_str) - 1] = '\0';
534                         sdb_strbuf_append(buf, "{\"name\": \"%s\", "
535                                         "\"last_update\": \"%s\"},",
536                                         SDB_OBJ(svc)->name, time_str);
537                 }
539                 sdb_llist_iter_destroy(svc_iter);
540                 sdb_strbuf_append(buf, "]},");
541         }
543         sdb_strbuf_append(buf, "]}");
545         sdb_llist_iter_destroy(host_iter);
546         pthread_rwlock_unlock(&obj_lock);
547         return 0;
548 } /* sdb_store_tojson */
550 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */