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