Code

plugin/store: Added support for "cname" plugins.
[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 "core/error.h"
31 #include "core/plugin.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 *host_list = NULL;
49 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
51 /*
52  * public types
53  */
55 static int
56 sdb_host_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
57 {
58         SDB_HOST(obj)->_last_update = sdb_gettime();
59         /* ignore errors -> last_update will be updated later */
61         SDB_HOST(obj)->attributes = sdb_llist_create();
62         if (! SDB_HOST(obj)->attributes)
63                 return -1;
64         SDB_HOST(obj)->services = sdb_llist_create();
65         if (! SDB_HOST(obj)->services)
66                 return -1;
67         return 0;
68 } /* sdb_host_init */
70 static void
71 sdb_host_destroy(sdb_object_t *obj)
72 {
73         assert(obj);
75         if (SDB_HOST(obj)->attributes)
76                 sdb_llist_destroy(SDB_HOST(obj)->attributes);
77         if (SDB_HOST(obj)->services)
78                 sdb_llist_destroy(SDB_HOST(obj)->services);
79 } /* sdb_host_destroy */
81 static sdb_object_t *
82 sdb_host_do_clone(const sdb_object_t *obj)
83 {
84         const sdb_host_t *host = (const sdb_host_t *)obj;
85         sdb_host_t *new;
87         new = sdb_host_create(obj->name);
88         if (! new)
89                 return NULL;
91         /* make sure these are initialized; else sdb_object_deref() might access
92          * arbitrary memory in case of an error */
93         new->services = new->attributes = NULL;
95         if (host->attributes) {
96                 new->attributes = sdb_llist_clone(host->attributes);
97                 if (! new->attributes) {
98                         sdb_object_deref(SDB_OBJ(new));
99                         return NULL;
100                 }
101         }
103         new->_last_update = host->_last_update;
104         if (host->services) {
105                 new->services = sdb_llist_clone(host->services);
106                 if (! new->services) {
107                         sdb_object_deref(SDB_OBJ(new));
108                         return NULL;
109                 }
110         }
111         return SDB_OBJ(new);
112 } /* sdb_host_do_clone */
114 static int
115 sdb_attr_init(sdb_object_t *obj, va_list ap)
117         const char *hostname = va_arg(ap, const char *);
118         const char *value = va_arg(ap, const char *);
120         SDB_ATTR(obj)->hostname = strdup(hostname);
121         SDB_ATTR(obj)->attr_value = strdup(value);
122         if ((! SDB_ATTR(obj)->hostname) || (! SDB_ATTR(obj)->attr_value))
123                 return -1;
125         SDB_ATTR(obj)->_last_update = sdb_gettime();
126         return 0;
127 } /* sdb_attr_init */
129 static void
130 sdb_attr_destroy(sdb_object_t *obj)
132         assert(obj);
134         if (SDB_ATTR(obj)->hostname)
135                 free(SDB_ATTR(obj)->hostname);
136         if (SDB_ATTR(obj)->attr_value)
137                 free(SDB_ATTR(obj)->attr_value);
138 } /* sdb_attr_destroy */
140 static sdb_object_t *
141 sdb_attr_clone(const sdb_object_t *obj)
143         const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
144         sdb_attribute_t *new;
146         new = sdb_attribute_create(attr->hostname,
147                         obj->name, attr->attr_value);
148         if (! new)
149                 return NULL;
151         new->_last_update = attr->_last_update;
152         return SDB_OBJ(new);
153 } /* sdb_attr_clone */
155 static int
156 sdb_svc_init(sdb_object_t *obj, va_list ap)
158         const char *hostname = va_arg(ap, const char *);
160         SDB_SVC(obj)->hostname = strdup(hostname);
161         if (! SDB_SVC(obj)->hostname)
162                 return -1;
164         SDB_SVC(obj)->_last_update = sdb_gettime();
165         /* ignore errors -> last_update will be updated later */
166         return 0;
167 } /* sdb_svc_init */
169 static void
170 sdb_svc_destroy(sdb_object_t *obj)
172         assert(obj);
174         if (SDB_SVC(obj)->hostname)
175                 free(SDB_SVC(obj)->hostname);
176 } /* sdb_svc_destroy */
178 static sdb_object_t *
179 sdb_svc_clone(const sdb_object_t *obj)
181         const sdb_service_t *svc = (const sdb_service_t *)obj;
182         sdb_service_t *new;
184         new = sdb_service_create(svc->hostname, obj->name);
185         if (! new)
186                 return NULL;
188         new->_last_update = svc->_last_update;
189         return SDB_OBJ(new);
190 } /* sdb_svc_clone */
192 const sdb_type_t sdb_host_type = {
193         sizeof(sdb_host_t),
195         sdb_host_init,
196         sdb_host_destroy,
197         sdb_host_do_clone
198 };
200 const sdb_type_t sdb_attribute_type = {
201         sizeof(sdb_attribute_t),
203         sdb_attr_init,
204         sdb_attr_destroy,
205         sdb_attr_clone
206 };
208 const sdb_type_t sdb_service_type = {
209         sizeof(sdb_service_t),
211         sdb_svc_init,
212         sdb_svc_destroy,
213         sdb_svc_clone
214 };
216 /*
217  * public API
218  */
220 sdb_host_t *
221 sdb_host_create(const char *name)
223         sdb_object_t *obj;
225         if (! name)
226                 return NULL;
228         obj = sdb_object_create(name, sdb_host_type);
229         if (! obj)
230                 return NULL;
231         return SDB_HOST(obj);
232 } /* sdb_host_create */
234 int
235 sdb_store_host(const sdb_host_t *host)
237         char *cname;
239         sdb_time_t last_update;
240         sdb_host_t *old;
242         int status = 0;
244         if ((! host) || (! SDB_CONST_OBJ(host)->name))
245                 return -1;
247         cname = sdb_plugin_cname(strdup(SDB_CONST_OBJ(host)->name));
248         if (! cname) {
249                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
250                 return -1;
251         }
253         last_update = host->_last_update;
254         if (last_update <= 0)
255                 last_update = sdb_gettime();
257         pthread_rwlock_wrlock(&host_lock);
259         if (! host_list) {
260                 if (! (host_list = sdb_llist_create())) {
261                         pthread_rwlock_unlock(&host_lock);
262                         return -1;
263                 }
264         }
266         old = SDB_HOST(sdb_llist_search_by_name(host_list, cname));
268         if (old) {
269                 if (old->_last_update > last_update) {
270                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - "
271                                         "value too old (%"PRIscTIME" < %"PRIscTIME")",
272                                         cname, last_update, old->_last_update);
273                         /* don't report an error; the host may be updated by multiple
274                          * backends */
275                         status = 1;
276                 }
277                 else {
278                         old->_last_update = last_update;
279                 }
280         }
281         else {
282                 sdb_host_t *new = SDB_HOST(sdb_object_clone(SDB_CONST_OBJ(host)));
283                 if (! new) {
284                         char errbuf[1024];
285                         sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s",
286                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
287                         pthread_rwlock_unlock(&host_lock);
288                         return -1;
289                 }
291                 free(SDB_OBJ(new)->name);
292                 SDB_OBJ(new)->name = cname;
294                 if (! new->attributes) {
295                         if (! (new->attributes = sdb_llist_create())) {
296                                 char errbuf[1024];
297                                 sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
298                                                 "host object '%s': %s", SDB_OBJ(new)->name,
299                                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
300                                 sdb_object_deref(SDB_OBJ(new));
301                                 pthread_rwlock_unlock(&host_lock);
302                                 return -1;
303                         }
304                 }
306                 if (! new->services) {
307                         if (! (new->services = sdb_llist_create())) {
308                                 char errbuf[1024];
309                                 sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
310                                                 "host object '%s': %s", SDB_OBJ(new)->name,
311                                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
312                                 sdb_object_deref(SDB_OBJ(new));
313                                 pthread_rwlock_unlock(&host_lock);
314                                 return -1;
315                         }
316                 }
318                 status = sdb_llist_insert_sorted(host_list, SDB_OBJ(new),
319                                 sdb_object_cmp_by_name);
321                 /* pass control to the list or destroy in case of an error */
322                 sdb_object_deref(SDB_OBJ(new));
323         }
325         pthread_rwlock_unlock(&host_lock);
326         return status;
327 } /* sdb_store_host */
329 const sdb_host_t *
330 sdb_store_get_host(const char *name)
332         sdb_host_t *host;
334         if (! name)
335                 return NULL;
337         host = SDB_HOST(sdb_llist_search_by_name(host_list, name));
338         if (! host)
339                 return NULL;
340         return host;
341 } /* sdb_store_get_host */
343 sdb_attribute_t *
344 sdb_attribute_create(const char *hostname,
345                 const char *name, const char *value)
347         sdb_object_t *obj;
349         if ((! hostname) || (! name) || (! value))
350                 return NULL;
352         obj = sdb_object_create(name, sdb_attribute_type, hostname, value);
353         if (! obj)
354                 return NULL;
355         return SDB_ATTR(obj);
356 } /* sdb_attribute_create */
358 int
359 sdb_store_attribute(const sdb_attribute_t *attr)
361         sdb_host_t *host;
362         sdb_attribute_t *old;
363         sdb_time_t last_update;
365         int status = 0;
367         if (! attr)
368                 return -1;
370         last_update = attr->_last_update;
371         if (last_update <= 0)
372                 last_update = sdb_gettime();
374         if (! host_list)
375                 return -1;
377         pthread_rwlock_wrlock(&host_lock);
379         host = SDB_HOST(sdb_llist_search_by_name(host_list, attr->hostname));
380         if (! host) {
381                 pthread_rwlock_unlock(&host_lock);
382                 return -1;
383         }
385         old = SDB_ATTR(sdb_llist_search_by_name(host->attributes,
386                                 SDB_CONST_OBJ(attr)->name));
387         if (old) {
388                 if (old->_last_update > last_update) {
389                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
390                                         "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
391                                         attr->hostname, SDB_CONST_OBJ(attr)->name, last_update,
392                                         old->_last_update);
393                         status = 1;
394                 }
395                 else {
396                         old->_last_update = last_update;
397                 }
398         }
399         else {
400                 sdb_attribute_t *new = SDB_ATTR(sdb_object_clone(SDB_CONST_OBJ(attr)));
401                 if (! new) {
402                         char errbuf[1024];
403                         sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
404                                         "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
405                         pthread_rwlock_unlock(&host_lock);
406                         return -1;
407                 }
409                 status = sdb_llist_insert_sorted(host->attributes, SDB_OBJ(new),
410                                 sdb_object_cmp_by_name);
412                 /* pass control to the list or destroy in case of an error */
413                 sdb_object_deref(SDB_OBJ(new));
414         }
416         pthread_rwlock_unlock(&host_lock);
417         return status;
418 } /* sdb_store_attribute */
420 sdb_service_t *
421 sdb_service_create(const char *hostname, const char *name)
423         sdb_object_t *obj;
425         if ((! hostname) || (! name))
426                 return NULL;
428         obj = sdb_object_create(name, sdb_service_type, hostname);
429         if (! obj)
430                 return NULL;
431         return SDB_SVC(obj);
432 } /* sdb_service_create */
434 int
435 sdb_store_service(const sdb_service_t *svc)
437         sdb_host_t *host;
438         sdb_service_t *old;
439         sdb_time_t last_update;
441         int status = 0;
443         if (! svc)
444                 return -1;
446         last_update = svc->_last_update;
447         if (last_update <= 0)
448                 last_update = sdb_gettime();
450         if (! host_list)
451                 return -1;
453         pthread_rwlock_wrlock(&host_lock);
455         host = SDB_HOST(sdb_llist_search_by_name(host_list, svc->hostname));
456         if (! host) {
457                 pthread_rwlock_unlock(&host_lock);
458                 return -1;
459         }
461         old = SDB_SVC(sdb_llist_search_by_name(host->services,
462                                 SDB_CONST_OBJ(svc)->name));
463         if (old) {
464                 if (old->_last_update > last_update) {
465                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update service "
466                                         "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
467                                         svc->hostname, SDB_CONST_OBJ(svc)->name, last_update,
468                                         old->_last_update);
469                         status = 1;
470                 }
471                 else {
472                         old->_last_update = last_update;
473                 }
474         }
475         else {
476                 sdb_service_t *new = SDB_SVC(sdb_object_clone(SDB_CONST_OBJ(svc)));
477                 if (! new) {
478                         char errbuf[1024];
479                         sdb_log(SDB_LOG_ERR, "store: Failed to clone service "
480                                         "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
481                         pthread_rwlock_unlock(&host_lock);
482                         return -1;
483                 }
485                 status = sdb_llist_insert_sorted(host->services, SDB_OBJ(new),
486                                 sdb_object_cmp_by_name);
488                 /* pass control to the list or destroy in case of an error */
489                 sdb_object_deref(SDB_OBJ(new));
490         }
492         pthread_rwlock_unlock(&host_lock);
493         return status;
494 } /* sdb_store_service */
496 const sdb_service_t *
497 sdb_store_get_service(const sdb_host_t *host, const char *name)
499         sdb_service_t *svc;
501         if ((! host) || (! name))
502                 return NULL;
504         svc = SDB_SVC(sdb_llist_search_by_name(host->services, name));
505         if (! svc)
506                 return NULL;
507         return svc;
508 } /* sdb_store_get_service */
510 int
511 sdb_store_dump(FILE *fh)
513         sdb_llist_iter_t *host_iter;
515         if (! fh)
516                 return -1;
518         pthread_rwlock_rdlock(&host_lock);
520         host_iter = sdb_llist_get_iter(host_list);
521         if (! host_iter) {
522                 pthread_rwlock_unlock(&host_lock);
523                 return -1;
524         }
526         while (sdb_llist_iter_has_next(host_iter)) {
527                 sdb_host_t *host = SDB_HOST(sdb_llist_iter_get_next(host_iter));
528                 sdb_llist_iter_t *svc_iter;
529                 sdb_llist_iter_t *attr_iter;
531                 char time_str[64];
533                 assert(host);
535                 if (! sdb_strftime(time_str, sizeof(time_str),
536                                         "%F %T %z", host->_last_update))
537                         snprintf(time_str, sizeof(time_str), "<error>");
538                 time_str[sizeof(time_str) - 1] = '\0';
540                 fprintf(fh, "Host '%s' (last updated: %s):\n",
541                                 SDB_OBJ(host)->name, time_str);
543                 attr_iter = sdb_llist_get_iter(host->attributes);
544                 if (! attr_iter) {
545                         char errbuf[1024];
546                         fprintf(fh, "Failed to retrieve attributes: %s\n",
547                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
548                         continue;
549                 }
551                 while (sdb_llist_iter_has_next(attr_iter)) {
552                         sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
553                         assert(attr);
555                         if (! sdb_strftime(time_str, sizeof(time_str),
556                                                 "%F %T %z", attr->_last_update))
557                                 snprintf(time_str, sizeof(time_str), "<error>");
558                         time_str[sizeof(time_str) - 1] = '\0';
560                         fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
561                                         SDB_OBJ(attr)->name, attr->attr_value, time_str);
562                 }
564                 sdb_llist_iter_destroy(attr_iter);
566                 svc_iter = sdb_llist_get_iter(host->services);
567                 if (! svc_iter) {
568                         char errbuf[1024];
569                         fprintf(fh, "Failed to retrieve services: %s\n",
570                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
571                         continue;
572                 }
574                 while (sdb_llist_iter_has_next(svc_iter)) {
575                         sdb_service_t *svc = SDB_SVC(sdb_llist_iter_get_next(svc_iter));
576                         assert(svc);
578                         if (! sdb_strftime(time_str, sizeof(time_str),
579                                                 "%F %T %z", svc->_last_update))
580                                 snprintf(time_str, sizeof(time_str), "<error>");
581                         time_str[sizeof(time_str) - 1] = '\0';
583                         fprintf(fh, "\tService '%s' (last updated: %s)\n",
584                                         SDB_OBJ(svc)->name, time_str);
585                 }
587                 sdb_llist_iter_destroy(svc_iter);
588         }
590         sdb_llist_iter_destroy(host_iter);
591         pthread_rwlock_unlock(&host_lock);
592         return 0;
593 } /* sdb_store_dump */
595 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */