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