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