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/param.h>
68 #include <sys/kerneldump.h>
69 #include <sys/diskslice.h>
70 #include <sys/ioctl.h>
71 #include <sys/mount.h>
85 /* The size of the buffer used for I/O. */
86 #define BUFFERSIZE (1024*1024)
90 #define STATUS_UNKNOWN 2
92 static int checkfor, compress, clear, force, keep, verbose; /* flags */
93 static int nfound, nsaved, nerr; /* statistics */
95 extern FILE *zopen(const char *, const char *);
98 printheader(FILE *f, const struct kerneldumpheader *h, const char *device,
99 int bounds, const int status)
103 const char *stat_str;
105 fprintf(f, "Dump header from device %s\n", device);
106 fprintf(f, " Architecture: %s\n", h->architecture);
107 fprintf(f, " Architecture Version: %u\n",
108 dtoh32(h->architectureversion));
109 dumplen = dtoh64(h->dumplength);
110 fprintf(f, " Dump Length: %lldB (%lld MB)\n", (long long)dumplen,
111 (long long)(dumplen >> 20));
112 fprintf(f, " Blocksize: %d\n", dtoh32(h->blocksize));
113 t = dtoh64(h->dumptime);
114 fprintf(f, " Dumptime: %s", ctime(&t));
115 fprintf(f, " Hostname: %s\n", h->hostname);
116 fprintf(f, " Magic: %s\n", h->magic);
117 fprintf(f, " Version String: %s", h->versionstring);
118 fprintf(f, " Panic String: %s\n", h->panicstring);
119 fprintf(f, " Dump Parity: %u\n", h->parity);
120 fprintf(f, " Bounds: %d\n", bounds);
130 stat_str = "unknown";
132 fprintf(f, " Dump Status: %s\n", stat_str);
144 if ((fp = fopen("bounds", "r")) == NULL) {
146 printf("unable to open bounds file, using 0\n");
150 if (fgets(buf, sizeof buf, fp) == NULL) {
151 syslog(LOG_WARNING, "unable to read from bounds, using 0");
157 ret = (int)strtol(buf, NULL, 10);
158 if (ret == 0 && (errno == EINVAL || errno == ERANGE))
159 syslog(LOG_WARNING, "invalid value found in bounds, using 0");
164 writebounds(int bounds) {
167 if ((fp = fopen("bounds", "w")) == NULL) {
168 syslog(LOG_WARNING, "unable to write to bounds file: %m");
173 printf("bounds number: %d\n", bounds);
175 fprintf(fp, "%d\n", bounds);
180 * Check that sufficient space is available on the disk that holds the
184 check_space(const char *savedir, off_t dumpsize)
187 off_t minfree, spacefree, totfree, needed;
189 char buf[100], path[MAXPATHLEN];
191 if (statfs(savedir, &fsbuf) < 0) {
192 syslog(LOG_ERR, "%s: %m", savedir);
195 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
196 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
198 (void)snprintf(path, sizeof(path), "%s/minfree", savedir);
199 if ((fp = fopen(path, "r")) == NULL)
202 if (fgets(buf, sizeof(buf), fp) == NULL)
209 needed = dumpsize / 1024 + 2; /* 2 for info file */
210 if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
212 "no dump, not enough free space on device (%lld available, need %lld)",
213 (long long)(minfree > 0 ? spacefree : totfree),
217 if (spacefree - needed < 0)
219 "dump performed, but free space threshold crossed");
223 #define BLOCKSIZE (1<<12)
224 #define BLOCKMASK (~(BLOCKSIZE-1))
227 DoFile(const char *savedir, const char *device)
229 static char *buf = NULL;
230 struct partinfo dpart;
231 struct kerneldumpheader kdhf, kdhl;
232 off_t mediasize, dumpsize, firsthd, lasthd, dmpcnt;
233 FILE *info, *fp, *fpkern;
235 int fd, fdinfo, fdkernin, error, wl;
236 int nr, nw, hs, he = 0;
240 bounds = getbounds();
243 status = STATUS_UNKNOWN;
246 buf = malloc(BUFFERSIZE);
248 syslog(LOG_ERR, "%m");
254 printf("checking for kernel dump on device %s\n", device);
256 fd = open(device, O_RDWR);
258 syslog(LOG_ERR, "%s: %m", device);
262 bzero(&dpart, sizeof(dpart));
263 error = ioctl(fd, DIOCGPART, &dpart);
266 "couldn't find media and/or sector size of %s: %m", device);
269 mediasize = dpart.media_size;
270 sectorsize = dpart.media_blksize;
273 printf("mediasize = %lld\n", (long long)mediasize);
274 printf("sectorsize = %u\n", sectorsize);
277 lasthd = mediasize - sectorsize;
278 lseek(fd, lasthd, SEEK_SET);
279 error = read(fd, &kdhl, sizeof kdhl);
280 if (error != sizeof kdhl) {
282 "error reading last dump header at offset %lld in %s: %m",
283 (long long)lasthd, device);
286 if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic)) {
288 printf("magic mismatch on last dump header on %s\n",
295 if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
296 sizeof kdhl.magic) == 0) {
298 printf("forcing magic on %s\n", device);
299 memcpy(kdhl.magic, KERNELDUMPMAGIC,
302 syslog(LOG_ERR, "unable to force dump - bad magic");
306 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
308 "unknown version (%d) in last dump header on %s",
309 dtoh32(kdhl.version), device);
320 if (kerneldump_parity(&kdhl)) {
322 "parity error on last dump header on %s", device);
328 dumpsize = dtoh64(kdhl.dumplength);
329 firsthd = lasthd - dumpsize - sizeof kdhf;
330 lseek(fd, firsthd, SEEK_SET);
331 error = read(fd, &kdhf, sizeof kdhf);
332 if (error != sizeof kdhf) {
334 "error reading first dump header at offset %lld in %s: %m",
335 (long long)firsthd, device);
341 printf("First dump headers:\n");
342 printheader(stdout, &kdhf, device, bounds, -1);
344 printf("\nLast dump headers:\n");
345 printheader(stdout, &kdhl, device, bounds, -1);
349 if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
351 "first and last dump headers disagree on %s", device);
357 status = STATUS_GOOD;
361 printf("A dump exists on %s\n", device);
366 if (kdhl.panicstring[0])
367 syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
369 syslog(LOG_ALERT, "reboot");
372 printf("Checking for available free space\n");
373 if (!check_space(savedir, dumpsize)) {
378 writebounds(bounds + 1);
383 fdkernin = open(getbootfile(), O_RDONLY, 0);
385 syslog(LOG_ERR, "%s: %m", getbootfile());
389 sprintf(buf, "kern.%d.gz", bounds);
390 fpkern = zopen(buf, "w");
392 sprintf(buf, "kern.%d", bounds);
393 fpkern = fopen(buf, "w");
395 if (fpkern == NULL) {
396 syslog(LOG_ERR, "%s: %m", buf);
400 syslog(LOG_NOTICE, "writing %skernel to %s",
401 compress ? "compressed " : "", buf);
403 while ((nr = read(fdkernin, buf, BUFFERSIZE)) > 0) {
404 nw = fwrite(buf, 1, nr, fpkern);
406 syslog(LOG_ERR, "kern.%d: %m", bounds);
408 "WARNING: kernel may be incomplete");
413 syslog(LOG_ERR, "%s: %m", getbootfile());
415 "WARNING: kernel may be incomplete");
422 sprintf(buf, "info.%d", bounds);
425 * Create or overwrite any existing dump header files.
427 fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
429 syslog(LOG_ERR, "%s: %m", buf);
433 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
435 sprintf(buf, "vmcore.%d.gz", bounds);
436 fp = zopen(buf, "w");
438 sprintf(buf, "vmcore.%d", bounds);
439 fp = fopen(buf, "w");
442 syslog(LOG_ERR, "%s: %m", buf);
449 info = fdopen(fdinfo, "w");
452 syslog(LOG_ERR, "fdopen failed: %m");
458 printheader(stdout, &kdhl, device, bounds, status);
460 printheader(info, &kdhl, device, bounds, status);
463 syslog(LOG_NOTICE, "writing %score to %s",
464 compress ? "compressed " : "", buf);
466 while (dumpsize > 0) {
470 nr = read(fd, buf, wl);
474 "WARNING: EOF on dump device");
476 syslog(LOG_ERR, "read error on %s: %m", device);
481 nw = fwrite(buf, 1, wl, fp);
483 for (nw = 0; nw < nr; nw = he) {
484 /* find a contiguous block of zeroes */
485 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
486 for (he = hs; he < nr && buf[he] == 0;
489 /* is the hole long enough to matter? */
490 if (he >= hs + BLOCKSIZE)
494 /* back down to a block boundary */
498 * 1) Don't go beyond the end of the buffer.
499 * 2) If the end of the buffer is less than
500 * BLOCKSIZE bytes away, we're at the end
501 * of the file, so just grab what's left.
503 if (hs + BLOCKSIZE > nr)
507 * At this point, we have a partial ordering:
508 * nw <= hs <= he <= nr
509 * If hs > nw, buf[nw..hs] contains non-zero data.
510 * If he > hs, buf[hs..he] is all zeroes.
513 if (fwrite(buf + nw, hs - nw, 1, fp)
517 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
523 "write error on vmcore.%d file: %m", bounds);
525 "WARNING: vmcore may be incomplete");
531 printf("%llu\r", (unsigned long long)dmpcnt);
539 if (fclose(fp) < 0) {
540 syslog(LOG_ERR, "error on vmcore.%d: %m", bounds);
547 printf("dump saved\n");
550 if (clear || !keep) {
552 printf("clearing dump header\n");
553 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
554 lseek(fd, lasthd, SEEK_SET);
555 error = write(fd, &kdhl, sizeof kdhl);
556 if (error != sizeof kdhl)
558 "error while clearing the dump header: %m");
573 fprintf(stderr, "%s\n%s\n%s\n",
574 "usage: savecore -c",
575 " savecore -C [-v] [directory device]",
576 " savecore [-fkvz] [directory [device ...]]");
581 main(int argc, char **argv)
583 const char *savedir = ".";
587 checkfor = compress = clear = force = keep = verbose = 0;
588 nfound = nsaved = nerr = 0;
590 openlog("savecore", LOG_PERROR, LOG_DAEMON);
592 while ((ch = getopt(argc, argv, "Ccfkvz")) != -1)
616 if (checkfor && (clear || force || keep))
621 error = chdir(argv[0]);
623 syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
635 if (strcmp(fsp->fs_vfstype, "swap") &&
636 strcmp(fsp->fs_vfstype, "dump"))
638 DoFile(savedir, fsp->fs_spec);
641 for (i = 0; i < argc; i++)
642 DoFile(savedir, argv[i]);
645 /* Emit minimal output. */
648 printf("No dump exists\n");
651 syslog(LOG_WARNING, "no dumps found");
653 else if (nsaved == 0) {
655 syslog(LOG_WARNING, "unsaved dumps found but not saved");
657 syslog(LOG_WARNING, "no unsaved dumps found");