2 8d1b399b 2021-07-22 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 8d1b399b 2021-07-22 op * Permission to use, copy, modify, and distribute this software for any
5 8d1b399b 2021-07-22 op * purpose with or without fee is hereby granted, provided that the above
6 8d1b399b 2021-07-22 op * copyright notice and this permission notice appear in all copies.
8 8d1b399b 2021-07-22 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8d1b399b 2021-07-22 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8d1b399b 2021-07-22 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8d1b399b 2021-07-22 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8d1b399b 2021-07-22 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8d1b399b 2021-07-22 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8d1b399b 2021-07-22 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8d1b399b 2021-07-22 op #include "compat.h"
19 8d1b399b 2021-07-22 op #include <stdlib.h>
20 8d1b399b 2021-07-22 op #include <string.h>
22 8d1b399b 2021-07-22 op #include "log.h"
23 8d1b399b 2021-07-22 op #include "table.h"
24 8d1b399b 2021-07-22 op #include "utils.h"
27 8d1b399b 2021-07-22 op table_open(struct kd_conf *conf, const char *name, const char *type,
28 8d1b399b 2021-07-22 op const char *path)
30 8d1b399b 2021-07-22 op struct table *t;
31 8d1b399b 2021-07-22 op struct kd_tables_conf *entry;
32 9efa7b7a 2021-07-22 op struct table_backend *backends[] = {
33 9efa7b7a 2021-07-22 op &table_static,
38 9efa7b7a 2021-07-22 op for (i = 0; backends[i] != NULL; ++i) {
39 9efa7b7a 2021-07-22 op b = backends[i];
40 8d1b399b 2021-07-22 op if (!strcmp(type, b->name))
43 8d1b399b 2021-07-22 op log_warn("unknown table type %s", type);
47 8d1b399b 2021-07-22 op if (b->open == NULL) {
48 8d1b399b 2021-07-22 op log_warn("can't open table %s (type %s)",
49 8d1b399b 2021-07-22 op name, b->name);
53 8d1b399b 2021-07-22 op t = xcalloc(1, sizeof(*t));
54 8d1b399b 2021-07-22 op strlcpy(t->t_name, name, sizeof(t->t_name));
55 8d1b399b 2021-07-22 op if (path != NULL)
56 8d1b399b 2021-07-22 op strlcpy(t->t_path, path, sizeof(t->t_path));
57 8d1b399b 2021-07-22 op t->t_backend = b;
59 8d1b399b 2021-07-22 op if (t->t_backend->open(t) == -1)
60 8d1b399b 2021-07-22 op fatal("can't open table %s (type %s)",
63 8d1b399b 2021-07-22 op entry = xcalloc(1, sizeof(*entry));
64 8d1b399b 2021-07-22 op entry->table = t;
65 c25feded 2021-07-26 op STAILQ_INSERT_HEAD(&conf->table_head, entry, entry);
70 8d1b399b 2021-07-22 op table_add(struct table *t, const char *key, const char *val)
72 8d1b399b 2021-07-22 op if (t->t_backend->add == NULL) {
73 8d1b399b 2021-07-22 op log_warn("can't add to table %s (type %s)",
74 8d1b399b 2021-07-22 op t->t_name, t->t_backend->name);
78 8d1b399b 2021-07-22 op return t->t_backend->add(t, key, val);
82 8d1b399b 2021-07-22 op table_lookup(struct table *t, const char *key, char **ret_val)
84 8d1b399b 2021-07-22 op if (t->t_backend->lookup == NULL) {
85 8d1b399b 2021-07-22 op log_warn("can't lookup table %s (type %s)",
86 8d1b399b 2021-07-22 op t->t_name, t->t_backend->name);
90 8d1b399b 2021-07-22 op return t->t_backend->lookup(t, key, ret_val);
94 8d1b399b 2021-07-22 op table_close(struct table *t)
96 8d1b399b 2021-07-22 op if (t->t_backend->close != NULL)
97 8d1b399b 2021-07-22 op t->t_backend->close(t);