1*0a6a1f1dSLionel Sambuc /* $NetBSD: state.c,v 1.17 2015/06/02 14:02:10 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2015 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc * by Christos Zoulas.
9*0a6a1f1dSLionel Sambuc *
10*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc * are met:
13*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc *
19*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc */
31*0a6a1f1dSLionel Sambuc #ifdef HAVE_CONFIG_H
32*0a6a1f1dSLionel Sambuc #include "config.h"
33*0a6a1f1dSLionel Sambuc #endif
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
36*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: state.c,v 1.17 2015/06/02 14:02:10 christos Exp $");
37*0a6a1f1dSLionel Sambuc
38*0a6a1f1dSLionel Sambuc #include <sys/types.h>
39*0a6a1f1dSLionel Sambuc #include <sys/socket.h>
40*0a6a1f1dSLionel Sambuc #include <stdio.h>
41*0a6a1f1dSLionel Sambuc #include <string.h>
42*0a6a1f1dSLionel Sambuc #include <errno.h>
43*0a6a1f1dSLionel Sambuc #include <fcntl.h>
44*0a6a1f1dSLionel Sambuc #include <syslog.h>
45*0a6a1f1dSLionel Sambuc #include <netinet/in.h>
46*0a6a1f1dSLionel Sambuc
47*0a6a1f1dSLionel Sambuc #include "bl.h"
48*0a6a1f1dSLionel Sambuc #include "internal.h"
49*0a6a1f1dSLionel Sambuc #include "conf.h"
50*0a6a1f1dSLionel Sambuc #include "support.h"
51*0a6a1f1dSLionel Sambuc #include "state.h"
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel Sambuc static HASHINFO openinfo = {
54*0a6a1f1dSLionel Sambuc 4096, /* bsize */
55*0a6a1f1dSLionel Sambuc 32, /* ffactor */
56*0a6a1f1dSLionel Sambuc 256, /* nelem */
57*0a6a1f1dSLionel Sambuc 8 * 1024 * 1024,/* cachesize */
58*0a6a1f1dSLionel Sambuc NULL, /* hash() */
59*0a6a1f1dSLionel Sambuc 0 /* lorder */
60*0a6a1f1dSLionel Sambuc };
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambuc int
state_close(DB * db)63*0a6a1f1dSLionel Sambuc state_close(DB *db)
64*0a6a1f1dSLionel Sambuc {
65*0a6a1f1dSLionel Sambuc if (db == NULL)
66*0a6a1f1dSLionel Sambuc return -1;
67*0a6a1f1dSLionel Sambuc if ((*db->close)(db) == -1) {
68*0a6a1f1dSLionel Sambuc (*lfun)(LOG_ERR, "%s: can't close db (%m)", __func__);
69*0a6a1f1dSLionel Sambuc return -1;
70*0a6a1f1dSLionel Sambuc }
71*0a6a1f1dSLionel Sambuc return 0;
72*0a6a1f1dSLionel Sambuc }
73*0a6a1f1dSLionel Sambuc
74*0a6a1f1dSLionel Sambuc DB *
state_open(const char * dbname,int flags,mode_t perm)75*0a6a1f1dSLionel Sambuc state_open(const char *dbname, int flags, mode_t perm)
76*0a6a1f1dSLionel Sambuc {
77*0a6a1f1dSLionel Sambuc DB *db;
78*0a6a1f1dSLionel Sambuc
79*0a6a1f1dSLionel Sambuc #ifdef __APPLE__
80*0a6a1f1dSLionel Sambuc flags &= O_CREAT|O_EXCL|O_EXLOCK|O_NONBLOCK|O_RDONLY|
81*0a6a1f1dSLionel Sambuc O_RDWR|O_SHLOCK|O_TRUNC;
82*0a6a1f1dSLionel Sambuc #endif
83*0a6a1f1dSLionel Sambuc db = dbopen(dbname, flags, perm, DB_HASH, &openinfo);
84*0a6a1f1dSLionel Sambuc if (db == NULL) {
85*0a6a1f1dSLionel Sambuc if (errno == ENOENT && (flags & O_CREAT) == 0)
86*0a6a1f1dSLionel Sambuc return NULL;
87*0a6a1f1dSLionel Sambuc (*lfun)(LOG_ERR, "%s: can't open `%s' (%m)", __func__, dbname);
88*0a6a1f1dSLionel Sambuc }
89*0a6a1f1dSLionel Sambuc return db;
90*0a6a1f1dSLionel Sambuc }
91*0a6a1f1dSLionel Sambuc
92*0a6a1f1dSLionel Sambuc static int
state_sizecheck(const DBT * t)93*0a6a1f1dSLionel Sambuc state_sizecheck(const DBT *t)
94*0a6a1f1dSLionel Sambuc {
95*0a6a1f1dSLionel Sambuc if (sizeof(struct conf) == t->size)
96*0a6a1f1dSLionel Sambuc return 0;
97*0a6a1f1dSLionel Sambuc (*lfun)(LOG_ERR, "Key size mismatch %zu != %zu", sizeof(struct conf),
98*0a6a1f1dSLionel Sambuc t->size);
99*0a6a1f1dSLionel Sambuc return -1;
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc
102*0a6a1f1dSLionel Sambuc static void
dumpkey(const struct conf * k)103*0a6a1f1dSLionel Sambuc dumpkey(const struct conf *k)
104*0a6a1f1dSLionel Sambuc {
105*0a6a1f1dSLionel Sambuc char buf[10240];
106*0a6a1f1dSLionel Sambuc hexdump(buf, sizeof(buf), __func__, k, sizeof(*k));
107*0a6a1f1dSLionel Sambuc (*lfun)(LOG_DEBUG, "%s", buf);
108*0a6a1f1dSLionel Sambuc (*lfun)(LOG_DEBUG, "%s: %s", __func__,
109*0a6a1f1dSLionel Sambuc conf_print(buf, sizeof(buf), "", "", k));
110*0a6a1f1dSLionel Sambuc
111*0a6a1f1dSLionel Sambuc }
112*0a6a1f1dSLionel Sambuc
113*0a6a1f1dSLionel Sambuc int
state_del(DB * db,const struct conf * c)114*0a6a1f1dSLionel Sambuc state_del(DB *db, const struct conf *c)
115*0a6a1f1dSLionel Sambuc {
116*0a6a1f1dSLionel Sambuc int rv;
117*0a6a1f1dSLionel Sambuc DBT k;
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc if (db == NULL)
120*0a6a1f1dSLionel Sambuc return -1;
121*0a6a1f1dSLionel Sambuc
122*0a6a1f1dSLionel Sambuc k.data = __UNCONST(c);
123*0a6a1f1dSLionel Sambuc k.size = sizeof(*c);
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc switch (rv = (*db->del)(db, &k, 0)) {
126*0a6a1f1dSLionel Sambuc case 0:
127*0a6a1f1dSLionel Sambuc case 1:
128*0a6a1f1dSLionel Sambuc if (debug > 1) {
129*0a6a1f1dSLionel Sambuc (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
130*0a6a1f1dSLionel Sambuc (*db->sync)(db, 0);
131*0a6a1f1dSLionel Sambuc }
132*0a6a1f1dSLionel Sambuc return 0;
133*0a6a1f1dSLionel Sambuc default:
134*0a6a1f1dSLionel Sambuc (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
135*0a6a1f1dSLionel Sambuc return -1;
136*0a6a1f1dSLionel Sambuc }
137*0a6a1f1dSLionel Sambuc }
138*0a6a1f1dSLionel Sambuc
139*0a6a1f1dSLionel Sambuc int
state_get(DB * db,const struct conf * c,struct dbinfo * dbi)140*0a6a1f1dSLionel Sambuc state_get(DB *db, const struct conf *c, struct dbinfo *dbi)
141*0a6a1f1dSLionel Sambuc {
142*0a6a1f1dSLionel Sambuc int rv;
143*0a6a1f1dSLionel Sambuc DBT k, v;
144*0a6a1f1dSLionel Sambuc
145*0a6a1f1dSLionel Sambuc if (db == NULL)
146*0a6a1f1dSLionel Sambuc return -1;
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc k.data = __UNCONST(c);
149*0a6a1f1dSLionel Sambuc k.size = sizeof(*c);
150*0a6a1f1dSLionel Sambuc
151*0a6a1f1dSLionel Sambuc switch (rv = (*db->get)(db, &k, &v, 0)) {
152*0a6a1f1dSLionel Sambuc case 0:
153*0a6a1f1dSLionel Sambuc case 1:
154*0a6a1f1dSLionel Sambuc if (rv)
155*0a6a1f1dSLionel Sambuc memset(dbi, 0, sizeof(*dbi));
156*0a6a1f1dSLionel Sambuc else
157*0a6a1f1dSLionel Sambuc memcpy(dbi, v.data, sizeof(*dbi));
158*0a6a1f1dSLionel Sambuc if (debug > 1)
159*0a6a1f1dSLionel Sambuc (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
160*0a6a1f1dSLionel Sambuc return 0;
161*0a6a1f1dSLionel Sambuc default:
162*0a6a1f1dSLionel Sambuc (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
163*0a6a1f1dSLionel Sambuc return -1;
164*0a6a1f1dSLionel Sambuc }
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc int
state_put(DB * db,const struct conf * c,const struct dbinfo * dbi)168*0a6a1f1dSLionel Sambuc state_put(DB *db, const struct conf *c, const struct dbinfo *dbi)
169*0a6a1f1dSLionel Sambuc {
170*0a6a1f1dSLionel Sambuc int rv;
171*0a6a1f1dSLionel Sambuc DBT k, v;
172*0a6a1f1dSLionel Sambuc
173*0a6a1f1dSLionel Sambuc if (db == NULL)
174*0a6a1f1dSLionel Sambuc return -1;
175*0a6a1f1dSLionel Sambuc
176*0a6a1f1dSLionel Sambuc k.data = __UNCONST(c);
177*0a6a1f1dSLionel Sambuc k.size = sizeof(*c);
178*0a6a1f1dSLionel Sambuc v.data = __UNCONST(dbi);
179*0a6a1f1dSLionel Sambuc v.size = sizeof(*dbi);
180*0a6a1f1dSLionel Sambuc
181*0a6a1f1dSLionel Sambuc switch (rv = (*db->put)(db, &k, &v, 0)) {
182*0a6a1f1dSLionel Sambuc case 0:
183*0a6a1f1dSLionel Sambuc if (debug > 1) {
184*0a6a1f1dSLionel Sambuc (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
185*0a6a1f1dSLionel Sambuc (*db->sync)(db, 0);
186*0a6a1f1dSLionel Sambuc }
187*0a6a1f1dSLionel Sambuc return 0;
188*0a6a1f1dSLionel Sambuc case 1:
189*0a6a1f1dSLionel Sambuc errno = EEXIST;
190*0a6a1f1dSLionel Sambuc /*FALLTHROUGH*/
191*0a6a1f1dSLionel Sambuc default:
192*0a6a1f1dSLionel Sambuc (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
193*0a6a1f1dSLionel Sambuc return -1;
194*0a6a1f1dSLionel Sambuc }
195*0a6a1f1dSLionel Sambuc }
196*0a6a1f1dSLionel Sambuc
197*0a6a1f1dSLionel Sambuc int
state_iterate(DB * db,struct conf * c,struct dbinfo * dbi,unsigned int first)198*0a6a1f1dSLionel Sambuc state_iterate(DB *db, struct conf *c, struct dbinfo *dbi, unsigned int first)
199*0a6a1f1dSLionel Sambuc {
200*0a6a1f1dSLionel Sambuc int rv;
201*0a6a1f1dSLionel Sambuc DBT k, v;
202*0a6a1f1dSLionel Sambuc
203*0a6a1f1dSLionel Sambuc if (db == NULL)
204*0a6a1f1dSLionel Sambuc return -1;
205*0a6a1f1dSLionel Sambuc
206*0a6a1f1dSLionel Sambuc first = first ? R_FIRST : R_NEXT;
207*0a6a1f1dSLionel Sambuc
208*0a6a1f1dSLionel Sambuc switch (rv = (*db->seq)(db, &k, &v, first)) {
209*0a6a1f1dSLionel Sambuc case 0:
210*0a6a1f1dSLionel Sambuc if (state_sizecheck(&k) == -1)
211*0a6a1f1dSLionel Sambuc return -1;
212*0a6a1f1dSLionel Sambuc memcpy(c, k.data, sizeof(*c));
213*0a6a1f1dSLionel Sambuc if (debug > 2)
214*0a6a1f1dSLionel Sambuc dumpkey(c);
215*0a6a1f1dSLionel Sambuc memcpy(dbi, v.data, sizeof(*dbi));
216*0a6a1f1dSLionel Sambuc if (debug > 1)
217*0a6a1f1dSLionel Sambuc (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
218*0a6a1f1dSLionel Sambuc return 1;
219*0a6a1f1dSLionel Sambuc case 1:
220*0a6a1f1dSLionel Sambuc if (debug > 1)
221*0a6a1f1dSLionel Sambuc (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
222*0a6a1f1dSLionel Sambuc return 0;
223*0a6a1f1dSLionel Sambuc default:
224*0a6a1f1dSLionel Sambuc (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
225*0a6a1f1dSLionel Sambuc return -1;
226*0a6a1f1dSLionel Sambuc }
227*0a6a1f1dSLionel Sambuc }
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc int
state_sync(DB * db)230*0a6a1f1dSLionel Sambuc state_sync(DB *db)
231*0a6a1f1dSLionel Sambuc {
232*0a6a1f1dSLionel Sambuc return (*db->sync)(db, 0);
233*0a6a1f1dSLionel Sambuc }
234