2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <sys/uio.h>
21 fb1a36c0 2022-01-09 op #include <stdint.h>
22 fb1a36c0 2022-01-09 op #include <stdlib.h>
23 fb1a36c0 2022-01-09 op #include <string.h>
25 fb1a36c0 2022-01-09 op #include "log.h"
26 fb1a36c0 2022-01-09 op #include "utils.h"
27 fb1a36c0 2022-01-09 op #include "kamid.h"
29 fb1a36c0 2022-01-09 op #include "table.h"
32 fb1a36c0 2022-01-09 op table_open(struct kd_conf *conf, const char *name, const char *type,
33 fb1a36c0 2022-01-09 op const char *path)
35 fb1a36c0 2022-01-09 op struct table *t;
36 fb1a36c0 2022-01-09 op struct kd_tables_conf *entry;
37 fb1a36c0 2022-01-09 op struct table_backend *backends[] = {
38 fb1a36c0 2022-01-09 op &table_static,
43 fb1a36c0 2022-01-09 op for (i = 0; backends[i] != NULL; ++i) {
44 fb1a36c0 2022-01-09 op b = backends[i];
45 fb1a36c0 2022-01-09 op if (!strcmp(type, b->name))
48 fb1a36c0 2022-01-09 op log_warn("unknown table type %s", type);
52 fb1a36c0 2022-01-09 op if (b->open == NULL) {
53 fb1a36c0 2022-01-09 op log_warn("can't open table %s (type %s)",
54 fb1a36c0 2022-01-09 op name, b->name);
58 fb1a36c0 2022-01-09 op t = xcalloc(1, sizeof(*t));
59 fb1a36c0 2022-01-09 op strlcpy(t->t_name, name, sizeof(t->t_name));
60 fb1a36c0 2022-01-09 op if (path != NULL)
61 fb1a36c0 2022-01-09 op strlcpy(t->t_path, path, sizeof(t->t_path));
62 fb1a36c0 2022-01-09 op t->t_backend = b;
64 fb1a36c0 2022-01-09 op if (t->t_backend->open(t) == -1)
65 fb1a36c0 2022-01-09 op fatal("can't open table %s (type %s)",
68 fb1a36c0 2022-01-09 op entry = xcalloc(1, sizeof(*entry));
69 fb1a36c0 2022-01-09 op entry->table = t;
70 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&conf->table_head, entry, entry);
75 fb1a36c0 2022-01-09 op table_add(struct table *t, const char *key, const char *val)
77 fb1a36c0 2022-01-09 op if (t->t_backend->add == NULL) {
78 fb1a36c0 2022-01-09 op log_warn("can't add to table %s (type %s)",
79 fb1a36c0 2022-01-09 op t->t_name, t->t_backend->name);
83 fb1a36c0 2022-01-09 op return t->t_backend->add(t, key, val);
87 fb1a36c0 2022-01-09 op table_lookup(struct table *t, const char *key, char **ret_val)
89 fb1a36c0 2022-01-09 op if (t->t_backend->lookup == NULL) {
90 fb1a36c0 2022-01-09 op log_warn("can't lookup table %s (type %s)",
91 fb1a36c0 2022-01-09 op t->t_name, t->t_backend->name);
95 fb1a36c0 2022-01-09 op return t->t_backend->lookup(t, key, ret_val);
99 fb1a36c0 2022-01-09 op table_close(struct table *t)
101 fb1a36c0 2022-01-09 op if (t->t_backend->close != NULL)
102 fb1a36c0 2022-01-09 op t->t_backend->close(t);