The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_shutdown.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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: releng/8.3/sys/kern/kern_shutdown.c 225728 2011-09-22 11:07:11Z attilio $");
   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, CTLFLAG_RW | CTLFLAG_TUN,
  106         &debugger_on_panic, 0, "Run debugger on kernel panic");
  107 TUNABLE_INT("debug.debugger_on_panic", &debugger_on_panic);
  108 
  109 #ifdef KDB_TRACE
  110 static int trace_on_panic = 1;
  111 #else
  112 static int trace_on_panic = 0;
  113 #endif
  114 SYSCTL_INT(_debug, OID_AUTO, trace_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
  115         &trace_on_panic, 0, "Print stack trace on kernel panic");
  116 TUNABLE_INT("debug.trace_on_panic", &trace_on_panic);
  117 #endif /* KDB */
  118 
  119 static int sync_on_panic = 0;
  120 SYSCTL_INT(_kern, OID_AUTO, sync_on_panic, CTLFLAG_RW | CTLFLAG_TUN,
  121         &sync_on_panic, 0, "Do a sync before rebooting from a panic");
  122 TUNABLE_INT("kern.sync_on_panic", &sync_on_panic);
  123 
  124 SYSCTL_NODE(_kern, OID_AUTO, shutdown, CTLFLAG_RW, 0, "Shutdown environment");
  125 
  126 #ifndef DIAGNOSTIC
  127 static int show_busybufs;
  128 #else
  129 static int show_busybufs = 1;
  130 #endif
  131 SYSCTL_INT(_kern_shutdown, OID_AUTO, show_busybufs, CTLFLAG_RW,
  132         &show_busybufs, 0, "");
  133 
  134 /*
  135  * Variable panicstr contains argument to first call to panic; used as flag
  136  * to indicate that the kernel has already called panic.
  137  */
  138 const char *panicstr;
  139 
  140 int dumping;                            /* system is dumping */
  141 int rebooting;                          /* system is rebooting */
  142 static struct dumperinfo dumper;        /* our selected dumper */
  143 
  144 /* Context information for dump-debuggers. */
  145 static struct pcb dumppcb;              /* Registers. */
  146 static lwpid_t dumptid;                 /* Thread ID. */
  147 
  148 static void boot(int) __dead2;
  149 static void poweroff_wait(void *, int);
  150 static void shutdown_halt(void *junk, int howto);
  151 static void shutdown_panic(void *junk, int howto);
  152 static void shutdown_reset(void *junk, int howto);
  153 
  154 /* register various local shutdown events */
  155 static void
  156 shutdown_conf(void *unused)
  157 {
  158 
  159         EVENTHANDLER_REGISTER(shutdown_final, poweroff_wait, NULL,
  160             SHUTDOWN_PRI_FIRST);
  161         EVENTHANDLER_REGISTER(shutdown_final, shutdown_halt, NULL,
  162             SHUTDOWN_PRI_LAST + 100);
  163         EVENTHANDLER_REGISTER(shutdown_final, shutdown_panic, NULL,
  164             SHUTDOWN_PRI_LAST + 100);
  165         EVENTHANDLER_REGISTER(shutdown_final, shutdown_reset, NULL,
  166             SHUTDOWN_PRI_LAST + 200);
  167 }
  168 
  169 SYSINIT(shutdown_conf, SI_SUB_INTRINSIC, SI_ORDER_ANY, shutdown_conf, NULL);
  170 
  171 /*
  172  * The system call that results in a reboot.
  173  */
  174 /* ARGSUSED */
  175 int
  176 reboot(struct thread *td, struct reboot_args *uap)
  177 {
  178         int error;
  179 
  180         error = 0;
  181 #ifdef MAC
  182         error = mac_system_check_reboot(td->td_ucred, uap->opt);
  183 #endif
  184         if (error == 0)
  185                 error = priv_check(td, PRIV_REBOOT);
  186         if (error == 0) {
  187                 mtx_lock(&Giant);
  188                 boot(uap->opt);
  189                 mtx_unlock(&Giant);
  190         }
  191         return (error);
  192 }
  193 
  194 /*
  195  * Called by events that want to shut down.. e.g  <CTL><ALT><DEL> on a PC
  196  */
  197 static int shutdown_howto = 0;
  198 
  199 void
  200 shutdown_nice(int howto)
  201 {
  202 
  203         shutdown_howto = howto;
  204 
  205         /* Send a signal to init(8) and have it shutdown the world */
  206         if (initproc != NULL) {
  207                 PROC_LOCK(initproc);
  208                 psignal(initproc, SIGINT);
  209                 PROC_UNLOCK(initproc);
  210         } else {
  211                 /* No init(8) running, so simply reboot */
  212                 boot(RB_NOSYNC);
  213         }
  214         return;
  215 }
  216 static int      waittime = -1;
  217 
  218 static void
  219 print_uptime(void)
  220 {
  221         int f;
  222         struct timespec ts;
  223 
  224         getnanouptime(&ts);
  225         printf("Uptime: ");
  226         f = 0;
  227         if (ts.tv_sec >= 86400) {
  228                 printf("%ldd", (long)ts.tv_sec / 86400);
  229                 ts.tv_sec %= 86400;
  230                 f = 1;
  231         }
  232         if (f || ts.tv_sec >= 3600) {
  233                 printf("%ldh", (long)ts.tv_sec / 3600);
  234                 ts.tv_sec %= 3600;
  235                 f = 1;
  236         }
  237         if (f || ts.tv_sec >= 60) {
  238                 printf("%ldm", (long)ts.tv_sec / 60);
  239                 ts.tv_sec %= 60;
  240                 f = 1;
  241         }
  242         printf("%lds\n", (long)ts.tv_sec);
  243 }
  244 
  245 static void
  246 doadump(void)
  247 {
  248 
  249         /*
  250          * Sometimes people have to call this from the kernel debugger. 
  251          * (if 'panic' can not dump)
  252          * Give them a clue as to why they can't dump.
  253          */
  254         if (dumper.dumper == NULL) {
  255                 printf("Cannot dump. Device not defined or unavailable.\n");
  256                 return;
  257         }
  258 
  259         savectx(&dumppcb);
  260         dumptid = curthread->td_tid;
  261         dumping++;
  262 #ifdef DDB
  263         if (textdump_pending)
  264                 textdump_dumpsys(&dumper);
  265         else
  266 #endif
  267                 dumpsys(&dumper);
  268         dumping--;
  269 }
  270 
  271 static int
  272 isbufbusy(struct buf *bp)
  273 {
  274         if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
  275             BUF_ISLOCKED(bp)) ||
  276             ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
  277                 return (1);
  278         return (0);
  279 }
  280 
  281 /*
  282  * Shutdown the system cleanly to prepare for reboot, halt, or power off.
  283  */
  284 static void
  285 boot(int howto)
  286 {
  287         static int first_buf_printf = 1;
  288 
  289 #if defined(SMP)
  290         /*
  291          * Bind us to CPU 0 so that all shutdown code runs there.  Some
  292          * systems don't shutdown properly (i.e., ACPI power off) if we
  293          * run on another processor.
  294          */
  295         thread_lock(curthread);
  296         sched_bind(curthread, 0);
  297         thread_unlock(curthread);
  298         KASSERT(PCPU_GET(cpuid) == 0, ("boot: not running on cpu 0"));
  299 #endif
  300         /* We're in the process of rebooting. */
  301         rebooting = 1;
  302 
  303         /* collect extra flags that shutdown_nice might have set */
  304         howto |= shutdown_howto;
  305 
  306         /* We are out of the debugger now. */
  307         kdb_active = 0;
  308 
  309         /*
  310          * Do any callouts that should be done BEFORE syncing the filesystems.
  311          */
  312         EVENTHANDLER_INVOKE(shutdown_pre_sync, howto);
  313 
  314         /* 
  315          * Now sync filesystems
  316          */
  317         if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) {
  318                 register struct buf *bp;
  319                 int iter, nbusy, pbusy;
  320 #ifndef PREEMPTION
  321                 int subiter;
  322 #endif
  323 
  324                 waittime = 0;
  325 
  326 #ifdef SW_WATCHDOG
  327                 wdog_kern_pat(WD_LASTVAL);
  328 #endif
  329                 sync(curthread, NULL);
  330 
  331                 /*
  332                  * With soft updates, some buffers that are
  333                  * written will be remarked as dirty until other
  334                  * buffers are written.
  335                  */
  336                 for (iter = pbusy = 0; iter < 20; iter++) {
  337                         nbusy = 0;
  338                         for (bp = &buf[nbuf]; --bp >= buf; )
  339                                 if (isbufbusy(bp))
  340                                         nbusy++;
  341                         if (nbusy == 0) {
  342                                 if (first_buf_printf)
  343                                         printf("All buffers synced.");
  344                                 break;
  345                         }
  346                         if (first_buf_printf) {
  347                                 printf("Syncing disks, buffers remaining... ");
  348                                 first_buf_printf = 0;
  349                         }
  350                         printf("%d ", nbusy);
  351                         if (nbusy < pbusy)
  352                                 iter = 0;
  353                         pbusy = nbusy;
  354 #ifdef SW_WATCHDOG
  355                         wdog_kern_pat(WD_LASTVAL);
  356 #endif
  357                         sync(curthread, NULL);
  358 
  359 #ifdef PREEMPTION
  360                         /*
  361                          * Drop Giant and spin for a while to allow
  362                          * interrupt threads to run.
  363                          */
  364                         DROP_GIANT();
  365                         DELAY(50000 * iter);
  366                         PICKUP_GIANT();
  367 #else
  368                         /*
  369                          * Drop Giant and context switch several times to
  370                          * allow interrupt threads to run.
  371                          */
  372                         DROP_GIANT();
  373                         for (subiter = 0; subiter < 50 * iter; subiter++) {
  374                                 thread_lock(curthread);
  375                                 mi_switch(SW_VOL, NULL);
  376                                 thread_unlock(curthread);
  377                                 DELAY(1000);
  378                         }
  379                         PICKUP_GIANT();
  380 #endif
  381                 }
  382                 printf("\n");
  383                 /*
  384                  * Count only busy local buffers to prevent forcing 
  385                  * a fsck if we're just a client of a wedged NFS server
  386                  */
  387                 nbusy = 0;
  388                 for (bp = &buf[nbuf]; --bp >= buf; ) {
  389                         if (isbufbusy(bp)) {
  390 #if 0
  391 /* XXX: This is bogus.  We should probably have a BO_REMOTE flag instead */
  392                                 if (bp->b_dev == NULL) {
  393                                         TAILQ_REMOVE(&mountlist,
  394                                             bp->b_vp->v_mount, mnt_list);
  395                                         continue;
  396                                 }
  397 #endif
  398                                 nbusy++;
  399                                 if (show_busybufs > 0) {
  400                                         printf(
  401             "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
  402                                             nbusy, bp, bp->b_vp, bp->b_flags,
  403                                             (intmax_t)bp->b_blkno,
  404                                             (intmax_t)bp->b_lblkno);
  405                                         BUF_LOCKPRINTINFO(bp);
  406                                         if (show_busybufs > 1)
  407                                                 vn_printf(bp->b_vp,
  408                                                     "vnode content: ");
  409                                 }
  410                         }
  411                 }
  412                 if (nbusy) {
  413                         /*
  414                          * Failed to sync all blocks. Indicate this and don't
  415                          * unmount filesystems (thus forcing an fsck on reboot).
  416                          */
  417                         printf("Giving up on %d buffers\n", nbusy);
  418                         DELAY(5000000); /* 5 seconds */
  419                 } else {
  420                         if (!first_buf_printf)
  421                                 printf("Final sync complete\n");
  422                         /*
  423                          * Unmount filesystems
  424                          */
  425                         if (panicstr == 0)
  426                                 vfs_unmountall();
  427                 }
  428                 swapoff_all();
  429                 DELAY(100000);          /* wait for console output to finish */
  430         }
  431 
  432         print_uptime();
  433 
  434         /*
  435          * Ok, now do things that assume all filesystem activity has
  436          * been completed.
  437          */
  438         EVENTHANDLER_INVOKE(shutdown_post_sync, howto);
  439 
  440         if ((howto & (RB_HALT|RB_DUMP)) == RB_DUMP && !cold && !dumping) 
  441                 doadump();
  442 
  443         /* Now that we're going to really halt the system... */
  444         EVENTHANDLER_INVOKE(shutdown_final, howto);
  445 
  446         for(;;) ;       /* safety against shutdown_reset not working */
  447         /* NOTREACHED */
  448 }
  449 
  450 /*
  451  * If the shutdown was a clean halt, behave accordingly.
  452  */
  453 static void
  454 shutdown_halt(void *junk, int howto)
  455 {
  456 
  457         if (howto & RB_HALT) {
  458                 printf("\n");
  459                 printf("The operating system has halted.\n");
  460                 printf("Please press any key to reboot.\n\n");
  461                 switch (cngetc()) {
  462                 case -1:                /* No console, just die */
  463                         cpu_halt();
  464                         /* NOTREACHED */
  465                 default:
  466                         howto &= ~RB_HALT;
  467                         break;
  468                 }
  469         }
  470 }
  471 
  472 /*
  473  * Check to see if the system paniced, pause and then reboot
  474  * according to the specified delay.
  475  */
  476 static void
  477 shutdown_panic(void *junk, int howto)
  478 {
  479         int loop;
  480 
  481         if (howto & RB_DUMP) {
  482                 if (PANIC_REBOOT_WAIT_TIME != 0) {
  483                         if (PANIC_REBOOT_WAIT_TIME != -1) {
  484                                 printf("Automatic reboot in %d seconds - "
  485                                        "press a key on the console to abort\n",
  486                                         PANIC_REBOOT_WAIT_TIME);
  487                                 for (loop = PANIC_REBOOT_WAIT_TIME * 10;
  488                                      loop > 0; --loop) {
  489                                         DELAY(1000 * 100); /* 1/10th second */
  490                                         /* Did user type a key? */
  491                                         if (cncheckc() != -1)
  492                                                 break;
  493                                 }
  494                                 if (!loop)
  495                                         return;
  496                         }
  497                 } else { /* zero time specified - reboot NOW */
  498                         return;
  499                 }
  500                 printf("--> Press a key on the console to reboot,\n");
  501                 printf("--> or switch off the system now.\n");
  502                 cngetc();
  503         }
  504 }
  505 
  506 /*
  507  * Everything done, now reset
  508  */
  509 static void
  510 shutdown_reset(void *junk, int howto)
  511 {
  512 
  513         printf("Rebooting...\n");
  514         DELAY(1000000); /* wait 1 sec for printf's to complete and be read */
  515 
  516         /*
  517          * Acquiring smp_ipi_mtx here has a double effect:
  518          * - it disables interrupts avoiding CPU0 preemption
  519          *   by fast handlers (thus deadlocking  against other CPUs)
  520          * - it avoids deadlocks against smp_rendezvous() or, more 
  521          *   generally, threads busy-waiting, with this spinlock held,
  522          *   and waiting for responses by threads on other CPUs
  523          *   (ie. smp_tlb_shootdown()).
  524          *
  525          * For the !SMP case it just needs to handle the former problem.
  526          */
  527 #ifdef SMP
  528         mtx_lock_spin(&smp_ipi_mtx);
  529 #else
  530         spinlock_enter();
  531 #endif
  532 
  533         /* cpu_boot(howto); */ /* doesn't do anything at the moment */
  534         cpu_reset();
  535         /* NOTREACHED */ /* assuming reset worked */
  536 }
  537 
  538 /*
  539  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
  540  * and then reboots.  If we are called twice, then we avoid trying to sync
  541  * the disks as this often leads to recursive panics.
  542  */
  543 void
  544 panic(const char *fmt, ...)
  545 {
  546 #ifdef SMP
  547         static volatile u_int panic_cpu = NOCPU;
  548 #endif
  549         struct thread *td = curthread;
  550         int bootopt, newpanic;
  551         va_list ap;
  552         static char buf[256];
  553 
  554         critical_enter();
  555 #ifdef SMP
  556         /*
  557          * We don't want multiple CPU's to panic at the same time, so we
  558          * use panic_cpu as a simple spinlock.  We have to keep checking
  559          * panic_cpu if we are spinning in case the panic on the first
  560          * CPU is canceled.
  561          */
  562         if (panic_cpu != PCPU_GET(cpuid))
  563                 while (atomic_cmpset_int(&panic_cpu, NOCPU,
  564                     PCPU_GET(cpuid)) == 0)
  565                         while (panic_cpu != NOCPU)
  566                                 ; /* nothing */
  567 #endif
  568 
  569         bootopt = RB_AUTOBOOT | RB_DUMP;
  570         newpanic = 0;
  571         if (panicstr)
  572                 bootopt |= RB_NOSYNC;
  573         else {
  574                 panicstr = fmt;
  575                 newpanic = 1;
  576         }
  577 
  578         va_start(ap, fmt);
  579         if (newpanic) {
  580                 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
  581                 panicstr = buf;
  582                 printf("panic: %s\n", buf);
  583         } else {
  584                 printf("panic: ");
  585                 vprintf(fmt, ap);
  586                 printf("\n");
  587         }
  588         va_end(ap);
  589 #ifdef SMP
  590         printf("cpuid = %d\n", PCPU_GET(cpuid));
  591 #endif
  592 
  593 #ifdef KDB
  594         if (newpanic && trace_on_panic)
  595                 kdb_backtrace();
  596         if (debugger_on_panic)
  597                 kdb_enter(KDB_WHY_PANIC, "panic");
  598 #ifdef RESTARTABLE_PANICS
  599         /* See if the user aborted the panic, in which case we continue. */
  600         if (panicstr == NULL) {
  601 #ifdef SMP
  602                 atomic_store_rel_int(&panic_cpu, NOCPU);
  603 #endif
  604                 return;
  605         }
  606 #endif
  607 #endif
  608         /*thread_lock(td); */
  609         td->td_flags |= TDF_INPANIC;
  610         /* thread_unlock(td); */
  611         if (!sync_on_panic)
  612                 bootopt |= RB_NOSYNC;
  613         critical_exit();
  614         boot(bootopt);
  615 }
  616 
  617 /*
  618  * Support for poweroff delay.
  619  */
  620 #ifndef POWEROFF_DELAY
  621 # define POWEROFF_DELAY 5000
  622 #endif
  623 static int poweroff_delay = POWEROFF_DELAY;
  624 
  625 SYSCTL_INT(_kern_shutdown, OID_AUTO, poweroff_delay, CTLFLAG_RW,
  626         &poweroff_delay, 0, "");
  627 
  628 static void
  629 poweroff_wait(void *junk, int howto)
  630 {
  631 
  632         if (!(howto & RB_POWEROFF) || poweroff_delay <= 0)
  633                 return;
  634         DELAY(poweroff_delay * 1000);
  635 }
  636 
  637 /*
  638  * Some system processes (e.g. syncer) need to be stopped at appropriate
  639  * points in their main loops prior to a system shutdown, so that they
  640  * won't interfere with the shutdown process (e.g. by holding a disk buf
  641  * to cause sync to fail).  For each of these system processes, register
  642  * shutdown_kproc() as a handler for one of shutdown events.
  643  */
  644 static int kproc_shutdown_wait = 60;
  645 SYSCTL_INT(_kern_shutdown, OID_AUTO, kproc_shutdown_wait, CTLFLAG_RW,
  646     &kproc_shutdown_wait, 0, "");
  647 
  648 void
  649 kproc_shutdown(void *arg, int howto)
  650 {
  651         struct proc *p;
  652         char procname[MAXCOMLEN + 1];
  653         int error;
  654 
  655         if (panicstr)
  656                 return;
  657 
  658         p = (struct proc *)arg;
  659         strlcpy(procname, p->p_comm, sizeof(procname));
  660         printf("Waiting (max %d seconds) for system process `%s' to stop...",
  661             kproc_shutdown_wait, procname);
  662         error = kproc_suspend(p, kproc_shutdown_wait * hz);
  663 
  664         if (error == EWOULDBLOCK)
  665                 printf("timed out\n");
  666         else
  667                 printf("done\n");
  668 }
  669 
  670 void
  671 kthread_shutdown(void *arg, int howto)
  672 {
  673         struct thread *td;
  674         char procname[MAXCOMLEN + 1];
  675         int error;
  676 
  677         if (panicstr)
  678                 return;
  679 
  680         td = (struct thread *)arg;
  681         strlcpy(procname, td->td_name, sizeof(procname));
  682         printf("Waiting (max %d seconds) for system thread `%s' to stop...",
  683             kproc_shutdown_wait, procname);
  684         error = kthread_suspend(td, kproc_shutdown_wait * hz);
  685 
  686         if (error == EWOULDBLOCK)
  687                 printf("timed out\n");
  688         else
  689                 printf("done\n");
  690 }
  691 
  692 /* Registration of dumpers */
  693 int
  694 set_dumper(struct dumperinfo *di)
  695 {
  696 
  697         if (di == NULL) {
  698                 bzero(&dumper, sizeof dumper);
  699                 return (0);
  700         }
  701         if (dumper.dumper != NULL)
  702                 return (EBUSY);
  703         dumper = *di;
  704         return (0);
  705 }
  706 
  707 /* Call dumper with bounds checking. */
  708 int
  709 dump_write(struct dumperinfo *di, void *virtual, vm_offset_t physical,
  710     off_t offset, size_t length)
  711 {
  712 
  713         if (length != 0 && (offset < di->mediaoffset ||
  714             offset - di->mediaoffset + length > di->mediasize)) {
  715                 printf("Attempt to write outside dump device boundaries.\n"
  716             "offset(%jd), mediaoffset(%jd), length(%ju), mediasize(%jd).\n",
  717                     (intmax_t)offset, (intmax_t)di->mediaoffset,
  718                     (uintmax_t)length, (intmax_t)di->mediasize);
  719                 return (ENOSPC);
  720         }
  721         return (di->dumper(di->priv, virtual, physical, offset, length));
  722 }
  723 
  724 void
  725 mkdumpheader(struct kerneldumpheader *kdh, char *magic, uint32_t archver,
  726     uint64_t dumplen, uint32_t blksz)
  727 {
  728 
  729         bzero(kdh, sizeof(*kdh));
  730         strncpy(kdh->magic, magic, sizeof(kdh->magic));
  731         strncpy(kdh->architecture, MACHINE_ARCH, sizeof(kdh->architecture));
  732         kdh->version = htod32(KERNELDUMPVERSION);
  733         kdh->architectureversion = htod32(archver);
  734         kdh->dumplength = htod64(dumplen);
  735         kdh->dumptime = htod64(time_second);
  736         kdh->blocksize = htod32(blksz);
  737         strncpy(kdh->hostname, prison0.pr_hostname, sizeof(kdh->hostname));
  738         strncpy(kdh->versionstring, version, sizeof(kdh->versionstring));
  739         if (panicstr != NULL)
  740                 strncpy(kdh->panicstring, panicstr, sizeof(kdh->panicstring));
  741         kdh->parity = kerneldump_parity(kdh);
  742 }

Cache object: f50fab8d09f475ed308c9cace6b834f0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.