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_SVC(sdb_object_create(obj->name, sdb_service_type,
185 svc->hostname));
186 if (! new)
187 return NULL;
189 new->_last_update = svc->_last_update;
190 return SDB_OBJ(new);
191 } /* sdb_svc_clone */
193 const sdb_type_t sdb_host_type = {
194 sizeof(sdb_host_t),
196 sdb_host_init,
197 sdb_host_destroy,
198 sdb_host_do_clone
199 };
201 const sdb_type_t sdb_attribute_type = {
202 sizeof(sdb_attribute_t),
204 sdb_attr_init,
205 sdb_attr_destroy,
206 sdb_attr_clone
207 };
209 const sdb_type_t sdb_service_type = {
210 sizeof(sdb_service_t),
212 sdb_svc_init,
213 sdb_svc_destroy,
214 sdb_svc_clone
215 };
217 /*
218 * public API
219 */
221 int
222 sdb_store_host(const char *name, sdb_time_t last_update)
223 {
224 sdb_host_t *old;
226 char *cname;
227 int status = 0;
229 if (! name)
230 return -1;
232 cname = sdb_plugin_cname(strdup(name));
233 if (! cname) {
234 sdb_log(SDB_LOG_ERR, "store: strdup failed");
235 return -1;
236 }
238 last_update = last_update;
239 if (last_update <= 0)
240 last_update = sdb_gettime();
242 pthread_rwlock_wrlock(&host_lock);
244 if (! host_list) {
245 if (! (host_list = sdb_llist_create())) {
246 pthread_rwlock_unlock(&host_lock);
247 return -1;
248 }
249 }
251 old = SDB_HOST(sdb_llist_search_by_name(host_list, cname));
253 if (old) {
254 if (old->_last_update > last_update) {
255 sdb_log(SDB_LOG_DEBUG, "store: Cannot update host '%s' - "
256 "value too old (%"PRIscTIME" < %"PRIscTIME")",
257 cname, last_update, old->_last_update);
258 /* don't report an error; the host may be updated by multiple
259 * backends */
260 status = 1;
261 }
262 else {
263 old->_last_update = last_update;
264 }
265 }
266 else {
267 sdb_host_t *new = SDB_HOST(sdb_object_create(name, sdb_host_type));
268 if (! new) {
269 char errbuf[1024];
270 sdb_log(SDB_LOG_ERR, "store: Failed to clone host object: %s",
271 sdb_strerror(errno, errbuf, sizeof(errbuf)));
272 pthread_rwlock_unlock(&host_lock);
273 return -1;
274 }
276 free(SDB_OBJ(new)->name);
277 SDB_OBJ(new)->name = cname;
279 if (! new->attributes) {
280 if (! (new->attributes = sdb_llist_create())) {
281 char errbuf[1024];
282 sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
283 "host object '%s': %s", SDB_OBJ(new)->name,
284 sdb_strerror(errno, errbuf, sizeof(errbuf)));
285 sdb_object_deref(SDB_OBJ(new));
286 pthread_rwlock_unlock(&host_lock);
287 return -1;
288 }
289 }
291 if (! new->services) {
292 if (! (new->services = sdb_llist_create())) {
293 char errbuf[1024];
294 sdb_log(SDB_LOG_ERR, "store: Failed to initialize "
295 "host object '%s': %s", SDB_OBJ(new)->name,
296 sdb_strerror(errno, errbuf, sizeof(errbuf)));
297 sdb_object_deref(SDB_OBJ(new));
298 pthread_rwlock_unlock(&host_lock);
299 return -1;
300 }
301 }
303 status = sdb_llist_insert_sorted(host_list, SDB_OBJ(new),
304 sdb_object_cmp_by_name);
306 /* pass control to the list or destroy in case of an error */
307 sdb_object_deref(SDB_OBJ(new));
308 }
310 pthread_rwlock_unlock(&host_lock);
311 return status;
312 } /* sdb_store_host */
314 _Bool
315 sdb_store_has_host(const char *name)
316 {
317 sdb_host_t *host;
319 if (! name)
320 return NULL;
322 host = SDB_HOST(sdb_llist_search_by_name(host_list, name));
323 return host != NULL;
324 } /* sdb_store_has_host */
326 int
327 sdb_store_attribute(const char *hostname, const char *key, const char *value,
328 sdb_time_t last_update)
329 {
330 sdb_host_t *host;
331 sdb_attribute_t *old;
333 int status = 0;
335 if ((! hostname) || (! key))
336 return -1;
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, 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, key));
353 if (old) {
354 if (old->_last_update > last_update) {
355 sdb_log(SDB_LOG_DEBUG, "store: Cannot update attribute "
356 "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
357 hostname, key, last_update, old->_last_update);
358 status = 1;
359 }
360 else {
361 old->_last_update = last_update;
362 }
363 }
364 else {
365 sdb_attribute_t *new = SDB_ATTR(sdb_object_create(key,
366 sdb_attribute_type, hostname, value));
367 if (! new) {
368 char errbuf[1024];
369 sdb_log(SDB_LOG_ERR, "store: Failed to clone attribute "
370 "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
371 pthread_rwlock_unlock(&host_lock);
372 return -1;
373 }
375 status = sdb_llist_insert_sorted(host->attributes, SDB_OBJ(new),
376 sdb_object_cmp_by_name);
378 /* pass control to the list or destroy in case of an error */
379 sdb_object_deref(SDB_OBJ(new));
380 }
382 pthread_rwlock_unlock(&host_lock);
383 return status;
384 } /* sdb_store_attribute */
386 int
387 sdb_store_service(const sdb_service_t *svc)
388 {
389 sdb_host_t *host;
390 sdb_service_t *old;
391 sdb_time_t last_update;
393 int status = 0;
395 if (! svc)
396 return -1;
398 last_update = svc->_last_update;
399 if (last_update <= 0)
400 last_update = sdb_gettime();
402 if (! host_list)
403 return -1;
405 pthread_rwlock_wrlock(&host_lock);
407 host = SDB_HOST(sdb_llist_search_by_name(host_list, svc->hostname));
408 if (! host) {
409 pthread_rwlock_unlock(&host_lock);
410 return -1;
411 }
413 old = SDB_SVC(sdb_llist_search_by_name(host->services,
414 SDB_CONST_OBJ(svc)->name));
415 if (old) {
416 if (old->_last_update > last_update) {
417 sdb_log(SDB_LOG_DEBUG, "store: Cannot update service "
418 "'%s/%s' - value too old (%"PRIscTIME" < %"PRIscTIME")",
419 svc->hostname, SDB_CONST_OBJ(svc)->name, last_update,
420 old->_last_update);
421 status = 1;
422 }
423 else {
424 old->_last_update = last_update;
425 }
426 }
427 else {
428 sdb_service_t *new = SDB_SVC(sdb_object_clone(SDB_CONST_OBJ(svc)));
429 if (! new) {
430 char errbuf[1024];
431 sdb_log(SDB_LOG_ERR, "store: Failed to clone service "
432 "object: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
433 pthread_rwlock_unlock(&host_lock);
434 return -1;
435 }
437 status = sdb_llist_insert_sorted(host->services, SDB_OBJ(new),
438 sdb_object_cmp_by_name);
440 /* pass control to the list or destroy in case of an error */
441 sdb_object_deref(SDB_OBJ(new));
442 }
444 pthread_rwlock_unlock(&host_lock);
445 return status;
446 } /* sdb_store_service */
448 int
449 sdb_store_dump(FILE *fh)
450 {
451 sdb_llist_iter_t *host_iter;
453 if (! fh)
454 return -1;
456 pthread_rwlock_rdlock(&host_lock);
458 host_iter = sdb_llist_get_iter(host_list);
459 if (! host_iter) {
460 pthread_rwlock_unlock(&host_lock);
461 return -1;
462 }
464 while (sdb_llist_iter_has_next(host_iter)) {
465 sdb_host_t *host = SDB_HOST(sdb_llist_iter_get_next(host_iter));
466 sdb_llist_iter_t *svc_iter;
467 sdb_llist_iter_t *attr_iter;
469 char time_str[64];
471 assert(host);
473 if (! sdb_strftime(time_str, sizeof(time_str),
474 "%F %T %z", host->_last_update))
475 snprintf(time_str, sizeof(time_str), "<error>");
476 time_str[sizeof(time_str) - 1] = '\0';
478 fprintf(fh, "Host '%s' (last updated: %s):\n",
479 SDB_OBJ(host)->name, time_str);
481 attr_iter = sdb_llist_get_iter(host->attributes);
482 if (! attr_iter) {
483 char errbuf[1024];
484 fprintf(fh, "Failed to retrieve attributes: %s\n",
485 sdb_strerror(errno, errbuf, sizeof(errbuf)));
486 continue;
487 }
489 while (sdb_llist_iter_has_next(attr_iter)) {
490 sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
491 assert(attr);
493 if (! sdb_strftime(time_str, sizeof(time_str),
494 "%F %T %z", attr->_last_update))
495 snprintf(time_str, sizeof(time_str), "<error>");
496 time_str[sizeof(time_str) - 1] = '\0';
498 fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
499 SDB_OBJ(attr)->name, attr->attr_value, time_str);
500 }
502 sdb_llist_iter_destroy(attr_iter);
504 svc_iter = sdb_llist_get_iter(host->services);
505 if (! svc_iter) {
506 char errbuf[1024];
507 fprintf(fh, "Failed to retrieve services: %s\n",
508 sdb_strerror(errno, errbuf, sizeof(errbuf)));
509 continue;
510 }
512 while (sdb_llist_iter_has_next(svc_iter)) {
513 sdb_service_t *svc = SDB_SVC(sdb_llist_iter_get_next(svc_iter));
514 assert(svc);
516 if (! sdb_strftime(time_str, sizeof(time_str),
517 "%F %T %z", svc->_last_update))
518 snprintf(time_str, sizeof(time_str), "<error>");
519 time_str[sizeof(time_str) - 1] = '\0';
521 fprintf(fh, "\tService '%s' (last updated: %s)\n",
522 SDB_OBJ(svc)->name, time_str);
523 }
525 sdb_llist_iter_destroy(svc_iter);
526 }
528 sdb_llist_iter_destroy(host_iter);
529 pthread_rwlock_unlock(&host_lock);
530 return 0;
531 } /* sdb_store_dump */
533 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */