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