diff options
author | rgrimes <rgrimes@FreeBSD.org> | 1994-05-26 05:23:31 +0000 |
---|---|---|
committer | rgrimes <rgrimes@FreeBSD.org> | 1994-05-26 05:23:31 +0000 |
commit | 862fdf11a2ede45dec0da01ed575525d79468981 (patch) | |
tree | 7a1c88ccb8006476bd4b4a548a6ad48fbfc33a01 /usr.sbin/kvm_mkdb | |
parent | 8e1a19ddde0df113b0b26b0ea621afd61dbaf91f (diff) | |
download | FreeBSD-src-862fdf11a2ede45dec0da01ed575525d79468981.zip FreeBSD-src-862fdf11a2ede45dec0da01ed575525d79468981.tar.gz |
BSD 4.4 Lite usr.sbin Sources
Diffstat (limited to 'usr.sbin/kvm_mkdb')
-rw-r--r-- | usr.sbin/kvm_mkdb/Makefile | 7 | ||||
-rw-r--r-- | usr.sbin/kvm_mkdb/extern.h | 38 | ||||
-rw-r--r-- | usr.sbin/kvm_mkdb/kvm_mkdb.8 | 69 | ||||
-rw-r--r-- | usr.sbin/kvm_mkdb/kvm_mkdb.c | 111 | ||||
-rw-r--r-- | usr.sbin/kvm_mkdb/nlist.c | 186 | ||||
-rw-r--r-- | usr.sbin/kvm_mkdb/testdb.c | 113 |
6 files changed, 524 insertions, 0 deletions
diff --git a/usr.sbin/kvm_mkdb/Makefile b/usr.sbin/kvm_mkdb/Makefile new file mode 100644 index 0000000..ea2b883 --- /dev/null +++ b/usr.sbin/kvm_mkdb/Makefile @@ -0,0 +1,7 @@ +# @(#)Makefile 8.1 (Berkeley) 6/6/93 + +PROG= kvm_mkdb +SRCS= kvm_mkdb.c nlist.c testdb.c +MAN8= kvm_mkdb.0 + +.include <bsd.prog.mk> diff --git a/usr.sbin/kvm_mkdb/extern.h b/usr.sbin/kvm_mkdb/extern.h new file mode 100644 index 0000000..6d72268 --- /dev/null +++ b/usr.sbin/kvm_mkdb/extern.h @@ -0,0 +1,38 @@ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)extern.h 8.1 (Berkeley) 6/6/93 + */ + +void create_knlist __P((char *, DB *)); +void error __P((char *)); +int testdb __P(()); diff --git a/usr.sbin/kvm_mkdb/kvm_mkdb.8 b/usr.sbin/kvm_mkdb/kvm_mkdb.8 new file mode 100644 index 0000000..d9dcbf9 --- /dev/null +++ b/usr.sbin/kvm_mkdb/kvm_mkdb.8 @@ -0,0 +1,69 @@ +.\" Copyright (c) 1989, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)kvm_mkdb.8 8.1 (Berkeley) 6/9/93 +.\" +.Dd June 9, 1993 +.Dt KVM_MKDB 8 +.Os +.Sh NAME +.Nm kvm_mkdb +.Nd create kernel database +.Sh SYNOPSIS +.Nm kvm_mkdb +.Op file +.Sh DESCRIPTION +.Nm Kvm_mkdb +creates a database in +.Pa /var/db +containing information about the specified file. +If no file is specified, +.Pa /vmunix +is used by default. +The file is named ``kvm_filename.db'', where ``filename'' is the +name of the file read. +Various library routines consult this database. +The only information currently stored is the kernel namelist, which is +used by the +.Xr kvm_nlist 3 +function, however, in the future the database may contain other static +information about the current system. +.Sh FILES +.Bl -tag -width /var/db/kvm_vmunix.db -compact +.It Pa /vmunix +.It Pa /var/db/kvm_vmunix.db +.El +.Sh SEE ALSO +.Xr kvm_nlist 3 +.Sh HISTORY +The +.Nm kvm_mkdb +utility first appeared in 4.4BSD. diff --git a/usr.sbin/kvm_mkdb/kvm_mkdb.c b/usr.sbin/kvm_mkdb/kvm_mkdb.c new file mode 100644 index 0000000..f80b109 --- /dev/null +++ b/usr.sbin/kvm_mkdb/kvm_mkdb.c @@ -0,0 +1,111 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1990, 1993\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)kvm_mkdb.c 8.1 (Berkeley) 6/6/93"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <db.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <paths.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "extern.h" + +static void usage __P((void)); + +int +main(argc, argv) + int argc; + char *argv[]; +{ + DB *db; + int ch; + char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN]; + + while ((ch = getopt(argc, argv, "")) != EOF) + switch (ch) { + case '?': + default: + usage(); + } + argc -= optind; + argv += optind; + + if (argc > 1) + usage(); + + /* If the existing db file matches the currently running kernel, exit */ + if (testdb()) + exit(0); + +#define basename(cp) ((p = rindex((cp), '/')) != NULL ? p + 1 : (cp)) + nlistpath = argc > 0 ? argv[0] : _PATH_UNIX; + nlistname = basename(nlistpath); + + (void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp", + _PATH_VARDB, nlistname); + (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db", + _PATH_VARDB, nlistname); + (void)umask(0); + db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, NULL); + if (db == NULL) + err(1, "%s", dbtemp); + create_knlist(nlistpath, db); + if (db->close(db)) + err(1, "%s", dbtemp); + if (rename(dbtemp, dbname)) + err(1, "rename %s to %s", dbtemp, dbname); + exit(0); +} + +void +usage() +{ + (void)fprintf(stderr, "usage: kvm_mkdb [file]\n"); + exit(1); +} diff --git a/usr.sbin/kvm_mkdb/nlist.c b/usr.sbin/kvm_mkdb/nlist.c new file mode 100644 index 0000000..ad31a1a --- /dev/null +++ b/usr.sbin/kvm_mkdb/nlist.c @@ -0,0 +1,186 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/6/93"; +#endif /* not lint */ + +#include <sys/param.h> + +#include <a.out.h> +#include <db.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <kvm.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "extern.h" + +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 void badread __P((int, char *)); + +static char *kfile; + +void +create_knlist(name, db) + char *name; + DB *db; +{ + register int nsyms; + struct exec ebuf; + FILE *fp; + NLIST nbuf; + DBT data, key; + int fd, nr, strsize; + char *strtab, buf[1024]; + + kfile = name; + if ((fd = open(name, O_RDONLY, 0)) < 0) + err(1, "%s", name); + + /* Read in exec structure. */ + nr = read(fd, &ebuf, sizeof(struct exec)); + if (nr != sizeof(struct exec)) + badfmt("no exec header"); + + /* Check magic number and symbol count. */ + if (N_BADMAG(ebuf)) + badfmt("bad magic number"); + if (!ebuf.a_syms) + badfmt("stripped"); + + /* Seek to string table. */ + if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1) + badfmt("corrupted string table"); + + /* Read in the size of the symbol table. */ + nr = read(fd, (char *)&strsize, sizeof(strsize)); + if (nr != sizeof(strsize)) + badread(nr, "no symbol table"); + + /* Read in the string table. */ + strsize -= sizeof(strsize); + if (!(strtab = malloc(strsize))) + err(1, NULL); + if ((nr = read(fd, strtab, strsize)) != strsize) + badread(nr, "corrupted symbol table"); + + /* Seek to symbol table. */ + if (!(fp = fdopen(fd, "r"))) + err(1, "%s", name); + if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1) + err(1, "%s", name); + + data.data = (u_char *)&nbuf; + data.size = sizeof(NLIST); + + /* Read each symbol and enter it into the database. */ + nsyms = ebuf.a_syms / sizeof(struct nlist); + while (nsyms--) { + if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) { + if (feof(fp)) + badfmt("corrupted symbol table"); + err(1, "%s", name); + } + if (!nbuf._strx || nbuf.n_type&N_STAB) + continue; + + key.data = (u_char *)strtab + nbuf._strx - sizeof(long); + key.size = strlen((char *)key.data); + if (db->put(db, &key, &data, 0)) + err(1, "record enter"); + + if (strcmp((char *)key.data, VRS_SYM) == 0) { + long cur_off, voff; +#ifndef KERNTEXTOFF +#define KERNTEXTOFF KERNBASE +#endif + /* + * Calculate offset relative to a normal (non-kernel) + * a.out. KERNTEXTOFF is where the kernel is really + * loaded; N_TXTADDR is where a normal file is loaded. + * From there, locate file offset in text or data. + */ + voff = nbuf.n_value - KERNTEXTOFF + N_TXTADDR(ebuf); + if ((nbuf.n_type & N_TYPE) == N_TEXT) + voff += N_TXTOFF(ebuf) - N_TXTADDR(ebuf); + else + voff += N_DATOFF(ebuf) - N_DATADDR(ebuf); + cur_off = ftell(fp); + if (fseek(fp, voff, SEEK_SET) == -1) + badfmt("corrupted string table"); + + /* + * 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"); + + key.data = (u_char *)VRS_KEY; + key.size = sizeof(VRS_KEY) - 1; + data.data = (u_char *)buf; + data.size = strlen(buf); + if (db->put(db, &key, &data, 0)) + err(1, "record enter"); + + /* 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"); + } + } + (void)fclose(fp); +} + +static void +badread(nr, p) + int nr; + char *p; +{ + if (nr < 0) + err(1, "%s", kfile); + badfmt(p); +} diff --git a/usr.sbin/kvm_mkdb/testdb.c b/usr.sbin/kvm_mkdb/testdb.c new file mode 100644 index 0000000..f4f3bf0 --- /dev/null +++ b/usr.sbin/kvm_mkdb/testdb.c @@ -0,0 +1,113 @@ +/*- + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)testdb.c 8.1 (Berkeley) 6/6/93"; +#endif /* not lint */ + +#include <sys/param.h> +#include <sys/file.h> +#include <errno.h> +#include <limits.h> +#include <kvm.h> +#include <db.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <paths.h> + +#include "extern.h" + +/* Return true if the db file is valid, else false */ +int +testdb() +{ + 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 kversion[_POSIX2_LINE_MAX]; + + ret = 0; + db = NULL; + + if ((kd = open(_PATH_KMEM, O_RDONLY, 0)) < 0) + goto close; + + uf = _PATH_UNIX; + if ((cp = rindex(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; + + /* Read the version out of the database */ + rec.data = VRS_KEY; + rec.size = sizeof(VRS_KEY) - 1; + if ((db->get)(db, &rec, &rec, 0)) + goto close; + if (rec.data == 0 || rec.size > sizeof(dbversion)) + goto close; + bcopy(rec.data, dbversion, rec.size); + dbversionlen = rec.size; + + /* Read version string from kernel memory */ + rec.data = VRS_SYM; + rec.size = sizeof(VRS_SYM) - 1; + if ((db->get)(db, &rec, &rec, 0)) + goto close; + if (rec.data == 0 || rec.size != sizeof(struct nlist)) + goto close; + bcopy(rec.data, &nitem, sizeof(nitem)); + /* + * Theoretically possible for lseek to be seeking to -1. Not + * that it's something to lie awake nights about, however. + */ + errno = 0; + if (lseek(kd, (off_t)nitem.n_value, SEEK_SET) == -1 && errno != 0) + goto close; + cc = read(kd, kversion, sizeof(kversion)); + if (cc < 0 || cc != sizeof(kversion)) + goto close; + + /* If they match, we win */ + ret = bcmp(dbversion, kversion, dbversionlen) == 0; + +close: if (kd >= 0) + (void)close(kd); + if (db) + (void)(db->close)(db); + return (ret); +} |