1 /* $OpenBSD: dev_mkdb.c,v 1.19 2022/12/04 23:50:50 cheloha 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 %s", _PATH_DEV); 69 if (unveil(dbtmp, "rwc") == -1) 70 err(1, "unveil %s", dbtmp); 71 if (unveil(dbname, "wc") == -1) 72 err(1, "unveil %s", dbname); 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 default: 79 usage(); 80 } 81 argc -= optind; 82 argv += optind; 83 84 if (argc > 0) 85 usage(); 86 87 if (chdir(_PATH_DEV)) 88 err(1, "%s", _PATH_DEV); 89 90 dirp = opendir("."); 91 92 bzero(&info, sizeof(info)); 93 info.bsize = 8192; 94 db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_RDWR|O_TRUNC, 95 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &info); 96 if (db == NULL) 97 err(1, "%s", dbtmp); 98 99 /* 100 * Keys are a mode_t followed by a dev_t. The former is the type of 101 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 102 * that the structure may contain padding, so we have to clear it 103 * out here. 104 */ 105 bzero(&bkey, sizeof(bkey)); 106 key.data = &bkey; 107 key.size = sizeof(bkey); 108 data.data = buf; 109 while ((dp = readdir(dirp))) { 110 if (strcmp(dp->d_name, "..") == 0) 111 continue; 112 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