Home | History | Annotate | Line # | Download | only in kvm_mkdb
kvm_mkdb.c revision 1.20
      1  1.20    lukem /* $NetBSD: kvm_mkdb.c,v 1.20 2008/07/21 13:36:58 lukem Exp $ */
      2  1.10  thorpej 
      3   1.1      cgd /*-
      4   1.6       pk  * Copyright (c) 1990, 1993
      5   1.6       pk  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.19      agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.19      agc  *    may be used to endorse or promote products derived from this software
     17  1.19      agc  *    without specific prior written permission.
     18  1.19      agc  *
     19  1.19      agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.19      agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.19      agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.19      agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.19      agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.19      agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.19      agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.19      agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.19      agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.19      agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.19      agc  * SUCH DAMAGE.
     30  1.19      agc  */
     31  1.19      agc 
     32  1.19      agc /*-
     33  1.19      agc  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     34  1.19      agc  *
     35  1.19      agc  * Redistribution and use in source and binary forms, with or without
     36  1.19      agc  * modification, are permitted provided that the following conditions
     37  1.19      agc  * are met:
     38  1.19      agc  * 1. Redistributions of source code must retain the above copyright
     39  1.19      agc  *    notice, this list of conditions and the following disclaimer.
     40  1.19      agc  * 2. Redistributions in binary form must reproduce the above copyright
     41  1.19      agc  *    notice, this list of conditions and the following disclaimer in the
     42  1.19      agc  *    documentation and/or other materials provided with the distribution.
     43   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     44   1.1      cgd  *    must display the following acknowledgement:
     45   1.1      cgd  *	This product includes software developed by the University of
     46   1.1      cgd  *	California, Berkeley and its contributors.
     47   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     48   1.1      cgd  *    may be used to endorse or promote products derived from this software
     49   1.1      cgd  *    without specific prior written permission.
     50   1.1      cgd  *
     51   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61   1.1      cgd  * SUCH DAMAGE.
     62   1.1      cgd  */
     63   1.1      cgd 
     64  1.13    lukem #include <sys/cdefs.h>
     65   1.1      cgd #ifndef lint
     66  1.20    lukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
     67  1.20    lukem  The Regents of the University of California.  All rights reserved.");
     68   1.1      cgd #endif /* not lint */
     69   1.1      cgd 
     70   1.1      cgd #ifndef lint
     71  1.10  thorpej #if 0
     72  1.11      cgd static char sccsid[] = "from: @(#)kvm_mkdb.c	8.3 (Berkeley) 5/4/95";
     73  1.10  thorpej #else
     74  1.20    lukem __RCSID("$NetBSD: kvm_mkdb.c,v 1.20 2008/07/21 13:36:58 lukem Exp $");
     75  1.10  thorpej #endif
     76   1.1      cgd #endif /* not lint */
     77   1.1      cgd 
     78   1.1      cgd #include <sys/param.h>
     79   1.1      cgd #include <sys/stat.h>
     80   1.6       pk 
     81   1.1      cgd #include <db.h>
     82   1.6       pk #include <err.h>
     83   1.1      cgd #include <errno.h>
     84   1.6       pk #include <fcntl.h>
     85   1.6       pk #include <paths.h>
     86   1.1      cgd #include <stdio.h>
     87   1.6       pk #include <stdlib.h>
     88   1.1      cgd #include <string.h>
     89  1.11      cgd #include <unistd.h>
     90   1.5       pk 
     91   1.6       pk #include "extern.h"
     92   1.1      cgd 
     93  1.13    lukem 	int	main __P((int, char **));
     94  1.13    lukem static	void	usage __P((void));
     95   1.5       pk 
     96   1.8  mycroft HASHINFO openinfo = {
     97   1.8  mycroft 	4096,		/* bsize */
     98   1.8  mycroft 	128,		/* ffactor */
     99   1.8  mycroft 	1024,		/* nelem */
    100   1.8  mycroft 	2048 * 1024,	/* cachesize */
    101   1.8  mycroft 	NULL,		/* hash() */
    102   1.8  mycroft 	0		/* lorder */
    103   1.8  mycroft };
    104   1.8  mycroft 
    105   1.9      cgd static DB *db;
    106  1.16  msaitoh static char *dbname = NULL;
    107   1.9      cgd static char dbtemp[MAXPATHLEN];
    108   1.9      cgd 
    109   1.5       pk int
    110   1.1      cgd main(argc, argv)
    111   1.1      cgd 	int argc;
    112   1.6       pk 	char *argv[];
    113   1.1      cgd {
    114   1.1      cgd 	int ch;
    115  1.18    ragge 	char *nlistpath;
    116  1.16  msaitoh 	int docheck = 0;
    117  1.18    ragge 	int fd;
    118   1.1      cgd 
    119  1.15  msaitoh 	while ((ch = getopt(argc, argv, "o:")) != -1)
    120   1.6       pk 		switch (ch) {
    121  1.15  msaitoh 		case 'o':
    122  1.15  msaitoh 			dbname = optarg;
    123  1.15  msaitoh 			break;
    124  1.15  msaitoh 
    125   1.1      cgd 		case '?':
    126   1.1      cgd 		default:
    127   1.1      cgd 			usage();
    128   1.1      cgd 		}
    129   1.1      cgd 	argc -= optind;
    130   1.1      cgd 	argv += optind;
    131   1.1      cgd 
    132   1.6       pk 	if (argc > 1)
    133   1.6       pk 		usage();
    134   1.6       pk 
    135  1.16  msaitoh 	if (dbname == NULL) {
    136  1.16  msaitoh 		dbname = _PATH_KVMDB;
    137  1.16  msaitoh 		docheck = 1;
    138  1.16  msaitoh 	} else 	if (strncmp(_PATH_KVMDB, dbname, sizeof(_PATH_KVMDB)) == 0) {
    139  1.16  msaitoh 		docheck = 1;
    140  1.16  msaitoh 	}
    141  1.16  msaitoh 	if (docheck) {
    142  1.16  msaitoh 		/*
    143  1.16  msaitoh 		 * If the existing db file matches the currently running
    144  1.16  msaitoh 		 * kernel, exit
    145  1.16  msaitoh 		 */
    146  1.16  msaitoh 		if (testdb())
    147  1.16  msaitoh 			exit(0);
    148  1.16  msaitoh 	}
    149   1.6       pk 
    150  1.18    ragge 	if (argc <= 0) {
    151  1.18    ragge 		/*
    152  1.18    ragge 		 * Check for useability of _PATH_KSYMS, if not
    153  1.18    ragge 		 * then fallback to _PATH_UNIX.
    154  1.18    ragge 		 * Should we complain if failure?
    155  1.18    ragge 		 */
    156  1.18    ragge 		if ((fd = open(_PATH_KSYMS, O_RDONLY)) >= 0) {
    157  1.18    ragge 			close(fd);
    158  1.18    ragge 			nlistpath = _PATH_KSYMS;
    159  1.18    ragge 		} else
    160  1.18    ragge 			nlistpath = _PATH_UNIX;
    161  1.18    ragge 	} else
    162  1.18    ragge 		nlistpath = argv[0];
    163   1.1      cgd 
    164  1.15  msaitoh 	(void)snprintf(dbtemp, sizeof(dbtemp), "%s.tmp", dbname);
    165   1.1      cgd 	(void)umask(0);
    166   1.6       pk 	db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
    167   1.8  mycroft 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, &openinfo);
    168   1.6       pk 	if (db == NULL)
    169   1.6       pk 		err(1, "%s", dbtemp);
    170   1.1      cgd 	create_knlist(nlistpath, db);
    171   1.9      cgd 	if (db->close(db)) {
    172   1.9      cgd 		warn("%s", dbtemp);
    173   1.9      cgd 		db = NULL;
    174   1.9      cgd 		punt();
    175   1.9      cgd 	}
    176   1.9      cgd 	db = NULL;
    177   1.9      cgd 	if (rename(dbtemp, dbname)) {
    178   1.9      cgd 		warn("rename %s to %s", dbtemp, dbname);
    179   1.9      cgd 		punt();
    180   1.9      cgd 	}
    181   1.6       pk 	exit(0);
    182   1.1      cgd }
    183   1.1      cgd 
    184   1.5       pk void
    185   1.1      cgd usage()
    186   1.1      cgd {
    187  1.15  msaitoh 	(void)fprintf(stderr, "usage: kvm_mkdb [-o database] [file]\n");
    188   1.9      cgd 	exit(1);
    189   1.9      cgd }
    190   1.9      cgd 
    191   1.9      cgd void
    192   1.9      cgd punt()
    193   1.9      cgd {
    194   1.9      cgd 
    195   1.9      cgd 	if (db != NULL)
    196   1.9      cgd 		db->close(db);
    197   1.9      cgd 	unlink(dbtemp);
    198   1.1      cgd 	exit(1);
    199   1.1      cgd }
    200