xref: /original-bsd/usr.sbin/kvm_mkdb/kvm_mkdb.c (revision 0f81f0ee)
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.3 (Berkeley) 05/04/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 #include <unistd.h>
30 
31 #include "extern.h"
32 
33 static void usage __P((void));
34 
35 HASHINFO openinfo = {
36 	4096,		/* bsize */
37 	128,		/* ffactor */
38 	1024,		/* nelem */
39 	2048 * 1024,	/* cachesize */
40 	NULL,		/* hash() */
41 	0		/* lorder */
42 };
43 
44 int
45 main(argc, argv)
46 	int argc;
47 	char *argv[];
48 {
49 	DB *db;
50 	int ch;
51 	char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
52 
53 	while ((ch = getopt(argc, argv, "")) != EOF)
54 		switch (ch) {
55 		case '?':
56 		default:
57 			usage();
58 		}
59 	argc -= optind;
60 	argv += optind;
61 
62 	if (argc > 1)
63 		usage();
64 
65 	/* If the existing db file matches the currently running kernel, exit */
66 	if (testdb())
67 		exit(0);
68 
69 #define	basename(cp)	((p = rindex((cp), '/')) != NULL ? p + 1 : (cp))
70 	nlistpath = argc > 0 ? argv[0] : _PATH_UNIX;
71 	nlistname = basename(nlistpath);
72 
73 	(void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
74 	    _PATH_VARDB, nlistname);
75 	(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
76 	    _PATH_VARDB, nlistname);
77 	(void)umask(0);
78 	db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
79 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, &openinfo);
80 	if (db == NULL)
81 		err(1, "%s", dbtemp);
82 	create_knlist(nlistpath, db);
83 	if (db->close(db))
84 		err(1, "%s", dbtemp);
85 	if (rename(dbtemp, dbname))
86 		err(1, "rename %s to %s", dbtemp, dbname);
87 	exit(0);
88 }
89 
90 void
91 usage()
92 {
93 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
94 	exit(1);
95 }
96