1 /*- 2 * Copyright (c) 1986, 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_shutdown.c 8.3 (Berkeley) 1/21/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_ddb.h" 41 #include "opt_kdb.h" 42 #include "opt_panic.h" 43 #include "opt_sched.h" 44 #include "opt_watchdog.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/bio.h> 49 #include <sys/buf.h> 50 #include <sys/conf.h> 51 #include <sys/cons.h> 52 #include <sys/eventhandler.h> 53 #include <sys/jail.h> 54 #include <sys/kdb.h> 55 #include <sys/kernel.h> 56 #include <sys/kerneldump.h> 57 #include <sys/kthread.h> 58 #include <sys/malloc.h> 59 #include <sys/mount.h> 60 #include <sys/priv.h> 61 #include <sys/proc.h> 62 #include <sys/reboot.h> 63 #include <sys/resourcevar.h> 64 #include <sys/sched.h> 65 #include <sys/smp.h> 66 #include <sys/sysctl.h> 67 #include <sys/sysproto.h> 68 #include <sys/vnode.h> 69 #ifdef SW_WATCHDOG 70 #include <sys/watchdog.h> 71 #endif 72 73 #include <ddb/ddb.h> 74 75 #include <machine/cpu.h> 76 #include <machine/pcb.h> 77 #include <machine/smp.h> 78 79 #include <security/mac/mac_framework.h> 80 81 #include <vm/vm.h> 82 #include <vm/vm_object.h> 83 #include <vm/vm_page.h> 84 #include <vm/vm_pager.h> 85 #include <vm/swap_pager.h> 86 87 #include <sys/signalvar.h> 88 89 #ifndef PANIC_REBOOT_WAIT_TIME 90 #define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 91 #endif 92 93 /* 94 * Note that stdarg.h and the ANSI style va_start macro is used for both 95 * ANSI and traditional C compilers. 96 */ 97 #include <machine/stdarg.h> 98 99 #ifdef KDB 100 #ifdef KDB_UNATTENDED 101 int debugger_on_panic = 0; 102 #else 103 int debugger_on_panic = 1; 104 #endif 105 SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, 106 CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN, 107 &debugger_on_panic, 0, "Run debugger on kernel panic"); 108 TUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic); 109 110 #ifdef KDB_TRACE 111 static int trace_on_panic = 1; 112 #else 113 static int trace_on_panic = 0; 114 #endif 115 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, 116 CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_TUN, 117 &trace_on_panic, 0, "Print stack trace on kernel panic"); 118 TUNABLE_INT("debug.trace_on_panic", &trace_on_panic); 119 #endif /* KDB */ 120 121 static int sync_on_panic = 0; 122 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN, 123 &sync_on_panic, 0, "Do a sync before rebooting from a panic"); 124 TUNABLE_INT("kern.sync_on_panic", &sync_on_panic); 125 126 static int stop_scheduler_on_panic = 0; 127 SYSCTL_INT(_kern, OID_AUTO, stop_scheduler_on_panic, CTLFLAG_RW | CTLFLAG_TUN, 128 &stop_scheduler_on_panic, 0, "stop scheduler upon entering panic"); 129 TUNABLE_INT("kern.stop_scheduler_on_panic", &stop_scheduler_on_panic); 130 131 static SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, 132 "Shutdown environment"); 133 134 #ifndef DIAGNOSTIC 135 static int show_busybufs; 136 #else 137 static int show_busybufs = 1; 138 #endif 139 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW, 140 &show_busybufs, 0, ""); 141 142 /* 143 * Variable panicstr contains argument to first call to panic; used as flag 144 * to indicate that the kernel has already called panic. 145 */ 146 const char *panicstr; 147 148 int stop_scheduler; /* system stopped CPUs for panic */ 149 int dumping; /* system is dumping */ 150 int rebooting; /* system is rebooting */ 151 static struct dumperinfo dumper; /* our selected dumper */ 152 153 /* Context information for dump-debuggers. */ 154 static struct pcb dumppcb; /* Registers. */ 155 static lwpid_t dumptid; /* Thread ID. */ 156 157 static void poweroff_wait(void *, int); 158 static void shutdown_halt(void *junk, int howto); 159 static void shutdown_panic(void *junk, int howto); 160 static void shutdown_reset(void *junk, int howto); 161 162 /* register various local shutdown events */ 163 static void 164 shutdown_conf(void *unused) 165 { 166 167 EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL, 168 SHUTDOWN_PRI_FIRST); 169 EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL, 170 SHUTDOWN_PRI_LAST + 100); 171 EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL, 172 SHUTDOWN_PRI_LAST + 100); 173 EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL, 174 SHUTDOWN_PRI_LAST + 200); 175 } 176 177 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL); 178 179 /* 180 * The system call that results in a reboot. 181 */ 182 /* ARGSUSED */ 183 int 184 sys_reboot(struct thread *td, struct reboot_args *uap) 185 { 186 int error; 187 188 error = 0; 189 #ifdef MAC 190 error = mac_system_check_reboot(td->td_ucred, uap->opt); 191 #endif 192 if (error == 0) 193 error = priv_check(td, PRIV_REBOOT); 194 if (error == 0) { 195 mtx_lock(&Giant); 196 kern_reboot(uap->opt); 197 mtx_unlock(&Giant); 198 } 199 return (error); 200 } 201 202 /* 203 * Called by events that want to shut down.. e.g <CTL><ALT><DEL> on a PC 204 */ 205 static int shutdown_howto = 0; 206 207 void 208 shutdown_nice(int howto) 209 { 210 211 shutdown_howto = howto; 212 213 /* Send a signal to init(8) and have it shutdown the world */ 214 if (initproc != NULL) { 215 PROC_LOCK(initproc); 216 kern_psignal(initproc, SIGINT); 217 PROC_UNLOCK(initproc); 218 } else { 219 /* No init(8) running, so simply reboot */ 220 kern_reboot(RB_NOSYNC); 221 } 222 return; 223 } 224 static int waittime = -1; 225 226 static void 227 print_uptime(void) 228 { 229 int f; 230 struct timespec ts; 231 232 getnanouptime(&ts); 233 printf("Uptime: "); 234 f = 0; 235 if (ts.tv_sec >= 86400) { 236 printf("%ldd", (long)ts.tv_sec / 86400); 237 ts.tv_sec %= 86400; 238 f = 1; 239 } 240 if (f || ts.tv_sec >= 3600) { 241 printf("%ldh", (long)ts.tv_sec / 3600); 242 ts.tv_sec %= 3600; 243 f = 1; 244 } 245 if (f || ts.tv_sec >= 60) { 246 printf("%ldm", (long)ts.tv_sec / 60); 247 ts.tv_sec %= 60; 248 f = 1; 249 } 250 printf("%lds\n", (long)ts.tv_sec); 251 } 252 253 int 254 doadump(boolean_t textdump) 255 { 256 boolean_t coredump; 257 258 if (dumping) 259 return (EBUSY); 260 if (dumper.dumper == NULL) 261 return (ENXIO); 262 263 savectx(&dumppcb); 264 dumptid = curthread->td_tid; 265 dumping++; 266 267 coredump = TRUE; 268 #ifdef DDB 269 if (textdump && textdump_pending) { 270 coredump = FALSE; 271 textdump_dumpsys(&dumper); 272 } 273 #endif 274 if (coredump) 275 dumpsys(&dumper); 276 277 dumping--; 278 return (0); 279 } 280 281 static int 282 isbufbusy(struct buf *bp) 283 { 284 if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 && 285 BUF_ISLOCKED(bp)) || 286 ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI)) 287 return (1); 288 return (0); 289 } 290 291 /* 292 * Shutdown the system cleanly to prepare for reboot, halt, or power off. 293 */ 294 void 295 kern_reboot(int howto) 296 { 297 static int first_buf_printf = 1; 298 299 #if defined(SMP) 300 /* 301 * Bind us to CPU 0 so that all shutdown code runs there. Some 302 * systems don't shutdown properly (i.e., ACPI power off) if we 303 * run on another processor. 304 */ 305 if (!SCHEDULER_STOPPED()) { 306 thread_lock(curthread); 307 sched_bind(curthread, 0); 308 thread_unlock(curthread); 309 KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0")); 310 } 311 #endif 312 /* We're in the process of rebooting. */ 313 rebooting = 1; 314 315 /* collect extra flags that shutdown_nice might have set */ 316 howto |= shutdown_howto; 317 318 /* We are out of the debugger now. */ 319 kdb_active = 0; 320 321 /* 322 * Do any callouts that should be done BEFORE syncing the filesystems. 323 */ 324 EVENTHANDLER_INVOKE(shutdown_pre_sync, howto); 325 326 /* 327 * Now sync filesystems 328 */ 329 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 330 register struct buf *bp; 331 int iter, nbusy, pbusy; 332 #ifndef PREEMPTION 333 int subiter; 334 #endif 335 336 waittime = 0; 337 338 #ifdef SW_WATCHDOG 339 wdog_kern_pat(WD_LASTVAL); 340 #endif 341 sys_sync(curthread, NULL); 342 343 /* 344 * With soft updates, some buffers that are 345 * written will be remarked as dirty until other 346 * buffers are written. 347 */ 348 for (iter = pbusy = 0; iter < 20; iter++) { 349 nbusy = 0; 350 for (bp = &buf[nbuf]; --bp >= buf; ) 351 if (isbufbusy(bp)) 352 nbusy++; 353 if (nbusy == 0) { 354 if (first_buf_printf) 355 printf("All buffers synced."); 356 break; 357 } 358 if (first_buf_printf) { 359 printf("Syncing disks, buffers remaining... "); 360 first_buf_printf = 0; 361 } 362 printf("%d ", nbusy); 363 if (nbusy < pbusy) 364 iter = 0; 365 pbusy = nbusy; 366 #ifdef SW_WATCHDOG 367 wdog_kern_pat(WD_LASTVAL); 368 #endif 369 sys_sync(curthread, NULL); 370 371 #ifdef PREEMPTION 372 /* 373 * Drop Giant and spin for a while to allow 374 * interrupt threads to run. 375 */ 376 DROP_GIANT(); 377 DELAY(50000 * iter); 378 PICKUP_GIANT(); 379 #else 380 /* 381 * Drop Giant and context switch several times to 382 * allow interrupt threads to run. 383 */ 384 DROP_GIANT(); 385 for (subiter = 0; subiter < 50 * iter; subiter++) { 386 thread_lock(curthread); 387 mi_switch(SW_VOL, NULL); 388 thread_unlock(curthread); 389 DELAY(1000); 390 } 391 PICKUP_GIANT(); 392 #endif 393 } 394 printf("\n"); 395 /* 396 * Count only busy local buffers to prevent forcing 397 * a fsck if we're just a client of a wedged NFS server 398 */ 399 nbusy = 0; 400 for (bp = &buf[nbuf]; --bp >= buf; ) { 401 if (isbufbusy(bp)) { 402 #if 0 403 /* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */ 404 if (bp->b_dev == NULL) { 405 TAILQ_REMOVE(&mountlist, 406 bp->b_vp->v_mount, mnt_list); 407 continue; 408 } 409 #endif 410 nbusy++; 411 if (show_busybufs > 0) { 412 printf( 413 "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:", 414 nbusy, bp, bp->b_vp, bp->b_flags, 415 (intmax_t)bp->b_blkno, 416 (intmax_t)bp->b_lblkno); 417 BUF_LOCKPRINTINFO(bp); 418 if (show_busybufs > 1) 419 vn_printf(bp->b_vp, 420 "vnode content: "); 421 } 422 } 423 } 424 if (nbusy) { 425 /* 426 * Failed to sync all blocks. Indicate this and don't 427 * unmount filesystems (thus forcing an fsck on reboot). 428 */ 429 printf("Giving up on %d buffers\n", nbusy); 430 DELAY(5000000); /* 5 seconds */ 431 } else { 432 if (!first_buf_printf) 433 printf("Final sync complete\n"); 434 /* 435 * Unmount filesystems 436 */ 437 if (panicstr == 0) 438 vfs_unmountall(); 439 } 440 swapoff_all(); 441 DELAY(100000); /* wait for console output to finish */ 442 } 443 444 print_uptime(); 445 446 cngrab(); 447 448 /* 449 * Ok, now do things that assume all filesystem activity has 450 * been completed. 451 */ 452 EVENTHANDLER_INVOKE(shutdown_post_sync, howto); 453 454 if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 455 doadump(TRUE); 456 457 /* Now that we're going to really halt the system... */ 458 EVENTHANDLER_INVOKE(shutdown_final, howto); 459 460 for(;;) ; /* safety against shutdown_reset not working */ 461 /* NOTREACHED */ 462 } 463 464 /* 465 * If the shutdown was a clean halt, behave accordingly. 466 */ 467 static void 468 shutdown_halt(void *junk, int howto) 469 { 470 471 if (howto & RB_HALT) { 472 printf("\n"); 473 printf("The operating system has halted.\n"); 474 printf("Please press any key to reboot.\n\n"); 475 switch (cngetc()) { 476 case -1: /* No console, just die */ 477 cpu_halt(); 478 /* NOTREACHED */ 479 default: 480 howto &= ~RB_HALT; 481 break; 482 } 483 } 484 } 485 486 /* 487 * Check to see if the system paniced, pause and then reboot 488 * according to the specified delay. 489 */ 490 static void 491 shutdown_panic(void *junk, int howto) 492 { 493 int loop; 494 495 if (howto & RB_DUMP) { 496 if (PANIC_REBOOT_WAIT_TIME != 0) { 497 if (PANIC_REBOOT_WAIT_TIME != -1) { 498 printf("Automatic reboot in %d seconds - " 499 "press a key on the console to abort\n", 500 PANIC_REBOOT_WAIT_TIME); 501 for (loop = PANIC_REBOOT_WAIT_TIME * 10; 502 loop > 0; --loop) { 503 DELAY(1000 * 100); /* 1/10th second */ 504 /* Did user type a key? */ 505 if (cncheckc() != -1) 506 break; 507 } 508 if (!loop) 509 return; 510 } 511 } else { /* zero time specified - reboot NOW */ 512 return; 513 } 514 printf("--> Press a key on the console to reboot,\n"); 515 printf("--> or switch off the system now.\n"); 516 cngetc(); 517 } 518 } 519 520 /* 521 * Everything done, now reset 522 */ 523 static void 524 shutdown_reset(void *junk, int howto) 525 { 526 527 printf("Rebooting...\n"); 528 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 529 530 /* 531 * Acquiring smp_ipi_mtx here has a double effect: 532 * - it disables interrupts avoiding CPU0 preemption 533 * by fast handlers (thus deadlocking against other CPUs) 534 * - it avoids deadlocks against smp_rendezvous() or, more 535 * generally, threads busy-waiting, with this spinlock held, 536 * and waiting for responses by threads on other CPUs 537 * (ie. smp_tlb_shootdown()). 538 * 539 * For the !SMP case it just needs to handle the former problem. 540 */ 541 #ifdef SMP 542 mtx_lock_spin(&smp_ipi_mtx); 543 #else 544 spinlock_enter(); 545 #endif 546 547 /* cpu_boot(howto); */ /* doesn't do anything at the moment */ 548 cpu_reset(); 549 /* NOTREACHED */ /* assuming reset worked */ 550 } 551 552 /* 553 * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 554 * and then reboots. If we are called twice, then we avoid trying to sync 555 * the disks as this often leads to recursive panics. 556 */ 557 void 558 panic(const char *fmt, ...) 559 { 560 #ifdef SMP 561 static volatile u_int panic_cpu = NOCPU; 562 cpuset_t other_cpus; 563 #endif 564 struct thread *td = curthread; 565 int bootopt, newpanic; 566 va_list ap; 567 static char buf[256]; 568 569 if (stop_scheduler_on_panic) 570 spinlock_enter(); 571 else 572 critical_enter(); 573 574 #ifdef SMP 575 /* 576 * We don't want multiple CPU's to panic at the same time, so we 577 * use panic_cpu as a simple spinlock. We have to keep checking 578 * panic_cpu if we are spinning in case the panic on the first 579 * CPU is canceled. 580 */ 581 if (panic_cpu != PCPU_GET(cpuid)) 582 while (atomic_cmpset_int(&panic_cpu, NOCPU, 583 PCPU_GET(cpuid)) == 0) 584 while (panic_cpu != NOCPU) 585 ; /* nothing */ 586 587 if (stop_scheduler_on_panic) { 588 if (panicstr == NULL && !kdb_active) { 589 other_cpus = all_cpus; 590 CPU_CLR(PCPU_GET(cpuid), &other_cpus); 591 stop_cpus_hard(other_cpus); 592 } 593 594 /* 595 * We set stop_scheduler here and not in the block above, 596 * because we want to ensure that if panic has been called and 597 * stop_scheduler_on_panic is true, then stop_scheduler will 598 * always be set. Even if panic has been entered from kdb. 599 */ 600 stop_scheduler = 1; 601 } 602 #endif 603 604 bootopt = RB_AUTOBOOT; 605 newpanic = 0; 606 if (panicstr) 607 bootopt |= RB_NOSYNC; 608 else { 609 bootopt |= RB_DUMP; 610 panicstr = fmt; 611 newpanic = 1; 612 } 613 614 va_start(ap, fmt); 615 if (newpanic) { 616 (void)vsnprintf(buf, sizeof(buf), fmt, ap); 617 panicstr = buf; 618 cngrab(); 619 printf("panic: %s\n", buf); 620 } else { 621 printf("panic: "); 622 vprintf(fmt, ap); 623 printf("\n"); 624 } 625 va_end(ap); 626 #ifdef SMP 627 printf("cpuid = %d\n", PCPU_GET(cpuid)); 628 #endif 629 630 #ifdef KDB 631 if (newpanic && trace_on_panic) 632 kdb_backtrace(); 633 if (debugger_on_panic) 634 kdb_enter(KDB_WHY_PANIC, "panic"); 635 #endif 636 /*thread_lock(td); */ 637 td->td_flags |= TDF_INPANIC; 638 /* thread_unlock(td); */ 639 if (!sync_on_panic) 640 bootopt |= RB_NOSYNC; 641 if (!stop_scheduler_on_panic) 642 critical_exit(); 643 kern_reboot(bootopt); 644 } 645 646 /* 647 * Support for poweroff delay. 648 * 649 * Please note that setting this delay too short might power off your machine 650 * before the write cache on your hard disk has been flushed, leading to 651 * soft-updates inconsistencies. 652 */ 653 #ifndef POWEROFF_DELAY 654 # define POWEROFF_DELAY 5000 655 #endif 656 static int poweroff_delay = POWEROFF_DELAY; 657 658 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW, 659 &poweroff_delay, 0, "Delay before poweroff to write disk caches (msec)"); 660 661 static void 662 poweroff_wait(void *junk, int howto) 663 { 664 665 if (!(howto & RB_POWEROFF) || poweroff_delay <= 0) 666 return; 667 DELAY(poweroff_delay * 1000); 668 } 669 670 /* 671 * Some system processes (e.g. syncer) need to be stopped at appropriate 672 * points in their main loops prior to a system shutdown, so that they 673 * won't interfere with the shutdown process (e.g. by holding a disk buf 674 * to cause sync to fail). For each of these system processes, register 675 * shutdown_kproc() as a handler for one of shutdown events. 676 */ 677 static int kproc_shutdown_wait = 60; 678 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW, 679 &kproc_shutdown_wait, 0, "Max wait time (sec) to stop for each process"); 680 681 void 682 kproc_shutdown(void *arg, int howto) 683 { 684 struct proc *p; 685 int error; 686 687 if (panicstr) 688 return; 689 690 p = (struct proc *)arg; 691 printf("Waiting (max %d seconds) for system process `%s' to stop...", 692 kproc_shutdown_wait, p->p_comm); 693 error = kproc_suspend(p, kproc_shutdown_wait * hz); 694 695 if (error == EWOULDBLOCK) 696 printf("timed out\n"); 697 else 698 printf("done\n"); 699 } 700 701 void 702 kthread_shutdown(void *arg, int howto) 703 { 704 struct thread *td; 705 int error; 706 707 if (panicstr) 708 return; 709 710 td = (struct thread *)arg; 711 printf("Waiting (max %d seconds) for system thread `%s' to stop...", 712 kproc_shutdown_wait, td->td_name); 713 error = kthread_suspend(td, kproc_shutdown_wait * hz); 714 715 if (error == EWOULDBLOCK) 716 printf("timed out\n"); 717 else 718 printf("done\n"); 719 } 720 721 /* Registration of dumpers */ 722 int 723 set_dumper(struct dumperinfo *di) 724 { 725 726 if (di == NULL) { 727 bzero(&dumper, sizeof dumper); 728 return (0); 729 } 730 if (dumper.dumper != NULL) 731 return (EBUSY); 732 dumper = *di; 733 return (0); 734 } 735 736 /* Call dumper with bounds checking. */ 737 int 738 dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical, 739 off_t offset, size_t length) 740 { 741 742 if (length != 0 && (offset < di->mediaoffset || 743 offset - di->mediaoffset + length > di->mediasize)) { 744 printf("Attempt to write outside dump device boundaries.\n" 745 "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n", 746 (intmax_t)offset, (intmax_t)di->mediaoffset, 747 (uintmax_t)length, (intmax_t)di->mediasize); 748 return (ENOSPC); 749 } 750 return (di->dumper(di->priv, virtual, physical, offset, length)); 751 } 752 753 void 754 mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver, 755 uint64_t dumplen, uint32_t blksz) 756 { 757 758 bzero(kdh, sizeof(*kdh)); 759 strncpy(kdh->magic, magic, sizeof(kdh->magic)); 760 strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture)); 761 kdh->version = htod32(KERNELDUMPVERSION); 762 kdh->architectureversion = htod32(archver); 763 kdh->dumplength = htod64(dumplen); 764 kdh->dumptime = htod64(time_second); 765 kdh->blocksize = htod32(blksz); 766 strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname)); 767 strncpy(kdh->versionstring, version, sizeof(kdh->versionstring)); 768 if (panicstr != NULL) 769 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring)); 770 kdh->parity = kerneldump_parity(kdh); 771 } 772