1 /*- 2 * Copyright (c) 1986, 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1986, 1992, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)savecore.c 8.3 (Berkeley) 1/2/94"; 43 #endif 44 static const char rcsid[] = 45 "$Id: savecore.c,v 1.21 1998/07/28 06:38:57 charnier Exp $"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/stat.h> 50 #include <sys/mount.h> 51 #include <sys/syslog.h> 52 53 #include <vm/vm.h> 54 #include <vm/vm_param.h> 55 #include <vm/pmap.h> 56 57 #include <dirent.h> 58 #include <errno.h> 59 #include <fcntl.h> 60 #include <nlist.h> 61 #include <paths.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <unistd.h> 66 #include "zopen.h" 67 68 #ifdef __alpha__ 69 #define ok(number) ALPHA_K0SEG_TO_PHYS(number) 70 #endif 71 72 #ifdef __i386__ 73 #define ok(number) ((number) - KERNBASE) 74 #endif 75 76 struct nlist current_nl[] = { /* Namelist for currently running system. */ 77 #define X_DUMPDEV 0 78 { "_dumpdev" }, 79 #define X_DUMPLO 1 80 { "_dumplo" }, 81 #define X_TIME 2 82 { "_time_second" }, 83 #define X_DUMPSIZE 3 84 { "_dumpsize" }, 85 #define X_VERSION 4 86 { "_version" }, 87 #define X_PANICSTR 5 88 { "_panicstr" }, 89 #define X_DUMPMAG 6 90 { "_dumpmag" }, 91 { "" }, 92 }; 93 int cursyms[] = { X_DUMPDEV, X_DUMPLO, X_VERSION, X_DUMPMAG, -1 }; 94 int dumpsyms[] = { X_TIME, X_DUMPSIZE, X_VERSION, X_PANICSTR, X_DUMPMAG, -1 }; 95 96 struct nlist dump_nl[] = { /* Name list for dumped system. */ 97 { "_dumpdev" }, /* Entries MUST be the same as */ 98 { "_dumplo" }, /* those in current_nl[]. */ 99 { "_time_second" }, 100 { "_dumpsize" }, 101 { "_version" }, 102 { "_panicstr" }, 103 { "_dumpmag" }, 104 { "" }, 105 }; 106 107 /* Types match kernel declarations. */ 108 long dumplo; /* where dump starts on dumpdev */ 109 int dumpmag; /* magic number in dump */ 110 int dumpsize; /* amount of memory dumped */ 111 112 char *kernel; 113 char *dirname; /* directory to save dumps in */ 114 char *ddname; /* name of dump device */ 115 dev_t dumpdev; /* dump device */ 116 int dumpfd; /* read/write descriptor on block dev */ 117 time_t now; /* current date */ 118 char panic_mesg[1024]; 119 int panicstr; 120 char vers[1024]; 121 122 int clear, compress, force, verbose; /* flags */ 123 124 void check_kmem __P((void)); 125 int check_space __P((void)); 126 void clear_dump __P((void)); 127 int Create __P((char *, int)); 128 int dump_exists __P((void)); 129 char *find_dev __P((dev_t, int)); 130 int get_crashtime __P((void)); 131 void get_dumpsize __P((void)); 132 void kmem_setup __P((void)); 133 void log __P((int, char *, ...)); 134 void Lseek __P((int, off_t, int)); 135 int Open __P((const char *, int rw)); 136 int Read __P((int, void *, int)); 137 char *rawname __P((char *s)); 138 void save_core __P((void)); 139 void usage __P((void)); 140 void Write __P((int, void *, int)); 141 142 int 143 main(argc, argv) 144 int argc; 145 char *argv[]; 146 { 147 int ch; 148 149 openlog("savecore", LOG_PERROR, LOG_DAEMON); 150 151 while ((ch = getopt(argc, argv, "cdfN:vz")) != -1) 152 switch(ch) { 153 case 'c': 154 clear = 1; 155 break; 156 case 'd': /* Not documented. */ 157 case 'v': 158 verbose = 1; 159 break; 160 case 'f': 161 force = 1; 162 break; 163 case 'N': 164 kernel = optarg; 165 break; 166 case 'z': 167 compress = 1; 168 break; 169 case '?': 170 default: 171 usage(); 172 } 173 argc -= optind; 174 argv += optind; 175 176 if (!clear) { 177 if (argc != 1 && argc != 2) 178 usage(); 179 dirname = argv[0]; 180 } 181 if (argc == 2) 182 kernel = argv[1]; 183 184 (void)time(&now); 185 kmem_setup(); 186 187 if (clear) { 188 clear_dump(); 189 exit(0); 190 } 191 192 if (!dump_exists() && !force) 193 exit(1); 194 195 check_kmem(); 196 197 if (panicstr) 198 syslog(LOG_ALERT, "reboot after panic: %s", panic_mesg); 199 else 200 syslog(LOG_ALERT, "reboot"); 201 202 get_dumpsize(); 203 204 if ((!get_crashtime() || !check_space()) && !force) 205 exit(1); 206 207 save_core(); 208 209 clear_dump(); 210 exit(0); 211 } 212 213 void 214 kmem_setup() 215 { 216 FILE *fp; 217 int kmem, i; 218 const char *dump_sys; 219 220 /* 221 * Some names we need for the currently running system, others for 222 * the system that was running when the dump was made. The values 223 * obtained from the current system are used to look for things in 224 * /dev/kmem that cannot be found in the dump_sys namelist, but are 225 * presumed to be the same (since the disk partitions are probably 226 * the same!) 227 */ 228 if ((nlist(getbootfile(), current_nl)) == -1) 229 syslog(LOG_ERR, "%s: nlist: %s", getbootfile(), 230 strerror(errno)); 231 for (i = 0; cursyms[i] != -1; i++) 232 if (current_nl[cursyms[i]].n_value == 0) { 233 syslog(LOG_ERR, "%s: %s not in namelist", 234 getbootfile(), current_nl[cursyms[i]].n_name); 235 exit(1); 236 } 237 238 dump_sys = kernel ? kernel : getbootfile(); 239 if ((nlist(dump_sys, dump_nl)) == -1) 240 syslog(LOG_ERR, "%s: nlist: %s", dump_sys, strerror(errno)); 241 for (i = 0; dumpsyms[i] != -1; i++) 242 if (dump_nl[dumpsyms[i]].n_value == 0) { 243 syslog(LOG_ERR, "%s: %s not in namelist", 244 dump_sys, dump_nl[dumpsyms[i]].n_name); 245 exit(1); 246 } 247 248 kmem = Open(_PATH_KMEM, O_RDONLY); 249 Lseek(kmem, (off_t)current_nl[X_DUMPDEV].n_value, L_SET); 250 (void)Read(kmem, &dumpdev, sizeof(dumpdev)); 251 if (dumpdev == NODEV) { 252 syslog(LOG_WARNING, "no core dump (no dumpdev)"); 253 exit(1); 254 } 255 Lseek(kmem, (off_t)current_nl[X_DUMPLO].n_value, L_SET); 256 (void)Read(kmem, &dumplo, sizeof(dumplo)); 257 if (verbose) 258 (void)printf("dumplo = %ld (%ld * %d)\n", 259 dumplo, dumplo/DEV_BSIZE, DEV_BSIZE); 260 Lseek(kmem, (off_t)current_nl[X_DUMPMAG].n_value, L_SET); 261 (void)Read(kmem, &dumpmag, sizeof(dumpmag)); 262 dumplo *= DEV_BSIZE; 263 ddname = find_dev(dumpdev, S_IFBLK); 264 dumpfd = Open(ddname, O_RDWR); 265 fp = fdopen(kmem, "r"); 266 if (fp == NULL) { 267 syslog(LOG_ERR, "%s: fdopen: %m", _PATH_KMEM); 268 exit(1); 269 } 270 if (kernel) 271 return; 272 (void)fseek(fp, (off_t)current_nl[X_VERSION].n_value, L_SET); 273 (void)fgets(vers, sizeof(vers), fp); 274 275 /* Don't fclose(fp), we use dumpfd later. */ 276 } 277 278 void 279 check_kmem() 280 { 281 register char *cp; 282 FILE *fp; 283 char core_vers[1024]; 284 285 fp = fdopen(dumpfd, "r"); 286 if (fp == NULL) { 287 syslog(LOG_ERR, "%s: fdopen: %m", ddname); 288 exit(1); 289 } 290 fseek(fp, (off_t)(dumplo + ok(dump_nl[X_VERSION].n_value)), L_SET); 291 fgets(core_vers, sizeof(core_vers), fp); 292 if (strcmp(vers, core_vers) && kernel == 0) 293 syslog(LOG_WARNING, 294 "warning: %s version mismatch:\n\t%s\nand\t%s\n", 295 getbootfile(), vers, core_vers); 296 (void)fseek(fp, 297 (off_t)(dumplo + ok(dump_nl[X_PANICSTR].n_value)), L_SET); 298 (void)fread(&panicstr, sizeof(panicstr), 1, fp); 299 if (panicstr) { 300 (void)fseek(fp, dumplo + ok(panicstr), L_SET); 301 cp = panic_mesg; 302 do 303 *cp = getc(fp); 304 while (*cp++ && cp < &panic_mesg[sizeof(panic_mesg)]); 305 } 306 /* Don't fclose(fp), we use dumpfd later. */ 307 } 308 309 void 310 clear_dump() 311 { 312 long newdumplo; 313 314 newdumplo = 0; 315 Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET); 316 Write(dumpfd, &newdumplo, sizeof(newdumplo)); 317 } 318 319 int 320 dump_exists() 321 { 322 int newdumpmag; 323 324 Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPMAG].n_value)), L_SET); 325 (void)Read(dumpfd, &newdumpmag, sizeof(newdumpmag)); 326 if (newdumpmag != dumpmag) { 327 if (verbose) 328 syslog(LOG_WARNING, "magic number mismatch (%x != %x)", 329 newdumpmag, dumpmag); 330 syslog(LOG_WARNING, "no core dump"); 331 return (0); 332 } 333 return (1); 334 } 335 336 char buf[1024 * 1024]; 337 338 void 339 save_core() 340 { 341 register FILE *fp; 342 register int bounds, ifd, nr, nw, ofd; 343 char *rawp, path[MAXPATHLEN]; 344 mode_t oumask; 345 346 /* 347 * Get the current number and update the bounds file. Do the update 348 * now, because may fail later and don't want to overwrite anything. 349 */ 350 (void)snprintf(path, sizeof(path), "%s/bounds", dirname); 351 if ((fp = fopen(path, "r")) == NULL) 352 goto err1; 353 if (fgets(buf, sizeof(buf), fp) == NULL) { 354 if (ferror(fp)) 355 err1: syslog(LOG_WARNING, "%s: %s", path, strerror(errno)); 356 bounds = 0; 357 } else 358 bounds = atoi(buf); 359 if (fp != NULL) 360 (void)fclose(fp); 361 if ((fp = fopen(path, "w")) == NULL) 362 syslog(LOG_ERR, "%s: %m", path); 363 else { 364 (void)fprintf(fp, "%d\n", bounds + 1); 365 (void)fclose(fp); 366 } 367 368 /* Create the core file. */ 369 oumask = umask(S_IRWXG|S_IRWXO); /* Restrict access to the core file.*/ 370 (void)snprintf(path, sizeof(path), "%s/vmcore.%d%s", 371 dirname, bounds, compress ? ".Z" : ""); 372 if (compress) { 373 if ((fp = zopen(path, "w", 0)) == NULL) { 374 syslog(LOG_ERR, "%s: %s", path, strerror(errno)); 375 exit(1); 376 } 377 } else 378 ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 379 (void)umask(oumask); 380 381 /* Open the raw device. */ 382 rawp = rawname(ddname); 383 if ((ifd = open(rawp, O_RDONLY)) == -1) { 384 syslog(LOG_WARNING, "%s: %m; using block device", rawp); 385 ifd = dumpfd; 386 } 387 388 /* Seek to the start of the core. */ 389 Lseek(ifd, (off_t)dumplo, L_SET); 390 391 /* Copy the core file. */ 392 syslog(LOG_NOTICE, "writing %score to %s", 393 compress ? "compressed " : "", path); 394 for (; dumpsize > 0; dumpsize -= nr) { 395 (void)printf("%6dK\r", dumpsize / 1024); 396 (void)fflush(stdout); 397 nr = read(ifd, buf, MIN(dumpsize, sizeof(buf))); 398 if (nr <= 0) { 399 if (nr == 0) 400 syslog(LOG_WARNING, 401 "WARNING: EOF on dump device"); 402 else 403 syslog(LOG_ERR, "%s: %m", rawp); 404 goto err2; 405 } 406 if (compress) 407 nw = fwrite(buf, 1, nr, fp); 408 else 409 nw = write(ofd, buf, nr); 410 if (nw != nr) { 411 syslog(LOG_ERR, "%s: %s", 412 path, strerror(nw == 0 ? EIO : errno)); 413 err2: syslog(LOG_WARNING, 414 "WARNING: vmcore may be incomplete"); 415 (void)printf("\n"); 416 exit(1); 417 } 418 } 419 (void)close(ifd); 420 if (compress) 421 (void)fclose(fp); 422 else 423 (void)close(ofd); 424 425 /* Copy the kernel. */ 426 ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY); 427 (void)snprintf(path, sizeof(path), "%s/kernel.%d%s", 428 dirname, bounds, compress ? ".Z" : ""); 429 if (compress) { 430 if ((fp = zopen(path, "w", 0)) == NULL) { 431 syslog(LOG_ERR, "%s: %s", path, strerror(errno)); 432 exit(1); 433 } 434 } else 435 ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 436 syslog(LOG_NOTICE, "writing %skernel to %s", 437 compress ? "compressed " : "", path); 438 while ((nr = read(ifd, buf, sizeof(buf))) > 0) { 439 if (compress) 440 nw = fwrite(buf, 1, nr, fp); 441 else 442 nw = write(ofd, buf, nr); 443 if (nw != nr) { 444 syslog(LOG_ERR, "%s: %s", 445 path, strerror(nw == 0 ? EIO : errno)); 446 syslog(LOG_WARNING, 447 "WARNING: kernel may be incomplete"); 448 exit(1); 449 } 450 } 451 if (nr < 0) { 452 syslog(LOG_ERR, "%s: %s", 453 kernel ? kernel : getbootfile(), strerror(errno)); 454 syslog(LOG_WARNING, 455 "WARNING: kernel may be incomplete"); 456 exit(1); 457 } 458 if (compress) 459 (void)fclose(fp); 460 else 461 (void)close(ofd); 462 } 463 464 char * 465 find_dev(dev, type) 466 register dev_t dev; 467 register int type; 468 { 469 register DIR *dfd; 470 struct dirent *dir; 471 struct stat sb; 472 char *dp, devname[MAXPATHLEN + 1]; 473 474 if ((dfd = opendir(_PATH_DEV)) == NULL) { 475 syslog(LOG_ERR, "%s: %s", _PATH_DEV, strerror(errno)); 476 exit(1); 477 } 478 (void)strcpy(devname, _PATH_DEV); 479 while ((dir = readdir(dfd))) { 480 (void)strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name); 481 if (lstat(devname, &sb)) { 482 syslog(LOG_ERR, "%s: %s", devname, strerror(errno)); 483 continue; 484 } 485 if ((sb.st_mode & S_IFMT) != type) 486 continue; 487 if (dev == sb.st_rdev) { 488 closedir(dfd); 489 if ((dp = strdup(devname)) == NULL) { 490 syslog(LOG_ERR, "%s", strerror(errno)); 491 exit(1); 492 } 493 return (dp); 494 } 495 } 496 closedir(dfd); 497 syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev)); 498 exit(1); 499 } 500 501 char * 502 rawname(s) 503 char *s; 504 { 505 char *sl, name[MAXPATHLEN]; 506 507 if ((sl = rindex(s, '/')) == NULL || sl[1] == '0') { 508 syslog(LOG_ERR, 509 "can't make raw dump device name from %s", s); 510 return (s); 511 } 512 (void)snprintf(name, sizeof(name), "%.*s/r%s", sl - s, s, sl + 1); 513 if ((sl = strdup(name)) == NULL) { 514 syslog(LOG_ERR, "%s", strerror(errno)); 515 exit(1); 516 } 517 return (sl); 518 } 519 520 int 521 get_crashtime() 522 { 523 time_t dumptime; /* Time the dump was taken. */ 524 525 Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET); 526 (void)Read(dumpfd, &dumptime, sizeof(dumptime)); 527 if (dumptime == 0) { 528 if (verbose) 529 syslog(LOG_ERR, "dump time is zero"); 530 return (0); 531 } 532 (void)printf("savecore: system went down at %s", ctime(&dumptime)); 533 #define LEEWAY (7 * 86400) 534 if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) { 535 (void)printf("dump time is unreasonable\n"); 536 return (0); 537 } 538 return (1); 539 } 540 541 void 542 get_dumpsize() 543 { 544 /* Read the dump size. */ 545 Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET); 546 (void)Read(dumpfd, &dumpsize, sizeof(dumpsize)); 547 dumpsize *= getpagesize(); 548 } 549 550 int 551 check_space() 552 { 553 register FILE *fp; 554 const char *tkernel; 555 off_t minfree, spacefree, totfree, kernelsize, needed; 556 struct stat st; 557 struct statfs fsbuf; 558 char buf[100], path[MAXPATHLEN]; 559 560 tkernel = kernel ? kernel : getbootfile(); 561 if (stat(tkernel, &st) < 0) { 562 syslog(LOG_ERR, "%s: %m", tkernel); 563 exit(1); 564 } 565 kernelsize = st.st_blocks * S_BLKSIZE; 566 567 if (statfs(dirname, &fsbuf) < 0) { 568 syslog(LOG_ERR, "%s: %m", dirname); 569 exit(1); 570 } 571 spacefree = ((off_t) fsbuf.f_bavail * fsbuf.f_bsize) / 1024; 572 totfree = ((off_t) fsbuf.f_bfree * fsbuf.f_bsize) / 1024; 573 574 (void)snprintf(path, sizeof(path), "%s/minfree", dirname); 575 if ((fp = fopen(path, "r")) == NULL) 576 minfree = 0; 577 else { 578 if (fgets(buf, sizeof(buf), fp) == NULL) 579 minfree = 0; 580 else 581 minfree = atoi(buf); 582 (void)fclose(fp); 583 } 584 585 needed = (dumpsize + kernelsize) / 1024; 586 if (((minfree > 0) ? spacefree : totfree) - needed < minfree) { 587 syslog(LOG_WARNING, 588 "no dump, not enough free space on device"); 589 return (0); 590 } 591 if (spacefree - needed < 0) 592 syslog(LOG_WARNING, 593 "dump performed, but free space threshold crossed"); 594 return (1); 595 } 596 597 int 598 Open(name, rw) 599 const char *name; 600 int rw; 601 { 602 int fd; 603 604 if ((fd = open(name, rw, 0)) < 0) { 605 syslog(LOG_ERR, "%s: %m", name); 606 exit(1); 607 } 608 return (fd); 609 } 610 611 int 612 Read(fd, bp, size) 613 int fd, size; 614 void *bp; 615 { 616 int nr; 617 618 nr = read(fd, bp, size); 619 if (nr != size) { 620 syslog(LOG_ERR, "read: %m"); 621 exit(1); 622 } 623 return (nr); 624 } 625 626 void 627 Lseek(fd, off, flag) 628 int fd, flag; 629 off_t off; 630 { 631 off_t ret; 632 633 ret = lseek(fd, off, flag); 634 if (ret == -1) { 635 syslog(LOG_ERR, "lseek: %m"); 636 exit(1); 637 } 638 } 639 640 int 641 Create(file, mode) 642 char *file; 643 int mode; 644 { 645 register int fd; 646 647 fd = creat(file, mode); 648 if (fd < 0) { 649 syslog(LOG_ERR, "%s: %m", file); 650 exit(1); 651 } 652 return (fd); 653 } 654 655 void 656 Write(fd, bp, size) 657 int fd, size; 658 void *bp; 659 { 660 int n; 661 662 if ((n = write(fd, bp, size)) < size) { 663 syslog(LOG_ERR, "write: %s", strerror(n == -1 ? errno : EIO)); 664 exit(1); 665 } 666 } 667 668 void 669 usage() 670 { 671 (void)syslog(LOG_ERR, "usage: savecore [-cfvz] [-N system] directory"); 672 exit(1); 673 } 674