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>
83 /* The size of the buffer used for I/O. */
84 #define BUFFERSIZE (1024*1024)
88 #define STATUS_UNKNOWN 2
90 static int checkfor, compress, clear, force, keep, verbose; /* flags */
91 static int nfound, nsaved, nerr; /* statistics */
93 extern FILE *zopen(const char *, const char *);
95 static sig_atomic_t got_siginfo;
96 static void infohandler(int);
99 printheader(FILE *f, const struct kerneldumpheader *h, const char *device,
100 int bounds, const int status)
104 const char *stat_str;
106 fprintf(f, "Dump header from device %s\n", device);
107 fprintf(f, " Architecture: %s\n", h->architecture);
108 fprintf(f, " Architecture Version: %u\n",
109 dtoh32(h->architectureversion));
110 dumplen = dtoh64(h->dumplength);
111 fprintf(f, " Dump Length: %lldB (%lld MB)\n", (long long)dumplen,
112 (long long)(dumplen >> 20));
113 fprintf(f, " Blocksize: %d\n", dtoh32(h->blocksize));
114 t = dtoh64(h->dumptime);
115 fprintf(f, " Dumptime: %s", ctime(&t));
116 fprintf(f, " Hostname: %s\n", h->hostname);
117 fprintf(f, " Magic: %s\n", h->magic);
118 fprintf(f, " Version String: %s", h->versionstring);
119 fprintf(f, " Panic String: %s\n", h->panicstring);
120 fprintf(f, " Dump Parity: %u\n", h->parity);
121 fprintf(f, " Bounds: %d\n", bounds);
131 stat_str = "unknown";
133 fprintf(f, " Dump Status: %s\n", stat_str);
145 if ((fp = fopen("bounds", "r")) == NULL) {
147 printf("unable to open bounds file, using 0\n");
151 if (fgets(buf, sizeof buf, fp) == NULL) {
153 syslog(LOG_WARNING, "bounds file is empty, using 0");
155 syslog(LOG_WARNING, "bounds file: %s", strerror(errno));
161 ret = (int)strtol(buf, NULL, 10);
162 if (ret == 0 && (errno == EINVAL || errno == ERANGE))
163 syslog(LOG_WARNING, "invalid value found in bounds, using 0");
169 writebounds(int bounds) {
172 if ((fp = fopen("bounds", "w")) == NULL) {
173 syslog(LOG_WARNING, "unable to write to bounds file: %m");
178 printf("bounds number: %d\n", bounds);
180 fprintf(fp, "%d\n", bounds);
185 * Check that sufficient space is available on the disk that holds the
189 check_space(const char *savedir, off_t dumpsize)
192 off_t minfree, spacefree, totfree, needed;
194 char buf[100], path[MAXPATHLEN];
196 if (statfs(savedir, &fsbuf) < 0) {
197 syslog(LOG_ERR, "%s: %m", savedir);
200 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
201 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
203 (void)snprintf(path, sizeof(path), "%s/minfree", savedir);
204 if ((fp = fopen(path, "r")) == NULL)
207 if (fgets(buf, sizeof(buf), fp) == NULL)
214 needed = dumpsize / 1024 + 2; /* 2 for info file */
215 if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
217 "no dump, not enough free space on device (%lld available, need %lld)",
218 (long long)(minfree > 0 ? spacefree : totfree),
222 if (spacefree - needed < 0)
224 "dump performed, but free space threshold crossed");
228 #define BLOCKSIZE (1<<12)
229 #define BLOCKMASK (~(BLOCKSIZE-1))
232 DoRegularFile(int fd, off_t dumpsize, char *buf, const char *device,
233 const char *filename, FILE *fp)
235 int he, hs, nr, nw, wl;
236 off_t dmpcnt, origsize;
241 while (dumpsize > 0) {
245 nr = read(fd, buf, wl);
249 "WARNING: EOF on dump device");
251 syslog(LOG_ERR, "read error on %s: %m", device);
256 nw = fwrite(buf, 1, wl, fp);
258 for (nw = 0; nw < nr; nw = he) {
259 /* find a contiguous block of zeroes */
260 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
261 for (he = hs; he < nr && buf[he] == 0;
264 /* is the hole long enough to matter? */
265 if (he >= hs + BLOCKSIZE)
269 /* back down to a block boundary */
273 * 1) Don't go beyond the end of the buffer.
274 * 2) If the end of the buffer is less than
275 * BLOCKSIZE bytes away, we're at the end
276 * of the file, so just grab what's left.
278 if (hs + BLOCKSIZE > nr)
282 * At this point, we have a partial ordering:
283 * nw <= hs <= he <= nr
284 * If hs > nw, buf[nw..hs] contains non-zero data.
285 * If he > hs, buf[hs..he] is all zeroes.
288 if (fwrite(buf + nw, hs - nw, 1, fp)
292 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
298 "write error on %s file: %m", filename);
300 "WARNING: vmcore may be incomplete");
306 printf("%llu\r", (unsigned long long)dmpcnt);
311 printf("%s %.1lf%%\n", filename, (100.0 - (100.0 *
312 (double)dumpsize / (double)origsize)));
320 * Specialized version of dump-reading logic for use with textdumps, which
321 * are written backwards from the end of the partition, and must be reversed
322 * before being written to the file. Textdumps are small, so do a bit less
323 * work to optimize/sparsify.
326 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
327 const char *device, const char *filename, FILE *fp)
330 off_t dmpcnt, totsize;
335 if ((dumpsize % wl) != 0) {
336 syslog(LOG_ERR, "textdump uneven multiple of 512 on %s",
341 while (dumpsize > 0) {
342 nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
346 "WARNING: EOF on dump device");
348 syslog(LOG_ERR, "read error on %s: %m", device);
352 nw = fwrite(buf, 1, wl, fp);
355 "write error on %s file: %m", filename);
357 "WARNING: textdump may be incomplete");
363 printf("%llu\r", (unsigned long long)dmpcnt);
372 DoFile(const char *savedir, const char *device)
374 static char filename[PATH_MAX];
375 static char *buf = NULL;
376 struct kerneldumpheader kdhf, kdhl;
377 off_t mediasize, dumpsize, firsthd, lasthd;
380 int fd, fdinfo, error;
385 bounds = getbounds();
387 status = STATUS_UNKNOWN;
390 buf = malloc(BUFFERSIZE);
392 syslog(LOG_ERR, "%m");
398 printf("checking for kernel dump on device %s\n", device);
400 fd = open(device, O_RDWR);
402 syslog(LOG_ERR, "%s: %m", device);
406 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
408 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
411 "couldn't find media and/or sector size of %s: %m", device);
416 printf("mediasize = %lld\n", (long long)mediasize);
417 printf("sectorsize = %u\n", sectorsize);
420 lasthd = mediasize - sectorsize;
421 lseek(fd, lasthd, SEEK_SET);
422 error = read(fd, &kdhl, sizeof kdhl);
423 if (error != sizeof kdhl) {
425 "error reading last dump header at offset %lld in %s: %m",
426 (long long)lasthd, device);
430 if (strncmp(kdhl.magic, TEXTDUMPMAGIC, sizeof kdhl) == 0) {
432 printf("textdump magic on last dump header on %s\n",
435 if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) {
437 "unknown version (%d) in last dump header on %s",
438 dtoh32(kdhl.version), device);
444 } else if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic) ==
446 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
448 "unknown version (%d) in last dump header on %s",
449 dtoh32(kdhl.version), device);
457 printf("magic mismatch on last dump header on %s\n",
464 if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
465 sizeof kdhl.magic) == 0) {
467 printf("forcing magic on %s\n", device);
468 memcpy(kdhl.magic, KERNELDUMPMAGIC,
471 syslog(LOG_ERR, "unable to force dump - bad magic");
474 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
476 "unknown version (%d) in last dump header on %s",
477 dtoh32(kdhl.version), device);
489 if (kerneldump_parity(&kdhl)) {
491 "parity error on last dump header on %s", device);
497 dumpsize = dtoh64(kdhl.dumplength);
498 firsthd = lasthd - dumpsize - sizeof kdhf;
499 lseek(fd, firsthd, SEEK_SET);
500 error = read(fd, &kdhf, sizeof kdhf);
501 if (error != sizeof kdhf) {
503 "error reading first dump header at offset %lld in %s: %m",
504 (long long)firsthd, device);
510 printf("First dump headers:\n");
511 printheader(stdout, &kdhf, device, bounds, -1);
513 printf("\nLast dump headers:\n");
514 printheader(stdout, &kdhl, device, bounds, -1);
518 if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
520 "first and last dump headers disagree on %s", device);
526 status = STATUS_GOOD;
530 printf("A dump exists on %s\n", device);
535 if (kdhl.panicstring[0])
536 syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
538 syslog(LOG_ALERT, "reboot");
541 printf("Checking for available free space\n");
542 if (!check_space(savedir, dumpsize)) {
547 writebounds(bounds + 1);
549 sprintf(buf, "info.%d", bounds);
552 * Create or overwrite any existing dump header files.
554 fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
556 syslog(LOG_ERR, "%s: %m", buf);
560 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
562 sprintf(filename, "%s.%d.gz", istextdump ? "textdump.tar" :
564 fp = zopen(filename, "w");
566 sprintf(filename, "%s.%d", istextdump ? "textdump.tar" :
568 fp = fopen(filename, "w");
571 syslog(LOG_ERR, "%s: %m", filename);
578 info = fdopen(fdinfo, "w");
581 syslog(LOG_ERR, "fdopen failed: %m");
587 printheader(stdout, &kdhl, device, bounds, status);
589 printheader(info, &kdhl, device, bounds, status);
592 syslog(LOG_NOTICE, "writing %score to %s",
593 compress ? "compressed " : "", filename);
596 if (DoTextdumpFile(fd, dumpsize, lasthd, buf, device,
600 if (DoRegularFile(fd, dumpsize, buf, device, filename, fp)
607 if (fclose(fp) < 0) {
608 syslog(LOG_ERR, "error on %s: %m", filename);
615 printf("dump saved\n");
618 if (clear || !keep) {
620 printf("clearing dump header\n");
621 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
622 lseek(fd, lasthd, SEEK_SET);
623 error = write(fd, &kdhl, sizeof kdhl);
624 if (error != sizeof kdhl)
626 "error while clearing the dump header: %m");
641 fprintf(stderr, "%s\n%s\n%s\n",
642 "usage: savecore -c",
643 " savecore -C [-v] [directory device]",
644 " savecore [-fkvz] [directory [device ...]]");
649 main(int argc, char **argv)
651 const char *savedir = ".";
655 checkfor = compress = clear = force = keep = verbose = 0;
656 nfound = nsaved = nerr = 0;
658 openlog("savecore", LOG_PERROR, LOG_DAEMON);
659 signal(SIGINFO, infohandler);
661 while ((ch = getopt(argc, argv, "Ccfkvz")) != -1)
685 if (checkfor && (clear || force || keep))
690 error = chdir(argv[0]);
692 syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
704 if (strcmp(fsp->fs_vfstype, "swap") &&
705 strcmp(fsp->fs_vfstype, "dump"))
707 DoFile(savedir, fsp->fs_spec);
710 for (i = 0; i < argc; i++)
711 DoFile(savedir, argv[i]);
714 /* Emit minimal output. */
717 printf("No dump exists\n");
720 syslog(LOG_WARNING, "no dumps found");
722 else if (nsaved == 0) {
724 syslog(LOG_WARNING, "unsaved dumps found but not saved");
726 syslog(LOG_WARNING, "no unsaved dumps found");
733 infohandler(int sig __unused)