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)
160 const char *stat_str;
161 const char *comp_str;
164 xo_emit_h(xo, "{Lwc:Dump header from device}{:dump_device/%s}\n",
166 xo_emit_h(xo, "{P: }{Lwc:Architecture}{:architecture/%s}\n",
169 "{P: }{Lwc:Architecture Version}{:architecture_version/%u}\n",
170 dtoh32(h->architectureversion));
171 dumplen = dtoh64(h->dumplength);
172 xo_emit_h(xo, "{P: }{Lwc:Dump Length}{:dump_length_bytes/%lld}\n",
174 xo_emit_h(xo, "{P: }{Lwc:Blocksize}{:blocksize/%d}\n",
175 dtoh32(h->blocksize));
176 switch (h->compression) {
177 case KERNELDUMP_COMP_NONE:
180 case KERNELDUMP_COMP_GZIP:
183 case KERNELDUMP_COMP_ZSTD:
190 xo_emit_h(xo, "{P: }{Lwc:Compression}{:compression/%s}\n", comp_str);
191 t = dtoh64(h->dumptime);
192 xo_emit_h(xo, "{P: }{Lwc:Dumptime}{:dumptime/%s}", ctime(&t));
193 xo_emit_h(xo, "{P: }{Lwc:Hostname}{:hostname/%s}\n", h->hostname);
194 xo_emit_h(xo, "{P: }{Lwc:Magic}{:magic/%s}\n", h->magic);
195 xo_emit_h(xo, "{P: }{Lwc:Version String}{:version_string/%s}",
197 xo_emit_h(xo, "{P: }{Lwc:Panic String}{:panic_string/%s}\n",
199 xo_emit_h(xo, "{P: }{Lwc:Dump Parity}{:dump_parity/%u}\n", h->parity);
200 xo_emit_h(xo, "{P: }{Lwc:Bounds}{:bounds/%d}\n", bounds);
210 stat_str = "unknown";
213 xo_emit_h(xo, "{P: }{Lwc:Dump Status}{:dump_status/%s}\n", stat_str);
218 getbounds(int savedirfd)
225 * If we are just checking, then we haven't done a chdir to the dump
226 * directory and we should not try to read a bounds file.
233 if ((fp = xfopenat(savedirfd, "bounds", O_RDONLY, "r")) == NULL) {
235 printf("unable to open bounds file, using 0\n");
238 if (fgets(buf, sizeof(buf), fp) == NULL) {
240 logmsg(LOG_WARNING, "bounds file is empty, using 0");
242 logmsg(LOG_WARNING, "bounds file: %s", strerror(errno));
248 ret = (int)strtol(buf, NULL, 10);
249 if (ret == 0 && (errno == EINVAL || errno == ERANGE))
250 logmsg(LOG_WARNING, "invalid value found in bounds, using 0");
256 writebounds(int savedirfd, int bounds)
260 if ((fp = xfopenat(savedirfd, "bounds", O_WRONLY | O_CREAT | O_TRUNC,
261 "w", 0644)) == NULL) {
262 logmsg(LOG_WARNING, "unable to write to bounds file: %m");
267 printf("bounds number: %d\n", bounds);
269 fprintf(fp, "%d\n", bounds);
274 writekey(int savedirfd, const char *keyname, uint8_t *dumpkey,
275 uint32_t dumpkeysize)
279 fd = openat(savedirfd, keyname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
281 logmsg(LOG_ERR, "Unable to open %s to write the key: %m.",
286 if (write(fd, dumpkey, dumpkeysize) != (ssize_t)dumpkeysize) {
287 logmsg(LOG_ERR, "Unable to write the key to %s: %m.", keyname);
297 file_size(int savedirfd, const char *path)
301 /* Ignore all errors, this file may not exist. */
302 if (fstatat(savedirfd, path, &sb, 0) == -1)
308 saved_dump_size(int savedirfd, int bounds)
310 static char path[PATH_MAX];
315 (void)snprintf(path, sizeof(path), "info.%d", bounds);
316 dumpsize += file_size(savedirfd, path);
317 (void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
318 dumpsize += file_size(savedirfd, path);
319 (void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
320 dumpsize += file_size(savedirfd, path);
321 (void)snprintf(path, sizeof(path), "vmcore.%d.zst", bounds);
322 dumpsize += file_size(savedirfd, path);
323 (void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
324 dumpsize += file_size(savedirfd, path);
325 (void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
326 dumpsize += file_size(savedirfd, path);
332 saved_dump_remove(int savedirfd, int bounds)
334 static char path[PATH_MAX];
336 (void)snprintf(path, sizeof(path), "info.%d", bounds);
337 (void)unlinkat(savedirfd, path, 0);
338 (void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
339 (void)unlinkat(savedirfd, path, 0);
340 (void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
341 (void)unlinkat(savedirfd, path, 0);
342 (void)snprintf(path, sizeof(path), "vmcore.%d.zst", bounds);
343 (void)unlinkat(savedirfd, path, 0);
344 (void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
345 (void)unlinkat(savedirfd, path, 0);
346 (void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
347 (void)unlinkat(savedirfd, path, 0);
351 symlinks_remove(int savedirfd)
354 (void)unlinkat(savedirfd, "info.last", 0);
355 (void)unlinkat(savedirfd, "key.last", 0);
356 (void)unlinkat(savedirfd, "vmcore.last", 0);
357 (void)unlinkat(savedirfd, "vmcore.last.gz", 0);
358 (void)unlinkat(savedirfd, "vmcore.last.zst", 0);
359 (void)unlinkat(savedirfd, "vmcore_encrypted.last", 0);
360 (void)unlinkat(savedirfd, "vmcore_encrypted.last.gz", 0);
361 (void)unlinkat(savedirfd, "textdump.tar.last", 0);
362 (void)unlinkat(savedirfd, "textdump.tar.last.gz", 0);
366 * Check that sufficient space is available on the disk that holds the
370 check_space(const char *savedir, int savedirfd, off_t dumpsize, int bounds)
375 off_t available, minfree, spacefree, totfree, needed;
377 if (fstatfs(savedirfd, &fsbuf) < 0) {
378 logmsg(LOG_ERR, "%s: %m", savedir);
381 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
382 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
384 if ((fp = xfopenat(savedirfd, "minfree", O_RDONLY, "r")) == NULL)
387 if (fgets(buf, sizeof(buf), fp) == NULL)
393 minfree = strtoll(buf, &endp, 10);
394 if (minfree == 0 && errno != 0)
397 while (*endp != '\0' && isspace(*endp))
399 if (*endp != '\0' || minfree < 0)
404 "`minfree` didn't contain a valid size "
405 "(`%s`). Defaulting to 0", buf);
410 available = minfree > 0 ? spacefree - minfree : totfree;
411 needed = dumpsize / 1024 + 2; /* 2 for info file */
412 needed -= saved_dump_size(savedirfd, bounds);
413 if (available < needed) {
415 "no dump: not enough free space on device (need at least "
416 "%jdkB for dump; %jdkB available; %jdkB reserved)",
418 (intmax_t)available + minfree,
422 if (spacefree - needed < 0)
424 "dump performed, but free space threshold crossed");
429 compare_magic(const struct kerneldumpheader *kdh, const char *magic)
432 return (strncmp(kdh->magic, magic, sizeof(kdh->magic)) == 0);
435 #define BLOCKSIZE (1<<12)
436 #define BLOCKMASK (~(BLOCKSIZE-1))
439 DoRegularFile(int fd, off_t dumpsize, u_int sectorsize, bool sparse, char *buf,
440 const char *device, const char *filename, FILE *fp)
442 int he, hs, nr, nw, wl;
443 off_t dmpcnt, origsize;
448 while (dumpsize > 0) {
452 nr = read(fd, buf, roundup(wl, sectorsize));
453 if (nr != (int)roundup(wl, sectorsize)) {
456 "WARNING: EOF on dump device");
458 logmsg(LOG_ERR, "read error on %s: %m", device);
463 nw = fwrite(buf, 1, wl, fp);
465 for (nw = 0; nw < nr; nw = he) {
466 /* find a contiguous block of zeroes */
467 for (hs = nw; hs < nr; hs += BLOCKSIZE) {
468 for (he = hs; he < nr && buf[he] == 0;
471 /* is the hole long enough to matter? */
472 if (he >= hs + BLOCKSIZE)
476 /* back down to a block boundary */
480 * 1) Don't go beyond the end of the buffer.
481 * 2) If the end of the buffer is less than
482 * BLOCKSIZE bytes away, we're at the end
483 * of the file, so just grab what's left.
485 if (hs + BLOCKSIZE > nr)
489 * At this point, we have a partial ordering:
490 * nw <= hs <= he <= nr
491 * If hs > nw, buf[nw..hs] contains non-zero
492 * data. If he > hs, buf[hs..he] is all zeroes.
495 if (fwrite(buf + nw, hs - nw, 1, fp)
499 if (fseeko(fp, he - hs, SEEK_CUR) == -1)
505 "write error on %s file: %m", filename);
507 "WARNING: vmcore may be incomplete");
513 printf("%llu\r", (unsigned long long)dmpcnt);
518 printf("%s %.1lf%%\n", filename, (100.0 - (100.0 *
519 (double)dumpsize / (double)origsize)));
527 * Specialized version of dump-reading logic for use with textdumps, which
528 * are written backwards from the end of the partition, and must be reversed
529 * before being written to the file. Textdumps are small, so do a bit less
530 * work to optimize/sparsify.
533 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
534 const char *device, const char *filename, FILE *fp)
537 off_t dmpcnt, totsize;
542 if ((dumpsize % wl) != 0) {
543 logmsg(LOG_ERR, "textdump uneven multiple of 512 on %s",
548 while (dumpsize > 0) {
549 nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
553 "WARNING: EOF on dump device");
555 logmsg(LOG_ERR, "read error on %s: %m", device);
559 nw = fwrite(buf, 1, wl, fp);
562 "write error on %s file: %m", filename);
564 "WARNING: textdump may be incomplete");
570 printf("%llu\r", (unsigned long long)dmpcnt);
579 DoFile(const char *savedir, int savedirfd, const char *device)
581 xo_handle_t *xostdout, *xoinfo;
582 static char infoname[PATH_MAX], corename[PATH_MAX], linkname[PATH_MAX];
583 static char keyname[PATH_MAX];
584 static char *buf = NULL;
586 struct kerneldumpheader kdhf, kdhl;
588 off_t mediasize, dumpextent, dumplength, firsthd, lasthd;
590 int fdcore, fddev, error;
592 u_int sectorsize, xostyle;
593 uint32_t dumpkeysize;
594 bool iscompressed, isencrypted, istextdump, ret;
596 bounds = getbounds(savedirfd);
599 status = STATUS_UNKNOWN;
601 xostdout = xo_create_to_file(stdout, XO_STYLE_TEXT, 0);
602 if (xostdout == NULL) {
603 logmsg(LOG_ERR, "%s: %m", infoname);
607 if (maxdumps > 0 && bounds == maxdumps)
611 buf = malloc(BUFFERSIZE);
613 logmsg(LOG_ERR, "%m");
619 printf("checking for kernel dump on device %s\n", device);
621 fddev = fileargs_open(capfa, device);
623 logmsg(LOG_ERR, "%s: %m", device);
627 error = ioctl(fddev, DIOCGMEDIASIZE, &mediasize);
629 error = ioctl(fddev, DIOCGSECTORSIZE, §orsize);
632 "couldn't find media and/or sector size of %s: %m", device);
637 printf("mediasize = %lld bytes\n", (long long)mediasize);
638 printf("sectorsize = %u bytes\n", sectorsize);
641 if (sectorsize < sizeof(kdhl)) {
643 "Sector size is less the kernel dump header %zu",
648 lasthd = mediasize - sectorsize;
649 temp = malloc(sectorsize);
651 logmsg(LOG_ERR, "%m");
654 if (lseek(fddev, lasthd, SEEK_SET) != lasthd ||
655 read(fddev, temp, sectorsize) != (ssize_t)sectorsize) {
657 "error reading last dump header at offset %lld in %s: %m",
658 (long long)lasthd, device);
661 memcpy(&kdhl, temp, sizeof(kdhl));
662 iscompressed = istextdump = false;
663 if (compare_magic(&kdhl, TEXTDUMPMAGIC)) {
665 printf("textdump magic on last dump header on %s\n",
668 if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) {
670 "unknown version (%d) in last dump header on %s",
671 dtoh32(kdhl.version), device);
677 } else if (compare_magic(&kdhl, KERNELDUMPMAGIC)) {
678 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
680 "unknown version (%d) in last dump header on %s",
681 dtoh32(kdhl.version), device);
687 switch (kdhl.compression) {
688 case KERNELDUMP_COMP_NONE:
690 case KERNELDUMP_COMP_GZIP:
691 case KERNELDUMP_COMP_ZSTD:
692 if (compress && verbose)
693 printf("dump is already compressed\n");
698 logmsg(LOG_ERR, "unknown compression type %d on %s",
699 kdhl.compression, device);
704 printf("magic mismatch on last dump header on %s\n",
711 if (compare_magic(&kdhl, KERNELDUMPMAGIC_CLEARED)) {
713 printf("forcing magic on %s\n", device);
714 memcpy(kdhl.magic, KERNELDUMPMAGIC, sizeof(kdhl.magic));
716 logmsg(LOG_ERR, "unable to force dump - bad magic");
719 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
721 "unknown version (%d) in last dump header on %s",
722 dtoh32(kdhl.version), device);
734 if (kerneldump_parity(&kdhl)) {
736 "parity error on last dump header on %s", device);
742 dumpextent = dtoh64(kdhl.dumpextent);
743 dumplength = dtoh64(kdhl.dumplength);
744 dumpkeysize = dtoh32(kdhl.dumpkeysize);
745 firsthd = lasthd - dumpextent - sectorsize - dumpkeysize;
746 if (lseek(fddev, firsthd, SEEK_SET) != firsthd ||
747 read(fddev, temp, sectorsize) != (ssize_t)sectorsize) {
749 "error reading first dump header at offset %lld in %s: %m",
750 (long long)firsthd, device);
754 memcpy(&kdhf, temp, sizeof(kdhf));
757 printf("First dump headers:\n");
758 printheader(xostdout, &kdhf, device, bounds, -1);
760 printf("\nLast dump headers:\n");
761 printheader(xostdout, &kdhl, device, bounds, -1);
765 if (memcmp(&kdhl, &kdhf, sizeof(kdhl))) {
767 "first and last dump headers disagree on %s", device);
773 status = STATUS_GOOD;
777 printf("A dump exists on %s\n", device);
782 if (kdhl.panicstring[0] != '\0')
783 logmsg(LOG_ALERT, "reboot after panic: %.*s",
784 (int)sizeof(kdhl.panicstring), kdhl.panicstring);
786 logmsg(LOG_ALERT, "reboot");
789 printf("Checking for available free space\n");
791 if (!check_space(savedir, savedirfd, dumplength, bounds)) {
796 writebounds(savedirfd, bounds + 1);
798 saved_dump_remove(savedirfd, bounds);
800 snprintf(infoname, sizeof(infoname), "info.%d", bounds);
803 * Create or overwrite any existing dump header files.
805 if ((info = xfopenat(savedirfd, infoname,
806 O_WRONLY | O_CREAT | O_TRUNC, "w", 0600)) == NULL) {
807 logmsg(LOG_ERR, "open(%s): %m", infoname);
812 isencrypted = (dumpkeysize > 0);
814 snprintf(corename, sizeof(corename), "%s.%d.gz",
815 istextdump ? "textdump.tar" :
816 (isencrypted ? "vmcore_encrypted" : "vmcore"), bounds);
817 else if (iscompressed && !isencrypted)
818 snprintf(corename, sizeof(corename), "vmcore.%d.%s", bounds,
819 (kdhl.compression == KERNELDUMP_COMP_GZIP) ? "gz" : "zst");
821 snprintf(corename, sizeof(corename), "%s.%d",
822 istextdump ? "textdump.tar" :
823 (isencrypted ? "vmcore_encrypted" : "vmcore"), bounds);
824 fdcore = openat(savedirfd, corename, O_WRONLY | O_CREAT | O_TRUNC,
827 logmsg(LOG_ERR, "open(%s): %m", corename);
834 core = zdopen(fdcore, "w");
836 core = fdopen(fdcore, "w");
838 logmsg(LOG_ERR, "%s: %m", corename);
846 xostyle = xo_get_style(NULL);
847 xoinfo = xo_create_to_file(info, xostyle, 0);
848 if (xoinfo == NULL) {
849 logmsg(LOG_ERR, "%s: %m", infoname);
854 xo_open_container_h(xoinfo, "crashdump");
857 printheader(xostdout, &kdhl, device, bounds, status);
859 printheader(xoinfo, &kdhl, device, bounds, status);
860 xo_close_container_h(xoinfo, "crashdump");
866 dumpkey = calloc(1, dumpkeysize);
867 if (dumpkey == NULL) {
868 logmsg(LOG_ERR, "Unable to allocate kernel dump key.");
873 if (read(fddev, dumpkey, dumpkeysize) != (ssize_t)dumpkeysize) {
874 logmsg(LOG_ERR, "Unable to read kernel dump key: %m.");
879 snprintf(keyname, sizeof(keyname), "key.%d", bounds);
880 ret = writekey(savedirfd, keyname, dumpkey, dumpkeysize);
881 explicit_bzero(dumpkey, dumpkeysize);
888 logmsg(LOG_NOTICE, "writing %s%score to %s/%s",
889 isencrypted ? "encrypted " : "", compress ? "compressed " : "",
893 if (DoTextdumpFile(fddev, dumplength, lasthd, buf, device,
897 if (DoRegularFile(fddev, dumplength, sectorsize,
898 !(compress || iscompressed || isencrypted), buf, device,
899 corename, core) < 0) {
906 if (fclose(core) < 0) {
907 logmsg(LOG_ERR, "error on %s: %m", corename);
912 symlinks_remove(savedirfd);
913 if (symlinkat(infoname, savedirfd, "info.last") == -1) {
914 logmsg(LOG_WARNING, "unable to create symlink %s/%s: %m",
915 savedir, "info.last");
918 if (symlinkat(keyname, savedirfd, "key.last") == -1) {
920 "unable to create symlink %s/%s: %m", savedir,
924 if (compress || iscompressed) {
925 snprintf(linkname, sizeof(linkname), "%s.last.%s",
926 istextdump ? "textdump.tar" :
927 (isencrypted ? "vmcore_encrypted" : "vmcore"),
928 (kdhl.compression == KERNELDUMP_COMP_ZSTD) ? "zst" : "gz");
930 snprintf(linkname, sizeof(linkname), "%s.last",
931 istextdump ? "textdump.tar" :
932 (isencrypted ? "vmcore_encrypted" : "vmcore"));
934 if (symlinkat(corename, savedirfd, linkname) == -1) {
935 logmsg(LOG_WARNING, "unable to create symlink %s/%s: %m",
942 printf("dump saved\n");
947 printf("clearing dump header\n");
948 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof(kdhl.magic));
949 memcpy(temp, &kdhl, sizeof(kdhl));
950 if (lseek(fddev, lasthd, SEEK_SET) != lasthd ||
951 write(fddev, temp, sectorsize) != (ssize_t)sectorsize)
953 "error while clearing the dump header: %m");
955 xo_close_container_h(xostdout, "crashdump");
956 xo_finish_h(xostdout);
972 enum_dumpdevs(int *argcp)
979 * We cannot use getfsent(3) in capability mode, so we must
980 * scan /etc/fstab and build up a list of candidate devices
985 argv = malloc(n * sizeof(*argv));
987 logmsg(LOG_ERR, "malloc(): %m");
994 if (strcmp(fsp->fs_vfstype, "swap") != 0 &&
995 strcmp(fsp->fs_vfstype, "dump") != 0)
999 argv = realloc(argv, n * sizeof(*argv));
1001 logmsg(LOG_ERR, "realloc(): %m");
1005 argv[argc] = strdup(fsp->fs_spec);
1006 if (argv[argc] == NULL) {
1007 logmsg(LOG_ERR, "strdup(): %m");
1017 init_caps(int argc, char **argv)
1019 cap_rights_t rights;
1020 cap_channel_t *capcas;
1022 capcas = cap_init();
1023 if (capcas == NULL) {
1024 logmsg(LOG_ERR, "cap_init(): %m");
1028 * The fileargs capability does not currently provide a way to limit
1031 (void)cap_rights_init(&rights, CAP_PREAD, CAP_WRITE, CAP_IOCTL);
1032 capfa = fileargs_init(argc, argv, checkfor || keep ? O_RDONLY : O_RDWR,
1033 0, &rights, FA_OPEN);
1034 if (capfa == NULL) {
1035 logmsg(LOG_ERR, "fileargs_init(): %m");
1038 caph_cache_catpages();
1039 caph_cache_tzdata();
1040 if (caph_enter_casper() != 0) {
1041 logmsg(LOG_ERR, "caph_enter_casper(): %m");
1044 capsyslog = cap_service_open(capcas, "system.syslog");
1045 if (capsyslog == NULL) {
1046 logmsg(LOG_ERR, "cap_service_open(system.syslog): %m");
1055 xo_error("%s\n%s\n%s\n",
1056 "usage: savecore -c [-v] [device ...]",
1057 " savecore -C [-v] [device ...]",
1058 " savecore [-fkvz] [-m maxdumps] [directory [device ...]]");
1063 main(int argc, char **argv)
1065 cap_rights_t rights;
1066 const char *savedir;
1067 int i, ch, error, savedirfd;
1069 checkfor = compress = clear = force = keep = verbose = 0;
1070 nfound = nsaved = nerr = 0;
1073 openlog("savecore", LOG_PERROR, LOG_DAEMON);
1074 signal(SIGINFO, infohandler);
1076 argc = xo_parse_args(argc, argv);
1080 while ((ch = getopt(argc, argv, "Ccfkm:vz")) != -1)
1095 maxdumps = atoi(optarg);
1096 if (maxdumps <= 0) {
1097 logmsg(LOG_ERR, "Invalid maxdump value");
1111 if (checkfor && (clear || force || keep))
1113 if (clear && (compress || keep))
1115 if (maxdumps > 0 && (checkfor || clear))
1119 if (argc >= 1 && !checkfor && !clear) {
1120 error = chdir(argv[0]);
1122 logmsg(LOG_ERR, "chdir(%s): %m", argv[0]);
1130 argv = enum_dumpdevs(&argc);
1132 savedirfd = open(savedir, O_RDONLY | O_DIRECTORY);
1133 if (savedirfd < 0) {
1134 logmsg(LOG_ERR, "open(%s): %m", savedir);
1137 (void)cap_rights_init(&rights, CAP_CREATE, CAP_FCNTL, CAP_FSTATAT,
1138 CAP_FSTATFS, CAP_PREAD, CAP_SYMLINKAT, CAP_FTRUNCATE, CAP_UNLINKAT,
1140 if (caph_rights_limit(savedirfd, &rights) < 0) {
1141 logmsg(LOG_ERR, "cap_rights_limit(): %m");
1145 /* Enter capability mode. */
1146 init_caps(argc, argv);
1148 for (i = 0; i < argc; i++)
1149 DoFile(savedir, savedirfd, argv[i]);
1151 /* Emit minimal output. */
1155 printf("No dump exists\n");
1159 logmsg(LOG_WARNING, "no dumps found");
1160 } else if (nsaved == 0) {
1164 "unsaved dumps found but not saved");
1167 logmsg(LOG_WARNING, "no unsaved dumps found");
1174 infohandler(int sig __unused)