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 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
72 #include <sys/kerneldump.h>
73 #include <sys/param.h>
74 #include <sys/mount.h>
88 /* The size of the buffer used for I/O. */
89 #define BUFFERSIZE (1024*1024)
93 #define STATUS_UNKNOWN 2
95 static int checkfor, compress, clear, force, keep, verbose; /* flags */
96 static int nfound, nsaved, nerr; /* statistics */
98 extern FILE *zopen(const char *, const char *);
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 * Check that sufficient space is available on the disk that holds the
187 check_space(const char *savedir, off_t dumpsize)
190 off_t minfree, spacefree, totfree, needed;
192 char buf[100], path[MAXPATHLEN];
194 if (statfs(savedir, &fsbuf) < 0) {
195 syslog(LOG_ERR, "%s: %m", savedir);
198 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
199 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
201 (void)snprintf(path, sizeof(path), "%s/minfree", savedir);
202 if ((fp = fopen(path, "r")) == NULL)
205 if (fgets(buf, sizeof(buf), fp) == NULL)
212 needed = dumpsize / 1024 + 2; /* 2 for info file */
213 if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
215 "no dump, not enough free space on device (%lld available, need %lld)",
216 (long long)(minfree > 0 ? spacefree : totfree),
220 if (spacefree - needed < 0)
222 "dump performed, but free space threshold crossed");
226 #define BLOCKSIZE (1<<12)
227 #define BLOCKMASK (~(BLOCKSIZE-1))
230 DoRegularFile(int fd, off_t dumpsize, char *buf, const char *device,
231 const char *filename, FILE *fp)
233 int he, hs, nr, nw, wl;
238 while (dumpsize > 0) {
242 nr = read(fd, buf, wl);
246 "WARNING: EOF on dump device");
248 syslog(LOG_ERR, "read error on %s: %m", device);
253 nw = fwrite(buf, 1, wl, fp);
255 for (nw = 0; nw < nr; nw = he) {
256 /* find a contiguous block of zeroes */
257 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
258 for (he = hs; he < nr && buf[he] == 0;
261 /* is the hole long enough to matter? */
262 if (he >= hs + BLOCKSIZE)
266 /* back down to a block boundary */
270 * 1) Don't go beyond the end of the buffer.
271 * 2) If the end of the buffer is less than
272 * BLOCKSIZE bytes away, we're at the end
273 * of the file, so just grab what's left.
275 if (hs + BLOCKSIZE > nr)
279 * At this point, we have a partial ordering:
280 * nw <= hs <= he <= nr
281 * If hs > nw, buf[nw..hs] contains non-zero data.
282 * If he > hs, buf[hs..he] is all zeroes.
285 if (fwrite(buf + nw, hs - nw, 1, fp)
289 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
295 "write error on %s file: %m", filename);
297 "WARNING: vmcore may be incomplete");
303 printf("%llu\r", (unsigned long long)dmpcnt);
312 * Specialized version of dump-reading logic for use with textdumps, which
313 * are written backwards from the end of the partition, and must be reversed
314 * before being written to the file. Textdumps are small, so do a bit less
315 * work to optimize/sparsify.
318 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
319 const char *device, const char *filename, FILE *fp)
322 off_t dmpcnt, totsize;
327 if ((dumpsize % wl) != 0) {
328 syslog(LOG_ERR, "textdump uneven multiple of 512 on %s",
333 while (dumpsize > 0) {
334 nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
338 "WARNING: EOF on dump device");
340 syslog(LOG_ERR, "read error on %s: %m", device);
344 nw = fwrite(buf, 1, wl, fp);
347 "write error on %s file: %m", filename);
349 "WARNING: textdump may be incomplete");
355 printf("%llu\r", (unsigned long long)dmpcnt);
364 DoFile(const char *savedir, const char *device)
366 static char filename[PATH_MAX];
367 static char *buf = NULL;
368 struct kerneldumpheader kdhf, kdhl;
369 off_t mediasize, dumpsize, firsthd, lasthd;
372 int fd, fdinfo, error;
377 bounds = getbounds();
379 status = STATUS_UNKNOWN;
382 buf = malloc(BUFFERSIZE);
384 syslog(LOG_ERR, "%m");
390 printf("checking for kernel dump on device %s\n", device);
392 fd = open(device, O_RDWR);
394 syslog(LOG_ERR, "%s: %m", device);
398 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
400 error = ioctl(fd, DIOCGSECTORSIZE, §orsize);
403 "couldn't find media and/or sector size of %s: %m", device);
408 printf("mediasize = %lld\n", (long long)mediasize);
409 printf("sectorsize = %u\n", sectorsize);
412 lasthd = mediasize - sectorsize;
413 lseek(fd, lasthd, SEEK_SET);
414 error = read(fd, &kdhl, sizeof kdhl);
415 if (error != sizeof kdhl) {
417 "error reading last dump header at offset %lld in %s: %m",
418 (long long)lasthd, device);
422 if (strncmp(kdhl.magic, TEXTDUMPMAGIC, sizeof kdhl) == 0) {
424 printf("textdump magic on last dump header on %s\n",
427 if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) {
429 "unknown version (%d) in last dump header on %s",
430 dtoh32(kdhl.version), device);
436 } else if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic) ==
438 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
440 "unknown version (%d) in last dump header on %s",
441 dtoh32(kdhl.version), device);
449 printf("magic mismatch on last dump header on %s\n",
456 if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
457 sizeof kdhl.magic) == 0) {
459 printf("forcing magic on %s\n", device);
460 memcpy(kdhl.magic, KERNELDUMPMAGIC,
463 syslog(LOG_ERR, "unable to force dump - bad magic");
466 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
468 "unknown version (%d) in last dump header on %s",
469 dtoh32(kdhl.version), device);
481 if (kerneldump_parity(&kdhl)) {
483 "parity error on last dump header on %s", device);
489 dumpsize = dtoh64(kdhl.dumplength);
490 firsthd = lasthd - dumpsize - sizeof kdhf;
491 lseek(fd, firsthd, SEEK_SET);
492 error = read(fd, &kdhf, sizeof kdhf);
493 if (error != sizeof kdhf) {
495 "error reading first dump header at offset %lld in %s: %m",
496 (long long)firsthd, device);
502 printf("First dump headers:\n");
503 printheader(stdout, &kdhf, device, bounds, -1);
505 printf("\nLast dump headers:\n");
506 printheader(stdout, &kdhl, device, bounds, -1);
510 if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
512 "first and last dump headers disagree on %s", device);
518 status = STATUS_GOOD;
522 printf("A dump exists on %s\n", device);
527 if (kdhl.panicstring[0])
528 syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
530 syslog(LOG_ALERT, "reboot");
533 printf("Checking for available free space\n");
534 if (!check_space(savedir, dumpsize)) {
539 writebounds(bounds + 1);
541 sprintf(buf, "info.%d", bounds);
544 * Create or overwrite any existing dump header files.
546 fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
548 syslog(LOG_ERR, "%s: %m", buf);
552 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
554 sprintf(filename, "%s.%d.gz", istextdump ? "textdump.tar" :
556 fp = zopen(filename, "w");
558 sprintf(filename, "%s.%d", istextdump ? "textdump.tar" :
560 fp = fopen(filename, "w");
563 syslog(LOG_ERR, "%s: %m", filename);
570 info = fdopen(fdinfo, "w");
573 syslog(LOG_ERR, "fdopen failed: %m");
579 printheader(stdout, &kdhl, device, bounds, status);
581 printheader(info, &kdhl, device, bounds, status);
584 syslog(LOG_NOTICE, "writing %score to %s",
585 compress ? "compressed " : "", filename);
588 if (DoTextdumpFile(fd, dumpsize, lasthd, buf, device,
592 if (DoRegularFile(fd, dumpsize, buf, device, filename, fp)
599 if (fclose(fp) < 0) {
600 syslog(LOG_ERR, "error on %s: %m", filename);
607 printf("dump saved\n");
610 if (clear || !keep) {
612 printf("clearing dump header\n");
613 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
614 lseek(fd, lasthd, SEEK_SET);
615 error = write(fd, &kdhl, sizeof kdhl);
616 if (error != sizeof kdhl)
618 "error while clearing the dump header: %m");
633 fprintf(stderr, "%s\n%s\n%s\n",
634 "usage: savecore -c",
635 " savecore -C [-v] [directory device]",
636 " savecore [-fkvz] [directory [device ...]]");
641 main(int argc, char **argv)
643 const char *savedir = ".";
647 checkfor = compress = clear = force = keep = verbose = 0;
648 nfound = nsaved = nerr = 0;
650 openlog("savecore", LOG_PERROR, LOG_DAEMON);
652 while ((ch = getopt(argc, argv, "Ccfkvz")) != -1)
676 if (checkfor && (clear || force || keep))
681 error = chdir(argv[0]);
683 syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
695 if (strcmp(fsp->fs_vfstype, "swap") &&
696 strcmp(fsp->fs_vfstype, "dump"))
698 DoFile(savedir, fsp->fs_spec);
701 for (i = 0; i < argc; i++)
702 DoFile(savedir, argv[i]);
705 /* Emit minimal output. */
708 printf("No dump exists\n");
711 syslog(LOG_WARNING, "no dumps found");
713 else if (nsaved == 0) {
715 syslog(LOG_WARNING, "unsaved dumps found but not saved");
717 syslog(LOG_WARNING, "no unsaved dumps found");