📄 savecore.c
字号:
/*- * Copyright (c) 1986, 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 lintstatic char copyright[] ="@(#) Copyright (c) 1986, 1992, 1993\n\ The Regents of the University of California. All rights reserved.\n";#endif /* not lint */#ifndef lintstatic char sccsid[] = "@(#)savecore.c 8.3 (Berkeley) 1/2/94";#endif /* not lint */#include <sys/param.h>#include <sys/stat.h>#include <sys/mount.h>#include <sys/syslog.h>#include <sys/time.h>#include <dirent.h>#include <errno.h>#include <fcntl.h>#include <nlist.h>#include <paths.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <tzfile.h>#include <unistd.h>#define ok(number) ((number) - KERNBASE)struct nlist current_nl[] = { /* Namelist for currently running system. */#define X_DUMPDEV 0 { "_dumpdev" },#define X_DUMPLO 1 { "_dumplo" },#define X_TIME 2 { "_time" },#define X_DUMPSIZE 3 { "_dumpsize" },#define X_VERSION 4 { "_version" },#define X_PANICSTR 5 { "_panicstr" },#define X_DUMPMAG 6 { "_dumpmag" }, { "" },};int cursyms[] = { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };struct nlist dump_nl[] = { /* Name list for dumped system. */ { "_dumpdev" }, /* Entries MUST be the same as */ { "_dumplo" }, /* those in current_nl[]. */ { "_time" }, { "_dumpsize" }, { "_version" }, { "_panicstr" }, { "_dumpmag" }, { "" },};/* Types match kernel declarations. */long dumplo; /* where dump starts on dumpdev */int dumpmag; /* magic number in dump */int dumpsize; /* amount of memory dumped */char *vmunix;char *dirname; /* directory to save dumps in */char *ddname; /* name of dump device */dev_t dumpdev; /* dump device */int dumpfd; /* read/write descriptor on block dev */time_t now; /* current date */char panic_mesg[1024];int panicstr;char vers[1024];int clear, compress, force, verbose; /* flags */void check_kmem __P((void));int check_space __P((void));void clear_dump __P((void));int Create __P((char *, int));int dump_exists __P((void));char *find_dev __P((dev_t, int));int get_crashtime __P((void));void kmem_setup __P((void));void log __P((int, char *, ...));void Lseek __P((int, off_t, int));int Open __P((char *, int rw));int Read __P((int, void *, int));char *rawname __P((char *s));void save_core __P((void));void usage __P((void));void Write __P((int, void *, int));intmain(argc, argv) int argc; char *argv[];{ int ch; openlog("savecore", LOG_PERROR, LOG_DAEMON); while ((ch = getopt(argc, argv, "cdfNvz")) != EOF) switch(ch) { case 'c': clear = 1; break; case 'd': /* Not documented. */ case 'v': verbose = 1; break; case 'f': force = 1; break; case 'N': vmunix = optarg; break; case 'z': compress = 1; break; case '?': default: usage(); } argc -= optind; argv += optind; if (!clear) { if (argc != 1 && argc != 2) usage(); dirname = argv[0]; } if (argc == 2) vmunix = argv[1]; (void)time(&now); kmem_setup(); if (clear) { clear_dump(); exit(0); } if (!dump_exists() && !force) exit(1); check_kmem(); if (panicstr) syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg); else syslog(LOG_ALERT, "reboot"); if ((!get_crashtime() || !check_space()) && !force) exit(1); save_core(); clear_dump(); exit(0);}voidkmem_setup(){ FILE *fp; int kmem, i; char *dump_sys; /* * Some names we need for the currently running system, others for * the system that was running when the dump was made. The values * obtained from the current system are used to look for things in * /dev/kmem that cannot be found in the dump_sys namelist, but are * presumed to be the same (since the disk partitions are probably * the same!) */ if ((nlist(_PATH_UNIX, current_nl)) == -1) syslog(LOG_ERR, "%s: nlist: %s", _PATH_UNIX, strerror(errno)); for (i = 0; cursyms[i] != -1; i++) if (current_nl[cursyms[i]].n_value == 0) { syslog(LOG_ERR, "%s: %s not in namelist", _PATH_UNIX, current_nl[cursyms[i]].n_name); exit(1); } dump_sys = vmunix ? vmunix : _PATH_UNIX; if ((nlist(dump_sys, dump_nl)) == -1) syslog(LOG_ERR, "%s: nlist: %s", dump_sys, strerror(errno)); for (i = 0; dumpsyms[i] != -1; i++) if (dump_nl[dumpsyms[i]].n_value == 0) { syslog(LOG_ERR, "%s: %s not in namelist", dump_sys, dump_nl[dumpsyms[i]].n_name); exit(1); } kmem = Open(_PATH_KMEM, O_RDONLY); Lseek(kmem, (off_t)current_nl[X_DUMPDEV].n_value, L_SET); (void)Read(kmem, &dumpdev, sizeof(dumpdev)); if (dumpdev == NODEV) { syslog(LOG_WARNING, "no core dump (no dumpdev)"); exit(1); } Lseek(kmem, (off_t)current_nl[X_DUMPLO].n_value, L_SET); (void)Read(kmem, &dumplo, sizeof(dumplo)); if (verbose) (void)printf("dumplo = %d (%d * %d)\n", dumplo, dumplo/DEV_BSIZE, DEV_BSIZE); Lseek(kmem, (off_t)current_nl[X_DUMPMAG].n_value, L_SET); (void)Read(kmem, &dumpmag, sizeof(dumpmag)); dumplo *= DEV_BSIZE; ddname = find_dev(dumpdev, S_IFBLK); dumpfd = Open(ddname, O_RDWR); fp = fdopen(kmem, "r"); if (fp == NULL) { syslog(LOG_ERR, "%s: fdopen: %m", _PATH_KMEM); exit(1); } if (vmunix) return; (void)fseek(fp, (off_t)current_nl[X_VERSION].n_value, L_SET); (void)fgets(vers, sizeof(vers), fp); /* Don't fclose(fp), we use dumpfd later. */}voidcheck_kmem(){ register char *cp; FILE *fp; char core_vers[1024]; fp = fdopen(dumpfd, "r"); if (fp == NULL) { syslog(LOG_ERR, "%s: fdopen: %m", ddname); exit(1); } fseek(fp, (off_t)(dumplo + ok(dump_nl[X_VERSION].n_value)), L_SET); fgets(core_vers, sizeof(core_vers), fp); if (strcmp(vers, core_vers) && vmunix == 0) syslog(LOG_WARNING, "warning: %s version mismatch:\n\t%s\nand\t%s\n", _PATH_UNIX, vers, core_vers); (void)fseek(fp, (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET); (void)fread(&panicstr, sizeof(panicstr), 1, fp); if (panicstr) { (void)fseek(fp, dumplo + ok(panicstr), L_SET); cp = panic_mesg; do *cp = getc(fp); while (*cp++ && cp < &panic_mesg[sizeof(panic_mesg)]); } /* Don't fclose(fp), we use dumpfd later. */}voidclear_dump(){ long newdumplo; newdumplo = 0; Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET); Write(dumpfd, &newdumplo, sizeof(newdumplo));}intdump_exists(){ int newdumpmag; Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET); (void)Read(dumpfd, &newdumpmag, sizeof(newdumpmag)); if (newdumpmag != dumpmag) { if (verbose) syslog(LOG_WARNING, "magic number mismatch (%x != %x)", newdumpmag, dumpmag); syslog(LOG_WARNING, "no core dump"); return (0); } return (1);}char buf[1024 * 1024];voidsave_core(){ register FILE *fp; register int bounds, ifd, nr, nw, ofd;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -