File: | jdk/src/java.base/unix/native/libjsig/jsig.c |
Warning: | line 155, column 7 Value stored to 'sigblocked' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* |
2 | * Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | * Copyright (c) 2012, 2015 SAP SE. All rights reserved. |
4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5 | * |
6 | * This code is free software; you can redistribute it and/or modify it |
7 | * under the terms of the GNU General Public License version 2 only, as |
8 | * published by the Free Software Foundation. Oracle designates this |
9 | * particular file as subject to the "Classpath" exception as provided |
10 | * by Oracle in the LICENSE file that accompanied this code. |
11 | * |
12 | * This code is distributed in the hope that it will be useful, but WITHOUT |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
15 | * version 2 for more details (a copy is included in the LICENSE file that |
16 | * accompanied this code). |
17 | * |
18 | * You should have received a copy of the GNU General Public License version |
19 | * 2 along with this work; if not, write to the Free Software Foundation, |
20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
21 | * |
22 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
23 | * or visit www.oracle.com if you need additional information or have any |
24 | * questions. |
25 | * |
26 | */ |
27 | |
28 | /* This is a special library that should be loaded before libc & |
29 | * libthread to interpose the signal handler installation functions: |
30 | * sigaction(), signal(), sigset(). |
31 | * Used for signal-chaining. See RFE 4381843. |
32 | * Use of signal() and sigset() is now deprecated as these old API's should |
33 | * not be used - sigaction is the only truly supported API. |
34 | */ |
35 | |
36 | #include "jni.h" |
37 | |
38 | #include <dlfcn.h> |
39 | #include <errno(*__errno_location ()).h> |
40 | #include <pthread.h> |
41 | #include <signal.h> |
42 | #include <stdio.h> |
43 | #include <stdlib.h> |
44 | #include <string.h> |
45 | |
46 | #if (__STDC_VERSION__199901L >= 199901L) |
47 | #include <stdbool.h> |
48 | #else |
49 | #define bool_Bool int |
50 | #define true1 1 |
51 | #define false0 0 |
52 | #endif |
53 | |
54 | #define MAX_SIGNALS(64 + 1) NSIG(64 + 1) |
55 | |
56 | static struct sigaction sact[MAX_SIGNALS(64 + 1)]; /* saved signal handlers */ |
57 | |
58 | static sigset_t jvmsigs; /* Signals used by jvm. */ |
59 | |
60 | #ifdef MACOSX |
61 | static __thread bool_Bool reentry = false0; /* prevent reentry deadlock (per-thread) */ |
62 | #endif |
63 | |
64 | /* Used to synchronize the installation of signal handlers. */ |
65 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, 0, 0, 0, { 0, 0 } } }; |
66 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER{ { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } }; |
67 | static pthread_t tid; |
68 | |
69 | typedef void (*sa_handler_t)(int); |
70 | typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); |
71 | typedef sa_handler_t (*signal_function_t)(int, sa_handler_t); |
72 | typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *); |
73 | |
74 | static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */ |
75 | static sigaction_t os_sigaction = 0; /* os's version of sigaction() */ |
76 | |
77 | static bool_Bool jvm_signal_installing = false0; |
78 | static bool_Bool jvm_signal_installed = false0; |
79 | |
80 | |
81 | static void signal_lock() { |
82 | pthread_mutex_lock(&mutex); |
83 | /* When the jvm is installing its set of signal handlers, threads |
84 | * other than the jvm thread should wait. */ |
85 | if (jvm_signal_installing) { |
86 | /* tid is not initialized until jvm_signal_installing is set to true. */ |
87 | if (pthread_equal(tid, pthread_self()) == 0) { |
88 | do { |
89 | pthread_cond_wait(&cond, &mutex); |
90 | } while (jvm_signal_installing); |
91 | } |
92 | } |
93 | } |
94 | |
95 | static void signal_unlock() { |
96 | pthread_mutex_unlock(&mutex); |
97 | } |
98 | |
99 | static sa_handler_t call_os_signal(int sig, sa_handler_t disp, |
100 | bool_Bool is_sigset) { |
101 | sa_handler_t res; |
102 | |
103 | if (os_signal == NULL((void*)0)) { |
104 | // Deprecation warning first time through |
105 | printf(HOTSPOT_VM_DISTRO " VM warning: the use of signal() and sigset() "__printf_chk (2 - 1, "OpenJDK" " VM warning: the use of signal() and sigset() " "for signal chaining was deprecated in version 16.0 and will " "be removed in a future release. Use sigaction() instead.\n" ) |
106 | "for signal chaining was deprecated in version 16.0 and will "__printf_chk (2 - 1, "OpenJDK" " VM warning: the use of signal() and sigset() " "for signal chaining was deprecated in version 16.0 and will " "be removed in a future release. Use sigaction() instead.\n" ) |
107 | "be removed in a future release. Use sigaction() instead.\n")__printf_chk (2 - 1, "OpenJDK" " VM warning: the use of signal() and sigset() " "for signal chaining was deprecated in version 16.0 and will " "be removed in a future release. Use sigaction() instead.\n" ); |
108 | if (!is_sigset) { |
109 | os_signal = (signal_function_t)dlsym(RTLD_NEXT((void *) -1l), "signal"); |
110 | } else { |
111 | os_signal = (signal_function_t)dlsym(RTLD_NEXT((void *) -1l), "sigset"); |
112 | } |
113 | if (os_signal == NULL((void*)0)) { |
114 | printf("%s\n", dlerror())__printf_chk (2 - 1, "%s\n", dlerror()); |
115 | exit(0); |
116 | } |
117 | } |
118 | |
119 | #ifdef MACOSX |
120 | /* On macosx, the OS implementation of signal calls sigaction. |
121 | * Make sure we do not deadlock with ourself. (See JDK-8072147). */ |
122 | reentry = true1; |
123 | #endif |
124 | |
125 | res = (*os_signal)(sig, disp); |
126 | |
127 | #ifdef MACOSX |
128 | reentry = false0; |
129 | #endif |
130 | |
131 | return res; |
132 | } |
133 | |
134 | static void save_signal_handler(int sig, sa_handler_t disp, bool_Bool is_sigset) { |
135 | sigset_t set; |
136 | |
137 | sact[sig].sa_handler__sigaction_handler.sa_handler = disp; |
138 | sigemptyset(&set); |
139 | sact[sig].sa_mask = set; |
140 | sact[sig].sa_flags = 0; |
141 | } |
142 | |
143 | static sa_handler_t set_signal(int sig, sa_handler_t disp, bool_Bool is_sigset) { |
144 | sa_handler_t oldhandler; |
145 | bool_Bool sigused; |
146 | bool_Bool sigblocked; |
147 | |
148 | signal_lock(); |
149 | |
150 | sigused = sigismember(&jvmsigs, sig); |
151 | if (jvm_signal_installed && sigused) { |
152 | /* jvm has installed its signal handler for this signal. */ |
153 | /* Save the handler. Don't really install it. */ |
154 | if (is_sigset) { |
155 | sigblocked = sigismember(&(sact[sig].sa_mask), sig); |
Value stored to 'sigblocked' is never read | |
156 | } |
157 | oldhandler = sact[sig].sa_handler__sigaction_handler.sa_handler; |
158 | save_signal_handler(sig, disp, is_sigset); |
159 | |
160 | signal_unlock(); |
161 | return oldhandler; |
162 | } else if (jvm_signal_installing) { |
163 | /* jvm is installing its signal handlers. Install the new |
164 | * handlers and save the old ones. jvm uses sigaction(). |
165 | * Leave the piece here just in case. */ |
166 | oldhandler = call_os_signal(sig, disp, is_sigset); |
167 | save_signal_handler(sig, oldhandler, is_sigset); |
168 | |
169 | /* Record the signals used by jvm */ |
170 | sigaddset(&jvmsigs, sig); |
171 | |
172 | signal_unlock(); |
173 | return oldhandler; |
174 | } else { |
175 | /* jvm has no relation with this signal (yet). Install the |
176 | * the handler. */ |
177 | oldhandler = call_os_signal(sig, disp, is_sigset); |
178 | |
179 | signal_unlock(); |
180 | return oldhandler; |
181 | } |
182 | } |
183 | |
184 | JNIEXPORT__attribute__((visibility("default"))) sa_handler_t signal(int sig, sa_handler_t disp) { |
185 | if (sig < 0 || sig >= MAX_SIGNALS(64 + 1)) { |
186 | errno(*__errno_location ()) = EINVAL22; |
187 | return SIG_ERR((__sighandler_t) -1); |
188 | } |
189 | |
190 | return set_signal(sig, disp, false0); |
191 | } |
192 | |
193 | JNIEXPORT__attribute__((visibility("default"))) sa_handler_t sigset(int sig, sa_handler_t disp) { |
194 | #ifdef _ALLBSD_SOURCE |
195 | printf("sigset() is not supported by BSD")__printf_chk (2 - 1, "sigset() is not supported by BSD"); |
196 | exit(0); |
197 | #else |
198 | if (sig < 0 || sig >= MAX_SIGNALS(64 + 1)) { |
199 | errno(*__errno_location ()) = EINVAL22; |
200 | return (sa_handler_t)-1; |
201 | } |
202 | |
203 | return set_signal(sig, disp, true1); |
204 | #endif |
205 | } |
206 | |
207 | static int call_os_sigaction(int sig, const struct sigaction *act, |
208 | struct sigaction *oact) { |
209 | if (os_sigaction == NULL((void*)0)) { |
210 | os_sigaction = (sigaction_t)dlsym(RTLD_NEXT((void *) -1l), "sigaction"); |
211 | if (os_sigaction == NULL((void*)0)) { |
212 | printf("%s\n", dlerror())__printf_chk (2 - 1, "%s\n", dlerror()); |
213 | exit(0); |
214 | } |
215 | } |
216 | return (*os_sigaction)(sig, act, oact); |
217 | } |
218 | |
219 | JNIEXPORT__attribute__((visibility("default"))) int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { |
220 | int res; |
221 | bool_Bool sigused; |
222 | struct sigaction oldAct; |
223 | |
224 | if (sig < 0 || sig >= MAX_SIGNALS(64 + 1)) { |
225 | errno(*__errno_location ()) = EINVAL22; |
226 | return -1; |
227 | } |
228 | |
229 | #ifdef MACOSX |
230 | if (reentry) { |
231 | return call_os_sigaction(sig, act, oact); |
232 | } |
233 | #endif |
234 | |
235 | signal_lock(); |
236 | |
237 | sigused = sigismember(&jvmsigs, sig); |
238 | if (jvm_signal_installed && sigused) { |
239 | /* jvm has installed its signal handler for this signal. */ |
240 | /* Save the handler. Don't really install it. */ |
241 | if (oact != NULL((void*)0)) { |
242 | *oact = sact[sig]; |
243 | } |
244 | if (act != NULL((void*)0)) { |
245 | sact[sig] = *act; |
246 | } |
247 | |
248 | signal_unlock(); |
249 | return 0; |
250 | } else if (jvm_signal_installing) { |
251 | /* jvm is installing its signal handlers. Install the new |
252 | * handlers and save the old ones. */ |
253 | res = call_os_sigaction(sig, act, &oldAct); |
254 | sact[sig] = oldAct; |
255 | if (oact != NULL((void*)0)) { |
256 | *oact = oldAct; |
257 | } |
258 | |
259 | /* Record the signals used by jvm. */ |
260 | sigaddset(&jvmsigs, sig); |
261 | |
262 | signal_unlock(); |
263 | return res; |
264 | } else { |
265 | /* jvm has no relation with this signal (yet). Install the |
266 | * the handler. */ |
267 | res = call_os_sigaction(sig, act, oact); |
268 | |
269 | signal_unlock(); |
270 | return res; |
271 | } |
272 | } |
273 | |
274 | /* The three functions for the jvm to call into. */ |
275 | JNIEXPORT__attribute__((visibility("default"))) void JVM_begin_signal_setting() { |
276 | signal_lock(); |
277 | sigemptyset(&jvmsigs); |
278 | jvm_signal_installing = true1; |
279 | tid = pthread_self(); |
280 | signal_unlock(); |
281 | } |
282 | |
283 | JNIEXPORT__attribute__((visibility("default"))) void JVM_end_signal_setting() { |
284 | signal_lock(); |
285 | jvm_signal_installed = true1; |
286 | jvm_signal_installing = false0; |
287 | pthread_cond_broadcast(&cond); |
288 | signal_unlock(); |
289 | } |
290 | |
291 | JNIEXPORT__attribute__((visibility("default"))) struct sigaction *JVM_get_signal_action(int sig) { |
292 | /* Does race condition make sense here? */ |
293 | if (sigismember(&jvmsigs, sig)) { |
294 | return &sact[sig]; |
295 | } |
296 | return NULL((void*)0); |
297 | } |