xref: /netbsd-src/usr.sbin/kvm_mkdb/kvm_mkdb.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1990, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)kvm_mkdb.c	8.1 (Berkeley) 6/6/93";*/
42 static char *rcsid = "$Id: kvm_mkdb.c,v 1.8 1994/08/29 23:17:00 mycroft Exp $";
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 
48 #include <db.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <paths.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "extern.h"
58 
59 static void usage __P((void));
60 
61 HASHINFO openinfo = {
62 	4096,		/* bsize */
63 	128,		/* ffactor */
64 	1024,		/* nelem */
65 	2048 * 1024,	/* cachesize */
66 	NULL,		/* hash() */
67 	0		/* lorder */
68 };
69 
70 int
71 main(argc, argv)
72 	int argc;
73 	char *argv[];
74 {
75 	DB *db;
76 	int ch;
77 	char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
78 
79 	while ((ch = getopt(argc, argv, "")) != EOF)
80 		switch (ch) {
81 		case '?':
82 		default:
83 			usage();
84 		}
85 	argc -= optind;
86 	argv += optind;
87 
88 	if (argc > 1)
89 		usage();
90 
91 	/* If the existing db file matches the currently running kernel, exit */
92 	if (testdb())
93 		exit(0);
94 
95 #define	basename(cp)	((p = rindex((cp), '/')) != NULL ? p + 1 : (cp))
96 	nlistpath = argc > 0 ? argv[0] : _PATH_UNIX;
97 	nlistname = basename(nlistpath);
98 
99 	(void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
100 	    _PATH_VARDB, nlistname);
101 	(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
102 	    _PATH_VARDB, nlistname);
103 	(void)umask(0);
104 	db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
105 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, &openinfo);
106 	if (db == NULL)
107 		err(1, "%s", dbtemp);
108 	create_knlist(nlistpath, db);
109 	if (db->close(db))
110 		err(1, "%s", dbtemp);
111 	if (rename(dbtemp, dbname))
112 		err(1, "rename %s to %s", dbtemp, dbname);
113 	exit(0);
114 }
115 
116 void
117 usage()
118 {
119 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
120 	exit(1);
121 }
122