Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / dev_mkdb / dev_mkdb.c
blob567d1bd67011fcbafbfed8c151c6fc8ba1063acc
1 /* $NetBSD: dev_mkdb.c,v 1.26 2008/12/28 21:39:35 christos Exp $ */
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "from: @(#)dev_mkdb.c 8.1 (Berkeley) 6/6/93";
41 #else
42 __RCSID("$NetBSD: dev_mkdb.c,v 1.26 2008/12/28 21:39:35 christos Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/param.h>
47 #include <sys/stat.h>
49 #include <db.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <fts.h>
54 #include <kvm.h>
55 #include <nlist.h>
56 #include <paths.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
62 static void usage(void) __unused;
64 HASHINFO openinfo = {
65 4096, /* bsize */
66 128, /* ffactor */
67 1024, /* nelem */
68 2048 * 1024, /* cachesize */
69 NULL, /* hash() */
70 0 /* lorder */
73 int
74 main(int argc, char **argv)
76 struct stat *st;
77 struct {
78 mode_t type;
79 dev_t dev;
80 } bkey;
81 DB *db;
82 DBT data, key;
83 FTS *ftsp;
84 FTSENT *p;
85 int ch;
86 char buf[MAXPATHLEN + 1];
87 char dbtmp[MAXPATHLEN + 1];
88 char dbname[MAXPATHLEN + 1];
89 char *dbname_arg = NULL;
90 char *pathv[2];
91 char path_dev[MAXPATHLEN + 1] = _PATH_DEV;
92 char cur_dir[MAXPATHLEN + 1];
93 struct timeval tv;
94 char *q;
95 size_t dlen;
97 setprogname(argv[0]);
99 while ((ch = getopt(argc, argv, "o:")) != -1)
100 switch (ch) {
101 case 'o':
102 dbname_arg = optarg;
103 break;
104 case '?':
105 default:
106 usage();
108 argc -= optind;
109 argv += optind;
111 if (argc == 1)
112 if (strlcpy(path_dev, argv[0], sizeof(path_dev)) >=
113 sizeof(path_dev))
114 errx(1, "device path too long");
116 if (argc > 1)
117 usage();
119 if (getcwd(cur_dir, sizeof(cur_dir)) == NULL)
120 err(1, "%s", cur_dir);
122 if (chdir(path_dev) == -1)
123 err(1, "%s", path_dev);
125 pathv[0] = path_dev;
126 pathv[1] = NULL;
127 dlen = strlen(path_dev) - 1;
128 ftsp = fts_open(pathv, FTS_PHYSICAL, NULL);
129 if (ftsp == NULL)
130 err(1, "fts_open: %s", path_dev);
132 if (chdir(cur_dir) == -1)
133 err(1, "%s", cur_dir);
135 if (dbname_arg) {
136 if (strlcpy(dbname, dbname_arg, sizeof(dbname)) >=
137 sizeof(dbname))
138 errx(1, "dbname too long");
139 } else {
140 if (snprintf(dbname, sizeof(dbname), "%sdev.db",
141 _PATH_VARRUN) >= (int)sizeof(dbname))
142 errx(1, "dbname too long");
145 * We use rename() to produce the dev.db file from a temporary file,
146 * and rename() is not able to move files across filesystems. Hence we
147 * need the temporary file to be in the same directory as dev.db.
149 * Additionally, we might be working in a world writable directory,
150 * we must ensure that we are not opening an existing file, therefore
151 * the loop on dbopen.
153 (void)strlcpy(dbtmp, dbname, sizeof(dbtmp));
154 q = dbtmp + strlen(dbtmp);
155 do {
156 (void)gettimeofday(&tv, NULL);
157 (void)snprintf(q, sizeof(dbtmp) - (q - dbtmp),
158 "%ld.tmp", (long)tv.tv_usec);
159 db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC,
160 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &openinfo);
161 } while (!db && (errno == EEXIST));
162 if (db == NULL)
163 err(1, "%s", dbtmp);
166 * Keys are a mode_t followed by a dev_t. The former is the type of
167 * the file (mode & S_IFMT), the latter is the st_rdev field. Note
168 * that the structure may contain padding, so we have to clear it
169 * out here.
171 (void)memset(&bkey, 0, sizeof(bkey));
172 key.data = &bkey;
173 key.size = sizeof(bkey);
174 data.data = (u_char *)(void *)buf;
175 while ((p = fts_read(ftsp)) != NULL) {
176 switch (p->fts_info) {
177 case FTS_DEFAULT:
178 st = p->fts_statp;
179 break;
180 default:
181 continue;
184 /* Create the key. */
185 if (S_ISCHR(st->st_mode))
186 bkey.type = S_IFCHR;
187 else if (S_ISBLK(st->st_mode))
188 bkey.type = S_IFBLK;
189 else
190 continue;
191 bkey.dev = st->st_rdev;
194 * Create the data; nul terminate the name so caller doesn't
195 * have to. Skip path_dev and slash. Handle old versions
196 * of fts(3), that added multiple slashes, if the pathname
197 * ended with a slash.
199 while (p->fts_path[dlen] == '/')
200 dlen++;
201 (void)strlcpy(buf, p->fts_path + dlen, sizeof(buf));
202 data.size = p->fts_pathlen - dlen + 1;
203 if ((*db->put)(db, &key, &data, 0))
204 err(1, "dbput %s", dbtmp);
206 (void)(*db->close)(db);
207 (void)fts_close(ftsp);
209 if (chdir(cur_dir) == -1)
210 err(1, "%s", cur_dir);
211 if (rename(dbtmp, dbname) == -1)
212 err(1, "rename %s to %s", dbtmp, dbname);
213 return 0;
216 static void
217 usage(void)
220 (void)fprintf(stderr, "Usage: %s [-o database] [directory]\n",
221 getprogname());
222 exit(1);