summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1998-08-19 06:47:56 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1998-08-19 06:47:56 +0000
commitf4fc9566a4d35c0cc2bf8c7f1136287d8eec8c44 (patch)
tree3d571fd1c7c1cb87b7eb6c613b8bb14176923d38 /usr.sbin
parent644231079adf9d2b4f6a8afda11b4157f508bfbf (diff)
Make kvm_mkdb work again
o testdb() needs to take as an arg the name of the kernel so it can do the version test correctly. o add undocumented verbose flag to tell when we are rebuilding the .db file o DO_* -> _NLIST_DO_*, this prevented kvm_mkdb from doing anything at all. o preserve a few error messages so you get error output when none of the exec types match the target. o check malloc return values o get_kerntext should not subtract the sizeof(struct exec) from kernel_text
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/kvm_mkdb/extern.h5
-rw-r--r--usr.sbin/kvm_mkdb/kvm_mkdb.c29
-rw-r--r--usr.sbin/kvm_mkdb/nlist.c64
-rw-r--r--usr.sbin/kvm_mkdb/testdb.c14
4 files changed, 63 insertions, 49 deletions
diff --git a/usr.sbin/kvm_mkdb/extern.h b/usr.sbin/kvm_mkdb/extern.h
index 9a46bb5f9d4..b576ba29499 100644
--- a/usr.sbin/kvm_mkdb/extern.h
+++ b/usr.sbin/kvm_mkdb/extern.h
@@ -31,9 +31,8 @@
* SUCH DAMAGE.
*
* from: @(#)extern.h 8.1 (Berkeley) 6/6/93
- * $OpenBSD: extern.h,v 1.2 1997/01/15 22:08:15 millert Exp $
+ * $OpenBSD: extern.h,v 1.3 1998/08/19 06:47:52 millert Exp $
*/
void create_knlist __P((char *, DB *));
-void error __P((char *));
-int testdb __P(());
+int testdb __P((char *));
diff --git a/usr.sbin/kvm_mkdb/kvm_mkdb.c b/usr.sbin/kvm_mkdb/kvm_mkdb.c
index 85012947ce4..c98c3b1641f 100644
--- a/usr.sbin/kvm_mkdb/kvm_mkdb.c
+++ b/usr.sbin/kvm_mkdb/kvm_mkdb.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kvm_mkdb.c,v 1.2 1997/01/15 22:08:15 millert Exp $ */
+/* $OpenBSD: kvm_mkdb.c,v 1.3 1998/08/19 06:47:53 millert Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -41,9 +41,9 @@ static char copyright[] =
#ifndef lint
#if 0
-static char sccsid[] = "from: @(#)kvm_mkdb.c 8.1 (Berkeley) 6/6/93";
+static char sccsid[] = "from: @(#)kvm_mkdb.c 8.3 (Berkeley) 5/4/95";
#else
-static char *rcsid = "$OpenBSD: kvm_mkdb.c,v 1.2 1997/01/15 22:08:15 millert Exp $";
+static char *rcsid = "$OpenBSD: kvm_mkdb.c,v 1.3 1998/08/19 06:47:53 millert Exp $";
#endif
#endif /* not lint */
@@ -54,10 +54,12 @@ static char *rcsid = "$OpenBSD: kvm_mkdb.c,v 1.2 1997/01/15 22:08:15 millert Exp
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <libgen.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include "extern.h"
@@ -78,11 +80,14 @@ main(argc, argv)
char *argv[];
{
DB *db;
- int ch;
- char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
+ int ch, verbose = 0;
+ char *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
- while ((ch = getopt(argc, argv, "")) != -1)
+ while ((ch = getopt(argc, argv, "v")) != -1)
switch (ch) {
+ case 'v':
+ verbose = 1;
+ break;
case '?':
default:
usage();
@@ -93,11 +98,6 @@ main(argc, argv)
if (argc > 1)
usage();
- /* If the existing db file matches the currently running kernel, exit */
- if (testdb())
- exit(0);
-
-#define basename(cp) ((p = strrchr((cp), '/')) != NULL ? p + 1 : (cp))
nlistpath = argc > 0 ? argv[0] : _PATH_UNIX;
nlistname = basename(nlistpath);
@@ -105,6 +105,13 @@ main(argc, argv)
_PATH_VARDB, nlistname);
(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
_PATH_VARDB, nlistname);
+
+ /* If the existing db file matches the currently running kernel, exit */
+ if (testdb(dbname))
+ exit(0);
+ else if (verbose)
+ warnx("rebuilding %s", dbname);
+
(void)umask(0);
db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, &openinfo);
diff --git a/usr.sbin/kvm_mkdb/nlist.c b/usr.sbin/kvm_mkdb/nlist.c
index 8a477cb4460..936f72ebfe6 100644
--- a/usr.sbin/kvm_mkdb/nlist.c
+++ b/usr.sbin/kvm_mkdb/nlist.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: nlist.c,v 1.7 1997/12/15 10:15:39 deraadt Exp $ */
+/* $OpenBSD: nlist.c,v 1.8 1998/08/19 06:47:54 millert Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "from: @(#)nlist.c 8.1 (Berkeley) 6/6/93";
#else
-static char *rcsid = "$OpenBSD: nlist.c,v 1.7 1997/12/15 10:15:39 deraadt Exp $";
+static char *rcsid = "$OpenBSD: nlist.c,v 1.8 1998/08/19 06:47:54 millert Exp $";
#endif
#endif /* not lint */
@@ -61,7 +61,7 @@ static char *rcsid = "$OpenBSD: nlist.c,v 1.7 1997/12/15 10:15:39 deraadt Exp $"
#include <sys/stat.h>
#include <sys/file.h>
-#ifdef DO_ELF
+#ifdef _NLIST_DO_ELF
#include <elf_abi.h>
#endif
@@ -71,8 +71,9 @@ typedef struct nlist NLIST;
#define badfmt(str) errx(1, "%s: %s: %s", kfile, str, strerror(EFTYPE))
static char *kfile;
+static char *fmterr;
-#if defined(DO_AOUT)
+#if defined(_NLIST_DO_AOUT)
static void badread __P((int, char *));
static u_long get_kerntext __P((char *kfn, u_int magic));
@@ -93,17 +94,22 @@ __aout_knlist(name, db)
kfile = name;
if ((fd = open(name, O_RDONLY, 0)) < 0)
- err(1, "%s", name);
+ err(1, "can't open %s", name);
/* Read in exec structure. */
nr = read(fd, &ebuf, sizeof(struct exec));
- if (nr != sizeof(struct exec))
- return(-1);
+ if (nr != sizeof(struct exec)) {
+ fmterr = "no exec header";
+ return (-1);
+ }
/* Check magic number and symbol count. */
- if (N_BADMAG(ebuf))
- return(-1);
+ if (N_BADMAG(ebuf)) {
+ fmterr = "bad magic number";
+ return (-1);
+ }
+ /* Must have a symbol table. */
if (!ebuf.a_syms)
badfmt("stripped");
@@ -119,7 +125,7 @@ __aout_knlist(name, db)
/* Read in the string table. */
strsize -= sizeof(strsize);
if (!(strtab = malloc(strsize)))
- err(1, NULL);
+ errx(1, "cannot allocate memory");
if ((nr = read(fd, strtab, strsize)) != strsize)
badread(nr, "corrupted symbol table");
@@ -190,7 +196,7 @@ __aout_knlist(name, db)
}
}
(void)fclose(fp);
- return(0);
+ return (0);
}
/*
@@ -219,8 +225,6 @@ get_kerntext(name, magic)
if (nlist(name, nl) != 0)
return (KERNTEXTOFF);
- if (magic == ZMAGIC || magic == QMAGIC)
- return (nl[0].n_value - sizeof(struct exec));
return (nl[0].n_value);
}
@@ -234,9 +238,9 @@ badread(nr, p)
badfmt(p);
}
-#endif /* DO_AOUT */
+#endif /* _NLIST_DO_AOUT */
-#ifdef DO_ELF
+#ifdef _NLIST_DO_ELF
int
__elf_knlist(name, db)
char *name;
@@ -266,9 +270,11 @@ __elf_knlist(name, db)
if (fseek(fp, (off_t)0, SEEK_SET) == -1 ||
fread(&eh, sizeof(eh), 1, fp) != 1 ||
!IS_ELF(eh))
- return(-1);
+ return (-1);
sh = (Elf32_Shdr *)malloc(sizeof(Elf32_Shdr) * eh.e_shnum);
+ if (sh == NULL)
+ errx(1, "cannot allocate memory");
if (fseek (fp, eh.e_shoff, SEEK_SET) < 0)
badfmt("no exec header");
@@ -277,6 +283,8 @@ __elf_knlist(name, db)
badfmt("no exec header");
shstr = (char *)malloc(sh[eh.e_shstrndx].sh_size);
+ if (shstr == NULL)
+ errx(1, "cannot allocate memory");
if (fseek (fp, sh[eh.e_shstrndx].sh_offset, SEEK_SET) < 0)
badfmt("corrupt file");
if (fread(shstr, sh[eh.e_shstrndx].sh_size, 1, fp) != 1)
@@ -396,11 +404,11 @@ __elf_knlist(name, db)
}
munmap(strtab, symstrsize);
(void)fclose(fp);
- return(0);
+ return (0);
}
-#endif /* DO_ELF */
+#endif /* _NLIST_DO_ELF */
-#ifdef DO_ECOFF
+#ifdef _NLIST_DO_ECOFF
int
__ecoff_knlist(name, db)
char *name;
@@ -408,18 +416,18 @@ __ecoff_knlist(name, db)
{
return (-1);
}
-#endif /* DO_ECOFF */
+#endif /* _NLIST_DO_ECOFF */
static struct knlist_handlers {
int (*fn) __P((char *name, DB *db));
} nlist_fn[] = {
-#ifdef DO_AOUT
+#ifdef _NLIST_DO_AOUT
{ __aout_knlist },
#endif
-#ifdef DO_ELF
+#ifdef _NLIST_DO_ELF
{ __elf_knlist },
#endif
-#ifdef DO_ECOFF
+#ifdef _NLIST_DO_ECOFF
{ __ecoff_knlist },
#endif
};
@@ -429,11 +437,15 @@ create_knlist(name, db)
char *name;
DB *db;
{
- int n, i;
+ int i, error;
for (i = 0; i < sizeof(nlist_fn)/sizeof(nlist_fn[0]); i++) {
- n = (nlist_fn[i].fn)(name, db);
- if (n != -1)
+ fmterr = NULL;
+ if ((error = (nlist_fn[i].fn)(name, db)) == 0)
break;
}
+ if (fmterr != NULL)
+ badfmt(fmterr);
+ if (error)
+ errx(1, "cannot determine executable type of %s", name);
}
diff --git a/usr.sbin/kvm_mkdb/testdb.c b/usr.sbin/kvm_mkdb/testdb.c
index 090ed911731..6b5cd804f81 100644
--- a/usr.sbin/kvm_mkdb/testdb.c
+++ b/usr.sbin/kvm_mkdb/testdb.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: testdb.c,v 1.2 1997/01/15 22:08:17 millert Exp $ */
+/* $OpenBSD: testdb.c,v 1.3 1998/08/19 06:47:55 millert Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "from: @(#)testdb.c 8.1 (Berkeley) 6/6/93";
#else
-static char *rcsid = "$OpenBSD: testdb.c,v 1.2 1997/01/15 22:08:17 millert Exp $";
+static char *rcsid = "$OpenBSD: testdb.c,v 1.3 1998/08/19 06:47:55 millert Exp $";
#endif
#endif /* not lint */
@@ -56,14 +56,14 @@ static char *rcsid = "$OpenBSD: testdb.c,v 1.2 1997/01/15 22:08:17 millert Exp $
/* Return true if the db file is valid, else false */
int
-testdb()
+testdb(dbname)
+ char *dbname;
{
register DB *db;
register int cc, kd, ret, dbversionlen;
- register char *cp, *uf;
DBT rec;
struct nlist nitem;
- char dbname[MAXPATHLEN], dbversion[_POSIX2_LINE_MAX];
+ char dbversion[_POSIX2_LINE_MAX];
char kversion[_POSIX2_LINE_MAX];
ret = 0;
@@ -72,10 +72,6 @@ testdb()
if ((kd = open(_PATH_KMEM, O_RDONLY, 0)) < 0)
goto close;
- uf = _PATH_UNIX;
- if ((cp = strrchr(uf, '/')) != 0)
- uf = cp + 1;
- (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", _PATH_VARDB, uf);
if ((db = dbopen(dbname, O_RDONLY, 0, DB_HASH, NULL)) == NULL)
goto close;