Home | History | Annotate | Line # | Download | only in dev_mkdb
dev_mkdb.c revision 1.21
      1 /*	$NetBSD: dev_mkdb.c,v 1.21 2005/03/16 02:56:18 xtraeme 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/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) 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 #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.21 2005/03/16 02:56:18 xtraeme Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/stat.h>
     48 
     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>
     61 
     62 void	usage(void);
     63 
     64 HASHINFO openinfo = {
     65 	4096,		/* bsize */
     66 	128,		/* ffactor */
     67 	1024,		/* nelem */
     68 	2048 * 1024,	/* cachesize */
     69 	NULL,		/* hash() */
     70 	0		/* lorder */
     71 };
     72 
     73 int
     74 main(int argc, char **argv)
     75 {
     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 	u_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 
     96 	while ((ch = getopt(argc, argv, "o:")) != -1)
     97 		switch (ch) {
     98 		case 'o':
     99 			dbname_arg = optarg;
    100 			break;
    101 		case '?':
    102 		default:
    103 			usage();
    104 		}
    105 	argc -= optind;
    106 	argv += optind;
    107 
    108 	if (argc == 1)
    109 		if (strlcpy(path_dev, argv[0], sizeof(path_dev)) >= sizeof(path_dev))
    110 			errx(1, "device path too long");
    111 
    112 	if (argc > 1)
    113 		usage();
    114 
    115 	if (!getcwd(cur_dir, sizeof(cur_dir)))
    116 		err(1, "%s", cur_dir);
    117 
    118 	if (chdir(path_dev))
    119 		err(1, "%s", path_dev);
    120 
    121 	pathv[0] = path_dev;
    122 	pathv[1] = NULL;
    123 	ftsp = fts_open(pathv, FTS_PHYSICAL, NULL);
    124 	if (ftsp == NULL)
    125 		err(1, "fts_open: %s", path_dev);
    126 
    127 	if (chdir(cur_dir))
    128 		err(1, "%s", cur_dir);
    129 
    130 	if (dbname_arg) {
    131 		if (strlcpy(dbname, dbname_arg, sizeof(dbname)) >= sizeof(dbname))
    132 			errx(1, "dbname too long");
    133 	} else {
    134 		if (snprintf(dbname, sizeof(dbname), "%sdev.db", _PATH_VARRUN) >= sizeof(dbname))
    135 			errx(1, "dbname too long");
    136 	}
    137 	/*
    138 	 * We use rename() to produce the dev.db file from a temporary file,
    139 	 * and rename() is not able to move files across filesystems. Hence we
    140 	 * need the temporary file to be in the same directory as dev.db.
    141 	 *
    142 	 * Additionally, we might be working in a world writable directory,
    143 	 * we must ensure that we are not opening an existing file, therefore
    144 	 * the loop on dbopen.
    145 	 */
    146 	(void)strlcpy(dbtmp, dbname, sizeof(dbtmp));
    147 	q = dbtmp + strlen(dbtmp);
    148 	do {
    149 		(void)gettimeofday(&tv, NULL);
    150 		(void)snprintf(q, sizeof(dbtmp) - (q - dbtmp),
    151 			    "%ld.tmp", tv.tv_usec);
    152 		db = dbopen(dbtmp, O_CREAT|O_EXCL|O_EXLOCK|O_RDWR|O_TRUNC,
    153 		    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, &openinfo);
    154 	} while (!db && (errno == EEXIST));
    155 	if (db == NULL)
    156 		err(1, "%s", dbtmp);
    157 
    158 	/*
    159 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
    160 	 * the file (mode & S_IFMT), the latter is the st_rdev field.  Note
    161 	 * that the structure may contain padding, so we have to clear it
    162 	 * out here.
    163 	 */
    164 	memset(&bkey, 0, sizeof(bkey));
    165 	key.data = &bkey;
    166 	key.size = sizeof(bkey);
    167 	data.data = buf;
    168 	while ((p = fts_read(ftsp)) != NULL) {
    169 		switch (p->fts_info) {
    170 		case FTS_DEFAULT:
    171 			st = p->fts_statp;
    172 			break;
    173 		default:
    174 			continue;
    175 		}
    176 
    177 		/* Create the key. */
    178 		if (S_ISCHR(st->st_mode))
    179 			bkey.type = S_IFCHR;
    180 		else if (S_ISBLK(st->st_mode))
    181 			bkey.type = S_IFBLK;
    182 		else
    183 			continue;
    184 		bkey.dev = st->st_rdev;
    185 
    186 		/*
    187 		 * Create the data; nul terminate the name so caller doesn't
    188 		 * have to.  Skip path_dev and slash.
    189 		 */
    190 		strlcpy(buf, p->fts_path + (strlen(path_dev) + 1), sizeof(buf));
    191 		data.size = p->fts_pathlen - (strlen(path_dev) + 1) + 1;
    192 		if ((*db->put)(db, &key, &data, 0)) {
    193 			err(1, "dbput %s", dbtmp);
    194 		}
    195 	}
    196 	(void)(*db->close)(db);
    197 	fts_close(ftsp);
    198 
    199 	if (chdir(cur_dir))
    200 		err(1, "%s", cur_dir);
    201 	if (rename(dbtmp, dbname))
    202 		err(1, "rename %s to %s", dbtmp, dbname);
    203 	exit(0);
    204 }
    205 
    206 void
    207 usage(void)
    208 {
    209 
    210 	(void)fprintf(stderr, "usage: dev_mkdb [-o database] [directory]\n");
    211 	exit(1);
    212 }
    213