|
1 /* |
|
2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * Copyright 2012, 2013 SAP AG. 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. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 2 along with this work; if not, write to the Free Software Foundation, |
|
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 * |
|
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
21 * or visit www.oracle.com if you need additional information or have any |
|
22 * questions. |
|
23 * |
|
24 */ |
|
25 |
|
26 /* CopyrightVersion 1.2 */ |
|
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 <signal.h> |
|
35 #include <dlfcn.h> |
|
36 #include <pthread.h> |
|
37 #include <stdio.h> |
|
38 #include <stdlib.h> |
|
39 |
|
40 #define bool int |
|
41 #define true 1 |
|
42 #define false 0 |
|
43 |
|
44 // Highest so far on AIX 5.2 is SIGSAK (63) |
|
45 #define MAXSIGNUM 63 |
|
46 #define MASK(sig) ((unsigned int)1 << sig) |
|
47 |
|
48 static struct sigaction sact[MAXSIGNUM]; /* saved signal handlers */ |
|
49 static unsigned int jvmsigs = 0; /* signals used by jvm */ |
|
50 |
|
51 /* used to synchronize the installation of signal handlers */ |
|
52 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
|
53 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
|
54 static pthread_t tid = 0; |
|
55 |
|
56 typedef void (*sa_handler_t)(int); |
|
57 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); |
|
58 // signal_t is already defined on AIX |
|
59 typedef sa_handler_t (*signal_like_function_t)(int, sa_handler_t); |
|
60 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *); |
|
61 |
|
62 static signal_like_function_t os_signal = 0; /* os's version of signal()/sigset() */ |
|
63 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */ |
|
64 |
|
65 static bool jvm_signal_installing = false; |
|
66 static bool jvm_signal_installed = false; |
|
67 |
|
68 static void signal_lock() { |
|
69 pthread_mutex_lock(&mutex); |
|
70 /* When the jvm is installing its set of signal handlers, threads |
|
71 * other than the jvm thread should wait */ |
|
72 if (jvm_signal_installing) { |
|
73 if (tid != pthread_self()) { |
|
74 pthread_cond_wait(&cond, &mutex); |
|
75 } |
|
76 } |
|
77 } |
|
78 |
|
79 static void signal_unlock() { |
|
80 pthread_mutex_unlock(&mutex); |
|
81 } |
|
82 |
|
83 static sa_handler_t call_os_signal(int sig, sa_handler_t disp, |
|
84 bool is_sigset) { |
|
85 if (os_signal == NULL) { |
|
86 if (!is_sigset) { |
|
87 // Aix: call functions directly instead of dlsym'ing them |
|
88 os_signal = signal; |
|
89 } else { |
|
90 // Aix: call functions directly instead of dlsym'ing them |
|
91 os_signal = sigset; |
|
92 } |
|
93 if (os_signal == NULL) { |
|
94 printf("%s\n", dlerror()); |
|
95 exit(0); |
|
96 } |
|
97 } |
|
98 return (*os_signal)(sig, disp); |
|
99 } |
|
100 |
|
101 static void save_signal_handler(int sig, sa_handler_t disp) { |
|
102 sigset_t set; |
|
103 sact[sig].sa_handler = disp; |
|
104 sigemptyset(&set); |
|
105 sact[sig].sa_mask = set; |
|
106 sact[sig].sa_flags = 0; |
|
107 } |
|
108 |
|
109 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) { |
|
110 sa_handler_t oldhandler; |
|
111 bool sigused; |
|
112 |
|
113 signal_lock(); |
|
114 |
|
115 sigused = (MASK(sig) & jvmsigs) != 0; |
|
116 if (jvm_signal_installed && sigused) { |
|
117 /* jvm has installed its signal handler for this signal. */ |
|
118 /* Save the handler. Don't really install it. */ |
|
119 oldhandler = sact[sig].sa_handler; |
|
120 save_signal_handler(sig, disp); |
|
121 |
|
122 signal_unlock(); |
|
123 return oldhandler; |
|
124 } else if (jvm_signal_installing) { |
|
125 /* jvm is installing its signal handlers. Install the new |
|
126 * handlers and save the old ones. jvm uses sigaction(). |
|
127 * Leave the piece here just in case. */ |
|
128 oldhandler = call_os_signal(sig, disp, is_sigset); |
|
129 save_signal_handler(sig, oldhandler); |
|
130 |
|
131 /* Record the signals used by jvm */ |
|
132 jvmsigs |= MASK(sig); |
|
133 |
|
134 signal_unlock(); |
|
135 return oldhandler; |
|
136 } else { |
|
137 /* jvm has no relation with this signal (yet). Install the |
|
138 * the handler. */ |
|
139 oldhandler = call_os_signal(sig, disp, is_sigset); |
|
140 |
|
141 signal_unlock(); |
|
142 return oldhandler; |
|
143 } |
|
144 } |
|
145 |
|
146 sa_handler_t signal(int sig, sa_handler_t disp) { |
|
147 return set_signal(sig, disp, false); |
|
148 } |
|
149 |
|
150 sa_handler_t sigset(int sig, sa_handler_t disp) { |
|
151 return set_signal(sig, disp, true); |
|
152 } |
|
153 |
|
154 static int call_os_sigaction(int sig, const struct sigaction *act, |
|
155 struct sigaction *oact) { |
|
156 if (os_sigaction == NULL) { |
|
157 // Aix: call functions directly instead of dlsym'ing them |
|
158 os_sigaction = sigaction; |
|
159 if (os_sigaction == NULL) { |
|
160 printf("%s\n", dlerror()); |
|
161 exit(0); |
|
162 } |
|
163 } |
|
164 return (*os_sigaction)(sig, act, oact); |
|
165 } |
|
166 |
|
167 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { |
|
168 int res; |
|
169 bool sigused; |
|
170 struct sigaction oldAct; |
|
171 |
|
172 signal_lock(); |
|
173 |
|
174 sigused = (MASK(sig) & jvmsigs) != 0; |
|
175 if (jvm_signal_installed && sigused) { |
|
176 /* jvm has installed its signal handler for this signal. */ |
|
177 /* Save the handler. Don't really install it. */ |
|
178 if (oact != NULL) { |
|
179 *oact = sact[sig]; |
|
180 } |
|
181 if (act != NULL) { |
|
182 sact[sig] = *act; |
|
183 } |
|
184 |
|
185 signal_unlock(); |
|
186 return 0; |
|
187 } else if (jvm_signal_installing) { |
|
188 /* jvm is installing its signal handlers. Install the new |
|
189 * handlers and save the old ones. */ |
|
190 res = call_os_sigaction(sig, act, &oldAct); |
|
191 sact[sig] = oldAct; |
|
192 if (oact != NULL) { |
|
193 *oact = oldAct; |
|
194 } |
|
195 |
|
196 /* Record the signals used by jvm */ |
|
197 jvmsigs |= MASK(sig); |
|
198 |
|
199 signal_unlock(); |
|
200 return res; |
|
201 } else { |
|
202 /* jvm has no relation with this signal (yet). Install the |
|
203 * the handler. */ |
|
204 res = call_os_sigaction(sig, act, oact); |
|
205 |
|
206 signal_unlock(); |
|
207 return res; |
|
208 } |
|
209 } |
|
210 |
|
211 /* The three functions for the jvm to call into */ |
|
212 void JVM_begin_signal_setting() { |
|
213 signal_lock(); |
|
214 jvm_signal_installing = true; |
|
215 tid = pthread_self(); |
|
216 signal_unlock(); |
|
217 } |
|
218 |
|
219 void JVM_end_signal_setting() { |
|
220 signal_lock(); |
|
221 jvm_signal_installed = true; |
|
222 jvm_signal_installing = false; |
|
223 pthread_cond_broadcast(&cond); |
|
224 signal_unlock(); |
|
225 } |
|
226 |
|
227 struct sigaction *JVM_get_signal_action(int sig) { |
|
228 /* Does race condition make sense here? */ |
|
229 if ((MASK(sig) & jvmsigs) != 0) { |
|
230 return &sact[sig]; |
|
231 } |
|
232 return NULL; |
|
233 } |