1 /* $NetBSD: state.c,v 1.19 2016/09/26 19:43:43 christos Exp $ */
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: state.c,v 1.19 2016/09/26 19:43:43 christos Exp $");
38 #include <sys/types.h>
39 #include <sys/socket.h>
45 #include <netinet/in.h>
53 static HASHINFO openinfo = {
57 8 * 1024 * 1024,/* cachesize */
67 if ((*db->close)(db) == -1) {
68 (*lfun)(LOG_ERR, "%s: can't close db (%m)", __func__);
75 state_open(const char *dbname, int flags, mode_t perm)
80 flags &= O_CREAT|O_EXCL|O_EXLOCK|O_NONBLOCK|O_RDONLY|
81 O_RDWR|O_SHLOCK|O_TRUNC;
83 db = dbopen(dbname, flags, perm, DB_HASH, &openinfo);
85 if (errno == ENOENT && (flags & O_CREAT) == 0)
87 (*lfun)(LOG_ERR, "%s: can't open `%s' (%m)", __func__, dbname);
93 state_sizecheck(const DBT *t)
95 if (sizeof(struct conf) == t->size)
97 (*lfun)(LOG_ERR, "Key size mismatch %zu != %zu", sizeof(struct conf),
103 dumpkey(const struct conf *k)
106 blhexdump(buf, sizeof(buf), __func__, k, sizeof(*k));
107 (*lfun)(LOG_DEBUG, "%s", buf);
108 (*lfun)(LOG_DEBUG, "%s: %s", __func__,
109 conf_print(buf, sizeof(buf), "", "", k));
114 state_del(DB *db, const struct conf *c)
122 k.data = __UNCONST(c);
125 switch (rv = (*db->del)(db, &k, 0)) {
129 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
134 (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
140 state_get(DB *db, const struct conf *c, struct dbinfo *dbi)
148 k.data = __UNCONST(c);
151 switch (rv = (*db->get)(db, &k, &v, 0)) {
155 memset(dbi, 0, sizeof(*dbi));
157 memcpy(dbi, v.data, sizeof(*dbi));
159 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
162 (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
168 state_put(DB *db, const struct conf *c, const struct dbinfo *dbi)
176 k.data = __UNCONST(c);
178 v.data = __UNCONST(dbi);
179 v.size = sizeof(*dbi);
181 switch (rv = (*db->put)(db, &k, &v, 0)) {
184 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
192 (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
198 state_iterate(DB *db, struct conf *c, struct dbinfo *dbi, unsigned int first)
204 (*lfun)(LOG_ERR, "%s: called with no database file", __func__);
208 first = first ? R_FIRST : R_NEXT;
210 switch (rv = (*db->seq)(db, &k, &v, first)) {
212 if (state_sizecheck(&k) == -1)
214 memcpy(c, k.data, sizeof(*c));
217 memcpy(dbi, v.data, sizeof(*dbi));
219 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
223 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
226 (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
234 return (*db->sync)(db, 0);