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