author | mdoerr |
Thu, 17 May 2018 14:19:54 +0200 | |
changeset 50162 | f9fe56417050 |
parent 48875 | c1e7612f6b11 |
child 58744 | c9c3bb79861e |
permissions | -rw-r--r-- |
36009 | 1 |
/* |
48875
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
36009 | 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 |
import java.util.ArrayList; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Objects; |
|
29 |
import java.util.concurrent.Semaphore; |
|
30 |
import java.util.concurrent.TimeUnit; |
|
31 |
||
32 |
import org.testng.Assert; |
|
33 |
import org.testng.TestNG; |
|
34 |
import org.testng.annotations.Test; |
|
35 |
import org.testng.annotations.BeforeSuite; |
|
36 |
import org.testng.annotations.DataProvider; |
|
37 |
||
38 |
import jdk.test.lib.Platform; |
|
39 |
import jdk.test.lib.Utils; |
|
40 |
||
41 |
import sun.misc.Signal; |
|
42 |
import sun.misc.SignalHandler; |
|
43 |
||
44 |
/* |
|
45 |
* @test |
|
40686
1b855fa5c9c5
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36970
diff
changeset
|
46 |
* @library /test/lib |
36966 | 47 |
* @modules jdk.unsupported |
36970
ae857479b5a8
8153928: test/lib/share/classes/jdk/test/lib/Utils.java introduced dependency to java.base/jdk.internal.misc
amlu
parents:
36966
diff
changeset
|
48 |
* java.base/jdk.internal.misc |
45466
faf1c55d2046
8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents:
40686
diff
changeset
|
49 |
* @build jdk.test.lib.Utils |
faf1c55d2046
8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents:
40686
diff
changeset
|
50 |
* jdk.test.lib.Asserts |
faf1c55d2046
8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents:
40686
diff
changeset
|
51 |
* jdk.test.lib.JDKToolFinder |
faf1c55d2046
8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents:
40686
diff
changeset
|
52 |
* jdk.test.lib.JDKToolLauncher |
faf1c55d2046
8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents:
40686
diff
changeset
|
53 |
* jdk.test.lib.Platform |
faf1c55d2046
8181759: add explicit @build actions for jdk.test.lib classes in all :tier1 tests
iignatyev
parents:
40686
diff
changeset
|
54 |
* jdk.test.lib.process.* |
36009 | 55 |
* @run testng/othervm -Xrs -DXrs=true SunMiscSignalTest |
56 |
* @run testng/othervm SunMiscSignalTest |
|
57 |
* @summary sun.misc.Signal test |
|
58 |
*/ |
|
59 |
||
60 |
@Test |
|
61 |
public class SunMiscSignalTest { |
|
62 |
||
63 |
// Set to true to enable additional debug output |
|
64 |
static boolean debug = true; |
|
65 |
||
66 |
// True to test while running with -Xrs |
|
67 |
static boolean RUNNING_WITH_Xrs = Boolean.getBoolean("Xrs"); |
|
68 |
||
69 |
/** |
|
70 |
* Print a debug message if enabled. |
|
71 |
* |
|
72 |
* @param format the format |
|
73 |
* @param args the arguments |
|
74 |
*/ |
|
75 |
static void printf(String format, Object... args) { |
|
76 |
if (debug) { |
|
77 |
System.out.printf(" " + format, args); |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
enum IsSupported {NO, YES} |
|
82 |
||
83 |
enum CanRegister {NO, YES} |
|
84 |
||
85 |
enum CanRaise {NO, YES} |
|
86 |
||
87 |
enum Invoked {NO, YES} |
|
88 |
||
89 |
enum RestrictedSignals {NORMAL, XRS} |
|
90 |
||
91 |
@BeforeSuite |
|
92 |
static void setup() { |
|
93 |
System.out.printf("-Xrs: %s%n", RUNNING_WITH_Xrs); |
|
94 |
} |
|
95 |
||
96 |
// Provider of signals to be tested with variations for -Xrs and |
|
97 |
// platform dependencies |
|
98 |
// -Xrs restricted signals signals the VM will not handle SIGINT, SIGTERM, SIGHUP and others |
|
99 |
@DataProvider(name = "supportedSignals") |
|
100 |
static Object[][] supportedSignals() { |
|
101 |
RestrictedSignals rs = RUNNING_WITH_Xrs ? RestrictedSignals.XRS : RestrictedSignals.NORMAL; |
|
102 |
CanRegister registerXrs = RUNNING_WITH_Xrs ? CanRegister.NO : CanRegister.YES; |
|
103 |
CanRaise raiseXrs = RUNNING_WITH_Xrs ? CanRaise.NO : CanRaise.YES; |
|
104 |
Invoked invokedXrs = RUNNING_WITH_Xrs ? Invoked.NO : Invoked.YES; |
|
105 |
||
106 |
Object[][] commonSignals = new Object[][]{ |
|
107 |
{"INT", IsSupported.YES, registerXrs, raiseXrs, invokedXrs}, |
|
108 |
{"TERM", IsSupported.YES, registerXrs, raiseXrs, invokedXrs}, |
|
109 |
{"ABRT", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
110 |
}; |
|
111 |
||
112 |
Object[][] posixSignals = { |
|
113 |
{"HUP", IsSupported.YES, registerXrs, raiseXrs, invokedXrs}, |
|
114 |
{"QUIT", IsSupported.YES, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
115 |
{"USR1", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
116 |
{"USR2", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
117 |
{"PIPE", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
118 |
{"ALRM", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
119 |
{"CHLD", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
120 |
{"CONT", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
121 |
{"TSTP", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
122 |
{"TTIN", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
123 |
{"TTOU", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
124 |
{"URG", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
125 |
{"XCPU", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
126 |
{"XFSZ", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
127 |
{"VTALRM", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
128 |
{"PROF", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
129 |
{"WINCH", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
130 |
{"IO", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
131 |
{"SYS", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
|
132 |
}; |
|
133 |
||
48875
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
134 |
Object[][] posixNonOSXSignals = { |
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
135 |
{"BUS", IsSupported.YES, CanRegister.YES, CanRaise.YES, invokedXrs}, |
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
136 |
}; |
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
137 |
|
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
138 |
Object[][] posixOSXSignals = { |
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
139 |
{"BUS", IsSupported.YES, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
140 |
}; |
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
141 |
|
36009 | 142 |
Object[][] windowsSignals = { |
143 |
{"HUP", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
144 |
{"QUIT", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
145 |
{"BUS", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
146 |
{"USR1", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
147 |
{"USR2", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
148 |
{"PIPE", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
149 |
{"ALRM", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
150 |
{"CHLD", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
151 |
{"CONT", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
152 |
{"TSTP", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
153 |
{"TTIN", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
154 |
{"TTOU", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
155 |
{"URG", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
156 |
{"XCPU", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
157 |
{"XFSZ", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
158 |
{"VTALRM", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
159 |
{"PROF", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
160 |
{"WINCH", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
161 |
{"IO", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
162 |
{"SYS", IsSupported.NO, CanRegister.NO, CanRaise.NO, Invoked.NO}, |
|
163 |
}; |
|
164 |
||
48875
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
165 |
Object[][] combinedPosixSignals = concatArrays(posixSignals, |
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
166 |
(Platform.isOSX() ? posixOSXSignals : posixNonOSXSignals)); |
c1e7612f6b11
8193308: Disallow installing user signal handlers for SIGBUS on OSX
rwestberg
parents:
47216
diff
changeset
|
167 |
return concatArrays(commonSignals, (Platform.isWindows() ? windowsSignals : combinedPosixSignals)); |
36009 | 168 |
} |
169 |
||
170 |
// Provider of invalid signal names |
|
171 |
@DataProvider(name = "invalidSunMiscSignalNames") |
|
172 |
Object[][] invalidSunMiscSignalNames() { |
|
173 |
return new Object[][]{ |
|
174 |
{""}, |
|
175 |
{"I"}, |
|
176 |
{"SIG"}, |
|
177 |
{"SIGabc"}, |
|
178 |
{"SIGINT"}, // prefix not allowed |
|
179 |
{"abc"}, |
|
180 |
}; |
|
181 |
} |
|
182 |
||
183 |
static Object[][] concatArrays(Object[][]... arrays) { |
|
184 |
int l = 0; |
|
185 |
for (Object[][] a : arrays) { |
|
186 |
l += a.length; |
|
187 |
} |
|
188 |
||
189 |
Object[][] newArray = new Object[l][]; |
|
190 |
l = 0; |
|
191 |
for (int i = 0; i < arrays.length; i++) { |
|
192 |
System.arraycopy(arrays[i], 0, newArray, l, arrays[i].length); |
|
193 |
l += arrays[i].length; |
|
194 |
} |
|
195 |
||
196 |
return newArray; |
|
197 |
} |
|
198 |
||
36117
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
199 |
// Return true if the signal is one of the shutdown signals known to the VM |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
200 |
private static boolean isShutdownSignal(Signal signal) { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
201 |
String name = signal.getName(); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
202 |
return name.equals("INT") || name.equals("HUP") || name.equals("TERM"); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
203 |
} |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
204 |
|
36009 | 205 |
/** |
206 |
* Quick verification of supported signals using sun.misc.Signal. |
|
207 |
* |
|
208 |
* @param name the signal name |
|
209 |
* @throws InterruptedException would be an error if thrown |
|
210 |
*/ |
|
211 |
@Test(dataProvider = "supportedSignals") |
|
212 |
static void testSunMisc(String name, IsSupported supported, CanRegister register, |
|
213 |
CanRaise raise, Invoked invoked) throws InterruptedException { |
|
214 |
Handler h = new Handler(); |
|
215 |
SignalHandler orig = null; |
|
216 |
Signal signal = null; |
|
217 |
try { |
|
218 |
signal = new Signal(name); |
|
219 |
Assert.assertEquals(supported, IsSupported.YES, "Unexpected support for " + name); |
|
220 |
||
221 |
Assert.assertEquals(signal.getName(), name, "getName() mismatch, "); |
|
222 |
||
223 |
Assert.assertEquals(signal.toString(), "SIG" + name, "toString() mismatch, "); |
|
224 |
||
225 |
try { |
|
36117
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
226 |
orig = Signal.handle(signal, h); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
227 |
printf("oldHandler: %s%n", orig); |
36009 | 228 |
Assert.assertEquals(CanRegister.YES, register, "Unexpected handle succeeded " + name); |
229 |
try { |
|
230 |
Signal.raise(signal); |
|
231 |
Assert.assertEquals(CanRaise.YES, raise, "Unexpected raise success for " + name); |
|
232 |
Invoked inv = h.semaphore().tryAcquire(Utils.adjustTimeout(100L), |
|
233 |
TimeUnit.MILLISECONDS) ? Invoked.YES : Invoked.NO; |
|
36117
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
234 |
if (!isShutdownSignal(signal)) { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
235 |
// Normal case |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
236 |
Assert.assertEquals(inv, invoked, "handler not invoked;"); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
237 |
} else { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
238 |
if (orig == SignalHandler.SIG_IGN) { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
239 |
Assert.assertEquals(inv, Invoked.NO, "handler should not be invoked"); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
240 |
} else { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
241 |
Assert.assertEquals(inv, invoked, "handler not invoked;"); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
242 |
} |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
243 |
} |
36009 | 244 |
} catch (IllegalArgumentException uoe3) { |
245 |
Assert.assertNotEquals(CanRaise.YES, raise, "raise failed for " + name + |
|
246 |
": " + uoe3.getMessage()); |
|
247 |
} |
|
248 |
} catch (IllegalArgumentException uoe2) { |
|
249 |
Assert.assertNotEquals(CanRegister.YES, register, "handle failed for: " + name + |
|
250 |
": " + uoe2.getMessage()); |
|
251 |
} |
|
252 |
} catch (IllegalArgumentException uoe) { |
|
253 |
Assert.assertNotEquals(IsSupported.YES, supported, "Support missing for " + name + |
|
254 |
": " + uoe.getMessage()); |
|
255 |
return; |
|
256 |
} finally { |
|
257 |
// Restore original signal handler |
|
258 |
if (orig != null && signal != null) { |
|
259 |
Signal.handle(signal, orig); |
|
260 |
} |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
// Test Signal is equal to itself and not equals to others |
|
265 |
@Test(dataProvider = "supportedSignals") |
|
266 |
static void testEquals(String name, IsSupported supported, CanRegister register, |
|
267 |
CanRaise raise, Invoked invoked) { |
|
268 |
Object[][] data = supportedSignals(); |
|
269 |
for (int i = 0; i < data.length; i++) { |
|
270 |
IsSupported otherSupported = (IsSupported) data[i][1]; |
|
271 |
if (supported == IsSupported.NO || otherSupported == IsSupported.NO) { |
|
272 |
continue; |
|
273 |
} |
|
274 |
String otherName = (String) data[i][0]; |
|
275 |
||
276 |
Signal sig1 = new Signal(name); |
|
277 |
Signal sig2 = new Signal(otherName); |
|
278 |
if (name.equals(otherName)) { |
|
279 |
Assert.assertEquals(sig1, sig2, "Equals failed; "); |
|
280 |
Assert.assertEquals(sig1.hashCode(), sig2.hashCode(), "HashCode wrong; "); |
|
281 |
} else { |
|
282 |
Assert.assertNotEquals(sig1, sig2, "NotEquals failed; "); |
|
283 |
Assert.assertNotEquals(sig1.hashCode(), sig2.hashCode(), "HashCode wrong; "); |
|
284 |
} |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
@Test(dataProvider = "invalidSunMiscSignalNames") |
|
289 |
static void testSunMiscIAE(String name) { |
|
290 |
try { |
|
291 |
new Signal(name); |
|
292 |
Assert.fail("Should have thrown IAE for signal: " + name); |
|
293 |
} catch (IllegalArgumentException iae) { |
|
294 |
Assert.assertEquals(iae.getMessage(), "Unknown signal: " + name, "getMessage() incorrect; "); |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
// Note: JDK 8 did not check/throw NPE, passing null resulted in a segv |
|
299 |
@Test(expectedExceptions = NullPointerException.class) |
|
300 |
static void nullSignal() { |
|
301 |
new Signal(null); |
|
302 |
} |
|
303 |
||
304 |
// Test expected exception when raising a signal when no handler defined |
|
36117
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
305 |
@Test |
36009 | 306 |
static void testRaiseNoConsumer() { |
307 |
Signal signal = new Signal("INT"); |
|
308 |
SignalHandler orig = null; |
|
309 |
try { |
|
36117
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
310 |
orig = Signal.handle(signal, SignalHandler.SIG_DFL); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
311 |
printf("oldHandler: %s%n", orig); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
312 |
if (orig == SignalHandler.SIG_IGN) { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
313 |
// SIG_IGN for TERM means it cannot be handled |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
314 |
return; |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
315 |
} |
36009 | 316 |
Signal.raise(signal); |
36117
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
317 |
Assert.fail("Should have thrown IllegalArgumentException"); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
318 |
} catch (IllegalArgumentException iae) { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
319 |
printf("IAE message: %s%n", iae.getMessage()); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
320 |
} finally { |
36009 | 321 |
// Restore original signal handler |
322 |
if (orig != null && signal != null) { |
|
323 |
Signal.handle(signal, orig); |
|
324 |
} |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
/** |
|
329 |
* The thread that runs the handler for sun.misc.Signal should be a |
|
330 |
* Daemon thread. |
|
331 |
*/ |
|
332 |
@Test |
|
333 |
static void isDaemonThread() throws InterruptedException { |
|
334 |
if (RUNNING_WITH_Xrs) { |
|
335 |
return; |
|
336 |
} |
|
337 |
Handler handler = new Handler(); |
|
338 |
Signal signal = new Signal("INT"); |
|
36117
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
339 |
SignalHandler orig = Signal.handle(signal, handler); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
340 |
printf("oldHandler: %s%n", orig); |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
341 |
if (orig == SignalHandler.SIG_IGN) { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
342 |
// SIG_IGN for INT means it cannot be handled |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
343 |
return; |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
344 |
} |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
345 |
|
36009 | 346 |
Signal.raise(signal); |
347 |
boolean handled = handler.semaphore() |
|
348 |
.tryAcquire(Utils.adjustTimeout(100L), TimeUnit.MILLISECONDS); |
|
36740
f80cd68bc7f4
8152005: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36117
diff
changeset
|
349 |
if (!handled) { |
f80cd68bc7f4
8152005: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36117
diff
changeset
|
350 |
// For debug try again |
f80cd68bc7f4
8152005: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36117
diff
changeset
|
351 |
printf("Second try to see signal"); |
f80cd68bc7f4
8152005: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36117
diff
changeset
|
352 |
handled = handler.semaphore() |
f80cd68bc7f4
8152005: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36117
diff
changeset
|
353 |
.tryAcquire(Utils.adjustTimeout(2L), TimeUnit.SECONDS); |
f80cd68bc7f4
8152005: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36117
diff
changeset
|
354 |
} |
36009 | 355 |
Assert.assertEquals(handled, !RUNNING_WITH_Xrs, |
356 |
"raising s.m.Signal did not get a callback;"); |
|
357 |
||
358 |
Assert.assertTrue(handler.wasDaemon(), "Thread.isDaemon running the handler; "); |
|
359 |
} |
|
360 |
||
361 |
// Check that trying to invoke SIG_DFL.handle throws UnsupportedOperationException. |
|
362 |
@Test(expectedExceptions = UnsupportedOperationException.class) |
|
363 |
static void cannotHandleSIGDFL() { |
|
364 |
Signal signal = new Signal("INT"); |
|
365 |
Assert.assertNotNull(SignalHandler.SIG_DFL, "SIG_DFL null; "); |
|
366 |
SignalHandler.SIG_DFL.handle(signal); |
|
367 |
} |
|
368 |
||
369 |
// Check that trying to invoke SIG_IGN.handle throws UnsupportedOperationException. |
|
370 |
@Test(expectedExceptions = UnsupportedOperationException.class) |
|
371 |
static void cannotHandleSIGIGN() { |
|
372 |
Signal signal = new Signal("INT"); |
|
373 |
Assert.assertNotNull(SignalHandler.SIG_IGN, "SIG_IGN null; "); |
|
374 |
SignalHandler.SIG_IGN.handle(signal); |
|
375 |
} |
|
376 |
||
377 |
// Check that setting a Signal handler returns the previous handler. |
|
378 |
@Test() |
|
379 |
static void checkLastHandler() { |
|
380 |
if (RUNNING_WITH_Xrs) { |
|
381 |
return; |
|
382 |
} |
|
383 |
Signal signal = new Signal("TERM"); |
|
384 |
Handler h1 = new Handler(); |
|
385 |
Handler h2 = new Handler(); |
|
386 |
SignalHandler orig = Signal.handle(signal, h1); |
|
36117
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
387 |
if (orig == SignalHandler.SIG_IGN) { |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
388 |
// SIG_IGN for TERM means it cannot be handled |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
389 |
return; |
f3e1df22686a
8150337: sun/misc/SunMiscSignalTest.java failed intermittently
rriggs
parents:
36009
diff
changeset
|
390 |
} |
36009 | 391 |
|
392 |
try { |
|
393 |
SignalHandler prev = Signal.handle(signal, h2); |
|
394 |
Assert.assertSame(prev, h1, "prev handler mismatch"); |
|
395 |
||
396 |
prev = Signal.handle(signal, h1); |
|
397 |
Assert.assertSame(prev, h2, "prev handler mismatch"); |
|
398 |
} finally { |
|
399 |
if (orig != null && signal != null) { |
|
400 |
Signal.handle(signal, orig); |
|
401 |
} |
|
402 |
} |
|
403 |
} |
|
404 |
||
405 |
/** |
|
406 |
* Test Handler, a SignalHandler for Signal notifications. |
|
407 |
* Signals a semaphore when invoked and records whether |
|
408 |
* the thread calling the Handler was a daemon. |
|
409 |
*/ |
|
410 |
static class Handler implements SignalHandler { |
|
411 |
// A semaphore to check for accept being called |
|
412 |
Semaphore sema = new Semaphore(0); |
|
413 |
||
414 |
Boolean wasDaemon = null; |
|
415 |
||
416 |
Semaphore semaphore() { |
|
417 |
return sema; |
|
418 |
} |
|
419 |
||
420 |
synchronized Boolean wasDaemon() { |
|
421 |
return wasDaemon; |
|
422 |
} |
|
423 |
||
424 |
/** |
|
425 |
* Releases the semaphore when called as SignalHandler.handle. |
|
426 |
* |
|
427 |
* @param signal the Signal that occurred |
|
428 |
*/ |
|
429 |
@Override |
|
430 |
public void handle(Signal signal) { |
|
431 |
synchronized (this) { |
|
432 |
wasDaemon = Thread.currentThread().isDaemon(); |
|
433 |
} |
|
434 |
sema.release(); |
|
435 |
printf("sun.misc.handle sig: %s, num: %d%n", signal.getName(), signal.getNumber()); |
|
436 |
} |
|
437 |
||
438 |
public String toString() { |
|
439 |
return "Handler: sem: " + sema.getQueueLength() + |
|
440 |
", wasDaemon: " + Objects.toString(wasDaemon()); |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
// Main can be used to run the tests from the command line with only testng.jar. |
|
445 |
@SuppressWarnings("raw_types") |
|
446 |
@Test(enabled = false) |
|
447 |
public static void main(String[] args) { |
|
448 |
Class<?>[] testclass = {SunMiscSignalTest.class}; |
|
449 |
TestNG testng = new TestNG(); |
|
450 |
testng.setTestClasses(testclass); |
|
451 |
testng.run(); |
|
452 |
} |
|
453 |
||
454 |
} |