savecore.c revision 174923
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: head/sbin/savecore/savecore.c 174923 2007-12-26 11:42:10Z rwatson $");
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
95static int checkfor, compress, clear, force, keep, verbose;	/* flags */
96static int nfound, nsaved, nerr;			/* statistics */
97
98extern FILE *zopen(const char *, const char *);
99
100static void
101printheader(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
139static int
140getbounds(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
166static void
167writebounds(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 */
186static int
187check_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
229static int
230DoRegularFile(int fd, off_t dumpsize, char *buf, const char *device,
231    const char *filename, FILE *fp)
232{
233	int he, hs, nr, nw, wl;
234	off_t dmpcnt;
235
236	dmpcnt = 0;
237	he = 0;
238	while (dumpsize > 0) {
239		wl = BUFFERSIZE;
240		if (wl > dumpsize)
241			wl = dumpsize;
242		nr = read(fd, buf, wl);
243		if (nr != wl) {
244			if (nr == 0)
245				syslog(LOG_WARNING,
246				    "WARNING: EOF on dump device");
247			else
248				syslog(LOG_ERR, "read error on %s: %m", device);
249			nerr++;
250			return (-1);
251		}
252		if (compress) {
253			nw = fwrite(buf, 1, wl, fp);
254		} else {
255			for (nw = 0; nw < nr; nw = he) {
256				/* find a contiguous block of zeroes */
257				for (hs = nw; hs < nr; hs += BLOCKSIZE) {
258					for (he = hs; he < nr && buf[he] == 0;
259					    ++he)
260						/* nothing */ ;
261					/* is the hole long enough to matter? */
262					if (he >= hs + BLOCKSIZE)
263						break;
264				}
265
266				/* back down to a block boundary */
267				he &= BLOCKMASK;
268
269				/*
270				 * 1) Don't go beyond the end of the buffer.
271				 * 2) If the end of the buffer is less than
272				 *    BLOCKSIZE bytes away, we're at the end
273				 *    of the file, so just grab what's left.
274				 */
275				if (hs + BLOCKSIZE > nr)
276					hs = he = nr;
277
278				/*
279				 * At this point, we have a partial ordering:
280				 *     nw <= hs <= he <= nr
281				 * If hs > nw, buf[nw..hs] contains non-zero data.
282				 * If he > hs, buf[hs..he] is all zeroes.
283				 */
284				if (hs > nw)
285					if (fwrite(buf + nw, hs - nw, 1, fp)
286					    != 1)
287					break;
288				if (he > hs)
289					if (fseeko(fp, he - hs, SEEK_CUR) == -1)
290						break;
291			}
292		}
293		if (nw != wl) {
294			syslog(LOG_ERR,
295			    "write error on %s file: %m", filename);
296			syslog(LOG_WARNING,
297			    "WARNING: vmcore may be incomplete");
298			nerr++;
299			return (-1);
300		}
301		if (verbose) {
302			dmpcnt += wl;
303			printf("%llu\r", (unsigned long long)dmpcnt);
304			fflush(stdout);
305		}
306		dumpsize -= wl;
307	}
308	return (0);
309}
310
311/*
312 * Specialized version of dump-reading logic for use with textdumps, which
313 * are written backwards from the end of the partition, and must be reversed
314 * before being written to the file.  Textdumps are small, so do a bit less
315 * work to optimize/sparsify.
316 */
317static int
318DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
319    const char *device, const char *filename, FILE *fp)
320{
321	int nr, nw, wl;
322	off_t dmpcnt, totsize;
323
324	totsize = dumpsize;
325	dmpcnt = 0;
326	wl = 512;
327	if ((dumpsize % wl) != 0) {
328		syslog(LOG_ERR, "textdump uneven multiple of 512 on %s",
329		    device);
330		nerr++;
331		return (-1);
332	}
333	while (dumpsize > 0) {
334		nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
335		if (nr != wl) {
336			if (nr == 0)
337				syslog(LOG_WARNING,
338				    "WARNING: EOF on dump device");
339			else
340				syslog(LOG_ERR, "read error on %s: %m", device);
341			nerr++;
342			return (-1);
343		}
344		nw = fwrite(buf, 1, wl, fp);
345		if (nw != wl) {
346			syslog(LOG_ERR,
347			    "write error on %s file: %m", filename);
348			syslog(LOG_WARNING,
349			    "WARNING: textdump may be incomplete");
350			nerr++;
351			return (-1);
352		}
353		if (verbose) {
354			dmpcnt += wl;
355			printf("%llu\r", (unsigned long long)dmpcnt);
356			fflush(stdout);
357		}
358		dumpsize -= wl;
359	}
360	return (0);
361}
362
363static void
364DoFile(const char *savedir, const char *device)
365{
366	static char filename[PATH_MAX];
367	static char *buf = NULL;
368	struct kerneldumpheader kdhf, kdhl;
369	off_t mediasize, dumpsize, firsthd, lasthd;
370	FILE *info, *fp;
371	mode_t oumask;
372	int fd, fdinfo, error;
373	int bounds, status;
374	u_int sectorsize;
375	int istextdump;
376
377	bounds = getbounds();
378	mediasize = 0;
379	status = STATUS_UNKNOWN;
380
381	if (buf == NULL) {
382		buf = malloc(BUFFERSIZE);
383		if (buf == NULL) {
384			syslog(LOG_ERR, "%m");
385			return;
386		}
387	}
388
389	if (verbose)
390		printf("checking for kernel dump on device %s\n", device);
391
392	fd = open(device, O_RDWR);
393	if (fd < 0) {
394		syslog(LOG_ERR, "%s: %m", device);
395		return;
396	}
397
398	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
399	if (!error)
400		error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
401	if (error) {
402		syslog(LOG_ERR,
403		    "couldn't find media and/or sector size of %s: %m", device);
404		goto closefd;
405	}
406
407	if (verbose) {
408		printf("mediasize = %lld\n", (long long)mediasize);
409		printf("sectorsize = %u\n", sectorsize);
410	}
411
412	lasthd = mediasize - sectorsize;
413	lseek(fd, lasthd, SEEK_SET);
414	error = read(fd, &kdhl, sizeof kdhl);
415	if (error != sizeof kdhl) {
416		syslog(LOG_ERR,
417		    "error reading last dump header at offset %lld in %s: %m",
418		    (long long)lasthd, device);
419		goto closefd;
420	}
421	istextdump = 0;
422	if (memcmp(kdhl.magic, TEXTDUMPMAGIC, sizeof kdhl.magic)) {
423		if (verbose)
424			printf("textdump magic on last dump header on %s\n",
425			    device);
426		istextdump = 1;
427	} else if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic)) {
428		if (verbose)
429			printf("magic mismatch on last dump header on %s\n",
430			    device);
431
432		status = STATUS_BAD;
433		if (force == 0)
434			goto closefd;
435
436		if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
437			    sizeof kdhl.magic) == 0) {
438			if (verbose)
439				printf("forcing magic on %s\n", device);
440			memcpy(kdhl.magic, KERNELDUMPMAGIC,
441			    sizeof kdhl.magic);
442		} else {
443			syslog(LOG_ERR, "unable to force dump - bad magic");
444			goto closefd;
445		}
446	}
447	if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
448		syslog(LOG_ERR,
449		    "unknown version (%d) in last dump header on %s",
450		    dtoh32(kdhl.version), device);
451
452		status = STATUS_BAD;
453		if (force == 0)
454			goto closefd;
455	}
456
457	nfound++;
458	if (clear)
459		goto nuke;
460
461	if (kerneldump_parity(&kdhl)) {
462		syslog(LOG_ERR,
463		    "parity error on last dump header on %s", device);
464		nerr++;
465		status = STATUS_BAD;
466		if (force == 0)
467			goto closefd;
468	}
469	dumpsize = dtoh64(kdhl.dumplength);
470	firsthd = lasthd - dumpsize - sizeof kdhf;
471	lseek(fd, firsthd, SEEK_SET);
472	error = read(fd, &kdhf, sizeof kdhf);
473	if (error != sizeof kdhf) {
474		syslog(LOG_ERR,
475		    "error reading first dump header at offset %lld in %s: %m",
476		    (long long)firsthd, device);
477		nerr++;
478		goto closefd;
479	}
480
481	if (verbose >= 2) {
482		printf("First dump headers:\n");
483		printheader(stdout, &kdhf, device, bounds, -1);
484
485		printf("\nLast dump headers:\n");
486		printheader(stdout, &kdhl, device, bounds, -1);
487		printf("\n");
488	}
489
490	if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
491		syslog(LOG_ERR,
492		    "first and last dump headers disagree on %s", device);
493		nerr++;
494		status = STATUS_BAD;
495		if (force == 0)
496			goto closefd;
497	} else {
498		status = STATUS_GOOD;
499	}
500
501	if (checkfor) {
502		printf("A dump exists on %s\n", device);
503		close(fd);
504		exit(0);
505	}
506
507	if (kdhl.panicstring[0])
508		syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
509	else
510		syslog(LOG_ALERT, "reboot");
511
512	if (verbose)
513		printf("Checking for available free space\n");
514	if (!check_space(savedir, dumpsize)) {
515		nerr++;
516		goto closefd;
517	}
518
519	writebounds(bounds + 1);
520
521	sprintf(buf, "info.%d", bounds);
522
523	/*
524	 * Create or overwrite any existing dump header files.
525	 */
526	fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
527	if (fdinfo < 0) {
528		syslog(LOG_ERR, "%s: %m", buf);
529		nerr++;
530		goto closefd;
531	}
532	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
533	if (compress) {
534		sprintf(filename, "%s.%d.gz", istextdump ? "textdump.tar" :
535		    "vmcore", bounds);
536		fp = zopen(filename, "w");
537	} else {
538		sprintf(filename, "%s.%d", istextdump ? "textdump.tar" :
539		    "vmcore", bounds);
540		fp = fopen(filename, "w");
541	}
542	if (fp == NULL) {
543		syslog(LOG_ERR, "%s: %m", filename);
544		close(fdinfo);
545		nerr++;
546		goto closefd;
547	}
548	(void)umask(oumask);
549
550	info = fdopen(fdinfo, "w");
551
552	if (info == NULL) {
553		syslog(LOG_ERR, "fdopen failed: %m");
554		nerr++;
555		goto closefd;
556	}
557
558	if (verbose)
559		printheader(stdout, &kdhl, device, bounds, status);
560
561	printheader(info, &kdhl, device, bounds, status);
562	fclose(info);
563
564	syslog(LOG_NOTICE, "writing %score to %s",
565	    compress ? "compressed " : "", filename);
566
567	if (istextdump) {
568		if (DoTextdumpFile(fd, dumpsize, lasthd, buf, device,
569		    filename, fp) < 0)
570			goto closeall;
571	} else {
572		if (DoRegularFile(fd, dumpsize, buf, device, filename, fp)
573		    < 0)
574			goto closeall;
575	}
576	if (verbose)
577		printf("\n");
578
579	if (fclose(fp) < 0) {
580		syslog(LOG_ERR, "error on %s: %m", filename);
581		nerr++;
582		goto closeall;
583	}
584	nsaved++;
585
586	if (verbose)
587		printf("dump saved\n");
588
589nuke:
590	if (clear || !keep) {
591		if (verbose)
592			printf("clearing dump header\n");
593		memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
594		lseek(fd, lasthd, SEEK_SET);
595		error = write(fd, &kdhl, sizeof kdhl);
596		if (error != sizeof kdhl)
597			syslog(LOG_ERR,
598			    "error while clearing the dump header: %m");
599	}
600	close(fd);
601	return;
602
603closeall:
604	fclose(fp);
605
606closefd:
607	close(fd);
608}
609
610static void
611usage(void)
612{
613	fprintf(stderr, "%s\n%s\n%s\n",
614	    "usage: savecore -c",
615	    "       savecore -C [-v] [directory device]",
616	    "       savecore [-fkvz] [directory [device ...]]");
617	exit (1);
618}
619
620int
621main(int argc, char **argv)
622{
623	const char *savedir = ".";
624	struct fstab *fsp;
625	int i, ch, error;
626
627	checkfor = compress = clear = force = keep = verbose = 0;
628	nfound = nsaved = nerr = 0;
629
630	openlog("savecore", LOG_PERROR, LOG_DAEMON);
631
632	while ((ch = getopt(argc, argv, "Ccfkvz")) != -1)
633		switch(ch) {
634		case 'C':
635			checkfor = 1;
636			break;
637		case 'c':
638			clear = 1;
639			break;
640		case 'k':
641			keep = 1;
642			break;
643		case 'v':
644			verbose++;
645			break;
646		case 'f':
647			force = 1;
648			break;
649		case 'z':
650			compress = 1;
651			break;
652		case '?':
653		default:
654			usage();
655		}
656	if (checkfor && (clear || force || keep))
657		usage();
658	argc -= optind;
659	argv += optind;
660	if (argc >= 1) {
661		error = chdir(argv[0]);
662		if (error) {
663			syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
664			exit(1);
665		}
666		savedir = argv[0];
667		argc--;
668		argv++;
669	}
670	if (argc == 0) {
671		for (;;) {
672			fsp = getfsent();
673			if (fsp == NULL)
674				break;
675			if (strcmp(fsp->fs_vfstype, "swap") &&
676			    strcmp(fsp->fs_vfstype, "dump"))
677				continue;
678			DoFile(savedir, fsp->fs_spec);
679		}
680	} else {
681		for (i = 0; i < argc; i++)
682			DoFile(savedir, argv[i]);
683	}
684
685	/* Emit minimal output. */
686	if (nfound == 0) {
687		if (checkfor) {
688			printf("No dump exists\n");
689			exit(1);
690		}
691		syslog(LOG_WARNING, "no dumps found");
692	}
693	else if (nsaved == 0) {
694		if (nerr != 0)
695			syslog(LOG_WARNING, "unsaved dumps found but not saved");
696		else
697			syslog(LOG_WARNING, "no unsaved dumps found");
698	}
699
700	return (0);
701}
702