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