xref: /netbsd/usr.sbin/kvm_mkdb/testdb.c (revision bf9ec67e)
1 /*	$NetBSD: testdb.c,v 1.8 2001/03/19 15:18:59 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 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 #if 0
39 static char sccsid[] = "from: @(#)testdb.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: testdb.c,v 1.8 2001/03/19 15:18:59 msaitoh Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/file.h>
47 #include <sys/sysctl.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <limits.h>
51 #include <kvm.h>
52 #include <db.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <string.h>
57 #include <paths.h>
58 
59 #include "extern.h"
60 
61 /* Return true if the db file is valid, else false */
62 int
63 testdb()
64 {
65 	DB *db;
66 	int kd, ret, dbversionlen;
67 	DBT rec;
68 	char dbversion[_POSIX2_LINE_MAX];
69 	char *kversion;
70 	int mib[2];
71 	size_t size;
72 
73 	ret = 0;
74 	db = NULL;
75 
76 	if ((kd = open(_PATH_KMEM, O_RDONLY, 0)) < 0)
77 		goto close;
78 
79 	if ((db = dbopen(_PATH_KVMDB, O_RDONLY, 0, DB_HASH, NULL)) == NULL)
80 		goto close;
81 
82 	/* Read the version out of the database */
83 	rec.data = VRS_KEY;
84 	rec.size = sizeof(VRS_KEY) - 1;
85 	if ((db->get)(db, &rec, &rec, 0))
86 		goto close;
87 	if (rec.data == 0 || rec.size == 0 || rec.size > sizeof(dbversion))
88 		goto close;
89 	memmove(dbversion, rec.data, rec.size);
90 	dbversionlen = rec.size;
91 
92 	/* Read version string from kernel memory */
93 	mib[0] = CTL_KERN;
94 	mib[1] = KERN_VERSION;
95 	if (sysctl(mib, 2, NULL, &size, NULL, 0) == -1)
96 		errx(1, "can't get size of kernel version string");
97 
98 	if ((kversion = malloc(size)) == NULL)
99 		err(1, "couldn't allocate space for buffer data");
100 
101 	if (sysctl(mib, 2, kversion, &size, NULL, 0) == -1)
102 		errx(1, "can't get kernel version string");
103 
104 	/* If they match, we win */
105 	ret = memcmp(dbversion, kversion, dbversionlen) == 0;
106 
107 close:	if (kd >= 0)
108 		(void)close(kd);
109 	if (db)
110 		(void)(db->close)(db);
111 	return (ret);
112 }
113