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 (%lld available, need %lld)", 281 (long long)(minfree > 0 ? spacefree : totfree), 282 (long long)needed); 283 return (0); 284 } 285 if (spacefree - needed < 0) 286 syslog(LOG_WARNING, 287 "dump performed, but free space threshold crossed"); 288 return (1); 289 } 290 291 #define BLOCKSIZE (1<<12) 292 #define BLOCKMASK (~(BLOCKSIZE-1)) 293 294 static int 295 DoRegularFile(int fd, off_t dumpsize, char *buf, const char *device, 296 const char *filename, FILE *fp) 297 { 298 int he, hs, nr, nw, wl; 299 off_t dmpcnt, origsize; 300 301 dmpcnt = 0; 302 origsize = dumpsize; 303 he = 0; 304 while (dumpsize > 0) { 305 wl = BUFFERSIZE; 306 if (wl > dumpsize) 307 wl = dumpsize; 308 nr = read(fd, buf, wl); 309 if (nr != wl) { 310 if (nr == 0) 311 syslog(LOG_WARNING, 312 "WARNING: EOF on dump device"); 313 else 314 syslog(LOG_ERR, "read error on %s: %m", device); 315 nerr++; 316 return (-1); 317 } 318 if (compress) { 319 nw = fwrite(buf, 1, wl, fp); 320 } else { 321 for (nw = 0; nw < nr; nw = he) { 322 /* find a contiguous block of zeroes */ 323 for (hs = nw; hs < nr; hs += BLOCKSIZE) { 324 for (he = hs; he < nr && buf[he] == 0; 325 ++he) 326 /* nothing */ ; 327 /* is the hole long enough to matter? */ 328 if (he >= hs + BLOCKSIZE) 329 break; 330 } 331 332 /* back down to a block boundary */ 333 he &= BLOCKMASK; 334 335 /* 336 * 1) Don't go beyond the end of the buffer. 337 * 2) If the end of the buffer is less than 338 * BLOCKSIZE bytes away, we're at the end 339 * of the file, so just grab what's left. 340 */ 341 if (hs + BLOCKSIZE > nr) 342 hs = he = nr; 343 344 /* 345 * At this point, we have a partial ordering: 346 * nw <= hs <= he <= nr 347 * If hs > nw, buf[nw..hs] contains non-zero data. 348 * If he > hs, buf[hs..he] is all zeroes. 349 */ 350 if (hs > nw) 351 if (fwrite(buf + nw, hs - nw, 1, fp) 352 != 1) 353 break; 354 if (he > hs) 355 if (fseeko(fp, he - hs, SEEK_CUR) == -1) 356 break; 357 } 358 } 359 if (nw != wl) { 360 syslog(LOG_ERR, 361 "write error on %s file: %m", filename); 362 syslog(LOG_WARNING, 363 "WARNING: vmcore may be incomplete"); 364 nerr++; 365 return (-1); 366 } 367 if (verbose) { 368 dmpcnt += wl; 369 printf("%llu\r", (unsigned long long)dmpcnt); 370 fflush(stdout); 371 } 372 dumpsize -= wl; 373 if (got_siginfo) { 374 printf("%s %.1lf%%\n", filename, (100.0 - (100.0 * 375 (double)dumpsize / (double)origsize))); 376 got_siginfo = 0; 377 } 378 } 379 return (0); 380 } 381 382 /* 383 * Specialized version of dump-reading logic for use with textdumps, which 384 * are written backwards from the end of the partition, and must be reversed 385 * before being written to the file. Textdumps are small, so do a bit less 386 * work to optimize/sparsify. 387 */ 388 static int 389 DoTextdumpFile(int fd, off_t dumpsize, off_t lasthd, char *buf, 390 const char *device, const char *filename, FILE *fp) 391 { 392 int nr, nw, wl; 393 off_t dmpcnt, totsize; 394 395 totsize = dumpsize; 396 dmpcnt = 0; 397 wl = 512; 398 if ((dumpsize % wl) != 0) { 399 syslog(LOG_ERR, "textdump uneven multiple of 512 on %s", 400 device); 401 nerr++; 402 return (-1); 403 } 404 while (dumpsize > 0) { 405 nr = pread(fd, buf, wl, lasthd - (totsize - dumpsize) - wl); 406 if (nr != wl) { 407 if (nr == 0) 408 syslog(LOG_WARNING, 409 "WARNING: EOF on dump device"); 410 else 411 syslog(LOG_ERR, "read error on %s: %m", device); 412 nerr++; 413 return (-1); 414 } 415 nw = fwrite(buf, 1, wl, fp); 416 if (nw != wl) { 417 syslog(LOG_ERR, 418 "write error on %s file: %m", filename); 419 syslog(LOG_WARNING, 420 "WARNING: textdump may be incomplete"); 421 nerr++; 422 return (-1); 423 } 424 if (verbose) { 425 dmpcnt += wl; 426 printf("%llu\r", (unsigned long long)dmpcnt); 427 fflush(stdout); 428 } 429 dumpsize -= wl; 430 } 431 return (0); 432 } 433 434 static void 435 DoFile(const char *savedir, const char *device) 436 { 437 static char infoname[PATH_MAX], corename[PATH_MAX], linkname[PATH_MAX]; 438 static char *buf = NULL; 439 struct kerneldumpheader kdhf, kdhl; 440 off_t mediasize, dumpsize, firsthd, lasthd; 441 FILE *info, *fp; 442 mode_t oumask; 443 int fd, fdinfo, error; 444 int bounds, status; 445 u_int sectorsize; 446 int istextdump; 447 448 bounds = getbounds(); 449 mediasize = 0; 450 status = STATUS_UNKNOWN; 451 452 if (maxdumps > 0 && bounds == maxdumps) 453 bounds = 0; 454 455 if (buf == NULL) { 456 buf = malloc(BUFFERSIZE); 457 if (buf == NULL) { 458 syslog(LOG_ERR, "%m"); 459 return; 460 } 461 } 462 463 if (verbose) 464 printf("checking for kernel dump on device %s\n", device); 465 466 fd = open(device, (checkfor || keep) ? O_RDONLY : O_RDWR); 467 if (fd < 0) { 468 syslog(LOG_ERR, "%s: %m", device); 469 return; 470 } 471 472 error = ioctl(fd, DIOCGMEDIASIZE, &mediasize); 473 if (!error) 474 error = ioctl(fd, DIOCGSECTORSIZE, §orsize); 475 if (error) { 476 syslog(LOG_ERR, 477 "couldn't find media and/or sector size of %s: %m", device); 478 goto closefd; 479 } 480 481 if (verbose) { 482 printf("mediasize = %lld\n", (long long)mediasize); 483 printf("sectorsize = %u\n", sectorsize); 484 } 485 486 lasthd = mediasize - sectorsize; 487 lseek(fd, lasthd, SEEK_SET); 488 error = read(fd, &kdhl, sizeof kdhl); 489 if (error != sizeof kdhl) { 490 syslog(LOG_ERR, 491 "error reading last dump header at offset %lld in %s: %m", 492 (long long)lasthd, device); 493 goto closefd; 494 } 495 istextdump = 0; 496 if (strncmp(kdhl.magic, TEXTDUMPMAGIC, sizeof kdhl) == 0) { 497 if (verbose) 498 printf("textdump magic on last dump header on %s\n", 499 device); 500 istextdump = 1; 501 if (dtoh32(kdhl.version) != KERNELDUMP_TEXT_VERSION) { 502 syslog(LOG_ERR, 503 "unknown version (%d) in last dump header on %s", 504 dtoh32(kdhl.version), device); 505 506 status = STATUS_BAD; 507 if (force == 0) 508 goto closefd; 509 } 510 } else if (memcmp(kdhl.magic, KERNELDUMPMAGIC, sizeof kdhl.magic) == 511 0) { 512 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) { 513 syslog(LOG_ERR, 514 "unknown version (%d) in last dump header on %s", 515 dtoh32(kdhl.version), device); 516 517 status = STATUS_BAD; 518 if (force == 0) 519 goto closefd; 520 } 521 } else { 522 if (verbose) 523 printf("magic mismatch on last dump header on %s\n", 524 device); 525 526 status = STATUS_BAD; 527 if (force == 0) 528 goto closefd; 529 530 if (memcmp(kdhl.magic, KERNELDUMPMAGIC_CLEARED, 531 sizeof kdhl.magic) == 0) { 532 if (verbose) 533 printf("forcing magic on %s\n", device); 534 memcpy(kdhl.magic, KERNELDUMPMAGIC, 535 sizeof kdhl.magic); 536 } else { 537 syslog(LOG_ERR, "unable to force dump - bad magic"); 538 goto closefd; 539 } 540 if (dtoh32(kdhl.version) != KERNELDUMPVERSION) { 541 syslog(LOG_ERR, 542 "unknown version (%d) in last dump header on %s", 543 dtoh32(kdhl.version), device); 544 545 status = STATUS_BAD; 546 if (force == 0) 547 goto closefd; 548 } 549 } 550 551 nfound++; 552 if (clear) 553 goto nuke; 554 555 if (kerneldump_parity(&kdhl)) { 556 syslog(LOG_ERR, 557 "parity error on last dump header on %s", device); 558 nerr++; 559 status = STATUS_BAD; 560 if (force == 0) 561 goto closefd; 562 } 563 dumpsize = dtoh64(kdhl.dumplength); 564 firsthd = lasthd - dumpsize - sizeof kdhf; 565 lseek(fd, firsthd, SEEK_SET); 566 error = read(fd, &kdhf, sizeof kdhf); 567 if (error != sizeof kdhf) { 568 syslog(LOG_ERR, 569 "error reading first dump header at offset %lld in %s: %m", 570 (long long)firsthd, device); 571 nerr++; 572 goto closefd; 573 } 574 575 if (verbose >= 2) { 576 printf("First dump headers:\n"); 577 printheader(stdout, &kdhf, device, bounds, -1); 578 579 printf("\nLast dump headers:\n"); 580 printheader(stdout, &kdhl, device, bounds, -1); 581 printf("\n"); 582 } 583 584 if (memcmp(&kdhl, &kdhf, sizeof kdhl)) { 585 syslog(LOG_ERR, 586 "first and last dump headers disagree on %s", device); 587 nerr++; 588 status = STATUS_BAD; 589 if (force == 0) 590 goto closefd; 591 } else { 592 status = STATUS_GOOD; 593 } 594 595 if (checkfor) { 596 printf("A dump exists on %s\n", device); 597 close(fd); 598 exit(0); 599 } 600 601 if (kdhl.panicstring[0]) 602 syslog(LOG_ALERT, "reboot after panic: %s", kdhl.panicstring); 603 else 604 syslog(LOG_ALERT, "reboot"); 605 606 if (verbose) 607 printf("Checking for available free space\n"); 608 609 if (!check_space(savedir, dumpsize, bounds)) { 610 nerr++; 611 goto closefd; 612 } 613 614 writebounds(bounds + 1); 615 616 saved_dump_remove(bounds); 617 618 snprintf(infoname, sizeof(infoname), "info.%d", bounds); 619 620 /* 621 * Create or overwrite any existing dump header files. 622 */ 623 fdinfo = open(infoname, O_WRONLY | O_CREAT | O_TRUNC, 0600); 624 if (fdinfo < 0) { 625 syslog(LOG_ERR, "%s: %m", infoname); 626 nerr++; 627 goto closefd; 628 } 629 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/ 630 if (compress) { 631 snprintf(corename, sizeof(corename), "%s.%d.gz", 632 istextdump ? "textdump.tar" : "vmcore", bounds); 633 fp = zopen(corename, "w"); 634 } else { 635 snprintf(corename, sizeof(corename), "%s.%d", 636 istextdump ? "textdump.tar" : "vmcore", bounds); 637 fp = fopen(corename, "w"); 638 } 639 if (fp == NULL) { 640 syslog(LOG_ERR, "%s: %m", corename); 641 close(fdinfo); 642 nerr++; 643 goto closefd; 644 } 645 (void)umask(oumask); 646 647 info = fdopen(fdinfo, "w"); 648 649 if (info == NULL) { 650 syslog(LOG_ERR, "fdopen failed: %m"); 651 nerr++; 652 goto closefd; 653 } 654 655 if (verbose) 656 printheader(stdout, &kdhl, device, bounds, status); 657 658 printheader(info, &kdhl, device, bounds, status); 659 fclose(info); 660 661 syslog(LOG_NOTICE, "writing %score to %s/%s", 662 compress ? "compressed " : "", savedir, corename); 663 664 if (istextdump) { 665 if (DoTextdumpFile(fd, dumpsize, lasthd, buf, device, 666 corename, fp) < 0) 667 goto closeall; 668 } else { 669 if (DoRegularFile(fd, dumpsize, buf, device, corename, fp) 670 < 0) 671 goto closeall; 672 } 673 if (verbose) 674 printf("\n"); 675 676 if (fclose(fp) < 0) { 677 syslog(LOG_ERR, "error on %s: %m", corename); 678 nerr++; 679 goto closefd; 680 } 681 682 symlinks_remove(); 683 if (symlink(infoname, "info.last") == -1) { 684 syslog(LOG_WARNING, "unable to create symlink %s/%s: %m", 685 savedir, "info.last"); 686 } 687 if (compress) { 688 snprintf(linkname, sizeof(linkname), "%s.last.gz", 689 istextdump ? "textdump.tar" : "vmcore"); 690 } else { 691 snprintf(linkname, sizeof(linkname), "%s.last", 692 istextdump ? "textdump.tar" : "vmcore"); 693 } 694 if (symlink(corename, linkname) == -1) { 695 syslog(LOG_WARNING, "unable to create symlink %s/%s: %m", 696 savedir, linkname); 697 } 698 699 nsaved++; 700 701 if (verbose) 702 printf("dump saved\n"); 703 704 nuke: 705 if (!keep) { 706 if (verbose) 707 printf("clearing dump header\n"); 708 memcpy(kdhl.magic, KERNELDUMPMAGIC_CLEARED, sizeof kdhl.magic); 709 lseek(fd, lasthd, SEEK_SET); 710 error = write(fd, &kdhl, sizeof kdhl); 711 if (error != sizeof kdhl) 712 syslog(LOG_ERR, 713 "error while clearing the dump header: %m"); 714 } 715 close(fd); 716 return; 717 718 closeall: 719 fclose(fp); 720 721 closefd: 722 close(fd); 723 } 724 725 static void 726 usage(void) 727 { 728 fprintf(stderr, "%s\n%s\n%s\n", 729 "usage: savecore -c [-v] [device ...]", 730 " savecore -C [-v] [device ...]", 731 " savecore [-fkvz] [-m maxdumps] [directory [device ...]]"); 732 exit(1); 733 } 734 735 int 736 main(int argc, char **argv) 737 { 738 const char *savedir = "."; 739 struct fstab *fsp; 740 int i, ch, error; 741 742 checkfor = compress = clear = force = keep = verbose = 0; 743 nfound = nsaved = nerr = 0; 744 745 openlog("savecore", LOG_PERROR, LOG_DAEMON); 746 signal(SIGINFO, infohandler); 747 748 while ((ch = getopt(argc, argv, "Ccfkm:vz")) != -1) 749 switch(ch) { 750 case 'C': 751 checkfor = 1; 752 break; 753 case 'c': 754 clear = 1; 755 break; 756 case 'f': 757 force = 1; 758 break; 759 case 'k': 760 keep = 1; 761 break; 762 case 'm': 763 maxdumps = atoi(optarg); 764 if (maxdumps <= 0) { 765 syslog(LOG_ERR, "Invalid maxdump value"); 766 exit(1); 767 } 768 break; 769 case 'v': 770 verbose++; 771 break; 772 case 'z': 773 compress = 1; 774 break; 775 case '?': 776 default: 777 usage(); 778 } 779 if (checkfor && (clear || force || keep)) 780 usage(); 781 if (clear && (compress || keep)) 782 usage(); 783 if (maxdumps > 0 && (checkfor || clear)) 784 usage(); 785 argc -= optind; 786 argv += optind; 787 if (argc >= 1 && !checkfor && !clear) { 788 error = chdir(argv[0]); 789 if (error) { 790 syslog(LOG_ERR, "chdir(%s): %m", argv[0]); 791 exit(1); 792 } 793 savedir = argv[0]; 794 argc--; 795 argv++; 796 } 797 if (argc == 0) { 798 for (;;) { 799 fsp = getfsent(); 800 if (fsp == NULL) 801 break; 802 if (strcmp(fsp->fs_vfstype, "swap") && 803 strcmp(fsp->fs_vfstype, "dump")) 804 continue; 805 DoFile(savedir, fsp->fs_spec); 806 } 807 } else { 808 for (i = 0; i < argc; i++) 809 DoFile(savedir, argv[i]); 810 } 811 812 /* Emit minimal output. */ 813 if (nfound == 0) { 814 if (checkfor) { 815 printf("No dump exists\n"); 816 exit(1); 817 } 818 syslog(LOG_WARNING, "no dumps found"); 819 } 820 else if (nsaved == 0) { 821 if (nerr != 0) 822 syslog(LOG_WARNING, "unsaved dumps found but not saved"); 823 else 824 syslog(LOG_WARNING, "no unsaved dumps found"); 825 } 826 827 return (0); 828 } 829 830 static void 831 infohandler(int sig __unused) 832 { 833 got_siginfo = 1; 834 } 835