1 /* $OpenBSD: kvm_mkdb.c,v 1.19 2014/12/23 03:29:52 tedu 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 #include <sys/types.h> 35 #include <sys/time.h> 36 #include <sys/resource.h> 37 38 #include <db.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <libgen.h> 43 #include <paths.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <grp.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 struct group *gr; 135 136 r = snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp", 137 dbdir, nlistname); 138 if (r < 0 || r >= sizeof(dbtemp)) { 139 warnx("Directory name too long"); 140 return (1); 141 } 142 r = snprintf(dbname, sizeof(dbname), "%skvm_%s.db", 143 dbdir, nlistname); 144 if (r < 0 || r >= sizeof(dbtemp)) { 145 warnx("Directory name too long"); 146 return (1); 147 } 148 149 /* If the existing db file matches the currently running kernel, exit */ 150 if (testdb(dbname)) { 151 if (verbose) 152 warnx("%s already up to date", dbname); 153 return(0); 154 } else if (verbose) 155 warnx("rebuilding %s", dbname); 156 157 (void)umask(0); 158 db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR, 159 S_IRUSR | S_IWUSR | S_IRGRP, DB_HASH, &openinfo); 160 if (db == NULL) { 161 warn("can't dbopen %s", dbtemp); 162 return(1); 163 } 164 if (create_knlist(nlistpath, fd, db) != 0) { 165 warn("cannot determine executable type of %s", nlistpath); 166 (void)unlink(dbtemp); 167 return(1); 168 } 169 if (db->close(db)) { 170 warn("can't dbclose %s", dbtemp); 171 (void)unlink(dbtemp); 172 return(1); 173 } 174 175 if ((gr = getgrnam("kmem")) == NULL) { 176 warn("can't find kmem group"); 177 } else if (chown(dbtemp, -1, gr->gr_gid)) { 178 warn("can't chown %s", dbtemp); 179 (void)unlink(dbtemp); 180 return(1); 181 } 182 if (rename(dbtemp, dbname)) { 183 warn("rename %s to %s", dbtemp, dbname); 184 (void)unlink(dbtemp); 185 return(1); 186 } 187 188 return(0); 189 } 190 191 void 192 usage(void) 193 { 194 (void)fprintf(stderr, "usage: kvm_mkdb [-v] [-o directory] [file]\n"); 195 exit(1); 196 } 197