xref: /original-bsd/usr.sbin/kvm_mkdb/kvm_mkdb.c (revision 00695d63)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1990, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)kvm_mkdb.c	8.2 (Berkeley) 04/27/95";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 
21 #include <db.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <paths.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "extern.h"
31 
32 static void usage __P((void));
33 
34 HASHINFO openinfo = {
35 	4096,		/* bsize */
36 	128,		/* ffactor */
37 	1024,		/* nelem */
38 	2048 * 1024,	/* cachesize */
39 	NULL,		/* hash() */
40 	0		/* lorder */
41 };
42 
43 int
44 main(argc, argv)
45 	int argc;
46 	char *argv[];
47 {
48 	DB *db;
49 	int ch;
50 	char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
51 
52 	while ((ch = getopt(argc, argv, "")) != EOF)
53 		switch (ch) {
54 		case '?':
55 		default:
56 			usage();
57 		}
58 	argc -= optind;
59 	argv += optind;
60 
61 	if (argc > 1)
62 		usage();
63 
64 	/* If the existing db file matches the currently running kernel, exit */
65 	if (testdb())
66 		exit(0);
67 
68 #define	basename(cp)	((p = rindex((cp), '/')) != NULL ? p + 1 : (cp))
69 	nlistpath = argc > 0 ? argv[0] : _PATH_UNIX;
70 	nlistname = basename(nlistpath);
71 
72 	(void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
73 	    _PATH_VARDB, nlistname);
74 	(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
75 	    _PATH_VARDB, nlistname);
76 	(void)umask(0);
77 	db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
78 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, &openinfo);
79 	if (db == NULL)
80 		err(1, "%s", dbtemp);
81 	create_knlist(nlistpath, db);
82 	if (db->close(db))
83 		err(1, "%s", dbtemp);
84 	if (rename(dbtemp, dbname))
85 		err(1, "rename %s to %s", dbtemp, dbname);
86 	exit(0);
87 }
88 
89 void
90 usage()
91 {
92 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
93 	exit(1);
94 }
95