Home | History | Annotate | Line # | Download | only in savecore
savecore.c revision 1.6
      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.6 1994/02/14 19:32:21 cgd 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 #define ok(number) ((number) - KERNBASE)
     61 
     62 struct nlist current_nl[] = {	/* namelist for currently running system */
     63 #define X_DUMPDEV	0
     64 	{ "_dumpdev" },
     65 #define X_DUMPLO	1
     66 	{ "_dumplo" },
     67 #define X_TIME		2
     68 	{ "_time" },
     69 #define	X_DUMPSIZE	3
     70 	{ "_dumpsize" },
     71 #define X_VERSION	4
     72 	{ "_version" },
     73 #define X_PANICSTR	5
     74 	{ "_panicstr" },
     75 #define	X_DUMPMAG	6
     76 	{ "_dumpmag" },
     77 	{ "" },
     78 };
     79 
     80 struct nlist dump_nl[] = {	/* name list for dumped system */
     81 	{ "_dumpdev" },		/* entries MUST be the same as */
     82 	{ "_dumplo" },		/*	those in current_nl[]  */
     83 	{ "_time" },
     84 	{ "_dumpsize" },
     85 	{ "_version" },
     86 	{ "_panicstr" },
     87 	{ "_dumpmag" },
     88 	{ "" },
     89 };
     90 
     91 char	*system;
     92 char	*dirname;			/* directory to save dumps in */
     93 char	*ddname;			/* name of dump device */
     94 int	dumpfd;				/* read/write descriptor on block dev */
     95 char	*find_dev();
     96 dev_t	dumpdev;			/* dump device */
     97 time_t	dumptime;			/* time the dump was taken */
     98 int	dumplo;				/* where dump starts on dumpdev */
     99 int	dumpsize;			/* amount of memory dumped */
    100 int	dumpmag;			/* magic number in dump */
    101 time_t	now;				/* current date */
    102 char	*path();
    103 char	*malloc();
    104 char	*ctime();
    105 char	vers[80];
    106 char	core_vers[80];
    107 char	panic_mesg[80];
    108 int	panicstr;
    109 off_t	lseek();
    110 off_t	Lseek();
    111 int	verbose;
    112 int	force;
    113 int	clear;
    114 extern	int errno;
    115 
    116 main(argc, argv)
    117 	char **argv;
    118 	int argc;
    119 {
    120 	extern char *optarg;
    121 	extern int optind;
    122 	int ch;
    123 	char *cp;
    124 
    125 	while ((ch = getopt(argc, argv, "cdfv")) != EOF)
    126 		switch(ch) {
    127 		case 'c':
    128 			clear = 1;
    129 			break;
    130 		case 'd':		/* not documented */
    131 		case 'v':
    132 			verbose = 1;
    133 			break;
    134 		case 'f':
    135 			force = 1;
    136 			break;
    137 		case '?':
    138 		default:
    139 			usage();
    140 		}
    141 	argc -= optind;
    142 	argv += optind;
    143 
    144 	/* This is wrong, but I want "savecore -c" to work. */
    145 	if (!clear) {
    146 		if (argc != 1 && argc != 2)
    147 			usage();
    148 		dirname = argv[0];
    149 	}
    150 	if (argc == 2)
    151 		system = argv[1];
    152 
    153 	openlog("savecore", LOG_ODELAY, LOG_AUTH);
    154 
    155 	read_kmem();
    156 	if (!dump_exists()) {
    157 /*		(void)fprintf(stderr, "savecore: no core dump\n");*/
    158 		if (!force)
    159 			exit(0);
    160 	}
    161 	if (clear) {
    162 		clear_dump();
    163 		exit(0);
    164 	}
    165 	(void) time(&now);
    166 	check_kmem();
    167 	if (panicstr)
    168 		log(LOG_CRIT, "reboot after panic: %s\n", panic_mesg);
    169 	else
    170 		syslog(LOG_CRIT, "reboot\n");
    171 
    172 	if (access(dirname, W_OK) < 0) {
    173 		Perror(LOG_ERR, "%s: %m\n", dirname);
    174 		exit(1);
    175 	}
    176 	if ((!get_crashtime() || !check_space()) && !force)
    177 		exit(1);
    178 	save_core();
    179 	clear_dump();
    180 	exit(0);
    181 }
    182 
    183 dump_exists()
    184 {
    185 	int word;
    186 
    187 	Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
    188 	Read(dumpfd, (char *)&word, sizeof (word));
    189 	if (verbose && word != dumpmag)
    190 		printf("magic number mismatch: %x != %x\n", word, dumpmag);
    191 	return (word == dumpmag);
    192 }
    193 
    194 clear_dump()
    195 {
    196 	int zero = 0;
    197 
    198 	Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET);
    199 	Write(dumpfd, (char *)&zero, sizeof (zero));
    200 }
    201 
    202 char *
    203 find_dev(dev, type)
    204 	register dev_t dev;
    205 	register int type;
    206 {
    207 	register DIR *dfd = opendir(_PATH_DEV);
    208 	struct dirent *dir;
    209 	struct stat statb;
    210 	static char devname[MAXPATHLEN + 1];
    211 	char *dp;
    212 
    213 	strcpy(devname, _PATH_DEV);
    214 	while ((dir = readdir(dfd))) {
    215 		strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name);
    216 		if (stat(devname, &statb)) {
    217 			perror(devname);
    218 			continue;
    219 		}
    220 		if ((statb.st_mode&S_IFMT) != type)
    221 			continue;
    222 		if (dev == statb.st_rdev) {
    223 			closedir(dfd);
    224 			dp = malloc(strlen(devname)+1);
    225 			strcpy(dp, devname);
    226 			return (dp);
    227 		}
    228 	}
    229 	closedir(dfd);
    230 	log(LOG_ERR, "Can't find device %d/%d\n", major(dev), minor(dev));
    231 	exit(1);
    232 	/*NOTREACHED*/
    233 }
    234 
    235 char *
    236 rawname(s)
    237 	char *s;
    238 {
    239 	static char name[MAXPATHLEN];
    240 	char *sl, *rindex();
    241 
    242 	if ((sl = rindex(s, '/')) == NULL || sl[1] == '0') {
    243 		log(LOG_ERR, "can't make raw dump device name from %s?\n", s);
    244 		return (s);
    245 	}
    246 	sprintf(name, "%.*s/r%s", sl - s, s, sl + 1);
    247 	return (name);
    248 }
    249 
    250 int	cursyms[] =
    251     { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, -1 };
    252 int	dumpsyms[] =
    253     { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 };
    254 read_kmem()
    255 {
    256 	register char *cp;
    257 	FILE *fp;
    258 	char *dump_sys;
    259 	int kmem, i;
    260 
    261 	dump_sys = system ? system : _PATH_UNIX;
    262 	nlist(_PATH_UNIX, current_nl);
    263 	nlist(dump_sys, dump_nl);
    264 	/*
    265 	 * Some names we need for the currently running system,
    266 	 * others for the system that was running when the dump was made.
    267 	 * The values obtained from the current system are used
    268 	 * to look for things in /dev/kmem that cannot be found
    269 	 * in the dump_sys namelist, but are presumed to be the same
    270 	 * (since the disk partitions are probably the same!)
    271 	 */
    272 	for (i = 0; cursyms[i] != -1; i++)
    273 		if (current_nl[cursyms[i]].n_value == 0) {
    274 			log(LOG_ERR, "%s: %s not in namelist\n", _PATH_UNIX,
    275 			    current_nl[cursyms[i]].n_name);
    276 			exit(1);
    277 		}
    278 	for (i = 0; dumpsyms[i] != -1; i++)
    279 		if (dump_nl[dumpsyms[i]].n_value == 0) {
    280 			log(LOG_ERR, "%s: %s not in namelist\n", dump_sys,
    281 			    dump_nl[dumpsyms[i]].n_name);
    282 			exit(1);
    283 		}
    284 	kmem = Open(_PATH_KMEM, O_RDONLY);
    285 	Lseek(kmem, (long)current_nl[X_DUMPDEV].n_value, L_SET);
    286 	Read(kmem, (char *)&dumpdev, sizeof (dumpdev));
    287 	if (dumpdev == NODEV) {
    288 		if (verbose)
    289 			printf("dumpdev = NODEV\n");
    290 		exit(2);
    291 	}
    292 	Lseek(kmem, (long)current_nl[X_DUMPLO].n_value, L_SET);
    293 	Read(kmem, (char *)&dumplo, sizeof (dumplo));
    294 	if (verbose)
    295 		printf("dumplo = %d (%d * %d)\n", dumplo, dumplo/DEV_BSIZE,
    296 		    DEV_BSIZE);
    297 	Lseek(kmem, (long)current_nl[X_DUMPMAG].n_value, L_SET);
    298 	Read(kmem, (char *)&dumpmag, sizeof (dumpmag));
    299 	dumplo *= DEV_BSIZE;
    300 	ddname = find_dev(dumpdev, S_IFBLK);
    301 	dumpfd = Open(ddname, O_RDWR);
    302 	fp = fdopen(kmem, "r");
    303 	if (fp == NULL) {
    304 		log(LOG_ERR, "Couldn't fdopen kmem\n");
    305 		exit(1);
    306 	}
    307 	if (system)
    308 		return;
    309 	fseek(fp, (long)current_nl[X_VERSION].n_value, L_SET);
    310 	fgets(vers, sizeof (vers), fp);
    311 	fclose(fp);
    312 }
    313 
    314 check_kmem()
    315 {
    316 	FILE *fp;
    317 	register char *cp;
    318 
    319 	fp = fdopen(dumpfd, "r");
    320 	if (fp == NULL) {
    321 		log(LOG_ERR, "Can't fdopen dumpfd\n");
    322 		exit(1);
    323 	}
    324 
    325 	fseek(fp, (off_t)(dumplo+ok(dump_nl[X_VERSION].n_value)), L_SET);
    326 	fgets(core_vers, sizeof (core_vers), fp);
    327 	if (!eq(vers, core_vers) && system == 0) {
    328 		log(LOG_WARNING, "Warning: %s version mismatch:\n", _PATH_UNIX);
    329 		log(LOG_WARNING, "\t%s\n", vers);
    330 		log(LOG_WARNING, "and\t%s\n", core_vers);
    331 	}
    332 
    333 	fseek(fp, (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET);
    334 	fread((char *)&panicstr, sizeof (panicstr), 1, fp);
    335 	if (panicstr) {
    336 		fseek(fp, dumplo + ok(panicstr), L_SET);
    337 		cp = panic_mesg;
    338 		do
    339 			*cp = getc(fp);
    340 		while (*cp++ && cp < &panic_mesg[sizeof(panic_mesg)]);
    341 	}
    342 	/* don't fclose(fp); we want the file descriptor */
    343 }
    344 
    345 get_crashtime()
    346 {
    347 	time_t clobber = (time_t)0;
    348 
    349 	Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET);
    350 	Read(dumpfd, (char *)&dumptime, sizeof dumptime);
    351 	if (dumptime == 0) {
    352 		if (verbose)
    353 			printf("Dump time is zero.\n");
    354 		return (0);
    355 	}
    356 	printf("System went down at %s", ctime(&dumptime));
    357 	if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) {
    358 		printf("dump time is unreasonable\n");
    359 		return (0);
    360 	}
    361 	return (1);
    362 }
    363 
    364 char *
    365 path(file)
    366 	char *file;
    367 {
    368 	register char *cp = malloc(strlen(file) + strlen(dirname) + 2);
    369 
    370 	(void) strcpy(cp, dirname);
    371 	(void) strcat(cp, "/");
    372 	(void) strcat(cp, file);
    373 	return (cp);
    374 }
    375 
    376 check_space()
    377 {
    378 	long minfree, spacefree;
    379 	struct statfs fsbuf;
    380 
    381 	if (statfs(dirname, &fsbuf) < 0) {
    382 		Perror(LOG_ERR, "%s: %m\n", dirname);
    383 		exit(1);
    384 	}
    385  	spacefree = fsbuf.f_bavail * fsbuf.f_fsize / 1024;
    386 	minfree = read_number("minfree");
    387  	if (minfree > 0 && spacefree - dumpsize < minfree) {
    388 		log(LOG_WARNING, "Dump omitted, not enough space on device\n");
    389 		return (0);
    390 	}
    391 	if (spacefree - dumpsize < minfree)
    392 		log(LOG_WARNING,
    393 		    "Dump performed, but free space threshold crossed\n");
    394 	return (1);
    395 }
    396 
    397 read_number(fn)
    398 	char *fn;
    399 {
    400 	char lin[80];
    401 	register FILE *fp;
    402 
    403 	fp = fopen(path(fn), "r");
    404 	if (fp == NULL)
    405 		return (0);
    406 	if (fgets(lin, 80, fp) == NULL) {
    407 		fclose(fp);
    408 		return (0);
    409 	}
    410 	fclose(fp);
    411 	return (atoi(lin));
    412 }
    413 
    414 /*#define	BUFSIZE		(256*1024)		/* 1/4 Mb */
    415 #define	BUFSIZE		(8*1024)
    416 
    417 save_core()
    418 {
    419 	register int n;
    420 	register char *cp;
    421 	register int ifd, ofd, bounds;
    422 	int ret;
    423 	char *bfile;
    424 	register FILE *fp;
    425 
    426 	cp = malloc(BUFSIZE);
    427 	if (cp == 0) {
    428 		log(LOG_ERR, "savecore: Can't allocate i/o buffer.\n");
    429 		return;
    430 	}
    431 	bounds = read_number("bounds");
    432 	ifd = Open(system ? system : _PATH_UNIX, O_RDONLY);
    433 	(void)sprintf(cp, "system.%d", bounds);
    434 	ofd = Create(path(cp), 0644);
    435 	while((n = Read(ifd, cp, BUFSIZE)) > 0)
    436 		Write(ofd, cp, n);
    437 	close(ifd);
    438 	close(ofd);
    439 	if ((ifd = open(rawname(ddname), O_RDONLY)) == -1) {
    440 		log(LOG_WARNING, "Can't open %s (%m); using block device",
    441 			rawname(ddname));
    442 		ifd = dumpfd;
    443 	}
    444 	Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET);
    445 	Read(dumpfd, (char *)&dumpsize, sizeof (dumpsize));
    446 	(void)sprintf(cp, "ram.%d", bounds);
    447 	ofd = Create(path(cp), 0644);
    448 	Lseek(ifd, (off_t)dumplo, L_SET);
    449 	dumpsize *= NBPG;
    450 	log(LOG_NOTICE, "Saving %d bytes of image in ram.%d\n",
    451 	    dumpsize, bounds);
    452 	while (dumpsize > 0) {
    453 		n = read(ifd, cp,
    454 		    dumpsize > BUFSIZE ? BUFSIZE : dumpsize);
    455 		if (n <= 0) {
    456 			if (n == 0)
    457 				log(LOG_WARNING,
    458 				    "WARNING: EOF on dump device; %s\n",
    459 				    "ram file may be incomplete");
    460 			else
    461 				Perror(LOG_ERR, "read from dumpdev: %m",
    462 				    "read");
    463 			break;
    464 		}
    465 		if ((ret = write(ofd, cp, n)) < n) {
    466 			if (ret < 0)
    467 				Perror(LOG_ERR, "write: %m", "write");
    468 			else
    469 				log(LOG_ERR, "short write: wrote %d of %d\n",
    470 				    ret, n);
    471 			log(LOG_WARNING, "WARNING: ram file may be incomplete\n");
    472 			break;
    473 		}
    474 		dumpsize -= n;
    475 	}
    476 	close(ifd);
    477 	close(ofd);
    478 	bfile = path("bounds");
    479 	fp = fopen(bfile, "w");
    480 	if (fp) {
    481 		fprintf(fp, "%d\n", bounds+1);
    482 		fclose(fp);
    483 	} else
    484 		Perror(LOG_ERR, "Can't create bounds file %s: %m", bfile);
    485 	free(cp);
    486 }
    487 
    488 /*
    489  * Versions of std routines that exit on error.
    490  */
    491 Open(name, rw)
    492 	char *name;
    493 	int rw;
    494 {
    495 	int fd;
    496 
    497 	fd = open(name, rw);
    498 	if (fd < 0) {
    499 		Perror(LOG_ERR, "%s: %m", name);
    500 		exit(1);
    501 	}
    502 	return (fd);
    503 }
    504 
    505 Read(fd, buff, size)
    506 	int fd, size;
    507 	char *buff;
    508 {
    509 	int ret;
    510 
    511 	ret = read(fd, buff, size);
    512 	if (ret < 0) {
    513 		Perror(LOG_ERR, "read: %m", "read");
    514 		exit(1);
    515 	}
    516 	return (ret);
    517 }
    518 
    519 off_t
    520 Lseek(fd, off, flag)
    521 	int fd, flag;
    522 	long off;
    523 {
    524 	long ret;
    525 
    526 	ret = lseek(fd, off, flag);
    527 	if (ret == -1) {
    528 		Perror(LOG_ERR, "lseek: %m", "lseek");
    529 		exit(1);
    530 	}
    531 	return (ret);
    532 }
    533 
    534 Create(file, mode)
    535 	char *file;
    536 	int mode;
    537 {
    538 	register int fd;
    539 
    540 	fd = creat(file, mode);
    541 	if (fd < 0) {
    542 		Perror(LOG_ERR, "%s: %m", file);
    543 		exit(1);
    544 	}
    545 	return (fd);
    546 }
    547 
    548 Write(fd, buf, size)
    549 	int fd, size;
    550 	char *buf;
    551 {
    552 	int n;
    553 
    554 	if ((n = write(fd, buf, size)) < size) {
    555 		if (n < 0)
    556 			Perror(LOG_ERR, "write: %m", "write");
    557 		else
    558 			log(LOG_ERR, "short write: wrote %d of %d\n", n, size);
    559 		exit(1);
    560 	}
    561 }
    562 
    563 /* VARARGS2 */
    564 log(level, msg, a1, a2)
    565 	int level;
    566 	char *msg;
    567 {
    568 
    569 	fprintf(stderr, msg, a1, a2);
    570 	syslog(level, msg, a1, a2);
    571 }
    572 
    573 Perror(level, msg, s)
    574 	int level;
    575 	char *msg, *s;
    576 {
    577 	int oerrno = errno;
    578 
    579 	perror(s);
    580 	errno = oerrno;
    581 	syslog(level, msg, s);
    582 }
    583 
    584 usage()
    585 {
    586 	(void)fprintf(stderr, "usage: savecore [-cfv] dirname [system]\n");
    587 	exit(1);
    588 }
    589