1 /* $OpenBSD: dev_mkdb.c,v 1.20 2023/12/24 06:35:05 gnezdo 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 <err.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <fts.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 FTS *fts; 51 FTSENT *dp; 52 char *paths[] = { ".", NULL }; 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 char dbtmp[PATH_MAX], dbname[PATH_MAX]; 62 63 (void)snprintf(dbtmp, sizeof(dbtmp), "%sdev.tmp", _PATH_VARRUN); 64 (void)snprintf(dbname, sizeof(dbname), "%sdev.db", _PATH_VARRUN); 65 66 if (unveil(_PATH_DEV, "r") == -1) 67 err(1, "unveil %s", _PATH_DEV); 68 if (unveil(dbtmp, "rwc") == -1) 69 err(1, "unveil %s", dbtmp); 70 if (unveil(dbname, "wc") == -1) 71 err(1, "unveil %s", dbname); 72 if (pledge("stdio rpath wpath cpath flock", NULL) == -1) 73 err(1, "pledge"); 74 75 while ((ch = getopt(argc, argv, "")) != -1) 76 switch(ch) { 77 default: 78 usage(); 79 } 80 argc -= optind; 81 argv += optind; 82 83 if (argc > 0) 84 usage(); 85 86 if (chdir(_PATH_DEV)) 87 err(1, "%s", _PATH_DEV); 88 89 fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR, NULL); 90 if (!fts) 91 err(1, "fts_open"); 92 93 94 bzero(&info, sizeof(info)); 95 info.bsize = 8192; 96 db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_RDWR|O_TRUNC, 97 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &info); 98 if (db == NULL) 99 err(1, "%s", dbtmp); 100 101 /* 102 * Keys are a mode_t followed by a dev_t. The former is the type of 103 * the file (mode & S_IFMT), the latter is the st_rdev field. Note 104 * that the structure may contain padding, so we have to clear it 105 * out here. 106 */ 107 bzero(&bkey, sizeof(bkey)); 108 key.data = &bkey; 109 key.size = sizeof(bkey); 110 while ((dp = fts_read(fts))) { 111 if (dp->fts_info != FTS_DEFAULT) 112 continue; 113 114 /* Create the key. */ 115 if (S_ISCHR(dp->fts_statp->st_mode)) 116 bkey.type = S_IFCHR; 117 else if (S_ISBLK(dp->fts_statp->st_mode)) 118 bkey.type = S_IFBLK; 119 else 120 continue; 121 bkey.dev = dp->fts_statp->st_rdev; 122 123 /* 124 * Create the data; nul terminate the name so caller doesn't 125 * have to. strlen("./") is 2, which is stripped to remove the 126 * traversal root name. 127 */ 128 data.data = dp->fts_path + 2; 129 data.size = dp->fts_pathlen - 2 + 1; 130 if ((db->put)(db, &key, &data, 0)) 131 err(1, "dbput %s", dbtmp); 132 } 133 fts_close(fts); 134 135 (void)(db->close)(db); 136 if (rename(dbtmp, dbname)) 137 err(1, "rename %s to %s", dbtmp, dbname); 138 139 return (0); 140 } 141 142 void 143 usage(void) 144 { 145 146 (void)fprintf(stderr, "usage: dev_mkdb\n"); 147 exit(1); 148 } 149