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