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