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