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-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 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/store-private.h"
34 #include "core/plugin.h"
35 #include "utils/error.h"
36 #include "utils/llist.h"
38 #include <assert.h>
40 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #include <pthread.h>
48 /*
49  * private variables
50  */
52 static sdb_llist_t *host_list = NULL;
53 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
55 /*
56  * private types
57  */
59 static sdb_type_t sdb_host_type;
60 static sdb_type_t sdb_service_type;
61 static sdb_type_t sdb_attribute_type;
63 static int
64 store_obj_init(sdb_object_t *obj, va_list ap)
65 {
66         sdb_store_obj_t *sobj = STORE_OBJ(obj);
68         sobj->type = va_arg(ap, int);
70         sobj->last_update = va_arg(ap, sdb_time_t);
71         sobj->interval = 0;
72         sobj->backends = NULL;
73         sobj->backends_num = 0;
74         sobj->parent = NULL;
75         return 0;
76 } /* store_obj_init */
78 static void
79 store_obj_destroy(sdb_object_t *obj)
80 {
81         sdb_store_obj_t *sobj = STORE_OBJ(obj);
82         size_t i;
84         for (i = 0; i < sobj->backends_num; ++i)
85                 free(sobj->backends[i]);
86         free(sobj->backends);
87         sobj->backends = NULL;
88         sobj->backends_num = 0;
90         if (sobj->parent)
91                 sdb_object_deref(SDB_OBJ(sobj->parent));
92 } /* store_obj_destroy */
94 static int
95 sdb_host_init(sdb_object_t *obj, va_list ap)
96 {
97         sdb_host_t *sobj = HOST(obj);
98         int ret;
100         /* this will consume the first argument (type) of ap */
101         ret = store_obj_init(obj, ap);
102         if (ret)
103                 return ret;
105         sobj->services = sdb_llist_create();
106         if (! sobj->services)
107                 return -1;
108         sobj->attributes = sdb_llist_create();
109         if (! sobj->attributes)
110                 return -1;
111         return 0;
112 } /* sdb_host_init */
114 static void
115 sdb_host_destroy(sdb_object_t *obj)
117         sdb_host_t *sobj = HOST(obj);
118         assert(obj);
120         store_obj_destroy(obj);
122         if (sobj->services)
123                 sdb_llist_destroy(sobj->services);
124         if (sobj->attributes)
125                 sdb_llist_destroy(sobj->attributes);
126 } /* sdb_host_destroy */
128 static int
129 sdb_service_init(sdb_object_t *obj, va_list ap)
131         sdb_service_t *sobj = SVC(obj);
132         int ret;
134         /* this will consume the first argument (type) of ap */
135         ret = store_obj_init(obj, ap);
136         if (ret)
137                 return ret;
139         sobj->attributes = sdb_llist_create();
140         if (! sobj->attributes)
141                 return -1;
142         return 0;
143 } /* sdb_service_init */
145 static void
146 sdb_service_destroy(sdb_object_t *obj)
148         sdb_service_t *sobj = SVC(obj);
149         assert(obj);
151         store_obj_destroy(obj);
153         if (sobj->attributes)
154                 sdb_llist_destroy(sobj->attributes);
155 } /* sdb_service_destroy */
157 static int
158 sdb_attr_init(sdb_object_t *obj, va_list ap)
160         const sdb_data_t *value;
161         int ret;
163         /* this will consume the first two arguments
164          * (type and last_update) of ap */
165         ret = store_obj_init(obj, ap);
166         if (ret)
167                 return ret;
168         value = va_arg(ap, const sdb_data_t *);
170         if (value)
171                 if (sdb_data_copy(&ATTR(obj)->value, value))
172                         return -1;
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);
182         sdb_data_free_datum(&ATTR(obj)->value);
183 } /* sdb_attr_destroy */
185 static sdb_type_t sdb_host_type = {
186         sizeof(sdb_host_t),
187         sdb_host_init,
188         sdb_host_destroy
189 };
191 static sdb_type_t sdb_service_type = {
192         sizeof(sdb_service_t),
193         sdb_service_init,
194         sdb_service_destroy
195 };
197 static sdb_type_t sdb_attribute_type = {
198         sizeof(sdb_attribute_t),
199         sdb_attr_init,
200         sdb_attr_destroy
201 };
203 /*
204  * private helper functions
205  */
207 static sdb_host_t *
208 lookup_host(const char *name)
210         return HOST(sdb_llist_search_by_name(host_list, name));
211 } /* lookup_host */
213 /* The host_lock has to be acquired before calling this function. */
214 static int
215 store_obj(const char *hostname, int type, const char *name,
216                 sdb_time_t last_update, sdb_store_obj_t **updated_obj)
218         char *host_cname = NULL, *cname = NULL;
219         char **tmp;
221         sdb_llist_t *parent_list;
222         sdb_store_obj_t *old, *new;
223         const sdb_plugin_info_t *info;
225         int status = 0;
226         size_t i;
228         if (last_update <= 0)
229                 last_update = sdb_gettime();
231         assert((type == 0)
232                         || (type == SDB_HOST)
233                         || (type == SDB_SERVICE)
234                         || (type == SDB_ATTRIBUTE));
236         assert(hostname || (type == SDB_HOST));
237         assert((! hostname)
238                         || (type == SDB_SERVICE)
239                         || (type == SDB_ATTRIBUTE));
241         if (! host_list)
242                 if (! (host_list = sdb_llist_create()))
243                         return -1;
244         parent_list = host_list;
246         if (type == SDB_HOST) {
247                 cname = sdb_plugin_cname(strdup(name));
248                 if (! cname) {
249                         sdb_log(SDB_LOG_ERR, "store: strdup failed");
250                         return -1;
251                 }
252                 name = cname;
253         }
255         if (hostname) {
256                 sdb_host_t *host;
258                 host_cname = sdb_plugin_cname(strdup(hostname));
259                 if (! host_cname) {
260                         sdb_log(SDB_LOG_ERR, "store: strdup failed");
261                         free(cname);
262                         return -1;
263                 }
264                 hostname = host_cname;
266                 host = lookup_host(hostname);
267                 if (! host) {
268                         sdb_log(SDB_LOG_ERR, "store: Failed to store %s '%s' - "
269                                         "host '%s' not found", SDB_STORE_TYPE_TO_NAME(type),
270                                         name, hostname);
271                         free(host_cname);
272                         free(cname);
273                         return -1;
274                 }
276                 if (type == SDB_ATTRIBUTE)
277                         parent_list = host->attributes;
278                 else
279                         parent_list = host->services;
280         }
282         if (type == SDB_HOST)
283                 old = STORE_OBJ(sdb_llist_search_by_name(host_list, name));
284         else
285                 old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
287         if (old) {
288                 if (old->last_update > last_update) {
289                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
290                                         "value too old (%"PRIscTIME" < %"PRIscTIME")",
291                                         SDB_STORE_TYPE_TO_NAME(type), name,
292                                         last_update, old->last_update);
293                         /* don't report an error; the object may be updated by multiple
294                          * backends */
295                         status = 1;
296                 }
297                 else {
298                         sdb_time_t interval = last_update - old->last_update;
299                         old->last_update = last_update;
300                         if (interval) {
301                                 if (old->interval)
302                                         old->interval = (sdb_time_t)((0.9 * (double)old->interval)
303                                                         + (0.1 * (double)interval));
304                                 else
305                                         old->interval = interval;
306                         }
307                 }
309                 new = old;
310         }
311         else {
312                 if (type == SDB_ATTRIBUTE) {
313                         /* the value will be updated by the caller */
314                         new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
315                                                 type, last_update, NULL));
316                 }
317                 else {
318                         sdb_type_t t;
319                         t = type == SDB_HOST ? sdb_host_type : sdb_service_type;
320                         new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
321                 }
323                 if (new) {
324                         status = sdb_llist_insert_sorted(parent_list, SDB_OBJ(new),
325                                         sdb_object_cmp_by_name);
327                         /* pass control to the list or destroy in case of an error */
328                         sdb_object_deref(SDB_OBJ(new));
329                 }
330                 else {
331                         char errbuf[1024];
332                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
333                                         SDB_STORE_TYPE_TO_NAME(type), name,
334                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
335                         status = -1;
336                 }
337         }
339         free(host_cname);
340         free(cname);
342         if (status < 0)
343                 return status;
344         assert(new);
346         if (updated_obj)
347                 *updated_obj = new;
349         info = sdb_plugin_current();
350         if (! info)
351                 return status;
353         for (i = 0; i < new->backends_num; ++i)
354                 if (!strcasecmp(new->backends[i], info->plugin_name))
355                         return status;
357         tmp = realloc(new->backends,
358                         (new->backends_num + 1) * sizeof(*new->backends));
359         if (! tmp)
360                 return -1;
362         new->backends = tmp;
363         new->backends[new->backends_num] = strdup(info->plugin_name);
364         if (! new->backends[new->backends_num])
365                 return -1;
367         ++new->backends_num;
368         return status;
369 } /* store_obj */
371 /*
372  * store_common_tojson serializes common object attributes to JSON.
373  *
374  * The function never returns an error. Rather, an error message will be part
375  * of the serialized data.
376  */
377 static void
378 store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
380         char time_str[64];
381         char interval_str[64];
382         size_t i;
384         if (! sdb_strftime(time_str, sizeof(time_str),
385                                 "%F %T %z", obj->last_update))
386                 snprintf(time_str, sizeof(time_str), "<error>");
387         time_str[sizeof(time_str) - 1] = '\0';
389         if (! sdb_strfinterval(interval_str, sizeof(interval_str),
390                                 obj->interval))
391                 snprintf(interval_str, sizeof(interval_str), "<error>");
392         interval_str[sizeof(interval_str) - 1] = '\0';
394         sdb_strbuf_append(buf, "\"last_update\": \"%s\", "
395                         "\"update_interval\": \"%s\", \"backends\": [",
396                         time_str, interval_str);
398         for (i = 0; i < obj->backends_num; ++i) {
399                 sdb_strbuf_append(buf, "\"%s\"", obj->backends[i]);
400                 if (i < obj->backends_num - 1)
401                         sdb_strbuf_append(buf, ",");
402         }
403         sdb_strbuf_append(buf, "]");
404 } /* store_common_tojson */
406 /*
407  * store_obj_tojson serializes attribute / service objects to JSON.
408  *
409  * The function never returns an error. Rather, an error message will be part
410  * of the serialized data.
411  */
412 static void
413 store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
415         sdb_llist_iter_t *iter;
417         assert((type == SDB_ATTRIBUTE) || (type == SDB_SERVICE));
419         sdb_strbuf_append(buf, "[");
420         iter = sdb_llist_get_iter(list);
421         if (! iter) {
422                 char errbuf[1024];
423                 sdb_log(SDB_LOG_ERR, "store: Failed to retrieve %ss: %s\n",
424                                 SDB_STORE_TYPE_TO_NAME(type),
425                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
426                 sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve %ss: %s\"}",
427                                 SDB_STORE_TYPE_TO_NAME(type), errbuf);
428         }
430         /* has_next returns false if the iterator is NULL */
431         while (sdb_llist_iter_has_next(iter)) {
432                 sdb_store_obj_t *sobj = STORE_OBJ(sdb_llist_iter_get_next(iter));
433                 assert(sobj);
434                 assert(sobj->type == type);
436                 sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(sobj)->name);
437                 if (sobj->type == SDB_ATTRIBUTE) {
438                         char tmp[sdb_data_strlen(&ATTR(sobj)->value) + 1];
439                         sdb_data_format(&ATTR(sobj)->value, tmp, sizeof(tmp),
440                                         SDB_DOUBLE_QUOTED);
441                         sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
442                 }
444                 store_common_tojson(sobj, buf);
445                 sdb_strbuf_append(buf, "}");
447                 if (sdb_llist_iter_has_next(iter))
448                         sdb_strbuf_append(buf, ",");
449         }
451         sdb_llist_iter_destroy(iter);
452         sdb_strbuf_append(buf, "]");
453 } /* store_obj_tojson */
455 /*
456  * public API
457  */
459 void
460 sdb_store_clear(void)
462         sdb_llist_destroy(host_list);
463         host_list = NULL;
464 } /* sdb_store_clear */
466 int
467 sdb_store_host(const char *name, sdb_time_t last_update)
469         int status;
471         if (! name)
472                 return -1;
474         pthread_rwlock_wrlock(&host_lock);
475         status = store_obj(/* hostname = */ NULL,
476                         /* stored object = */ SDB_HOST, name, last_update,
477                         /* updated_obj = */ NULL);
478         pthread_rwlock_unlock(&host_lock);
479         return status;
480 } /* sdb_store_host */
482 _Bool
483 sdb_store_has_host(const char *name)
485         sdb_host_t *host;
487         if (! name)
488                 return NULL;
490         host = lookup_host(name);
491         return host != NULL;
492 } /* sdb_store_has_host */
494 sdb_store_obj_t *
495 sdb_store_get_host(const char *name)
497         sdb_host_t *host;
499         if (! name)
500                 return NULL;
502         host = lookup_host(name);
503         if (! host)
504                 return NULL;
506         sdb_object_ref(SDB_OBJ(host));
507         return STORE_OBJ(host);
508 } /* sdb_store_get_host */
510 int
511 sdb_store_attribute(const char *hostname,
512                 const char *key, const sdb_data_t *value,
513                 sdb_time_t last_update)
515         int status;
517         sdb_store_obj_t *updated_attr = NULL;
519         if ((! hostname) || (! key))
520                 return -1;
522         pthread_rwlock_wrlock(&host_lock);
523         status = store_obj(hostname,
524                         /* stored object = */ SDB_ATTRIBUTE, key, last_update,
525                         &updated_attr);
527         if (status >= 0) {
528                 assert(updated_attr);
529                 sdb_data_free_datum(&ATTR(updated_attr)->value);
530                 if (sdb_data_copy(&ATTR(updated_attr)->value, value)) {
531                         sdb_object_deref(SDB_OBJ(updated_attr));
532                         status = -1;
533                 }
534         }
536         pthread_rwlock_unlock(&host_lock);
537         return status;
538 } /* sdb_store_attribute */
540 int
541 sdb_store_service(const char *hostname, const char *name,
542                 sdb_time_t last_update)
544         int status;
546         if ((! hostname) || (! name))
547                 return -1;
549         pthread_rwlock_wrlock(&host_lock);
550         status = store_obj(hostname,
551                         /* stored object = */ SDB_SERVICE, name, last_update,
552                         /* updated obj = */ NULL);
553         pthread_rwlock_unlock(&host_lock);
554         return status;
555 } /* sdb_store_service */
557 int
558 sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, int flags)
560         sdb_host_t *host;
562         if ((! h) || (h->type != SDB_HOST) || (! buf))
563                 return -1;
565         host = HOST(h);
567         sdb_strbuf_append(buf, "{\"name\": \"%s\", ", SDB_OBJ(host)->name);
568         store_common_tojson(h, buf);
570         if (! (flags & SDB_SKIP_ATTRIBUTES)) {
571                 sdb_strbuf_append(buf, ", \"attributes\": ");
572                 store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
573         }
575         if (! (flags & SDB_SKIP_SERVICES)) {
576                 sdb_strbuf_append(buf, ", \"services\": ");
577                 store_obj_tojson(host->services, SDB_SERVICE, buf);
578         }
580         sdb_strbuf_append(buf, "}");
581         return 0;
582 } /* sdb_store_host_tojson */
584 int
585 sdb_store_tojson(sdb_strbuf_t *buf, int flags)
587         sdb_llist_iter_t *host_iter;
589         if (! buf)
590                 return -1;
592         pthread_rwlock_rdlock(&host_lock);
594         host_iter = sdb_llist_get_iter(host_list);
595         if (! host_iter) {
596                 pthread_rwlock_unlock(&host_lock);
597                 return -1;
598         }
600         sdb_strbuf_append(buf, "{\"hosts\":[");
602         while (sdb_llist_iter_has_next(host_iter)) {
603                 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
604                 assert(host);
606                 if (sdb_store_host_tojson(host, buf, flags))
607                         return -1;
609                 if (sdb_llist_iter_has_next(host_iter))
610                         sdb_strbuf_append(buf, ",");
611         }
613         sdb_strbuf_append(buf, "]}");
615         sdb_llist_iter_destroy(host_iter);
616         pthread_rwlock_unlock(&host_lock);
617         return 0;
618 } /* sdb_store_tojson */
620 /* TODO: actually support hierarchical data */
621 int
622 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data)
624         sdb_llist_iter_t *host_iter;
625         int status = 0;
627         pthread_rwlock_rdlock(&host_lock);
629         host_iter = sdb_llist_get_iter(host_list);
630         if (! host_iter)
631                 status = -1;
633         /* has_next returns false if the iterator is NULL */
634         while (sdb_llist_iter_has_next(host_iter)) {
635                 sdb_store_obj_t *host = STORE_OBJ(sdb_llist_iter_get_next(host_iter));
636                 assert(host);
638                 if (cb(host, user_data)) {
639                         status = -1;
640                         break;
641                 }
642         }
644         sdb_llist_iter_destroy(host_iter);
645         pthread_rwlock_unlock(&host_lock);
646         return status;
647 } /* sdb_store_iterate */
649 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */