Commit | Line | Data |
---|---|---|
41485987 | 1 | /*- |
b5fe3dec KB |
2 | * Copyright (c) 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
c9619dbd | 4 | * |
41485987 | 5 | * %sccs.include.redist.c% |
c9619dbd MT |
6 | */ |
7 | ||
8 | #ifndef lint | |
b5fe3dec KB |
9 | static char copyright[] = |
10 | "@(#) Copyright (c) 1990, 1993\n\ | |
11 | The Regents of the University of California. All rights reserved.\n"; | |
c9619dbd MT |
12 | #endif /* not lint */ |
13 | ||
c9619dbd | 14 | #ifndef lint |
b5fe3dec | 15 | static char sccsid[] = "@(#)kvm_mkdb.c 8.1 (Berkeley) %G%"; |
c9619dbd | 16 | #endif /* not lint */ |
41485987 | 17 | |
c9619dbd | 18 | #include <sys/param.h> |
01f88b32 | 19 | #include <sys/stat.h> |
6e74e80d | 20 | |
573b8d71 | 21 | #include <db.h> |
6e74e80d | 22 | #include <err.h> |
c9619dbd | 23 | #include <errno.h> |
6e74e80d KB |
24 | #include <fcntl.h> |
25 | #include <paths.h> | |
c9619dbd | 26 | #include <stdio.h> |
13e7db0b | 27 | #include <stdlib.h> |
01f88b32 | 28 | #include <string.h> |
c9619dbd | 29 | |
13e7db0b | 30 | #include "extern.h" |
c9619dbd | 31 | |
6e74e80d | 32 | static void usage __P((void)); |
13e7db0b KB |
33 | |
34 | int | |
c9619dbd | 35 | main(argc, argv) |
01f88b32 | 36 | int argc; |
6e74e80d | 37 | char *argv[]; |
c9619dbd | 38 | { |
573b8d71 | 39 | DB *db; |
c9619dbd | 40 | int ch; |
13e7db0b | 41 | char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN]; |
c9619dbd | 42 | |
c9619dbd | 43 | while ((ch = getopt(argc, argv, "")) != EOF) |
6e74e80d | 44 | switch (ch) { |
c9619dbd MT |
45 | case '?': |
46 | default: | |
01f88b32 | 47 | usage(); |
c9619dbd MT |
48 | } |
49 | argc -= optind; | |
50 | argv += optind; | |
51 | ||
9208bec5 KB |
52 | if (argc > 1) |
53 | usage(); | |
54 | ||
13e7db0b KB |
55 | /* If the existing db file matches the currently running kernel, exit */ |
56 | if (testdb()) | |
57 | exit(0); | |
58 | ||
59 | #define basename(cp) ((p = rindex((cp), '/')) != NULL ? p + 1 : (cp)) | |
8b27240c | 60 | nlistpath = argc > 0 ? argv[0] : _PATH_UNIX; |
c9619dbd | 61 | nlistname = basename(nlistpath); |
573b8d71 | 62 | |
13e7db0b KB |
63 | (void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp", |
64 | _PATH_VARDB, nlistname); | |
65 | (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", | |
66 | _PATH_VARDB, nlistname); | |
01f88b32 | 67 | (void)umask(0); |
6e74e80d KB |
68 | db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR, |
69 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, NULL); | |
70 | if (db == NULL) | |
71 | err(1, "%s", dbtemp); | |
01f88b32 | 72 | create_knlist(nlistpath, db); |
6e74e80d KB |
73 | if (db->close(db)) |
74 | err(1, "%s", dbtemp); | |
75 | if (rename(dbtemp, dbname)) | |
76 | err(1, "rename %s to %s", dbtemp, dbname); | |
01f88b32 | 77 | exit(0); |
c9619dbd MT |
78 | } |
79 | ||
13e7db0b | 80 | void |
01f88b32 | 81 | usage() |
c9619dbd | 82 | { |
01f88b32 | 83 | (void)fprintf(stderr, "usage: kvm_mkdb [file]\n"); |
c9619dbd MT |
84 | exit(1); |
85 | } |