Code

data: Let sdb_data_format() different quoting styles.
[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_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 int
394 sdb_store_host(const char *name, sdb_time_t last_update)
396         int status;
398         if (! name)
399                 return -1;
401         pthread_rwlock_wrlock(&obj_lock);
402         status = store_obj(/* parent = */ 0, NULL,
403                         /* stored object = */ SDB_HOST, name, last_update,
404                         /* updated_obj = */ NULL);
405         pthread_rwlock_unlock(&obj_lock);
406         return status;
407 } /* sdb_store_host */
409 _Bool
410 sdb_store_has_host(const char *name)
412         sdb_store_obj_t *host;
414         if (! name)
415                 return NULL;
417         host = store_lookup(SDB_HOST, name);
418         return host != NULL;
419 } /* sdb_store_has_host */
421 sdb_store_base_t *
422 sdb_store_get_host(const char *name)
424         sdb_store_obj_t *host;
426         if (! name)
427                 return NULL;
429         host = store_lookup(SDB_HOST, name);
430         if (! host)
431                 return NULL;
433         sdb_object_ref(SDB_OBJ(host));
434         return STORE_BASE(host);
435 } /* sdb_store_get_host */
437 int
438 sdb_store_attribute(const char *hostname,
439                 const char *key, const sdb_data_t *value,
440                 sdb_time_t last_update)
442         int status;
444         sdb_store_base_t *updated_attr = NULL;
446         if ((! hostname) || (! key))
447                 return -1;
449         pthread_rwlock_wrlock(&obj_lock);
450         status = store_obj(/* parent = */ SDB_HOST, hostname,
451                         /* stored object = */ SDB_ATTRIBUTE, key, last_update,
452                         &updated_attr);
454         if (status >= 0) {
455                 assert(updated_attr);
456                 if (sdb_data_copy(&SDB_ATTR(updated_attr)->value, value)) {
457                         sdb_object_deref(SDB_OBJ(updated_attr));
458                         status = -1;
459                 }
460         }
462         pthread_rwlock_unlock(&obj_lock);
463         return status;
464 } /* sdb_store_attribute */
466 int
467 sdb_store_service(const char *hostname, const char *name,
468                 sdb_time_t last_update)
470         int status;
472         if ((! hostname) || (! name))
473                 return -1;
475         pthread_rwlock_wrlock(&obj_lock);
476         status = store_obj(/* parent = */ SDB_HOST, hostname,
477                         /* stored object = */ SDB_SERVICE, name, last_update,
478                         /* updated obj = */ NULL);
479         pthread_rwlock_unlock(&obj_lock);
480         return status;
481 } /* sdb_store_service */
483 int
484 sdb_store_host_tojson(sdb_store_base_t *h, sdb_strbuf_t *buf, int flags)
486         sdb_store_obj_t *host;
487         char time_str[64];
489         if ((! h) || (h->type != SDB_HOST) || (! buf))
490                 return -1;
492         host = SDB_STORE_OBJ(h);
494         if (! sdb_strftime(time_str, sizeof(time_str),
495                                 "%F %T %z", host->_last_update))
496                 snprintf(time_str, sizeof(time_str), "<error>");
497         time_str[sizeof(time_str) - 1] = '\0';
499         sdb_strbuf_append(buf, "{\"name\": \"%s\", "
500                         "\"last_update\": \"%s\"",
501                         SDB_OBJ(host)->name, time_str);
503         if (! (flags & SDB_SKIP_ATTRIBUTES)) {
504                 sdb_strbuf_append(buf, ", \"attributes\": ");
505                 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
506         }
508         if (! (flags & SDB_SKIP_SERVICES)) {
509                 sdb_strbuf_append(buf, ", \"services\": ");
510                 store_obj_tojson(host->children, SDB_SERVICE, buf);
511         }
513         sdb_strbuf_append(buf, "}");
514         return 0;
515 } /* sdb_store_host_tojson */
517 /* TODO: actually support hierarchical data */
518 int
519 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
521         sdb_llist_iter_t *host_iter;
523         if (! buf)
524                 return -1;
526         pthread_rwlock_rdlock(&obj_lock);
528         host_iter = sdb_llist_get_iter(obj_list);
529         if (! host_iter) {
530                 pthread_rwlock_unlock(&obj_lock);
531                 return -1;
532         }
534         sdb_strbuf_append(buf, "{\"hosts\":[");
536         while (sdb_llist_iter_has_next(host_iter)) {
537                 sdb_store_base_t *host = STORE_BASE(sdb_llist_iter_get_next(host_iter));
538                 assert(host);
540                 if (sdb_store_host_tojson(host, buf, flags))
541                         return -1;
543                 if (sdb_llist_iter_has_next(host_iter))
544                         sdb_strbuf_append(buf, ",");
545         }
547         sdb_strbuf_append(buf, "]}");
549         sdb_llist_iter_destroy(host_iter);
550         pthread_rwlock_unlock(&obj_lock);
551         return 0;
552 } /* sdb_store_tojson */
554 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */