2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
35 * Copyright (c) 1986, 1992, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
66 #include <sys/param.h>
68 #include <sys/kerneldump.h>
69 #include <sys/mount.h>
84 /* The size of the buffer used for I/O. */
85 #define BUFFERSIZE (1024*1024)
89 #define STATUS_UNKNOWN 2
91 static int checkfor, compress, clear, force, keep, verbose; /* flags */
92 static int nfound, nsaved, nerr; /* statistics */
95 extern FILE *zopen(const char *, const char *);
97 static sig_atomic_t got_siginfo;
98 static void infohandler(int);
101 printheader(FILE *f, const struct kerneldumpheader *h, const char *device,
102 int bounds, const int status)
106 const char *stat_str;
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);
133 stat_str = "unknown";
135 fprintf(f, " Dump Status: %s\n", stat_str);
147 if ((fp = fopen("bounds", "r")) == NULL) {
149 printf("unable to open bounds file, using 0\n");
153 if (fgets(buf, sizeof buf, fp) == NULL) {
154 syslog(LOG_WARNING, "unable to read from bounds, using 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");
167 writebounds(int bounds) {
170 if ((fp = fopen("bounds", "w")) == NULL) {
171 syslog(LOG_WARNING, "unable to write to bounds file: %m");
176 printf("bounds number: %d\n", bounds);
178 fprintf(fp, "%d\n", bounds);
183 file_size(const char *path)
187 /* Ignore all errors, those file may not exists. */
188 if (stat(path, &sb) == -1)
194 saved_dump_size(int bounds)
196 static char path[PATH_MAX];
201 (void)snprintf(path, sizeof(path), "info.%d", bounds);
202 dumpsize += file_size(path);
203 (void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
204 dumpsize += file_size(path);
205 (void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
206 dumpsize += file_size(path);
207 (void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
208 dumpsize += file_size(path);
209 (void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
210 dumpsize += file_size(path);
216 saved_dump_remove(int bounds)
218 static char path[PATH_MAX];
220 (void)snprintf(path, sizeof(path), "info.%d", bounds);
222 (void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
224 (void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
226 (void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
228 (void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
233 symlinks_remove(void)
236 (void)unlink("info.last");
237 (void)unlink("vmcore.last");
238 (void)unlink("vmcore.last.gz");
239 (void)unlink("textdump.tar.last");
240 (void)unlink("textdump.tar.last.gz");
244 * Check that sufficient space is available on the disk that holds the
248 check_space(const char *savedir, off_t dumpsize, int bounds)
251 off_t minfree, spacefree, totfree, needed;
255 if (statfs(".", &fsbuf) < 0) {
256 syslog(LOG_ERR, "%s: %m", savedir);
259 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
260 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
262 if ((fp = fopen("minfree", "r")) == NULL)
265 if (fgets(buf, sizeof(buf), fp) == NULL)
272 needed = dumpsize / 1024 + 2; /* 2 for info file */
273 needed -= saved_dump_size(bounds);
274 if ((minfree > 0 ? spacefree : totfree) - needed < minfree) {
276 "no dump, not enough free space on device (%lld available, need %lld)",
277 (long long)(minfree > 0 ? spacefree : totfree),
281 if (spacefree - needed < 0)
283 "dump performed, but free space threshold crossed");
287 #define BLOCKSIZE (1<<12)
288 #define BLOCKMASK (~(BLOCKSIZE-1))
291 DoRegularFile(int fd, off_t dumpsize, char *buf, const char *device,
292 const char *filename, FILE *fp)
294 int he, hs, nr, nw, wl;
295 off_t dmpcnt, origsize;
300 while (dumpsize > 0) {
304 nr = read(fd, buf, wl);
308 "WARNING: EOF on dump device");
310 syslog(LOG_ERR, "read error on %s: %m", device);
315 nw = fwrite(buf, 1, wl, fp);
317 for (nw = 0; nw < nr; nw = he) {
318 /* find a contiguous block of zeroes */
319 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
320 for (he = hs; he < nr && buf[he] == 0;
323 /* is the hole long enough to matter? */
324 if (he >= hs + BLOCKSIZE)
328 /* back down to a block boundary */
332 * 1) Don't go beyond the end of the buffer.
333 * 2) If the end of the buffer is less than
334 * BLOCKSIZE bytes away, we're at the end
335 * of the file, so just grab what's left.
337 if (hs + BLOCKSIZE > nr)
341 * At this point, we have a partial ordering:
342 * nw <= hs <= he <= nr
343 * If hs > nw, buf[nw..hs] contains non-zero data.
344 * If he > hs, buf[hs..he] is all zeroes.
347 if (fwrite(buf + nw, hs - nw, 1, fp)
351 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
357 "write error on %s file: %m", filename);
359 "WARNING: vmcore may be incomplete");
365 printf("%llu\r", (unsigned long long)dmpcnt);
370 printf("%s %.1lf%%\n", filename, (100.0 - (100.0 *
371 (double)dumpsize / (double)origsize)));
379 * Specialized version of dump-reading logic for use with textdumps, which
380 * are written backwards from the end of the partition, and must be reversed
381 * before being written to the file. Textdumps are small, so do a bit less
382 * work to optimize/sparsify.
385 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
386 const char *device, const char *filename, FILE *fp)
389 off_t dmpcnt, totsize;
394 if ((dumpsize % wl) != 0) {
395 syslog(LOG_ERR, "textdump uneven multiple of 512 on %s",
400 while (dumpsize > 0) {
401 nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
405 "WARNING: EOF on dump device");
407 syslog(LOG_ERR, "read error on %s: %m", device);
411 nw = fwrite(buf, 1, wl, fp);
414 "write error on %s file: %m", filename);
416 "WARNING: textdump may be incomplete");
422 printf("%llu\r", (unsigned long long)dmpcnt);
431 DoFile(const char *savedir, const char *device)
433 static char infoname[PATH_MAX], corename[PATH_MAX], linkname[PATH_MAX];
434 static char *buf = NULL;
435 struct kerneldumpheader kdhf, kdhl;
436 off_t mediasize, dumpsize, firsthd, lasthd;
439 int fd, fdinfo, error;
444 bounds = getbounds();
446 status = STATUS_UNKNOWN;
448 if (maxdumps > 0 && bounds == maxdumps)
452 buf = malloc(BUFFERSIZE);
454 syslog(LOG_ERR, "%m");
460 printf("checking for kernel dump on device %s\n", device);
462 fd = open(device, (checkfor || keep) ? O_RDONLY : O_RDWR);
464 syslog(LOG_ERR, "%s: %m", device);
468 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
470 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
473 "couldn't find media and/or sector size of %s: %m", device);
478 printf("mediasize = %lld\n", (long long)mediasize);
479 printf("sectorsize = %u\n", sectorsize);
482 lasthd = mediasize - sectorsize;
483 lseek(fd, lasthd, SEEK_SET);
484 error = read(fd, &kdhl, sizeof kdhl);
485 if (error != sizeof kdhl) {
487 "error reading last dump header at offset %lld in %s: %m",
488 (long long)lasthd, device);
492 if (strncmp(kdhl.magic, TEXTDUMPMAGIC, sizeof kdhl) == 0) {
494 printf("textdump magic on last dump header on %s\n",
497 if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) {
499 "unknown version (%d) in last dump header on %s",
500 dtoh32(kdhl.version), device);
506 } else if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic) ==
508 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
510 "unknown version (%d) in last dump header on %s",
511 dtoh32(kdhl.version), device);
519 printf("magic mismatch on last dump header on %s\n",
526 if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
527 sizeof kdhl.magic) == 0) {
529 printf("forcing magic on %s\n", device);
530 memcpy(kdhl.magic, KERNELDUMPMAGIC,
533 syslog(LOG_ERR, "unable to force dump - bad magic");
536 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
538 "unknown version (%d) in last dump header on %s",
539 dtoh32(kdhl.version), device);
551 if (kerneldump_parity(&kdhl)) {
553 "parity error on last dump header on %s", device);
559 dumpsize = dtoh64(kdhl.dumplength);
560 firsthd = lasthd - dumpsize - sizeof kdhf;
561 lseek(fd, firsthd, SEEK_SET);
562 error = read(fd, &kdhf, sizeof kdhf);
563 if (error != sizeof kdhf) {
565 "error reading first dump header at offset %lld in %s: %m",
566 (long long)firsthd, device);
572 printf("First dump headers:\n");
573 printheader(stdout, &kdhf, device, bounds, -1);
575 printf("\nLast dump headers:\n");
576 printheader(stdout, &kdhl, device, bounds, -1);
580 if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
582 "first and last dump headers disagree on %s", device);
588 status = STATUS_GOOD;
592 printf("A dump exists on %s\n", device);
597 if (kdhl.panicstring[0])
598 syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
600 syslog(LOG_ALERT, "reboot");
603 printf("Checking for available free space\n");
605 if (!check_space(savedir, dumpsize, bounds)) {
610 writebounds(bounds + 1);
612 saved_dump_remove(bounds);
614 snprintf(infoname, sizeof(infoname), "info.%d", bounds);
617 * Create or overwrite any existing dump header files.
619 fdinfo = open(infoname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
621 syslog(LOG_ERR, "%s: %m", buf);
625 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
627 snprintf(corename, sizeof(corename), "%s.%d.gz",
628 istextdump ? "textdump.tar" : "vmcore", bounds);
629 fp = zopen(corename, "w");
631 snprintf(corename, sizeof(corename), "%s.%d",
632 istextdump ? "textdump.tar" : "vmcore", bounds);
633 fp = fopen(corename, "w");
636 syslog(LOG_ERR, "%s: %m", corename);
643 info = fdopen(fdinfo, "w");
646 syslog(LOG_ERR, "fdopen failed: %m");
652 printheader(stdout, &kdhl, device, bounds, status);
654 printheader(info, &kdhl, device, bounds, status);
657 syslog(LOG_NOTICE, "writing %score to %s/%s",
658 compress ? "compressed " : "", savedir, corename);
661 if (DoTextdumpFile(fd, dumpsize, lasthd, buf, device,
665 if (DoRegularFile(fd, dumpsize, buf, device, corename, fp)
672 if (fclose(fp) < 0) {
673 syslog(LOG_ERR, "error on %s: %m", corename);
679 if (symlink(infoname, "info.last") == -1) {
680 syslog(LOG_WARNING, "unable to create symlink %s/%s: %m",
681 savedir, "info.last");
684 snprintf(linkname, sizeof(linkname), "%s.last.gz",
685 istextdump ? "textdump.tar" : "vmcore");
687 snprintf(linkname, sizeof(linkname), "%s.last",
688 istextdump ? "textdump.tar" : "vmcore");
690 if (symlink(corename, linkname) == -1) {
691 syslog(LOG_WARNING, "unable to create symlink %s/%s: %m",
698 printf("dump saved\n");
703 printf("clearing dump header\n");
704 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
705 lseek(fd, lasthd, SEEK_SET);
706 error = write(fd, &kdhl, sizeof kdhl);
707 if (error != sizeof kdhl)
709 "error while clearing the dump header: %m");
724 fprintf(stderr, "%s\n%s\n%s\n",
725 "usage: savecore -c [-v] [device ...]",
726 " savecore -C [-v] [device ...]",
727 " savecore [-fkvz] [-m maxdumps] [directory [device ...]]");
732 main(int argc, char **argv)
734 const char *savedir = ".";
738 checkfor = compress = clear = force = keep = verbose = 0;
739 nfound = nsaved = nerr = 0;
741 openlog("savecore", LOG_PERROR, LOG_DAEMON);
742 signal(SIGINFO, infohandler);
744 while ((ch = getopt(argc, argv, "Ccfkm:vz")) != -1)
759 maxdumps = atoi(optarg);
761 syslog(LOG_ERR, "Invalid maxdump value");
775 if (checkfor && (clear || force || keep))
777 if (clear && (compress || keep))
779 if (maxdumps > 0 && (checkfor || clear))
783 if (argc >= 1 && !checkfor && !clear) {
784 error = chdir(argv[0]);
786 syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
798 if (strcmp(fsp->fs_vfstype, "swap") &&
799 strcmp(fsp->fs_vfstype, "dump"))
801 DoFile(savedir, fsp->fs_spec);
804 for (i = 0; i < argc; i++)
805 DoFile(savedir, argv[i]);
808 /* Emit minimal output. */
811 printf("No dump exists\n");
814 syslog(LOG_WARNING, "no dumps found");
816 else if (nsaved == 0) {
818 syslog(LOG_WARNING, "unsaved dumps found but not saved");
820 syslog(LOG_WARNING, "no unsaved dumps found");
827 infohandler(int sig __unused)