1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/os/linux/vm/jsig.c Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,227 @@ 1.4 +/* 1.5 + * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +/* CopyrightVersion 1.2 */ 1.29 + 1.30 +/* This is a special library that should be loaded before libc & 1.31 + * libthread to interpose the signal handler installation functions: 1.32 + * sigaction(), signal(), sigset(). 1.33 + * Used for signal-chaining. See RFE 4381843. 1.34 + */ 1.35 + 1.36 +#include <signal.h> 1.37 +#include <dlfcn.h> 1.38 +#include <pthread.h> 1.39 +#include <stdio.h> 1.40 +#include <stdlib.h> 1.41 + 1.42 +#define bool int 1.43 +#define true 1 1.44 +#define false 0 1.45 + 1.46 +#define MAXSIGNUM 32 1.47 +#define MASK(sig) ((unsigned int)1 << sig) 1.48 + 1.49 +static struct sigaction sact[MAXSIGNUM]; /* saved signal handlers */ 1.50 +static unsigned int jvmsigs = 0; /* signals used by jvm */ 1.51 + 1.52 +/* used to synchronize the installation of signal handlers */ 1.53 +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 1.54 +static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 1.55 +static pthread_t tid = 0; 1.56 + 1.57 +typedef void (*sa_handler_t)(int); 1.58 +typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); 1.59 +typedef sa_handler_t (*signal_t)(int, sa_handler_t); 1.60 +typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *); 1.61 + 1.62 +static signal_t os_signal = 0; /* os's version of signal()/sigset() */ 1.63 +static sigaction_t os_sigaction = 0; /* os's version of sigaction() */ 1.64 + 1.65 +static bool jvm_signal_installing = false; 1.66 +static bool jvm_signal_installed = false; 1.67 + 1.68 +static void signal_lock() { 1.69 + pthread_mutex_lock(&mutex); 1.70 + /* When the jvm is installing its set of signal handlers, threads 1.71 + * other than the jvm thread should wait */ 1.72 + if (jvm_signal_installing) { 1.73 + if (tid != pthread_self()) { 1.74 + pthread_cond_wait(&cond, &mutex); 1.75 + } 1.76 + } 1.77 +} 1.78 + 1.79 +static void signal_unlock() { 1.80 + pthread_mutex_unlock(&mutex); 1.81 +} 1.82 + 1.83 +static sa_handler_t call_os_signal(int sig, sa_handler_t disp, 1.84 + bool is_sigset) { 1.85 + if (os_signal == NULL) { 1.86 + if (!is_sigset) { 1.87 + os_signal = (signal_t)dlsym(RTLD_NEXT, "signal"); 1.88 + } else { 1.89 + os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset"); 1.90 + } 1.91 + if (os_signal == NULL) { 1.92 + printf("%s\n", dlerror()); 1.93 + exit(0); 1.94 + } 1.95 + } 1.96 + return (*os_signal)(sig, disp); 1.97 +} 1.98 + 1.99 +static void save_signal_handler(int sig, sa_handler_t disp) { 1.100 + sigset_t set; 1.101 + sact[sig].sa_handler = disp; 1.102 + sigemptyset(&set); 1.103 + sact[sig].sa_mask = set; 1.104 + sact[sig].sa_flags = 0; 1.105 +} 1.106 + 1.107 +static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) { 1.108 + sa_handler_t oldhandler; 1.109 + bool sigused; 1.110 + 1.111 + signal_lock(); 1.112 + 1.113 + sigused = (MASK(sig) & jvmsigs) != 0; 1.114 + if (jvm_signal_installed && sigused) { 1.115 + /* jvm has installed its signal handler for this signal. */ 1.116 + /* Save the handler. Don't really install it. */ 1.117 + oldhandler = sact[sig].sa_handler; 1.118 + save_signal_handler(sig, disp); 1.119 + 1.120 + signal_unlock(); 1.121 + return oldhandler; 1.122 + } else if (jvm_signal_installing) { 1.123 + /* jvm is installing its signal handlers. Install the new 1.124 + * handlers and save the old ones. jvm uses sigaction(). 1.125 + * Leave the piece here just in case. */ 1.126 + oldhandler = call_os_signal(sig, disp, is_sigset); 1.127 + save_signal_handler(sig, oldhandler); 1.128 + 1.129 + /* Record the signals used by jvm */ 1.130 + jvmsigs |= MASK(sig); 1.131 + 1.132 + signal_unlock(); 1.133 + return oldhandler; 1.134 + } else { 1.135 + /* jvm has no relation with this signal (yet). Install the 1.136 + * the handler. */ 1.137 + oldhandler = call_os_signal(sig, disp, is_sigset); 1.138 + 1.139 + signal_unlock(); 1.140 + return oldhandler; 1.141 + } 1.142 +} 1.143 + 1.144 +sa_handler_t signal(int sig, sa_handler_t disp) { 1.145 + return set_signal(sig, disp, false); 1.146 +} 1.147 + 1.148 +sa_handler_t sigset(int sig, sa_handler_t disp) { 1.149 + return set_signal(sig, disp, true); 1.150 + } 1.151 + 1.152 +static int call_os_sigaction(int sig, const struct sigaction *act, 1.153 + struct sigaction *oact) { 1.154 + if (os_sigaction == NULL) { 1.155 + os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction"); 1.156 + if (os_sigaction == NULL) { 1.157 + printf("%s\n", dlerror()); 1.158 + exit(0); 1.159 + } 1.160 + } 1.161 + return (*os_sigaction)(sig, act, oact); 1.162 +} 1.163 + 1.164 +int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { 1.165 + int res; 1.166 + bool sigused; 1.167 + struct sigaction oldAct; 1.168 + 1.169 + signal_lock(); 1.170 + 1.171 + sigused = (MASK(sig) & jvmsigs) != 0; 1.172 + if (jvm_signal_installed && sigused) { 1.173 + /* jvm has installed its signal handler for this signal. */ 1.174 + /* Save the handler. Don't really install it. */ 1.175 + if (oact != NULL) { 1.176 + *oact = sact[sig]; 1.177 + } 1.178 + if (act != NULL) { 1.179 + sact[sig] = *act; 1.180 + } 1.181 + 1.182 + signal_unlock(); 1.183 + return 0; 1.184 + } else if (jvm_signal_installing) { 1.185 + /* jvm is installing its signal handlers. Install the new 1.186 + * handlers and save the old ones. */ 1.187 + res = call_os_sigaction(sig, act, &oldAct); 1.188 + sact[sig] = oldAct; 1.189 + if (oact != NULL) { 1.190 + *oact = oldAct; 1.191 + } 1.192 + 1.193 + /* Record the signals used by jvm */ 1.194 + jvmsigs |= MASK(sig); 1.195 + 1.196 + signal_unlock(); 1.197 + return res; 1.198 + } else { 1.199 + /* jvm has no relation with this signal (yet). Install the 1.200 + * the handler. */ 1.201 + res = call_os_sigaction(sig, act, oact); 1.202 + 1.203 + signal_unlock(); 1.204 + return res; 1.205 + } 1.206 +} 1.207 + 1.208 +/* The three functions for the jvm to call into */ 1.209 +void JVM_begin_signal_setting() { 1.210 + signal_lock(); 1.211 + jvm_signal_installing = true; 1.212 + tid = pthread_self(); 1.213 + signal_unlock(); 1.214 +} 1.215 + 1.216 +void JVM_end_signal_setting() { 1.217 + signal_lock(); 1.218 + jvm_signal_installed = true; 1.219 + jvm_signal_installing = false; 1.220 + pthread_cond_broadcast(&cond); 1.221 + signal_unlock(); 1.222 +} 1.223 + 1.224 +struct sigaction *JVM_get_signal_action(int sig) { 1.225 + /* Does race condition make sense here? */ 1.226 + if ((MASK(sig) & jvmsigs) != 0) { 1.227 + return &sact[sig]; 1.228 + } 1.229 + return NULL; 1.230 +}