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>
87 /* The size of the buffer used for I/O. */
88 #define BUFFERSIZE (1024*1024)
92 #define STATUS_UNKNOWN 2
94 static int checkfor, compress, clear, force, keep, verbose; /* flags */
95 static int nfound, nsaved, nerr; /* statistics */
98 extern FILE *zopen(const char *, const char *);
100 static sig_atomic_t got_siginfo;
101 static void infohandler(int);
104 printheader(xo_handle_t *xo, const struct kerneldumpheader *h,
105 const char *device, int bounds, const int status)
109 const char *stat_str;
112 xo_emit_h(xo, "{Lwc:Dump header from device}{:dump_device/%s}\n",
114 xo_emit_h(xo, "{P: }{Lwc:Architecture}{:architecture/%s}\n",
117 "{P: }{Lwc:Architecture Version}{:architecture_version/%u}\n",
118 dtoh32(h->architectureversion));
119 dumplen = dtoh64(h->dumplength);
120 xo_emit_h(xo, "{P: }{Lwc:Dump Length}{:dump_length_bytes/%lld}\n",
122 xo_emit_h(xo, "{P: }{Lwc:Blocksize}{:blocksize/%d}\n",
123 dtoh32(h->blocksize));
125 t = dtoh64(h->dumptime);
126 xo_emit_h(xo, "{P: }{Lwc:Dumptime}{:dumptime/%s}", ctime(&t));
127 xo_emit_h(xo, "{P: }{Lwc:Hostname}{:hostname/%s}\n", h->hostname);
128 xo_emit_h(xo, "{P: }{Lwc:Magic}{:magic/%s}\n", h->magic);
129 xo_emit_h(xo, "{P: }{Lwc:Version String}{:version_string/%s}",
131 xo_emit_h(xo, "{P: }{Lwc:Panic String}{:panic_string/%s}\n",
133 xo_emit_h(xo, "{P: }{Lwc:Dump Parity}{:dump_parity/%u}\n", h->parity);
134 xo_emit_h(xo, "{P: }{Lwc:Bounds}{:bounds/%d}\n", bounds);
144 stat_str = "unknown";
147 xo_emit_h(xo, "{P: }{Lwc:Dump Status}{:dump_status/%s}\n", stat_str);
159 * If we are just checking, then we haven't done a chdir to the dump
160 * directory and we should not try to read a bounds file.
167 if ((fp = fopen("bounds", "r")) == NULL) {
169 printf("unable to open bounds file, using 0\n");
173 if (fgets(buf, sizeof buf, fp) == NULL) {
175 syslog(LOG_WARNING, "bounds file is empty, using 0");
177 syslog(LOG_WARNING, "bounds file: %s", strerror(errno));
183 ret = (int)strtol(buf, NULL, 10);
184 if (ret == 0 && (errno == EINVAL || errno == ERANGE))
185 syslog(LOG_WARNING, "invalid value found in bounds, using 0");
191 writebounds(int bounds)
195 if ((fp = fopen("bounds", "w")) == NULL) {
196 syslog(LOG_WARNING, "unable to write to bounds file: %m");
201 printf("bounds number: %d\n", bounds);
203 fprintf(fp, "%d\n", bounds);
208 file_size(const char *path)
212 /* Ignore all errors, those file may not exists. */
213 if (stat(path, &sb) == -1)
219 saved_dump_size(int bounds)
221 static char path[PATH_MAX];
226 (void)snprintf(path, sizeof(path), "info.%d", bounds);
227 dumpsize += file_size(path);
228 (void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
229 dumpsize += file_size(path);
230 (void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
231 dumpsize += file_size(path);
232 (void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
233 dumpsize += file_size(path);
234 (void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
235 dumpsize += file_size(path);
241 saved_dump_remove(int bounds)
243 static char path[PATH_MAX];
245 (void)snprintf(path, sizeof(path), "info.%d", bounds);
247 (void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
249 (void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
251 (void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
253 (void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
258 symlinks_remove(void)
261 (void)unlink("info.last");
262 (void)unlink("vmcore.last");
263 (void)unlink("vmcore.last.gz");
264 (void)unlink("textdump.tar.last");
265 (void)unlink("textdump.tar.last.gz");
269 * Check that sufficient space is available on the disk that holds the
273 check_space(const char *savedir, off_t dumpsize, int bounds)
276 off_t available, minfree, spacefree, totfree, needed;
280 if (statfs(".", &fsbuf) < 0) {
281 syslog(LOG_ERR, "%s: %m", savedir);
284 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
285 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
287 if ((fp = fopen("minfree", "r")) == NULL)
290 if (fgets(buf, sizeof(buf), fp) == NULL)
296 minfree = strtoll(buf, &endp, 10);
297 if (minfree == 0 && errno != 0)
300 while (*endp != '\0' && isspace(*endp))
302 if (*endp != '\0' || minfree < 0)
307 "`minfree` didn't contain a valid size "
308 "(`%s`). Defaulting to 0", buf);
313 available = minfree > 0 ? spacefree - minfree : totfree;
314 needed = dumpsize / 1024 + 2; /* 2 for info file */
315 needed -= saved_dump_size(bounds);
316 if (available < needed) {
318 "no dump: not enough free space on device (need at least "
319 "%jdkB for dump; %jdkB available; %jdkB reserved)",
321 (intmax_t)available + minfree,
325 if (spacefree - needed < 0)
327 "dump performed, but free space threshold crossed");
332 compare_magic(const struct kerneldumpheader *kdh, const char *magic)
335 return (strncmp(kdh->magic, magic, sizeof(kdh->magic)) == 0);
338 #define BLOCKSIZE (1<<12)
339 #define BLOCKMASK (~(BLOCKSIZE-1))
342 DoRegularFile(int fd, off_t dumpsize, char *buf, const char *device,
343 const char *filename, FILE *fp)
345 int he, hs, nr, nw, wl;
346 off_t dmpcnt, origsize;
351 while (dumpsize > 0) {
355 nr = read(fd, buf, wl);
359 "WARNING: EOF on dump device");
361 syslog(LOG_ERR, "read error on %s: %m", device);
366 nw = fwrite(buf, 1, wl, fp);
368 for (nw = 0; nw < nr; nw = he) {
369 /* find a contiguous block of zeroes */
370 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
371 for (he = hs; he < nr && buf[he] == 0;
374 /* is the hole long enough to matter? */
375 if (he >= hs + BLOCKSIZE)
379 /* back down to a block boundary */
383 * 1) Don't go beyond the end of the buffer.
384 * 2) If the end of the buffer is less than
385 * BLOCKSIZE bytes away, we're at the end
386 * of the file, so just grab what's left.
388 if (hs + BLOCKSIZE > nr)
392 * At this point, we have a partial ordering:
393 * nw <= hs <= he <= nr
394 * If hs > nw, buf[nw..hs] contains non-zero
395 * data. If he > hs, buf[hs..he] is all zeroes.
398 if (fwrite(buf + nw, hs - nw, 1, fp)
402 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
408 "write error on %s file: %m", filename);
410 "WARNING: vmcore may be incomplete");
416 printf("%llu\r", (unsigned long long)dmpcnt);
421 printf("%s %.1lf%%\n", filename, (100.0 - (100.0 *
422 (double)dumpsize / (double)origsize)));
430 * Specialized version of dump-reading logic for use with textdumps, which
431 * are written backwards from the end of the partition, and must be reversed
432 * before being written to the file. Textdumps are small, so do a bit less
433 * work to optimize/sparsify.
436 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
437 const char *device, const char *filename, FILE *fp)
440 off_t dmpcnt, totsize;
445 if ((dumpsize % wl) != 0) {
446 syslog(LOG_ERR, "textdump uneven multiple of 512 on %s",
451 while (dumpsize > 0) {
452 nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
456 "WARNING: EOF on dump device");
458 syslog(LOG_ERR, "read error on %s: %m", device);
462 nw = fwrite(buf, 1, wl, fp);
465 "write error on %s file: %m", filename);
467 "WARNING: textdump may be incomplete");
473 printf("%llu\r", (unsigned long long)dmpcnt);
482 DoFile(const char *savedir, const char *device)
484 xo_handle_t *xostdout, *xoinfo;
485 static char infoname[PATH_MAX], corename[PATH_MAX], linkname[PATH_MAX];
486 static char *buf = NULL, *temp = NULL;
487 struct kerneldumpheader kdhf, kdhl;
488 off_t mediasize, dumpsize, firsthd, lasthd;
491 int fd, fdinfo, error;
493 u_int sectorsize, xostyle;
496 bounds = getbounds();
498 status = STATUS_UNKNOWN;
500 xostdout = xo_create_to_file(stdout, XO_STYLE_TEXT, 0);
501 if (xostdout == NULL) {
502 syslog(LOG_ERR, "%s: %m", infoname);
506 if (maxdumps > 0 && bounds == maxdumps)
510 buf = malloc(BUFFERSIZE);
512 syslog(LOG_ERR, "%m");
518 printf("checking for kernel dump on device %s\n", device);
520 fd = open(device, (checkfor || keep) ? O_RDONLY : O_RDWR);
522 syslog(LOG_ERR, "%s: %m", device);
526 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
528 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
531 "couldn't find media and/or sector size of %s: %m", device);
536 printf("mediasize = %lld bytes\n", (long long)mediasize);
537 printf("sectorsize = %u bytes\n", sectorsize);
540 if (sectorsize < sizeof(kdhl)) {
542 "Sector size is less the kernel dump header %zu",
547 lasthd = mediasize - sectorsize;
549 temp = malloc(sectorsize);
551 syslog(LOG_ERR, "%m");
555 if (lseek(fd, lasthd, SEEK_SET) != lasthd ||
556 read(fd, temp, sectorsize) != (ssize_t)sectorsize) {
558 "error reading last dump header at offset %lld in %s: %m",
559 (long long)lasthd, device);
562 memcpy(&kdhl, temp, sizeof(kdhl));
564 if (compare_magic(&kdhl, TEXTDUMPMAGIC)) {
566 printf("textdump magic on last dump header on %s\n",
569 if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) {
571 "unknown version (%d) in last dump header on %s",
572 dtoh32(kdhl.version), device);
578 } else if (compare_magic(&kdhl, KERNELDUMPMAGIC)) {
579 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
581 "unknown version (%d) in last dump header on %s",
582 dtoh32(kdhl.version), device);
590 printf("magic mismatch on last dump header on %s\n",
597 if (compare_magic(&kdhl, KERNELDUMPMAGIC_CLEARED)) {
599 printf("forcing magic on %s\n", device);
600 memcpy(kdhl.magic, KERNELDUMPMAGIC,
603 syslog(LOG_ERR, "unable to force dump - bad magic");
606 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
608 "unknown version (%d) in last dump header on %s",
609 dtoh32(kdhl.version), device);
621 if (kerneldump_parity(&kdhl)) {
623 "parity error on last dump header on %s", device);
629 dumpsize = dtoh64(kdhl.dumplength);
630 firsthd = lasthd - dumpsize - sectorsize;
631 if (lseek(fd, firsthd, SEEK_SET) != firsthd ||
632 read(fd, temp, sectorsize) != (ssize_t)sectorsize) {
634 "error reading first dump header at offset %lld in %s: %m",
635 (long long)firsthd, device);
639 memcpy(&kdhf, temp, sizeof(kdhf));
642 printf("First dump headers:\n");
643 printheader(xostdout, &kdhf, device, bounds, -1);
645 printf("\nLast dump headers:\n");
646 printheader(xostdout, &kdhl, device, bounds, -1);
650 if (memcmp(&kdhl, &kdhf, sizeof(kdhl))) {
652 "first and last dump headers disagree on %s", device);
658 status = STATUS_GOOD;
662 printf("A dump exists on %s\n", device);
667 if (kdhl.panicstring[0] != '\0')
668 syslog(LOG_ALERT, "reboot after panic: %.*s",
669 (int)sizeof(kdhl.panicstring), kdhl.panicstring);
671 syslog(LOG_ALERT, "reboot");
674 printf("Checking for available free space\n");
676 if (!check_space(savedir, dumpsize, bounds)) {
681 writebounds(bounds + 1);
683 saved_dump_remove(bounds);
685 snprintf(infoname, sizeof(infoname), "info.%d", bounds);
688 * Create or overwrite any existing dump header files.
690 fdinfo = open(infoname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
692 syslog(LOG_ERR, "%s: %m", infoname);
697 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file. */
699 snprintf(corename, sizeof(corename), "%s.%d.gz",
700 istextdump ? "textdump.tar" : "vmcore", bounds);
701 fp = zopen(corename, "w");
703 snprintf(corename, sizeof(corename), "%s.%d",
704 istextdump ? "textdump.tar" : "vmcore", bounds);
705 fp = fopen(corename, "w");
708 syslog(LOG_ERR, "%s: %m", corename);
715 info = fdopen(fdinfo, "w");
718 syslog(LOG_ERR, "fdopen failed: %m");
723 xostyle = xo_get_style(NULL);
724 xoinfo = xo_create_to_file(info, xostyle, 0);
725 if (xoinfo == NULL) {
726 syslog(LOG_ERR, "%s: %m", infoname);
730 xo_open_container_h(xoinfo, "crashdump");
733 printheader(xostdout, &kdhl, device, bounds, status);
735 printheader(xoinfo, &kdhl, device, bounds, status);
736 xo_close_container_h(xoinfo, "crashdump");
741 syslog(LOG_NOTICE, "writing %score to %s/%s",
742 compress ? "compressed " : "", savedir, corename);
745 if (DoTextdumpFile(fd, dumpsize, lasthd, buf, device,
749 if (DoRegularFile(fd, dumpsize, buf, device, corename, fp)
756 if (fclose(fp) < 0) {
757 syslog(LOG_ERR, "error on %s: %m", corename);
763 if (symlink(infoname, "info.last") == -1) {
764 syslog(LOG_WARNING, "unable to create symlink %s/%s: %m",
765 savedir, "info.last");
768 snprintf(linkname, sizeof(linkname), "%s.last.gz",
769 istextdump ? "textdump.tar" : "vmcore");
771 snprintf(linkname, sizeof(linkname), "%s.last",
772 istextdump ? "textdump.tar" : "vmcore");
774 if (symlink(corename, linkname) == -1) {
775 syslog(LOG_WARNING, "unable to create symlink %s/%s: %m",
782 printf("dump saved\n");
787 printf("clearing dump header\n");
788 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof(kdhl.magic));
789 memcpy(temp, &kdhl, sizeof(kdhl));
790 if (lseek(fd, lasthd, SEEK_SET) != lasthd ||
791 write(fd, temp, sectorsize) != (ssize_t)sectorsize)
793 "error while clearing the dump header: %m");
795 xo_close_container_h(xostdout, "crashdump");
796 xo_finish_h(xostdout);
810 xo_error("%s\n%s\n%s\n",
811 "usage: savecore -c [-v] [device ...]",
812 " savecore -C [-v] [device ...]",
813 " savecore [-fkvz] [-m maxdumps] [directory [device ...]]");
818 main(int argc, char **argv)
820 const char *savedir = ".";
824 checkfor = compress = clear = force = keep = verbose = 0;
825 nfound = nsaved = nerr = 0;
827 openlog("savecore", LOG_PERROR, LOG_DAEMON);
828 signal(SIGINFO, infohandler);
830 argc = xo_parse_args(argc, argv);
834 while ((ch = getopt(argc, argv, "Ccfkm:vz")) != -1)
849 maxdumps = atoi(optarg);
851 syslog(LOG_ERR, "Invalid maxdump value");
865 if (checkfor && (clear || force || keep))
867 if (clear && (compress || keep))
869 if (maxdumps > 0 && (checkfor || clear))
873 if (argc >= 1 && !checkfor && !clear) {
874 error = chdir(argv[0]);
876 syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
888 if (strcmp(fsp->fs_vfstype, "swap") &&
889 strcmp(fsp->fs_vfstype, "dump"))
891 DoFile(savedir, fsp->fs_spec);
895 for (i = 0; i < argc; i++)
896 DoFile(savedir, argv[i]);
899 /* Emit minimal output. */
903 printf("No dump exists\n");
907 syslog(LOG_WARNING, "no dumps found");
908 } else if (nsaved == 0) {
912 "unsaved dumps found but not saved");
915 syslog(LOG_WARNING, "no unsaved dumps found");
922 infohandler(int sig __unused)