Blame


1 fb1a36c0 2022-01-09 op /*
2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 fb1a36c0 2022-01-09 op *
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.
7 fb1a36c0 2022-01-09 op *
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.
15 fb1a36c0 2022-01-09 op */
16 fb1a36c0 2022-01-09 op
17 bbcba3ed 2022-01-10 op #include "compat.h"
18 bbcba3ed 2022-01-10 op
19 fb1a36c0 2022-01-09 op #include <sys/uio.h>
20 fb1a36c0 2022-01-09 op
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>
24 fb1a36c0 2022-01-09 op
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"
28 fb1a36c0 2022-01-09 op
29 fb1a36c0 2022-01-09 op #include "table.h"
30 fb1a36c0 2022-01-09 op
31 fb1a36c0 2022-01-09 op int
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)
34 fb1a36c0 2022-01-09 op {
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,
39 fb1a36c0 2022-01-09 op NULL,
40 fb1a36c0 2022-01-09 op }, *b;
41 fb1a36c0 2022-01-09 op size_t i;
42 fb1a36c0 2022-01-09 op
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))
46 fb1a36c0 2022-01-09 op goto found;
47 fb1a36c0 2022-01-09 op }
48 fb1a36c0 2022-01-09 op log_warn("unknown table type %s", type);
49 fb1a36c0 2022-01-09 op return -1;
50 fb1a36c0 2022-01-09 op
51 fb1a36c0 2022-01-09 op found:
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);
55 fb1a36c0 2022-01-09 op return -1;
56 fb1a36c0 2022-01-09 op }
57 fb1a36c0 2022-01-09 op
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;
63 fb1a36c0 2022-01-09 op
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)",
66 fb1a36c0 2022-01-09 op name, path);
67 fb1a36c0 2022-01-09 op
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);
71 fb1a36c0 2022-01-09 op return 0;
72 fb1a36c0 2022-01-09 op }
73 fb1a36c0 2022-01-09 op
74 fb1a36c0 2022-01-09 op int
75 fb1a36c0 2022-01-09 op table_add(struct table *t, const char *key, const char *val)
76 fb1a36c0 2022-01-09 op {
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);
80 fb1a36c0 2022-01-09 op return -1;
81 fb1a36c0 2022-01-09 op }
82 fb1a36c0 2022-01-09 op
83 fb1a36c0 2022-01-09 op return t->t_backend->add(t, key, val);
84 fb1a36c0 2022-01-09 op }
85 fb1a36c0 2022-01-09 op
86 fb1a36c0 2022-01-09 op int
87 fb1a36c0 2022-01-09 op table_lookup(struct table *t, const char *key, char **ret_val)
88 fb1a36c0 2022-01-09 op {
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);
92 fb1a36c0 2022-01-09 op return -1;
93 fb1a36c0 2022-01-09 op }
94 fb1a36c0 2022-01-09 op
95 fb1a36c0 2022-01-09 op return t->t_backend->lookup(t, key, ret_val);
96 fb1a36c0 2022-01-09 op }
97 fb1a36c0 2022-01-09 op
98 fb1a36c0 2022-01-09 op void
99 fb1a36c0 2022-01-09 op table_close(struct table *t)
100 fb1a36c0 2022-01-09 op {
101 fb1a36c0 2022-01-09 op if (t->t_backend->close != NULL)
102 fb1a36c0 2022-01-09 op t->t_backend->close(t);
103 fb1a36c0 2022-01-09 op }