1 /*
2 * Copyright (c) 2001, 2018, 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 */
33
34 #include <dlfcn.h>
35 #include <errno.h>
36 #include <pthread.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #if (__STDC_VERSION__ >= 199901L)
43 #include <stdbool.h>
44 #else
45 #define bool int
46 #define true 1
47 #define false 0
48 #endif
49
50 #ifdef SOLARIS
51 #define MAX_SIGNALS (SIGRTMAX+1)
52
53 /* On solaris, MAX_SIGNALS is a macro, not a constant, so we must allocate sact dynamically. */
54 static struct sigaction *sact = (struct sigaction *)NULL; /* saved signal handlers */
55 #else
56 #define MAX_SIGNALS NSIG
57
58 static struct sigaction sact[MAX_SIGNALS]; /* saved signal handlers */
59 #endif
60
61 static sigset_t jvmsigs; /* Signals used by jvm. */
62
63 #ifdef MACOSX
64 static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */
65 #endif
66
67 /* Used to synchronize the installation of signal handlers. */
68 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
70 static pthread_t tid = 0;
71
72 typedef void (*sa_handler_t)(int);
73 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
74 typedef sa_handler_t (*signal_function_t)(int, sa_handler_t);
75 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
76
77 static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */
78 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
79
80 static bool jvm_signal_installing = false;
81 static bool jvm_signal_installed = false;
82
83
84 /* assume called within signal_lock */
allocate_sact()85 static void allocate_sact() {
86 #ifdef SOLARIS
87 if (sact == NULL) {
88 sact = (struct sigaction *)malloc((MAX_SIGNALS) * (size_t)sizeof(struct sigaction));
89 if (sact == NULL) {
90 printf("%s\n", "libjsig.so unable to allocate memory");
91 exit(0);
92 }
93 memset(sact, 0, (MAX_SIGNALS) * (size_t)sizeof(struct sigaction));
94 }
95 #endif
96 }
97
signal_lock()98 static void signal_lock() {
99 pthread_mutex_lock(&mutex);
100 /* When the jvm is installing its set of signal handlers, threads
101 * other than the jvm thread should wait. */
102 if (jvm_signal_installing) {
103 if (tid != pthread_self()) {
104 pthread_cond_wait(&cond, &mutex);
105 }
106 }
107 }
108
signal_unlock()109 static void signal_unlock() {
110 pthread_mutex_unlock(&mutex);
111 }
112
call_os_signal(int sig,sa_handler_t disp,bool is_sigset)113 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
114 bool is_sigset) {
115 sa_handler_t res;
116
117 if (os_signal == NULL) {
118 if (!is_sigset) {
119 os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal");
120 } else {
121 os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset");
122 }
123 if (os_signal == NULL) {
124 printf("%s\n", dlerror());
125 exit(0);
126 }
127 }
128
129 #ifdef MACOSX
130 /* On macosx, the OS implementation of signal calls sigaction.
131 * Make sure we do not deadlock with ourself. (See JDK-8072147). */
132 reentry = true;
133 #endif
134
135 res = (*os_signal)(sig, disp);
136
137 #ifdef MACOSX
138 reentry = false;
139 #endif
140
141 return res;
142 }
143
save_signal_handler(int sig,sa_handler_t disp,bool is_sigset)144 static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
145 sigset_t set;
146
147 sact[sig].sa_handler = disp;
148 sigemptyset(&set);
149 sact[sig].sa_mask = set;
150 if (!is_sigset) {
151 #ifdef SOLARIS
152 sact[sig].sa_flags = SA_NODEFER;
153 if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
154 sact[sig].sa_flags |= SA_RESETHAND;
155 }
156 #else
157 sact[sig].sa_flags = 0;
158 #endif
159 } else {
160 sact[sig].sa_flags = 0;
161 }
162 }
163
set_signal(int sig,sa_handler_t disp,bool is_sigset)164 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
165 sa_handler_t oldhandler;
166 bool sigused;
167 bool sigblocked;
168
169 signal_lock();
170 allocate_sact();
171
172 sigused = sigismember(&jvmsigs, sig);
173 if (jvm_signal_installed && sigused) {
174 /* jvm has installed its signal handler for this signal. */
175 /* Save the handler. Don't really install it. */
176 if (is_sigset) {
177 sigblocked = sigismember(&(sact[sig].sa_mask), sig);
178 }
179 oldhandler = sact[sig].sa_handler;
180 save_signal_handler(sig, disp, is_sigset);
181
182 #ifdef SOLARIS
183 if (is_sigset && sigblocked) {
184 /* We won't honor the SIG_HOLD request to change the signal mask */
185 oldhandler = SIG_HOLD;
186 }
187 #endif
188
189 signal_unlock();
190 return oldhandler;
191 } else if (jvm_signal_installing) {
192 /* jvm is installing its signal handlers. Install the new
193 * handlers and save the old ones. jvm uses sigaction().
194 * Leave the piece here just in case. */
195 oldhandler = call_os_signal(sig, disp, is_sigset);
196 save_signal_handler(sig, oldhandler, is_sigset);
197
198 /* Record the signals used by jvm */
199 sigaddset(&jvmsigs, sig);
200
201 signal_unlock();
202 return oldhandler;
203 } else {
204 /* jvm has no relation with this signal (yet). Install the
205 * the handler. */
206 oldhandler = call_os_signal(sig, disp, is_sigset);
207
208 signal_unlock();
209 return oldhandler;
210 }
211 }
212
signal(int sig,sa_handler_t disp)213 sa_handler_t signal(int sig, sa_handler_t disp) {
214 if (sig < 0 || sig >= MAX_SIGNALS) {
215 errno = EINVAL;
216 return SIG_ERR;
217 }
218
219 return set_signal(sig, disp, false);
220 }
221
sigset(int sig,sa_handler_t disp)222 sa_handler_t sigset(int sig, sa_handler_t disp) {
223 #ifdef _ALLBSD_SOURCE
224 printf("sigset() is not supported by BSD");
225 exit(0);
226 #else
227 if (sig < 0 || sig >= MAX_SIGNALS) {
228 errno = EINVAL;
229 return (sa_handler_t)-1;
230 }
231
232 return set_signal(sig, disp, true);
233 #endif
234 }
235
call_os_sigaction(int sig,const struct sigaction * act,struct sigaction * oact)236 static int call_os_sigaction(int sig, const struct sigaction *act,
237 struct sigaction *oact) {
238 if (os_sigaction == NULL) {
239 os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
240 if (os_sigaction == NULL) {
241 printf("%s\n", dlerror());
242 exit(0);
243 }
244 }
245 return (*os_sigaction)(sig, act, oact);
246 }
247
sigaction(int sig,const struct sigaction * act,struct sigaction * oact)248 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
249 int res;
250 bool sigused;
251 struct sigaction oldAct;
252
253 if (sig < 0 || sig >= MAX_SIGNALS) {
254 errno = EINVAL;
255 return -1;
256 }
257
258 #ifdef MACOSX
259 if (reentry) {
260 return call_os_sigaction(sig, act, oact);
261 }
262 #endif
263
264 signal_lock();
265
266 allocate_sact();
267 sigused = sigismember(&jvmsigs, sig);
268 if (jvm_signal_installed && sigused) {
269 /* jvm has installed its signal handler for this signal. */
270 /* Save the handler. Don't really install it. */
271 if (oact != NULL) {
272 *oact = sact[sig];
273 }
274 if (act != NULL) {
275 sact[sig] = *act;
276 }
277
278 signal_unlock();
279 return 0;
280 } else if (jvm_signal_installing) {
281 /* jvm is installing its signal handlers. Install the new
282 * handlers and save the old ones. */
283 res = call_os_sigaction(sig, act, &oldAct);
284 sact[sig] = oldAct;
285 if (oact != NULL) {
286 *oact = oldAct;
287 }
288
289 /* Record the signals used by jvm. */
290 sigaddset(&jvmsigs, sig);
291
292 signal_unlock();
293 return res;
294 } else {
295 /* jvm has no relation with this signal (yet). Install the
296 * the handler. */
297 res = call_os_sigaction(sig, act, oact);
298
299 signal_unlock();
300 return res;
301 }
302 }
303
304 /* The three functions for the jvm to call into. */
JVM_begin_signal_setting()305 void JVM_begin_signal_setting() {
306 signal_lock();
307 sigemptyset(&jvmsigs);
308 jvm_signal_installing = true;
309 tid = pthread_self();
310 signal_unlock();
311 }
312
JVM_end_signal_setting()313 void JVM_end_signal_setting() {
314 signal_lock();
315 jvm_signal_installed = true;
316 jvm_signal_installing = false;
317 pthread_cond_broadcast(&cond);
318 signal_unlock();
319 }
320
JVM_get_signal_action(int sig)321 struct sigaction *JVM_get_signal_action(int sig) {
322 allocate_sact();
323 /* Does race condition make sense here? */
324 if (sigismember(&jvmsigs, sig)) {
325 return &sact[sig];
326 }
327 return NULL;
328 }
329