142800Sbostic /*-
2*59022Sbostic  * Copyright (c) 1990, 1993 The Regents of the University of California.
340358Smarc  * All rights reserved.
440358Smarc  *
542800Sbostic  * %sccs.include.redist.c%
640358Smarc  */
740358Smarc 
840358Smarc #ifndef lint
940358Smarc char copyright[] =
10*59022Sbostic "@(#) Copyright (c) 1990, 1993 The Regents of the University of California.\n\
1140358Smarc  All rights reserved.\n";
1240358Smarc #endif /* not lint */
1340358Smarc 
1440358Smarc #ifndef lint
15*59022Sbostic static char sccsid[] = "@(#)kvm_mkdb.c	5.18 (Berkeley) 04/10/93";
1640358Smarc #endif /* not lint */
1742800Sbostic 
1840358Smarc #include <sys/param.h>
1946395Sbostic #include <sys/stat.h>
20*59022Sbostic 
2146947Sbostic #include <db.h>
22*59022Sbostic #include <err.h>
2340358Smarc #include <errno.h>
24*59022Sbostic #include <fcntl.h>
25*59022Sbostic #include <paths.h>
2646395Sbostic #include <stdio.h>
2753326Sbostic #include <stdlib.h>
2842064Sbostic #include <string.h>
2940358Smarc 
3053326Sbostic #include "extern.h"
3140358Smarc 
32*59022Sbostic static void usage __P((void));
3353326Sbostic 
3453326Sbostic int
3540358Smarc main(argc, argv)
3646395Sbostic 	int argc;
37*59022Sbostic 	char *argv[];
3840358Smarc {
3946947Sbostic 	DB *db;
4040358Smarc 	int ch;
4153326Sbostic 	char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
4240358Smarc 
4340358Smarc 	while ((ch = getopt(argc, argv, "")) != EOF)
44*59022Sbostic 		switch (ch) {
4540358Smarc 		case '?':
4640358Smarc 		default:
4746395Sbostic 			usage();
4840358Smarc 		}
4940358Smarc 	argc -= optind;
5040358Smarc 	argv += optind;
5140358Smarc 
5253261Sbostic 	if (argc > 1)
5353261Sbostic 		usage();
5453261Sbostic 
5553326Sbostic 	/* If the existing db file matches the currently running kernel, exit */
5653326Sbostic 	if (testdb())
5753326Sbostic 		exit(0);
5853326Sbostic 
5953326Sbostic #define	basename(cp)	((p = rindex((cp), '/')) != NULL ? p + 1 : (cp))
6053802Sbostic 	nlistpath = argc > 0 ? argv[0] : _PATH_UNIX;
6140358Smarc 	nlistname = basename(nlistpath);
6246947Sbostic 
6353326Sbostic 	(void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
6453326Sbostic 	    _PATH_VARDB, nlistname);
6553326Sbostic 	(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
6653326Sbostic 	    _PATH_VARDB, nlistname);
6746395Sbostic 	(void)umask(0);
68*59022Sbostic 	db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
69*59022Sbostic 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, NULL);
70*59022Sbostic 	if (db == NULL)
71*59022Sbostic 		err(1, "%s", dbtemp);
7246395Sbostic 	create_knlist(nlistpath, db);
73*59022Sbostic 	if (db->close(db))
74*59022Sbostic 		err(1, "%s", dbtemp);
75*59022Sbostic 	if (rename(dbtemp, dbname))
76*59022Sbostic 		err(1, "rename %s to %s", dbtemp, dbname);
7740358Smarc 	exit(0);
7840358Smarc }
7940358Smarc 
8053326Sbostic void
8146395Sbostic usage()
8240358Smarc {
8346395Sbostic 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
8440358Smarc 	exit(1);
8540358Smarc }
86