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