Home | History | Annotate | Line # | Download | only in savecore
savecore.c revision 1.27
      1  1.27      leo /*	$NetBSD: savecore.c,v 1.27 1996/06/23 20:30:39 leo Exp $	*/
      2  1.21      cgd 
      3  1.13       pk /*-
      4  1.13       pk  * Copyright (c) 1986, 1992, 1993
      5  1.13       pk  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  */
     35   1.1      cgd 
     36   1.1      cgd #ifndef lint
     37  1.13       pk static char copyright[] =
     38  1.13       pk "@(#) Copyright (c) 1986, 1992, 1993\n\
     39  1.13       pk 	The Regents of the University of California.  All rights reserved.\n";
     40   1.1      cgd #endif /* not lint */
     41   1.1      cgd 
     42   1.1      cgd #ifndef lint
     43  1.21      cgd #if 0
     44  1.21      cgd static char sccsid[] = "@(#)savecore.c	8.3 (Berkeley) 1/2/94";
     45  1.21      cgd #else
     46  1.27      leo static char rcsid[] = "$NetBSD: savecore.c,v 1.27 1996/06/23 20:30:39 leo Exp $";
     47  1.21      cgd #endif
     48   1.1      cgd #endif /* not lint */
     49   1.1      cgd 
     50   1.1      cgd #include <sys/param.h>
     51  1.13       pk #include <sys/stat.h>
     52   1.1      cgd #include <sys/mount.h>
     53  1.13       pk #include <sys/syslog.h>
     54   1.1      cgd #include <sys/time.h>
     55  1.13       pk 
     56   1.1      cgd #include <dirent.h>
     57   1.7      cgd #include <errno.h>
     58  1.13       pk #include <fcntl.h>
     59   1.1      cgd #include <nlist.h>
     60   1.1      cgd #include <paths.h>
     61   1.7      cgd #include <stdio.h>
     62   1.7      cgd #include <stdlib.h>
     63  1.13       pk #include <string.h>
     64  1.13       pk #include <tzfile.h>
     65   1.7      cgd #include <unistd.h>
     66  1.25      leo #include <limits.h>
     67  1.25      leo #include <kvm.h>
     68  1.18      cgd 
     69  1.18      cgd extern FILE *zopen __P((const char *fname, const char *mode, int bits));
     70   1.1      cgd 
     71  1.25      leo #define KREAD(kd, addr, p)\
     72  1.25      leo 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
     73   1.1      cgd 
     74  1.13       pk struct nlist current_nl[] = {	/* Namelist for currently running system. */
     75   1.1      cgd #define X_DUMPDEV	0
     76   1.1      cgd 	{ "_dumpdev" },
     77   1.1      cgd #define X_DUMPLO	1
     78   1.1      cgd 	{ "_dumplo" },
     79   1.1      cgd #define X_TIME		2
     80   1.1      cgd 	{ "_time" },
     81   1.1      cgd #define	X_DUMPSIZE	3
     82   1.1      cgd 	{ "_dumpsize" },
     83   1.1      cgd #define X_VERSION	4
     84   1.1      cgd 	{ "_version" },
     85   1.1      cgd #define X_PANICSTR	5
     86   1.1      cgd 	{ "_panicstr" },
     87   1.1      cgd #define	X_DUMPMAG	6
     88   1.1      cgd 	{ "_dumpmag" },
     89  1.25      leo 	{ NULL },
     90   1.1      cgd };
     91  1.13       pk int cursyms[] = { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
     92  1.13       pk int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
     93   1.1      cgd 
     94  1.13       pk struct nlist dump_nl[] = {	/* Name list for dumped system. */
     95  1.13       pk 	{ "_dumpdev" },		/* Entries MUST be the same as */
     96  1.13       pk 	{ "_dumplo" },		/*	those in current_nl[].  */
     97   1.1      cgd 	{ "_time" },
     98   1.1      cgd 	{ "_dumpsize" },
     99   1.1      cgd 	{ "_version" },
    100   1.1      cgd 	{ "_panicstr" },
    101   1.1      cgd 	{ "_dumpmag" },
    102  1.25      leo 	{ NULL },
    103   1.1      cgd };
    104   1.1      cgd 
    105  1.13       pk /* Types match kernel declarations. */
    106  1.13       pk long	dumplo;				/* where dump starts on dumpdev */
    107  1.13       pk int	dumpmag;			/* magic number in dump */
    108  1.13       pk int	dumpsize;			/* amount of memory dumped */
    109  1.13       pk 
    110  1.15  mycroft char	*kernel;
    111   1.1      cgd char	*dirname;			/* directory to save dumps in */
    112   1.1      cgd char	*ddname;			/* name of dump device */
    113  1.13       pk dev_t	dumpdev;			/* dump device */
    114   1.1      cgd int	dumpfd;				/* read/write descriptor on block dev */
    115  1.25      leo kvm_t	*kd_dump;			/* kvm descriptor on block dev	*/
    116   1.1      cgd time_t	now;				/* current date */
    117  1.13       pk char	panic_mesg[1024];
    118   1.1      cgd int	panicstr;
    119  1.13       pk char	vers[1024];
    120   1.7      cgd 
    121  1.13       pk int	clear, compress, force, verbose;	/* flags */
    122  1.13       pk 
    123  1.13       pk void	 check_kmem __P((void));
    124  1.13       pk int	 check_space __P((void));
    125  1.13       pk void	 clear_dump __P((void));
    126  1.13       pk int	 Create __P((char *, int));
    127  1.13       pk int	 dump_exists __P((void));
    128   1.7      cgd char	*find_dev __P((dev_t, int));
    129  1.13       pk int	 get_crashtime __P((void));
    130  1.13       pk void	 kmem_setup __P((void));
    131  1.13       pk void	 log __P((int, char *, ...));
    132  1.13       pk void	 Lseek __P((int, off_t, int));
    133  1.13       pk int	 Open __P((char *, int rw));
    134  1.13       pk char	*rawname __P((char *s));
    135  1.13       pk void	 save_core __P((void));
    136  1.13       pk void	 usage __P((void));
    137  1.13       pk void	 Write __P((int, void *, int));
    138   1.1      cgd 
    139  1.13       pk int
    140   1.1      cgd main(argc, argv)
    141   1.1      cgd 	int argc;
    142  1.13       pk 	char *argv[];
    143   1.1      cgd {
    144   1.1      cgd 	int ch;
    145   1.1      cgd 
    146  1.13       pk 	openlog("savecore", LOG_PERROR, LOG_DAEMON);
    147  1.13       pk 
    148  1.20  mycroft 	while ((ch = getopt(argc, argv, "cdfN:vz")) != -1)
    149   1.1      cgd 		switch(ch) {
    150   1.1      cgd 		case 'c':
    151   1.1      cgd 			clear = 1;
    152   1.1      cgd 			break;
    153  1.13       pk 		case 'd':		/* Not documented. */
    154   1.1      cgd 		case 'v':
    155   1.1      cgd 			verbose = 1;
    156   1.1      cgd 			break;
    157   1.1      cgd 		case 'f':
    158   1.1      cgd 			force = 1;
    159   1.1      cgd 			break;
    160  1.13       pk 		case 'N':
    161  1.15  mycroft 			kernel = optarg;
    162  1.13       pk 			break;
    163  1.13       pk 		case 'z':
    164  1.13       pk 			compress = 1;
    165  1.13       pk 			break;
    166   1.1      cgd 		case '?':
    167   1.1      cgd 		default:
    168   1.1      cgd 			usage();
    169   1.1      cgd 		}
    170   1.1      cgd 	argc -= optind;
    171   1.1      cgd 	argv += optind;
    172   1.1      cgd 
    173   1.1      cgd 	if (!clear) {
    174   1.1      cgd 		if (argc != 1 && argc != 2)
    175   1.1      cgd 			usage();
    176   1.1      cgd 		dirname = argv[0];
    177   1.1      cgd 	}
    178   1.1      cgd 	if (argc == 2)
    179  1.15  mycroft 		kernel = argv[1];
    180   1.1      cgd 
    181  1.13       pk 	(void)time(&now);
    182  1.13       pk 	kmem_setup();
    183   1.1      cgd 
    184   1.1      cgd 	if (clear) {
    185   1.1      cgd 		clear_dump();
    186   1.1      cgd 		exit(0);
    187   1.1      cgd 	}
    188  1.13       pk 
    189  1.13       pk 	if (!dump_exists() && !force)
    190  1.13       pk 		exit(1);
    191  1.13       pk 
    192   1.1      cgd 	check_kmem();
    193  1.13       pk 
    194   1.1      cgd 	if (panicstr)
    195  1.13       pk 		syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg);
    196   1.1      cgd 	else
    197  1.13       pk 		syslog(LOG_ALERT, "reboot");
    198   1.1      cgd 
    199   1.1      cgd 	if ((!get_crashtime() || !check_space()) && !force)
    200   1.1      cgd 		exit(1);
    201   1.1      cgd 
    202  1.13       pk 	save_core();
    203   1.1      cgd 
    204  1.13       pk 	clear_dump();
    205  1.13       pk 	exit(0);
    206   1.1      cgd }
    207   1.1      cgd 
    208  1.13       pk void
    209  1.13       pk kmem_setup()
    210   1.1      cgd {
    211  1.25      leo 	kvm_t	*kd_kern;
    212  1.25      leo 	char	errbuf[_POSIX2_LINE_MAX];
    213  1.25      leo 	int	i, hdrsz;
    214  1.25      leo 	char	*dump_sys;
    215   1.1      cgd 
    216   1.1      cgd 	/*
    217  1.13       pk 	 * Some names we need for the currently running system, others for
    218  1.13       pk 	 * the system that was running when the dump was made.  The values
    219  1.13       pk 	 * obtained from the current system are used to look for things in
    220  1.13       pk 	 * /dev/kmem that cannot be found in the dump_sys namelist, but are
    221  1.13       pk 	 * presumed to be the same (since the disk partitions are probably
    222  1.13       pk 	 * the same!)
    223   1.1      cgd 	 */
    224  1.25      leo 	kd_kern = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
    225  1.25      leo 	if (kd_kern == NULL) {
    226  1.25      leo 		syslog(LOG_ERR, "%s: kvm_openfiles: %s", _PATH_UNIX, errbuf);
    227  1.25      leo 		exit(1);
    228  1.25      leo 	}
    229  1.25      leo 	if (kvm_nlist(kd_kern, current_nl) == -1)
    230  1.25      leo 		syslog(LOG_ERR, "%s: kvm_nlist: %s", _PATH_UNIX,
    231  1.25      leo 			kvm_geterr(kd_kern));
    232  1.25      leo 
    233   1.1      cgd 	for (i = 0; cursyms[i] != -1; i++)
    234   1.1      cgd 		if (current_nl[cursyms[i]].n_value == 0) {
    235  1.13       pk 			syslog(LOG_ERR, "%s: %s not in namelist",
    236  1.13       pk 			    _PATH_UNIX, current_nl[cursyms[i]].n_name);
    237   1.1      cgd 			exit(1);
    238   1.1      cgd 		}
    239  1.13       pk 
    240  1.25      leo 	KREAD(kd_kern, current_nl[X_DUMPDEV].n_value, &dumpdev);
    241   1.5       pk 	if (dumpdev == NODEV) {
    242  1.13       pk 		syslog(LOG_WARNING, "no core dump (no dumpdev)");
    243  1.13       pk 		exit(1);
    244   1.5       pk 	}
    245  1.25      leo 	KREAD(kd_kern, current_nl[X_DUMPLO].n_value, &dumplo);
    246  1.22      cgd 	dumplo *= DEV_BSIZE;
    247   1.1      cgd 	if (verbose)
    248  1.13       pk 		(void)printf("dumplo = %d (%d * %d)\n",
    249  1.22      cgd 		    dumplo, dumplo / DEV_BSIZE, DEV_BSIZE);
    250  1.25      leo 	KREAD(kd_kern, current_nl[X_DUMPMAG].n_value, &dumpmag);
    251  1.25      leo 
    252  1.25      leo 	if (kernel == NULL) {
    253  1.25      leo 		(void)kvm_read(kd_kern, current_nl[X_VERSION].n_value,
    254  1.25      leo 			vers, sizeof(vers));
    255  1.25      leo 		vers[sizeof(vers) - 1] = '\0';
    256  1.25      leo 	}
    257  1.25      leo 
    258   1.1      cgd 	ddname = find_dev(dumpdev, S_IFBLK);
    259   1.1      cgd 	dumpfd = Open(ddname, O_RDWR);
    260  1.25      leo 
    261  1.25      leo 	dump_sys = kernel ? kernel : _PATH_UNIX;
    262  1.25      leo 
    263  1.25      leo 	kd_dump = kvm_openfiles(dump_sys, ddname, NULL, O_RDWR, errbuf);
    264  1.25      leo 	if (kd_dump == NULL) {
    265  1.25      leo 		syslog(LOG_ERR, "%s: kvm_openfiles: %s", dump_sys, errbuf);
    266   1.1      cgd 		exit(1);
    267   1.1      cgd 	}
    268  1.13       pk 
    269  1.25      leo 	if (kvm_nlist(kd_dump, dump_nl) == -1)
    270  1.25      leo 		syslog(LOG_ERR, "%s: kvm_nlist: %s", dump_sys,
    271  1.25      leo 			kvm_geterr(kd_dump));
    272  1.25      leo 
    273  1.25      leo 	for (i = 0; dumpsyms[i] != -1; i++)
    274  1.25      leo 		if (dump_nl[dumpsyms[i]].n_value == 0) {
    275  1.25      leo 			syslog(LOG_ERR, "%s: %s not in namelist",
    276  1.25      leo 			    dump_sys, dump_nl[dumpsyms[i]].n_name);
    277  1.25      leo 			exit(1);
    278  1.25      leo 		}
    279  1.26      leo 	hdrsz = kvm_dump_mkheader(kd_dump, (off_t)dumplo);
    280  1.27      leo 
    281  1.27      leo 	/*
    282  1.27      leo 	 * If 'hdrsz' == 0, kvm_dump_mkheader() failed on the magic-number
    283  1.27      leo 	 * checks, ergo no dump is present...
    284  1.27      leo 	 */
    285  1.27      leo 	if (hdrsz == 0) {
    286  1.27      leo 		syslog(LOG_WARNING, "no core dump");
    287  1.27      leo 		exit(1);
    288  1.27      leo 	}
    289  1.25      leo 	if (hdrsz == -1) {
    290  1.25      leo 		syslog(LOG_ERR, "%s: kvm_dump_mkheader: %s", dump_sys,
    291  1.26      leo 			kvm_geterr(kd_dump));
    292  1.25      leo 		exit(1);
    293  1.25      leo 	}
    294  1.25      leo 	dumplo += hdrsz;
    295  1.25      leo 	kvm_close(kd_kern);
    296   1.1      cgd }
    297   1.1      cgd 
    298  1.13       pk void
    299   1.1      cgd check_kmem()
    300   1.1      cgd {
    301  1.25      leo 	register char	*cp;
    302  1.25      leo 	register int	panicloc;
    303  1.13       pk 	char core_vers[1024];
    304   1.1      cgd 
    305  1.25      leo 	(void)kvm_read(kd_dump, dump_nl[X_VERSION].n_value, core_vers,
    306  1.25      leo 		sizeof(core_vers));
    307  1.25      leo 	core_vers[sizeof(core_vers) - 1] = '\0';
    308  1.25      leo 
    309  1.15  mycroft 	if (strcmp(vers, core_vers) && kernel == 0)
    310  1.13       pk 		syslog(LOG_WARNING,
    311  1.13       pk 		    "warning: %s version mismatch:\n\t%s\nand\t%s\n",
    312  1.13       pk 		    _PATH_UNIX, vers, core_vers);
    313  1.25      leo 
    314  1.25      leo 	KREAD(kd_dump, dump_nl[X_PANICSTR].n_value, &panicstr);
    315   1.1      cgd 	if (panicstr) {
    316  1.25      leo 		cp       = panic_mesg;
    317  1.25      leo 		panicloc = panicstr;
    318  1.25      leo 		do {
    319  1.25      leo 			KREAD(kd_dump, panicloc, cp);
    320  1.25      leo 			panicloc++;
    321  1.25      leo 		} while (*cp++ && cp < &panic_mesg[sizeof(panic_mesg)]);
    322   1.1      cgd 	}
    323   1.1      cgd }
    324   1.1      cgd 
    325  1.13       pk int
    326  1.13       pk dump_exists()
    327   1.1      cgd {
    328  1.13       pk 	int newdumpmag;
    329   1.1      cgd 
    330  1.25      leo 	KREAD(kd_dump, dump_nl[X_DUMPMAG].n_value, &newdumpmag);
    331  1.23      cgd 
    332  1.23      cgd 	/* Read the dump size. */
    333  1.25      leo 	KREAD(kd_dump, dump_nl[X_DUMPSIZE].n_value, &dumpsize);
    334  1.23      cgd 	dumpsize *= getpagesize();
    335  1.23      cgd 
    336  1.23      cgd 	/*
    337  1.23      cgd 	 * Return zero if core dump doesn't seem to be there, and note
    338  1.23      cgd 	 * it for syslog.  This check and return happens after the dump size
    339  1.23      cgd 	 * is read, so dumpsize is whether or not the core is valid (for -f).
    340  1.23      cgd 	 */
    341  1.13       pk 	if (newdumpmag != dumpmag) {
    342   1.1      cgd 		if (verbose)
    343  1.13       pk 			syslog(LOG_WARNING, "magic number mismatch (%x != %x)",
    344  1.13       pk 			    newdumpmag, dumpmag);
    345  1.13       pk 		syslog(LOG_WARNING, "no core dump");
    346   1.1      cgd 		return (0);
    347   1.1      cgd 	}
    348  1.13       pk 	return (1);
    349  1.13       pk }
    350  1.13       pk 
    351  1.25      leo void
    352  1.25      leo clear_dump()
    353  1.25      leo {
    354  1.25      leo 	if (kvm_dump_inval(kd_dump) == -1)
    355  1.25      leo 		syslog(LOG_ERR, "%s: kvm_clear_dump: %s", ddname,
    356  1.25      leo 			kvm_geterr(kd_dump));
    357  1.25      leo 
    358  1.25      leo }
    359  1.25      leo 
    360  1.13       pk char buf[1024 * 1024];
    361  1.16  deraadt 
    362  1.13       pk void
    363  1.13       pk save_core()
    364  1.13       pk {
    365  1.13       pk 	register FILE *fp;
    366  1.13       pk 	register int bounds, ifd, nr, nw, ofd;
    367  1.13       pk 	char *rawp, path[MAXPATHLEN];
    368  1.13       pk 
    369  1.13       pk 	/*
    370  1.13       pk 	 * Get the current number and update the bounds file.  Do the update
    371  1.13       pk 	 * now, because may fail later and don't want to overwrite anything.
    372  1.13       pk 	 */
    373  1.13       pk 	(void)snprintf(path, sizeof(path), "%s/bounds", dirname);
    374  1.13       pk 	if ((fp = fopen(path, "r")) == NULL)
    375  1.13       pk 		goto err1;
    376  1.13       pk 	if (fgets(buf, sizeof(buf), fp) == NULL) {
    377  1.13       pk 		if (ferror(fp))
    378  1.13       pk err1:			syslog(LOG_WARNING, "%s: %s", path, strerror(errno));
    379  1.13       pk 		bounds = 0;
    380  1.13       pk 	} else
    381  1.13       pk 		bounds = atoi(buf);
    382  1.13       pk 	if (fp != NULL)
    383  1.13       pk 		(void)fclose(fp);
    384  1.13       pk 	if ((fp = fopen(path, "w")) == NULL)
    385  1.13       pk 		syslog(LOG_ERR, "%s: %m", path);
    386  1.13       pk 	else {
    387  1.13       pk 		(void)fprintf(fp, "%d\n", bounds + 1);
    388  1.13       pk 		(void)fclose(fp);
    389  1.13       pk 	}
    390  1.13       pk 	(void)fclose(fp);
    391  1.13       pk 
    392  1.13       pk 	/* Create the core file. */
    393  1.24  mycroft 	(void)snprintf(path, sizeof(path), "%s/netbsd.%d.core%s",
    394  1.13       pk 	    dirname, bounds, compress ? ".Z" : "");
    395  1.13       pk 	if (compress) {
    396  1.13       pk 		if ((fp = zopen(path, "w", 0)) == NULL) {
    397  1.13       pk 			syslog(LOG_ERR, "%s: %s", path, strerror(errno));
    398  1.13       pk 			exit(1);
    399  1.13       pk 		}
    400  1.25      leo 	} else {
    401  1.13       pk 		ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    402  1.25      leo 		fp  = fdopen(ofd, "w");
    403  1.25      leo 		if (fp == NULL) {
    404  1.25      leo 			syslog(LOG_ERR, "%s: fdopen: %s", path);
    405  1.25      leo 			exit(1);
    406  1.25      leo 		}
    407  1.25      leo 	}
    408  1.13       pk 
    409  1.13       pk 	/* Open the raw device. */
    410  1.13       pk 	rawp = rawname(ddname);
    411  1.13       pk 	if ((ifd = open(rawp, O_RDONLY)) == -1) {
    412  1.13       pk 		syslog(LOG_WARNING, "%s: %m; using block device", rawp);
    413  1.13       pk 		ifd = dumpfd;
    414  1.13       pk 	}
    415  1.13       pk 
    416  1.13       pk 	/* Seek to the start of the core. */
    417  1.13       pk 	Lseek(ifd, (off_t)dumplo, L_SET);
    418  1.13       pk 
    419  1.25      leo 	if (kvm_dump_wrtheader(kd_dump, fp, dumpsize) == -1) {
    420  1.25      leo 		syslog(LOG_ERR, "kvm_dump_wrtheader: %s : %s", path,
    421  1.25      leo 			kvm_geterr(kd_dump));
    422  1.25      leo 		exit(1);
    423  1.25      leo 	}
    424  1.25      leo 
    425  1.13       pk 	/* Copy the core file. */
    426  1.13       pk 	syslog(LOG_NOTICE, "writing %score to %s",
    427  1.13       pk 	    compress ? "compressed " : "", path);
    428  1.13       pk 	for (; dumpsize > 0; dumpsize -= nr) {
    429  1.13       pk 		(void)printf("%6dK\r", dumpsize / 1024);
    430  1.13       pk 		(void)fflush(stdout);
    431  1.13       pk 		nr = read(ifd, buf, MIN(dumpsize, sizeof(buf)));
    432  1.13       pk 		if (nr <= 0) {
    433  1.13       pk 			if (nr == 0)
    434  1.13       pk 				syslog(LOG_WARNING,
    435  1.13       pk 				    "WARNING: EOF on dump device");
    436  1.13       pk 			else
    437  1.13       pk 				syslog(LOG_ERR, "%s: %m", rawp);
    438  1.13       pk 			goto err2;
    439  1.13       pk 		}
    440  1.25      leo 		nw = fwrite(buf, 1, nr, fp);
    441  1.13       pk 		if (nw != nr) {
    442  1.13       pk 			syslog(LOG_ERR, "%s: %s",
    443  1.13       pk 			    path, strerror(nw == 0 ? EIO : errno));
    444  1.13       pk err2:			syslog(LOG_WARNING,
    445  1.15  mycroft 			    "WARNING: core may be incomplete");
    446  1.13       pk 			(void)printf("\n");
    447  1.13       pk 			exit(1);
    448  1.13       pk 		}
    449  1.13       pk 	}
    450  1.13       pk 	(void)close(ifd);
    451  1.25      leo 	(void)fclose(fp);
    452  1.13       pk 
    453  1.13       pk 	/* Copy the kernel. */
    454  1.15  mycroft 	ifd = Open(kernel ? kernel : _PATH_UNIX, O_RDONLY);
    455  1.13       pk 	(void)snprintf(path, sizeof(path), "%s/netbsd.%d%s",
    456  1.13       pk 	    dirname, bounds, compress ? ".Z" : "");
    457  1.13       pk 	if (compress) {
    458  1.13       pk 		if ((fp = zopen(path, "w", 0)) == NULL) {
    459  1.13       pk 			syslog(LOG_ERR, "%s: %s", path, strerror(errno));
    460  1.13       pk 			exit(1);
    461  1.13       pk 		}
    462  1.13       pk 	} else
    463  1.13       pk 		ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    464  1.13       pk 	syslog(LOG_NOTICE, "writing %skernel to %s",
    465  1.13       pk 	    compress ? "compressed " : "", path);
    466  1.13       pk 	while ((nr = read(ifd, buf, sizeof(buf))) > 0) {
    467  1.13       pk 		if (compress)
    468  1.13       pk 			nw = fwrite(buf, 1, nr, fp);
    469  1.13       pk 		else
    470  1.13       pk 			nw = write(ofd, buf, nr);
    471  1.13       pk 		if (nw != nr) {
    472  1.13       pk 			syslog(LOG_ERR, "%s: %s",
    473  1.13       pk 			    path, strerror(nw == 0 ? EIO : errno));
    474  1.13       pk 			syslog(LOG_WARNING,
    475  1.15  mycroft 			    "WARNING: kernel may be incomplete");
    476  1.13       pk 			exit(1);
    477  1.13       pk 		}
    478  1.13       pk 	}
    479  1.13       pk 	if (nr < 0) {
    480  1.13       pk 		syslog(LOG_ERR, "%s: %s",
    481  1.15  mycroft 		    kernel ? kernel : _PATH_UNIX, strerror(errno));
    482  1.13       pk 		syslog(LOG_WARNING,
    483  1.15  mycroft 		    "WARNING: kernel may be incomplete");
    484  1.13       pk 		exit(1);
    485   1.1      cgd 	}
    486  1.13       pk 	if (compress)
    487  1.13       pk 		(void)fclose(fp);
    488  1.13       pk 	else
    489  1.13       pk 		(void)close(ofd);
    490   1.1      cgd }
    491   1.1      cgd 
    492   1.1      cgd char *
    493  1.13       pk find_dev(dev, type)
    494  1.13       pk 	register dev_t dev;
    495  1.13       pk 	register int type;
    496   1.1      cgd {
    497  1.13       pk 	register DIR *dfd;
    498  1.13       pk 	struct dirent *dir;
    499  1.13       pk 	struct stat sb;
    500  1.13       pk 	char *dp, devname[MAXPATHLEN + 1];
    501   1.1      cgd 
    502  1.13       pk 	if ((dfd = opendir(_PATH_DEV)) == NULL) {
    503  1.13       pk 		syslog(LOG_ERR, "%s: %s", _PATH_DEV, strerror(errno));
    504  1.13       pk 		exit(1);
    505  1.13       pk 	}
    506  1.13       pk 	(void)strcpy(devname, _PATH_DEV);
    507  1.13       pk 	while ((dir = readdir(dfd))) {
    508  1.13       pk 		(void)strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name);
    509  1.13       pk 		if (lstat(devname, &sb)) {
    510  1.13       pk 			syslog(LOG_ERR, "%s: %s", devname, strerror(errno));
    511  1.13       pk 			continue;
    512  1.13       pk 		}
    513  1.13       pk 		if ((sb.st_mode & S_IFMT) != type)
    514  1.13       pk 			continue;
    515  1.13       pk 		if (dev == sb.st_rdev) {
    516  1.13       pk 			closedir(dfd);
    517  1.13       pk 			if ((dp = strdup(devname)) == NULL) {
    518  1.13       pk 				syslog(LOG_ERR, "%s", strerror(errno));
    519  1.13       pk 				exit(1);
    520  1.13       pk 			}
    521  1.13       pk 			return (dp);
    522  1.13       pk 		}
    523  1.13       pk 	}
    524  1.13       pk 	closedir(dfd);
    525  1.13       pk 	syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev));
    526  1.13       pk 	exit(1);
    527   1.1      cgd }
    528   1.1      cgd 
    529  1.13       pk char *
    530  1.13       pk rawname(s)
    531  1.13       pk 	char *s;
    532   1.1      cgd {
    533  1.13       pk 	char *sl, name[MAXPATHLEN];
    534   1.1      cgd 
    535  1.17  mycroft 	if ((sl = strrchr(s, '/')) == NULL || sl[1] == '0') {
    536  1.13       pk 		syslog(LOG_ERR,
    537  1.13       pk 		    "can't make raw dump device name from %s", s);
    538  1.13       pk 		return (s);
    539  1.13       pk 	}
    540  1.13       pk 	(void)snprintf(name, sizeof(name), "%.*s/r%s", sl - s, s, sl + 1);
    541  1.13       pk 	if ((sl = strdup(name)) == NULL) {
    542  1.13       pk 		syslog(LOG_ERR, "%s", strerror(errno));
    543   1.1      cgd 		exit(1);
    544   1.1      cgd 	}
    545  1.13       pk 	return (sl);
    546   1.1      cgd }
    547   1.1      cgd 
    548  1.13       pk int
    549  1.13       pk get_crashtime()
    550   1.1      cgd {
    551  1.13       pk 	time_t dumptime;			/* Time the dump was taken. */
    552   1.1      cgd 
    553  1.25      leo 	KREAD(kd_dump, dump_nl[X_TIME].n_value, &dumptime);
    554  1.13       pk 	if (dumptime == 0) {
    555  1.13       pk 		if (verbose)
    556  1.13       pk 			syslog(LOG_ERR, "dump time is zero");
    557   1.1      cgd 		return (0);
    558  1.13       pk 	}
    559  1.13       pk 	(void)printf("savecore: system went down at %s", ctime(&dumptime));
    560  1.13       pk #define	LEEWAY	(7 * SECSPERDAY)
    561  1.13       pk 	if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
    562  1.13       pk 		(void)printf("dump time is unreasonable\n");
    563   1.1      cgd 		return (0);
    564   1.1      cgd 	}
    565  1.13       pk 	return (1);
    566   1.1      cgd }
    567   1.1      cgd 
    568  1.13       pk int
    569  1.13       pk check_space()
    570   1.1      cgd {
    571   1.1      cgd 	register FILE *fp;
    572  1.15  mycroft 	char *tkernel;
    573  1.15  mycroft 	off_t minfree, spacefree, kernelsize, needed;
    574  1.13       pk 	struct stat st;
    575  1.13       pk 	struct statfs fsbuf;
    576  1.13       pk 	char buf[100], path[MAXPATHLEN];
    577  1.13       pk 
    578  1.15  mycroft 	tkernel = kernel ? kernel : _PATH_UNIX;
    579  1.15  mycroft 	if (stat(tkernel, &st) < 0) {
    580  1.15  mycroft 		syslog(LOG_ERR, "%s: %m", tkernel);
    581  1.13       pk 		exit(1);
    582  1.13       pk 	}
    583  1.15  mycroft 	kernelsize = st.st_blocks * S_BLKSIZE;
    584  1.13       pk 	if (statfs(dirname, &fsbuf) < 0) {
    585  1.13       pk 		syslog(LOG_ERR, "%s: %m", dirname);
    586  1.13       pk 		exit(1);
    587  1.13       pk 	}
    588  1.13       pk  	spacefree = (fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
    589   1.1      cgd 
    590  1.13       pk 	(void)snprintf(path, sizeof(path), "%s/minfree", dirname);
    591  1.13       pk 	if ((fp = fopen(path, "r")) == NULL)
    592  1.13       pk 		minfree = 0;
    593  1.13       pk 	else {
    594  1.13       pk 		if (fgets(buf, sizeof(buf), fp) == NULL)
    595  1.13       pk 			minfree = 0;
    596  1.13       pk 		else
    597  1.13       pk 			minfree = atoi(buf);
    598  1.13       pk 		(void)fclose(fp);
    599   1.1      cgd 	}
    600  1.13       pk 
    601  1.15  mycroft 	needed = (dumpsize + kernelsize) / 1024;
    602  1.13       pk  	if (minfree > 0 && spacefree - needed < minfree) {
    603  1.13       pk 		syslog(LOG_WARNING,
    604  1.13       pk 		    "no dump, not enough free space on device");
    605  1.13       pk 		return (0);
    606   1.1      cgd 	}
    607  1.13       pk 	if (spacefree - needed < minfree)
    608  1.13       pk 		syslog(LOG_WARNING,
    609  1.13       pk 		    "dump performed, but free space threshold crossed");
    610  1.13       pk 	return (1);
    611   1.1      cgd }
    612   1.1      cgd 
    613  1.13       pk int
    614   1.1      cgd Open(name, rw)
    615   1.1      cgd 	char *name;
    616   1.1      cgd 	int rw;
    617   1.1      cgd {
    618   1.1      cgd 	int fd;
    619   1.1      cgd 
    620  1.13       pk 	if ((fd = open(name, rw, 0)) < 0) {
    621  1.13       pk 		syslog(LOG_ERR, "%s: %m", name);
    622   1.1      cgd 		exit(1);
    623   1.1      cgd 	}
    624   1.1      cgd 	return (fd);
    625   1.1      cgd }
    626   1.1      cgd 
    627  1.13       pk void
    628   1.1      cgd Lseek(fd, off, flag)
    629   1.1      cgd 	int fd, flag;
    630  1.13       pk 	off_t off;
    631   1.1      cgd {
    632  1.13       pk 	off_t ret;
    633   1.1      cgd 
    634   1.1      cgd 	ret = lseek(fd, off, flag);
    635   1.1      cgd 	if (ret == -1) {
    636  1.13       pk 		syslog(LOG_ERR, "lseek: %m");
    637   1.1      cgd 		exit(1);
    638   1.1      cgd 	}
    639   1.1      cgd }
    640   1.1      cgd 
    641  1.13       pk int
    642   1.1      cgd Create(file, mode)
    643   1.1      cgd 	char *file;
    644   1.1      cgd 	int mode;
    645   1.1      cgd {
    646   1.1      cgd 	register int fd;
    647   1.1      cgd 
    648  1.17  mycroft 	fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, mode);
    649   1.1      cgd 	if (fd < 0) {
    650  1.13       pk 		syslog(LOG_ERR, "%s: %m", file);
    651   1.1      cgd 		exit(1);
    652   1.1      cgd 	}
    653   1.1      cgd 	return (fd);
    654   1.1      cgd }
    655   1.1      cgd 
    656  1.13       pk void
    657  1.13       pk Write(fd, bp, size)
    658   1.1      cgd 	int fd, size;
    659  1.13       pk 	void *bp;
    660   1.1      cgd {
    661   1.1      cgd 	int n;
    662   1.1      cgd 
    663  1.13       pk 	if ((n = write(fd, bp, size)) < size) {
    664  1.13       pk 		syslog(LOG_ERR, "write: %s", strerror(n == -1 ? errno : EIO));
    665   1.1      cgd 		exit(1);
    666   1.1      cgd 	}
    667   1.1      cgd }
    668   1.1      cgd 
    669  1.13       pk void
    670   1.1      cgd usage()
    671   1.1      cgd {
    672  1.13       pk 	(void)syslog(LOG_ERR, "usage: savecore [-cfvz] [-N system] directory");
    673   1.1      cgd 	exit(1);
    674   1.1      cgd }
    675