Code

data: Let sdb_data_format output to a character array.
[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-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)
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)
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)
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)
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)
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)
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)
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_strbuf_append(buf, "\"value\": %s, \"last_update\": \"%s\"}",
375                                         tmp, time_str);
376                 }
377                 else
378                         sdb_strbuf_append(buf, "\"last_update\": \"%s\"}", time_str);
380                 if (sdb_llist_iter_has_next(iter))
381                         sdb_strbuf_append(buf, ",");
382         }
384         sdb_llist_iter_destroy(iter);
385         sdb_strbuf_append(buf, "]");
386 } /* store_obj_tojson */
388 /*
389  * public API
390  */
392 int
393 sdb_store_host(const char *name, sdb_time_t last_update)
395         int status;
397         if (! name)
398                 return -1;
400         pthread_rwlock_wrlock(&obj_lock);
401         status = store_obj(/* parent = */ 0, NULL,
402                         /* stored object = */ SDB_HOST, name, last_update,
403                         /* updated_obj = */ NULL);
404         pthread_rwlock_unlock(&obj_lock);
405         return status;
406 } /* sdb_store_host */
408 _Bool
409 sdb_store_has_host(const char *name)
411         sdb_store_obj_t *host;
413         if (! name)
414                 return NULL;
416         host = store_lookup(SDB_HOST, name);
417         return host != NULL;
418 } /* sdb_store_has_host */
420 sdb_store_base_t *
421 sdb_store_get_host(const char *name)
423         sdb_store_obj_t *host;
425         if (! name)
426                 return NULL;
428         host = store_lookup(SDB_HOST, name);
429         if (! host)
430                 return NULL;
432         sdb_object_ref(SDB_OBJ(host));
433         return STORE_BASE(host);
434 } /* sdb_store_get_host */
436 int
437 sdb_store_attribute(const char *hostname,
438                 const char *key, const sdb_data_t *value,
439                 sdb_time_t last_update)
441         int status;
443         sdb_store_base_t *updated_attr = NULL;
445         if ((! hostname) || (! key))
446                 return -1;
448         pthread_rwlock_wrlock(&obj_lock);
449         status = store_obj(/* parent = */ SDB_HOST, hostname,
450                         /* stored object = */ SDB_ATTRIBUTE, key, last_update,
451                         &updated_attr);
453         if (status >= 0) {
454                 assert(updated_attr);
455                 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
456                         sdb_object_deref(SDB_OBJ(updated_attr));
457                         status = -1;
458                 }
459         }
461         pthread_rwlock_unlock(&obj_lock);
462         return status;
463 } /* sdb_store_attribute */
465 int
466 sdb_store_service(const char *hostname, const char *name,
467                 sdb_time_t last_update)
469         int status;
471         if ((! hostname) || (! name))
472                 return -1;
474         pthread_rwlock_wrlock(&obj_lock);
475         status = store_obj(/* parent = */ SDB_HOST, hostname,
476                         /* stored object = */ SDB_SERVICE, name, last_update,
477                         /* updated obj = */ NULL);
478         pthread_rwlock_unlock(&obj_lock);
479         return status;
480 } /* sdb_store_service */
482 int
483 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
485         sdb_store_obj_t *host;
486         char time_str[64];
488         if ((! h) || (h->type != SDB_HOST) || (! buf))
489                 return -1;
491         host = SDB_STORE_OBJ(h);
493         if (! sdb_strftime(time_str, sizeof(time_str),
494                                 "%F %T %z", host->_last_update))
495                 snprintf(time_str, sizeof(time_str), "<error>");
496         time_str[sizeof(time_str) - 1] = '\0';
498         sdb_strbuf_append(buf, "{\"name\": \"%s\", "
499                         "\"last_update\": \"%s\"",
500                         SDB_OBJ(host)->name, time_str);
502         if (! (flags & SDB_SKIP_ATTRIBUTES)) {
503                 sdb_strbuf_append(buf, ", \"attributes\": ");
504                 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
505         }
507         if (! (flags & SDB_SKIP_SERVICES)) {
508                 sdb_strbuf_append(buf, ", \"services\": ");
509                 store_obj_tojson(host->children, SDB_SERVICE, buf);
510         }
512         sdb_strbuf_append(buf, "}");
513         return 0;
514 } /* sdb_store_host_tojson */
516 /* TODO: actually support hierarchical data */
517 int
518 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
520         sdb_llist_iter_t *host_iter;
522         if (! buf)
523                 return -1;
525         pthread_rwlock_rdlock(&obj_lock);
527         host_iter = sdb_llist_get_iter(obj_list);
528         if (! host_iter) {
529                 pthread_rwlock_unlock(&obj_lock);
530                 return -1;
531         }
533         sdb_strbuf_append(buf, "{\"hosts\":[");
535         while (sdb_llist_iter_has_next(host_iter)) {
536                 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
537                 assert(host);
539                 if (sdb_store_host_tojson(host, buf, flags))
540                         return -1;
542                 if (sdb_llist_iter_has_next(host_iter))
543                         sdb_strbuf_append(buf, ",");
544         }
546         sdb_strbuf_append(buf, "]}");
548         sdb_llist_iter_destroy(host_iter);
549         pthread_rwlock_unlock(&obj_lock);
550         return 0;
551 } /* sdb_store_tojson */
553 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */