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 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, "cdfN:vz")) != 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 354 /* Create the core file. */ 355 (void)snprintf(path, sizeof(path), "%s/vmcore.%d%s", 356 dirname, bounds, compress ? ".Z" : ""); 357 if (compress) { 358 if ((fp = zopen(path, "w", 0)) == NULL) { 359 syslog(LOG_ERR, "%s: %s", path, strerror(errno)); 360 exit(1); 361 } 362 } else 363 ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 364 365 /* Open the raw device. */ 366 rawp = rawname(ddname); 367 if ((ifd = open(rawp, O_RDONLY)) == -1) { 368 syslog(LOG_WARNING, "%s: %m; using block device", rawp); 369 ifd = dumpfd; 370 } 371 372 /* Read the dump size. */ 373 Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_DUMPSIZE].n_value)), L_SET); 374 (void)Read(dumpfd, &dumpsize, sizeof(dumpsize)); 375 376 /* Seek to the start of the core. */ 377 Lseek(ifd, (off_t)dumplo, L_SET); 378 379 /* Copy the core file. */ 380 dumpsize *= NBPG; 381 syslog(LOG_NOTICE, "writing %score to %s", 382 compress ? "compressed " : "", path); 383 for (; dumpsize > 0; dumpsize -= nr) { 384 (void)printf("%6dK\r", dumpsize / 1024); 385 (void)fflush(stdout); 386 nr = read(ifd, buf, MIN(dumpsize, sizeof(buf))); 387 if (nr <= 0) { 388 if (nr == 0) 389 syslog(LOG_WARNING, 390 "WARNING: EOF on dump device"); 391 else 392 syslog(LOG_ERR, "%s: %m", rawp); 393 goto err2; 394 } 395 if (compress) 396 nw = fwrite(buf, 1, nr, fp); 397 else 398 nw = write(ofd, buf, nr); 399 if (nw != nr) { 400 syslog(LOG_ERR, "%s: %s", 401 path, strerror(nw == 0 ? EIO : errno)); 402 err2: syslog(LOG_WARNING, 403 "WARNING: vmcore may be incomplete"); 404 (void)printf("\n"); 405 exit(1); 406 } 407 } 408 (void)close(ifd); 409 if (compress) 410 (void)fclose(fp); 411 else 412 (void)close(ofd); 413 414 /* Copy the kernel. */ 415 ifd = Open(kernel ? kernel : getbootfile(), O_RDONLY); 416 (void)snprintf(path, sizeof(path), "%s/kernel.%d%s", 417 dirname, bounds, compress ? ".Z" : ""); 418 if (compress) { 419 if ((fp = zopen(path, "w", 0)) == NULL) { 420 syslog(LOG_ERR, "%s: %s", path, strerror(errno)); 421 exit(1); 422 } 423 } else 424 ofd = Create(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 425 syslog(LOG_NOTICE, "writing %skernel to %s", 426 compress ? "compressed " : "", path); 427 while ((nr = read(ifd, buf, sizeof(buf))) > 0) { 428 if (compress) 429 nw = fwrite(buf, 1, nr, fp); 430 else 431 nw = write(ofd, buf, nr); 432 if (nw != nr) { 433 syslog(LOG_ERR, "%s: %s", 434 path, strerror(nw == 0 ? EIO : errno)); 435 syslog(LOG_WARNING, 436 "WARNING: kernel may be incomplete"); 437 exit(1); 438 } 439 } 440 if (nr < 0) { 441 syslog(LOG_ERR, "%s: %s", 442 kernel ? kernel : getbootfile(), strerror(errno)); 443 syslog(LOG_WARNING, 444 "WARNING: kernel may be incomplete"); 445 exit(1); 446 } 447 if (compress) 448 (void)fclose(fp); 449 else 450 (void)close(ofd); 451 } 452 453 char * 454 find_dev(dev, type) 455 register dev_t dev; 456 register int type; 457 { 458 register DIR *dfd; 459 struct dirent *dir; 460 struct stat sb; 461 char *dp, devname[MAXPATHLEN + 1]; 462 463 if ((dfd = opendir(_PATH_DEV)) == NULL) { 464 syslog(LOG_ERR, "%s: %s", _PATH_DEV, strerror(errno)); 465 exit(1); 466 } 467 (void)strcpy(devname, _PATH_DEV); 468 while ((dir = readdir(dfd))) { 469 (void)strcpy(devname + sizeof(_PATH_DEV) - 1, dir->d_name); 470 if (lstat(devname, &sb)) { 471 syslog(LOG_ERR, "%s: %s", devname, strerror(errno)); 472 continue; 473 } 474 if ((sb.st_mode & S_IFMT) != type) 475 continue; 476 if (dev == sb.st_rdev) { 477 closedir(dfd); 478 if ((dp = strdup(devname)) == NULL) { 479 syslog(LOG_ERR, "%s", strerror(errno)); 480 exit(1); 481 } 482 return (dp); 483 } 484 } 485 closedir(dfd); 486 syslog(LOG_ERR, "can't find device %d/%d", major(dev), minor(dev)); 487 exit(1); 488 } 489 490 char * 491 rawname(s) 492 char *s; 493 { 494 char *sl, name[MAXPATHLEN]; 495 496 if ((sl = rindex(s, '/')) == NULL || sl[1] == '0') { 497 syslog(LOG_ERR, 498 "can't make raw dump device name from %s", s); 499 return (s); 500 } 501 (void)snprintf(name, sizeof(name), "%.*s/r%s", sl - s, s, sl + 1); 502 if ((sl = strdup(name)) == NULL) { 503 syslog(LOG_ERR, "%s", strerror(errno)); 504 exit(1); 505 } 506 return (sl); 507 } 508 509 int 510 get_crashtime() 511 { 512 time_t dumptime; /* Time the dump was taken. */ 513 514 Lseek(dumpfd, (off_t)(dumplo + ok(dump_nl[X_TIME].n_value)), L_SET); 515 (void)Read(dumpfd, &dumptime, sizeof(dumptime)); 516 if (dumptime == 0) { 517 if (verbose) 518 syslog(LOG_ERR, "dump time is zero"); 519 return (0); 520 } 521 (void)printf("savecore: system went down at %s", ctime(&dumptime)); 522 #define LEEWAY (7 * 86400) 523 if (dumptime < now - LEEWAY || dumptime > now + LEEWAY) { 524 (void)printf("dump time is unreasonable\n"); 525 return (0); 526 } 527 return (1); 528 } 529 530 int 531 check_space() 532 { 533 register FILE *fp; 534 const char *tkernel; 535 off_t minfree, spacefree, kernelsize, needed; 536 struct stat st; 537 struct statfs fsbuf; 538 char buf[100], path[MAXPATHLEN]; 539 540 tkernel = kernel ? kernel : getbootfile(); 541 if (stat(tkernel, &st) < 0) { 542 syslog(LOG_ERR, "%s: %m", tkernel); 543 exit(1); 544 } 545 kernelsize = st.st_blocks * S_BLKSIZE; 546 if (statfs(dirname, &fsbuf) < 0) { 547 syslog(LOG_ERR, "%s: %m", dirname); 548 exit(1); 549 } 550 spacefree = (fsbuf.f_bavail * fsbuf.f_bsize) / 1024; 551 552 (void)snprintf(path, sizeof(path), "%s/minfree", dirname); 553 if ((fp = fopen(path, "r")) == NULL) 554 minfree = 0; 555 else { 556 if (fgets(buf, sizeof(buf), fp) == NULL) 557 minfree = 0; 558 else 559 minfree = atoi(buf); 560 (void)fclose(fp); 561 } 562 563 needed = (dumpsize + kernelsize) / 1024; 564 if (minfree > 0 && spacefree - needed < minfree) { 565 syslog(LOG_WARNING, 566 "no dump, not enough free space on device"); 567 return (0); 568 } 569 if (spacefree - needed < minfree) 570 syslog(LOG_WARNING, 571 "dump performed, but free space threshold crossed"); 572 return (1); 573 } 574 575 int 576 Open(name, rw) 577 const char *name; 578 int rw; 579 { 580 int fd; 581 582 if ((fd = open(name, rw, 0)) < 0) { 583 syslog(LOG_ERR, "%s: %m", name); 584 exit(1); 585 } 586 return (fd); 587 } 588 589 int 590 Read(fd, bp, size) 591 int fd, size; 592 void *bp; 593 { 594 int nr; 595 596 nr = read(fd, bp, size); 597 if (nr != size) { 598 syslog(LOG_ERR, "read: %m"); 599 exit(1); 600 } 601 return (nr); 602 } 603 604 void 605 Lseek(fd, off, flag) 606 int fd, flag; 607 off_t off; 608 { 609 off_t ret; 610 611 ret = lseek(fd, off, flag); 612 if (ret == -1) { 613 syslog(LOG_ERR, "lseek: %m"); 614 exit(1); 615 } 616 } 617 618 int 619 Create(file, mode) 620 char *file; 621 int mode; 622 { 623 register int fd; 624 625 fd = creat(file, mode); 626 if (fd < 0) { 627 syslog(LOG_ERR, "%s: %m", file); 628 exit(1); 629 } 630 return (fd); 631 } 632 633 void 634 Write(fd, bp, size) 635 int fd, size; 636 void *bp; 637 { 638 int n; 639 640 if ((n = write(fd, bp, size)) < size) { 641 syslog(LOG_ERR, "write: %s", strerror(n == -1 ? errno : EIO)); 642 exit(1); 643 } 644 } 645 646 void 647 usage() 648 { 649 (void)syslog(LOG_ERR, "usage: savecore [-cfvz] [-N system] directory"); 650 exit(1); 651 } 652