xref: /freebsd/sbin/savecore/savecore.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * Copyright (c) 1986, 1992, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  */
66 
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
69 
70 #include <sys/param.h>
71 #include <sys/disk.h>
72 #include <sys/kerneldump.h>
73 #include <sys/param.h>
74 #include <sys/mount.h>
75 #include <sys/stat.h>
76 #include <errno.h>
77 #include <fcntl.h>
78 #include <fstab.h>
79 #include <paths.h>
80 #include <stdarg.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <syslog.h>
85 #include <time.h>
86 #include <unistd.h>
87 
88 /* The size of the buffer used for I/O. */
89 #define	BUFFERSIZE	(1024*1024)
90 
91 #define	STATUS_BAD	0
92 #define	STATUS_GOOD	1
93 #define	STATUS_UNKNOWN	2
94 
95 static int checkfor, compress, clear, force, keep, verbose;	/* flags */
96 static int nfound, nsaved, nerr;			/* statistics */
97 
98 extern FILE *zopen(const char *, const char *);
99 
100 static void
101 printheader(FILE *f, const struct kerneldumpheader *h, const char *device,
102     int bounds, const int status)
103 {
104 	uint64_t dumplen;
105 	time_t t;
106 	const char *stat_str;
107 
108 	fprintf(f, "Dump header from device %s\n", device);
109 	fprintf(f, "  Architecture: %s\n", h->architecture);
110 	fprintf(f, "  Architecture Version: %u\n", h->architectureversion);
111 	dumplen = dtoh64(h->dumplength);
112 	fprintf(f, "  Dump Length: %lldB (%lld MB)\n", (long long)dumplen,
113 	    (long long)(dumplen >> 20));
114 	fprintf(f, "  Blocksize: %d\n", dtoh32(h->blocksize));
115 	t = dtoh64(h->dumptime);
116 	fprintf(f, "  Dumptime: %s", ctime(&t));
117 	fprintf(f, "  Hostname: %s\n", h->hostname);
118 	fprintf(f, "  Magic: %s\n", h->magic);
119 	fprintf(f, "  Version String: %s", h->versionstring);
120 	fprintf(f, "  Panic String: %s\n", h->panicstring);
121 	fprintf(f, "  Dump Parity: %u\n", h->parity);
122 	fprintf(f, "  Bounds: %d\n", bounds);
123 
124 	switch(status) {
125 	case STATUS_BAD:
126 		stat_str = "bad";
127 		break;
128 	case STATUS_GOOD:
129 		stat_str = "good";
130 		break;
131 	default:
132 		stat_str = "unknown";
133 	}
134 	fprintf(f, "  Dump Status: %s\n", stat_str);
135 	fflush(f);
136 }
137 
138 static int
139 getbounds(void) {
140 	FILE *fp;
141 	char buf[6];
142 	int ret;
143 
144 	ret = 0;
145 
146 	if ((fp = fopen("bounds", "r")) == NULL) {
147 		if (verbose)
148 			printf("unable to open bounds file, using 0\n");
149 		return (ret);
150 	}
151 
152 	if (fgets(buf, sizeof buf, fp) == NULL) {
153 		syslog(LOG_WARNING, "unable to read from bounds, using 0");
154 		fclose(fp);
155 		return (ret);
156 	}
157 
158 	errno = 0;
159 	ret = (int)strtol(buf, NULL, 10);
160 	if (ret == 0 && (errno == EINVAL || errno == ERANGE))
161 		syslog(LOG_WARNING, "invalid value found in bounds, using 0");
162 	return (ret);
163 }
164 
165 static void
166 writebounds(int bounds) {
167 	FILE *fp;
168 
169 	if ((fp = fopen("bounds", "w")) == NULL) {
170 		syslog(LOG_WARNING, "unable to write to bounds file: %m");
171 		return;
172 	}
173 
174 	if (verbose)
175 		printf("bounds number: %d\n", bounds);
176 
177 	fprintf(fp, "%d\n", bounds);
178 	fclose(fp);
179 }
180 
181 /*
182  * Check that sufficient space is available on the disk that holds the
183  * save directory.
184  */
185 static int
186 check_space(const char *savedir, off_t dumpsize)
187 {
188 	FILE *fp;
189 	off_t minfree, spacefree, totfree, needed;
190 	struct statfs fsbuf;
191 	char buf[100], path[MAXPATHLEN];
192 
193 	if (statfs(savedir, &fsbuf) < 0) {
194 		syslog(LOG_ERR, "%s: %m", savedir);
195 		exit(1);
196 	}
197  	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
198 	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
199 
200 	(void)snprintf(path, sizeof(path), "%s/minfree", savedir);
201 	if ((fp = fopen(path, "r")) == NULL)
202 		minfree = 0;
203 	else {
204 		if (fgets(buf, sizeof(buf), fp) == NULL)
205 			minfree = 0;
206 		else
207 			minfree = atoi(buf);
208 		(void)fclose(fp);
209 	}
210 
211 	needed = dumpsize / 1024 + 2;	/* 2 for info file */
212  	if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
213 		syslog(LOG_WARNING,
214 	"no dump, not enough free space on device (%lld available, need %lld)",
215 		    (long long)(minfree > 0 ? spacefree : totfree),
216 		    (long long)needed);
217 		return (0);
218 	}
219 	if (spacefree - needed < 0)
220 		syslog(LOG_WARNING,
221 		    "dump performed, but free space threshold crossed");
222 	return (1);
223 }
224 
225 #define BLOCKSIZE (1<<12)
226 #define BLOCKMASK (~(BLOCKSIZE-1))
227 
228 static void
229 DoFile(const char *savedir, const char *device)
230 {
231 	static char *buf = NULL;
232 	struct kerneldumpheader kdhf, kdhl;
233 	off_t mediasize, dumpsize, firsthd, lasthd, dmpcnt;
234 	FILE *info, *fp;
235 	mode_t oumask;
236 	int fd, fdinfo, error, wl;
237 	int nr, nw, hs, he = 0;
238 	int bounds, status;
239 	u_int sectorsize;
240 
241 	bounds = getbounds();
242 	dmpcnt = 0;
243 	mediasize = 0;
244 	status = STATUS_UNKNOWN;
245 
246 	if (buf == NULL) {
247 		buf = malloc(BUFFERSIZE);
248 		if (buf == NULL) {
249 			syslog(LOG_ERR, "%m");
250 			return;
251 		}
252 	}
253 
254 	if (verbose)
255 		printf("checking for kernel dump on device %s\n", device);
256 
257 	fd = open(device, O_RDWR);
258 	if (fd < 0) {
259 		syslog(LOG_ERR, "%s: %m", device);
260 		return;
261 	}
262 
263 	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
264 	if (!error)
265 		error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
266 	if (error) {
267 		syslog(LOG_ERR,
268 		    "couldn't find media and/or sector size of %s: %m", device);
269 		goto closefd;
270 	}
271 
272 	if (verbose) {
273 		printf("mediasize = %lld\n", (long long)mediasize);
274 		printf("sectorsize = %u\n", sectorsize);
275 	}
276 
277 	lasthd = mediasize - sectorsize;
278 	lseek(fd, lasthd, SEEK_SET);
279 	error = read(fd, &kdhl, sizeof kdhl);
280 	if (error != sizeof kdhl) {
281 		syslog(LOG_ERR,
282 		    "error reading last dump header at offset %lld in %s: %m",
283 		    (long long)lasthd, device);
284 		goto closefd;
285 	}
286 	if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic)) {
287 		if (verbose)
288 			printf("magic mismatch on last dump header on %s\n",
289 			    device);
290 
291 		status = STATUS_BAD;
292 		if (force == 0)
293 			goto closefd;
294 
295 		if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
296 			    sizeof kdhl.magic) == 0) {
297 			if (verbose)
298 				printf("forcing magic on %s\n", device);
299 			memcpy(kdhl.magic, KERNELDUMPMAGIC,
300 			    sizeof kdhl.magic);
301 		} else {
302 			syslog(LOG_ERR, "unable to force dump - bad magic");
303 			goto closefd;
304 		}
305 	}
306 	if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
307 		syslog(LOG_ERR,
308 		    "unknown version (%d) in last dump header on %s",
309 		    dtoh32(kdhl.version), device);
310 
311 		status = STATUS_BAD;
312 		if (force == 0)
313 			goto closefd;
314 	}
315 
316 	nfound++;
317 	if (clear)
318 		goto nuke;
319 
320 	if (kerneldump_parity(&kdhl)) {
321 		syslog(LOG_ERR,
322 		    "parity error on last dump header on %s", device);
323 		nerr++;
324 		status = STATUS_BAD;
325 		if (force == 0)
326 			goto closefd;
327 	}
328 	dumpsize = dtoh64(kdhl.dumplength);
329 	firsthd = lasthd - dumpsize - sizeof kdhf;
330 	lseek(fd, firsthd, SEEK_SET);
331 	error = read(fd, &kdhf, sizeof kdhf);
332 	if (error != sizeof kdhf) {
333 		syslog(LOG_ERR,
334 		    "error reading first dump header at offset %lld in %s: %m",
335 		    (long long)firsthd, device);
336 		nerr++;
337 		goto closefd;
338 	}
339 
340 	if (verbose >= 2) {
341 		printf("First dump headers:\n");
342 		printheader(stdout, &kdhf, device, bounds, -1);
343 
344 		printf("\nLast dump headers:\n");
345 		printheader(stdout, &kdhl, device, bounds, -1);
346 		printf("\n");
347 	}
348 
349 	if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
350 		syslog(LOG_ERR,
351 		    "first and last dump headers disagree on %s", device);
352 		nerr++;
353 		status = STATUS_BAD;
354 		if (force == 0)
355 			goto closefd;
356 	} else {
357 		status = STATUS_GOOD;
358 	}
359 
360 	if (checkfor) {
361 		printf("A dump exists on %s\n", device);
362 		close(fd);
363 		exit(0);
364 	}
365 
366 	if (kdhl.panicstring[0])
367 		syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
368 	else
369 		syslog(LOG_ALERT, "reboot");
370 
371 	if (verbose)
372 		printf("Checking for available free space\n");
373 	if (!check_space(savedir, dumpsize)) {
374 		nerr++;
375 		goto closefd;
376 	}
377 
378 	writebounds(bounds + 1);
379 
380 	sprintf(buf, "info.%d", bounds);
381 
382 	/*
383 	 * Create or overwrite any existing dump header files.
384 	 */
385 	fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
386 	if (fdinfo < 0) {
387 		syslog(LOG_ERR, "%s: %m", buf);
388 		nerr++;
389 		goto closefd;
390 	}
391 	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
392 	if (compress) {
393 		sprintf(buf, "vmcore.%d.gz", bounds);
394 		fp = zopen(buf, "w");
395 	} else {
396 		sprintf(buf, "vmcore.%d", bounds);
397 		fp = fopen(buf, "w");
398 	}
399 	if (fp == NULL) {
400 		syslog(LOG_ERR, "%s: %m", buf);
401 		close(fdinfo);
402 		nerr++;
403 		goto closefd;
404 	}
405 	(void)umask(oumask);
406 
407 	info = fdopen(fdinfo, "w");
408 
409 	if (verbose)
410 		printheader(stdout, &kdhl, device, bounds, status);
411 
412 	printheader(info, &kdhl, device, bounds, status);
413 	fclose(info);
414 
415 	syslog(LOG_NOTICE, "writing %score to %s",
416 	    compress ? "compressed " : "", buf);
417 
418 	while (dumpsize > 0) {
419 		wl = BUFFERSIZE;
420 		if (wl > dumpsize)
421 			wl = dumpsize;
422 		nr = read(fd, buf, wl);
423 		if (nr != wl) {
424 			if (nr == 0)
425 				syslog(LOG_WARNING,
426 				    "WARNING: EOF on dump device");
427 			else
428 				syslog(LOG_ERR, "read error on %s: %m", device);
429 			nerr++;
430 			goto closeall;
431 		}
432 		if (compress) {
433 			nw = fwrite(buf, 1, wl, fp);
434 		} else {
435 			for (nw = 0; nw < nr; nw = he) {
436 				/* find a contiguous block of zeroes */
437 				for (hs = nw; hs < nr; hs += BLOCKSIZE) {
438 					for (he = hs; he < nr && buf[he] == 0;
439 					    ++he)
440 						/* nothing */ ;
441 					/* is the hole long enough to matter? */
442 					if (he >= hs + BLOCKSIZE)
443 						break;
444 				}
445 
446 				/* back down to a block boundary */
447 				he &= BLOCKMASK;
448 
449 				/*
450 				 * 1) Don't go beyond the end of the buffer.
451 				 * 2) If the end of the buffer is less than
452 				 *    BLOCKSIZE bytes away, we're at the end
453 				 *    of the file, so just grab what's left.
454 				 */
455 				if (hs + BLOCKSIZE > nr)
456 					hs = he = nr;
457 
458 				/*
459 				 * At this point, we have a partial ordering:
460 				 *     nw <= hs <= he <= nr
461 				 * If hs > nw, buf[nw..hs] contains non-zero data.
462 				 * If he > hs, buf[hs..he] is all zeroes.
463 				 */
464 				if (hs > nw)
465 					if (fwrite(buf + nw, hs - nw, 1, fp)
466 					    != 1)
467 					break;
468 				if (he > hs)
469 					if (fseeko(fp, he - hs, SEEK_CUR) == -1)
470 						break;
471 			}
472 		}
473 		if (nw != wl) {
474 			syslog(LOG_ERR,
475 			    "write error on vmcore.%d file: %m", bounds);
476 			syslog(LOG_WARNING,
477 			    "WARNING: vmcore may be incomplete");
478 			nerr++;
479 			goto closeall;
480 		}
481 		if (verbose) {
482 			dmpcnt += wl;
483 			printf("%llu\r", (unsigned long long)dmpcnt);
484 			fflush(stdout);
485 		}
486 		dumpsize -= wl;
487 	}
488 	if (verbose)
489 		printf("\n");
490 
491 	if (fclose(fp) < 0) {
492 		syslog(LOG_ERR, "error on vmcore.%d: %m", bounds);
493 		nerr++;
494 		goto closeall;
495 	}
496 	nsaved++;
497 
498 	if (verbose)
499 		printf("dump saved\n");
500 
501 nuke:
502 	if (clear || !keep) {
503 		if (verbose)
504 			printf("clearing dump header\n");
505 		memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
506 		lseek(fd, lasthd, SEEK_SET);
507 		error = write(fd, &kdhl, sizeof kdhl);
508 		if (error != sizeof kdhl)
509 			syslog(LOG_ERR,
510 			    "error while clearing the dump header: %m");
511 	}
512 	close(fd);
513 	return;
514 
515 closeall:
516 	fclose(fp);
517 
518 closefd:
519 	close(fd);
520 }
521 
522 static void
523 usage(void)
524 {
525 	fprintf(stderr, "%s\n%s\n%s\n",
526 	    "usage: savecore -c",
527 	    "       savecore -C [-v] [directory device]",
528 	    "       savecore [-fkvz] [directory [device ...]]");
529 	exit (1);
530 }
531 
532 int
533 main(int argc, char **argv)
534 {
535 	const char *savedir = ".";
536 	struct fstab *fsp;
537 	int i, ch, error;
538 
539 	checkfor = compress = clear = force = keep = verbose = 0;
540 	nfound = nsaved = nerr = 0;
541 
542 	openlog("savecore", LOG_PERROR, LOG_DAEMON);
543 
544 	while ((ch = getopt(argc, argv, "Ccfkvz")) != -1)
545 		switch(ch) {
546 		case 'C':
547 			checkfor = 1;
548 			break;
549 		case 'c':
550 			clear = 1;
551 			break;
552 		case 'k':
553 			keep = 1;
554 			break;
555 		case 'v':
556 			verbose++;
557 			break;
558 		case 'f':
559 			force = 1;
560 			break;
561 		case 'z':
562 			compress = 1;
563 			break;
564 		case '?':
565 		default:
566 			usage();
567 		}
568 	if (checkfor && (clear || force || keep))
569 		usage();
570 	argc -= optind;
571 	argv += optind;
572 	if (argc >= 1) {
573 		error = chdir(argv[0]);
574 		if (error) {
575 			syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
576 			exit(1);
577 		}
578 		savedir = argv[0];
579 		argc--;
580 		argv++;
581 	}
582 	if (argc == 0) {
583 		for (;;) {
584 			fsp = getfsent();
585 			if (fsp == NULL)
586 				break;
587 			if (strcmp(fsp->fs_vfstype, "swap") &&
588 			    strcmp(fsp->fs_vfstype, "dump"))
589 				continue;
590 			DoFile(savedir, fsp->fs_spec);
591 		}
592 	} else {
593 		for (i = 0; i < argc; i++)
594 			DoFile(savedir, argv[i]);
595 	}
596 
597 	/* Emit minimal output. */
598 	if (nfound == 0) {
599 		if (checkfor) {
600 			printf("No dump exists\n");
601 			exit(1);
602 		}
603 		syslog(LOG_WARNING, "no dumps found");
604 	}
605 	else if (nsaved == 0) {
606 		if (nerr != 0)
607 			syslog(LOG_WARNING, "unsaved dumps found but not saved");
608 		else
609 			syslog(LOG_WARNING, "no unsaved dumps found");
610 	}
611 
612 	return (0);
613 }
614