etc/services - sync with NetBSD-8
[minix.git] / external / bsd / blacklist / bin / state.c
blob1f545a64552c2e88026bd1a9b2882ecbb03ee4c7
1 /* $NetBSD: state.c,v 1.17 2015/06/02 14:02:10 christos Exp $ */
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: state.c,v 1.17 2015/06/02 14:02:10 christos Exp $");
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <syslog.h>
45 #include <netinet/in.h>
47 #include "bl.h"
48 #include "internal.h"
49 #include "conf.h"
50 #include "support.h"
51 #include "state.h"
53 static HASHINFO openinfo = {
54 4096, /* bsize */
55 32, /* ffactor */
56 256, /* nelem */
57 8 * 1024 * 1024,/* cachesize */
58 NULL, /* hash() */
59 0 /* lorder */
62 int
63 state_close(DB *db)
65 if (db == NULL)
66 return -1;
67 if ((*db->close)(db) == -1) {
68 (*lfun)(LOG_ERR, "%s: can't close db (%m)", __func__);
69 return -1;
71 return 0;
74 DB *
75 state_open(const char *dbname, int flags, mode_t perm)
77 DB *db;
79 #ifdef __APPLE__
80 flags &= O_CREAT|O_EXCL|O_EXLOCK|O_NONBLOCK|O_RDONLY|
81 O_RDWR|O_SHLOCK|O_TRUNC;
82 #endif
83 db = dbopen(dbname, flags, perm, DB_HASH, &openinfo);
84 if (db == NULL) {
85 if (errno == ENOENT && (flags & O_CREAT) == 0)
86 return NULL;
87 (*lfun)(LOG_ERR, "%s: can't open `%s' (%m)", __func__, dbname);
89 return db;
92 static int
93 state_sizecheck(const DBT *t)
95 if (sizeof(struct conf) == t->size)
96 return 0;
97 (*lfun)(LOG_ERR, "Key size mismatch %zu != %zu", sizeof(struct conf),
98 t->size);
99 return -1;
102 static void
103 dumpkey(const struct conf *k)
105 char buf[10240];
106 hexdump(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)
116 int rv;
117 DBT k;
119 if (db == NULL)
120 return -1;
122 k.data = __UNCONST(c);
123 k.size = sizeof(*c);
125 switch (rv = (*db->del)(db, &k, 0)) {
126 case 0:
127 case 1:
128 if (debug > 1) {
129 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
130 (*db->sync)(db, 0);
132 return 0;
133 default:
134 (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
135 return -1;
140 state_get(DB *db, const struct conf *c, struct dbinfo *dbi)
142 int rv;
143 DBT k, v;
145 if (db == NULL)
146 return -1;
148 k.data = __UNCONST(c);
149 k.size = sizeof(*c);
151 switch (rv = (*db->get)(db, &k, &v, 0)) {
152 case 0:
153 case 1:
154 if (rv)
155 memset(dbi, 0, sizeof(*dbi));
156 else
157 memcpy(dbi, v.data, sizeof(*dbi));
158 if (debug > 1)
159 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
160 return 0;
161 default:
162 (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
163 return -1;
168 state_put(DB *db, const struct conf *c, const struct dbinfo *dbi)
170 int rv;
171 DBT k, v;
173 if (db == NULL)
174 return -1;
176 k.data = __UNCONST(c);
177 k.size = sizeof(*c);
178 v.data = __UNCONST(dbi);
179 v.size = sizeof(*dbi);
181 switch (rv = (*db->put)(db, &k, &v, 0)) {
182 case 0:
183 if (debug > 1) {
184 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
185 (*db->sync)(db, 0);
187 return 0;
188 case 1:
189 errno = EEXIST;
190 /*FALLTHROUGH*/
191 default:
192 (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
193 return -1;
198 state_iterate(DB *db, struct conf *c, struct dbinfo *dbi, unsigned int first)
200 int rv;
201 DBT k, v;
203 if (db == NULL)
204 return -1;
206 first = first ? R_FIRST : R_NEXT;
208 switch (rv = (*db->seq)(db, &k, &v, first)) {
209 case 0:
210 if (state_sizecheck(&k) == -1)
211 return -1;
212 memcpy(c, k.data, sizeof(*c));
213 if (debug > 2)
214 dumpkey(c);
215 memcpy(dbi, v.data, sizeof(*dbi));
216 if (debug > 1)
217 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
218 return 1;
219 case 1:
220 if (debug > 1)
221 (*lfun)(LOG_DEBUG, "%s: returns %d", __func__, rv);
222 return 0;
223 default:
224 (*lfun)(LOG_ERR, "%s: failed (%m)", __func__);
225 return -1;
230 state_sync(DB *db)
232 return (*db->sync)(db, 0);