kvm_mkdb.c revision 1.21 1 1.21 sevan /* $NetBSD: kvm_mkdb.c,v 1.21 2018/01/23 21:06:25 sevan Exp $ */
2 1.10 thorpej
3 1.1 cgd /*-
4 1.6 pk * Copyright (c) 1990, 1993
5 1.6 pk * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.19 agc * 3. Neither the name of the University nor the names of its contributors
16 1.19 agc * may be used to endorse or promote products derived from this software
17 1.19 agc * without specific prior written permission.
18 1.19 agc *
19 1.19 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.19 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.19 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.19 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.19 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.19 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.19 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.19 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.19 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.19 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.19 agc * SUCH DAMAGE.
30 1.19 agc */
31 1.19 agc
32 1.19 agc /*-
33 1.19 agc * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
34 1.19 agc *
35 1.19 agc * Redistribution and use in source and binary forms, with or without
36 1.19 agc * modification, are permitted provided that the following conditions
37 1.19 agc * are met:
38 1.19 agc * 1. Redistributions of source code must retain the above copyright
39 1.19 agc * notice, this list of conditions and the following disclaimer.
40 1.19 agc * 2. Redistributions in binary form must reproduce the above copyright
41 1.19 agc * notice, this list of conditions and the following disclaimer in the
42 1.19 agc * documentation and/or other materials provided with the distribution.
43 1.1 cgd * 3. All advertising materials mentioning features or use of this software
44 1.1 cgd * must display the following acknowledgement:
45 1.1 cgd * This product includes software developed by the University of
46 1.1 cgd * California, Berkeley and its contributors.
47 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
48 1.1 cgd * may be used to endorse or promote products derived from this software
49 1.1 cgd * without specific prior written permission.
50 1.1 cgd *
51 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 1.1 cgd * SUCH DAMAGE.
62 1.1 cgd */
63 1.1 cgd
64 1.13 lukem #include <sys/cdefs.h>
65 1.1 cgd #ifndef lint
66 1.20 lukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
67 1.20 lukem The Regents of the University of California. All rights reserved.");
68 1.1 cgd #endif /* not lint */
69 1.1 cgd
70 1.1 cgd #ifndef lint
71 1.10 thorpej #if 0
72 1.11 cgd static char sccsid[] = "from: @(#)kvm_mkdb.c 8.3 (Berkeley) 5/4/95";
73 1.10 thorpej #else
74 1.21 sevan __RCSID("$NetBSD: kvm_mkdb.c,v 1.21 2018/01/23 21:06:25 sevan Exp $");
75 1.10 thorpej #endif
76 1.1 cgd #endif /* not lint */
77 1.1 cgd
78 1.1 cgd #include <sys/param.h>
79 1.1 cgd #include <sys/stat.h>
80 1.6 pk
81 1.1 cgd #include <db.h>
82 1.6 pk #include <err.h>
83 1.1 cgd #include <errno.h>
84 1.6 pk #include <fcntl.h>
85 1.6 pk #include <paths.h>
86 1.1 cgd #include <stdio.h>
87 1.6 pk #include <stdlib.h>
88 1.1 cgd #include <string.h>
89 1.11 cgd #include <unistd.h>
90 1.5 pk
91 1.6 pk #include "extern.h"
92 1.1 cgd
93 1.21 sevan static void usage(void) __dead;
94 1.5 pk
95 1.8 mycroft HASHINFO openinfo = {
96 1.8 mycroft 4096, /* bsize */
97 1.8 mycroft 128, /* ffactor */
98 1.8 mycroft 1024, /* nelem */
99 1.8 mycroft 2048 * 1024, /* cachesize */
100 1.8 mycroft NULL, /* hash() */
101 1.8 mycroft 0 /* lorder */
102 1.8 mycroft };
103 1.8 mycroft
104 1.9 cgd static DB *db;
105 1.16 msaitoh static char *dbname = NULL;
106 1.9 cgd static char dbtemp[MAXPATHLEN];
107 1.9 cgd
108 1.5 pk int
109 1.21 sevan main(int argc, char *argv[])
110 1.1 cgd {
111 1.1 cgd int ch;
112 1.18 ragge char *nlistpath;
113 1.16 msaitoh int docheck = 0;
114 1.18 ragge int fd;
115 1.1 cgd
116 1.15 msaitoh while ((ch = getopt(argc, argv, "o:")) != -1)
117 1.6 pk switch (ch) {
118 1.15 msaitoh case 'o':
119 1.15 msaitoh dbname = optarg;
120 1.15 msaitoh break;
121 1.15 msaitoh
122 1.1 cgd case '?':
123 1.1 cgd default:
124 1.1 cgd usage();
125 1.1 cgd }
126 1.1 cgd argc -= optind;
127 1.1 cgd argv += optind;
128 1.1 cgd
129 1.6 pk if (argc > 1)
130 1.6 pk usage();
131 1.6 pk
132 1.16 msaitoh if (dbname == NULL) {
133 1.16 msaitoh dbname = _PATH_KVMDB;
134 1.16 msaitoh docheck = 1;
135 1.16 msaitoh } else if (strncmp(_PATH_KVMDB, dbname, sizeof(_PATH_KVMDB)) == 0) {
136 1.16 msaitoh docheck = 1;
137 1.16 msaitoh }
138 1.16 msaitoh if (docheck) {
139 1.16 msaitoh /*
140 1.16 msaitoh * If the existing db file matches the currently running
141 1.16 msaitoh * kernel, exit
142 1.16 msaitoh */
143 1.16 msaitoh if (testdb())
144 1.16 msaitoh exit(0);
145 1.16 msaitoh }
146 1.6 pk
147 1.18 ragge if (argc <= 0) {
148 1.18 ragge /*
149 1.18 ragge * Check for useability of _PATH_KSYMS, if not
150 1.18 ragge * then fallback to _PATH_UNIX.
151 1.18 ragge * Should we complain if failure?
152 1.18 ragge */
153 1.18 ragge if ((fd = open(_PATH_KSYMS, O_RDONLY)) >= 0) {
154 1.18 ragge close(fd);
155 1.18 ragge nlistpath = _PATH_KSYMS;
156 1.18 ragge } else
157 1.18 ragge nlistpath = _PATH_UNIX;
158 1.18 ragge } else
159 1.18 ragge nlistpath = argv[0];
160 1.1 cgd
161 1.15 msaitoh (void)snprintf(dbtemp, sizeof(dbtemp), "%s.tmp", dbname);
162 1.1 cgd (void)umask(0);
163 1.6 pk db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
164 1.8 mycroft S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, &openinfo);
165 1.6 pk if (db == NULL)
166 1.6 pk err(1, "%s", dbtemp);
167 1.1 cgd create_knlist(nlistpath, db);
168 1.9 cgd if (db->close(db)) {
169 1.9 cgd warn("%s", dbtemp);
170 1.9 cgd db = NULL;
171 1.9 cgd punt();
172 1.9 cgd }
173 1.9 cgd db = NULL;
174 1.9 cgd if (rename(dbtemp, dbname)) {
175 1.9 cgd warn("rename %s to %s", dbtemp, dbname);
176 1.9 cgd punt();
177 1.9 cgd }
178 1.6 pk exit(0);
179 1.1 cgd }
180 1.1 cgd
181 1.21 sevan static void
182 1.21 sevan usage(void)
183 1.1 cgd {
184 1.15 msaitoh (void)fprintf(stderr, "usage: kvm_mkdb [-o database] [file]\n");
185 1.9 cgd exit(1);
186 1.9 cgd }
187 1.9 cgd
188 1.9 cgd void
189 1.9 cgd punt()
190 1.9 cgd {
191 1.9 cgd
192 1.9 cgd if (db != NULL)
193 1.9 cgd db->close(db);
194 1.9 cgd unlink(dbtemp);
195 1.1 cgd exit(1);
196 1.1 cgd }
197