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