dev_mkdb.c revision 1.13 1 /* $NetBSD: dev_mkdb.c,v 1.13 2001/07/08 20:01:43 manu 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.13 2001/07/08 20:01:43 manu 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];
86 char dbname[MAXPATHLEN + 1];
87 char *dbname_arg = NULL;
88 char *pathv[2];
89 char path_dev[MAXPATHLEN + 1] = _PATH_DEV;
90 char cur_dir[MAXPATHLEN + 1];
91
92 while ((ch = getopt(argc, argv, "o:")) != -1)
93 switch (ch) {
94 case 'o':
95 if (strlen(optarg) <= MAXPATHLEN)
96 dbname_arg = optarg;
97 break;
98 case '?':
99 default:
100 usage();
101 }
102 argc -= optind;
103 argv += optind;
104
105 if ((argc == 1) && (strlen(argv[0]) <= MAXPATHLEN))
106 strncpy(path_dev, argv[0], MAXPATHLEN);
107
108 if (argc > 1)
109 usage();
110
111 if (!getcwd(cur_dir, MAXPATHLEN))
112 err(1, "%s", cur_dir);
113
114 if (chdir(path_dev))
115 err(1, "%s", path_dev);
116
117 pathv[0] = path_dev;
118 pathv[1] = NULL;
119 ftsp = fts_open(pathv, FTS_PHYSICAL, NULL);
120 if (ftsp == NULL)
121 err(1, "fts_open: %s", path_dev);
122
123 /*
124 * We use rename() to produce the dev.db file, and rename()
125 * is not able to move files across filesystems. Hence we use
126 * /var/run for dev.tmp, since dev.db will be in /var/run.
127 */
128 (void)snprintf(dbtmp, MAXPATHLEN, "%sdev.tmp", _PATH_VARRUN);
129 if (dbname_arg)
130 strncpy(dbname, dbname_arg, MAXPATHLEN);
131 else
132 (void)snprintf(dbname, MAXPATHLEN, "%sdev.db", _PATH_VARRUN);
133 db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC,
134 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
135 if (db == NULL)
136 err(1, "%s", dbtmp);
137
138 /*
139 * Keys are a mode_t followed by a dev_t. The former is the type of
140 * the file (mode & S_IFMT), the latter is the st_rdev field. Note
141 * that the structure may contain padding, so we have to clear it
142 * out here.
143 */
144 memset(&bkey, 0, sizeof(bkey));
145 key.data = &bkey;
146 key.size = sizeof(bkey);
147 data.data = buf;
148 while ((p = fts_read(ftsp)) != NULL) {
149 switch (p->fts_info) {
150 case FTS_DEFAULT:
151 st = p->fts_statp;
152 break;
153 default:
154 continue;
155 }
156
157 /* Create the key. */
158 if (S_ISCHR(st->st_mode))
159 bkey.type = S_IFCHR;
160 else if (S_ISBLK(st->st_mode))
161 bkey.type = S_IFBLK;
162 else
163 continue;
164 bkey.dev = st->st_rdev;
165
166 /*
167 * Create the data; nul terminate the name so caller doesn't
168 * have to. Skip path_dev and slash.
169 */
170 strlcpy(buf, p->fts_path + strlen(path_dev), sizeof(buf));
171 data.size = p->fts_pathlen - strlen(path_dev) + 1;
172 if ((*db->put)(db, &key, &data, 0)) {
173 err(1, "dbput %s", dbtmp);
174 }
175 }
176 (void)(*db->close)(db);
177 fts_close(ftsp);
178 if (chdir(cur_dir))
179 err(1, "%s", cur_dir);
180 if (rename(dbtmp, dbname))
181 err(1, "rename %s to %s", dbtmp, dbname);
182 exit(0);
183 }
184
185 void
186 usage()
187 {
188
189 (void)fprintf(stderr, "usage: dev_mkdb [-o database] [directory]\n");
190 exit(1);
191 }
192