xref: /freebsd/sbin/savecore/savecore.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
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.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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
23  *    permission.
24  *
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
35  * SUCH DAMAGE.
36  *
37  * Copyright (c) 1986, 1992, 1993
38  *	The Regents of the University of California.  All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
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.
51  *
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
62  * SUCH DAMAGE.
63  */
64 
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67 
68 #include <sys/param.h>
69 #include <sys/disk.h>
70 #include <sys/kerneldump.h>
71 #include <sys/mount.h>
72 #include <sys/stat.h>
73 #include <ctype.h>
74 #include <errno.h>
75 #include <fcntl.h>
76 #include <fstab.h>
77 #include <paths.h>
78 #include <signal.h>
79 #include <stdarg.h>
80 #include <stdbool.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <syslog.h>
85 #include <time.h>
86 #include <unistd.h>
87 #include <libxo/xo.h>
88 
89 /* The size of the buffer used for I/O. */
90 #define	BUFFERSIZE	(1024*1024)
91 
92 #define	STATUS_BAD	0
93 #define	STATUS_GOOD	1
94 #define	STATUS_UNKNOWN	2
95 
96 static int checkfor, compress, clear, force, keep, verbose;	/* flags */
97 static int nfound, nsaved, nerr;			/* statistics */
98 static int maxdumps;
99 
100 extern FILE *zopen(const char *, const char *);
101 
102 static sig_atomic_t got_siginfo;
103 static void infohandler(int);
104 
105 static void
106 printheader(xo_handle_t *xo, const struct kerneldumpheader *h,
107     const char *device, int bounds, const int status)
108 {
109 	uint64_t dumplen;
110 	time_t t;
111 	const char *stat_str;
112 
113 	xo_flush_h(xo);
114 	xo_emit_h(xo, "{Lwc:Dump header from device}{:dump_device/%s}\n",
115 	    device);
116 	xo_emit_h(xo, "{P:  }{Lwc:Architecture}{:architecture/%s}\n",
117 	    h->architecture);
118 	xo_emit_h(xo,
119 	    "{P:  }{Lwc:Architecture Version}{:architecture_version/%u}\n",
120 	    dtoh32(h->architectureversion));
121 	dumplen = dtoh64(h->dumplength);
122 	xo_emit_h(xo, "{P:  }{Lwc:Dump Length}{:dump_length_bytes/%lld}\n",
123 	    (long long)dumplen);
124 	xo_emit_h(xo, "{P:  }{Lwc:Blocksize}{:blocksize/%d}\n",
125 	    dtoh32(h->blocksize));
126 	xo_emit_h(xo, "{P:  }{Lwc:Compression}{:compression/%s}\n",
127 	    h->compression == KERNELDUMP_COMP_GZIP ?
128 	    "gzip" : "none");
129 
130 	t = dtoh64(h->dumptime);
131 	xo_emit_h(xo, "{P:  }{Lwc:Dumptime}{:dumptime/%s}", ctime(&t));
132 	xo_emit_h(xo, "{P:  }{Lwc:Hostname}{:hostname/%s}\n", h->hostname);
133 	xo_emit_h(xo, "{P:  }{Lwc:Magic}{:magic/%s}\n", h->magic);
134 	xo_emit_h(xo, "{P:  }{Lwc:Version String}{:version_string/%s}",
135 	    h->versionstring);
136 	xo_emit_h(xo, "{P:  }{Lwc:Panic String}{:panic_string/%s}\n",
137 	    h->panicstring);
138 	xo_emit_h(xo, "{P:  }{Lwc:Dump Parity}{:dump_parity/%u}\n", h->parity);
139 	xo_emit_h(xo, "{P:  }{Lwc:Bounds}{:bounds/%d}\n", bounds);
140 
141 	switch (status) {
142 	case STATUS_BAD:
143 		stat_str = "bad";
144 		break;
145 	case STATUS_GOOD:
146 		stat_str = "good";
147 		break;
148 	default:
149 		stat_str = "unknown";
150 		break;
151 	}
152 	xo_emit_h(xo, "{P:  }{Lwc:Dump Status}{:dump_status/%s}\n", stat_str);
153 	xo_flush_h(xo);
154 }
155 
156 static int
157 getbounds(void)
158 {
159 	FILE *fp;
160 	char buf[6];
161 	int ret;
162 
163 	ret = 0;
164 
165 	if ((fp = fopen("bounds", "r")) == NULL) {
166 		if (verbose)
167 			printf("unable to open bounds file, using 0\n");
168 		return (ret);
169 	}
170 
171 	if (fgets(buf, sizeof buf, fp) == NULL) {
172 		if (feof(fp))
173 			syslog(LOG_WARNING, "bounds file is empty, using 0");
174 		else
175 			syslog(LOG_WARNING, "bounds file: %s", strerror(errno));
176 		fclose(fp);
177 		return (ret);
178 	}
179 
180 	errno = 0;
181 	ret = (int)strtol(buf, NULL, 10);
182 	if (ret == 0 && (errno == EINVAL || errno == ERANGE))
183 		syslog(LOG_WARNING, "invalid value found in bounds, using 0");
184 	fclose(fp);
185 	return (ret);
186 }
187 
188 static void
189 writebounds(int bounds)
190 {
191 	FILE *fp;
192 
193 	if ((fp = fopen("bounds", "w")) == NULL) {
194 		syslog(LOG_WARNING, "unable to write to bounds file: %m");
195 		return;
196 	}
197 
198 	if (verbose)
199 		printf("bounds number: %d\n", bounds);
200 
201 	fprintf(fp, "%d\n", bounds);
202 	fclose(fp);
203 }
204 
205 static bool
206 writekey(const char *keyname, uint8_t *dumpkey, uint32_t dumpkeysize)
207 {
208 	int fd;
209 
210 	fd = open(keyname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
211 	if (fd == -1) {
212 		syslog(LOG_ERR, "Unable to open %s to write the key: %m.",
213 		    keyname);
214 		return (false);
215 	}
216 
217 	if (write(fd, dumpkey, dumpkeysize) != (ssize_t)dumpkeysize) {
218 		syslog(LOG_ERR, "Unable to write the key to %s: %m.", keyname);
219 		close(fd);
220 		return (false);
221 	}
222 
223 	close(fd);
224 	return (true);
225 }
226 
227 static off_t
228 file_size(const char *path)
229 {
230 	struct stat sb;
231 
232 	/* Ignore all errors, those file may not exists. */
233 	if (stat(path, &sb) == -1)
234 		return (0);
235 	return (sb.st_size);
236 }
237 
238 static off_t
239 saved_dump_size(int bounds)
240 {
241 	static char path[PATH_MAX];
242 	off_t dumpsize;
243 
244 	dumpsize = 0;
245 
246 	(void)snprintf(path, sizeof(path), "info.%d", bounds);
247 	dumpsize += file_size(path);
248 	(void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
249 	dumpsize += file_size(path);
250 	(void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
251 	dumpsize += file_size(path);
252 	(void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
253 	dumpsize += file_size(path);
254 	(void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
255 	dumpsize += file_size(path);
256 
257 	return (dumpsize);
258 }
259 
260 static void
261 saved_dump_remove(int bounds)
262 {
263 	static char path[PATH_MAX];
264 
265 	(void)snprintf(path, sizeof(path), "info.%d", bounds);
266 	(void)unlink(path);
267 	(void)snprintf(path, sizeof(path), "vmcore.%d", bounds);
268 	(void)unlink(path);
269 	(void)snprintf(path, sizeof(path), "vmcore.%d.gz", bounds);
270 	(void)unlink(path);
271 	(void)snprintf(path, sizeof(path), "textdump.tar.%d", bounds);
272 	(void)unlink(path);
273 	(void)snprintf(path, sizeof(path), "textdump.tar.%d.gz", bounds);
274 	(void)unlink(path);
275 }
276 
277 static void
278 symlinks_remove(void)
279 {
280 
281 	(void)unlink("info.last");
282 	(void)unlink("key.last");
283 	(void)unlink("vmcore.last");
284 	(void)unlink("vmcore.last.gz");
285 	(void)unlink("vmcore_encrypted.last");
286 	(void)unlink("vmcore_encrypted.last.gz");
287 	(void)unlink("textdump.tar.last");
288 	(void)unlink("textdump.tar.last.gz");
289 }
290 
291 /*
292  * Check that sufficient space is available on the disk that holds the
293  * save directory.
294  */
295 static int
296 check_space(const char *savedir, off_t dumpsize, int bounds)
297 {
298 	FILE *fp;
299 	off_t available, minfree, spacefree, totfree, needed;
300 	struct statfs fsbuf;
301 	char buf[100];
302 
303 	if (statfs(".", &fsbuf) < 0) {
304 		syslog(LOG_ERR, "%s: %m", savedir);
305 		exit(1);
306 	}
307 	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
308 	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
309 
310 	if ((fp = fopen("minfree", "r")) == NULL)
311 		minfree = 0;
312 	else {
313 		if (fgets(buf, sizeof(buf), fp) == NULL)
314 			minfree = 0;
315 		else {
316 			char *endp;
317 
318 			errno = 0;
319 			minfree = strtoll(buf, &endp, 10);
320 			if (minfree == 0 && errno != 0)
321 				minfree = -1;
322 			else {
323 				while (*endp != '\0' && isspace(*endp))
324 					endp++;
325 				if (*endp != '\0' || minfree < 0)
326 					minfree = -1;
327 			}
328 			if (minfree < 0)
329 				syslog(LOG_WARNING,
330 				    "`minfree` didn't contain a valid size "
331 				    "(`%s`). Defaulting to 0", buf);
332 		}
333 		(void)fclose(fp);
334 	}
335 
336 	available = minfree > 0 ? spacefree - minfree : totfree;
337 	needed = dumpsize / 1024 + 2;	/* 2 for info file */
338 	needed -= saved_dump_size(bounds);
339 	if (available < needed) {
340 		syslog(LOG_WARNING,
341 		    "no dump: not enough free space on device (need at least "
342 		    "%jdkB for dump; %jdkB available; %jdkB reserved)",
343 		    (intmax_t)needed,
344 		    (intmax_t)available + minfree,
345 		    (intmax_t)minfree);
346 		return (0);
347 	}
348 	if (spacefree - needed < 0)
349 		syslog(LOG_WARNING,
350 		    "dump performed, but free space threshold crossed");
351 	return (1);
352 }
353 
354 static bool
355 compare_magic(const struct kerneldumpheader *kdh, const char *magic)
356 {
357 
358 	return (strncmp(kdh->magic, magic, sizeof(kdh->magic)) == 0);
359 }
360 
361 #define BLOCKSIZE (1<<12)
362 #define BLOCKMASK (~(BLOCKSIZE-1))
363 
364 static int
365 DoRegularFile(int fd, off_t dumpsize, u_int sectorsize, bool sparse, char *buf,
366     const char *device, const char *filename, FILE *fp)
367 {
368 	int he, hs, nr, nw, wl;
369 	off_t dmpcnt, origsize;
370 
371 	dmpcnt = 0;
372 	origsize = dumpsize;
373 	he = 0;
374 	while (dumpsize > 0) {
375 		wl = BUFFERSIZE;
376 		if (wl > dumpsize)
377 			wl = dumpsize;
378 		nr = read(fd, buf, roundup(wl, sectorsize));
379 		if (nr != (int)roundup(wl, sectorsize)) {
380 			if (nr == 0)
381 				syslog(LOG_WARNING,
382 				    "WARNING: EOF on dump device");
383 			else
384 				syslog(LOG_ERR, "read error on %s: %m", device);
385 			nerr++;
386 			return (-1);
387 		}
388 		if (!sparse) {
389 			nw = fwrite(buf, 1, wl, fp);
390 		} else {
391 			for (nw = 0; nw < nr; nw = he) {
392 				/* find a contiguous block of zeroes */
393 				for (hs = nw; hs < nr; hs += BLOCKSIZE) {
394 					for (he = hs; he < nr && buf[he] == 0;
395 					    ++he)
396 						/* nothing */ ;
397 					/* is the hole long enough to matter? */
398 					if (he >= hs + BLOCKSIZE)
399 						break;
400 				}
401 
402 				/* back down to a block boundary */
403 				he &= BLOCKMASK;
404 
405 				/*
406 				 * 1) Don't go beyond the end of the buffer.
407 				 * 2) If the end of the buffer is less than
408 				 *    BLOCKSIZE bytes away, we're at the end
409 				 *    of the file, so just grab what's left.
410 				 */
411 				if (hs + BLOCKSIZE > nr)
412 					hs = he = nr;
413 
414 				/*
415 				 * At this point, we have a partial ordering:
416 				 *     nw <= hs <= he <= nr
417 				 * If hs > nw, buf[nw..hs] contains non-zero
418 				 * data. If he > hs, buf[hs..he] is all zeroes.
419 				 */
420 				if (hs > nw)
421 					if (fwrite(buf + nw, hs - nw, 1, fp)
422 					    != 1)
423 					break;
424 				if (he > hs)
425 					if (fseeko(fp, he - hs, SEEK_CUR) == -1)
426 						break;
427 			}
428 		}
429 		if (nw != wl) {
430 			syslog(LOG_ERR,
431 			    "write error on %s file: %m", filename);
432 			syslog(LOG_WARNING,
433 			    "WARNING: vmcore may be incomplete");
434 			nerr++;
435 			return (-1);
436 		}
437 		if (verbose) {
438 			dmpcnt += wl;
439 			printf("%llu\r", (unsigned long long)dmpcnt);
440 			fflush(stdout);
441 		}
442 		dumpsize -= wl;
443 		if (got_siginfo) {
444 			printf("%s %.1lf%%\n", filename, (100.0 - (100.0 *
445 			    (double)dumpsize / (double)origsize)));
446 			got_siginfo = 0;
447 		}
448 	}
449 	return (0);
450 }
451 
452 /*
453  * Specialized version of dump-reading logic for use with textdumps, which
454  * are written backwards from the end of the partition, and must be reversed
455  * before being written to the file.  Textdumps are small, so do a bit less
456  * work to optimize/sparsify.
457  */
458 static int
459 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
460     const char *device, const char *filename, FILE *fp)
461 {
462 	int nr, nw, wl;
463 	off_t dmpcnt, totsize;
464 
465 	totsize = dumpsize;
466 	dmpcnt = 0;
467 	wl = 512;
468 	if ((dumpsize % wl) != 0) {
469 		syslog(LOG_ERR, "textdump uneven multiple of 512 on %s",
470 		    device);
471 		nerr++;
472 		return (-1);
473 	}
474 	while (dumpsize > 0) {
475 		nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
476 		if (nr != wl) {
477 			if (nr == 0)
478 				syslog(LOG_WARNING,
479 				    "WARNING: EOF on dump device");
480 			else
481 				syslog(LOG_ERR, "read error on %s: %m", device);
482 			nerr++;
483 			return (-1);
484 		}
485 		nw = fwrite(buf, 1, wl, fp);
486 		if (nw != wl) {
487 			syslog(LOG_ERR,
488 			    "write error on %s file: %m", filename);
489 			syslog(LOG_WARNING,
490 			    "WARNING: textdump may be incomplete");
491 			nerr++;
492 			return (-1);
493 		}
494 		if (verbose) {
495 			dmpcnt += wl;
496 			printf("%llu\r", (unsigned long long)dmpcnt);
497 			fflush(stdout);
498 		}
499 		dumpsize -= wl;
500 	}
501 	return (0);
502 }
503 
504 static void
505 DoFile(const char *savedir, const char *device)
506 {
507 	xo_handle_t *xostdout, *xoinfo;
508 	static char infoname[PATH_MAX], corename[PATH_MAX], linkname[PATH_MAX];
509 	static char keyname[PATH_MAX];
510 	static char *buf = NULL;
511 	char *temp = NULL;
512 	struct kerneldumpheader kdhf, kdhl;
513 	uint8_t *dumpkey;
514 	off_t mediasize, dumpextent, dumplength, firsthd, lasthd;
515 	FILE *info, *fp;
516 	mode_t oumask;
517 	int fd, fdinfo, error;
518 	int bounds, status;
519 	u_int sectorsize, xostyle;
520 	uint32_t dumpkeysize;
521 	bool iscompressed, isencrypted, istextdump, ret;
522 
523 	bounds = getbounds();
524 	dumpkey = NULL;
525 	mediasize = 0;
526 	status = STATUS_UNKNOWN;
527 
528 	xostdout = xo_create_to_file(stdout, XO_STYLE_TEXT, 0);
529 	if (xostdout == NULL) {
530 		syslog(LOG_ERR, "%s: %m", infoname);
531 		return;
532 	}
533 
534 	if (maxdumps > 0 && bounds == maxdumps)
535 		bounds = 0;
536 
537 	if (buf == NULL) {
538 		buf = malloc(BUFFERSIZE);
539 		if (buf == NULL) {
540 			syslog(LOG_ERR, "%m");
541 			return;
542 		}
543 	}
544 
545 	if (verbose)
546 		printf("checking for kernel dump on device %s\n", device);
547 
548 	fd = open(device, (checkfor || keep) ? O_RDONLY : O_RDWR);
549 	if (fd < 0) {
550 		syslog(LOG_ERR, "%s: %m", device);
551 		return;
552 	}
553 
554 	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
555 	if (!error)
556 		error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
557 	if (error) {
558 		syslog(LOG_ERR,
559 		    "couldn't find media and/or sector size of %s: %m", device);
560 		goto closefd;
561 	}
562 
563 	if (verbose) {
564 		printf("mediasize = %lld bytes\n", (long long)mediasize);
565 		printf("sectorsize = %u bytes\n", sectorsize);
566 	}
567 
568 	if (sectorsize < sizeof(kdhl)) {
569 		syslog(LOG_ERR,
570 		    "Sector size is less the kernel dump header %zu",
571 		    sizeof(kdhl));
572 		goto closefd;
573 	}
574 
575 	lasthd = mediasize - sectorsize;
576 	temp = malloc(sectorsize);
577 	if (temp == NULL) {
578 		syslog(LOG_ERR, "%m");
579 		goto closefd;
580 	}
581 	if (lseek(fd, lasthd, SEEK_SET) != lasthd ||
582 	    read(fd, temp, sectorsize) != (ssize_t)sectorsize) {
583 		syslog(LOG_ERR,
584 		    "error reading last dump header at offset %lld in %s: %m",
585 		    (long long)lasthd, device);
586 		goto closefd;
587 	}
588 	memcpy(&kdhl, temp, sizeof(kdhl));
589 	iscompressed = istextdump = false;
590 	if (compare_magic(&kdhl, TEXTDUMPMAGIC)) {
591 		if (verbose)
592 			printf("textdump magic on last dump header on %s\n",
593 			    device);
594 		istextdump = true;
595 		if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) {
596 			syslog(LOG_ERR,
597 			    "unknown version (%d) in last dump header on %s",
598 			    dtoh32(kdhl.version), device);
599 
600 			status = STATUS_BAD;
601 			if (force == 0)
602 				goto closefd;
603 		}
604 	} else if (compare_magic(&kdhl, KERNELDUMPMAGIC)) {
605 		if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
606 			syslog(LOG_ERR,
607 			    "unknown version (%d) in last dump header on %s",
608 			    dtoh32(kdhl.version), device);
609 
610 			status = STATUS_BAD;
611 			if (force == 0)
612 				goto closefd;
613 		}
614 		switch (kdhl.compression) {
615 		case KERNELDUMP_COMP_NONE:
616 			break;
617 		case KERNELDUMP_COMP_GZIP:
618 			if (compress && verbose)
619 				printf("dump is already compressed\n");
620 			compress = false;
621 			iscompressed = true;
622 			break;
623 		default:
624 			syslog(LOG_ERR, "unknown compression type %d on %s",
625 			    kdhl.compression, device);
626 			break;
627 		}
628 	} else {
629 		if (verbose)
630 			printf("magic mismatch on last dump header on %s\n",
631 			    device);
632 
633 		status = STATUS_BAD;
634 		if (force == 0)
635 			goto closefd;
636 
637 		if (compare_magic(&kdhl, KERNELDUMPMAGIC_CLEARED)) {
638 			if (verbose)
639 				printf("forcing magic on %s\n", device);
640 			memcpy(kdhl.magic, KERNELDUMPMAGIC, sizeof(kdhl.magic));
641 		} else {
642 			syslog(LOG_ERR, "unable to force dump - bad magic");
643 			goto closefd;
644 		}
645 		if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
646 			syslog(LOG_ERR,
647 			    "unknown version (%d) in last dump header on %s",
648 			    dtoh32(kdhl.version), device);
649 
650 			status = STATUS_BAD;
651 			if (force == 0)
652 				goto closefd;
653 		}
654 	}
655 
656 	nfound++;
657 	if (clear)
658 		goto nuke;
659 
660 	if (kerneldump_parity(&kdhl)) {
661 		syslog(LOG_ERR,
662 		    "parity error on last dump header on %s", device);
663 		nerr++;
664 		status = STATUS_BAD;
665 		if (force == 0)
666 			goto closefd;
667 	}
668 	dumpextent = dtoh64(kdhl.dumpextent);
669 	dumplength = dtoh64(kdhl.dumplength);
670 	dumpkeysize = dtoh32(kdhl.dumpkeysize);
671 	firsthd = lasthd - dumpextent - sectorsize - dumpkeysize;
672 	if (lseek(fd, firsthd, SEEK_SET) != firsthd ||
673 	    read(fd, temp, sectorsize) != (ssize_t)sectorsize) {
674 		syslog(LOG_ERR,
675 		    "error reading first dump header at offset %lld in %s: %m",
676 		    (long long)firsthd, device);
677 		nerr++;
678 		goto closefd;
679 	}
680 	memcpy(&kdhf, temp, sizeof(kdhf));
681 
682 	if (verbose >= 2) {
683 		printf("First dump headers:\n");
684 		printheader(xostdout, &kdhf, device, bounds, -1);
685 
686 		printf("\nLast dump headers:\n");
687 		printheader(xostdout, &kdhl, device, bounds, -1);
688 		printf("\n");
689 	}
690 
691 	if (memcmp(&kdhl, &kdhf, sizeof(kdhl))) {
692 		syslog(LOG_ERR,
693 		    "first and last dump headers disagree on %s", device);
694 		nerr++;
695 		status = STATUS_BAD;
696 		if (force == 0)
697 			goto closefd;
698 	} else {
699 		status = STATUS_GOOD;
700 	}
701 
702 	if (checkfor) {
703 		printf("A dump exists on %s\n", device);
704 		close(fd);
705 		exit(0);
706 	}
707 
708 	if (kdhl.panicstring[0] != '\0')
709 		syslog(LOG_ALERT, "reboot after panic: %.*s",
710 		    (int)sizeof(kdhl.panicstring), kdhl.panicstring);
711 	else
712 		syslog(LOG_ALERT, "reboot");
713 
714 	if (verbose)
715 		printf("Checking for available free space\n");
716 
717 	if (!check_space(savedir, dumplength, bounds)) {
718 		nerr++;
719 		goto closefd;
720 	}
721 
722 	writebounds(bounds + 1);
723 
724 	saved_dump_remove(bounds);
725 
726 	snprintf(infoname, sizeof(infoname), "info.%d", bounds);
727 
728 	/*
729 	 * Create or overwrite any existing dump header files.
730 	 */
731 	fdinfo = open(infoname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
732 	if (fdinfo < 0) {
733 		syslog(LOG_ERR, "%s: %m", infoname);
734 		nerr++;
735 		goto closefd;
736 	}
737 
738 	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file. */
739 	isencrypted = (dumpkeysize > 0);
740 	if (compress) {
741 		snprintf(corename, sizeof(corename), "%s.%d.gz",
742 		    istextdump ? "textdump.tar" :
743 		    (isencrypted ? "vmcore_encrypted" : "vmcore"), bounds);
744 		fp = zopen(corename, "w");
745 	} else if (iscompressed && !isencrypted) {
746 		snprintf(corename, sizeof(corename), "vmcore.%d.gz", bounds);
747 		fp = fopen(corename, "w");
748 	} else {
749 		snprintf(corename, sizeof(corename), "%s.%d",
750 		    istextdump ? "textdump.tar" :
751 		    (isencrypted ? "vmcore_encrypted" : "vmcore"), bounds);
752 		fp = fopen(corename, "w");
753 	}
754 	if (fp == NULL) {
755 		syslog(LOG_ERR, "%s: %m", corename);
756 		close(fdinfo);
757 		nerr++;
758 		goto closefd;
759 	}
760 	(void)umask(oumask);
761 
762 	info = fdopen(fdinfo, "w");
763 
764 	if (info == NULL) {
765 		syslog(LOG_ERR, "fdopen failed: %m");
766 		nerr++;
767 		goto closeall;
768 	}
769 
770 	xostyle = xo_get_style(NULL);
771 	xoinfo = xo_create_to_file(info, xostyle, 0);
772 	if (xoinfo == NULL) {
773 		syslog(LOG_ERR, "%s: %m", infoname);
774 		nerr++;
775 		goto closeall;
776 	}
777 	xo_open_container_h(xoinfo, "crashdump");
778 
779 	if (verbose)
780 		printheader(xostdout, &kdhl, device, bounds, status);
781 
782 	printheader(xoinfo, &kdhl, device, bounds, status);
783 	xo_close_container_h(xoinfo, "crashdump");
784 	xo_flush_h(xoinfo);
785 	xo_finish_h(xoinfo);
786 	fclose(info);
787 
788 	if (isencrypted) {
789 		dumpkey = calloc(1, dumpkeysize);
790 		if (dumpkey == NULL) {
791 			syslog(LOG_ERR, "Unable to allocate kernel dump key.");
792 			nerr++;
793 			goto closeall;
794 		}
795 
796 		if (read(fd, dumpkey, dumpkeysize) != (ssize_t)dumpkeysize) {
797 			syslog(LOG_ERR, "Unable to read kernel dump key: %m.");
798 			nerr++;
799 			goto closeall;
800 		}
801 
802 		snprintf(keyname, sizeof(keyname), "key.%d", bounds);
803 		ret = writekey(keyname, dumpkey, dumpkeysize);
804 		explicit_bzero(dumpkey, dumpkeysize);
805 		if (!ret) {
806 			nerr++;
807 			goto closeall;
808 		}
809 	}
810 
811 	syslog(LOG_NOTICE, "writing %s%score to %s/%s",
812 	    isencrypted ? "encrypted " : "", compress ? "compressed " : "",
813 	    savedir, corename);
814 
815 	if (istextdump) {
816 		if (DoTextdumpFile(fd, dumplength, lasthd, buf, device,
817 		    corename, fp) < 0)
818 			goto closeall;
819 	} else {
820 		if (DoRegularFile(fd, dumplength, sectorsize,
821 		    !(compress || iscompressed || isencrypted), buf, device,
822 		    corename, fp) < 0) {
823 			goto closeall;
824 		}
825 	}
826 	if (verbose)
827 		printf("\n");
828 
829 	if (fclose(fp) < 0) {
830 		syslog(LOG_ERR, "error on %s: %m", corename);
831 		nerr++;
832 		goto closefd;
833 	}
834 
835 	symlinks_remove();
836 	if (symlink(infoname, "info.last") == -1) {
837 		syslog(LOG_WARNING, "unable to create symlink %s/%s: %m",
838 		    savedir, "info.last");
839 	}
840 	if (isencrypted) {
841 		if (symlink(keyname, "key.last") == -1) {
842 			syslog(LOG_WARNING,
843 			    "unable to create symlink %s/%s: %m", savedir,
844 			    "key.last");
845 		}
846 	}
847 	if (compress || iscompressed) {
848 		snprintf(linkname, sizeof(linkname), "%s.last.gz",
849 		    istextdump ? "textdump.tar" :
850 		    (isencrypted ? "vmcore_encrypted" : "vmcore"));
851 	} else {
852 		snprintf(linkname, sizeof(linkname), "%s.last",
853 		    istextdump ? "textdump.tar" :
854 		    (isencrypted ? "vmcore_encrypted" : "vmcore"));
855 	}
856 	if (symlink(corename, linkname) == -1) {
857 		syslog(LOG_WARNING, "unable to create symlink %s/%s: %m",
858 		    savedir, linkname);
859 	}
860 
861 	nsaved++;
862 
863 	if (verbose)
864 		printf("dump saved\n");
865 
866 nuke:
867 	if (!keep) {
868 		if (verbose)
869 			printf("clearing dump header\n");
870 		memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof(kdhl.magic));
871 		memcpy(temp, &kdhl, sizeof(kdhl));
872 		if (lseek(fd, lasthd, SEEK_SET) != lasthd ||
873 		    write(fd, temp, sectorsize) != (ssize_t)sectorsize)
874 			syslog(LOG_ERR,
875 			    "error while clearing the dump header: %m");
876 	}
877 	xo_close_container_h(xostdout, "crashdump");
878 	xo_finish_h(xostdout);
879 	free(dumpkey);
880 	free(temp);
881 	close(fd);
882 	return;
883 
884 closeall:
885 	fclose(fp);
886 
887 closefd:
888 	free(dumpkey);
889 	free(temp);
890 	close(fd);
891 }
892 
893 static void
894 usage(void)
895 {
896 	xo_error("%s\n%s\n%s\n",
897 	    "usage: savecore -c [-v] [device ...]",
898 	    "       savecore -C [-v] [device ...]",
899 	    "       savecore [-fkvz] [-m maxdumps] [directory [device ...]]");
900 	exit(1);
901 }
902 
903 int
904 main(int argc, char **argv)
905 {
906 	const char *savedir = ".";
907 	struct fstab *fsp;
908 	int i, ch, error;
909 
910 	checkfor = compress = clear = force = keep = verbose = 0;
911 	nfound = nsaved = nerr = 0;
912 
913 	openlog("savecore", LOG_PERROR, LOG_DAEMON);
914 	signal(SIGINFO, infohandler);
915 
916 	argc = xo_parse_args(argc, argv);
917 	if (argc < 0)
918 		exit(1);
919 
920 	while ((ch = getopt(argc, argv, "Ccfkm:vz")) != -1)
921 		switch(ch) {
922 		case 'C':
923 			checkfor = 1;
924 			break;
925 		case 'c':
926 			clear = 1;
927 			break;
928 		case 'f':
929 			force = 1;
930 			break;
931 		case 'k':
932 			keep = 1;
933 			break;
934 		case 'm':
935 			maxdumps = atoi(optarg);
936 			if (maxdumps <= 0) {
937 				syslog(LOG_ERR, "Invalid maxdump value");
938 				exit(1);
939 			}
940 			break;
941 		case 'v':
942 			verbose++;
943 			break;
944 		case 'z':
945 			compress = 1;
946 			break;
947 		case '?':
948 		default:
949 			usage();
950 		}
951 	if (checkfor && (clear || force || keep))
952 		usage();
953 	if (clear && (compress || keep))
954 		usage();
955 	if (maxdumps > 0 && (checkfor || clear))
956 		usage();
957 	argc -= optind;
958 	argv += optind;
959 	if (argc >= 1 && !checkfor && !clear) {
960 		error = chdir(argv[0]);
961 		if (error) {
962 			syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
963 			exit(1);
964 		}
965 		savedir = argv[0];
966 		argc--;
967 		argv++;
968 	}
969 	if (argc == 0) {
970 		for (;;) {
971 			fsp = getfsent();
972 			if (fsp == NULL)
973 				break;
974 			if (strcmp(fsp->fs_vfstype, "swap") &&
975 			    strcmp(fsp->fs_vfstype, "dump"))
976 				continue;
977 			DoFile(savedir, fsp->fs_spec);
978 		}
979 		endfsent();
980 	} else {
981 		for (i = 0; i < argc; i++)
982 			DoFile(savedir, argv[i]);
983 	}
984 
985 	/* Emit minimal output. */
986 	if (nfound == 0) {
987 		if (checkfor) {
988 			if (verbose)
989 				printf("No dump exists\n");
990 			exit(1);
991 		}
992 		if (verbose)
993 			syslog(LOG_WARNING, "no dumps found");
994 	} else if (nsaved == 0) {
995 		if (nerr != 0) {
996 			if (verbose)
997 				syslog(LOG_WARNING,
998 				    "unsaved dumps found but not saved");
999 			exit(1);
1000 		} else if (verbose)
1001 			syslog(LOG_WARNING, "no unsaved dumps found");
1002 	}
1003 
1004 	return (0);
1005 }
1006 
1007 static void
1008 infohandler(int sig __unused)
1009 {
1010 	got_siginfo = 1;
1011 }
1012