1996-10-01 22:21:48 +04:00
|
|
|
/* $NetBSD: savecore.c,v 1.28 1996/10/01 18:21:48 cgd Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1986, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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
|
1994-06-10 17:35:24 +04:00
|
|
|
static char copyright[] =
|
|
|
|
"@(#) Copyright (c) 1986, 1992, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-18 17:54:19 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)savecore.c 8.3 (Berkeley) 1/2/94";
|
|
|
|
#else
|
1996-10-01 22:21:48 +04:00
|
|
|
static char rcsid[] = "$NetBSD: savecore.c,v 1.28 1996/10/01 18:21:48 cgd Exp $";
|
1995-03-18 17:54:19 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
1994-06-10 17:35:24 +04:00
|
|
|
#include <sys/mount.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/syslog.h>
|
1994-06-10 17:35:24 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <dirent.h>
|
1994-04-05 02:45:08 +04:00
|
|
|
#include <errno.h>
|
1994-06-10 17:35:24 +04:00
|
|
|
#include <fcntl.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <nlist.h>
|
|
|
|
#include <paths.h>
|
1994-04-05 02:45:08 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1994-06-10 17:35:24 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <tzfile.h>
|
1994-04-05 02:45:08 +04:00
|
|
|
#include <unistd.h>
|
1996-03-16 13:29:36 +03:00
|
|
|
#include <limits.h>
|
|
|
|
#include <kvm.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-10-31 07:37:32 +03:00
|
|
|
extern FILE *zopen __P((const char *fname, const char *mode, int bits));
|
|
|
|
|
1996-03-16 13:29:36 +03:00
|
|
|
#define KREAD(kd, addr, p)\
|
|
|
|
(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
struct nlist current_nl[] = { /* Namelist for currently running system. */
|
1993-03-21 12:45:37 +03:00
|
|
|
#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" },
|
1996-03-16 13:29:36 +03:00
|
|
|
{ NULL },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
1994-06-10 17:35:24 +04:00
|
|
|
int cursyms[] = { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
|
|
|
|
int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
struct nlist dump_nl[] = { /* Name list for dumped system. */
|
|
|
|
{ "_dumpdev" }, /* Entries MUST be the same as */
|
|
|
|
{ "_dumplo" }, /* those in current_nl[]. */
|
1993-03-21 12:45:37 +03:00
|
|
|
{ "_time" },
|
|
|
|
{ "_dumpsize" },
|
|
|
|
{ "_version" },
|
|
|
|
{ "_panicstr" },
|
|
|
|
{ "_dumpmag" },
|
1996-03-16 13:29:36 +03:00
|
|
|
{ NULL },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
/* Types match kernel declarations. */
|
|
|
|
long dumplo; /* where dump starts on dumpdev */
|
|
|
|
int dumpmag; /* magic number in dump */
|
|
|
|
int dumpsize; /* amount of memory dumped */
|
|
|
|
|
1994-09-17 04:23:00 +04:00
|
|
|
char *kernel;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *dirname; /* directory to save dumps in */
|
|
|
|
char *ddname; /* name of dump device */
|
|
|
|
dev_t dumpdev; /* dump device */
|
1994-06-10 17:35:24 +04:00
|
|
|
int dumpfd; /* read/write descriptor on block dev */
|
1996-03-16 13:29:36 +03:00
|
|
|
kvm_t *kd_dump; /* kvm descriptor on block dev */
|
1993-03-21 12:45:37 +03:00
|
|
|
time_t now; /* current date */
|
1994-06-10 17:35:24 +04:00
|
|
|
char panic_mesg[1024];
|
1996-10-01 22:21:48 +04:00
|
|
|
long panicstr;
|
1994-06-10 17:35:24 +04:00
|
|
|
char vers[1024];
|
1994-04-05 02:45:08 +04:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
int clear, compress, force, verbose; /* flags */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
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));
|
|
|
|
char *rawname __P((char *s));
|
|
|
|
void save_core __P((void));
|
|
|
|
void usage __P((void));
|
|
|
|
void Write __P((int, void *, int));
|
|
|
|
|
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
1994-06-10 17:35:24 +04:00
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
openlog("savecore", LOG_PERROR, LOG_DAEMON);
|
|
|
|
|
1995-01-04 08:30:07 +03:00
|
|
|
while ((ch = getopt(argc, argv, "cdfN:vz")) != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch(ch) {
|
|
|
|
case 'c':
|
|
|
|
clear = 1;
|
|
|
|
break;
|
1994-06-10 17:35:24 +04:00
|
|
|
case 'd': /* Not documented. */
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
1994-06-10 17:35:24 +04:00
|
|
|
case 'N':
|
1994-09-17 04:23:00 +04:00
|
|
|
kernel = optarg;
|
1994-06-10 17:35:24 +04:00
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
compress = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (!clear) {
|
|
|
|
if (argc != 1 && argc != 2)
|
|
|
|
usage();
|
|
|
|
dirname = argv[0];
|
|
|
|
}
|
|
|
|
if (argc == 2)
|
1994-09-17 04:23:00 +04:00
|
|
|
kernel = argv[1];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
(void)time(&now);
|
|
|
|
kmem_setup();
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (clear) {
|
|
|
|
clear_dump();
|
|
|
|
exit(0);
|
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
|
|
|
|
if (!dump_exists() && !force)
|
|
|
|
exit(1);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
check_kmem();
|
1994-06-10 17:35:24 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (panicstr)
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg);
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_ALERT, "reboot");
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if ((!get_crashtime() || !check_space()) && !force)
|
|
|
|
exit(1);
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
save_core();
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
clear_dump();
|
|
|
|
exit(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
void
|
|
|
|
kmem_setup()
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1996-03-16 13:29:36 +03:00
|
|
|
kvm_t *kd_kern;
|
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
int i, hdrsz;
|
|
|
|
char *dump_sys;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
1994-06-10 17:35:24 +04:00
|
|
|
* 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!)
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1996-03-16 13:29:36 +03:00
|
|
|
kd_kern = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
|
|
|
|
if (kd_kern == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: kvm_openfiles: %s", _PATH_UNIX, errbuf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (kvm_nlist(kd_kern, current_nl) == -1)
|
|
|
|
syslog(LOG_ERR, "%s: kvm_nlist: %s", _PATH_UNIX,
|
|
|
|
kvm_geterr(kd_kern));
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
for (i = 0; cursyms[i] != -1; i++)
|
|
|
|
if (current_nl[cursyms[i]].n_value == 0) {
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_ERR, "%s: %s not in namelist",
|
|
|
|
_PATH_UNIX, current_nl[cursyms[i]].n_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
|
1996-03-16 13:29:36 +03:00
|
|
|
KREAD(kd_kern, current_nl[X_DUMPDEV].n_value, &dumpdev);
|
1993-12-08 19:43:16 +03:00
|
|
|
if (dumpdev == NODEV) {
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_WARNING, "no core dump (no dumpdev)");
|
|
|
|
exit(1);
|
1993-12-08 19:43:16 +03:00
|
|
|
}
|
1996-03-16 13:29:36 +03:00
|
|
|
KREAD(kd_kern, current_nl[X_DUMPLO].n_value, &dumplo);
|
1995-06-25 10:28:13 +04:00
|
|
|
dumplo *= DEV_BSIZE;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (verbose)
|
1994-06-10 17:35:24 +04:00
|
|
|
(void)printf("dumplo = %d (%d * %d)\n",
|
1995-06-25 10:28:13 +04:00
|
|
|
dumplo, dumplo / DEV_BSIZE, DEV_BSIZE);
|
1996-03-16 13:29:36 +03:00
|
|
|
KREAD(kd_kern, current_nl[X_DUMPMAG].n_value, &dumpmag);
|
|
|
|
|
|
|
|
if (kernel == NULL) {
|
|
|
|
(void)kvm_read(kd_kern, current_nl[X_VERSION].n_value,
|
|
|
|
vers, sizeof(vers));
|
|
|
|
vers[sizeof(vers) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
ddname = find_dev(dumpdev, S_IFBLK);
|
|
|
|
dumpfd = Open(ddname, O_RDWR);
|
1996-03-16 13:29:36 +03:00
|
|
|
|
|
|
|
dump_sys = kernel ? kernel : _PATH_UNIX;
|
|
|
|
|
|
|
|
kd_dump = kvm_openfiles(dump_sys, ddname, NULL, O_RDWR, errbuf);
|
|
|
|
if (kd_dump == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: kvm_openfiles: %s", dump_sys, errbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
|
1996-03-16 13:29:36 +03:00
|
|
|
if (kvm_nlist(kd_dump, dump_nl) == -1)
|
|
|
|
syslog(LOG_ERR, "%s: kvm_nlist: %s", dump_sys,
|
|
|
|
kvm_geterr(kd_dump));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
1996-03-19 00:16:05 +03:00
|
|
|
hdrsz = kvm_dump_mkheader(kd_dump, (off_t)dumplo);
|
1996-06-24 00:30:39 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If 'hdrsz' == 0, kvm_dump_mkheader() failed on the magic-number
|
|
|
|
* checks, ergo no dump is present...
|
|
|
|
*/
|
|
|
|
if (hdrsz == 0) {
|
|
|
|
syslog(LOG_WARNING, "no core dump");
|
|
|
|
exit(1);
|
|
|
|
}
|
1996-03-16 13:29:36 +03:00
|
|
|
if (hdrsz == -1) {
|
|
|
|
syslog(LOG_ERR, "%s: kvm_dump_mkheader: %s", dump_sys,
|
1996-03-19 00:16:05 +03:00
|
|
|
kvm_geterr(kd_dump));
|
1996-03-16 13:29:36 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
dumplo += hdrsz;
|
|
|
|
kvm_close(kd_kern);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
check_kmem()
|
|
|
|
{
|
1996-03-16 13:29:36 +03:00
|
|
|
register char *cp;
|
1996-10-01 22:21:48 +04:00
|
|
|
register long panicloc;
|
1994-06-10 17:35:24 +04:00
|
|
|
char core_vers[1024];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-03-16 13:29:36 +03:00
|
|
|
(void)kvm_read(kd_dump, dump_nl[X_VERSION].n_value, core_vers,
|
|
|
|
sizeof(core_vers));
|
|
|
|
core_vers[sizeof(core_vers) - 1] = '\0';
|
|
|
|
|
1994-09-17 04:23:00 +04:00
|
|
|
if (strcmp(vers, core_vers) && kernel == 0)
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"warning: %s version mismatch:\n\t%s\nand\t%s\n",
|
|
|
|
_PATH_UNIX, vers, core_vers);
|
1996-03-16 13:29:36 +03:00
|
|
|
|
|
|
|
KREAD(kd_dump, dump_nl[X_PANICSTR].n_value, &panicstr);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (panicstr) {
|
1996-03-16 13:29:36 +03:00
|
|
|
cp = panic_mesg;
|
|
|
|
panicloc = panicstr;
|
|
|
|
do {
|
|
|
|
KREAD(kd_dump, panicloc, cp);
|
|
|
|
panicloc++;
|
|
|
|
} while (*cp++ && cp < &panic_mesg[sizeof(panic_mesg)]);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dump_exists()
|
|
|
|
{
|
|
|
|
int newdumpmag;
|
|
|
|
|
1996-03-16 13:29:36 +03:00
|
|
|
KREAD(kd_dump, dump_nl[X_DUMPMAG].n_value, &newdumpmag);
|
1995-07-25 00:35:02 +04:00
|
|
|
|
|
|
|
/* Read the dump size. */
|
1996-03-16 13:29:36 +03:00
|
|
|
KREAD(kd_dump, dump_nl[X_DUMPSIZE].n_value, &dumpsize);
|
1995-07-25 00:35:02 +04:00
|
|
|
dumpsize *= getpagesize();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return zero if core dump doesn't seem to be there, and note
|
|
|
|
* it for syslog. This check and return happens after the dump size
|
|
|
|
* is read, so dumpsize is whether or not the core is valid (for -f).
|
|
|
|
*/
|
1994-06-10 17:35:24 +04:00
|
|
|
if (newdumpmag != dumpmag) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (verbose)
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_WARNING, "magic number mismatch (%x != %x)",
|
|
|
|
newdumpmag, dumpmag);
|
|
|
|
syslog(LOG_WARNING, "no core dump");
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
1996-03-16 13:29:36 +03:00
|
|
|
void
|
|
|
|
clear_dump()
|
|
|
|
{
|
|
|
|
if (kvm_dump_inval(kd_dump) == -1)
|
|
|
|
syslog(LOG_ERR, "%s: kvm_clear_dump: %s", ddname,
|
|
|
|
kvm_geterr(kd_dump));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
char buf[1024 * 1024];
|
|
|
|
|
|
|
|
void
|
|
|
|
save_core()
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-10 17:35:24 +04:00
|
|
|
register FILE *fp;
|
|
|
|
register int bounds, ifd, nr, nw, ofd;
|
|
|
|
char *rawp, path[MAXPATHLEN];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
/*
|
|
|
|
* Get the current number and update the bounds file. Do the update
|
|
|
|
* now, because may fail later and don't want to overwrite anything.
|
|
|
|
*/
|
|
|
|
(void)snprintf(path, sizeof(path), "%s/bounds", dirname);
|
|
|
|
if ((fp = fopen(path, "r")) == NULL)
|
|
|
|
goto err1;
|
|
|
|
if (fgets(buf, sizeof(buf), fp) == NULL) {
|
|
|
|
if (ferror(fp))
|
|
|
|
err1: syslog(LOG_WARNING, "%s: %s", path, strerror(errno));
|
|
|
|
bounds = 0;
|
|
|
|
} else
|
|
|
|
bounds = atoi(buf);
|
|
|
|
if (fp != NULL)
|
|
|
|
(void)fclose(fp);
|
|
|
|
if ((fp = fopen(path, "w")) == NULL)
|
|
|
|
syslog(LOG_ERR, "%s: %m", path);
|
|
|
|
else {
|
|
|
|
(void)fprintf(fp, "%d\n", bounds + 1);
|
|
|
|
(void)fclose(fp);
|
|
|
|
}
|
|
|
|
(void)fclose(fp);
|
|
|
|
|
|
|
|
/* Create the core file. */
|
1996-03-08 11:52:12 +03:00
|
|
|
(void)snprintf(path, sizeof(path), "%s/netbsd.%d.core%s",
|
1994-06-10 17:35:24 +04:00
|
|
|
dirname, bounds, compress ? ".Z" : "");
|
|
|
|
if (compress) {
|
|
|
|
if ((fp = zopen(path, "w", 0)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: %s", path, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
1996-03-16 13:29:36 +03:00
|
|
|
} else {
|
1994-06-10 17:35:24 +04:00
|
|
|
ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
1996-03-16 13:29:36 +03:00
|
|
|
fp = fdopen(ofd, "w");
|
|
|
|
if (fp == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: fdopen: %s", path);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
|
|
|
|
/* Open the raw device. */
|
|
|
|
rawp = rawname(ddname);
|
|
|
|
if ((ifd = open(rawp, O_RDONLY)) == -1) {
|
|
|
|
syslog(LOG_WARNING, "%s: %m; using block device", rawp);
|
|
|
|
ifd = dumpfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Seek to the start of the core. */
|
|
|
|
Lseek(ifd, (off_t)dumplo, L_SET);
|
|
|
|
|
1996-03-16 13:29:36 +03:00
|
|
|
if (kvm_dump_wrtheader(kd_dump, fp, dumpsize) == -1) {
|
|
|
|
syslog(LOG_ERR, "kvm_dump_wrtheader: %s : %s", path,
|
|
|
|
kvm_geterr(kd_dump));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
/* Copy the core file. */
|
|
|
|
syslog(LOG_NOTICE, "writing %score to %s",
|
|
|
|
compress ? "compressed " : "", path);
|
|
|
|
for (; dumpsize > 0; dumpsize -= nr) {
|
|
|
|
(void)printf("%6dK\r", dumpsize / 1024);
|
|
|
|
(void)fflush(stdout);
|
|
|
|
nr = read(ifd, buf, MIN(dumpsize, sizeof(buf)));
|
|
|
|
if (nr <= 0) {
|
|
|
|
if (nr == 0)
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"WARNING: EOF on dump device");
|
|
|
|
else
|
|
|
|
syslog(LOG_ERR, "%s: %m", rawp);
|
|
|
|
goto err2;
|
|
|
|
}
|
1996-03-16 13:29:36 +03:00
|
|
|
nw = fwrite(buf, 1, nr, fp);
|
1994-06-10 17:35:24 +04:00
|
|
|
if (nw != nr) {
|
|
|
|
syslog(LOG_ERR, "%s: %s",
|
|
|
|
path, strerror(nw == 0 ? EIO : errno));
|
|
|
|
err2: syslog(LOG_WARNING,
|
1994-09-17 04:23:00 +04:00
|
|
|
"WARNING: core may be incomplete");
|
1994-06-10 17:35:24 +04:00
|
|
|
(void)printf("\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)close(ifd);
|
1996-03-16 13:29:36 +03:00
|
|
|
(void)fclose(fp);
|
1994-06-10 17:35:24 +04:00
|
|
|
|
|
|
|
/* Copy the kernel. */
|
1994-09-17 04:23:00 +04:00
|
|
|
ifd = Open(kernel ? kernel : _PATH_UNIX, O_RDONLY);
|
1994-06-10 17:35:24 +04:00
|
|
|
(void)snprintf(path, sizeof(path), "%s/netbsd.%d%s",
|
|
|
|
dirname, bounds, compress ? ".Z" : "");
|
|
|
|
if (compress) {
|
|
|
|
if ((fp = zopen(path, "w", 0)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: %s", path, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
|
|
|
syslog(LOG_NOTICE, "writing %skernel to %s",
|
|
|
|
compress ? "compressed " : "", path);
|
|
|
|
while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
|
|
|
|
if (compress)
|
|
|
|
nw = fwrite(buf, 1, nr, fp);
|
|
|
|
else
|
|
|
|
nw = write(ofd, buf, nr);
|
|
|
|
if (nw != nr) {
|
|
|
|
syslog(LOG_ERR, "%s: %s",
|
|
|
|
path, strerror(nw == 0 ? EIO : errno));
|
|
|
|
syslog(LOG_WARNING,
|
1994-09-17 04:23:00 +04:00
|
|
|
"WARNING: kernel may be incomplete");
|
1994-06-10 17:35:24 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nr < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %s",
|
1994-09-17 04:23:00 +04:00
|
|
|
kernel ? kernel : _PATH_UNIX, strerror(errno));
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_WARNING,
|
1994-09-17 04:23:00 +04:00
|
|
|
"WARNING: kernel may be incomplete");
|
1994-06-10 17:35:24 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (compress)
|
|
|
|
(void)fclose(fp);
|
|
|
|
else
|
|
|
|
(void)close(ofd);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
char *
|
|
|
|
find_dev(dev, type)
|
|
|
|
register dev_t dev;
|
|
|
|
register int type;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-10 17:35:24 +04:00
|
|
|
register DIR *dfd;
|
|
|
|
struct dirent *dir;
|
|
|
|
struct stat sb;
|
|
|
|
char *dp, devname[MAXPATHLEN + 1];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
if ((dfd = opendir(_PATH_DEV)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: %s", _PATH_DEV, strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
(void)strcpy(devname, _PATH_DEV);
|
|
|
|
while ((dir = readdir(dfd))) {
|
|
|
|
(void)strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name);
|
|
|
|
if (lstat(devname, &sb)) {
|
|
|
|
syslog(LOG_ERR, "%s: %s", devname, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((sb.st_mode & S_IFMT) != type)
|
|
|
|
continue;
|
|
|
|
if (dev == sb.st_rdev) {
|
|
|
|
closedir(dfd);
|
|
|
|
if ((dp = strdup(devname)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return (dp);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
closedir(dfd);
|
|
|
|
syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev));
|
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
char *
|
|
|
|
rawname(s)
|
|
|
|
char *s;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-10 17:35:24 +04:00
|
|
|
char *sl, name[MAXPATHLEN];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-09-23 18:26:58 +04:00
|
|
|
if ((sl = strrchr(s, '/')) == NULL || sl[1] == '0') {
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_ERR,
|
|
|
|
"can't make raw dump device name from %s", s);
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
(void)snprintf(name, sizeof(name), "%.*s/r%s", sl - s, s, sl + 1);
|
|
|
|
if ((sl = strdup(name)) == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return (sl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_crashtime()
|
|
|
|
{
|
|
|
|
time_t dumptime; /* Time the dump was taken. */
|
|
|
|
|
1996-03-16 13:29:36 +03:00
|
|
|
KREAD(kd_dump, dump_nl[X_TIME].n_value, &dumptime);
|
1994-06-10 17:35:24 +04:00
|
|
|
if (dumptime == 0) {
|
|
|
|
if (verbose)
|
|
|
|
syslog(LOG_ERR, "dump time is zero");
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
1994-06-10 17:35:24 +04:00
|
|
|
}
|
|
|
|
(void)printf("savecore: system went down at %s", ctime(&dumptime));
|
|
|
|
#define LEEWAY (7 * SECSPERDAY)
|
|
|
|
if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
|
|
|
|
(void)printf("dump time is unreasonable\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
return (0);
|
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
return (1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
int
|
|
|
|
check_space()
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
register FILE *fp;
|
1994-09-17 04:23:00 +04:00
|
|
|
char *tkernel;
|
|
|
|
off_t minfree, spacefree, kernelsize, needed;
|
1994-06-10 17:35:24 +04:00
|
|
|
struct stat st;
|
|
|
|
struct statfs fsbuf;
|
|
|
|
char buf[100], path[MAXPATHLEN];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-09-17 04:23:00 +04:00
|
|
|
tkernel = kernel ? kernel : _PATH_UNIX;
|
|
|
|
if (stat(tkernel, &st) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", tkernel);
|
1994-06-10 17:35:24 +04:00
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-09-17 04:23:00 +04:00
|
|
|
kernelsize = st.st_blocks * S_BLKSIZE;
|
1994-06-10 17:35:24 +04:00
|
|
|
if (statfs(dirname, &fsbuf) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", dirname);
|
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
spacefree = (fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
|
|
|
|
|
|
|
|
(void)snprintf(path, sizeof(path), "%s/minfree", dirname);
|
|
|
|
if ((fp = fopen(path, "r")) == NULL)
|
|
|
|
minfree = 0;
|
|
|
|
else {
|
|
|
|
if (fgets(buf, sizeof(buf), fp) == NULL)
|
|
|
|
minfree = 0;
|
|
|
|
else
|
|
|
|
minfree = atoi(buf);
|
|
|
|
(void)fclose(fp);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-06-10 17:35:24 +04:00
|
|
|
|
1994-09-17 04:23:00 +04:00
|
|
|
needed = (dumpsize + kernelsize) / 1024;
|
1994-06-10 17:35:24 +04:00
|
|
|
if (minfree > 0 && spacefree - needed < minfree) {
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"no dump, not enough free space on device");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (spacefree - needed < minfree)
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"dump performed, but free space threshold crossed");
|
|
|
|
return (1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
Open(name, rw)
|
|
|
|
char *name;
|
|
|
|
int rw;
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
if ((fd = open(name, rw, 0)) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", name);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
Lseek(fd, off, flag)
|
|
|
|
int fd, flag;
|
1994-06-10 17:35:24 +04:00
|
|
|
off_t off;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-10 17:35:24 +04:00
|
|
|
off_t ret;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
ret = lseek(fd, off, flag);
|
|
|
|
if (ret == -1) {
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_ERR, "lseek: %m");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
Create(file, mode)
|
|
|
|
char *file;
|
|
|
|
int mode;
|
|
|
|
{
|
|
|
|
register int fd;
|
|
|
|
|
1994-09-23 18:26:58 +04:00
|
|
|
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, mode);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (fd < 0) {
|
1994-06-10 17:35:24 +04:00
|
|
|
syslog(LOG_ERR, "%s: %m", file);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
void
|
|
|
|
Write(fd, bp, size)
|
1993-03-21 12:45:37 +03:00
|
|
|
int fd, size;
|
1994-06-10 17:35:24 +04:00
|
|
|
void *bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
if ((n = write(fd, bp, size)) < size) {
|
|
|
|
syslog(LOG_ERR, "write: %s", strerror(n == -1 ? errno : EIO));
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-06-10 17:35:24 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
usage()
|
|
|
|
{
|
1994-06-10 17:35:24 +04:00
|
|
|
(void)syslog(LOG_ERR, "usage: savecore [-cfvz] [-N system] directory");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|