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