1 /* 2 * Copyright (c) 1998, 2011, 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 sun.misc; 27 import jdk.internal.misc.NativeSignal; 28 29 import java.util.Map; 30 import java.util.concurrent.ConcurrentHashMap; 31 import java.util.function.BiConsumer; 32 33 /** 34 * This class provides ANSI/ISO C signal support. A Java program can register 35 * signal handlers for the current process. There are two restrictions: 36 * <ul> 37 * <li> 38 * Java code cannot register a handler for signals that are already used 39 * by the Java VM implementation. The <code>Signal.handle</code> 40 * function raises an <code>IllegalArgumentException</code> if such an attempt 41 * is made. 42 * <li> 43 * When <code>Signal.handle</code> is called, the VM internally registers a 44 * special C signal handler. There is no way to force the Java signal handler 45 * to run synchronously before the C signal handler returns. Instead, when the 46 * VM receives a signal, the special C signal handler creates a new thread 47 * (at priority <code>Thread.MAX_PRIORITY</code>) to 48 * run the registered Java signal handler. The C signal handler immediately 49 * returns. Note that because the Java signal handler runs in a newly created 50 * thread, it may not actually be executed until some time after the C signal 51 * handler returns. 52 * </ul> 53 * <p> 54 * Signal objects are created based on their names. For example: 55 * <blockquote><pre> 56 * new Signal("INT"); 57 * </pre></blockquote> 58 * constructs a signal object corresponding to <code>SIGINT</code>, which is 59 * typically produced when the user presses <code>Ctrl-C</code> at the command line. 60 * The <code>Signal</code> constructor throws <code>IllegalArgumentException</code> 61 * when it is passed an unknown signal. 62 * <p> 63 * This is an example of how Java code handles <code>SIGINT</code>: 64 * <blockquote><pre> 65 * SignalHandler handler = new SignalHandler () { 66 * public void handle(Signal sig) { 67 * ... // handle SIGINT 68 * } 69 * }; 70 * Signal.handle(new Signal("INT"), handler); 71 * </pre></blockquote> 72 * 73 * @author Sheng Liang 74 * @author Bill Shannon 75 * @see sun.misc.SignalHandler 76 * @since 1.2 77 */ 78 public final class Signal { 79 80 private static final Map<Integer, UtHandler> handlers = new ConcurrentHashMap<>(4); 81 82 private final java.util.Signal utSignal; 83 84 /* Returns the signal number */ 85 public int getNumber() { 86 return utSignal.number(); 87 } 88 89 /** 90 * Returns the signal name. 91 * 92 * @return the name of the signal. 93 * @see sun.misc.Signal#Signal(String name) 94 */ 95 public String getName() { 96 return utSignal.name(); 97 } 98 99 /** 100 * Compares the equality of two <code>Signal</code> objects. 101 * 102 * @param other the object to compare with. 103 * @return whether two <code>Signal</code> objects are equal. 104 */ 105 public boolean equals(Object other) { 106 if (this == other) { 107 return true; 108 } 109 if (other == null || !(other instanceof Signal)) { 110 return false; 111 } 112 return utSignal == ((Signal) other).utSignal; 113 } 114 115 /** 116 * Returns a hashcode for this Signal. 117 * 118 * @return a hash code value for this object. 119 */ 120 public int hashCode() { 121 return getNumber(); 122 } 123 124 /** 125 * Returns a string representation of this signal. For example, "SIGINT" 126 * for an object constructed using <code>new Signal ("INT")</code>. 127 * 128 * @return a string representation of the signal 129 */ 130 public String toString() { 131 return getName(); 132 } 133 134 /** 135 * Constructs a signal from its name. 136 * 137 * @param name the name of the signal. 138 * @exception IllegalArgumentException unknown signal 139 * @see sun.misc.Signal#getName() 140 */ 141 public Signal(String name) { 142 this(java.util.Signal.of("SIG" + name)); 143 } 144 145 private Signal(java.util.Signal utSignal) { 146 this.utSignal = utSignal; 147 } 148 149 /** 150 * Registers a signal handler. 151 * 152 * @param sig a signal 153 * @param handler the handler to be registered with the given signal. 154 * @return the old handler 155 * @exception IllegalArgumentException the signal is in use by the VM 156 * @see sun.misc.Signal#raise(Signal sig) 157 * @see sun.misc.SignalHandler 158 * @see sun.misc.SignalHandler#SIG_DFL 159 * @see sun.misc.SignalHandler#SIG_IGN 160 */ 161 public static synchronized SignalHandler handle(Signal sig, 162 SignalHandler handler) 163 throws IllegalArgumentException { 164 165 UtHandler newUtHandler = UtHandler.create(handler); 166 UtHandler oldUtHandler = handlers.put(sig.getNumber(), newUtHandler); 167 168 if (oldUtHandler != null) { 169 if (!oldUtHandler.unregisterFrom(sig.utSignal)) { 170 throw new IllegalStateException( 171 "Can't unregister old sun.misc.SignalHandler as some new " + 172 "handler has been registered with java.util.Signal API."); 173 } 174 } 175 176 newUtHandler.registerTo(sig.utSignal); 177 178 return (oldUtHandler == null) ? null : oldUtHandler.signalHandler; 179 } 180 181 /** 182 * Raises a signal in the current process. 183 * 184 * @param sig a signal 185 * @see sun.misc.Signal#handle(Signal sig, SignalHandler handler) 186 */ 187 public static void raise(Signal sig) throws IllegalArgumentException { 188 if (handlers.get(sig.getNumber()) == null) { 189 throw new IllegalArgumentException("Unhandled signal: " + sig); 190 } 191 sig.utSignal.raise(); 192 } 193 194 // abstraction of Java and Native handlers for java.util.Signal API 195 // with delegation to sun.misc.[SignalHandler|NativeSignalHandler] 196 private static abstract class UtHandler { 197 final SignalHandler signalHandler; 198 199 static UtHandler create(SignalHandler signalHandler) { 200 return (signalHandler instanceof NativeSignalHandler) 201 ? new UtHandler.Native((NativeSignalHandler) signalHandler) 202 : new UtHandler.Java(signalHandler); 203 } 204 205 UtHandler(SignalHandler signalHandler) { 206 this.signalHandler = signalHandler; 207 } 208 209 abstract void registerTo(java.util.Signal utSignal); 210 211 abstract boolean unregisterFrom(java.util.Signal utSignal); 212 213 private static class Java extends UtHandler 214 implements BiConsumer<java.util.Signal, Runnable> { 215 Java(SignalHandler signalHandler) { 216 super(signalHandler); 217 } 218 219 @Override 220 public void accept(java.util.Signal uSignal, Runnable runnable) { 221 signalHandler.handle(new Signal(uSignal)); 222 } 223 224 @Override 225 void registerTo(java.util.Signal utSignal) { 226 utSignal.register(this); 227 } 228 229 @Override 230 boolean unregisterFrom(java.util.Signal utSignal) { 231 return utSignal.unregister(this); 232 } 233 } 234 235 private static class Native extends UtHandler { 236 Native(NativeSignalHandler signalHandler) { 237 super(signalHandler); 238 } 239 240 @Override 241 void registerTo(java.util.Signal utSignal) { 242 ((NativeSignal) utSignal).registerNative( 243 ((NativeSignalHandler) signalHandler).getHandler() 244 ); 245 } 246 247 @Override 248 boolean unregisterFrom(java.util.Signal utSignal) { 249 return ((NativeSignal) utSignal).unregisterNative( 250 ((NativeSignalHandler) signalHandler).getHandler() 251 ); 252 } 253 } 254 } 255 }