Home | History | Annotate | Line # | Download | only in db
      1  1.4       mrg /*	$NetBSD: t_db_hash_seq.c,v 1.4 2020/09/07 00:28:44 mrg Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.4       mrg __RCSID("$NetBSD: t_db_hash_seq.c,v 1.4 2020/09/07 00:28:44 mrg Exp $");
     33  1.1  christos 
     34  1.1  christos #include <sys/types.h>
     35  1.1  christos #include <sys/socket.h>
     36  1.1  christos #include <db.h>
     37  1.1  christos #include <stdio.h>
     38  1.1  christos #include <string.h>
     39  1.1  christos #include <errno.h>
     40  1.1  christos #include <stdlib.h>
     41  1.1  christos #include <fcntl.h>
     42  1.1  christos #include <syslog.h>
     43  1.1  christos #include <netinet/in.h>
     44  1.1  christos 
     45  1.1  christos #define ATF
     46  1.1  christos 
     47  1.1  christos struct conf {
     48  1.1  christos 	struct sockaddr_storage	c_ss;
     49  1.1  christos 	int			c_lmask;
     50  1.1  christos 	int			c_port;
     51  1.1  christos 	int			c_proto;
     52  1.1  christos 	int			c_family;
     53  1.1  christos 	int			c_uid;
     54  1.1  christos 	int			c_nfail;
     55  1.1  christos 	char			c_name[128];
     56  1.1  christos 	int			c_rmask;
     57  1.1  christos 	int			c_duration;
     58  1.1  christos };
     59  1.1  christos 
     60  1.1  christos struct dbinfo {
     61  1.1  christos 	int count;
     62  1.1  christos 	time_t last;
     63  1.1  christos 	char id[64];
     64  1.1  christos };
     65  1.1  christos 
     66  1.1  christos #ifdef ATF
     67  1.1  christos #include <atf-c.h>
     68  1.1  christos 
     69  1.1  christos #define DO_ERR(msg, ...)	ATF_REQUIRE_MSG(0, msg, __VA_ARGS__)
     70  1.1  christos #define DO_WARNX(msg, ...)	ATF_REQUIRE_MSG(0, msg, __VA_ARGS__)
     71  1.1  christos #else
     72  1.1  christos #include <err.h>
     73  1.1  christos 
     74  1.1  christos #define DO_ERR(fmt, ...)	err(EXIT_FAILURE, fmt, __VA_ARGS__)
     75  1.1  christos #define DO_WARNX(fmt, ...)	warnx(fmt, __VA_ARGS__)
     76  1.1  christos #endif
     77  1.1  christos 
     78  1.1  christos #define DO_DEBUG(fmt, ...)	fprintf(stderr, fmt, __VA_ARGS__)
     79  1.1  christos 
     80  1.1  christos static HASHINFO openinfo = {
     81  1.1  christos 	4096,		/* bsize */
     82  1.1  christos 	32,		/* ffactor */
     83  1.1  christos 	256,		/* nelem */
     84  1.1  christos 	8 * 1024 * 1024,/* cachesize */
     85  1.1  christos 	NULL,		/* hash() */
     86  1.1  christos 	0		/* lorder */
     87  1.1  christos };
     88  1.1  christos 
     89  1.1  christos static int debug = 0;
     90  1.1  christos 
     91  1.1  christos static int
     92  1.1  christos state_close(DB *db)
     93  1.1  christos {
     94  1.1  christos 	if (db == NULL)
     95  1.1  christos 		return -1;
     96  1.1  christos 	if ((*db->close)(db) == -1)
     97  1.1  christos 		DO_ERR("%s: can't close db", __func__);
     98  1.1  christos 	return 0;
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos static DB *
    102  1.1  christos state_open(const char *dbname, int flags, mode_t perm)
    103  1.1  christos {
    104  1.1  christos 	DB *db;
    105  1.1  christos 
    106  1.1  christos 	db = dbopen(dbname, flags, perm, DB_HASH, &openinfo);
    107  1.1  christos 	if (db == NULL) {
    108  1.1  christos 		if (errno == ENOENT && (flags & O_CREAT) == 0)
    109  1.1  christos 			return NULL;
    110  1.4       mrg 		DO_ERR("%s: can't open `%s'", __func__,
    111  1.4       mrg 		    dbname ? dbname : "<memory>");
    112  1.1  christos 	}
    113  1.1  christos 	return db;
    114  1.1  christos }
    115  1.1  christos 
    116  1.1  christos static int
    117  1.1  christos state_sizecheck(const DBT *t)
    118  1.1  christos {
    119  1.1  christos 	if (sizeof(struct conf) == t->size)
    120  1.1  christos 		return 0;
    121  1.1  christos 	DO_WARNX("Key size mismatch %zu != %zu", sizeof(struct conf), t->size);
    122  1.1  christos 	return 0;
    123  1.1  christos }
    124  1.1  christos 
    125  1.1  christos static int
    126  1.1  christos state_del(DB *db, const struct conf *c)
    127  1.1  christos {
    128  1.1  christos 	int rv;
    129  1.1  christos 	DBT k;
    130  1.1  christos 
    131  1.1  christos 	if (db == NULL)
    132  1.1  christos 		return -1;
    133  1.1  christos 
    134  1.1  christos 	k.data = __UNCONST(c);
    135  1.1  christos 	k.size = sizeof(*c);
    136  1.1  christos 
    137  1.1  christos 	switch (rv = (*db->del)(db, &k, 1)) {
    138  1.1  christos 	case 0:
    139  1.1  christos 	case 1:
    140  1.1  christos 		if (debug > 1) {
    141  1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    142  1.1  christos 			(*db->sync)(db, 0);
    143  1.1  christos 		}
    144  1.1  christos 		return 0;
    145  1.1  christos 	default:
    146  1.1  christos 		DO_ERR("%s: failed", __func__);
    147  1.1  christos 		return -1;
    148  1.1  christos 	}
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos #if 0
    152  1.1  christos static int
    153  1.1  christos state_get(DB *db, const struct conf *c, struct dbinfo *dbi)
    154  1.1  christos {
    155  1.1  christos 	int rv;
    156  1.1  christos 	DBT k, v;
    157  1.1  christos 
    158  1.1  christos 	if (db == NULL)
    159  1.1  christos 		return -1;
    160  1.1  christos 
    161  1.1  christos 	k.data = __UNCONST(c);
    162  1.1  christos 	k.size = sizeof(*c);
    163  1.1  christos 
    164  1.1  christos 	switch (rv = (*db->get)(db, &k, &v, 0)) {
    165  1.1  christos 	case 0:
    166  1.1  christos 	case 1:
    167  1.1  christos 		if (rv)
    168  1.1  christos 			memset(dbi, 0, sizeof(*dbi));
    169  1.1  christos 		else
    170  1.1  christos 			memcpy(dbi, v.data, sizeof(*dbi));
    171  1.1  christos 		if (debug > 1)
    172  1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    173  1.1  christos 		return 0;
    174  1.1  christos 	default:
    175  1.1  christos 		DO_ERR("%s: failed", __func__);
    176  1.1  christos 		return -1;
    177  1.1  christos 	}
    178  1.1  christos }
    179  1.1  christos #endif
    180  1.1  christos 
    181  1.1  christos static int
    182  1.1  christos state_put(DB *db, const struct conf *c, const struct dbinfo *dbi)
    183  1.1  christos {
    184  1.1  christos 	int rv;
    185  1.1  christos 	DBT k, v;
    186  1.1  christos 
    187  1.1  christos 	if (db == NULL)
    188  1.1  christos 		return -1;
    189  1.1  christos 
    190  1.1  christos 	k.data = __UNCONST(c);
    191  1.1  christos 	k.size = sizeof(*c);
    192  1.1  christos 	v.data = __UNCONST(dbi);
    193  1.1  christos 	v.size = sizeof(*dbi);
    194  1.1  christos 
    195  1.1  christos 	switch (rv = (*db->put)(db, &k, &v, 0)) {
    196  1.1  christos 	case 0:
    197  1.1  christos 		if (debug > 1) {
    198  1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    199  1.1  christos 			(*db->sync)(db, 0);
    200  1.1  christos 		}
    201  1.1  christos 		return 0;
    202  1.1  christos 	case 1:
    203  1.1  christos 		errno = EEXIST;
    204  1.1  christos 		/*FALLTHROUGH*/
    205  1.1  christos 	default:
    206  1.1  christos 		DO_ERR("%s: failed", __func__);
    207  1.1  christos 	}
    208  1.1  christos }
    209  1.1  christos 
    210  1.1  christos static int
    211  1.1  christos state_iterate(DB *db, struct conf *c, struct dbinfo *dbi, unsigned int first)
    212  1.1  christos {
    213  1.1  christos 	int rv;
    214  1.1  christos 	DBT k, v;
    215  1.1  christos 
    216  1.1  christos 	if (db == NULL)
    217  1.1  christos 		return -1;
    218  1.1  christos 
    219  1.1  christos 	first = first ? R_FIRST : R_NEXT;
    220  1.1  christos 
    221  1.1  christos 	switch (rv = (*db->seq)(db, &k, &v, first)) {
    222  1.1  christos 	case 0:
    223  1.1  christos 		if (state_sizecheck(&k) == -1)
    224  1.1  christos 			return -1;
    225  1.1  christos 		memcpy(c, k.data, sizeof(*c));
    226  1.1  christos 		memcpy(dbi, v.data, sizeof(*dbi));
    227  1.1  christos 		if (debug > 1)
    228  1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    229  1.1  christos 		return 1;
    230  1.1  christos 	case 1:
    231  1.1  christos 		if (debug > 1)
    232  1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    233  1.1  christos 		return 0;
    234  1.1  christos 	default:
    235  1.1  christos 		DO_ERR("%s: failed", __func__);
    236  1.1  christos 		return -1;
    237  1.1  christos 	}
    238  1.1  christos }
    239  1.1  christos 
    240  1.1  christos #define MAXB 100
    241  1.1  christos 
    242  1.1  christos static int
    243  1.1  christos testdb(int skip)
    244  1.1  christos {
    245  1.1  christos 	size_t i;
    246  1.1  christos 	int f;
    247  1.1  christos 	char flag[MAXB];
    248  1.1  christos 	DB *db;
    249  1.1  christos 	struct conf c;
    250  1.1  christos 	struct dbinfo d;
    251  1.1  christos 
    252  1.2  christos 	db = state_open(NULL, O_RDWR|O_CREAT|O_TRUNC, 0600);
    253  1.1  christos 	if (db == NULL)
    254  1.1  christos 		DO_ERR("%s: cannot open `%s'", __func__, "foo");
    255  1.1  christos 
    256  1.1  christos 	memset(&c, 0, sizeof(c));
    257  1.1  christos 	memset(&d, 0, sizeof(d));
    258  1.1  christos 	memset(flag, 0, sizeof(flag));
    259  1.1  christos 
    260  1.1  christos 	for (i = 0; i < __arraycount(flag); i++) {
    261  1.1  christos 		c.c_port = i;
    262  1.1  christos 		state_put(db, &c, &d);
    263  1.1  christos 	}
    264  1.1  christos 
    265  1.1  christos 	for (f = 1, i = 0; state_iterate(db, &c, &d, f) == 1; f = 0, i++) {
    266  1.1  christos 		if (debug > 1)
    267  1.1  christos 			DO_DEBUG("%zu %d\n", i, c.c_port);
    268  1.1  christos 		if (flag[c.c_port])
    269  1.1  christos 			DO_WARNX("Already visited %d", c.c_port);
    270  1.1  christos 		flag[c.c_port] = 1;
    271  1.2  christos 		if (skip == 0 || c.c_port % skip != 0)
    272  1.1  christos 			continue;
    273  1.1  christos 		state_del(db, &c);
    274  1.1  christos 	}
    275  1.1  christos 	state_close(db);
    276  1.1  christos 	for (i = 0; i < __arraycount(flag); i++) {
    277  1.1  christos 		if (flag[i] == 0)
    278  1.1  christos 			DO_WARNX("Not visited %zu", i);
    279  1.1  christos 	}
    280  1.1  christos 	return 0;
    281  1.1  christos }
    282  1.1  christos 
    283  1.1  christos #ifndef ATF
    284  1.1  christos int
    285  1.1  christos main(int argc, char *argv[])
    286  1.1  christos {
    287  1.1  christos 	return testdb(6);
    288  1.1  christos }
    289  1.1  christos #else
    290  1.1  christos 
    291  1.2  christos ATF_TC(test_hash_del_none);
    292  1.2  christos ATF_TC_HEAD(test_hash_del_none, tc)
    293  1.1  christos {
    294  1.2  christos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting none");
    295  1.1  christos }
    296  1.1  christos 
    297  1.2  christos ATF_TC_BODY(test_hash_del_none, tc)
    298  1.1  christos {
    299  1.2  christos 	testdb(0);
    300  1.2  christos }
    301  1.2  christos 
    302  1.2  christos ATF_TC(test_hash_del_all);
    303  1.2  christos ATF_TC_HEAD(test_hash_del_all, tc)
    304  1.2  christos {
    305  1.2  christos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting all");
    306  1.2  christos }
    307  1.2  christos 
    308  1.2  christos ATF_TC_BODY(test_hash_del_all, tc)
    309  1.2  christos {
    310  1.2  christos 	testdb(1);
    311  1.2  christos }
    312  1.2  christos 
    313  1.2  christos ATF_TC(test_hash_del_alt);
    314  1.2  christos ATF_TC_HEAD(test_hash_del_alt, tc)
    315  1.2  christos {
    316  1.2  christos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables alternating deletets");
    317  1.2  christos }
    318  1.2  christos 
    319  1.2  christos ATF_TC_BODY(test_hash_del_alt, tc)
    320  1.2  christos {
    321  1.2  christos 	testdb(2);
    322  1.2  christos }
    323  1.2  christos 
    324  1.2  christos ATF_TC(test_hash_del_every_7);
    325  1.2  christos ATF_TC_HEAD(test_hash_del_every_7, tc)
    326  1.2  christos {
    327  1.2  christos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting every 7 elements");
    328  1.2  christos }
    329  1.2  christos 
    330  1.2  christos ATF_TC_BODY(test_hash_del_every_7, tc)
    331  1.2  christos {
    332  1.2  christos 	testdb(7);
    333  1.1  christos }
    334  1.1  christos 
    335  1.1  christos ATF_TP_ADD_TCS(tp)
    336  1.1  christos {
    337  1.2  christos 	ATF_TP_ADD_TC(tp, test_hash_del_none);
    338  1.2  christos 	ATF_TP_ADD_TC(tp, test_hash_del_all);
    339  1.2  christos 	ATF_TP_ADD_TC(tp, test_hash_del_alt);
    340  1.2  christos 	ATF_TP_ADD_TC(tp, test_hash_del_every_7);
    341  1.1  christos 
    342  1.3      maya 	return atf_no_error();
    343  1.1  christos }
    344  1.1  christos #endif
    345