xref: /freebsd/sbin/savecore/savecore.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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
21  *    permission.
22  *
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
33  * SUCH DAMAGE.
34  *
35  * Copyright (c) 1986, 1992, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
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  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  */
62 
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include <sys/param.h>
67 #include <sys/disk.h>
68 #include <sys/kerneldump.h>
69 #include <sys/param.h>
70 #include <sys/mount.h>
71 #include <sys/stat.h>
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <fstab.h>
75 #include <paths.h>
76 #include <stdarg.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <syslog.h>
81 #include <time.h>
82 #include <unistd.h>
83 
84 /* The size of the buffer used for I/O. */
85 #define	BUFFERSIZE	(1024*1024)
86 
87 #define	STATUS_BAD	0
88 #define	STATUS_GOOD	1
89 #define	STATUS_UNKNOWN	2
90 
91 static int checkfor, compress, clear, force, keep, verbose;	/* flags */
92 static int nfound, nsaved, nerr;			/* statistics */
93 
94 extern FILE *zopen(const char *, const char *);
95 
96 static sig_atomic_t got_siginfo;
97 static void infohandler(int);
98 
99 static void
100 printheader(FILE *f, const struct kerneldumpheader *h, const char *device,
101     int bounds, const int status)
102 {
103 	uint64_t dumplen;
104 	time_t t;
105 	const char *stat_str;
106 
107 	fprintf(f, "Dump header from device %s\n", device);
108 	fprintf(f, "  Architecture: %s\n", h->architecture);
109 	fprintf(f, "  Architecture Version: %u\n",
110 	    dtoh32(h->architectureversion));
111 	dumplen = dtoh64(h->dumplength);
112 	fprintf(f, "  Dump Length: %lldB (%lld MB)\n", (long long)dumplen,
113 	    (long long)(dumplen >> 20));
114 	fprintf(f, "  Blocksize: %d\n", dtoh32(h->blocksize));
115 	t = dtoh64(h->dumptime);
116 	fprintf(f, "  Dumptime: %s", ctime(&t));
117 	fprintf(f, "  Hostname: %s\n", h->hostname);
118 	fprintf(f, "  Magic: %s\n", h->magic);
119 	fprintf(f, "  Version String: %s", h->versionstring);
120 	fprintf(f, "  Panic String: %s\n", h->panicstring);
121 	fprintf(f, "  Dump Parity: %u\n", h->parity);
122 	fprintf(f, "  Bounds: %d\n", bounds);
123 
124 	switch(status) {
125 	case STATUS_BAD:
126 		stat_str = "bad";
127 		break;
128 	case STATUS_GOOD:
129 		stat_str = "good";
130 		break;
131 	default:
132 		stat_str = "unknown";
133 	}
134 	fprintf(f, "  Dump Status: %s\n", stat_str);
135 	fflush(f);
136 }
137 
138 static int
139 getbounds(void) {
140 	FILE *fp;
141 	char buf[6];
142 	int ret;
143 
144 	ret = 0;
145 
146 	if ((fp = fopen("bounds", "r")) == NULL) {
147 		if (verbose)
148 			printf("unable to open bounds file, using 0\n");
149 		return (ret);
150 	}
151 
152 	if (fgets(buf, sizeof buf, fp) == NULL) {
153 		syslog(LOG_WARNING, "unable to read from bounds, using 0");
154 		fclose(fp);
155 		return (ret);
156 	}
157 
158 	errno = 0;
159 	ret = (int)strtol(buf, NULL, 10);
160 	if (ret == 0 && (errno == EINVAL || errno == ERANGE))
161 		syslog(LOG_WARNING, "invalid value found in bounds, using 0");
162 	return (ret);
163 }
164 
165 static void
166 writebounds(int bounds) {
167 	FILE *fp;
168 
169 	if ((fp = fopen("bounds", "w")) == NULL) {
170 		syslog(LOG_WARNING, "unable to write to bounds file: %m");
171 		return;
172 	}
173 
174 	if (verbose)
175 		printf("bounds number: %d\n", bounds);
176 
177 	fprintf(fp, "%d\n", bounds);
178 	fclose(fp);
179 }
180 
181 /*
182  * Check that sufficient space is available on the disk that holds the
183  * save directory.
184  */
185 static int
186 check_space(const char *savedir, off_t dumpsize)
187 {
188 	FILE *fp;
189 	off_t minfree, spacefree, totfree, needed;
190 	struct statfs fsbuf;
191 	char buf[100], path[MAXPATHLEN];
192 
193 	if (statfs(savedir, &fsbuf) < 0) {
194 		syslog(LOG_ERR, "%s: %m", savedir);
195 		exit(1);
196 	}
197  	spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024;
198 	totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024;
199 
200 	(void)snprintf(path, sizeof(path), "%s/minfree", savedir);
201 	if ((fp = fopen(path, "r")) == NULL)
202 		minfree = 0;
203 	else {
204 		if (fgets(buf, sizeof(buf), fp) == NULL)
205 			minfree = 0;
206 		else
207 			minfree = atoi(buf);
208 		(void)fclose(fp);
209 	}
210 
211 	needed = dumpsize / 1024 + 2;	/* 2 for info file */
212  	if (((minfree > 0) ? spacefree : totfree) - needed < minfree) {
213 		syslog(LOG_WARNING,
214 	"no dump, not enough free space on device (%lld available, need %lld)",
215 		    (long long)(minfree > 0 ? spacefree : totfree),
216 		    (long long)needed);
217 		return (0);
218 	}
219 	if (spacefree - needed < 0)
220 		syslog(LOG_WARNING,
221 		    "dump performed, but free space threshold crossed");
222 	return (1);
223 }
224 
225 #define BLOCKSIZE (1<<12)
226 #define BLOCKMASK (~(BLOCKSIZE-1))
227 
228 static int
229 DoRegularFile(int fd, off_t dumpsize, char *buf, const char *device,
230     const char *filename, FILE *fp)
231 {
232 	int he, hs, nr, nw, wl;
233 	off_t dmpcnt, origsize;
234 
235 	dmpcnt = 0;
236 	origsize = dumpsize;
237 	he = 0;
238 	while (dumpsize > 0) {
239 		wl = BUFFERSIZE;
240 		if (wl > dumpsize)
241 			wl = dumpsize;
242 		nr = read(fd, buf, wl);
243 		if (nr != wl) {
244 			if (nr == 0)
245 				syslog(LOG_WARNING,
246 				    "WARNING: EOF on dump device");
247 			else
248 				syslog(LOG_ERR, "read error on %s: %m", device);
249 			nerr++;
250 			return (-1);
251 		}
252 		if (compress) {
253 			nw = fwrite(buf, 1, wl, fp);
254 		} else {
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;
259 					    ++he)
260 						/* nothing */ ;
261 					/* is the hole long enough to matter? */
262 					if (he >= hs + BLOCKSIZE)
263 						break;
264 				}
265 
266 				/* back down to a block boundary */
267 				he &= BLOCKMASK;
268 
269 				/*
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.
274 				 */
275 				if (hs + BLOCKSIZE > nr)
276 					hs = he = nr;
277 
278 				/*
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.
283 				 */
284 				if (hs > nw)
285 					if (fwrite(buf + nw, hs - nw, 1, fp)
286 					    != 1)
287 					break;
288 				if (he > hs)
289 					if (fseeko(fp, he - hs, SEEK_CUR) == -1)
290 						break;
291 			}
292 		}
293 		if (nw != wl) {
294 			syslog(LOG_ERR,
295 			    "write error on %s file: %m", filename);
296 			syslog(LOG_WARNING,
297 			    "WARNING: vmcore may be incomplete");
298 			nerr++;
299 			return (-1);
300 		}
301 		if (verbose) {
302 			dmpcnt += wl;
303 			printf("%llu\r", (unsigned long long)dmpcnt);
304 			fflush(stdout);
305 		}
306 		dumpsize -= wl;
307 		if (got_siginfo) {
308 			printf("%s %.1lf%%\n", filename, (100.0 - (100.0 *
309 			    (double)dumpsize / (double)origsize)));
310 			got_siginfo = 0;
311 		}
312 	}
313 	return (0);
314 }
315 
316 /*
317  * Specialized version of dump-reading logic for use with textdumps, which
318  * are written backwards from the end of the partition, and must be reversed
319  * before being written to the file.  Textdumps are small, so do a bit less
320  * work to optimize/sparsify.
321  */
322 static int
323 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf,
324     const char *device, const char *filename, FILE *fp)
325 {
326 	int nr, nw, wl;
327 	off_t dmpcnt, totsize;
328 
329 	totsize = dumpsize;
330 	dmpcnt = 0;
331 	wl = 512;
332 	if ((dumpsize % wl) != 0) {
333 		syslog(LOG_ERR, "textdump uneven multiple of 512 on %s",
334 		    device);
335 		nerr++;
336 		return (-1);
337 	}
338 	while (dumpsize > 0) {
339 		nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl);
340 		if (nr != wl) {
341 			if (nr == 0)
342 				syslog(LOG_WARNING,
343 				    "WARNING: EOF on dump device");
344 			else
345 				syslog(LOG_ERR, "read error on %s: %m", device);
346 			nerr++;
347 			return (-1);
348 		}
349 		nw = fwrite(buf, 1, wl, fp);
350 		if (nw != wl) {
351 			syslog(LOG_ERR,
352 			    "write error on %s file: %m", filename);
353 			syslog(LOG_WARNING,
354 			    "WARNING: textdump may be incomplete");
355 			nerr++;
356 			return (-1);
357 		}
358 		if (verbose) {
359 			dmpcnt += wl;
360 			printf("%llu\r", (unsigned long long)dmpcnt);
361 			fflush(stdout);
362 		}
363 		dumpsize -= wl;
364 	}
365 	return (0);
366 }
367 
368 static void
369 DoFile(const char *savedir, const char *device)
370 {
371 	static char filename[PATH_MAX];
372 	static char *buf = NULL;
373 	struct kerneldumpheader kdhf, kdhl;
374 	off_t mediasize, dumpsize, firsthd, lasthd;
375 	FILE *info, *fp;
376 	mode_t oumask;
377 	int fd, fdinfo, error;
378 	int bounds, status;
379 	u_int sectorsize;
380 	int istextdump;
381 
382 	bounds = getbounds();
383 	mediasize = 0;
384 	status = STATUS_UNKNOWN;
385 
386 	if (buf == NULL) {
387 		buf = malloc(BUFFERSIZE);
388 		if (buf == NULL) {
389 			syslog(LOG_ERR, "%m");
390 			return;
391 		}
392 	}
393 
394 	if (verbose)
395 		printf("checking for kernel dump on device %s\n", device);
396 
397 	fd = open(device, O_RDWR);
398 	if (fd < 0) {
399 		syslog(LOG_ERR, "%s: %m", device);
400 		return;
401 	}
402 
403 	error = ioctl(fd, DIOCGMEDIASIZE, &mediasize);
404 	if (!error)
405 		error = ioctl(fd, DIOCGSECTORSIZE, &sectorsize);
406 	if (error) {
407 		syslog(LOG_ERR,
408 		    "couldn't find media and/or sector size of %s: %m", device);
409 		goto closefd;
410 	}
411 
412 	if (verbose) {
413 		printf("mediasize = %lld\n", (long long)mediasize);
414 		printf("sectorsize = %u\n", sectorsize);
415 	}
416 
417 	lasthd = mediasize - sectorsize;
418 	lseek(fd, lasthd, SEEK_SET);
419 	error = read(fd, &kdhl, sizeof kdhl);
420 	if (error != sizeof kdhl) {
421 		syslog(LOG_ERR,
422 		    "error reading last dump header at offset %lld in %s: %m",
423 		    (long long)lasthd, device);
424 		goto closefd;
425 	}
426 	istextdump = 0;
427 	if (strncmp(kdhl.magic, TEXTDUMPMAGIC, sizeof kdhl) == 0) {
428 		if (verbose)
429 			printf("textdump magic on last dump header on %s\n",
430 			    device);
431 		istextdump = 1;
432 		if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) {
433 			syslog(LOG_ERR,
434 			    "unknown version (%d) in last dump header on %s",
435 			    dtoh32(kdhl.version), device);
436 
437 			status = STATUS_BAD;
438 			if (force == 0)
439 				goto closefd;
440 		}
441 	} else if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic) ==
442 	    0) {
443 		if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
444 			syslog(LOG_ERR,
445 			    "unknown version (%d) in last dump header on %s",
446 			    dtoh32(kdhl.version), device);
447 
448 			status = STATUS_BAD;
449 			if (force == 0)
450 				goto closefd;
451 		}
452 	} else {
453 		if (verbose)
454 			printf("magic mismatch on last dump header on %s\n",
455 			    device);
456 
457 		status = STATUS_BAD;
458 		if (force == 0)
459 			goto closefd;
460 
461 		if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED,
462 			    sizeof kdhl.magic) == 0) {
463 			if (verbose)
464 				printf("forcing magic on %s\n", device);
465 			memcpy(kdhl.magic, KERNELDUMPMAGIC,
466 			    sizeof kdhl.magic);
467 		} else {
468 			syslog(LOG_ERR, "unable to force dump - bad magic");
469 			goto closefd;
470 		}
471 		if (dtoh32(kdhl.version) != KERNELDUMPVERSION) {
472 			syslog(LOG_ERR,
473 			    "unknown version (%d) in last dump header on %s",
474 			    dtoh32(kdhl.version), device);
475 
476 			status = STATUS_BAD;
477 			if (force == 0)
478 				goto closefd;
479 		}
480 	}
481 
482 	nfound++;
483 	if (clear)
484 		goto nuke;
485 
486 	if (kerneldump_parity(&kdhl)) {
487 		syslog(LOG_ERR,
488 		    "parity error on last dump header on %s", device);
489 		nerr++;
490 		status = STATUS_BAD;
491 		if (force == 0)
492 			goto closefd;
493 	}
494 	dumpsize = dtoh64(kdhl.dumplength);
495 	firsthd = lasthd - dumpsize - sizeof kdhf;
496 	lseek(fd, firsthd, SEEK_SET);
497 	error = read(fd, &kdhf, sizeof kdhf);
498 	if (error != sizeof kdhf) {
499 		syslog(LOG_ERR,
500 		    "error reading first dump header at offset %lld in %s: %m",
501 		    (long long)firsthd, device);
502 		nerr++;
503 		goto closefd;
504 	}
505 
506 	if (verbose >= 2) {
507 		printf("First dump headers:\n");
508 		printheader(stdout, &kdhf, device, bounds, -1);
509 
510 		printf("\nLast dump headers:\n");
511 		printheader(stdout, &kdhl, device, bounds, -1);
512 		printf("\n");
513 	}
514 
515 	if (memcmp(&kdhl, &kdhf, sizeof kdhl)) {
516 		syslog(LOG_ERR,
517 		    "first and last dump headers disagree on %s", device);
518 		nerr++;
519 		status = STATUS_BAD;
520 		if (force == 0)
521 			goto closefd;
522 	} else {
523 		status = STATUS_GOOD;
524 	}
525 
526 	if (checkfor) {
527 		printf("A dump exists on %s\n", device);
528 		close(fd);
529 		exit(0);
530 	}
531 
532 	if (kdhl.panicstring[0])
533 		syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring);
534 	else
535 		syslog(LOG_ALERT, "reboot");
536 
537 	if (verbose)
538 		printf("Checking for available free space\n");
539 	if (!check_space(savedir, dumpsize)) {
540 		nerr++;
541 		goto closefd;
542 	}
543 
544 	writebounds(bounds + 1);
545 
546 	sprintf(buf, "info.%d", bounds);
547 
548 	/*
549 	 * Create or overwrite any existing dump header files.
550 	 */
551 	fdinfo = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0600);
552 	if (fdinfo < 0) {
553 		syslog(LOG_ERR, "%s: %m", buf);
554 		nerr++;
555 		goto closefd;
556 	}
557 	oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/
558 	if (compress) {
559 		sprintf(filename, "%s.%d.gz", istextdump ? "textdump.tar" :
560 		    "vmcore", bounds);
561 		fp = zopen(filename, "w");
562 	} else {
563 		sprintf(filename, "%s.%d", istextdump ? "textdump.tar" :
564 		    "vmcore", bounds);
565 		fp = fopen(filename, "w");
566 	}
567 	if (fp == NULL) {
568 		syslog(LOG_ERR, "%s: %m", filename);
569 		close(fdinfo);
570 		nerr++;
571 		goto closefd;
572 	}
573 	(void)umask(oumask);
574 
575 	info = fdopen(fdinfo, "w");
576 
577 	if (info == NULL) {
578 		syslog(LOG_ERR, "fdopen failed: %m");
579 		nerr++;
580 		goto closefd;
581 	}
582 
583 	if (verbose)
584 		printheader(stdout, &kdhl, device, bounds, status);
585 
586 	printheader(info, &kdhl, device, bounds, status);
587 	fclose(info);
588 
589 	syslog(LOG_NOTICE, "writing %score to %s",
590 	    compress ? "compressed " : "", filename);
591 
592 	if (istextdump) {
593 		if (DoTextdumpFile(fd, dumpsize, lasthd, buf, device,
594 		    filename, fp) < 0)
595 			goto closeall;
596 	} else {
597 		if (DoRegularFile(fd, dumpsize, buf, device, filename, fp)
598 		    < 0)
599 			goto closeall;
600 	}
601 	if (verbose)
602 		printf("\n");
603 
604 	if (fclose(fp) < 0) {
605 		syslog(LOG_ERR, "error on %s: %m", filename);
606 		nerr++;
607 		goto closeall;
608 	}
609 	nsaved++;
610 
611 	if (verbose)
612 		printf("dump saved\n");
613 
614 nuke:
615 	if (clear || !keep) {
616 		if (verbose)
617 			printf("clearing dump header\n");
618 		memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic);
619 		lseek(fd, lasthd, SEEK_SET);
620 		error = write(fd, &kdhl, sizeof kdhl);
621 		if (error != sizeof kdhl)
622 			syslog(LOG_ERR,
623 			    "error while clearing the dump header: %m");
624 	}
625 	close(fd);
626 	return;
627 
628 closeall:
629 	fclose(fp);
630 
631 closefd:
632 	close(fd);
633 }
634 
635 static void
636 usage(void)
637 {
638 	fprintf(stderr, "%s\n%s\n%s\n",
639 	    "usage: savecore -c",
640 	    "       savecore -C [-v] [directory device]",
641 	    "       savecore [-fkvz] [directory [device ...]]");
642 	exit (1);
643 }
644 
645 int
646 main(int argc, char **argv)
647 {
648 	const char *savedir = ".";
649 	struct fstab *fsp;
650 	int i, ch, error;
651 
652 	checkfor = compress = clear = force = keep = verbose = 0;
653 	nfound = nsaved = nerr = 0;
654 
655 	openlog("savecore", LOG_PERROR, LOG_DAEMON);
656 	signal(SIGINFO, infohandler);
657 
658 	while ((ch = getopt(argc, argv, "Ccfkvz")) != -1)
659 		switch(ch) {
660 		case 'C':
661 			checkfor = 1;
662 			break;
663 		case 'c':
664 			clear = 1;
665 			break;
666 		case 'k':
667 			keep = 1;
668 			break;
669 		case 'v':
670 			verbose++;
671 			break;
672 		case 'f':
673 			force = 1;
674 			break;
675 		case 'z':
676 			compress = 1;
677 			break;
678 		case '?':
679 		default:
680 			usage();
681 		}
682 	if (checkfor && (clear || force || keep))
683 		usage();
684 	argc -= optind;
685 	argv += optind;
686 	if (argc >= 1) {
687 		error = chdir(argv[0]);
688 		if (error) {
689 			syslog(LOG_ERR, "chdir(%s): %m", argv[0]);
690 			exit(1);
691 		}
692 		savedir = argv[0];
693 		argc--;
694 		argv++;
695 	}
696 	if (argc == 0) {
697 		for (;;) {
698 			fsp = getfsent();
699 			if (fsp == NULL)
700 				break;
701 			if (strcmp(fsp->fs_vfstype, "swap") &&
702 			    strcmp(fsp->fs_vfstype, "dump"))
703 				continue;
704 			DoFile(savedir, fsp->fs_spec);
705 		}
706 	} else {
707 		for (i = 0; i < argc; i++)
708 			DoFile(savedir, argv[i]);
709 	}
710 
711 	/* Emit minimal output. */
712 	if (nfound == 0) {
713 		if (checkfor) {
714 			printf("No dump exists\n");
715 			exit(1);
716 		}
717 		syslog(LOG_WARNING, "no dumps found");
718 	}
719 	else if (nsaved == 0) {
720 		if (nerr != 0)
721 			syslog(LOG_WARNING, "unsaved dumps found but not saved");
722 		else
723 			syslog(LOG_WARNING, "no unsaved dumps found");
724 	}
725 
726 	return (0);
727 }
728 
729 static void
730 infohandler(int sig __unused)
731 {
732 	got_siginfo = 1;
733 }
734