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