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