1 /* $NetBSD: dev_mkdb.c,v 1.19 2003/05/17 19:09:08 itojun 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "from: @(#)dev_mkdb.c 8.1 (Berkeley) 6/6/93"; 45 #else 46 __RCSID("$NetBSD: dev_mkdb.c,v 1.19 2003/05/17 19:09:08 itojun Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/stat.h> 52 53 #include <db.h> 54 #include <err.h> 55 #include <errno.h> 56 #include <fcntl.h> 57 #include <fts.h> 58 #include <kvm.h> 59 #include <nlist.h> 60 #include <paths.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 66 int main __P((int, char *[])); 67 void usage __P((void)); 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(argc, argv) 80 int argc; 81 char *argv[]; 82 { 83 struct stat *st; 84 struct { 85 mode_t type; 86 dev_t dev; 87 } bkey; 88 DB *db; 89 DBT data, key; 90 FTS *ftsp; 91 FTSENT *p; 92 int ch; 93 u_char buf[MAXPATHLEN + 1]; 94 char dbtmp[MAXPATHLEN + 1]; 95 char dbname[MAXPATHLEN + 1]; 96 char *dbname_arg = NULL; 97 char *pathv[2]; 98 char path_dev[MAXPATHLEN + 1] = _PATH_DEV; 99 char cur_dir[MAXPATHLEN + 1]; 100 struct timeval tv; 101 char *q; 102 103 while ((ch = getopt(argc, argv, "o:")) != -1) 104 switch (ch) { 105 case 'o': 106 dbname_arg = optarg; 107 break; 108 case '?': 109 default: 110 usage(); 111 } 112 argc -= optind; 113 argv += optind; 114 115 if (argc == 1) 116 if (strlcpy(path_dev, argv[0], sizeof(path_dev)) >= sizeof(path_dev)) 117 errx(1, "device path too long"); 118 119 if (argc > 1) 120 usage(); 121 122 if (!getcwd(cur_dir, sizeof(cur_dir))) 123 err(1, "%s", cur_dir); 124 125 if (chdir(path_dev)) 126 err(1, "%s", path_dev); 127 128 pathv[0] = path_dev; 129 pathv[1] = NULL; 130 ftsp = fts_open(pathv, FTS_PHYSICAL, NULL); 131 if (ftsp == NULL) 132 err(1, "fts_open: %s", path_dev); 133 134 if (chdir(cur_dir)) 135 err(1, "%s", cur_dir); 136 137 if (dbname_arg) { 138 if (strlcpy(dbname, dbname_arg, sizeof(dbname)) >= sizeof(dbname)) 139 errx(1, "dbname too long"); 140 } else { 141 if (snprintf(dbname, sizeof(dbname), "%sdev.db", _PATH_VARRUN) >= sizeof(dbname)) 142 errx(1, "dbname too long"); 143 } 144 /* 145 * We use rename() to produce the dev.db file from a temporary file, 146 * and rename() is not able to move files across filesystems. Hence we 147 * need the temporary file to be in the same directory as dev.db. 148 * 149 * Additionally, we might be working in a world writable directory, 150 * we must ensure that we are not opening an existing file, therefore 151 * the loop on dbopen. 152 */ 153 (void)strlcpy(dbtmp, dbname, sizeof(dbtmp)); 154 q = dbtmp + strlen(dbtmp); 155 do { 156 (void)gettimeofday(&tv, NULL); 157 (void)snprintf(q, sizeof(dbtmp) - (q - dbtmp), 158 "%ld.tmp", tv.tv_usec); 159 db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC, 160 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &openinfo); 161 } while (!db && (errno == EEXIST)); 162 if (db == NULL) 163 err(1, "%s", dbtmp); 164 165 /* 166 * Keys are a mode_t followed by a dev_t. The former is the type of 167 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 168 * that the structure may contain padding, so we have to clear it 169 * out here. 170 */ 171 memset(&bkey, 0, sizeof(bkey)); 172 key.data = &bkey; 173 key.size = sizeof(bkey); 174 data.data = buf; 175 while ((p = fts_read(ftsp)) != NULL) { 176 switch (p->fts_info) { 177 case FTS_DEFAULT: 178 st = p->fts_statp; 179 break; 180 default: 181 continue; 182 } 183 184 /* Create the key. */ 185 if (S_ISCHR(st->st_mode)) 186 bkey.type = S_IFCHR; 187 else if (S_ISBLK(st->st_mode)) 188 bkey.type = S_IFBLK; 189 else 190 continue; 191 bkey.dev = st->st_rdev; 192 193 /* 194 * Create the data; nul terminate the name so caller doesn't 195 * have to. Skip path_dev and slash. 196 */ 197 strlcpy(buf, p->fts_path + (strlen(path_dev) + 1), sizeof(buf)); 198 data.size = p->fts_pathlen - (strlen(path_dev) + 1) + 1; 199 if ((*db->put)(db, &key, &data, 0)) { 200 err(1, "dbput %s", dbtmp); 201 } 202 } 203 (void)(*db->close)(db); 204 fts_close(ftsp); 205 206 if (chdir(cur_dir)) 207 err(1, "%s", cur_dir); 208 if (rename(dbtmp, dbname)) 209 err(1, "rename %s to %s", dbtmp, dbname); 210 exit(0); 211 } 212 213 void 214 usage() 215 { 216 217 (void)fprintf(stderr, "usage: dev_mkdb [-o database] [directory]\n"); 218 exit(1); 219 } 220