1 /* $OpenBSD: dev_mkdb.c,v 1.10 2005/04/04 09:03:07 deraadt 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 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 /*static char sccsid[] = "from: @(#)dev_mkdb.c 8.1 (Berkeley) 6/6/93";*/ 40 static char rcsid[] = "$Id: dev_mkdb.c,v 1.10 2005/04/04 09:03:07 deraadt Exp $"; 41 #endif /* not lint */ 42 43 #include <sys/param.h> 44 #include <sys/stat.h> 45 46 #include <db.h> 47 #include <dirent.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <fcntl.h> 51 #include <paths.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 void usage(void); 58 59 int 60 main(int argc, char *argv[]) 61 { 62 DIR *dirp; 63 struct dirent *dp; 64 struct stat sb; 65 struct { 66 mode_t type; 67 dev_t dev; 68 } bkey; 69 DB *db; 70 DBT data, key; 71 HASHINFO info; 72 int ch; 73 u_char buf[MAXNAMLEN + 1]; 74 char dbtmp[MAXPATHLEN], dbname[MAXPATHLEN]; 75 76 while ((ch = getopt(argc, argv, "")) != -1) 77 switch((char)ch) { 78 case '?': 79 default: 80 usage(); 81 } 82 argc -= optind; 83 argv += optind; 84 85 if (argc > 0) 86 usage(); 87 88 if (chdir(_PATH_DEV)) 89 err(1, "%s", _PATH_DEV); 90 91 dirp = opendir("."); 92 93 (void)snprintf(dbtmp, sizeof(dbtmp), "%sdev.tmp", _PATH_VARRUN); 94 (void)snprintf(dbname, sizeof(dbtmp), "%sdev.db", _PATH_VARRUN); 95 bzero(&info, sizeof(info)); 96 info.bsize = 8192; 97 db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_RDWR|O_TRUNC, 98 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &info); 99 if (db == NULL) 100 err(1, "%s", dbtmp); 101 102 /* 103 * Keys are a mode_t followed by a dev_t. The former is the type of 104 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 105 * that the structure may contain padding, so we have to clear it 106 * out here. 107 */ 108 bzero(&bkey, sizeof(bkey)); 109 key.data = &bkey; 110 key.size = sizeof(bkey); 111 data.data = buf; 112 while ((dp = readdir(dirp))) { 113 if (lstat(dp->d_name, &sb)) { 114 warn("%s", dp->d_name); 115 continue; 116 } 117 118 /* Create the key. */ 119 if (S_ISCHR(sb.st_mode)) 120 bkey.type = S_IFCHR; 121 else if (S_ISBLK(sb.st_mode)) 122 bkey.type = S_IFBLK; 123 else 124 continue; 125 bkey.dev = sb.st_rdev; 126 127 /* 128 * Create the data; nul terminate the name so caller doesn't 129 * have to. 130 */ 131 bcopy(dp->d_name, buf, dp->d_namlen); 132 buf[dp->d_namlen] = '\0'; 133 data.size = dp->d_namlen + 1; 134 if ((db->put)(db, &key, &data, 0)) 135 err(1, "dbput %s", dbtmp); 136 } 137 (void)(db->close)(db); 138 if (rename(dbtmp, dbname)) 139 err(1, "rename %s to %s", dbtmp, dbname); 140 141 return (0); 142 } 143 144 void 145 usage(void) 146 { 147 148 (void)fprintf(stderr, "usage: dev_mkdb\n"); 149 exit(1); 150 } 151