savecore.c revision 97746
1178825Sdfr/*-
2233294Sstas * Copyright (c) 2002 Poul-Henning Kamp
3233294Sstas * Copyright (c) 2002 Networks Associates Technology, Inc.
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7233294Sstas * and NAI Labs, the Security Research Division of Network Associates, Inc.
8233294Sstas * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9178825Sdfr * DARPA CHATS research program.
10233294Sstas *
11233294Sstas * Redistribution and use in source and binary forms, with or without
12178825Sdfr * modification, are permitted provided that the following conditions
13233294Sstas * are met:
14233294Sstas * 1. Redistributions of source code must retain the above copyright
15233294Sstas *    notice, this list of conditions and the following disclaimer.
16178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
17233294Sstas *    notice, this list of conditions and the following disclaimer in the
18233294Sstas *    documentation and/or other materials provided with the distribution.
19233294Sstas * 3. The names of the authors may not be used to endorse or promote
20178825Sdfr *    products derived from this software without specific prior written
21233294Sstas *    permission.
22233294Sstas *
23233294Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32178825Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33178825Sdfr * SUCH DAMAGE.
34178825Sdfr *
35233294Sstas * Copyright (c) 1986, 1992, 1993
36178825Sdfr *	The Regents of the University of California.  All rights reserved.
37178825Sdfr *
38178825Sdfr * Redistribution and use in source and binary forms, with or without
39178825Sdfr * modification, are permitted provided that the following conditions
40178825Sdfr * are met:
41178825Sdfr * 1. Redistributions of source code must retain the above copyright
42178825Sdfr *    notice, this list of conditions and the following disclaimer.
43178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
44178825Sdfr *    notice, this list of conditions and the following disclaimer in the
45178825Sdfr *    documentation and/or other materials provided with the distribution.
46178825Sdfr * 3. All advertising materials mentioning features or use of this software
47178825Sdfr *    must display the following acknowledgement:
48178825Sdfr *	This product includes software developed by the University of
49178825Sdfr *	California, Berkeley and its contributors.
50178825Sdfr * 4. Neither the name of the University nor the names of its contributors
51178825Sdfr *    may be used to endorse or promote products derived from this software
52178825Sdfr *    without specific prior written permission.
53178825Sdfr *
54178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59178825Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60178825Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64233294Sstas * SUCH DAMAGE.
65233294Sstas */
66233294Sstas
67178825Sdfr#include <sys/cdefs.h>
68178825Sdfr__FBSDID("$FreeBSD: head/sbin/savecore/savecore.c 97746 2002-06-02 19:20:37Z marcel $");
69178825Sdfr
70178825Sdfr#include <sys/param.h>
71178825Sdfr#include <sys/disk.h>
72178825Sdfr#include <sys/kerneldump.h>
73178825Sdfr#include <sys/param.h>
74233294Sstas#include <sys/mount.h>
75178825Sdfr#include <sys/stat.h>
76178825Sdfr#include <errno.h>
77233294Sstas#include <fcntl.h>
78233294Sstas#include <fstab.h>
79233294Sstas#include <paths.h>
80178825Sdfr#include <stdarg.h>
81178825Sdfr#include <stdio.h>
82178825Sdfr#include <stdlib.h>
83178825Sdfr#include <string.h>
84178825Sdfr#include <syslog.h>
85178825Sdfr#include <time.h>
86178825Sdfr#include <unistd.h>
87178825Sdfr
88178825Sdfr/* The size of the buffer used for I/O. */
89178825Sdfr#define	BUFFERSIZE	(1024*1024)
90178825Sdfr
91178825Sdfrint compress, clear, force, keep, verbose;	/* flags */
92178825Sdfrint nfound, nsaved, nerr;			/* statistics */
93233294Sstas
94178825Sdfrextern FILE *zopen(const char *, const char *);
95178825Sdfr
96178825Sdfrstatic void
97178825Sdfrprintheader(FILE *f, const struct kerneldumpheader *h, const char *device,
98178825Sdfr    int bounds)
99233294Sstas{
100178825Sdfr	uint64_t dumplen;
101178825Sdfr	time_t t;
102178825Sdfr
103178825Sdfr	fprintf(f, "Good dump found on device %s\n", device);
104178825Sdfr	fprintf(f, "  Architecture: %s\n", h->architecture);
105178825Sdfr	fprintf(f, "  Architecture version: %d\n",
106178825Sdfr	    dtoh32(h->architectureversion));
107178825Sdfr	dumplen = dtoh64(h->dumplength);
108178825Sdfr	fprintf(f, "  Dump length: %lldB (%lld MB)\n", (long long)dumplen,
109233294Sstas	    (long long)(dumplen >> 20));
110178825Sdfr	fprintf(f, "  Blocksize: %d\n", dtoh32(h->blocksize));
111178825Sdfr	t = dtoh64(h->dumptime);
112178825Sdfr	fprintf(f, "  Dumptime: %s", ctime(&t));
113178825Sdfr	fprintf(f, "  Hostname: %s\n", h->hostname);
114178825Sdfr	fprintf(f, "  Versionstring: %s", h->versionstring);
115178825Sdfr	fprintf(f, "  Panicstring: %s\n", h->panicstring);
116178825Sdfr	fprintf(f, "  Bounds: %d\n", bounds);
117178825Sdfr	fflush(f);
118178825Sdfr}
119178825Sdfr
120178825Sdfrstatic int
121178825Sdfrgetbounds(void) {
122178825Sdfr	FILE *fp;
123178825Sdfr	char buf[6];
124178825Sdfr	int ret;
125178825Sdfr
126178825Sdfr	ret = 0;
127178825Sdfr
128178825Sdfr	if ((fp = fopen("bounds", "r")) == NULL) {
129178825Sdfr		syslog(LOG_WARNING, "unable to open bounds file, using 0");
130178825Sdfr		goto newfile;
131178825Sdfr	}
132178825Sdfr
133178825Sdfr	if (fgets(buf, sizeof buf, fp) == NULL) {
134178825Sdfr		syslog(LOG_WARNING, "unable to read from bounds, using 0");
135178825Sdfr		fclose(fp);
136178825Sdfr		goto newfile;
137178825Sdfr	}
138178825Sdfr
139178825Sdfr	errno = 0;
140178825Sdfr	ret = (int)strtol(buf, NULL, 10);
141178825Sdfr	if (ret == 0 && (errno == EINVAL || errno == ERANGE))
142178825Sdfr		syslog(LOG_WARNING, "invalid value found in bounds, using 0");
143178825Sdfr
144178825Sdfrnewfile:
145178825Sdfr
146178825Sdfr	if ((fp = fopen("bounds", "w")) == NULL) {
147178825Sdfr		syslog(LOG_WARNING, "unable to write to bounds file: %m");
148178825Sdfr		goto done;
149178825Sdfr	}
150178825Sdfr
151178825Sdfr	if (verbose)
152178825Sdfr		printf("bounds number: %d\n", ret);
153178825Sdfr
154178825Sdfr	fprintf(fp, "%d\n", (ret + 1));
155178825Sdfr	fclose(fp);
156178825Sdfr
157178825Sdfrdone:
158178825Sdfr	return (ret);
159178825Sdfr}
160233294Sstas
161178825Sdfr/*
162178825Sdfr * Check that sufficient space is available on the disk that holds the
163178825Sdfr * save directory.
164178825Sdfr */
165178825Sdfrstatic int
166178825Sdfrcheck_space(char *savedir, off_t dumpsize)
167178825Sdfr{
168233294Sstas	FILE *fp;
169233294Sstas	off_t minfree, spacefree, totfree, needed;
170233294Sstas	struct statfs fsbuf;
171178825Sdfr	char buf[100], path[MAXPATHLEN];
172178825Sdfr
173178825Sdfr	if (statfs(savedir, &fsbuf) < 0) {
174178825Sdfr		syslog(LOG_ERR, "%s: %m", savedir);
175178825Sdfr		exit(1);
176178825Sdfr	}
177178825Sdfr 	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
178233294Sstas	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
179178825Sdfr
180178825Sdfr	(void)snprintf(path, sizeof(path), "%s/minfree", savedir);
181178825Sdfr	if ((fp = fopen(path, "r")) == NULL)
182178825Sdfr		minfree = 0;
183233294Sstas	else {
184178825Sdfr		if (fgets(buf, sizeof(buf), fp) == NULL)
185178825Sdfr			minfree = 0;
186178825Sdfr		else
187178825Sdfr			minfree = atoi(buf);
188178825Sdfr		(void)fclose(fp);
189233294Sstas	}
190178825Sdfr
191178825Sdfr	needed = dumpsize / 1024 + 2;	/* 2 for info file */
192178825Sdfr 	if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
193178825Sdfr		syslog(LOG_WARNING,
194233294Sstas	"no dump, not enough free space on device (%lld available, need %lld)",
195178825Sdfr		    (long long)(minfree > 0 ? spacefree : totfree),
196178825Sdfr		    (long long)needed);
197178825Sdfr		return (0);
198178825Sdfr	}
199178825Sdfr	if (spacefree - needed < 0)
200178825Sdfr		syslog(LOG_WARNING,
201178825Sdfr		    "dump performed, but free space threshold crossed");
202178825Sdfr	return (1);
203178825Sdfr}
204178825Sdfr
205178825Sdfr#define BLOCKSIZE (1<<12)
206178825Sdfr#define BLOCKMASK (~(BLOCKSIZE-1))
207233294Sstas
208178825Sdfrstatic void
209233294SstasDoFile(char *savedir, const char *device)
210233294Sstas{
211233294Sstas	static char *buf = NULL;
212178825Sdfr	struct kerneldumpheader kdhf, kdhl;
213	off_t mediasize, dumpsize, firsthd, lasthd, dmpcnt;
214	FILE *info, *fp;
215	int fd, fdinfo, error, wl;
216	int nr, nw, hs, he;
217	int bounds;
218	u_int sectorsize;
219	mode_t oumask;
220
221	dmpcnt = 0;
222	mediasize = 0;
223
224	/*
225	 * XXX On ia64 something breaks when the buffer is put on the
226	 * stack. When the buffer is roughly larger than 128K the read()
227	 * below simply fails with errno=14 (EFAULT). We work around
228	 * this by doing a on-time allocation...
229	 */
230	if (buf == NULL) {
231		buf = malloc(BUFFERSIZE);
232		if (buf == NULL) {
233			syslog(LOG_ERR, "%m");
234			return;
235		}
236	}
237
238	if (verbose)
239		printf("checking for kernel dump on device %s\n", device);
240
241	fd = open(device, O_RDWR);
242	if (fd < 0) {
243		syslog(LOG_ERR, "%s: %m", device);
244		return;
245	}
246
247	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
248	if (!error)
249		error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
250	if (error) {
251		syslog(LOG_ERR,
252		    "couldn't find media and/or sector size of %s: %m", device);
253		goto closefd;
254	}
255
256	if (verbose) {
257		printf("mediasize = %lld\n", (long long)mediasize);
258		printf("sectorsize = %u\n", sectorsize);
259	}
260
261	lasthd = mediasize - sectorsize;
262	lseek(fd, lasthd, SEEK_SET);
263	error = read(fd, &kdhl, sizeof kdhl);
264	if (error != sizeof kdhl) {
265		syslog(LOG_ERR,
266		    "error reading last dump header at offset %lld in %s: %m",
267		    (long long)lasthd, device);
268		goto closefd;
269	}
270	if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic)) {
271		if (verbose)
272			printf("magic mismatch on last dump header on %s\n",
273			    device);
274
275		if (force == 0)
276			goto closefd;
277
278		if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
279			    sizeof kdhl.magic) == 0) {
280			if (verbose)
281				printf("forcing magic on %s\n", device);
282			memcpy(kdhl.magic, KERNELDUMPMAGIC,
283			    sizeof kdhl.magic);
284		} else {
285			syslog(LOG_ERR, "unable to force dump - bad magic");
286			goto closefd;
287		}
288	}
289	if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
290		syslog(LOG_ERR,
291		    "unknown version (%d) in last dump header on %s",
292		    dtoh32(kdhl.version), device);
293		goto closefd;
294	}
295
296	nfound++;
297	if (clear)
298		goto nuke;
299
300	if (kerneldump_parity(&kdhl)) {
301		syslog(LOG_ERR,
302		    "parity error on last dump header on %s", device);
303		nerr++;
304		goto closefd;
305	}
306	dumpsize = dtoh64(kdhl.dumplength);
307	firsthd = lasthd - dumpsize - sizeof kdhf;
308	lseek(fd, firsthd, SEEK_SET);
309	error = read(fd, &kdhf, sizeof kdhf);
310	if (error != sizeof kdhf) {
311		syslog(LOG_ERR,
312		    "error reading first dump header at offset %lld in %s: %m",
313		    (long long)firsthd, device);
314		nerr++;
315		goto closefd;
316	}
317	if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
318		syslog(LOG_ERR,
319		    "first and last dump headers disagree on %s", device);
320		nerr++;
321		goto closefd;
322	}
323
324	if (kdhl.panicstring[0])
325		syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
326	else
327		syslog(LOG_ALERT, "reboot");
328
329	if (verbose)
330		printf("Checking for available free space\n");
331	if (!check_space(savedir, dumpsize)) {
332		nerr++;
333		goto closefd;
334	}
335
336	bounds = getbounds();
337
338	sprintf(buf, "info.%d", bounds);
339
340	/*
341	 * Create or overwrite any existing files.
342	 */
343	fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
344	if (fdinfo < 0) {
345		syslog(LOG_ERR, "%s: %m", buf);
346		nerr++;
347		goto closefd;
348	}
349	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
350	if (compress) {
351		sprintf(buf, "vmcore.%d.gz", bounds);
352		fp = zopen(buf, "w");
353	} else {
354		sprintf(buf, "vmcore.%d", bounds);
355		fp = fopen(buf, "w");
356	}
357	if (fp == NULL) {
358		syslog(LOG_ERR, "%s: %m", buf);
359		close(fdinfo);
360		nerr++;
361		goto closefd;
362	}
363	(void)umask(oumask);
364
365	info = fdopen(fdinfo, "w");
366
367	if (verbose)
368		printheader(stdout, &kdhl, device, bounds);
369
370	printheader(info, &kdhl, device, bounds);
371	fclose(info);
372
373	syslog(LOG_NOTICE, "writing %score to %s",
374	    compress ? "compressed " : "", buf);
375
376	while (dumpsize > 0) {
377		wl = BUFFERSIZE;
378		if (wl > dumpsize)
379			wl = dumpsize;
380		nr = read(fd, buf, wl);
381		if (nr != wl) {
382			if (nr == 0)
383				syslog(LOG_WARNING,
384				    "WARNING: EOF on dump device");
385			else
386				syslog(LOG_ERR, "read error on %s: %m", device);
387			nerr++;
388			goto closeall;
389		}
390		if (compress) {
391			nw = fwrite(buf, 1, wl, fp);
392		} else {
393			for (nw = 0; nw < nr; nw = he) {
394			    /* find a contiguous block of zeroes */
395			    for (hs = nw; hs < nr; hs += BLOCKSIZE) {
396				for (he = hs; he < nr && buf[he] == 0; ++he)
397				    /* nothing */ ;
398				/* is the hole long enough to matter? */
399				if (he >= hs + BLOCKSIZE)
400				    break;
401			    }
402
403			    /* back down to a block boundary */
404			    he &= BLOCKMASK;
405
406			    /*
407			     * 1) Don't go beyond the end of the buffer.
408			     * 2) If the end of the buffer is less than
409			     *    BLOCKSIZE bytes away, we're at the end
410			     *    of the file, so just grab what's left.
411			     */
412			    if (hs + BLOCKSIZE > nr)
413				hs = he = nr;
414
415			    /*
416			     * At this point, we have a partial ordering:
417			     *     nw <= hs <= he <= nr
418			     * If hs > nw, buf[nw..hs] contains non-zero data.
419			     * If he > hs, buf[hs..he] is all zeroes.
420			     */
421			    if (hs > nw)
422				if (fwrite(buf + nw, hs - nw, 1, fp) != 1)
423				    break;
424			    if (he > hs)
425				if (fseek(fp, he - hs, SEEK_CUR) == -1)
426				    break;
427			}
428		}
429		if (nw != wl) {
430			syslog(LOG_ERR,
431			    "write error on vmcore.%d file: %m", bounds);
432			syslog(LOG_WARNING,
433			    "WARNING: vmcore may be incomplete");
434			nerr++;
435			goto closeall;
436		}
437		if (verbose) {
438			dmpcnt += wl;
439			printf("%llu\r", (unsigned long long)dmpcnt);
440			fflush(stdout);
441		}
442		dumpsize -= wl;
443	}
444	if (verbose)
445		printf("\n");
446
447	if (fclose(fp) < 0) {
448		syslog(LOG_ERR, "error on vmcore.%d: %m", bounds);
449		nerr++;
450		goto closeall;
451	}
452	nsaved++;
453
454	if (verbose)
455		printf("dump saved\n");
456
457nuke:
458	if (clear || !keep) {
459		if (verbose)
460			printf("clearing dump header\n");
461		memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
462		lseek(fd, lasthd, SEEK_SET);
463		error = write(fd, &kdhl, sizeof kdhl);
464		if (error != sizeof kdhl)
465			syslog(LOG_ERR,
466			    "error while clearing the dump header: %m");
467	}
468	close(fd);
469	return;
470
471closeall:
472	fclose(fp);
473
474closefd:
475	close(fd);
476}
477
478static void
479usage(void)
480{
481	fprintf(stderr, "usage: savecore [-cfkv] [directory [device...]]\n");
482	exit (1);
483}
484
485int
486main(int argc, char **argv)
487{
488	int i, ch, error;
489	struct fstab *fsp;
490	char *savedir;
491
492	openlog("savecore", LOG_PERROR, LOG_DAEMON);
493
494	savedir = strdup(".");
495	if (savedir == NULL) {
496		syslog(LOG_ERR, "Cannot allocate memory");
497		exit(1);
498	}
499	while ((ch = getopt(argc, argv, "cdfkN:vz")) != -1)
500		switch(ch) {
501		case 'c':
502			clear = 1;
503			break;
504		case 'k':
505			keep = 1;
506			break;
507		case 'v':
508			verbose = 1;
509			break;
510		case 'f':
511			force = 1;
512			break;
513		case 'z':
514			compress = 1;
515			break;
516		case 'd':	/* Obsolete */
517		case 'N':
518		case '?':
519		default:
520			usage();
521		}
522	argc -= optind;
523	argv += optind;
524	if (argc >= 1) {
525		error = chdir(argv[0]);
526		if (error) {
527			syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
528			exit(1);
529		}
530		savedir = argv[0];
531		argc--;
532		argv++;
533	}
534	if (argc == 0) {
535		for (;;) {
536			fsp = getfsent();
537			if (fsp == NULL)
538				break;
539			if (strcmp(fsp->fs_vfstype, "swap") &&
540			    strcmp(fsp->fs_vfstype, "dump"))
541				continue;
542			DoFile(savedir, fsp->fs_spec);
543		}
544	} else {
545		for (i = 0; i < argc; i++)
546			DoFile(savedir, argv[i]);
547	}
548
549	/* Emit minimal output. */
550	if (nfound == 0)
551		syslog(LOG_WARNING, "no dumps found");
552	else if (nsaved == 0) {
553		if (nerr != 0)
554			syslog(LOG_WARNING, "unsaved dumps found but not saved");
555		else
556			syslog(LOG_WARNING, "no unsaved dumps found");
557	}
558
559	return (0);
560}
561