Home | History | Annotate | Line # | Download | only in services_mkdb
output_db.c revision 1.1.28.1
      1  1.1.28.1  pgoyette /*	$NetBSD: output_db.c,v 1.1.28.1 2017/03/20 06:58:09 pgoyette Exp $	*/
      2       1.1     joerg 
      3       1.1     joerg /*-
      4       1.1     joerg  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5       1.1     joerg  * All rights reserved.
      6       1.1     joerg  *
      7       1.1     joerg  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1     joerg  * by Luke Mewburn and Christos Zoulas.
      9       1.1     joerg  *
     10       1.1     joerg  * Redistribution and use in source and binary forms, with or without
     11       1.1     joerg  * modification, are permitted provided that the following conditions
     12       1.1     joerg  * are met:
     13       1.1     joerg  * 1. Redistributions of source code must retain the above copyright
     14       1.1     joerg  *    notice, this list of conditions and the following disclaimer.
     15       1.1     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     joerg  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     joerg  *    documentation and/or other materials provided with the distribution.
     18       1.1     joerg  *
     19       1.1     joerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     joerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     joerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     joerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     joerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     joerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     joerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     joerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     joerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     joerg  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     joerg  */
     31       1.1     joerg 
     32       1.1     joerg #include <sys/cdefs.h>
     33       1.1     joerg #ifndef lint
     34  1.1.28.1  pgoyette __RCSID("$NetBSD: output_db.c,v 1.1.28.1 2017/03/20 06:58:09 pgoyette Exp $");
     35       1.1     joerg #endif /* not lint */
     36       1.1     joerg 
     37       1.1     joerg #include <sys/param.h>
     38  1.1.28.1  pgoyette #include <sys/stat.h>
     39       1.1     joerg 
     40       1.1     joerg #include <assert.h>
     41       1.1     joerg #include <db.h>
     42       1.1     joerg #include <err.h>
     43       1.1     joerg #include <fcntl.h>
     44       1.1     joerg #include <netdb.h>
     45       1.1     joerg #include <stdio.h>
     46       1.1     joerg #include <stdlib.h>
     47       1.1     joerg #include <string.h>
     48       1.1     joerg #include <unistd.h>
     49       1.1     joerg #include <util.h>
     50       1.1     joerg #include <ctype.h>
     51       1.1     joerg #include <errno.h>
     52       1.1     joerg #include <stringlist.h>
     53       1.1     joerg 
     54       1.1     joerg #include "extern.h"
     55       1.1     joerg 
     56       1.1     joerg static DB *db;
     57       1.1     joerg 
     58       1.1     joerg static const HASHINFO hinfo = {
     59       1.1     joerg 	.bsize = 256,
     60       1.1     joerg 	.ffactor = 4,
     61       1.1     joerg 	.nelem = 32768,
     62       1.1     joerg 	.cachesize = 1024,
     63       1.1     joerg 	.hash = NULL,
     64       1.1     joerg 	.lorder = 0
     65       1.1     joerg };
     66       1.1     joerg 
     67       1.1     joerg static void	store(DBT *, DBT *, int);
     68       1.1     joerg static void	killproto(DBT *);
     69       1.1     joerg static const char *mkaliases(StringList *, char *, size_t);
     70       1.1     joerg 
     71       1.1     joerg int
     72       1.1     joerg db_open(const char *tname)
     73       1.1     joerg {
     74       1.1     joerg 	db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
     75       1.1     joerg 	    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
     76       1.1     joerg 
     77       1.1     joerg 	return db != NULL ? 0 : -1;
     78       1.1     joerg }
     79       1.1     joerg 
     80       1.1     joerg int
     81       1.1     joerg db_close(void)
     82       1.1     joerg {
     83       1.1     joerg 	int rv;
     84       1.1     joerg 
     85       1.1     joerg 	rv = (db->close)(db);
     86       1.1     joerg 	db = NULL;
     87       1.1     joerg 
     88       1.1     joerg 	return rv;
     89       1.1     joerg }
     90       1.1     joerg 
     91       1.1     joerg void
     92       1.1     joerg db_add(StringList *sl, size_t port, const char *proto, size_t *cnt,
     93       1.1     joerg     int warndup)
     94       1.1     joerg {
     95       1.1     joerg 	size_t i;
     96       1.1     joerg 	char	 keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
     97       1.1     joerg 	DBT	 data, key;
     98       1.1     joerg 	key.data = keyb;
     99       1.1     joerg 	data.data = datab;
    100       1.1     joerg 
    101       1.1     joerg 	/* key `indirect key', data `full line' */
    102       1.1     joerg 	data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
    103       1.1     joerg 	key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
    104       1.1     joerg 	    sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
    105       1.1     joerg 	store(&data, &key, warndup);
    106       1.1     joerg 
    107       1.1     joerg 	/* key `\377port/proto', data = `indirect key' */
    108       1.1     joerg 	key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
    109       1.1     joerg 	    port, proto) + 1;
    110       1.1     joerg 	store(&key, &data, warndup);
    111       1.1     joerg 
    112       1.1     joerg 	/* key `\377port', data = `indirect key' */
    113       1.1     joerg 	killproto(&key);
    114       1.1     joerg 	store(&key, &data, warndup);
    115       1.1     joerg 
    116       1.1     joerg 	/* add references for service and all aliases */
    117       1.1     joerg 	for (i = 0; i < sl->sl_cur; i++) {
    118       1.1     joerg 		/* key `\376service/proto', data = `indirect key' */
    119       1.1     joerg 		key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
    120       1.1     joerg 		    sl->sl_str[i], proto) + 1;
    121       1.1     joerg 		store(&key, &data, warndup);
    122       1.1     joerg 
    123       1.1     joerg 		/* key `\376service', data = `indirect key' */
    124       1.1     joerg 		killproto(&key);
    125       1.1     joerg 		store(&key, &data, warndup);
    126       1.1     joerg 	}
    127       1.1     joerg 	sl_free(sl, 1);
    128       1.1     joerg }
    129       1.1     joerg 
    130       1.1     joerg static void
    131       1.1     joerg killproto(DBT *key)
    132       1.1     joerg {
    133       1.1     joerg 	char *p, *d = key->data;
    134       1.1     joerg 
    135       1.1     joerg 	if ((p = strchr(d, '/')) == NULL)
    136       1.1     joerg 		abort();
    137       1.1     joerg 	*p++ = '\0';
    138       1.1     joerg 	key->size = p - d;
    139       1.1     joerg }
    140       1.1     joerg 
    141       1.1     joerg static void
    142       1.1     joerg store(DBT *key, DBT *data, int warndup)
    143       1.1     joerg {
    144       1.1     joerg #ifdef DEBUG
    145       1.1     joerg 	int k = key->size - 1;
    146       1.1     joerg 	int d = data->size - 1;
    147       1.1     joerg 	(void)printf("store [%*.*s] [%*.*s]\n",
    148       1.1     joerg 		k, k, (char *)key->data + 1,
    149       1.1     joerg 		d, d, (char *)data->data + 1);
    150       1.1     joerg #endif
    151       1.1     joerg 	switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
    152       1.1     joerg 	case 0:
    153       1.1     joerg 		break;
    154       1.1     joerg 	case 1:
    155       1.1     joerg 		if (warndup)
    156       1.1     joerg 			warnx("duplicate service `%s'",
    157       1.1     joerg 			    &((char *)key->data)[1]);
    158       1.1     joerg 		break;
    159       1.1     joerg 	case -1:
    160       1.1     joerg 		err(1, "put");
    161       1.1     joerg 		break;
    162       1.1     joerg 	default:
    163       1.1     joerg 		abort();
    164       1.1     joerg 		break;
    165       1.1     joerg 	}
    166       1.1     joerg }
    167       1.1     joerg 
    168       1.1     joerg static const char *
    169       1.1     joerg mkaliases(StringList *sl, char *buf, size_t len)
    170       1.1     joerg {
    171       1.1     joerg 	size_t nc, i, pos;
    172       1.1     joerg 
    173       1.1     joerg 	buf[0] = 0;
    174       1.1     joerg 	for (i = 1, pos = 0; i < sl->sl_cur; i++) {
    175       1.1     joerg 		nc = strlcpy(buf + pos, sl->sl_str[i], len);
    176       1.1     joerg 		if (nc >= len)
    177       1.1     joerg 			goto out;
    178       1.1     joerg 		pos += nc;
    179       1.1     joerg 		len -= nc;
    180       1.1     joerg 		nc = strlcpy(buf + pos, " ", len);
    181       1.1     joerg 		if (nc >= len)
    182       1.1     joerg 			goto out;
    183       1.1     joerg 		pos += nc;
    184       1.1     joerg 		len -= nc;
    185       1.1     joerg 	}
    186       1.1     joerg 	return buf;
    187       1.1     joerg out:
    188       1.1     joerg 	warn("aliases for `%s' truncated", sl->sl_str[0]);
    189       1.1     joerg 	return buf;
    190       1.1     joerg }
    191