Code

ed5b7ceb5095c683bdb2a3c775c67ff389950ff7
[sysdb.git] / src / core / store.c
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.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 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                 /* don't lookups non-host types from hierarchical hosts */
228                 if ((type != SDB_HOST) && (sobj->type == SDB_HOST))
229                         continue;
231                 sobj = sdb_store_lookup_in_list(sobj->children, type, name);
232                 if (sobj) {
233                         sdb_llist_iter_destroy(iter);
234                         return sobj;
235                 }
236         }
237         sdb_llist_iter_destroy(iter);
238         return NULL;
239 } /* sdb_store_lookup_in_list */
241 static sdb_store_obj_t *
242 sdb_store_lookup(int type, const char *name)
244         return sdb_store_lookup_in_list(obj_list, type, name);
245 } /* sdb_store_lookup */
247 /* The obj_lock has to be acquired before calling this function. */
248 static int
249 store_obj(int parent_type, const char *parent_name,
250                 int type, const char *name, sdb_time_t last_update,
251                 store_obj_t **updated_obj)
253         char *parent_cname = NULL, *cname = NULL;
255         sdb_llist_t *parent_list;
256         store_obj_t *old;
257         int status = 0;
259         if (last_update <= 0)
260                 last_update = sdb_gettime();
262         assert((parent_type == 0)
263                         || (parent_type == SDB_HOST)
264                         || (parent_type == SDB_SERVICE));
265         assert((type == 0)
266                         || (type == SDB_HOST)
267                         || (type == SDB_SERVICE)
268                         || (type == SDB_ATTRIBUTE));
270         if (parent_type == SDB_HOST) {
271                 parent_cname = sdb_plugin_cname(strdup(parent_name));
272                 if (! parent_cname) {
273                         sdb_log(SDB_LOG_ERR, "store: strdup failed");
274                         return -1;
275                 }
276                 parent_name = parent_cname;
277         }
278         if (type == SDB_HOST) {
279                 cname = sdb_plugin_cname(strdup(name));
280                 if (! cname) {
281                         sdb_log(SDB_LOG_ERR, "store: strdup failed");
282                         return -1;
283                 }
284                 name = cname;
285         }
287         if (! obj_list) {
288                 if (! (obj_list = sdb_llist_create())) {
289                         free(parent_cname);
290                         free(cname);
291                         return -1;
292                 }
293         }
294         parent_list = obj_list;
296         if (parent_type && parent_name) {
297                 sdb_store_obj_t *parent;
299                 parent = sdb_store_lookup(parent_type, parent_name);
300                 if (! parent) {
301                         sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
302                                         "parent %s '%s' not found", TYPE_TO_NAME(type), name,
303                                         TYPE_TO_NAME(parent_type), parent_name);
304                         free(parent_cname);
305                         free(cname);
306                         return -1;
307                 }
309                 if (type == SDB_ATTRIBUTE)
310                         parent_list = parent->attributes;
311                 else
312                         parent_list = parent->children;
313         }
315         if (type == SDB_HOST)
316                 /* make sure that each host is unique */
317                 old = STORE_OBJ(sdb_store_lookup_in_list(obj_list, type, name));
318         else if (type == SDB_ATTRIBUTE)
319                 /* look into attributes of this host */
320                 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
321         else
322                 /* look into services assigned to this host (sdb_store_lookup_in_list
323                  * does not look up services from hierarchical hosts) */
324                 old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
326         if (old) {
327                 if (old->last_update > last_update) {
328                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
329                                         "value too old (%"PRIscTIME" < %"PRIscTIME")",
330                                         TYPE_TO_NAME(type), name, last_update, old->last_update);
331                         /* don't report an error; the object may be updated by multiple
332                          * backends */
333                         status = 1;
334                 }
335                 else {
336                         old->last_update = last_update;
337                 }
339                 if (updated_obj)
340                         *updated_obj = old;
341         }
342         else {
343                 store_obj_t *new;
345                 if (type == SDB_ATTRIBUTE)
346                         /* the value will be updated by the caller */
347                         new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
348                                                 last_update, NULL));
349                 else
350                         new = STORE_OBJ(sdb_object_create(name, sdb_store_obj_type,
351                                                 last_update, type));
353                 if (! new) {
354                         char errbuf[1024];
355                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
356                                         TYPE_TO_NAME(type), name,
357                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
358                         free(parent_cname);
359                         free(cname);
360                         return -1;
361                 }
363                 /* TODO: insert type-aware; the current version works as long as we
364                  * don't support to store hierarchical data */
365                 status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
366                                 sdb_object_cmp_by_name);
368                 /* pass control to the list or destroy in case of an error */
369                 sdb_object_deref(SDB_OBJ(new));
371                 if (updated_obj)
372                         *updated_obj = new;
373         }
374         free(parent_cname);
375         free(cname);
376         return status;
377 } /* sdb_store_obj */
379 /*
380  * public API
381  */
383 int
384 sdb_store_host(const char *name, sdb_time_t last_update)
386         int status;
388         if (! name)
389                 return -1;
391         pthread_rwlock_wrlock(&obj_lock);
392         status = store_obj(/* parent = */ 0, NULL,
393                         /* stored object = */ SDB_HOST, name, last_update,
394                         /* updated_obj = */ NULL);
395         pthread_rwlock_unlock(&obj_lock);
396         return status;
397 } /* sdb_store_host */
399 _Bool
400 sdb_store_has_host(const char *name)
402         sdb_store_obj_t *host;
404         if (! name)
405                 return NULL;
407         host = sdb_store_lookup(SDB_HOST, name);
408         return host != NULL;
409 } /* sdb_store_has_host */
411 int
412 sdb_store_attribute(const char *hostname, const char *key, const char *value,
413                 sdb_time_t last_update)
415         int status;
417         store_obj_t *updated_attr = NULL;
419         if ((! hostname) || (! key))
420                 return -1;
422         pthread_rwlock_wrlock(&obj_lock);
423         status = store_obj(/* parent = */ SDB_HOST, hostname,
424                         /* stored object = */ SDB_ATTRIBUTE, key, last_update,
425                         &updated_attr);
427         if (status >= 0) {
428                 assert(updated_attr);
429                 SDB_ATTR(updated_attr)->value = strdup(value);
430                 if (! SDB_ATTR(updated_attr)->value) {
431                         sdb_object_deref(SDB_OBJ(updated_attr));
432                         status = -1;
433                 }
434         }
436         pthread_rwlock_unlock(&obj_lock);
437         return status;
438 } /* sdb_store_attribute */
440 int
441 sdb_store_service(const char *hostname, const char *name,
442                 sdb_time_t last_update)
444         int status;
446         if ((! hostname) || (! name))
447                 return -1;
449         pthread_rwlock_wrlock(&obj_lock);
450         status = store_obj(/* parent = */ SDB_HOST, hostname,
451                         /* stored object = */ SDB_SERVICE, name, last_update,
452                         /* updated obj = */ NULL);
453         pthread_rwlock_unlock(&obj_lock);
454         return status;
455 } /* sdb_store_service */
457 /* TODO: actually support hierarchical data */
458 int
459 sdb_store_tojson(sdb_strbuf_t *buf)
461         sdb_llist_iter_t *host_iter;
463         if (! buf)
464                 return -1;
466         pthread_rwlock_rdlock(&obj_lock);
468         host_iter = sdb_llist_get_iter(obj_list);
469         if (! host_iter) {
470                 pthread_rwlock_unlock(&obj_lock);
471                 return -1;
472         }
474         sdb_strbuf_append(buf, "{\"hosts\":[");
476         while (sdb_llist_iter_has_next(host_iter)) {
477                 sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
478                 sdb_llist_iter_t *svc_iter;
479                 sdb_llist_iter_t *attr_iter;
481                 char time_str[64];
483                 assert(host);
485                 if (! sdb_strftime(time_str, sizeof(time_str),
486                                         "%F %T %z", host->_last_update))
487                         snprintf(time_str, sizeof(time_str), "<error>");
488                 time_str[sizeof(time_str) - 1] = '\0';
490                 sdb_strbuf_append(buf, "{\"name\": \"%s\", "
491                                 "\"last_update\": \"%s\", "
492                                 "\"attributes\": [",
493                                 SDB_OBJ(host)->name, time_str);
495                 attr_iter = sdb_llist_get_iter(host->attributes);
496                 if (! attr_iter) {
497                         char errbuf[1024];
498                         sdb_log(SDB_LOG_ERR, "store: Failed to retrieve attributes: %s\n",
499                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
500                         sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve "
501                                         "attributes: %s\"}", errbuf);
502                 }
504                 /* has_next returns false if the iterator is NULL */
505                 while (sdb_llist_iter_has_next(attr_iter)) {
506                         sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
507                         assert(attr);
509                         if (! sdb_strftime(time_str, sizeof(time_str),
510                                                 "%F %T %z", attr->_last_update))
511                                 snprintf(time_str, sizeof(time_str), "<error>");
512                         time_str[sizeof(time_str) - 1] = '\0';
514                         sdb_strbuf_append(buf, "{\"name\": \"%s\", "
515                                         "\"value\": \"%s\", \"last_update\": \"%s\"},",
516                                         SDB_OBJ(attr)->name, attr->value, time_str);
517                 }
519                 sdb_llist_iter_destroy(attr_iter);
520                 sdb_strbuf_append(buf, "], \"services\": [");
522                 svc_iter = sdb_llist_get_iter(host->children);
523                 if (! svc_iter) {
524                         char errbuf[1024];
525                         sdb_log(SDB_LOG_ERR, "store: Failed to retrieve services: %s\n",
526                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
527                         sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve "
528                                         "services: %s\"}", errbuf);
529                 }
531                 while (sdb_llist_iter_has_next(svc_iter)) {
532                         sdb_store_obj_t *svc = SDB_STORE_OBJ(sdb_llist_iter_get_next(svc_iter));
533                         assert(svc);
535                         if (! sdb_strftime(time_str, sizeof(time_str),
536                                                 "%F %T %z", svc->_last_update))
537                                 snprintf(time_str, sizeof(time_str), "<error>");
538                         time_str[sizeof(time_str) - 1] = '\0';
540                         sdb_strbuf_append(buf, "{\"name\": \"%s\", "
541                                         "\"last_update\": \"%s\"},",
542                                         SDB_OBJ(svc)->name, time_str);
543                 }
545                 sdb_llist_iter_destroy(svc_iter);
546                 sdb_strbuf_append(buf, "]},");
547         }
549         sdb_strbuf_append(buf, "]}");
551         sdb_llist_iter_destroy(host_iter);
552         pthread_rwlock_unlock(&obj_lock);
553         return 0;
554 } /* sdb_store_tojson */
556 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */