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