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(sdb_object_create(obj->name, sdb_host_type));
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)
116 {
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)
131 {
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)
142 {
143 const sdb_attribute_t *attr = (const sdb_attribute_t *)obj;
144 sdb_attribute_t *new;
146 new = SDB_ATTR(sdb_object_create(obj->name, sdb_attribute_type,
147 attr->hostname, 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)
157 {
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)
171 {
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)
180 {
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 int
221 sdb_store_host(const char *name, sdb_time_t last_update)
222 {
223 sdb_host_t *old;
225 char *cname;
226 int status = 0;
228 if (! name)
229 return -1;
231 cname = sdb_plugin_cname(strdup(name));
232 if (! cname) {
233 sdb_log(SDB_LOG_ERR, "store: strdup failed");
234 return -1;
235 }
237 last_update = last_update;
238 if (last_update <= 0)
239 last_update = sdb_gettime();
241 pthread_rwlock_wrlock(&host_lock);
243 if (! host_list) {
244 if (! (host_list = sdb_llist_create())) {
245 pthread_rwlock_unlock(&host_lock);
246 return -1;
247 }
248 }
250 old = SDB_HOST(sdb_llist_search_by_name(host_list, cname));
252 if (old) {
253 if (old->_last_update > last_update) {
254 sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - "
255 "value too old (%"PRIscTIME" < %"PRIscTIME")",
256 cname, last_update, old->_last_update);
257 /* don't report an error; the host may be updated by multiple
258 * backends */
259 status = 1;
260 }
261 else {
262 old->_last_update = last_update;
263 }
264 }
265 else {
266 sdb_host_t *new = SDB_HOST(sdb_object_create(name, sdb_host_type));
267 if (! new) {
268 char errbuf[1024];
269 sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s",
270 sdb_strerror(errno, errbuf, sizeof(errbuf)));
271 pthread_rwlock_unlock(&host_lock);
272 return -1;
273 }
275 free(SDB_OBJ(new)->name);
276 SDB_OBJ(new)->name = cname;
278 if (! new->attributes) {
279 if (! (new->attributes = sdb_llist_create())) {
280 char errbuf[1024];
281 sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
282 "host object '%s': %s", SDB_OBJ(new)->name,
283 sdb_strerror(errno, errbuf, sizeof(errbuf)));
284 sdb_object_deref(SDB_OBJ(new));
285 pthread_rwlock_unlock(&host_lock);
286 return -1;
287 }
288 }
290 if (! new->services) {
291 if (! (new->services = sdb_llist_create())) {
292 char errbuf[1024];
293 sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
294 "host object '%s': %s", SDB_OBJ(new)->name,
295 sdb_strerror(errno, errbuf, sizeof(errbuf)));
296 sdb_object_deref(SDB_OBJ(new));
297 pthread_rwlock_unlock(&host_lock);
298 return -1;
299 }
300 }
302 status = sdb_llist_insert_sorted(host_list, SDB_OBJ(new),
303 sdb_object_cmp_by_name);
305 /* pass control to the list or destroy in case of an error */
306 sdb_object_deref(SDB_OBJ(new));
307 }
309 pthread_rwlock_unlock(&host_lock);
310 return status;
311 } /* sdb_store_host */
313 _Bool
314 sdb_store_has_host(const char *name)
315 {
316 sdb_host_t *host;
318 if (! name)
319 return NULL;
321 host = SDB_HOST(sdb_llist_search_by_name(host_list, name));
322 return host != NULL;
323 } /* sdb_store_has_host */
325 int
326 sdb_store_attribute(const sdb_attribute_t *attr)
327 {
328 sdb_host_t *host;
329 sdb_attribute_t *old;
330 sdb_time_t last_update;
332 int status = 0;
334 if (! attr)
335 return -1;
337 last_update = attr->_last_update;
338 if (last_update <= 0)
339 last_update = sdb_gettime();
341 if (! host_list)
342 return -1;
344 pthread_rwlock_wrlock(&host_lock);
346 host = SDB_HOST(sdb_llist_search_by_name(host_list, attr->hostname));
347 if (! host) {
348 pthread_rwlock_unlock(&host_lock);
349 return -1;
350 }
352 old = SDB_ATTR(sdb_llist_search_by_name(host->attributes,
353 SDB_CONST_OBJ(attr)->name));
354 if (old) {
355 if (old->_last_update > last_update) {
356 sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
357 "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
358 attr->hostname, SDB_CONST_OBJ(attr)->name, last_update,
359 old->_last_update);
360 status = 1;
361 }
362 else {
363 old->_last_update = last_update;
364 }
365 }
366 else {
367 sdb_attribute_t *new = SDB_ATTR(sdb_object_clone(SDB_CONST_OBJ(attr)));
368 if (! new) {
369 char errbuf[1024];
370 sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
371 "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
372 pthread_rwlock_unlock(&host_lock);
373 return -1;
374 }
376 status = sdb_llist_insert_sorted(host->attributes, SDB_OBJ(new),
377 sdb_object_cmp_by_name);
379 /* pass control to the list or destroy in case of an error */
380 sdb_object_deref(SDB_OBJ(new));
381 }
383 pthread_rwlock_unlock(&host_lock);
384 return status;
385 } /* sdb_store_attribute */
387 sdb_service_t *
388 sdb_service_create(const char *hostname, const char *name)
389 {
390 sdb_object_t *obj;
392 if ((! hostname) || (! name))
393 return NULL;
395 obj = sdb_object_create(name, sdb_service_type, hostname);
396 if (! obj)
397 return NULL;
398 return SDB_SVC(obj);
399 } /* sdb_service_create */
401 int
402 sdb_store_service(const sdb_service_t *svc)
403 {
404 sdb_host_t *host;
405 sdb_service_t *old;
406 sdb_time_t last_update;
408 int status = 0;
410 if (! svc)
411 return -1;
413 last_update = svc->_last_update;
414 if (last_update <= 0)
415 last_update = sdb_gettime();
417 if (! host_list)
418 return -1;
420 pthread_rwlock_wrlock(&host_lock);
422 host = SDB_HOST(sdb_llist_search_by_name(host_list, svc->hostname));
423 if (! host) {
424 pthread_rwlock_unlock(&host_lock);
425 return -1;
426 }
428 old = SDB_SVC(sdb_llist_search_by_name(host->services,
429 SDB_CONST_OBJ(svc)->name));
430 if (old) {
431 if (old->_last_update > last_update) {
432 sdb_log(SDB_LOG_DEBUG, "store: Cannot update service "
433 "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
434 svc->hostname, SDB_CONST_OBJ(svc)->name, last_update,
435 old->_last_update);
436 status = 1;
437 }
438 else {
439 old->_last_update = last_update;
440 }
441 }
442 else {
443 sdb_service_t *new = SDB_SVC(sdb_object_clone(SDB_CONST_OBJ(svc)));
444 if (! new) {
445 char errbuf[1024];
446 sdb_log(SDB_LOG_ERR, "store: Failed to clone service "
447 "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
448 pthread_rwlock_unlock(&host_lock);
449 return -1;
450 }
452 status = sdb_llist_insert_sorted(host->services, SDB_OBJ(new),
453 sdb_object_cmp_by_name);
455 /* pass control to the list or destroy in case of an error */
456 sdb_object_deref(SDB_OBJ(new));
457 }
459 pthread_rwlock_unlock(&host_lock);
460 return status;
461 } /* sdb_store_service */
463 const sdb_service_t *
464 sdb_store_get_service(const sdb_host_t *host, const char *name)
465 {
466 sdb_service_t *svc;
468 if ((! host) || (! name))
469 return NULL;
471 svc = SDB_SVC(sdb_llist_search_by_name(host->services, name));
472 if (! svc)
473 return NULL;
474 return svc;
475 } /* sdb_store_get_service */
477 int
478 sdb_store_dump(FILE *fh)
479 {
480 sdb_llist_iter_t *host_iter;
482 if (! fh)
483 return -1;
485 pthread_rwlock_rdlock(&host_lock);
487 host_iter = sdb_llist_get_iter(host_list);
488 if (! host_iter) {
489 pthread_rwlock_unlock(&host_lock);
490 return -1;
491 }
493 while (sdb_llist_iter_has_next(host_iter)) {
494 sdb_host_t *host = SDB_HOST(sdb_llist_iter_get_next(host_iter));
495 sdb_llist_iter_t *svc_iter;
496 sdb_llist_iter_t *attr_iter;
498 char time_str[64];
500 assert(host);
502 if (! sdb_strftime(time_str, sizeof(time_str),
503 "%F %T %z", host->_last_update))
504 snprintf(time_str, sizeof(time_str), "<error>");
505 time_str[sizeof(time_str) - 1] = '\0';
507 fprintf(fh, "Host '%s' (last updated: %s):\n",
508 SDB_OBJ(host)->name, time_str);
510 attr_iter = sdb_llist_get_iter(host->attributes);
511 if (! attr_iter) {
512 char errbuf[1024];
513 fprintf(fh, "Failed to retrieve attributes: %s\n",
514 sdb_strerror(errno, errbuf, sizeof(errbuf)));
515 continue;
516 }
518 while (sdb_llist_iter_has_next(attr_iter)) {
519 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
520 assert(attr);
522 if (! sdb_strftime(time_str, sizeof(time_str),
523 "%F %T %z", attr->_last_update))
524 snprintf(time_str, sizeof(time_str), "<error>");
525 time_str[sizeof(time_str) - 1] = '\0';
527 fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
528 SDB_OBJ(attr)->name, attr->attr_value, time_str);
529 }
531 sdb_llist_iter_destroy(attr_iter);
533 svc_iter = sdb_llist_get_iter(host->services);
534 if (! svc_iter) {
535 char errbuf[1024];
536 fprintf(fh, "Failed to retrieve services: %s\n",
537 sdb_strerror(errno, errbuf, sizeof(errbuf)));
538 continue;
539 }
541 while (sdb_llist_iter_has_next(svc_iter)) {
542 sdb_service_t *svc = SDB_SVC(sdb_llist_iter_get_next(svc_iter));
543 assert(svc);
545 if (! sdb_strftime(time_str, sizeof(time_str),
546 "%F %T %z", svc->_last_update))
547 snprintf(time_str, sizeof(time_str), "<error>");
548 time_str[sizeof(time_str) - 1] = '\0';
550 fprintf(fh, "\tService '%s' (last updated: %s)\n",
551 SDB_OBJ(svc)->name, time_str);
552 }
554 sdb_llist_iter_destroy(svc_iter);
555 }
557 sdb_llist_iter_destroy(host_iter);
558 pthread_rwlock_unlock(&host_lock);
559 return 0;
560 } /* sdb_store_dump */
562 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */