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 fb1a36c0 2022-01-09 op #include <sys/types.h>
18 fb1a36c0 2022-01-09 op #include <sys/queue.h>
19 fb1a36c0 2022-01-09 op #include <sys/uio.h>
20 fb1a36c0 2022-01-09 op
21 fb1a36c0 2022-01-09 op #include <event.h>
22 fb1a36c0 2022-01-09 op #include <stdint.h>
23 fb1a36c0 2022-01-09 op #include <stdlib.h>
24 fb1a36c0 2022-01-09 op #include <string.h>
25 fb1a36c0 2022-01-09 op #include <imsg.h>
26 fb1a36c0 2022-01-09 op
27 fb1a36c0 2022-01-09 op #include "log.h"
28 fb1a36c0 2022-01-09 op #include "utils.h"
29 fb1a36c0 2022-01-09 op #include "kamid.h"
30 fb1a36c0 2022-01-09 op
31 fb1a36c0 2022-01-09 op #include "table.h"
32 fb1a36c0 2022-01-09 op
33 fb1a36c0 2022-01-09 op int
34 fb1a36c0 2022-01-09 op table_open(struct kd_conf *conf, const char *name, const char *type,
35 fb1a36c0 2022-01-09 op const char *path)
36 fb1a36c0 2022-01-09 op {
37 fb1a36c0 2022-01-09 op struct table *t;
38 fb1a36c0 2022-01-09 op struct kd_tables_conf *entry;
39 fb1a36c0 2022-01-09 op struct table_backend *backends[] = {
40 fb1a36c0 2022-01-09 op &table_static,
41 fb1a36c0 2022-01-09 op NULL,
42 fb1a36c0 2022-01-09 op }, *b;
43 fb1a36c0 2022-01-09 op size_t i;
44 fb1a36c0 2022-01-09 op
45 fb1a36c0 2022-01-09 op for (i = 0; backends[i] != NULL; ++i) {
46 fb1a36c0 2022-01-09 op b = backends[i];
47 fb1a36c0 2022-01-09 op if (!strcmp(type, b->name))
48 fb1a36c0 2022-01-09 op goto found;
49 fb1a36c0 2022-01-09 op }
50 fb1a36c0 2022-01-09 op log_warn("unknown table type %s", type);
51 fb1a36c0 2022-01-09 op return -1;
52 fb1a36c0 2022-01-09 op
53 fb1a36c0 2022-01-09 op found:
54 fb1a36c0 2022-01-09 op if (b->open == NULL) {
55 fb1a36c0 2022-01-09 op log_warn("can't open table %s (type %s)",
56 fb1a36c0 2022-01-09 op name, b->name);
57 fb1a36c0 2022-01-09 op return -1;
58 fb1a36c0 2022-01-09 op }
59 fb1a36c0 2022-01-09 op
60 fb1a36c0 2022-01-09 op t = xcalloc(1, sizeof(*t));
61 fb1a36c0 2022-01-09 op strlcpy(t->t_name, name, sizeof(t->t_name));
62 fb1a36c0 2022-01-09 op if (path != NULL)
63 fb1a36c0 2022-01-09 op strlcpy(t->t_path, path, sizeof(t->t_path));
64 fb1a36c0 2022-01-09 op t->t_backend = b;
65 fb1a36c0 2022-01-09 op
66 fb1a36c0 2022-01-09 op if (t->t_backend->open(t) == -1)
67 fb1a36c0 2022-01-09 op fatal("can't open table %s (type %s)",
68 fb1a36c0 2022-01-09 op name, path);
69 fb1a36c0 2022-01-09 op
70 fb1a36c0 2022-01-09 op entry = xcalloc(1, sizeof(*entry));
71 fb1a36c0 2022-01-09 op entry->table = t;
72 fb1a36c0 2022-01-09 op STAILQ_INSERT_HEAD(&conf->table_head, entry, entry);
73 fb1a36c0 2022-01-09 op return 0;
74 fb1a36c0 2022-01-09 op }
75 fb1a36c0 2022-01-09 op
76 fb1a36c0 2022-01-09 op int
77 fb1a36c0 2022-01-09 op table_add(struct table *t, const char *key, const char *val)
78 fb1a36c0 2022-01-09 op {
79 fb1a36c0 2022-01-09 op if (t->t_backend->add == NULL) {
80 fb1a36c0 2022-01-09 op log_warn("can't add to table %s (type %s)",
81 fb1a36c0 2022-01-09 op t->t_name, t->t_backend->name);
82 fb1a36c0 2022-01-09 op return -1;
83 fb1a36c0 2022-01-09 op }
84 fb1a36c0 2022-01-09 op
85 fb1a36c0 2022-01-09 op return t->t_backend->add(t, key, val);
86 fb1a36c0 2022-01-09 op }
87 fb1a36c0 2022-01-09 op
88 fb1a36c0 2022-01-09 op int
89 fb1a36c0 2022-01-09 op table_lookup(struct table *t, const char *key, char **ret_val)
90 fb1a36c0 2022-01-09 op {
91 fb1a36c0 2022-01-09 op if (t->t_backend->lookup == NULL) {
92 fb1a36c0 2022-01-09 op log_warn("can't lookup table %s (type %s)",
93 fb1a36c0 2022-01-09 op t->t_name, t->t_backend->name);
94 fb1a36c0 2022-01-09 op return -1;
95 fb1a36c0 2022-01-09 op }
96 fb1a36c0 2022-01-09 op
97 fb1a36c0 2022-01-09 op return t->t_backend->lookup(t, key, ret_val);
98 fb1a36c0 2022-01-09 op }
99 fb1a36c0 2022-01-09 op
100 fb1a36c0 2022-01-09 op void
101 fb1a36c0 2022-01-09 op table_close(struct table *t)
102 fb1a36c0 2022-01-09 op {
103 fb1a36c0 2022-01-09 op if (t->t_backend->close != NULL)
104 fb1a36c0 2022-01-09 op t->t_backend->close(t);
105 fb1a36c0 2022-01-09 op }