GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/kvm_mkdb/kvm_mkdb.c Lines: 0 87 0.0 %
Date: 2017-11-13 Branches: 0 63 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: kvm_mkdb.c,v 1.28 2016/04/25 16:03:57 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/stat.h>
33
#include <sys/types.h>
34
#include <sys/time.h>
35
#include <sys/resource.h>
36
37
#include <db.h>
38
#include <err.h>
39
#include <errno.h>
40
#include <fcntl.h>
41
#include <libgen.h>
42
#include <paths.h>
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include <string.h>
46
#include <unistd.h>
47
#include <grp.h>
48
49
#include "extern.h"
50
51
__dead void usage(void);
52
int kvm_mkdb(int, const char *, char *, char *, gid_t, int);
53
54
HASHINFO openinfo = {
55
	4096,		/* bsize */
56
	128,		/* ffactor */
57
	1024,		/* nelem */
58
	2048 * 1024,	/* cachesize */
59
	NULL,		/* hash() */
60
	0		/* lorder */
61
};
62
63
int
64
main(int argc, char *argv[])
65
{
66
	struct rlimit rl;
67
	struct group *gr;
68
	gid_t kvm_gid = -1;
69
	int fd, rval, ch, verbose = 0;
70
	char *nlistpath, *nlistname;
71
	char dbdir[PATH_MAX];
72
73
	if (pledge("stdio rpath wpath cpath fattr getpw flock id", NULL) == -1)
74
		err(1, "pledge");
75
76
	/* Try to use the kmem group to be able to fchown() in kvm_mkdb(). */
77
	if ((gr = getgrnam("kmem")) == NULL) {
78
		warn("can't find kmem group");
79
	} else {
80
		kvm_gid = gr->gr_gid;
81
		if (setresgid(kvm_gid, kvm_gid, kvm_gid) == -1)
82
			err(1, "setegid");
83
	}
84
85
	/* Increase our data size to the max if we can. */
86
	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
87
		rl.rlim_cur = rl.rlim_max;
88
		if (setrlimit(RLIMIT_DATA, &rl) < 0)
89
			warn("can't set rlimit data size");
90
	}
91
92
	if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1)
93
		err(1, "pledge");
94
95
	strlcpy(dbdir, _PATH_VARDB, sizeof(dbdir));
96
	while ((ch = getopt(argc, argv, "vo:")) != -1)
97
		switch (ch) {
98
		case 'v':
99
			verbose = 1;
100
			break;
101
		case 'o':
102
			rval = strlcpy(dbdir, optarg, sizeof(dbdir));
103
			if (rval == 0 || rval + 1 >= sizeof(dbdir))
104
				errx(1, "Invalid directory");
105
			/* Make sure there is a '/' at the end of the path */
106
			if (dbdir[strlen(dbdir) - 1] != '/')
107
				strlcat(dbdir, "/", sizeof(dbdir));
108
			break;
109
		default:
110
			usage();
111
		}
112
	argc -= optind;
113
	argv += optind;
114
115
	if (argc > 1)
116
		usage();
117
118
	/* If no kernel specified use _PATH_KSYMS and fall back to _PATH_UNIX */
119
	if (argc > 0) {
120
		nlistpath = argv[0];
121
		nlistname = basename(nlistpath);
122
		if ((fd = open(nlistpath, O_RDONLY, 0)) == -1)
123
			err(1, "can't open %s", nlistpath);
124
		rval = kvm_mkdb(fd, dbdir, nlistpath, nlistname, kvm_gid,
125
		    verbose);
126
	} else {
127
		nlistname = basename(_PATH_UNIX);
128
		if ((fd = open((nlistpath = _PATH_KSYMS), O_RDONLY, 0)) == -1 ||
129
		    (rval = kvm_mkdb(fd, dbdir, nlistpath, nlistname, kvm_gid,
130
		    verbose)) != 0) {
131
			if (fd == -1)
132
				warnx("can't open %s", _PATH_KSYMS);
133
			else
134
				warnx("will try again using %s instead", _PATH_UNIX);
135
			if ((fd = open((nlistpath = _PATH_UNIX), O_RDONLY, 0)) == -1)
136
				err(1, "can't open %s", nlistpath);
137
			rval = kvm_mkdb(fd, dbdir, nlistpath, nlistname,
138
			    kvm_gid, verbose);
139
		}
140
	}
141
	exit(rval);
142
}
143
144
int
145
kvm_mkdb(int fd, const char *dbdir, char *nlistpath, char *nlistname, gid_t gid,
146
    int verbose)
147
{
148
	DB *db;
149
	char dbtemp[PATH_MAX], dbname[PATH_MAX];
150
	int r;
151
152
	r = snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
153
	    dbdir, nlistname);
154
	if (r < 0 || r >= sizeof(dbtemp)) {
155
		warnx("Directory name too long");
156
		return (1);
157
	}
158
	r = snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
159
	    dbdir, nlistname);
160
	if (r < 0 || r >= sizeof(dbtemp)) {
161
		warnx("Directory name too long");
162
		return (1);
163
	}
164
165
	/* If the existing db file matches the currently running kernel, exit */
166
	if (testdb(dbname)) {
167
		if (verbose)
168
			warnx("%s already up to date", dbname);
169
		return(0);
170
	} else if (verbose)
171
		warnx("rebuilding %s", dbname);
172
173
	(void)umask(0);
174
	db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
175
	    S_IRUSR | S_IWUSR | S_IRGRP, DB_HASH, &openinfo);
176
	if (db == NULL) {
177
		warn("can't dbopen %s", dbtemp);
178
		return(1);
179
	}
180
181
	if (gid != -1 && fchown(db->fd(db), -1, gid) == -1) {
182
		warn("can't chown %s", dbtemp);
183
		(void)unlink(dbtemp);
184
		return(1);
185
	}
186
187
	if (create_knlist(nlistpath, fd, db) != 0) {
188
		warn("cannot determine executable type of %s", nlistpath);
189
		(void)unlink(dbtemp);
190
		return(1);
191
	}
192
	if (db->close(db)) {
193
		warn("can't dbclose %s", dbtemp);
194
		(void)unlink(dbtemp);
195
		return(1);
196
	}
197
198
	if (rename(dbtemp, dbname)) {
199
		warn("rename %s to %s", dbtemp, dbname);
200
		(void)unlink(dbtemp);
201
		return(1);
202
	}
203
204
	return(0);
205
}
206
207
__dead void
208
usage(void)
209
{
210
	(void)fprintf(stderr, "usage: kvm_mkdb [-v] [-o directory] [file]\n");
211
	exit(1);
212
}