xref: /freebsd/sbin/savecore/savecore.c (revision bfe691b2f75de2224c7ceb304ebcdef2b42d4179)
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",
111 	    dtoh32(h->architectureversion));
112 	dumplen = dtoh64(h->dumplength);
113 	fprintf(f, "  Dump Length: %lldB (%lld MB)\n", (long long)dumplen,
114 	    (long long)(dumplen >> 20));
115 	fprintf(f, "  Blocksize: %d\n", dtoh32(h->blocksize));
116 	t = dtoh64(h->dumptime);
117 	fprintf(f, "  Dumptime: %s", ctime(&t));
118 	fprintf(f, "  Hostname: %s\n", h->hostname);
119 	fprintf(f, "  Magic: %s\n", h->magic);
120 	fprintf(f, "  Version String: %s", h->versionstring);
121 	fprintf(f, "  Panic String: %s\n", h->panicstring);
122 	fprintf(f, "  Dump Parity: %u\n", h->parity);
123 	fprintf(f, "  Bounds: %d\n", bounds);
124 
125 	switch(status) {
126 	case STATUS_BAD:
127 		stat_str = "bad";
128 		break;
129 	case STATUS_GOOD:
130 		stat_str = "good";
131 		break;
132 	default:
133 		stat_str = "unknown";
134 	}
135 	fprintf(f, "  Dump Status: %s\n", stat_str);
136 	fflush(f);
137 }
138 
139 static int
140 getbounds(void) {
141 	FILE *fp;
142 	char buf[6];
143 	int ret;
144 
145 	ret = 0;
146 
147 	if ((fp = fopen("bounds", "r")) == NULL) {
148 		if (verbose)
149 			printf("unable to open bounds file, using 0\n");
150 		return (ret);
151 	}
152 
153 	if (fgets(buf, sizeof buf, fp) == NULL) {
154 		syslog(LOG_WARNING, "unable to read from bounds, using 0");
155 		fclose(fp);
156 		return (ret);
157 	}
158 
159 	errno = 0;
160 	ret = (int)strtol(buf, NULL, 10);
161 	if (ret == 0 && (errno == EINVAL || errno == ERANGE))
162 		syslog(LOG_WARNING, "invalid value found in bounds, using 0");
163 	return (ret);
164 }
165 
166 static void
167 writebounds(int bounds) {
168 	FILE *fp;
169 
170 	if ((fp = fopen("bounds", "w")) == NULL) {
171 		syslog(LOG_WARNING, "unable to write to bounds file: %m");
172 		return;
173 	}
174 
175 	if (verbose)
176 		printf("bounds number: %d\n", bounds);
177 
178 	fprintf(fp, "%d\n", bounds);
179 	fclose(fp);
180 }
181 
182 /*
183  * Check that sufficient space is available on the disk that holds the
184  * save directory.
185  */
186 static int
187 check_space(const char *savedir, off_t dumpsize)
188 {
189 	FILE *fp;
190 	off_t minfree, spacefree, totfree, needed;
191 	struct statfs fsbuf;
192 	char buf[100], path[MAXPATHLEN];
193 
194 	if (statfs(savedir, &fsbuf) < 0) {
195 		syslog(LOG_ERR, "%s: %m", savedir);
196 		exit(1);
197 	}
198  	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
199 	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
200 
201 	(void)snprintf(path, sizeof(path), "%s/minfree", savedir);
202 	if ((fp = fopen(path, "r")) == NULL)
203 		minfree = 0;
204 	else {
205 		if (fgets(buf, sizeof(buf), fp) == NULL)
206 			minfree = 0;
207 		else
208 			minfree = atoi(buf);
209 		(void)fclose(fp);
210 	}
211 
212 	needed = dumpsize / 1024 + 2;	/* 2 for info file */
213  	if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
214 		syslog(LOG_WARNING,
215 	"no dump, not enough free space on device (%lld available, need %lld)",
216 		    (long long)(minfree > 0 ? spacefree : totfree),
217 		    (long long)needed);
218 		return (0);
219 	}
220 	if (spacefree - needed < 0)
221 		syslog(LOG_WARNING,
222 		    "dump performed, but free space threshold crossed");
223 	return (1);
224 }
225 
226 #define BLOCKSIZE (1<<12)
227 #define BLOCKMASK (~(BLOCKSIZE-1))
228 
229 static void
230 DoFile(const char *savedir, const char *device)
231 {
232 	static char *buf = NULL;
233 	struct kerneldumpheader kdhf, kdhl;
234 	off_t mediasize, dumpsize, firsthd, lasthd, dmpcnt;
235 	FILE *info, *fp;
236 	mode_t oumask;
237 	int fd, fdinfo, error, wl;
238 	int nr, nw, hs, he = 0;
239 	int bounds, status;
240 	u_int sectorsize;
241 
242 	bounds = getbounds();
243 	dmpcnt = 0;
244 	mediasize = 0;
245 	status = STATUS_UNKNOWN;
246 
247 	if (buf == NULL) {
248 		buf = malloc(BUFFERSIZE);
249 		if (buf == NULL) {
250 			syslog(LOG_ERR, "%m");
251 			return;
252 		}
253 	}
254 
255 	if (verbose)
256 		printf("checking for kernel dump on device %s\n", device);
257 
258 	fd = open(device, O_RDWR);
259 	if (fd < 0) {
260 		syslog(LOG_ERR, "%s: %m", device);
261 		return;
262 	}
263 
264 	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
265 	if (!error)
266 		error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
267 	if (error) {
268 		syslog(LOG_ERR,
269 		    "couldn't find media and/or sector size of %s: %m", device);
270 		goto closefd;
271 	}
272 
273 	if (verbose) {
274 		printf("mediasize = %lld\n", (long long)mediasize);
275 		printf("sectorsize = %u\n", sectorsize);
276 	}
277 
278 	lasthd = mediasize - sectorsize;
279 	lseek(fd, lasthd, SEEK_SET);
280 	error = read(fd, &kdhl, sizeof kdhl);
281 	if (error != sizeof kdhl) {
282 		syslog(LOG_ERR,
283 		    "error reading last dump header at offset %lld in %s: %m",
284 		    (long long)lasthd, device);
285 		goto closefd;
286 	}
287 	if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic)) {
288 		if (verbose)
289 			printf("magic mismatch on last dump header on %s\n",
290 			    device);
291 
292 		status = STATUS_BAD;
293 		if (force == 0)
294 			goto closefd;
295 
296 		if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
297 			    sizeof kdhl.magic) == 0) {
298 			if (verbose)
299 				printf("forcing magic on %s\n", device);
300 			memcpy(kdhl.magic, KERNELDUMPMAGIC,
301 			    sizeof kdhl.magic);
302 		} else {
303 			syslog(LOG_ERR, "unable to force dump - bad magic");
304 			goto closefd;
305 		}
306 	}
307 	if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
308 		syslog(LOG_ERR,
309 		    "unknown version (%d) in last dump header on %s",
310 		    dtoh32(kdhl.version), device);
311 
312 		status = STATUS_BAD;
313 		if (force == 0)
314 			goto closefd;
315 	}
316 
317 	nfound++;
318 	if (clear)
319 		goto nuke;
320 
321 	if (kerneldump_parity(&kdhl)) {
322 		syslog(LOG_ERR,
323 		    "parity error on last dump header on %s", device);
324 		nerr++;
325 		status = STATUS_BAD;
326 		if (force == 0)
327 			goto closefd;
328 	}
329 	dumpsize = dtoh64(kdhl.dumplength);
330 	firsthd = lasthd - dumpsize - sizeof kdhf;
331 	lseek(fd, firsthd, SEEK_SET);
332 	error = read(fd, &kdhf, sizeof kdhf);
333 	if (error != sizeof kdhf) {
334 		syslog(LOG_ERR,
335 		    "error reading first dump header at offset %lld in %s: %m",
336 		    (long long)firsthd, device);
337 		nerr++;
338 		goto closefd;
339 	}
340 
341 	if (verbose >= 2) {
342 		printf("First dump headers:\n");
343 		printheader(stdout, &kdhf, device, bounds, -1);
344 
345 		printf("\nLast dump headers:\n");
346 		printheader(stdout, &kdhl, device, bounds, -1);
347 		printf("\n");
348 	}
349 
350 	if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
351 		syslog(LOG_ERR,
352 		    "first and last dump headers disagree on %s", device);
353 		nerr++;
354 		status = STATUS_BAD;
355 		if (force == 0)
356 			goto closefd;
357 	} else {
358 		status = STATUS_GOOD;
359 	}
360 
361 	if (checkfor) {
362 		printf("A dump exists on %s\n", device);
363 		close(fd);
364 		exit(0);
365 	}
366 
367 	if (kdhl.panicstring[0])
368 		syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
369 	else
370 		syslog(LOG_ALERT, "reboot");
371 
372 	if (verbose)
373 		printf("Checking for available free space\n");
374 	if (!check_space(savedir, dumpsize)) {
375 		nerr++;
376 		goto closefd;
377 	}
378 
379 	writebounds(bounds + 1);
380 
381 	sprintf(buf, "info.%d", bounds);
382 
383 	/*
384 	 * Create or overwrite any existing dump header files.
385 	 */
386 	fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
387 	if (fdinfo < 0) {
388 		syslog(LOG_ERR, "%s: %m", buf);
389 		nerr++;
390 		goto closefd;
391 	}
392 	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
393 	if (compress) {
394 		sprintf(buf, "vmcore.%d.gz", bounds);
395 		fp = zopen(buf, "w");
396 	} else {
397 		sprintf(buf, "vmcore.%d", bounds);
398 		fp = fopen(buf, "w");
399 	}
400 	if (fp == NULL) {
401 		syslog(LOG_ERR, "%s: %m", buf);
402 		close(fdinfo);
403 		nerr++;
404 		goto closefd;
405 	}
406 	(void)umask(oumask);
407 
408 	info = fdopen(fdinfo, "w");
409 
410 	if (verbose)
411 		printheader(stdout, &kdhl, device, bounds, status);
412 
413 	printheader(info, &kdhl, device, bounds, status);
414 	fclose(info);
415 
416 	syslog(LOG_NOTICE, "writing %score to %s",
417 	    compress ? "compressed " : "", buf);
418 
419 	while (dumpsize > 0) {
420 		wl = BUFFERSIZE;
421 		if (wl > dumpsize)
422 			wl = dumpsize;
423 		nr = read(fd, buf, wl);
424 		if (nr != wl) {
425 			if (nr == 0)
426 				syslog(LOG_WARNING,
427 				    "WARNING: EOF on dump device");
428 			else
429 				syslog(LOG_ERR, "read error on %s: %m", device);
430 			nerr++;
431 			goto closeall;
432 		}
433 		if (compress) {
434 			nw = fwrite(buf, 1, wl, fp);
435 		} else {
436 			for (nw = 0; nw < nr; nw = he) {
437 				/* find a contiguous block of zeroes */
438 				for (hs = nw; hs < nr; hs += BLOCKSIZE) {
439 					for (he = hs; he < nr && buf[he] == 0;
440 					    ++he)
441 						/* nothing */ ;
442 					/* is the hole long enough to matter? */
443 					if (he >= hs + BLOCKSIZE)
444 						break;
445 				}
446 
447 				/* back down to a block boundary */
448 				he &= BLOCKMASK;
449 
450 				/*
451 				 * 1) Don't go beyond the end of the buffer.
452 				 * 2) If the end of the buffer is less than
453 				 *    BLOCKSIZE bytes away, we're at the end
454 				 *    of the file, so just grab what's left.
455 				 */
456 				if (hs + BLOCKSIZE > nr)
457 					hs = he = nr;
458 
459 				/*
460 				 * At this point, we have a partial ordering:
461 				 *     nw <= hs <= he <= nr
462 				 * If hs > nw, buf[nw..hs] contains non-zero data.
463 				 * If he > hs, buf[hs..he] is all zeroes.
464 				 */
465 				if (hs > nw)
466 					if (fwrite(buf + nw, hs - nw, 1, fp)
467 					    != 1)
468 					break;
469 				if (he > hs)
470 					if (fseeko(fp, he - hs, SEEK_CUR) == -1)
471 						break;
472 			}
473 		}
474 		if (nw != wl) {
475 			syslog(LOG_ERR,
476 			    "write error on vmcore.%d file: %m", bounds);
477 			syslog(LOG_WARNING,
478 			    "WARNING: vmcore may be incomplete");
479 			nerr++;
480 			goto closeall;
481 		}
482 		if (verbose) {
483 			dmpcnt += wl;
484 			printf("%llu\r", (unsigned long long)dmpcnt);
485 			fflush(stdout);
486 		}
487 		dumpsize -= wl;
488 	}
489 	if (verbose)
490 		printf("\n");
491 
492 	if (fclose(fp) < 0) {
493 		syslog(LOG_ERR, "error on vmcore.%d: %m", bounds);
494 		nerr++;
495 		goto closeall;
496 	}
497 	nsaved++;
498 
499 	if (verbose)
500 		printf("dump saved\n");
501 
502 nuke:
503 	if (clear || !keep) {
504 		if (verbose)
505 			printf("clearing dump header\n");
506 		memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
507 		lseek(fd, lasthd, SEEK_SET);
508 		error = write(fd, &kdhl, sizeof kdhl);
509 		if (error != sizeof kdhl)
510 			syslog(LOG_ERR,
511 			    "error while clearing the dump header: %m");
512 	}
513 	close(fd);
514 	return;
515 
516 closeall:
517 	fclose(fp);
518 
519 closefd:
520 	close(fd);
521 }
522 
523 static void
524 usage(void)
525 {
526 	fprintf(stderr, "%s\n%s\n%s\n",
527 	    "usage: savecore -c",
528 	    "       savecore -C [-v] [directory device]",
529 	    "       savecore [-fkvz] [directory [device ...]]");
530 	exit (1);
531 }
532 
533 int
534 main(int argc, char **argv)
535 {
536 	const char *savedir = ".";
537 	struct fstab *fsp;
538 	int i, ch, error;
539 
540 	checkfor = compress = clear = force = keep = verbose = 0;
541 	nfound = nsaved = nerr = 0;
542 
543 	openlog("savecore", LOG_PERROR, LOG_DAEMON);
544 
545 	while ((ch = getopt(argc, argv, "Ccfkvz")) != -1)
546 		switch(ch) {
547 		case 'C':
548 			checkfor = 1;
549 			break;
550 		case 'c':
551 			clear = 1;
552 			break;
553 		case 'k':
554 			keep = 1;
555 			break;
556 		case 'v':
557 			verbose++;
558 			break;
559 		case 'f':
560 			force = 1;
561 			break;
562 		case 'z':
563 			compress = 1;
564 			break;
565 		case '?':
566 		default:
567 			usage();
568 		}
569 	if (checkfor && (clear || force || keep))
570 		usage();
571 	argc -= optind;
572 	argv += optind;
573 	if (argc >= 1) {
574 		error = chdir(argv[0]);
575 		if (error) {
576 			syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
577 			exit(1);
578 		}
579 		savedir = argv[0];
580 		argc--;
581 		argv++;
582 	}
583 	if (argc == 0) {
584 		for (;;) {
585 			fsp = getfsent();
586 			if (fsp == NULL)
587 				break;
588 			if (strcmp(fsp->fs_vfstype, "swap") &&
589 			    strcmp(fsp->fs_vfstype, "dump"))
590 				continue;
591 			DoFile(savedir, fsp->fs_spec);
592 		}
593 	} else {
594 		for (i = 0; i < argc; i++)
595 			DoFile(savedir, argv[i]);
596 	}
597 
598 	/* Emit minimal output. */
599 	if (nfound == 0) {
600 		if (checkfor) {
601 			printf("No dump exists\n");
602 			exit(1);
603 		}
604 		syslog(LOG_WARNING, "no dumps found");
605 	}
606 	else if (nsaved == 0) {
607 		if (nerr != 0)
608 			syslog(LOG_WARNING, "unsaved dumps found but not saved");
609 		else
610 			syslog(LOG_WARNING, "no unsaved dumps found");
611 	}
612 
613 	return (0);
614 }
615