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