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