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