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