summaryrefslogtreecommitdiff
path: root/usr.sbin/kvm_mkdb
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1999-03-24 05:25:58 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1999-03-24 05:25:58 +0000
commit90945a681e1dc56c1eb326f9fa95bf36e2d5c28d (patch)
tree9cc7f32740c4f0297164a6585745b6a1f769c984 /usr.sbin/kvm_mkdb
parent9a1ab4e5a3149672a0bfe95b3eff1a52e5bf02c7 (diff)
Only exit in nlist.c due to a memory error, otherwise just return. A return value of 1 indicates an incorrect executable type, a return value of -1 indicates some other problem. This gives us better fallback from /dev/ksyms to /bsd.
Diffstat (limited to 'usr.sbin/kvm_mkdb')
-rw-r--r--usr.sbin/kvm_mkdb/kvm_mkdb.c35
-rw-r--r--usr.sbin/kvm_mkdb/nlist.c152
2 files changed, 109 insertions, 78 deletions
diff --git a/usr.sbin/kvm_mkdb/kvm_mkdb.c b/usr.sbin/kvm_mkdb/kvm_mkdb.c
index 36555ec0f07..ddbb657d900 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.7 1999/03/24 04:51:23 millert Exp $ */
+/* $OpenBSD: kvm_mkdb.c,v 1.8 1999/03/24 05:25:55 millert Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -43,7 +43,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "from: @(#)kvm_mkdb.c 8.3 (Berkeley) 5/4/95";
#else
-static char *rcsid = "$OpenBSD: kvm_mkdb.c,v 1.7 1999/03/24 04:51:23 millert Exp $";
+static char *rcsid = "$OpenBSD: kvm_mkdb.c,v 1.8 1999/03/24 05:25:55 millert Exp $";
#endif
#endif /* not lint */
@@ -68,7 +68,7 @@ static char *rcsid = "$OpenBSD: kvm_mkdb.c,v 1.7 1999/03/24 04:51:23 millert Exp
#include "extern.h"
void usage __P((void));
-int kvm_mkdb __P((char *, char *, int));
+int kvm_mkdb __P((int, char *, char *, int));
HASHINFO openinfo = {
4096, /* bsize */
@@ -85,7 +85,7 @@ main(argc, argv)
char *argv[];
{
struct rlimit rl;
- int rval, ch, verbose = 0;
+ int fd, rval, ch, verbose = 0;
char *nlistpath, *nlistname;
/* Increase our data size to the max if we can. */
@@ -113,34 +113,35 @@ main(argc, argv)
/* If no kernel specified use _PATH_KSYMS and fall back to _PATH_UNIX */
if (argc > 0) {
nlistpath = argv[0];
- nlistname = basename(argv[0]);
- rval = kvm_mkdb(nlistpath, nlistname, verbose);
+ nlistname = basename(nlistpath);
+ if ((fd = open(nlistpath, O_RDONLY, 0)) == -1)
+ err(1, "can't open %s", nlistpath);
+ rval = kvm_mkdb(fd, nlistpath, nlistname, verbose);
} else {
nlistname = basename(_PATH_UNIX);
- nlistpath = _PATH_KSYMS;
- if ((rval = kvm_mkdb(nlistpath, nlistname, verbose)) != 0) {
- nlistpath = _PATH_UNIX;
- rval = kvm_mkdb(nlistpath, nlistname, verbose);
+ if ((fd = open((nlistpath = _PATH_KSYMS), O_RDONLY, 0)) == -1 ||
+ (rval = kvm_mkdb(fd, nlistpath, nlistname, verbose)) != 0) {
+ if (fd != -1)
+ warnx("will try again using %s instead",
+ _PATH_UNIX);
+ if ((fd = open((nlistpath = _PATH_UNIX), O_RDONLY, 0)) == -1)
+ err(1, "can't open %s", nlistpath);
+ rval = kvm_mkdb(fd, nlistpath, nlistname, verbose);
}
}
exit(rval);
}
int
-kvm_mkdb(nlistpath, nlistname, verbose)
+kvm_mkdb(fd, nlistpath, nlistname, verbose)
+ int fd;
char *nlistpath;
char *nlistname;
int verbose;
{
DB *db;
- int fd;
char dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
- if ((fd = open(nlistpath, O_RDONLY, 0)) == -1) {
- warn("can't open %s", nlistpath);
- return(1);
- }
-
(void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
_PATH_VARDB, nlistname);
(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
diff --git a/usr.sbin/kvm_mkdb/nlist.c b/usr.sbin/kvm_mkdb/nlist.c
index fe58f153e5a..3227173cdcb 100644
--- a/usr.sbin/kvm_mkdb/nlist.c
+++ b/usr.sbin/kvm_mkdb/nlist.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: nlist.c,v 1.17 1998/12/19 18:48:05 millert Exp $ */
+/* $OpenBSD: nlist.c,v 1.18 1999/03/24 05:25:57 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.17 1998/12/19 18:48:05 millert Exp $";
+static char *rcsid = "$OpenBSD: nlist.c,v 1.18 1999/03/24 05:25:57 millert Exp $";
#endif
#endif /* not lint */
@@ -74,13 +74,11 @@ typedef struct nlist NLIST;
#define _strx n_un.n_strx
#define _name n_un.n_name
-#define badfmt(str) errx(1, "%s: %s: %s", kfile, str, strerror(EFTYPE))
static char *kfile;
static char *fmterr;
#if defined(_NLIST_DO_AOUT)
-static void badread __P((int, char *));
static u_long get_kerntext __P((char *kfn, u_int magic));
int
@@ -103,40 +101,50 @@ __aout_knlist(fd, db)
nr = read(fd, &ebuf, sizeof(struct exec));
if (nr != sizeof(struct exec)) {
fmterr = "no exec header";
- return (-1);
+ return (1);
}
/* Check magic number and symbol count. */
if (N_BADMAG(ebuf)) {
fmterr = "bad magic number";
- return (-1);
+ return (1);
}
/* Must have a symbol table. */
- if (!ebuf.a_syms)
- badfmt("stripped");
+ if (!ebuf.a_syms) {
+ fmterr = "stripped";
+ return (-1);
+ }
/* Seek to string table. */
- if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
- badfmt("corrupted string table");
+ if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1) {
+ fmterr = "corrupted string table";
+ return (-1);
+ }
/* Read in the size of the string table. */
nr = read(fd, (char *)&strsize, sizeof(strsize));
- if (nr != sizeof(strsize))
- badread(nr, "no symbol table");
+ if (nr != sizeof(strsize)) {
+ fmterr = "no symbol table";
+ return (-1);
+ }
/* Read in the string table. */
strsize -= sizeof(strsize);
if (!(strtab = malloc(strsize)))
errx(1, "cannot allocate %d bytes for string table", strsize);
- if ((nr = read(fd, strtab, strsize)) != strsize)
- badread(nr, "corrupted symbol table");
+ if ((nr = read(fd, strtab, strsize)) != strsize) {
+ fmterr = "corrupted symbol table";
+ return (-1);
+ }
/* Seek to symbol table. */
if (!(fp = fdopen(fd, "r")))
err(1, "%s", kfile);
- if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
- err(1, "%s", kfile);
+ if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1) {
+ warn("fseek %s", kfile);
+ return (-1);
+ }
data.data = (u_char *)&nbuf;
data.size = sizeof(NLIST);
@@ -149,10 +157,12 @@ __aout_knlist(fd, db)
while (nsyms--) {
if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
if (feof(fp))
- badfmt("corrupted symbol table");
- err(1, "%s", kfile);
+ fmterr = "corrupted symbol table";
+ else
+ warn("%s", kfile);
+ return (-1);
}
- if (!nbuf._strx || nbuf.n_type&N_STAB)
+ if (!nbuf._strx || (nbuf.n_type & N_STAB))
continue;
/* If the symbol does not start with '_', add one */
@@ -193,16 +203,20 @@ __aout_knlist(fd, db)
else
voff += N_DATOFF(ebuf)-N_DATADDR(ebuf);
cur_off = ftell(fp);
- if (fseek(fp, voff, SEEK_SET) == -1)
- badfmt("corrupted string table");
+ if (fseek(fp, voff, SEEK_SET) == -1) {
+ fmterr = "corrupted string table";
+ return (-1);
+ }
/*
* Read version string up to, and including
* newline. This code assumes that a newline
* terminates the version line.
*/
- if (fgets(buf, sizeof(buf), fp) == NULL)
- badfmt("corrupted string table");
+ if (fgets(buf, sizeof(buf), fp) == NULL) {
+ fmterr = "corrupted string table";
+ return (-1);
+ }
} else {
/*
* No data segment and text is __LDPGSZ.
@@ -232,8 +246,10 @@ __aout_knlist(fd, db)
/* Restore to original values. */
data.data = (u_char *)&nbuf;
data.size = sizeof(NLIST);
- if (cur_off != -1 && fseek(fp, cur_off, SEEK_SET) == -1)
- badfmt("corrupted string table");
+ if (cur_off != -1 && fseek(fp, cur_off, SEEK_SET) == -1) {
+ fmterr = "corrupted string table";
+ return (-1);
+ }
}
}
(void)fclose(fp);
@@ -269,16 +285,6 @@ get_kerntext(name, magic)
return (nl[0].n_value);
}
-static void
-badread(nr, p)
- int nr;
- char *p;
-{
- if (nr < 0)
- err(1, "%s", kfile);
- badfmt(p);
-}
-
#endif /* _NLIST_DO_AOUT */
#ifdef _NLIST_DO_ELF
@@ -307,27 +313,35 @@ __elf_knlist(fd, 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 %d bytes for symbol header",
sizeof(Elf32_Shdr) * eh.e_shnum);
- if (fseek (fp, eh.e_shoff, SEEK_SET) < 0)
- badfmt("no exec header");
+ if (fseek (fp, eh.e_shoff, SEEK_SET) < 0) {
+ fmterr = "no exec header";
+ return (-1);
+ }
- if (fread(sh, sizeof(Elf32_Shdr) * eh.e_shnum, 1, fp) != 1)
- badfmt("no exec header");
+ if (fread(sh, sizeof(Elf32_Shdr) * eh.e_shnum, 1, fp) != 1) {
+ fmterr = "no exec header";
+ return (-1);
+ }
shstr = (char *)malloc(sh[eh.e_shstrndx].sh_size);
if (shstr == NULL)
errx(1, "cannot allocate %d bytes for symbol string",
sh[eh.e_shstrndx].sh_size);
- 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)
- badfmt("corrupt file");
+ if (fseek (fp, sh[eh.e_shstrndx].sh_offset, SEEK_SET) < 0) {
+ fmterr = "corrupt file";
+ return (-1);
+ }
+ if (fread(shstr, sh[eh.e_shstrndx].sh_size, 1, fp) != 1) {
+ fmterr = "corrupt file";
+ return (-1);
+ }
for (i = 0; i < eh.e_shnum; i++) {
if (strcmp (shstr + sh[i].sh_name, ".strtab") == 0) {
@@ -348,7 +362,8 @@ __elf_knlist(fd, db)
/* Check for files too large to mmap. */
/* XXX is this really possible? */
if (symstrsize > SIZE_T_MAX) {
- badfmt("corrupt file");
+ fmterr = "corrupt file";
+ return (-1);
}
/*
* Map string table into our address space. This gives us
@@ -358,11 +373,15 @@ __elf_knlist(fd, db)
*/
strtab = mmap(NULL, (size_t)symstrsize, PROT_READ,
MAP_PRIVATE|MAP_FILE, fileno(fp), symstroff);
- if (strtab == (char *)-1)
- badfmt("corrupt file");
+ if (strtab == (char *)-1) {
+ fmterr = "corrupt file";
+ return (-1);
+ }
- if (fseek(fp, symoff, SEEK_SET) == -1)
- badfmt("corrupt file");
+ if (fseek(fp, symoff, SEEK_SET) == -1) {
+ fmterr = "corrupt file";
+ return (-1);
+ }
data.data = (u_char *)&nbuf;
data.size = sizeof(NLIST);
@@ -372,8 +391,10 @@ __elf_knlist(fd, db)
symsize -= sizeof(Elf32_Sym);
if (fread((char *)&sbuf, sizeof(sbuf), 1, fp) != 1) {
if (feof(fp))
- badfmt("corrupted symbol table");
- err(1, "%s", kfile);
+ fmterr = "corrupted symbol table";
+ else
+ warn(kfile);
+ return (-1);
}
if (!sbuf.st_name)
continue;
@@ -416,16 +437,20 @@ __elf_knlist(fd, db)
*/
voff = nbuf.n_value - kernvma + kernoffs;
cur_off = ftell(fp);
- if (fseek(fp, voff, SEEK_SET) == -1)
- badfmt("corrupted string table");
+ if (fseek(fp, voff, SEEK_SET) == -1) {
+ fmterr = "corrupted string table";
+ return (-1);
+ }
/*
* Read version string up to, and including newline.
* This code assumes that a newline terminates the
* version line.
*/
- if (fgets(buf, sizeof(buf), fp) == NULL)
- badfmt("corrupted string table");
+ if (fgets(buf, sizeof(buf), fp) == NULL) {
+ fmterr = "corrupted string table";
+ return (-1);
+ }
key.data = (u_char *)VRS_KEY;
key.size = sizeof(VRS_KEY) - 1;
@@ -437,8 +462,10 @@ __elf_knlist(fd, db)
/* Restore to original values. */
data.data = (u_char *)&nbuf;
data.size = sizeof(NLIST);
- if (fseek(fp, cur_off, SEEK_SET) == -1)
- badfmt("corrupted string table");
+ if (fseek(fp, cur_off, SEEK_SET) == -1) {
+ fmterr = "corrupted string table";
+ return (-1);
+ }
}
}
munmap(strtab, symstrsize);
@@ -500,8 +527,10 @@ __ecoff_knlist(fd, db)
symhdroff = exechdrp->f.f_symptr;
symhdrsize = exechdrp->f.f_nsyms;
- if (symhdrsize == 0)
- badfmt("stripped");
+ if (symhdrsize == 0) {
+ fmterr = "stripped";
+ return (-1);
+ }
if (check(symhdroff, sizeof *symhdrp) ||
sizeof *symhdrp != symhdrsize)
@@ -601,11 +630,12 @@ create_knlist(name, fd, db)
for (i = 0; i < sizeof(nlist_fn)/sizeof(nlist_fn[0]); i++) {
fmterr = NULL;
kfile = name;
- if ((error = (nlist_fn[i].fn)(fd, db)) == 0)
+ /* rval of 1 means wrong executable type */
+ if ((error = (nlist_fn[i].fn)(fd, db)) != 1)
break;
}
if (fmterr != NULL)
- badfmt(fmterr);
+ warnx("%s: %s: %s", kfile, fmterr, strerror(EFTYPE));
return(error);
}