savecore.c revision 119734
1163953Srrs/*-
2169382Srrs * Copyright (c) 2002 Poul-Henning Kamp
3163953Srrs * Copyright (c) 2002 Networks Associates Technology, Inc.
4163953Srrs * All rights reserved.
5163953Srrs *
6163953Srrs * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7163953Srrs * and NAI Labs, the Security Research Division of Network Associates, Inc.
8163953Srrs * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9163953Srrs * DARPA CHATS research program.
10163953Srrs *
11163953Srrs * Redistribution and use in source and binary forms, with or without
12163953Srrs * modification, are permitted provided that the following conditions
13163953Srrs * are met:
14163953Srrs * 1. Redistributions of source code must retain the above copyright
15163953Srrs *    notice, this list of conditions and the following disclaimer.
16163953Srrs * 2. Redistributions in binary form must reproduce the above copyright
17163953Srrs *    notice, this list of conditions and the following disclaimer in the
18163953Srrs *    documentation and/or other materials provided with the distribution.
19163953Srrs * 3. The names of the authors may not be used to endorse or promote
20163953Srrs *    products derived from this software without specific prior written
21163953Srrs *    permission.
22163953Srrs *
23163953Srrs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24163953Srrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25163953Srrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26163953Srrs * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27163953Srrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28163953Srrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29163953Srrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30163953Srrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31163953Srrs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32163953Srrs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33163953Srrs * SUCH DAMAGE.
34163953Srrs *
35163953Srrs * Copyright (c) 1986, 1992, 1993
36163953Srrs *	The Regents of the University of California.  All rights reserved.
37163953Srrs *
38163953Srrs * Redistribution and use in source and binary forms, with or without
39163953Srrs * modification, are permitted provided that the following conditions
40163953Srrs * are met:
41163953Srrs * 1. Redistributions of source code must retain the above copyright
42167598Srrs *    notice, this list of conditions and the following disclaimer.
43167598Srrs * 2. Redistributions in binary form must reproduce the above copyright
44167598Srrs *    notice, this list of conditions and the following disclaimer in the
45167598Srrs *    documentation and/or other materials provided with the distribution.
46167598Srrs * 3. All advertising materials mentioning features or use of this software
47167598Srrs *    must display the following acknowledgement:
48167598Srrs *	This product includes software developed by the University of
49167598Srrs *	California, Berkeley and its contributors.
50167598Srrs * 4. Neither the name of the University nor the names of its contributors
51167598Srrs *    may be used to endorse or promote products derived from this software
52167598Srrs *    without specific prior written permission.
53167598Srrs *
54167598Srrs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55167598Srrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56167598Srrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57167598Srrs * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58167598Srrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59167598Srrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60167598Srrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61167598Srrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62167598Srrs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63167598Srrs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64167598Srrs * SUCH DAMAGE.
65167598Srrs */
66167598Srrs
67167598Srrs#include <sys/cdefs.h>
68167598Srrs__FBSDID("$FreeBSD: head/sbin/savecore/savecore.c 119734 2003-09-04 10:07:01Z dougb $");
69168299Srrs
70167598Srrs#include <sys/param.h>
71167598Srrs#include <sys/disk.h>
72171990Srrs#include <sys/kerneldump.h>
73171990Srrs#include <sys/param.h>
74171990Srrs#include <sys/mount.h>
75171990Srrs#include <sys/stat.h>
76167598Srrs#include <errno.h>
77172090Srrs#include <fcntl.h>
78172090Srrs#include <fstab.h>
79172090Srrs#include <paths.h>
80172090Srrs#include <stdarg.h>
81172090Srrs#include <stdio.h>
82172090Srrs#include <stdlib.h>
83163953Srrs#include <string.h>
84163953Srrs#include <syslog.h>
85163953Srrs#include <time.h>
86169352Srrs#include <unistd.h>
87170181Srrs
88163953Srrs/* The size of the buffer used for I/O. */
89163953Srrs#define	BUFFERSIZE	(1024*1024)
90163953Srrs
91163953Srrsint checkfor, compress, clear, force, keep, verbose;	/* flags */
92163953Srrsint nfound, nsaved, nerr;			/* statistics */
93163953Srrs
94163953Srrsextern FILE *zopen(const char *, const char *);
95163953Srrs
96163953Srrsstatic void
97163953Srrsprintheader(FILE *f, const struct kerneldumpheader *h, const char *device,
98169378Srrs    int bounds)
99169378Srrs{
100163953Srrs	uint64_t dumplen;
101163953Srrs	time_t t;
102163953Srrs
103163953Srrs	fprintf(f, "Good dump found on device %s\n", device);
104163953Srrs	fprintf(f, "  Architecture: %s\n", h->architecture);
105169378Srrs	fprintf(f, "  Architecture version: %d\n",
106163953Srrs	    dtoh32(h->architectureversion));
107169378Srrs	dumplen = dtoh64(h->dumplength);
108163953Srrs	fprintf(f, "  Dump length: %lldB (%lld MB)\n", (long long)dumplen,
109169378Srrs	    (long long)(dumplen >> 20));
110163953Srrs	fprintf(f, "  Blocksize: %d\n", dtoh32(h->blocksize));
111169378Srrs	t = dtoh64(h->dumptime);
112169352Srrs	fprintf(f, "  Dumptime: %s", ctime(&t));
113170181Srrs	fprintf(f, "  Hostname: %s\n", h->hostname);
114163953Srrs	fprintf(f, "  Versionstring: %s", h->versionstring);
115172190Srrs	fprintf(f, "  Panicstring: %s\n", h->panicstring);
116163953Srrs	fprintf(f, "  Bounds: %d\n", bounds);
117171990Srrs	fflush(f);
118163953Srrs}
119163953Srrs
120163953Srrsstatic int
121163953Srrsgetbounds(void) {
122163953Srrs	FILE *fp;
123163953Srrs	char buf[6];
124163953Srrs	int ret;
125163953Srrs
126163953Srrs	ret = 0;
127163953Srrs
128163953Srrs	if ((fp = fopen("bounds", "r")) == NULL) {
129163953Srrs		syslog(LOG_WARNING, "unable to open bounds file, using 0");
130163953Srrs		goto newfile;
131163953Srrs	}
132163953Srrs
133163953Srrs	if (fgets(buf, sizeof buf, fp) == NULL) {
134163953Srrs		syslog(LOG_WARNING, "unable to read from bounds, using 0");
135163953Srrs		fclose(fp);
136172090Srrs		goto newfile;
137172090Srrs	}
138172090Srrs
139172090Srrs	errno = 0;
140172090Srrs	ret = (int)strtol(buf, NULL, 10);
141172090Srrs	if (ret == 0 && (errno == EINVAL || errno == ERANGE))
142172090Srrs		syslog(LOG_WARNING, "invalid value found in bounds, using 0");
143172090Srrs
144172090Srrsnewfile:
145172090Srrs
146172090Srrs	if ((fp = fopen("bounds", "w")) == NULL) {
147172090Srrs		syslog(LOG_WARNING, "unable to write to bounds file: %m");
148163953Srrs		goto done;
149163953Srrs	}
150163953Srrs
151163953Srrs	if (verbose)
152163953Srrs		printf("bounds number: %d\n", ret);
153163953Srrs
154163953Srrs	fprintf(fp, "%d\n", (ret + 1));
155163953Srrs	fclose(fp);
156163953Srrs
157163953Srrsdone:
158163953Srrs	return (ret);
159163953Srrs}
160163953Srrs
161163953Srrs/*
162163953Srrs * Check that sufficient space is available on the disk that holds the
163163953Srrs * save directory.
164163953Srrs */
165163953Srrsstatic int
166163953Srrscheck_space(char *savedir, off_t dumpsize)
167163953Srrs{
168163953Srrs	FILE *fp;
169163953Srrs	off_t minfree, spacefree, totfree, needed;
170163953Srrs	struct statfs fsbuf;
171163953Srrs	char buf[100], path[MAXPATHLEN];
172163953Srrs
173163953Srrs	if (statfs(savedir, &fsbuf) < 0) {
174163953Srrs		syslog(LOG_ERR, "%s: %m", savedir);
175163953Srrs		exit(1);
176163953Srrs	}
177163953Srrs 	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
178163953Srrs	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
179163953Srrs
180163953Srrs	(void)snprintf(path, sizeof(path), "%s/minfree", savedir);
181163953Srrs	if ((fp = fopen(path, "r")) == NULL)
182163953Srrs		minfree = 0;
183163953Srrs	else {
184163953Srrs		if (fgets(buf, sizeof(buf), fp) == NULL)
185163953Srrs			minfree = 0;
186163953Srrs		else
187163953Srrs			minfree = atoi(buf);
188163953Srrs		(void)fclose(fp);
189163953Srrs	}
190163953Srrs
191163953Srrs	needed = dumpsize / 1024 + 2;	/* 2 for info file */
192163953Srrs 	if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
193163953Srrs		syslog(LOG_WARNING,
194163953Srrs	"no dump, not enough free space on device (%lld available, need %lld)",
195163953Srrs		    (long long)(minfree > 0 ? spacefree : totfree),
196163953Srrs		    (long long)needed);
197163953Srrs		return (0);
198163953Srrs	}
199163953Srrs	if (spacefree - needed < 0)
200170181Srrs		syslog(LOG_WARNING,
201163953Srrs		    "dump performed, but free space threshold crossed");
202170181Srrs	return (1);
203163953Srrs}
204163953Srrs
205163953Srrs#define BLOCKSIZE (1<<12)
206163953Srrs#define BLOCKMASK (~(BLOCKSIZE-1))
207163953Srrs
208163953Srrsstatic void
209163953SrrsDoFile(char *savedir, const char *device)
210163953Srrs{
211163953Srrs	static char *buf = NULL;
212163953Srrs	struct kerneldumpheader kdhf, kdhl;
213163953Srrs	off_t mediasize, dumpsize, firsthd, lasthd, dmpcnt;
214163953Srrs	FILE *info, *fp;
215163953Srrs	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 one-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 (checkfor) {
325		printf("A dump exists on %s\n", device);
326		close(fd);
327		exit(0);
328	}
329
330	if (kdhl.panicstring[0])
331		syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
332	else
333		syslog(LOG_ALERT, "reboot");
334
335	if (verbose)
336		printf("Checking for available free space\n");
337	if (!check_space(savedir, dumpsize)) {
338		nerr++;
339		goto closefd;
340	}
341
342	bounds = getbounds();
343
344	sprintf(buf, "info.%d", bounds);
345
346	/*
347	 * Create or overwrite any existing files.
348	 */
349	fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
350	if (fdinfo < 0) {
351		syslog(LOG_ERR, "%s: %m", buf);
352		nerr++;
353		goto closefd;
354	}
355	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
356	if (compress) {
357		sprintf(buf, "vmcore.%d.gz", bounds);
358		fp = zopen(buf, "w");
359	} else {
360		sprintf(buf, "vmcore.%d", bounds);
361		fp = fopen(buf, "w");
362	}
363	if (fp == NULL) {
364		syslog(LOG_ERR, "%s: %m", buf);
365		close(fdinfo);
366		nerr++;
367		goto closefd;
368	}
369	(void)umask(oumask);
370
371	info = fdopen(fdinfo, "w");
372
373	if (verbose)
374		printheader(stdout, &kdhl, device, bounds);
375
376	printheader(info, &kdhl, device, bounds);
377	fclose(info);
378
379	syslog(LOG_NOTICE, "writing %score to %s",
380	    compress ? "compressed " : "", buf);
381
382	while (dumpsize > 0) {
383		wl = BUFFERSIZE;
384		if (wl > dumpsize)
385			wl = dumpsize;
386		nr = read(fd, buf, wl);
387		if (nr != wl) {
388			if (nr == 0)
389				syslog(LOG_WARNING,
390				    "WARNING: EOF on dump device");
391			else
392				syslog(LOG_ERR, "read error on %s: %m", device);
393			nerr++;
394			goto closeall;
395		}
396		if (compress) {
397			nw = fwrite(buf, 1, wl, fp);
398		} else {
399			for (nw = 0; nw < nr; nw = he) {
400			    /* find a contiguous block of zeroes */
401			    for (hs = nw; hs < nr; hs += BLOCKSIZE) {
402				for (he = hs; he < nr && buf[he] == 0; ++he)
403				    /* nothing */ ;
404				/* is the hole long enough to matter? */
405				if (he >= hs + BLOCKSIZE)
406				    break;
407			    }
408
409			    /* back down to a block boundary */
410			    he &= BLOCKMASK;
411
412			    /*
413			     * 1) Don't go beyond the end of the buffer.
414			     * 2) If the end of the buffer is less than
415			     *    BLOCKSIZE bytes away, we're at the end
416			     *    of the file, so just grab what's left.
417			     */
418			    if (hs + BLOCKSIZE > nr)
419				hs = he = nr;
420
421			    /*
422			     * At this point, we have a partial ordering:
423			     *     nw <= hs <= he <= nr
424			     * If hs > nw, buf[nw..hs] contains non-zero data.
425			     * If he > hs, buf[hs..he] is all zeroes.
426			     */
427			    if (hs > nw)
428				if (fwrite(buf + nw, hs - nw, 1, fp) != 1)
429				    break;
430			    if (he > hs)
431				if (fseek(fp, he - hs, SEEK_CUR) == -1)
432				    break;
433			}
434		}
435		if (nw != wl) {
436			syslog(LOG_ERR,
437			    "write error on vmcore.%d file: %m", bounds);
438			syslog(LOG_WARNING,
439			    "WARNING: vmcore may be incomplete");
440			nerr++;
441			goto closeall;
442		}
443		if (verbose) {
444			dmpcnt += wl;
445			printf("%llu\r", (unsigned long long)dmpcnt);
446			fflush(stdout);
447		}
448		dumpsize -= wl;
449	}
450	if (verbose)
451		printf("\n");
452
453	if (fclose(fp) < 0) {
454		syslog(LOG_ERR, "error on vmcore.%d: %m", bounds);
455		nerr++;
456		goto closeall;
457	}
458	nsaved++;
459
460	if (verbose)
461		printf("dump saved\n");
462
463nuke:
464	if (clear || !keep) {
465		if (verbose)
466			printf("clearing dump header\n");
467		memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
468		lseek(fd, lasthd, SEEK_SET);
469		error = write(fd, &kdhl, sizeof kdhl);
470		if (error != sizeof kdhl)
471			syslog(LOG_ERR,
472			    "error while clearing the dump header: %m");
473	}
474	close(fd);
475	return;
476
477closeall:
478	fclose(fp);
479
480closefd:
481	close(fd);
482}
483
484static void
485usage(void)
486{
487	fprintf(stderr, "usage: savecore [-Cv|-cfkv] [directory [device...]]\n");
488	exit (1);
489}
490
491int
492main(int argc, char **argv)
493{
494	int i, ch, error;
495	struct fstab *fsp;
496	char *savedir;
497
498	openlog("savecore", LOG_PERROR, LOG_DAEMON);
499
500	savedir = strdup(".");
501	if (savedir == NULL) {
502		syslog(LOG_ERR, "Cannot allocate memory");
503		exit(1);
504	}
505	while ((ch = getopt(argc, argv, "CcdfkN:vz")) != -1)
506		switch(ch) {
507		case 'C':
508			checkfor = 1;
509			break;
510		case 'c':
511			clear = 1;
512			break;
513		case 'k':
514			keep = 1;
515			break;
516		case 'v':
517			verbose = 1;
518			break;
519		case 'f':
520			force = 1;
521			break;
522		case 'z':
523			compress = 1;
524			break;
525		case 'd':	/* Obsolete */
526		case 'N':
527		case '?':
528		default:
529			usage();
530		}
531	if (checkfor && (clear || force || keep))
532		usage();
533	argc -= optind;
534	argv += optind;
535	if (argc >= 1) {
536		error = chdir(argv[0]);
537		if (error) {
538			syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
539			exit(1);
540		}
541		savedir = argv[0];
542		argc--;
543		argv++;
544	}
545	if (argc == 0) {
546		for (;;) {
547			fsp = getfsent();
548			if (fsp == NULL)
549				break;
550			if (strcmp(fsp->fs_vfstype, "swap") &&
551			    strcmp(fsp->fs_vfstype, "dump"))
552				continue;
553			DoFile(savedir, fsp->fs_spec);
554		}
555	} else {
556		for (i = 0; i < argc; i++)
557			DoFile(savedir, argv[i]);
558	}
559
560	/* Emit minimal output. */
561	if (nfound == 0) {
562		if (checkfor) {
563			printf("No dump exists\n");
564			exit(1);
565		}
566		syslog(LOG_WARNING, "no dumps found");
567	}
568	else if (nsaved == 0) {
569		if (nerr != 0)
570			syslog(LOG_WARNING, "unsaved dumps found but not saved");
571		else
572			syslog(LOG_WARNING, "no unsaved dumps found");
573	}
574
575	return (0);
576}
577