Home | History | Annotate | Line # | Download | only in kvm_mkdb
      1 /* $NetBSD: kvm_mkdb.c,v 1.21 2018/01/23 21:06:25 sevan 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 /*-
     33  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. All advertising materials mentioning features or use of this software
     44  *    must display the following acknowledgement:
     45  *	This product includes software developed by the University of
     46  *	California, Berkeley and its contributors.
     47  * 4. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 #ifndef lint
     66 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
     67  The Regents of the University of California.  All rights reserved.");
     68 #endif /* not lint */
     69 
     70 #ifndef lint
     71 #if 0
     72 static char sccsid[] = "from: @(#)kvm_mkdb.c	8.3 (Berkeley) 5/4/95";
     73 #else
     74 __RCSID("$NetBSD: kvm_mkdb.c,v 1.21 2018/01/23 21:06:25 sevan Exp $");
     75 #endif
     76 #endif /* not lint */
     77 
     78 #include <sys/param.h>
     79 #include <sys/stat.h>
     80 
     81 #include <db.h>
     82 #include <err.h>
     83 #include <errno.h>
     84 #include <fcntl.h>
     85 #include <paths.h>
     86 #include <stdio.h>
     87 #include <stdlib.h>
     88 #include <string.h>
     89 #include <unistd.h>
     90 
     91 #include "extern.h"
     92 
     93 static	void	usage(void) __dead;
     94 
     95 HASHINFO openinfo = {
     96 	4096,		/* bsize */
     97 	128,		/* ffactor */
     98 	1024,		/* nelem */
     99 	2048 * 1024,	/* cachesize */
    100 	NULL,		/* hash() */
    101 	0		/* lorder */
    102 };
    103 
    104 static DB *db;
    105 static char *dbname = NULL;
    106 static char dbtemp[MAXPATHLEN];
    107 
    108 int
    109 main(int argc, char *argv[])
    110 {
    111 	int ch;
    112 	char *nlistpath;
    113 	int docheck = 0;
    114 	int fd;
    115 
    116 	while ((ch = getopt(argc, argv, "o:")) != -1)
    117 		switch (ch) {
    118 		case 'o':
    119 			dbname = optarg;
    120 			break;
    121 
    122 		case '?':
    123 		default:
    124 			usage();
    125 		}
    126 	argc -= optind;
    127 	argv += optind;
    128 
    129 	if (argc > 1)
    130 		usage();
    131 
    132 	if (dbname == NULL) {
    133 		dbname = _PATH_KVMDB;
    134 		docheck = 1;
    135 	} else 	if (strncmp(_PATH_KVMDB, dbname, sizeof(_PATH_KVMDB)) == 0) {
    136 		docheck = 1;
    137 	}
    138 	if (docheck) {
    139 		/*
    140 		 * If the existing db file matches the currently running
    141 		 * kernel, exit
    142 		 */
    143 		if (testdb())
    144 			exit(0);
    145 	}
    146 
    147 	if (argc <= 0) {
    148 		/*
    149 		 * Check for useability of _PATH_KSYMS, if not
    150 		 * then fallback to _PATH_UNIX.
    151 		 * Should we complain if failure?
    152 		 */
    153 		if ((fd = open(_PATH_KSYMS, O_RDONLY)) >= 0) {
    154 			close(fd);
    155 			nlistpath = _PATH_KSYMS;
    156 		} else
    157 			nlistpath = _PATH_UNIX;
    158 	} else
    159 		nlistpath = argv[0];
    160 
    161 	(void)snprintf(dbtemp, sizeof(dbtemp), "%s.tmp", dbname);
    162 	(void)umask(0);
    163 	db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
    164 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, &openinfo);
    165 	if (db == NULL)
    166 		err(1, "%s", dbtemp);
    167 	create_knlist(nlistpath, db);
    168 	if (db->close(db)) {
    169 		warn("%s", dbtemp);
    170 		db = NULL;
    171 		punt();
    172 	}
    173 	db = NULL;
    174 	if (rename(dbtemp, dbname)) {
    175 		warn("rename %s to %s", dbtemp, dbname);
    176 		punt();
    177 	}
    178 	exit(0);
    179 }
    180 
    181 static void
    182 usage(void)
    183 {
    184 	(void)fprintf(stderr, "usage: kvm_mkdb [-o database] [file]\n");
    185 	exit(1);
    186 }
    187 
    188 void
    189 punt()
    190 {
    191 
    192 	if (db != NULL)
    193 		db->close(db);
    194 	unlink(dbtemp);
    195 	exit(1);
    196 }
    197