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