1 /* $OpenBSD: kvm_mkdb.c,v 1.22 2015/10/13 15:43:19 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 void usage(void); 52 int kvm_mkdb(int, const char *, char *, char *, 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 int fd, rval, ch, verbose = 0; 68 char *nlistpath, *nlistname; 69 char dbdir[PATH_MAX]; 70 71 /* Increase our data size to the max if we can. */ 72 if (getrlimit(RLIMIT_DATA, &rl) == 0) { 73 rl.rlim_cur = rl.rlim_max; 74 if (setrlimit(RLIMIT_DATA, &rl) < 0) 75 warn("can't set rlimit data size"); 76 } 77 78 /* getpwnam() and fchown() later */ 79 if (pledge("stdio rpath wpath cpath getpw fattr proc", NULL) == -1) 80 err(1, "pledge"); 81 82 strlcpy(dbdir, _PATH_VARDB, sizeof(dbdir)); 83 while ((ch = getopt(argc, argv, "vo:")) != -1) 84 switch (ch) { 85 case 'v': 86 verbose = 1; 87 break; 88 case 'o': 89 rval = strlcpy(dbdir, optarg, sizeof(dbdir)); 90 if (rval == 0 || rval + 1 >= sizeof(dbdir)) 91 errx(1, "Invalid directory"); 92 /* Make sure there is a '/' at the end of the path */ 93 if (dbdir[strlen(dbdir) - 1] != '/') 94 strlcat(dbdir, "/", sizeof(dbdir)); 95 break; 96 default: 97 usage(); 98 } 99 argc -= optind; 100 argv += optind; 101 102 if (argc > 1) 103 usage(); 104 105 /* If no kernel specified use _PATH_KSYMS and fall back to _PATH_UNIX */ 106 if (argc > 0) { 107 nlistpath = argv[0]; 108 nlistname = basename(nlistpath); 109 if ((fd = open(nlistpath, O_RDONLY, 0)) == -1) 110 err(1, "can't open %s", nlistpath); 111 rval = kvm_mkdb(fd, dbdir, nlistpath, nlistname, verbose); 112 } else { 113 nlistname = basename(_PATH_UNIX); 114 if ((fd = open((nlistpath = _PATH_KSYMS), O_RDONLY, 0)) == -1 || 115 (rval = kvm_mkdb(fd, dbdir, nlistpath, nlistname, 116 verbose)) != 0) { 117 if (fd == -1) 118 warnx("can't open %s", _PATH_KSYMS); 119 else 120 warnx("will try again using %s instead", _PATH_UNIX); 121 if ((fd = open((nlistpath = _PATH_UNIX), O_RDONLY, 0)) == -1) 122 err(1, "can't open %s", nlistpath); 123 rval = kvm_mkdb(fd, dbdir, nlistpath, nlistname, 124 verbose); 125 } 126 } 127 exit(rval); 128 } 129 130 int 131 kvm_mkdb(int fd, const char *dbdir, char *nlistpath, char *nlistname, 132 int verbose) 133 { 134 DB *db; 135 char dbtemp[PATH_MAX], dbname[PATH_MAX]; 136 int r; 137 struct group *gr; 138 139 r = snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp", 140 dbdir, nlistname); 141 if (r < 0 || r >= sizeof(dbtemp)) { 142 warnx("Directory name too long"); 143 return (1); 144 } 145 r = snprintf(dbname, sizeof(dbname), "%skvm_%s.db", 146 dbdir, nlistname); 147 if (r < 0 || r >= sizeof(dbtemp)) { 148 warnx("Directory name too long"); 149 return (1); 150 } 151 152 /* If the existing db file matches the currently running kernel, exit */ 153 if (testdb(dbname)) { 154 if (verbose) 155 warnx("%s already up to date", dbname); 156 return(0); 157 } else if (verbose) 158 warnx("rebuilding %s", dbname); 159 160 (void)umask(0); 161 db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR, 162 S_IRUSR | S_IWUSR | S_IRGRP, DB_HASH, &openinfo); 163 if (db == NULL) { 164 warn("can't dbopen %s", dbtemp); 165 return(1); 166 } 167 168 if ((gr = getgrnam("kmem")) == NULL) { 169 warn("can't find kmem group"); 170 } else if (fchown(db->fd(db), -1, gr->gr_gid)) { 171 warn("can't chown %s", dbtemp); 172 (void)unlink(dbtemp); 173 return(1); 174 } 175 176 /* rename() later */ 177 if (pledge("stdio cpath", NULL) == -1) 178 err(1, "pledge"); 179 180 if (create_knlist(nlistpath, fd, db) != 0) { 181 warn("cannot determine executable type of %s", nlistpath); 182 (void)unlink(dbtemp); 183 return(1); 184 } 185 if (db->close(db)) { 186 warn("can't dbclose %s", dbtemp); 187 (void)unlink(dbtemp); 188 return(1); 189 } 190 191 if (rename(dbtemp, dbname)) { 192 warn("rename %s to %s", dbtemp, dbname); 193 (void)unlink(dbtemp); 194 return(1); 195 } 196 197 return(0); 198 } 199 200 void 201 usage(void) 202 { 203 (void)fprintf(stderr, "usage: kvm_mkdb [-v] [-o directory] [file]\n"); 204 exit(1); 205 } 206