1 /* $NetBSD: dev_mkdb.c,v 1.20 2003/08/07 11:25:18 agc 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/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) 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: @(#)dev_mkdb.c 8.1 (Berkeley) 6/6/93"; 41 #else 42 __RCSID("$NetBSD: dev_mkdb.c,v 1.20 2003/08/07 11:25:18 agc 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 <fts.h> 54 #include <kvm.h> 55 #include <nlist.h> 56 #include <paths.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 int main __P((int, char *[])); 63 void usage __P((void)); 64 65 HASHINFO openinfo = { 66 4096, /* bsize */ 67 128, /* ffactor */ 68 1024, /* nelem */ 69 2048 * 1024, /* cachesize */ 70 NULL, /* hash() */ 71 0 /* lorder */ 72 }; 73 74 int 75 main(argc, argv) 76 int argc; 77 char *argv[]; 78 { 79 struct stat *st; 80 struct { 81 mode_t type; 82 dev_t dev; 83 } bkey; 84 DB *db; 85 DBT data, key; 86 FTS *ftsp; 87 FTSENT *p; 88 int ch; 89 u_char buf[MAXPATHLEN + 1]; 90 char dbtmp[MAXPATHLEN + 1]; 91 char dbname[MAXPATHLEN + 1]; 92 char *dbname_arg = NULL; 93 char *pathv[2]; 94 char path_dev[MAXPATHLEN + 1] = _PATH_DEV; 95 char cur_dir[MAXPATHLEN + 1]; 96 struct timeval tv; 97 char *q; 98 99 while ((ch = getopt(argc, argv, "o:")) != -1) 100 switch (ch) { 101 case 'o': 102 dbname_arg = optarg; 103 break; 104 case '?': 105 default: 106 usage(); 107 } 108 argc -= optind; 109 argv += optind; 110 111 if (argc == 1) 112 if (strlcpy(path_dev, argv[0], sizeof(path_dev)) >= sizeof(path_dev)) 113 errx(1, "device path too long"); 114 115 if (argc > 1) 116 usage(); 117 118 if (!getcwd(cur_dir, sizeof(cur_dir))) 119 err(1, "%s", cur_dir); 120 121 if (chdir(path_dev)) 122 err(1, "%s", path_dev); 123 124 pathv[0] = path_dev; 125 pathv[1] = NULL; 126 ftsp = fts_open(pathv, FTS_PHYSICAL, NULL); 127 if (ftsp == NULL) 128 err(1, "fts_open: %s", path_dev); 129 130 if (chdir(cur_dir)) 131 err(1, "%s", cur_dir); 132 133 if (dbname_arg) { 134 if (strlcpy(dbname, dbname_arg, sizeof(dbname)) >= sizeof(dbname)) 135 errx(1, "dbname too long"); 136 } else { 137 if (snprintf(dbname, sizeof(dbname), "%sdev.db", _PATH_VARRUN) >= sizeof(dbname)) 138 errx(1, "dbname too long"); 139 } 140 /* 141 * We use rename() to produce the dev.db file from a temporary file, 142 * and rename() is not able to move files across filesystems. Hence we 143 * need the temporary file to be in the same directory as dev.db. 144 * 145 * Additionally, we might be working in a world writable directory, 146 * we must ensure that we are not opening an existing file, therefore 147 * the loop on dbopen. 148 */ 149 (void)strlcpy(dbtmp, dbname, sizeof(dbtmp)); 150 q = dbtmp + strlen(dbtmp); 151 do { 152 (void)gettimeofday(&tv, NULL); 153 (void)snprintf(q, sizeof(dbtmp) - (q - dbtmp), 154 "%ld.tmp", tv.tv_usec); 155 db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC, 156 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &openinfo); 157 } while (!db && (errno == EEXIST)); 158 if (db == NULL) 159 err(1, "%s", dbtmp); 160 161 /* 162 * Keys are a mode_t followed by a dev_t. The former is the type of 163 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 164 * that the structure may contain padding, so we have to clear it 165 * out here. 166 */ 167 memset(&bkey, 0, sizeof(bkey)); 168 key.data = &bkey; 169 key.size = sizeof(bkey); 170 data.data = buf; 171 while ((p = fts_read(ftsp)) != NULL) { 172 switch (p->fts_info) { 173 case FTS_DEFAULT: 174 st = p->fts_statp; 175 break; 176 default: 177 continue; 178 } 179 180 /* Create the key. */ 181 if (S_ISCHR(st->st_mode)) 182 bkey.type = S_IFCHR; 183 else if (S_ISBLK(st->st_mode)) 184 bkey.type = S_IFBLK; 185 else 186 continue; 187 bkey.dev = st->st_rdev; 188 189 /* 190 * Create the data; nul terminate the name so caller doesn't 191 * have to. Skip path_dev and slash. 192 */ 193 strlcpy(buf, p->fts_path + (strlen(path_dev) + 1), sizeof(buf)); 194 data.size = p->fts_pathlen - (strlen(path_dev) + 1) + 1; 195 if ((*db->put)(db, &key, &data, 0)) { 196 err(1, "dbput %s", dbtmp); 197 } 198 } 199 (void)(*db->close)(db); 200 fts_close(ftsp); 201 202 if (chdir(cur_dir)) 203 err(1, "%s", cur_dir); 204 if (rename(dbtmp, dbname)) 205 err(1, "rename %s to %s", dbtmp, dbname); 206 exit(0); 207 } 208 209 void 210 usage() 211 { 212 213 (void)fprintf(stderr, "usage: dev_mkdb [-o database] [directory]\n"); 214 exit(1); 215 } 216