1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 
  29 /**
  30  * Package-private utility class containing data structures and logic
  31  * governing the virtual-machine shutdown sequence.
  32  *
  33  * @author   Mark Reinhold
  34  * @since    1.3
  35  */
  36 
  37 class Shutdown {
  38 
  39     /* Shutdown state */
  40     private static final int RUNNING = 0;
  41     private static final int HOOKS = 1;
  42     private static final int COMPLETED = 2;
  43     private static int state = RUNNING;
  44 
  45     // The system shutdown hooks are registered with a predefined slot.
  46     // The list of shutdown hooks is as follows:
  47     // (0) Console restore hook
  48     // (1) Application hooks
  49     // (2) DeleteOnExit hook
  50     private static final int MAX_SYSTEM_HOOKS = 10;
  51     private static final Runnable[] hooks = new Runnable[MAX_SYSTEM_HOOKS];
  52 
  53     // the index of the currently running shutdown hook to the hooks array
  54     private static int currentRunningHook = 0;
  55 
  56     /* The preceding static fields are protected by this lock */
  57     private static class Lock { };
  58     private static Object lock = new Lock();
  59 
  60     /* Lock object for the native halt method */
  61     private static Object haltLock = new Lock();
  62 
  63     /**
  64      * Add a new shutdown hook.  Checks the shutdown state and the hook itself,
  65      * but does not do any security checks.
  66      *
  67      * The registerShutdownInProgress parameter should be false except
  68      * registering the DeleteOnExitHook since the first file may
  69      * be added to the delete on exit list by the application shutdown
  70      * hooks.
  71      *
  72      * @params slot  the slot in the shutdown hook array, whose element
  73      *               will be invoked in order during shutdown
  74      * @params registerShutdownInProgress true to allow the hook
  75      *               to be registered even if the shutdown is in progress.
  76      * @params hook  the hook to be registered
  77      *
  78      * @throws IllegalStateException
  79      *         if registerShutdownInProgress is false and shutdown is in progress; or
  80      *         if registerShutdownInProgress is true and the shutdown process
  81      *         already passes the given slot
  82      */
  83     static void add(int slot, boolean registerShutdownInProgress, Runnable hook) {
  84         synchronized (lock) {
  85             if (hooks[slot] != null)
  86                 throw new InternalError("Shutdown hook at slot " + slot + " already registered");
  87 
  88             if (!registerShutdownInProgress) {
  89                 if (state > RUNNING)
  90                     throw new IllegalStateException("Shutdown in progress");
  91             } else {
  92                 if (state > HOOKS || (state == HOOKS && slot <= currentRunningHook))
  93                     throw new IllegalStateException("Shutdown in progress");
  94             }
  95 
  96             hooks[slot] = hook;
  97         }
  98     }
  99 
 100     /* Run all registered shutdown hooks
 101      */
 102     private static void runHooks() {
 103         synchronized (lock) {
 104             /* Guard against the possibility of a daemon thread invoking exit
 105              * after DestroyJavaVM initiates the shutdown sequence
 106              */
 107             if (state != HOOKS) return;
 108         }
 109 
 110         for (int i=0; i < MAX_SYSTEM_HOOKS; i++) {
 111             try {
 112                 Runnable hook;
 113                 synchronized (lock) {
 114                     // acquire the lock to make sure the hook registered during
 115                     // shutdown is visible here.
 116                     currentRunningHook = i;
 117                     hook = hooks[i];
 118                 }
 119                 if (hook != null) hook.run();
 120             } catch(Throwable t) {
 121                 if (t instanceof ThreadDeath) {
 122                     ThreadDeath td = (ThreadDeath)t;
 123                     throw td;
 124                 }
 125             }
 126         }
 127 
 128         synchronized (lock) {
 129             state = COMPLETED;
 130         }
 131     }
 132 
 133     /* The halt method is synchronized on the halt lock
 134      * to avoid corruption of the delete-on-shutdown file list.
 135      * It invokes the true native halt method.
 136      */
 137     static void halt(int status) {
 138         synchronized (haltLock) {
 139             halt0(status);
 140         }
 141     }
 142 
 143     static native void halt0(int status);
 144 
 145     /* Invoked by Runtime.exit, which does all the security checks.
 146      * Also invoked by handlers for system-provided termination events,
 147      * which should pass a nonzero status code.
 148      */
 149     static void exit(int status) {
 150         synchronized (lock) {
 151             switch (state) {
 152             case RUNNING:       /* Initiate shutdown */
 153                 state = HOOKS;
 154                 break;
 155             case HOOKS:         /* Stall and halt */
 156                 break;
 157             case COMPLETED:
 158                 if (status != 0) {
 159                     /* Halt immediately on nonzero status */
 160                     halt(status);
 161                 }
 162                 break;
 163             }
 164         }
 165         synchronized (Shutdown.class) {
 166             /* Synchronize on the class object, causing any other thread
 167              * that attempts to initiate shutdown to stall indefinitely
 168              */
 169             runHooks();
 170             halt(status);
 171         }
 172     }
 173 
 174 
 175     /* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon
 176      * thread has finished.  Unlike the exit method, this method does not
 177      * actually halt the VM.
 178      */
 179     static void shutdown() {
 180         synchronized (lock) {
 181             switch (state) {
 182             case RUNNING:       /* Initiate shutdown */
 183                 state = HOOKS;
 184                 break;
 185             case HOOKS:         /* Stall and then return */
 186                 break;
 187             case COMPLETED:
 188                 return;
 189             }
 190         }
 191         synchronized (Shutdown.class) {
 192             runHooks();
 193         }
 194     }
 195 
 196 }