2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Copyright (c) 1986, 1992, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
68 #include <sys/param.h>
70 #include <sys/kerneldump.h>
71 #include <sys/mount.h>
74 #include <capsicum_helpers.h>
90 #include <libcasper.h>
91 #include <casper/cap_fileargs.h>
92 #include <casper/cap_syslog.h>
96 /* The size of the buffer used for I/O. */
97 #define BUFFERSIZE (1024*1024)
100 #define STATUS_GOOD 1
101 #define STATUS_UNKNOWN 2
103 static cap_channel_t *capsyslog;
104 static fileargs_t *capfa;
105 static int checkfor, compress, clear, force, keep, verbose; /* flags */
106 static int nfound, nsaved, nerr; /* statistics */
109 extern FILE *zdopen(int, const char *);
111 static sig_atomic_t got_siginfo;
112 static void infohandler(int);
115 logmsg(int pri, const char *fmt, ...)
120 if (capsyslog != NULL)
121 cap_vsyslog(capsyslog, pri, fmt, ap);
123 vsyslog(pri, fmt, ap);
128 xfopenat(int dirfd, const char *path, int flags, const char *modestr, ...)
135 if ((flags & O_CREAT) == O_CREAT) {
136 va_start(ap, modestr);
137 mode = (mode_t)va_arg(ap, int);
142 fd = openat(dirfd, path, flags, mode);
145 fp = fdopen(fd, modestr);
155 printheader(xo_handle_t *xo, const struct kerneldumpheader *h,
156 const char *device, int bounds, const int status)
162 const char *stat_str;
163 const char *comp_str;
166 xo_emit_h(xo, "{Lwc:Dump header from device}{:dump_device/%s}\n",
168 xo_emit_h(xo, "{P: }{Lwc:Architecture}{:architecture/%s}\n",
171 "{P: }{Lwc:Architecture Version}{:architecture_version/%u}\n",
172 dtoh32(h->architectureversion));
173 dumplen = dtoh64(h->dumplength);
174 xo_emit_h(xo, "{P: }{Lwc:Dump Length}{:dump_length_bytes/%lld}\n",
176 xo_emit_h(xo, "{P: }{Lwc:Blocksize}{:blocksize/%d}\n",
177 dtoh32(h->blocksize));
178 switch (h->compression) {
179 case KERNELDUMP_COMP_NONE:
182 case KERNELDUMP_COMP_GZIP:
185 case KERNELDUMP_COMP_ZSTD:
192 xo_emit_h(xo, "{P: }{Lwc:Compression}{:compression/%s}\n", comp_str);
193 t = dtoh64(h->dumptime);
194 localtime_r(&t, &tm);
195 if (strftime(time_str, sizeof(time_str), "%F %T %z", &tm) == 0)
197 xo_emit_h(xo, "{P: }{Lwc:Dumptime}{:dumptime/%s}\n", time_str);
198 xo_emit_h(xo, "{P: }{Lwc:Hostname}{:hostname/%s}\n", h->hostname);
199 xo_emit_h(xo, "{P: }{Lwc:Magic}{:magic/%s}\n", h->magic);
200 xo_emit_h(xo, "{P: }{Lwc:Version String}{:version_string/%s}",
202 xo_emit_h(xo, "{P: }{Lwc:Panic String}{:panic_string/%s}\n",
204 xo_emit_h(xo, "{P: }{Lwc:Dump Parity}{:dump_parity/%u}\n", h->parity);
205 xo_emit_h(xo, "{P: }{Lwc:Bounds}{:bounds/%d}\n", bounds);
215 stat_str = "unknown";
218 xo_emit_h(xo, "{P: }{Lwc:Dump Status}{:dump_status/%s}\n", stat_str);
223 getbounds(int savedirfd)
230 * If we are just checking, then we haven't done a chdir to the dump
231 * directory and we should not try to read a bounds file.
238 if ((fp = xfopenat(savedirfd, "bounds", O_RDONLY, "r")) == NULL) {
240 printf("unable to open bounds file, using 0\n");
243 if (fgets(buf, sizeof(buf), fp) == NULL) {
245 logmsg(LOG_WARNING, "bounds file is empty, using 0");
247 logmsg(LOG_WARNING, "bounds file: %s", strerror(errno));
253 ret = (int)strtol(buf, NULL, 10);
254 if (ret == 0 && (errno == EINVAL || errno == ERANGE))
255 logmsg(LOG_WARNING, "invalid value found in bounds, using 0");
261 writebounds(int savedirfd, int bounds)
265 if ((fp = xfopenat(savedirfd, "bounds", O_WRONLY | O_CREAT | O_TRUNC,
266 "w", 0644)) == NULL) {
267 logmsg(LOG_WARNING, "unable to write to bounds file: %m");
272 printf("bounds number: %d\n", bounds);
274 fprintf(fp, "%d\n", bounds);
279 writekey(int savedirfd, const char *keyname, uint8_t *dumpkey,
280 uint32_t dumpkeysize)
284 fd = openat(savedirfd, keyname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
286 logmsg(LOG_ERR, "Unable to open %s to write the key: %m.",
291 if (write(fd, dumpkey, dumpkeysize) != (ssize_t)dumpkeysize) {
292 logmsg(LOG_ERR, "Unable to write the key to %s: %m.", keyname);
302 file_size(int savedirfd, const char *path)
306 /* Ignore all errors, this file may not exist. */
307 if (fstatat(savedirfd, path, &sb, 0) == -1)
313 saved_dump_size(int savedirfd, int bounds)
315 static char path[PATH_MAX];
320 (void)snprintf(path, sizeof(path), "info.%d", bounds);
321 dumpsize += file_size(savedirfd, path);
322 (void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
323 dumpsize += file_size(savedirfd, path);
324 (void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
325 dumpsize += file_size(savedirfd, path);
326 (void)snprintf(path, sizeof(path), "vmcore.%d.zst", bounds);
327 dumpsize += file_size(savedirfd, path);
328 (void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
329 dumpsize += file_size(savedirfd, path);
330 (void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
331 dumpsize += file_size(savedirfd, path);
337 saved_dump_remove(int savedirfd, int bounds)
339 static char path[PATH_MAX];
341 (void)snprintf(path, sizeof(path), "info.%d", bounds);
342 (void)unlinkat(savedirfd, path, 0);
343 (void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
344 (void)unlinkat(savedirfd, path, 0);
345 (void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
346 (void)unlinkat(savedirfd, path, 0);
347 (void)snprintf(path, sizeof(path), "vmcore.%d.zst", bounds);
348 (void)unlinkat(savedirfd, path, 0);
349 (void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
350 (void)unlinkat(savedirfd, path, 0);
351 (void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
352 (void)unlinkat(savedirfd, path, 0);
356 symlinks_remove(int savedirfd)
359 (void)unlinkat(savedirfd, "info.last", 0);
360 (void)unlinkat(savedirfd, "key.last", 0);
361 (void)unlinkat(savedirfd, "vmcore.last", 0);
362 (void)unlinkat(savedirfd, "vmcore.last.gz", 0);
363 (void)unlinkat(savedirfd, "vmcore.last.zst", 0);
364 (void)unlinkat(savedirfd, "vmcore_encrypted.last", 0);
365 (void)unlinkat(savedirfd, "vmcore_encrypted.last.gz", 0);
366 (void)unlinkat(savedirfd, "textdump.tar.last", 0);
367 (void)unlinkat(savedirfd, "textdump.tar.last.gz", 0);
371 * Check that sufficient space is available on the disk that holds the
375 check_space(const char *savedir, int savedirfd, off_t dumpsize, int bounds)
380 off_t available, minfree, spacefree, totfree, needed;
382 if (fstatfs(savedirfd, &fsbuf) < 0) {
383 logmsg(LOG_ERR, "%s: %m", savedir);
386 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
387 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
389 if ((fp = xfopenat(savedirfd, "minfree", O_RDONLY, "r")) == NULL)
392 if (fgets(buf, sizeof(buf), fp) == NULL)
398 minfree = strtoll(buf, &endp, 10);
399 if (minfree == 0 && errno != 0)
402 while (*endp != '\0' && isspace(*endp))
404 if (*endp != '\0' || minfree < 0)
409 "`minfree` didn't contain a valid size "
410 "(`%s`). Defaulting to 0", buf);
415 available = minfree > 0 ? spacefree - minfree : totfree;
416 needed = dumpsize / 1024 + 2; /* 2 for info file */
417 needed -= saved_dump_size(savedirfd, bounds);
418 if (available < needed) {
420 "no dump: not enough free space on device (need at least "
421 "%jdkB for dump; %jdkB available; %jdkB reserved)",
423 (intmax_t)available + minfree,
427 if (spacefree - needed < 0)
429 "dump performed, but free space threshold crossed");
434 compare_magic(const struct kerneldumpheader *kdh, const char *magic)
437 return (strncmp(kdh->magic, magic, sizeof(kdh->magic)) == 0);
440 #define BLOCKSIZE (1<<12)
441 #define BLOCKMASK (~(BLOCKSIZE-1))
444 DoRegularFile(int fd, off_t dumpsize, u_int sectorsize, bool sparse, char *buf,
445 const char *device, const char *filename, FILE *fp)
447 int he, hs, nr, nw, wl;
448 off_t dmpcnt, origsize;
453 while (dumpsize > 0) {
457 nr = read(fd, buf, roundup(wl, sectorsize));
458 if (nr != (int)roundup(wl, sectorsize)) {
461 "WARNING: EOF on dump device");
463 logmsg(LOG_ERR, "read error on %s: %m", device);
468 nw = fwrite(buf, 1, wl, fp);
470 for (nw = 0; nw < nr; nw = he) {
471 /* find a contiguous block of zeroes */
472 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
473 for (he = hs; he < nr && buf[he] == 0;
476 /* is the hole long enough to matter? */
477 if (he >= hs + BLOCKSIZE)
481 /* back down to a block boundary */
485 * 1) Don't go beyond the end of the buffer.
486 * 2) If the end of the buffer is less than
487 * BLOCKSIZE bytes away, we're at the end
488 * of the file, so just grab what's left.
490 if (hs + BLOCKSIZE > nr)
494 * At this point, we have a partial ordering:
495 * nw <= hs <= he <= nr
496 * If hs > nw, buf[nw..hs] contains non-zero
497 * data. If he > hs, buf[hs..he] is all zeroes.
500 if (fwrite(buf + nw, hs - nw, 1, fp)
504 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
510 "write error on %s file: %m", filename);
512 "WARNING: vmcore may be incomplete");
518 printf("%llu\r", (unsigned long long)dmpcnt);
523 printf("%s %.1lf%%\n", filename, (100.0 - (100.0 *
524 (double)dumpsize / (double)origsize)));
532 * Specialized version of dump-reading logic for use with textdumps, which
533 * are written backwards from the end of the partition, and must be reversed
534 * before being written to the file. Textdumps are small, so do a bit less
535 * work to optimize/sparsify.
538 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
539 const char *device, const char *filename, FILE *fp)
542 off_t dmpcnt, totsize;
547 if ((dumpsize % wl) != 0) {
548 logmsg(LOG_ERR, "textdump uneven multiple of 512 on %s",
553 while (dumpsize > 0) {
554 nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
558 "WARNING: EOF on dump device");
560 logmsg(LOG_ERR, "read error on %s: %m", device);
564 nw = fwrite(buf, 1, wl, fp);
567 "write error on %s file: %m", filename);
569 "WARNING: textdump may be incomplete");
575 printf("%llu\r", (unsigned long long)dmpcnt);
584 DoFile(const char *savedir, int savedirfd, const char *device)
586 xo_handle_t *xostdout, *xoinfo;
587 static char infoname[PATH_MAX], corename[PATH_MAX], linkname[PATH_MAX];
588 static char keyname[PATH_MAX];
589 static char *buf = NULL;
591 struct kerneldumpheader kdhf, kdhl;
593 off_t mediasize, dumpextent, dumplength, firsthd, lasthd;
595 int fdcore, fddev, error;
597 u_int sectorsize, xostyle;
598 uint32_t dumpkeysize;
599 bool iscompressed, isencrypted, istextdump, ret;
601 bounds = getbounds(savedirfd);
604 status = STATUS_UNKNOWN;
606 xostdout = xo_create_to_file(stdout, XO_STYLE_TEXT, 0);
607 if (xostdout == NULL) {
608 logmsg(LOG_ERR, "%s: %m", infoname);
612 if (maxdumps > 0 && bounds == maxdumps)
616 buf = malloc(BUFFERSIZE);
618 logmsg(LOG_ERR, "%m");
624 printf("checking for kernel dump on device %s\n", device);
626 fddev = fileargs_open(capfa, device);
628 logmsg(LOG_ERR, "%s: %m", device);
632 error = ioctl(fddev, DIOCGMEDIASIZE, &mediasize);
634 error = ioctl(fddev, DIOCGSECTORSIZE, §orsize);
637 "couldn't find media and/or sector size of %s: %m", device);
642 printf("mediasize = %lld bytes\n", (long long)mediasize);
643 printf("sectorsize = %u bytes\n", sectorsize);
646 if (sectorsize < sizeof(kdhl)) {
648 "Sector size is less the kernel dump header %zu",
653 lasthd = mediasize - sectorsize;
654 temp = malloc(sectorsize);
656 logmsg(LOG_ERR, "%m");
659 if (lseek(fddev, lasthd, SEEK_SET) != lasthd ||
660 read(fddev, temp, sectorsize) != (ssize_t)sectorsize) {
662 "error reading last dump header at offset %lld in %s: %m",
663 (long long)lasthd, device);
666 memcpy(&kdhl, temp, sizeof(kdhl));
667 iscompressed = istextdump = false;
668 if (compare_magic(&kdhl, TEXTDUMPMAGIC)) {
670 printf("textdump magic on last dump header on %s\n",
673 if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) {
675 "unknown version (%d) in last dump header on %s",
676 dtoh32(kdhl.version), device);
682 } else if (compare_magic(&kdhl, KERNELDUMPMAGIC)) {
683 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
685 "unknown version (%d) in last dump header on %s",
686 dtoh32(kdhl.version), device);
692 switch (kdhl.compression) {
693 case KERNELDUMP_COMP_NONE:
695 case KERNELDUMP_COMP_GZIP:
696 case KERNELDUMP_COMP_ZSTD:
697 if (compress && verbose)
698 printf("dump is already compressed\n");
703 logmsg(LOG_ERR, "unknown compression type %d on %s",
704 kdhl.compression, device);
709 printf("magic mismatch on last dump header on %s\n",
716 if (compare_magic(&kdhl, KERNELDUMPMAGIC_CLEARED)) {
718 printf("forcing magic on %s\n", device);
719 memcpy(kdhl.magic, KERNELDUMPMAGIC, sizeof(kdhl.magic));
721 logmsg(LOG_ERR, "unable to force dump - bad magic");
724 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
726 "unknown version (%d) in last dump header on %s",
727 dtoh32(kdhl.version), device);
739 if (kerneldump_parity(&kdhl)) {
741 "parity error on last dump header on %s", device);
747 dumpextent = dtoh64(kdhl.dumpextent);
748 dumplength = dtoh64(kdhl.dumplength);
749 dumpkeysize = dtoh32(kdhl.dumpkeysize);
750 firsthd = lasthd - dumpextent - sectorsize - dumpkeysize;
751 if (lseek(fddev, firsthd, SEEK_SET) != firsthd ||
752 read(fddev, temp, sectorsize) != (ssize_t)sectorsize) {
754 "error reading first dump header at offset %lld in %s: %m",
755 (long long)firsthd, device);
759 memcpy(&kdhf, temp, sizeof(kdhf));
762 printf("First dump headers:\n");
763 printheader(xostdout, &kdhf, device, bounds, -1);
765 printf("\nLast dump headers:\n");
766 printheader(xostdout, &kdhl, device, bounds, -1);
770 if (memcmp(&kdhl, &kdhf, sizeof(kdhl))) {
772 "first and last dump headers disagree on %s", device);
778 status = STATUS_GOOD;
782 printf("A dump exists on %s\n", device);
787 if (kdhl.panicstring[0] != '\0')
788 logmsg(LOG_ALERT, "reboot after panic: %.*s",
789 (int)sizeof(kdhl.panicstring), kdhl.panicstring);
791 logmsg(LOG_ALERT, "reboot");
794 printf("Checking for available free space\n");
796 if (!check_space(savedir, savedirfd, dumplength, bounds)) {
801 writebounds(savedirfd, bounds + 1);
803 saved_dump_remove(savedirfd, bounds);
805 snprintf(infoname, sizeof(infoname), "info.%d", bounds);
808 * Create or overwrite any existing dump header files.
810 if ((info = xfopenat(savedirfd, infoname,
811 O_WRONLY | O_CREAT | O_TRUNC, "w", 0600)) == NULL) {
812 logmsg(LOG_ERR, "open(%s): %m", infoname);
817 isencrypted = (dumpkeysize > 0);
819 snprintf(corename, sizeof(corename), "%s.%d.gz",
820 istextdump ? "textdump.tar" :
821 (isencrypted ? "vmcore_encrypted" : "vmcore"), bounds);
822 else if (iscompressed && !isencrypted)
823 snprintf(corename, sizeof(corename), "vmcore.%d.%s", bounds,
824 (kdhl.compression == KERNELDUMP_COMP_GZIP) ? "gz" : "zst");
826 snprintf(corename, sizeof(corename), "%s.%d",
827 istextdump ? "textdump.tar" :
828 (isencrypted ? "vmcore_encrypted" : "vmcore"), bounds);
829 fdcore = openat(savedirfd, corename, O_WRONLY | O_CREAT | O_TRUNC,
832 logmsg(LOG_ERR, "open(%s): %m", corename);
839 core = zdopen(fdcore, "w");
841 core = fdopen(fdcore, "w");
843 logmsg(LOG_ERR, "%s: %m", corename);
851 xostyle = xo_get_style(NULL);
852 xoinfo = xo_create_to_file(info, xostyle, 0);
853 if (xoinfo == NULL) {
854 logmsg(LOG_ERR, "%s: %m", infoname);
859 xo_open_container_h(xoinfo, "crashdump");
862 printheader(xostdout, &kdhl, device, bounds, status);
864 printheader(xoinfo, &kdhl, device, bounds, status);
865 xo_close_container_h(xoinfo, "crashdump");
871 dumpkey = calloc(1, dumpkeysize);
872 if (dumpkey == NULL) {
873 logmsg(LOG_ERR, "Unable to allocate kernel dump key.");
878 if (read(fddev, dumpkey, dumpkeysize) != (ssize_t)dumpkeysize) {
879 logmsg(LOG_ERR, "Unable to read kernel dump key: %m.");
884 snprintf(keyname, sizeof(keyname), "key.%d", bounds);
885 ret = writekey(savedirfd, keyname, dumpkey, dumpkeysize);
886 explicit_bzero(dumpkey, dumpkeysize);
893 logmsg(LOG_NOTICE, "writing %s%score to %s/%s",
894 isencrypted ? "encrypted " : "", compress ? "compressed " : "",
898 if (DoTextdumpFile(fddev, dumplength, lasthd, buf, device,
902 if (DoRegularFile(fddev, dumplength, sectorsize,
903 !(compress || iscompressed || isencrypted), buf, device,
904 corename, core) < 0) {
911 if (fclose(core) < 0) {
912 logmsg(LOG_ERR, "error on %s: %m", corename);
917 symlinks_remove(savedirfd);
918 if (symlinkat(infoname, savedirfd, "info.last") == -1) {
919 logmsg(LOG_WARNING, "unable to create symlink %s/%s: %m",
920 savedir, "info.last");
923 if (symlinkat(keyname, savedirfd, "key.last") == -1) {
925 "unable to create symlink %s/%s: %m", savedir,
929 if (compress || iscompressed) {
930 snprintf(linkname, sizeof(linkname), "%s.last.%s",
931 istextdump ? "textdump.tar" :
932 (isencrypted ? "vmcore_encrypted" : "vmcore"),
933 (kdhl.compression == KERNELDUMP_COMP_ZSTD) ? "zst" : "gz");
935 snprintf(linkname, sizeof(linkname), "%s.last",
936 istextdump ? "textdump.tar" :
937 (isencrypted ? "vmcore_encrypted" : "vmcore"));
939 if (symlinkat(corename, savedirfd, linkname) == -1) {
940 logmsg(LOG_WARNING, "unable to create symlink %s/%s: %m",
947 printf("dump saved\n");
952 printf("clearing dump header\n");
953 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof(kdhl.magic));
954 memcpy(temp, &kdhl, sizeof(kdhl));
955 if (lseek(fddev, lasthd, SEEK_SET) != lasthd ||
956 write(fddev, temp, sectorsize) != (ssize_t)sectorsize)
958 "error while clearing the dump header: %m");
960 xo_close_container_h(xostdout, "crashdump");
961 xo_finish_h(xostdout);
976 /* Prepend "/dev/" to any arguments that don't already have it */
978 devify(int argc, char **argv)
983 devs = malloc(argc * sizeof(*argv));
985 logmsg(LOG_ERR, "malloc(): %m");
988 for (i = 0; i < argc; i++) {
989 if (strncmp(argv[i], _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
990 devs[i] = strdup(argv[i]);
994 fullpath = malloc(PATH_MAX);
995 if (fullpath == NULL) {
996 logmsg(LOG_ERR, "malloc(): %m");
999 l = snprintf(fullpath, PATH_MAX, "%s%s", _PATH_DEV,
1002 logmsg(LOG_ERR, "snprintf(): %m");
1004 } else if (l >= PATH_MAX) {
1005 logmsg(LOG_ERR, "device name too long");
1015 enum_dumpdevs(int *argcp)
1022 * We cannot use getfsent(3) in capability mode, so we must
1023 * scan /etc/fstab and build up a list of candidate devices
1024 * before proceeding.
1028 argv = malloc(n * sizeof(*argv));
1030 logmsg(LOG_ERR, "malloc(): %m");
1037 if (strcmp(fsp->fs_vfstype, "swap") != 0 &&
1038 strcmp(fsp->fs_vfstype, "dump") != 0)
1042 argv = realloc(argv, n * sizeof(*argv));
1044 logmsg(LOG_ERR, "realloc(): %m");
1048 argv[argc] = strdup(fsp->fs_spec);
1049 if (argv[argc] == NULL) {
1050 logmsg(LOG_ERR, "strdup(): %m");
1060 init_caps(int argc, char **argv)
1062 cap_rights_t rights;
1063 cap_channel_t *capcas;
1065 capcas = cap_init();
1066 if (capcas == NULL) {
1067 logmsg(LOG_ERR, "cap_init(): %m");
1071 * The fileargs capability does not currently provide a way to limit
1074 (void)cap_rights_init(&rights, CAP_PREAD, CAP_WRITE, CAP_IOCTL);
1075 capfa = fileargs_init(argc, argv, checkfor || keep ? O_RDONLY : O_RDWR,
1076 0, &rights, FA_OPEN);
1077 if (capfa == NULL) {
1078 logmsg(LOG_ERR, "fileargs_init(): %m");
1081 caph_cache_catpages();
1082 caph_cache_tzdata();
1083 if (caph_enter_casper() != 0) {
1084 logmsg(LOG_ERR, "caph_enter_casper(): %m");
1087 capsyslog = cap_service_open(capcas, "system.syslog");
1088 if (capsyslog == NULL) {
1089 logmsg(LOG_ERR, "cap_service_open(system.syslog): %m");
1098 xo_error("%s\n%s\n%s\n",
1099 "usage: savecore -c [-v] [device ...]",
1100 " savecore -C [-v] [device ...]",
1101 " savecore [-fkvz] [-m maxdumps] [directory [device ...]]");
1106 main(int argc, char **argv)
1108 cap_rights_t rights;
1109 const char *savedir;
1111 int i, ch, error, savedirfd;
1113 checkfor = compress = clear = force = keep = verbose = 0;
1114 nfound = nsaved = nerr = 0;
1117 openlog("savecore", LOG_PERROR, LOG_DAEMON);
1118 signal(SIGINFO, infohandler);
1120 argc = xo_parse_args(argc, argv);
1124 while ((ch = getopt(argc, argv, "Ccfkm:vz")) != -1)
1139 maxdumps = atoi(optarg);
1140 if (maxdumps <= 0) {
1141 logmsg(LOG_ERR, "Invalid maxdump value");
1155 if (checkfor && (clear || force || keep))
1157 if (clear && (compress || keep))
1159 if (maxdumps > 0 && (checkfor || clear))
1163 if (argc >= 1 && !checkfor && !clear) {
1164 error = chdir(argv[0]);
1166 logmsg(LOG_ERR, "chdir(%s): %m", argv[0]);
1174 devs = enum_dumpdevs(&argc);
1176 devs = devify(argc, argv);
1178 savedirfd = open(savedir, O_RDONLY | O_DIRECTORY);
1179 if (savedirfd < 0) {
1180 logmsg(LOG_ERR, "open(%s): %m", savedir);
1183 (void)cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL, CAP_FSTATAT,
1184 CAP_FSTATFS, CAP_PREAD, CAP_SYMLINKAT, CAP_FTRUNCATE, CAP_UNLINKAT,
1186 if (caph_rights_limit(savedirfd, &rights) < 0) {
1187 logmsg(LOG_ERR, "cap_rights_limit(): %m");
1191 /* Enter capability mode. */
1192 init_caps(argc, devs);
1194 for (i = 0; i < argc; i++)
1195 DoFile(savedir, savedirfd, devs[i]);
1197 /* Emit minimal output. */
1201 printf("No dump exists\n");
1205 logmsg(LOG_WARNING, "no dumps found");
1206 } else if (nsaved == 0) {
1210 "unsaved dumps found but not saved");
1213 logmsg(LOG_WARNING, "no unsaved dumps found");
1220 infohandler(int sig __unused)