xref: /netbsd-src/usr.sbin/dev_mkdb/dev_mkdb.c (revision e55cffd8e520e9b03f18a1bd98bb04223e79f69f)
1 /*	$NetBSD: dev_mkdb.c,v 1.10 2001/04/10 06:11:27 enami 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "from: @(#)dev_mkdb.c	8.1 (Berkeley) 6/6/93";
45 #else
46 __RCSID("$NetBSD: dev_mkdb.c,v 1.10 2001/04/10 06:11:27 enami Exp $");
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 
53 #include <db.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <fts.h>
58 #include <kvm.h>
59 #include <nlist.h>
60 #include <paths.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 
66 int	main __P((int, char *[]));
67 void	usage __P((void));
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	char *argv[];
73 {
74 	struct stat *st;
75 	struct {
76 		mode_t type;
77 		dev_t dev;
78 	} bkey;
79 	DB *db;
80 	DBT data, key;
81 	FTS *ftsp;
82 	FTSENT *p;
83 	int ch;
84 	u_char buf[MAXPATHLEN + 1];
85 	char dbtmp[MAXPATHLEN + 1], dbname[MAXPATHLEN + 1];
86 	char *pathv[2];
87 
88 	while ((ch = getopt(argc, argv, "")) != -1)
89 		switch (ch) {
90 		case '?':
91 		default:
92 			usage();
93 		}
94 	argc -= optind;
95 	argv += optind;
96 
97 	if (argc > 0)
98 		usage();
99 
100 	if (chdir(_PATH_DEV))
101 		err(1, "%s", _PATH_DEV);
102 
103 	pathv[0] = _PATH_DEV;
104 	pathv[1] = NULL;
105 	ftsp = fts_open(pathv, FTS_PHYSICAL, NULL);
106 	if (ftsp == NULL)
107 		err(1, "fts_open: %s", _PATH_DEV);
108 
109 	(void)snprintf(dbtmp, sizeof(dbtmp), "%sdev.tmp", _PATH_VARRUN);
110 	(void)snprintf(dbname, sizeof(dbtmp), "%sdev.db", _PATH_VARRUN);
111 	db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_RDWR|O_TRUNC,
112 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
113 	if (db == NULL)
114 		err(1, "%s", dbtmp);
115 
116 	/*
117 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
118 	 * the file (mode & S_IFMT), the latter is the st_rdev field.  Note
119 	 * that the structure may contain padding, so we have to clear it
120 	 * out here.
121 	 */
122 	memset(&bkey, 0, sizeof(bkey));
123 	key.data = &bkey;
124 	key.size = sizeof(bkey);
125 	data.data = buf;
126 	while ((p = fts_read(ftsp)) != NULL) {
127 		switch (p->fts_info) {
128 		case FTS_DEFAULT:
129 			st = p->fts_statp;
130 			break;
131 		default:
132 			continue;
133 		}
134 
135 		/* Create the key. */
136 		if (S_ISCHR(st->st_mode))
137 			bkey.type = S_IFCHR;
138 		else if (S_ISBLK(st->st_mode))
139 			bkey.type = S_IFBLK;
140 		else
141 			continue;
142 		bkey.dev = st->st_rdev;
143 
144 		/*
145 		 * Create the data; nul terminate the name so caller doesn't
146 		 * have to.  Skip _PATH_DEV and slash.
147 		 */
148 		strlcpy(buf, p->fts_path + sizeof(_PATH_DEV), sizeof(buf));
149 		data.size = p->fts_pathlen - sizeof(_PATH_DEV) + 1;
150 		if ((*db->put)(db, &key, &data, 0))
151 			err(1, "dbput %s", dbtmp);
152 	}
153 	(void)(*db->close)(db);
154 	fts_close(ftsp);
155 	if (rename(dbtmp, dbname))
156 		err(1, "rename %s to %s", dbtmp, dbname);
157 	exit(0);
158 }
159 
160 void
161 usage()
162 {
163 
164 	(void)fprintf(stderr, "usage: dev_mkdb\n");
165 	exit(1);
166 }
167