< prev index next >

src/java.base/aix/native/libjsig/jsig.c

Print this page




  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 #include <stdint.h>

  40 
  41 #define bool int
  42 #define true 1
  43 #define false 0
  44 
  45 static struct sigaction sact[NSIG]; /* saved signal handlers */
  46 static sigset_t jvmsigs; /* Signals used by jvm. */
  47 
  48 /* Used to synchronize the installation of signal handlers. */
  49 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  50 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  51 static pthread_t tid = 0;
  52 
  53 typedef void (*sa_handler_t)(int);
  54 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  55 // signal_t is already defined on AIX.
  56 typedef sa_handler_t (*signal_like_function_t)(int, sa_handler_t);
  57 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
  58 
  59 static signal_like_function_t os_signal = 0; /* os's version of signal()/sigset() */


 123      * handlers and save the old ones. jvm uses sigaction().
 124      * Leave the piece here just in case. */
 125     oldhandler = call_os_signal(sig, disp, is_sigset);
 126     save_signal_handler(sig, oldhandler);
 127 
 128     /* Record the signals used by jvm */
 129     sigaddset(&jvmsigs, sig);
 130 
 131     signal_unlock();
 132     return oldhandler;
 133   } else {
 134     /* jvm has no relation with this signal (yet). Install the
 135      * the handler. */
 136     oldhandler = call_os_signal(sig, disp, is_sigset);
 137 
 138     signal_unlock();
 139     return oldhandler;
 140   }
 141 }
 142 
 143 sa_handler_t signal(int sig, sa_handler_t disp) {

 144   return set_signal(sig, disp, false);
 145 }
 146 
 147 sa_handler_t sigset(int sig, sa_handler_t disp) {

 148   return set_signal(sig, disp, true);
 149 }
 150 
 151 static int call_os_sigaction(int sig, const struct sigaction  *act,
 152                              struct sigaction *oact) {
 153   if (os_sigaction == NULL) {
 154     // Aix: call functions directly instead of dlsym'ing them.
 155     os_sigaction = sigaction;
 156     if (os_sigaction == NULL) {
 157       printf("%s\n", dlerror());
 158       exit(0);
 159     }
 160   }
 161   return (*os_sigaction)(sig, act, oact);
 162 }
 163 
 164 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {

 165   int res;
 166   bool sigused;
 167   struct sigaction oldAct;
 168 
 169   signal_lock();
 170 
 171   sigused = sigismember(&jvmsigs, sig);
 172   if (jvm_signal_installed && sigused) {
 173     /* jvm has installed its signal handler for this signal. */
 174     /* Save the handler. Don't really install it. */
 175     if (oact != NULL) {
 176       *oact = sact[sig];
 177     }
 178     if (act != NULL) {
 179       sact[sig] = *act;
 180     }
 181 
 182     signal_unlock();
 183     return 0;
 184   } else if (jvm_signal_installing) {


 189     if (oact != NULL) {
 190       *oact = oldAct;
 191     }
 192 
 193     /* Record the signals used by jvm. */
 194     sigaddset(&jvmsigs, sig);
 195 
 196     signal_unlock();
 197     return res;
 198   } else {
 199     /* jvm has no relation with this signal (yet). Install the
 200      * the handler. */
 201     res = call_os_sigaction(sig, act, oact);
 202 
 203     signal_unlock();
 204     return res;
 205   }
 206 }
 207 
 208 /* The three functions for the jvm to call into. */
 209 void JVM_begin_signal_setting() {

 210   signal_lock();
 211   sigemptyset(&jvmsigs);
 212   jvm_signal_installing = true;
 213   tid = pthread_self();
 214   signal_unlock();
 215 }
 216 
 217 void JVM_end_signal_setting() {

 218   signal_lock();
 219   jvm_signal_installed = true;
 220   jvm_signal_installing = false;
 221   pthread_cond_broadcast(&cond);
 222   signal_unlock();
 223 }
 224 
 225 struct sigaction *JVM_get_signal_action(int sig) {

 226   /* Does race condition make sense here? */
 227   if (sigismember(&jvmsigs, sig)) {
 228     return &sact[sig];
 229   }
 230   return NULL;
 231 }


  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 #include <stdint.h>
  40 #include "jni.h"
  41 
  42 #define bool int
  43 #define true 1
  44 #define false 0
  45 
  46 static struct sigaction sact[NSIG]; /* saved signal handlers */
  47 static sigset_t jvmsigs; /* Signals used by jvm. */
  48 
  49 /* Used to synchronize the installation of signal handlers. */
  50 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  51 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  52 static pthread_t tid = 0;
  53 
  54 typedef void (*sa_handler_t)(int);
  55 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  56 // signal_t is already defined on AIX.
  57 typedef sa_handler_t (*signal_like_function_t)(int, sa_handler_t);
  58 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
  59 
  60 static signal_like_function_t os_signal = 0; /* os's version of signal()/sigset() */


 124      * handlers and save the old ones. jvm uses sigaction().
 125      * Leave the piece here just in case. */
 126     oldhandler = call_os_signal(sig, disp, is_sigset);
 127     save_signal_handler(sig, oldhandler);
 128 
 129     /* Record the signals used by jvm */
 130     sigaddset(&jvmsigs, sig);
 131 
 132     signal_unlock();
 133     return oldhandler;
 134   } else {
 135     /* jvm has no relation with this signal (yet). Install the
 136      * the handler. */
 137     oldhandler = call_os_signal(sig, disp, is_sigset);
 138 
 139     signal_unlock();
 140     return oldhandler;
 141   }
 142 }
 143 
 144 JNIEXPORT sa_handler_t JNICALL
 145 signal(int sig, sa_handler_t disp) {
 146   return set_signal(sig, disp, false);
 147 }
 148 
 149 JNIEXPORT sa_handler_t JNICALL
 150 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 JNIEXPORT int JNICALL
 168 sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
 169   int res;
 170   bool sigused;
 171   struct sigaction oldAct;
 172 
 173   signal_lock();
 174 
 175   sigused = sigismember(&jvmsigs, sig);
 176   if (jvm_signal_installed && sigused) {
 177     /* jvm has installed its signal handler for this signal. */
 178     /* Save the handler. Don't really install it. */
 179     if (oact != NULL) {
 180       *oact = sact[sig];
 181     }
 182     if (act != NULL) {
 183       sact[sig] = *act;
 184     }
 185 
 186     signal_unlock();
 187     return 0;
 188   } else if (jvm_signal_installing) {


 193     if (oact != NULL) {
 194       *oact = oldAct;
 195     }
 196 
 197     /* Record the signals used by jvm. */
 198     sigaddset(&jvmsigs, sig);
 199 
 200     signal_unlock();
 201     return res;
 202   } else {
 203     /* jvm has no relation with this signal (yet). Install the
 204      * the handler. */
 205     res = call_os_sigaction(sig, act, oact);
 206 
 207     signal_unlock();
 208     return res;
 209   }
 210 }
 211 
 212 /* The three functions for the jvm to call into. */
 213 JNIEXPORT void JNICALL
 214 JVM_begin_signal_setting() {
 215   signal_lock();
 216   sigemptyset(&jvmsigs);
 217   jvm_signal_installing = true;
 218   tid = pthread_self();
 219   signal_unlock();
 220 }
 221 
 222 JNIEXPORT void JNICALL
 223 JVM_end_signal_setting() {
 224   signal_lock();
 225   jvm_signal_installed = true;
 226   jvm_signal_installing = false;
 227   pthread_cond_broadcast(&cond);
 228   signal_unlock();
 229 }
 230 
 231 JNIEXPORT struct sigaction * JNICALL
 232 JVM_get_signal_action(int sig) {
 233   /* Does race condition make sense here? */
 234   if (sigismember(&jvmsigs, sig)) {
 235     return &sact[sig];
 236   }
 237   return NULL;
 238 }
< prev index next >