1 /* $OpenBSD: dev_mkdb.c,v 1.17 2018/10/18 14:37:01 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 #include <sys/stat.h> 33 34 #include <db.h> 35 #include <dirent.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <paths.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 void usage(void); 46 47 int 48 main(int argc, char *argv[]) 49 { 50 DIR *dirp; 51 struct dirent *dp; 52 struct stat sb; 53 struct { 54 mode_t type; 55 dev_t dev; 56 } bkey; 57 DB *db; 58 DBT data, key; 59 HASHINFO info; 60 int ch; 61 u_char buf[MAXNAMLEN + 1]; 62 char dbtmp[PATH_MAX], dbname[PATH_MAX]; 63 64 (void)snprintf(dbtmp, sizeof(dbtmp), "%sdev.tmp", _PATH_VARRUN); 65 (void)snprintf(dbname, sizeof(dbname), "%sdev.db", _PATH_VARRUN); 66 67 if (unveil(_PATH_DEV, "r") == -1) 68 err(1, "unveil"); 69 if (unveil(dbtmp, "rwc") == -1) 70 err(1, "unveil"); 71 if (unveil(dbname, "wc") == -1) 72 err(1, "unveil"); 73 if (pledge("stdio rpath wpath cpath flock", NULL) == -1) 74 err(1, "pledge"); 75 76 while ((ch = getopt(argc, argv, "")) != -1) 77 switch(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 bzero(&info, sizeof(info)); 94 info.bsize = 8192; 95 db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_RDWR|O_TRUNC, 96 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &info); 97 if (db == NULL) 98 err(1, "%s", dbtmp); 99 100 /* 101 * Keys are a mode_t followed by a dev_t. The former is the type of 102 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 103 * that the structure may contain padding, so we have to clear it 104 * out here. 105 */ 106 bzero(&bkey, sizeof(bkey)); 107 key.data = &bkey; 108 key.size = sizeof(bkey); 109 data.data = buf; 110 while ((dp = readdir(dirp))) { 111 if (strcmp(dp->d_name, "..") == 0) 112 continue; 113 114 if (lstat(dp->d_name, &sb)) { 115 warn("%s", dp->d_name); 116 continue; 117 } 118 119 /* Create the key. */ 120 if (S_ISCHR(sb.st_mode)) 121 bkey.type = S_IFCHR; 122 else if (S_ISBLK(sb.st_mode)) 123 bkey.type = S_IFBLK; 124 else 125 continue; 126 bkey.dev = sb.st_rdev; 127 128 /* 129 * Create the data; nul terminate the name so caller doesn't 130 * have to. 131 */ 132 bcopy(dp->d_name, buf, dp->d_namlen); 133 buf[dp->d_namlen] = '\0'; 134 data.size = dp->d_namlen + 1; 135 if ((db->put)(db, &key, &data, 0)) 136 err(1, "dbput %s", dbtmp); 137 } 138 (void)(db->close)(db); 139 if (rename(dbtmp, dbname)) 140 err(1, "rename %s to %s", dbtmp, dbname); 141 142 return (0); 143 } 144 145 void 146 usage(void) 147 { 148 149 (void)fprintf(stderr, "usage: dev_mkdb\n"); 150 exit(1); 151 } 152