Home | History | Annotate | Line # | Download | only in db
t_db_hash_seq.c revision 1.2.16.1
      1  1.2.16.1    martin /*	$NetBSD: t_db_hash_seq.c,v 1.2.16.1 2020/04/13 08:05:25 martin 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.2.16.1    martin __RCSID("$NetBSD: t_db_hash_seq.c,v 1.2.16.1 2020/04/13 08:05:25 martin 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.1  christos 		DO_ERR("%s: can't open `%s'", __func__, dbname);
    111       1.1  christos 	}
    112       1.1  christos 	return db;
    113       1.1  christos }
    114       1.1  christos 
    115       1.1  christos static int
    116       1.1  christos state_sizecheck(const DBT *t)
    117       1.1  christos {
    118       1.1  christos 	if (sizeof(struct conf) == t->size)
    119       1.1  christos 		return 0;
    120       1.1  christos 	DO_WARNX("Key size mismatch %zu != %zu", sizeof(struct conf), t->size);
    121       1.1  christos 	return 0;
    122       1.1  christos }
    123       1.1  christos 
    124       1.1  christos static int
    125       1.1  christos state_del(DB *db, const struct conf *c)
    126       1.1  christos {
    127       1.1  christos 	int rv;
    128       1.1  christos 	DBT k;
    129       1.1  christos 
    130       1.1  christos 	if (db == NULL)
    131       1.1  christos 		return -1;
    132       1.1  christos 
    133       1.1  christos 	k.data = __UNCONST(c);
    134       1.1  christos 	k.size = sizeof(*c);
    135       1.1  christos 
    136       1.1  christos 	switch (rv = (*db->del)(db, &k, 1)) {
    137       1.1  christos 	case 0:
    138       1.1  christos 	case 1:
    139       1.1  christos 		if (debug > 1) {
    140       1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    141       1.1  christos 			(*db->sync)(db, 0);
    142       1.1  christos 		}
    143       1.1  christos 		return 0;
    144       1.1  christos 	default:
    145       1.1  christos 		DO_ERR("%s: failed", __func__);
    146       1.1  christos 		return -1;
    147       1.1  christos 	}
    148       1.1  christos }
    149       1.1  christos 
    150       1.1  christos #if 0
    151       1.1  christos static int
    152       1.1  christos state_get(DB *db, const struct conf *c, struct dbinfo *dbi)
    153       1.1  christos {
    154       1.1  christos 	int rv;
    155       1.1  christos 	DBT k, v;
    156       1.1  christos 
    157       1.1  christos 	if (db == NULL)
    158       1.1  christos 		return -1;
    159       1.1  christos 
    160       1.1  christos 	k.data = __UNCONST(c);
    161       1.1  christos 	k.size = sizeof(*c);
    162       1.1  christos 
    163       1.1  christos 	switch (rv = (*db->get)(db, &k, &v, 0)) {
    164       1.1  christos 	case 0:
    165       1.1  christos 	case 1:
    166       1.1  christos 		if (rv)
    167       1.1  christos 			memset(dbi, 0, sizeof(*dbi));
    168       1.1  christos 		else
    169       1.1  christos 			memcpy(dbi, v.data, sizeof(*dbi));
    170       1.1  christos 		if (debug > 1)
    171       1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    172       1.1  christos 		return 0;
    173       1.1  christos 	default:
    174       1.1  christos 		DO_ERR("%s: failed", __func__);
    175       1.1  christos 		return -1;
    176       1.1  christos 	}
    177       1.1  christos }
    178       1.1  christos #endif
    179       1.1  christos 
    180       1.1  christos static int
    181       1.1  christos state_put(DB *db, const struct conf *c, const struct dbinfo *dbi)
    182       1.1  christos {
    183       1.1  christos 	int rv;
    184       1.1  christos 	DBT k, v;
    185       1.1  christos 
    186       1.1  christos 	if (db == NULL)
    187       1.1  christos 		return -1;
    188       1.1  christos 
    189       1.1  christos 	k.data = __UNCONST(c);
    190       1.1  christos 	k.size = sizeof(*c);
    191       1.1  christos 	v.data = __UNCONST(dbi);
    192       1.1  christos 	v.size = sizeof(*dbi);
    193       1.1  christos 
    194       1.1  christos 	switch (rv = (*db->put)(db, &k, &v, 0)) {
    195       1.1  christos 	case 0:
    196       1.1  christos 		if (debug > 1) {
    197       1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    198       1.1  christos 			(*db->sync)(db, 0);
    199       1.1  christos 		}
    200       1.1  christos 		return 0;
    201       1.1  christos 	case 1:
    202       1.1  christos 		errno = EEXIST;
    203       1.1  christos 		/*FALLTHROUGH*/
    204       1.1  christos 	default:
    205       1.1  christos 		DO_ERR("%s: failed", __func__);
    206       1.1  christos 	}
    207       1.1  christos }
    208       1.1  christos 
    209       1.1  christos static int
    210       1.1  christos state_iterate(DB *db, struct conf *c, struct dbinfo *dbi, unsigned int first)
    211       1.1  christos {
    212       1.1  christos 	int rv;
    213       1.1  christos 	DBT k, v;
    214       1.1  christos 
    215       1.1  christos 	if (db == NULL)
    216       1.1  christos 		return -1;
    217       1.1  christos 
    218       1.1  christos 	first = first ? R_FIRST : R_NEXT;
    219       1.1  christos 
    220       1.1  christos 	switch (rv = (*db->seq)(db, &k, &v, first)) {
    221       1.1  christos 	case 0:
    222       1.1  christos 		if (state_sizecheck(&k) == -1)
    223       1.1  christos 			return -1;
    224       1.1  christos 		memcpy(c, k.data, sizeof(*c));
    225       1.1  christos 		memcpy(dbi, v.data, sizeof(*dbi));
    226       1.1  christos 		if (debug > 1)
    227       1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    228       1.1  christos 		return 1;
    229       1.1  christos 	case 1:
    230       1.1  christos 		if (debug > 1)
    231       1.1  christos 			DO_DEBUG("%s: returns %d", __func__, rv);
    232       1.1  christos 		return 0;
    233       1.1  christos 	default:
    234       1.1  christos 		DO_ERR("%s: failed", __func__);
    235       1.1  christos 		return -1;
    236       1.1  christos 	}
    237       1.1  christos }
    238       1.1  christos 
    239       1.1  christos #define MAXB 100
    240       1.1  christos 
    241       1.1  christos static int
    242       1.1  christos testdb(int skip)
    243       1.1  christos {
    244       1.1  christos 	size_t i;
    245       1.1  christos 	int f;
    246       1.1  christos 	char flag[MAXB];
    247       1.1  christos 	DB *db;
    248       1.1  christos 	struct conf c;
    249       1.1  christos 	struct dbinfo d;
    250       1.1  christos 
    251       1.2  christos 	db = state_open(NULL, O_RDWR|O_CREAT|O_TRUNC, 0600);
    252       1.1  christos 	if (db == NULL)
    253       1.1  christos 		DO_ERR("%s: cannot open `%s'", __func__, "foo");
    254       1.1  christos 
    255       1.1  christos 	memset(&c, 0, sizeof(c));
    256       1.1  christos 	memset(&d, 0, sizeof(d));
    257       1.1  christos 	memset(flag, 0, sizeof(flag));
    258       1.1  christos 
    259       1.1  christos 	for (i = 0; i < __arraycount(flag); i++) {
    260       1.1  christos 		c.c_port = i;
    261       1.1  christos 		state_put(db, &c, &d);
    262       1.1  christos 	}
    263       1.1  christos 
    264       1.1  christos 	for (f = 1, i = 0; state_iterate(db, &c, &d, f) == 1; f = 0, i++) {
    265       1.1  christos 		if (debug > 1)
    266       1.1  christos 			DO_DEBUG("%zu %d\n", i, c.c_port);
    267       1.1  christos 		if (flag[c.c_port])
    268       1.1  christos 			DO_WARNX("Already visited %d", c.c_port);
    269       1.1  christos 		flag[c.c_port] = 1;
    270       1.2  christos 		if (skip == 0 || c.c_port % skip != 0)
    271       1.1  christos 			continue;
    272       1.1  christos 		state_del(db, &c);
    273       1.1  christos 	}
    274       1.1  christos 	state_close(db);
    275       1.1  christos 	for (i = 0; i < __arraycount(flag); i++) {
    276       1.1  christos 		if (flag[i] == 0)
    277       1.1  christos 			DO_WARNX("Not visited %zu", i);
    278       1.1  christos 	}
    279       1.1  christos 	return 0;
    280       1.1  christos }
    281       1.1  christos 
    282       1.1  christos #ifndef ATF
    283       1.1  christos int
    284       1.1  christos main(int argc, char *argv[])
    285       1.1  christos {
    286       1.1  christos 	return testdb(6);
    287       1.1  christos }
    288       1.1  christos #else
    289       1.1  christos 
    290       1.2  christos ATF_TC(test_hash_del_none);
    291       1.2  christos ATF_TC_HEAD(test_hash_del_none, tc)
    292       1.1  christos {
    293       1.2  christos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting none");
    294       1.1  christos }
    295       1.1  christos 
    296       1.2  christos ATF_TC_BODY(test_hash_del_none, tc)
    297       1.1  christos {
    298       1.2  christos 	testdb(0);
    299       1.2  christos }
    300       1.2  christos 
    301       1.2  christos ATF_TC(test_hash_del_all);
    302       1.2  christos ATF_TC_HEAD(test_hash_del_all, tc)
    303       1.2  christos {
    304       1.2  christos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting all");
    305       1.2  christos }
    306       1.2  christos 
    307       1.2  christos ATF_TC_BODY(test_hash_del_all, tc)
    308       1.2  christos {
    309       1.2  christos 	testdb(1);
    310       1.2  christos }
    311       1.2  christos 
    312       1.2  christos ATF_TC(test_hash_del_alt);
    313       1.2  christos ATF_TC_HEAD(test_hash_del_alt, tc)
    314       1.2  christos {
    315       1.2  christos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables alternating deletets");
    316       1.2  christos }
    317       1.2  christos 
    318       1.2  christos ATF_TC_BODY(test_hash_del_alt, tc)
    319       1.2  christos {
    320       1.2  christos 	testdb(2);
    321       1.2  christos }
    322       1.2  christos 
    323       1.2  christos ATF_TC(test_hash_del_every_7);
    324       1.2  christos ATF_TC_HEAD(test_hash_del_every_7, tc)
    325       1.2  christos {
    326       1.2  christos 	atf_tc_set_md_var(tc, "descr", "Check sequential scan of hash tables deleting every 7 elements");
    327       1.2  christos }
    328       1.2  christos 
    329       1.2  christos ATF_TC_BODY(test_hash_del_every_7, tc)
    330       1.2  christos {
    331       1.2  christos 	testdb(7);
    332       1.1  christos }
    333       1.1  christos 
    334       1.1  christos ATF_TP_ADD_TCS(tp)
    335       1.1  christos {
    336       1.2  christos 	ATF_TP_ADD_TC(tp, test_hash_del_none);
    337       1.2  christos 	ATF_TP_ADD_TC(tp, test_hash_del_all);
    338       1.2  christos 	ATF_TP_ADD_TC(tp, test_hash_del_alt);
    339       1.2  christos 	ATF_TP_ADD_TC(tp, test_hash_del_every_7);
    340       1.1  christos 
    341  1.2.16.1    martin 	return atf_no_error();
    342       1.1  christos }
    343       1.1  christos #endif
    344