1 /* $NetBSD: dev_mkdb.c,v 1.18 2002/12/15 18:23:00 hannken 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.18 2002/12/15 18:23:00 hannken 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 if (strlen(optarg) <= MAXPATHLEN) 107 dbname_arg = optarg; 108 break; 109 case '?': 110 default: 111 usage(); 112 } 113 argc -= optind; 114 argv += optind; 115 116 if ((argc == 1) && (strlen(argv[0]) <= MAXPATHLEN)) 117 strncpy(path_dev, argv[0], MAXPATHLEN); 118 119 if (argc > 1) 120 usage(); 121 122 if (!getcwd(cur_dir, MAXPATHLEN)) 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 strncpy(dbname, dbname_arg, MAXPATHLEN); 139 else 140 (void)snprintf(dbname, MAXPATHLEN, "%sdev.db", _PATH_VARRUN); 141 /* 142 * We use rename() to produce the dev.db file from a temporary file, 143 * and rename() is not able to move files across filesystems. Hence we 144 * need the temporary file to be in the same directory as dev.db. 145 * 146 * Additionally, we might be working in a world writable directory, 147 * we must ensure that we are not opening an existing file, therefore 148 * the loop on dbopen. 149 */ 150 (void)strncpy(dbtmp, dbname, MAXPATHLEN); 151 q = dbtmp + strlen(dbtmp); 152 do { 153 (void)gettimeofday(&tv, NULL); 154 (void)snprintf(q, MAXPATHLEN - (long)(q - dbtmp), 155 "%ld.tmp", tv.tv_usec); 156 db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC, 157 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &openinfo); 158 } while (!db && (errno == EEXIST)); 159 if (db == NULL) 160 err(1, "%s", dbtmp); 161 162 /* 163 * Keys are a mode_t followed by a dev_t. The former is the type of 164 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 165 * that the structure may contain padding, so we have to clear it 166 * out here. 167 */ 168 memset(&bkey, 0, sizeof(bkey)); 169 key.data = &bkey; 170 key.size = sizeof(bkey); 171 data.data = buf; 172 while ((p = fts_read(ftsp)) != NULL) { 173 switch (p->fts_info) { 174 case FTS_DEFAULT: 175 st = p->fts_statp; 176 break; 177 default: 178 continue; 179 } 180 181 /* Create the key. */ 182 if (S_ISCHR(st->st_mode)) 183 bkey.type = S_IFCHR; 184 else if (S_ISBLK(st->st_mode)) 185 bkey.type = S_IFBLK; 186 else 187 continue; 188 bkey.dev = st->st_rdev; 189 190 /* 191 * Create the data; nul terminate the name so caller doesn't 192 * have to. Skip path_dev and slash. 193 */ 194 strlcpy(buf, p->fts_path + (strlen(path_dev) + 1), sizeof(buf)); 195 data.size = p->fts_pathlen - (strlen(path_dev) + 1) + 1; 196 if ((*db->put)(db, &key, &data, 0)) { 197 err(1, "dbput %s", dbtmp); 198 } 199 } 200 (void)(*db->close)(db); 201 fts_close(ftsp); 202 203 if (chdir(cur_dir)) 204 err(1, "%s", cur_dir); 205 if (rename(dbtmp, dbname)) 206 err(1, "rename %s to %s", dbtmp, dbname); 207 exit(0); 208 } 209 210 void 211 usage() 212 { 213 214 (void)fprintf(stderr, "usage: dev_mkdb [-o database] [directory]\n"); 215 exit(1); 216 } 217