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