Code

Merged branch 'master' of git://git.tokkee.org/sysdb.git.
[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 "utils/llist.h"
31 #include "utils/string.h"
33 #include <assert.h>
35 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include <pthread.h>
43 /*
44  * private data types
45  */
47 /* type used for looking up named objects */
48 typedef struct {
49         sdb_object_t parent;
50         const char *obj_name;
51 } sdb_store_lookup_obj_t;
52 #define SDB_STORE_LOOKUP_OBJ_INIT { SDB_OBJECT_INIT, NULL }
54 /*
55  * private variables
56  */
58 static sdb_llist_t *host_list = NULL;
59 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
61 /*
62  * private helper functions
63  */
65 static int
66 sdb_store_obj_cmp_by_name(const sdb_object_t *a, const sdb_object_t *b)
67 {
68         const sdb_store_obj_t *h1 = (const sdb_store_obj_t *)a;
69         const sdb_store_obj_t *h2 = (const sdb_store_obj_t *)b;
71         assert(h1 && h2);
72         return strcasecmp(h1->name, h2->name);
73 } /* sdb_store_obj_cmp_by_name */
75 static int
76 sdb_cmp_store_obj_with_name(const sdb_object_t *a, const sdb_object_t *b)
77 {
78         const sdb_store_obj_t *obj = (const sdb_store_obj_t *)a;
79         const sdb_store_lookup_obj_t *lookup = (const sdb_store_lookup_obj_t *)b;
81         assert(obj && lookup);
82         return strcasecmp(obj->name, lookup->obj_name);
83 } /* sdb_cmp_store_obj_with_name */
85 static int
86 sdb_host_init(sdb_object_t *obj, va_list ap)
87 {
88         const char *name = va_arg(ap, const char *);
90         SDB_HOST(obj)->host_name = strdup(name);
91         if (! SDB_HOST(obj)->host_name)
92                 return -1;
94         SDB_HOST(obj)->host_last_update = sdb_gettime();
95         /* ignore errors -> last_update will be updated later */
97         SDB_HOST(obj)->attributes = sdb_llist_create();
98         if (! SDB_HOST(obj)->attributes)
99                 return -1;
100         SDB_HOST(obj)->services = sdb_llist_create();
101         if (! SDB_HOST(obj)->services)
102                 return -1;
103         return 0;
104 } /* sdb_host_init */
106 static void
107 sdb_host_destroy(sdb_object_t *obj)
109         assert(obj);
111         if (SDB_HOST(obj)->host_name)
112                 free(SDB_HOST(obj)->host_name);
114         if (SDB_HOST(obj)->attributes)
115                 sdb_llist_destroy(SDB_HOST(obj)->attributes);
116         if (SDB_HOST(obj)->services)
117                 sdb_llist_destroy(SDB_HOST(obj)->services);
118 } /* sdb_host_destroy */
120 static int
121 sdb_attr_init(sdb_object_t *obj, va_list ap)
123         const char *hostname = va_arg(ap, const char *);
124         const char *name = va_arg(ap, const char *);
125         const char *value = va_arg(ap, const char *);
127         SDB_ATTR(obj)->hostname = strdup(hostname);
128         SDB_ATTR(obj)->attr_name = strdup(name);
129         SDB_ATTR(obj)->attr_value = strdup(value);
130         if ((! SDB_ATTR(obj)->hostname)
131                         || (! SDB_ATTR(obj)->attr_name) || (! SDB_ATTR(obj)->attr_value))
132                 return -1;
134         SDB_ATTR(obj)->attr_last_update = sdb_gettime();
135         return 0;
136 } /* sdb_attr_init */
138 static void
139 sdb_attr_destroy(sdb_object_t *obj)
141         assert(obj);
143         if (SDB_ATTR(obj)->hostname)
144                 free(SDB_ATTR(obj)->hostname);
145         if (SDB_ATTR(obj)->attr_name)
146                 free(SDB_ATTR(obj)->attr_name);
147         if (SDB_ATTR(obj)->attr_value)
148                 free(SDB_ATTR(obj)->attr_value);
149 } /* sdb_attr_destroy */
151 static int
152 sdb_svc_init(sdb_object_t *obj, va_list ap)
154         const char *hostname = va_arg(ap, const char *);
155         const char *name = va_arg(ap, const char *);
157         SDB_SVC(obj)->hostname = strdup(hostname);
158         SDB_SVC(obj)->svc_name = strdup(name);
159         if ((! SDB_SVC(obj)->hostname) || (! SDB_SVC(obj)->svc_name))
160                 return -1;
162         SDB_SVC(obj)->svc_last_update = sdb_gettime();
163         /* ignore errors -> last_update will be updated later */
164         return 0;
165 } /* sdb_svc_init */
167 static void
168 sdb_svc_destroy(sdb_object_t *obj)
170         assert(obj);
172         if (SDB_SVC(obj)->hostname)
173                 free(SDB_SVC(obj)->hostname);
174         if (SDB_SVC(obj)->svc_name)
175                 free(SDB_SVC(obj)->svc_name);
176 } /* sdb_svc_destroy */
178 /*
179  * public API
180  */
182 sdb_host_t *
183 sdb_host_create(const char *name)
185         sdb_object_t *obj;
187         if (! name)
188                 return NULL;
190         obj = sdb_object_create(sizeof(sdb_host_t), sdb_host_init,
191                         sdb_host_destroy, name);
192         if (! obj)
193                 return NULL;
194         return SDB_HOST(obj);
195 } /* sdb_host_create */
197 sdb_host_t *
198 sdb_host_clone(const sdb_host_t *host)
200         sdb_host_t *new;
202         new = sdb_host_create(host->host_name);
203         if (! new)
204                 return NULL;
206         /* make sure these are initialized; else sdb_object_deref() might access
207          * arbitrary memory in case of an error */
208         new->services = new->attributes = NULL;
210         if (host->attributes) {
211                 new->attributes = sdb_llist_clone(host->attributes);
212                 if (! new->attributes) {
213                         sdb_object_deref(SDB_OBJ(new));
214                         return NULL;
215                 }
216         }
218         new->host_last_update = host->host_last_update;
219         if (host->services) {
220                 new->services = sdb_llist_clone(host->services);
221                 if (! new->services) {
222                         sdb_object_deref(SDB_OBJ(new));
223                         return NULL;
224                 }
225         }
226         return new;
227 } /* sdb_host_clone */
229 int
230 sdb_store_host(const sdb_host_t *host)
232         sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
233         sdb_time_t last_update;
235         sdb_host_t *old;
236         int status = 0;
238         if ((! host) || (! host->host_name))
239                 return -1;
241         last_update = host->host_last_update;
242         if (last_update <= 0)
243                 last_update = sdb_gettime();
245         pthread_rwlock_wrlock(&host_lock);
247         if (! host_list) {
248                 if (! (host_list = sdb_llist_create())) {
249                         pthread_rwlock_unlock(&host_lock);
250                         return -1;
251                 }
252         }
254         lookup.obj_name = host->host_name;
255         old = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
256                                 sdb_cmp_store_obj_with_name));
258         if (old) {
259                 if (old->host_last_update > last_update) {
260                         fprintf(stderr, "store: Cannot update host '%s' - "
261                                         "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
262                                         host->host_name, last_update, old->host_last_update);
263                         /* don't report an error; the host may be updated by multiple
264                          * backends */
265                         status = 1;
266                 }
267                 else {
268                         old->host_last_update = last_update;
269                 }
270         }
271         else {
272                 sdb_host_t *new = sdb_host_clone(host);
273                 if (! new) {
274                         char errbuf[1024];
275                         fprintf(stderr, "store: Failed to clone host object: %s\n",
276                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
277                         pthread_rwlock_unlock(&host_lock);
278                         return -1;
279                 }
281                 if (! new->attributes) {
282                         if (! (new->attributes = sdb_llist_create())) {
283                                 char errbuf[1024];
284                                 fprintf(stderr, "store: Failed to initialize "
285                                                 "host object '%s': %s\n", host->host_name,
286                                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
287                                 sdb_object_deref(SDB_OBJ(new));
288                                 pthread_rwlock_unlock(&host_lock);
289                                 return -1;
290                         }
291                 }
293                 if (! new->services) {
294                         if (! (new->services = sdb_llist_create())) {
295                                 char errbuf[1024];
296                                 fprintf(stderr, "store: Failed to initialize "
297                                                 "host object '%s': %s\n", host->host_name,
298                                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
299                                 sdb_object_deref(SDB_OBJ(new));
300                                 pthread_rwlock_unlock(&host_lock);
301                                 return -1;
302                         }
303                 }
305                 status = sdb_llist_insert_sorted(host_list, SDB_OBJ(new),
306                                 sdb_store_obj_cmp_by_name);
308                 /* pass control to the list or destroy in case of an error */
309                 sdb_object_deref(SDB_OBJ(new));
310         }
312         pthread_rwlock_unlock(&host_lock);
313         return status;
314 } /* sdb_store_host */
316 const sdb_host_t *
317 sdb_store_get_host(const char *name)
319         sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
320         sdb_host_t *host;
322         if (! name)
323                 return NULL;
325         lookup.obj_name = name;
326         host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
327                                 sdb_cmp_store_obj_with_name));
329         if (! host)
330                 return NULL;
331         return host;
332 } /* sdb_store_get_host */
334 sdb_attribute_t *
335 sdb_attribute_create(const char *hostname,
336                 const char *name, const char *value)
338         sdb_object_t *obj;
340         if ((! hostname) || (! name) || (! value))
341                 return NULL;
343         obj = sdb_object_create(sizeof(sdb_attribute_t), sdb_attr_init,
344                         sdb_attr_destroy, hostname, name, value);
345         if (! obj)
346                 return NULL;
347         return SDB_ATTR(obj);
348 } /* sdb_attribute_create */
350 sdb_attribute_t *
351 sdb_attribute_clone(const sdb_attribute_t *attr)
353         sdb_attribute_t *new;
355         new = sdb_attribute_create(attr->hostname,
356                         attr->attr_name, attr->attr_value);
357         if (! new)
358                 return NULL;
360         new->attr_last_update = attr->attr_last_update;
361         return new;
362 } /* sdb_attribute_clone */
364 int
365 sdb_store_attribute(const sdb_attribute_t *attr)
367         sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
368         sdb_host_t *host;
370         sdb_attribute_t *old;
372         sdb_time_t last_update;
374         int status = 0;
376         if (! attr)
377                 return -1;
379         last_update = attr->attr_last_update;
380         if (last_update <= 0)
381                 last_update = sdb_gettime();
383         if (! host_list)
384                 return -1;
386         pthread_rwlock_wrlock(&host_lock);
388         lookup.obj_name = attr->hostname;
389         host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
390                                 sdb_cmp_store_obj_with_name));
392         if (! host) {
393                 pthread_rwlock_unlock(&host_lock);
394                 return -1;
395         }
397         lookup.obj_name = attr->attr_name;
398         old = SDB_ATTR(sdb_llist_search(host->attributes,
399                                 (const sdb_object_t *)&lookup,
400                                 sdb_cmp_store_obj_with_name));
402         if (old) {
403                 if (old->host_last_update > last_update) {
404                         fprintf(stderr, "store: Cannot update attribute '%s/%s' - "
405                                         "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
406                                         attr->hostname, attr->attr_name, last_update,
407                                         old->host_last_update);
408                         status = 1;
409                 }
410                 else {
411                         old->attr_last_update = last_update;
412                 }
413         }
414         else {
415                 sdb_attribute_t *new = sdb_attribute_clone(attr);
416                 if (! new) {
417                         char errbuf[1024];
418                         fprintf(stderr, "store: Failed to clone attribute object: %s\n",
419                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
420                         pthread_rwlock_unlock(&host_lock);
421                         return -1;
422                 }
424                 status = sdb_llist_insert_sorted(host->attributes, SDB_OBJ(new),
425                                 sdb_store_obj_cmp_by_name);
427                 /* pass control to the list or destroy in case of an error */
428                 sdb_object_deref(SDB_OBJ(new));
429         }
431         pthread_rwlock_unlock(&host_lock);
432         return status;
433 } /* sdb_store_attribute */
435 sdb_service_t *
436 sdb_service_create(const char *hostname, const char *name)
438         sdb_object_t *obj;
440         if ((! hostname) || (! name))
441                 return NULL;
443         obj = sdb_object_create(sizeof(sdb_service_t), sdb_svc_init,
444                         sdb_svc_destroy, hostname, name);
445         if (! obj)
446                 return NULL;
447         return SDB_SVC(obj);
448 } /* sdb_service_create */
450 sdb_service_t *
451 sdb_service_clone(const sdb_service_t *svc)
453         sdb_service_t *new;
455         new = sdb_service_create(svc->hostname, svc->svc_name);
456         if (! new)
457                 return NULL;
459         new->svc_last_update = svc->svc_last_update;
460         return new;
461 } /* sdb_service_clone */
463 int
464 sdb_store_service(const sdb_service_t *svc)
466         sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
467         sdb_host_t *host;
469         sdb_service_t *old;
471         sdb_time_t last_update;
473         int status = 0;
475         if (! svc)
476                 return -1;
478         last_update = svc->svc_last_update;
479         if (last_update <= 0)
480                 last_update = sdb_gettime();
482         if (! host_list)
483                 return -1;
485         pthread_rwlock_wrlock(&host_lock);
487         lookup.obj_name = svc->hostname;
488         host = SDB_HOST(sdb_llist_search(host_list, (const sdb_object_t *)&lookup,
489                                 sdb_cmp_store_obj_with_name));
491         if (! host) {
492                 pthread_rwlock_unlock(&host_lock);
493                 return -1;
494         }
496         lookup.obj_name = svc->svc_name;
497         old = SDB_SVC(sdb_llist_search(host->services, (const sdb_object_t *)&lookup,
498                                 sdb_cmp_store_obj_with_name));
500         if (old) {
501                 if (old->host_last_update > last_update) {
502                         fprintf(stderr, "store: Cannot update service '%s/%s' - "
503                                         "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
504                                         svc->hostname, svc->svc_name, last_update,
505                                         old->host_last_update);
506                         status = 1;
507                 }
508                 else {
509                         old->svc_last_update = last_update;
510                 }
511         }
512         else {
513                 sdb_service_t *new = sdb_service_clone(svc);
514                 if (! new) {
515                         char errbuf[1024];
516                         fprintf(stderr, "store: Failed to clone service object: %s\n",
517                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
518                         pthread_rwlock_unlock(&host_lock);
519                         return -1;
520                 }
522                 status = sdb_llist_insert_sorted(host->services, SDB_OBJ(new),
523                                 sdb_store_obj_cmp_by_name);
525                 /* pass control to the list or destroy in case of an error */
526                 sdb_object_deref(SDB_OBJ(new));
527         }
529         pthread_rwlock_unlock(&host_lock);
530         return status;
531 } /* sdb_store_service */
533 const sdb_service_t *
534 sdb_store_get_service(const sdb_host_t *host, const char *name)
536         sdb_store_lookup_obj_t lookup = SDB_STORE_LOOKUP_OBJ_INIT;
537         sdb_service_t *svc;
539         if ((! host) || (! name))
540                 return NULL;
542         lookup.obj_name = name;
543         svc = SDB_SVC(sdb_llist_search(host->services,
544                                 (const sdb_object_t *)&lookup,
545                                 sdb_cmp_store_obj_with_name));
547         if (! svc)
548                 return NULL;
549         return svc;
550 } /* sdb_store_get_service */
552 int
553 sdb_store_dump(FILE *fh)
555         sdb_llist_iter_t *host_iter;
557         if (! fh)
558                 return -1;
560         pthread_rwlock_rdlock(&host_lock);
562         host_iter = sdb_llist_get_iter(host_list);
563         if (! host_iter) {
564                 pthread_rwlock_unlock(&host_lock);
565                 return -1;
566         }
568         while (sdb_llist_iter_has_next(host_iter)) {
569                 sdb_host_t *host = SDB_HOST(sdb_llist_iter_get_next(host_iter));
570                 sdb_llist_iter_t *svc_iter;
571                 sdb_llist_iter_t *attr_iter;
573                 char time_str[64];
575                 assert(host);
577                 if (! sdb_strftime(time_str, sizeof(time_str),
578                                         "%F %T %z", host->host_last_update))
579                         snprintf(time_str, sizeof(time_str), "<error>");
580                 time_str[sizeof(time_str) - 1] = '\0';
582                 fprintf(fh, "Host '%s' (last updated: %s):\n",
583                                 host->host_name, time_str);
585                 attr_iter = sdb_llist_get_iter(host->attributes);
586                 if (! attr_iter) {
587                         char errbuf[1024];
588                         fprintf(fh, "Failed to retrieve attributes: %s\n",
589                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
590                         continue;
591                 }
593                 while (sdb_llist_iter_has_next(attr_iter)) {
594                         sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
595                         assert(attr);
597                         if (! sdb_strftime(time_str, sizeof(time_str),
598                                                 "%F %T %z", attr->attr_last_update))
599                                 snprintf(time_str, sizeof(time_str), "<error>");
600                         time_str[sizeof(time_str) - 1] = '\0';
602                         fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
603                                         attr->attr_name, attr->attr_value, time_str);
604                 }
606                 sdb_llist_iter_destroy(attr_iter);
608                 svc_iter = sdb_llist_get_iter(host->services);
609                 if (! svc_iter) {
610                         char errbuf[1024];
611                         fprintf(fh, "Failed to retrieve services: %s\n",
612                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
613                         continue;
614                 }
616                 while (sdb_llist_iter_has_next(svc_iter)) {
617                         sdb_service_t *svc = SDB_SVC(sdb_llist_iter_get_next(svc_iter));
618                         assert(svc);
620                         if (! sdb_strftime(time_str, sizeof(time_str),
621                                                 "%F %T %z", svc->svc_last_update))
622                                 snprintf(time_str, sizeof(time_str), "<error>");
623                         time_str[sizeof(time_str) - 1] = '\0';
625                         fprintf(fh, "\tService '%s' (last updated: %s)\n",
626                                         svc->svc_name, time_str);
627                 }
629                 sdb_llist_iter_destroy(svc_iter);
630         }
632         sdb_llist_iter_destroy(host_iter);
633         pthread_rwlock_unlock(&host_lock);
634         return 0;
635 } /* sdb_store_dump */
637 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */