< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2001, 2015, 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.
   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 #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() */
  60 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
  61 
  62 static bool jvm_signal_installing = false;
  63 static bool jvm_signal_installed = false;
  64 
  65 static void signal_lock() {



  66   pthread_mutex_lock(&mutex);

  67   /* When the jvm is installing its set of signal handlers, threads
  68    * other than the jvm thread should wait. */
  69   if (jvm_signal_installing) {





  70     if (tid != pthread_self()) {
  71       pthread_cond_wait(&cond, &mutex);
  72     }

  73   }
  74 }
  75 
  76 static void signal_unlock() {



  77   pthread_mutex_unlock(&mutex);

  78 }
  79 
  80 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
  81                                    bool is_sigset) {


  82   if (os_signal == NULL) {
  83     if (!is_sigset) {
  84       // Aix: call functions directly instead of dlsym'ing them.
  85       os_signal = signal;



  86     } else {
  87       // Aix: call functions directly instead of dlsym'ing them.
  88       os_signal = sigset;



  89     }
  90     if (os_signal == NULL) {
  91       printf("%s\n", dlerror());
  92       exit(0);
  93     }
  94   }
  95   return (*os_signal)(sig, disp);





  96 }
  97 
  98 static void save_signal_handler(int sig, sa_handler_t disp) {
  99   sigset_t set;
 100   sact[sig].sa_handler = disp;
 101   sigemptyset(&set);
 102   sact[sig].sa_mask = set;







 103   sact[sig].sa_flags = 0;




 104 }
 105 
 106 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
 107   sa_handler_t oldhandler;
 108   bool sigused;

 109 
 110   signal_lock();
 111 
 112   sigused = sigismember(&jvmsigs, sig);
 113   if (jvm_signal_installed && sigused) {
 114     /* jvm has installed its signal handler for this signal. */
 115     /* Save the handler. Don't really install it. */



 116     oldhandler = sact[sig].sa_handler;
 117     save_signal_handler(sig, disp);







 118 
 119     signal_unlock();
 120     return oldhandler;
 121   } else if (jvm_signal_installing) {
 122     /* jvm is installing its signal handlers. Install the new
 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) {
 185     /* jvm is installing its signal handlers. Install the new
 186      * handlers and save the old ones. */
 187     res = call_os_sigaction(sig, act, &oldAct);
 188     sact[sig] = oldAct;


 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 }
   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.
   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 /* This is a special library that should be loaded before libc &
  27  * libthread to interpose the signal handler installation functions:
  28  * sigaction(), signal(), sigset().
  29  * Used for signal-chaining. See RFE 4381843.
  30  */
  31 

  32 #include <dlfcn.h>
  33 #include <signal.h>
  34 #include <stdio.h>
  35 #include <stdlib.h>

  36 
  37 #ifdef SOLARIS
  38   #include <synch.h>
  39   #include <thread.h>
  40 #else
  41   #include <pthread.h>
  42 #endif
  43 
  44 #if (__STDC_VERSION__ >= 199901L)
  45   #include <stdbool.h>
  46 #else
  47   #define bool int
  48   #define true 1
  49   #define false 0
  50 #endif
  51 
  52 #ifndef NSIG
  53   #define NSIG SIGRTMAX
  54 #endif
  55 
  56 static struct sigaction sact[NSIG]; /* saved signal handlers */
  57 static sigset_t jvmsigs; /* Signals used by jvm. */
  58 static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */
  59 
  60 /* Used to synchronize the installation of signal handlers. */
  61 #ifdef SOLARIS
  62 static mutex_t mutex = DEFAULTMUTEX;
  63 static cond_t cond = DEFAULTCV;
  64 static thread_t tid = 0;
  65 #else
  66 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  67 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  68 static pthread_t tid = 0;
  69 #endif
  70 
  71 typedef void (*sa_handler_t)(int);
  72 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  73 typedef sa_handler_t (*signal_function_t)(int, sa_handler_t);

  74 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
  75 
  76 static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */
  77 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
  78 
  79 static bool jvm_signal_installing = false;
  80 static bool jvm_signal_installed = false;
  81 
  82 static void signal_lock() {
  83 #ifdef SOLARIS
  84   mutex_lock(&mutex);
  85 #else
  86   pthread_mutex_lock(&mutex);
  87 #endif
  88   /* When the jvm is installing its set of signal handlers, threads
  89    * other than the jvm thread should wait. */
  90   if (jvm_signal_installing) {
  91 #ifdef SOLARIS
  92     if (tid != thr_self()) {
  93       cond_wait(&cond, &mutex);
  94     }
  95 #else
  96     if (tid != pthread_self()) {
  97       pthread_cond_wait(&cond, &mutex);
  98     }
  99 #endif
 100   }
 101 }
 102 
 103 static void signal_unlock() {
 104 #ifdef SOLARIS
 105   mutex_unlock(&mutex);
 106 #else
 107   pthread_mutex_unlock(&mutex);
 108 #endif
 109 }
 110 
 111 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
 112                                    bool is_sigset) {
 113   sa_handler_t res;
 114 
 115   if (os_signal == NULL) {
 116     if (!is_sigset) {
 117 #ifdef AIX
 118       os_signal = signal;
 119 #else
 120       os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal");
 121 #endif
 122     } else {
 123 #ifdef AIX
 124       os_signal = sigset;
 125 #else
 126       os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset");
 127 #endif
 128     }
 129     if (os_signal == NULL) {
 130       printf("%s\n", dlerror());
 131       exit(0);
 132     }
 133   }
 134   /* On some OSes like macosx, the OS implementation of signal calls sigaction.
 135    * Make sure we do not deadlock with ourself. (See JDK-8072147). */
 136   reentry = true;
 137   res = (*os_signal)(sig, disp);
 138   reentry = false;
 139   return res;
 140 }
 141 
 142 static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
 143   sigset_t set;
 144   sact[sig].sa_handler = disp;
 145   sigemptyset(&set);
 146   sact[sig].sa_mask = set;
 147   if (!is_sigset) {
 148 #ifdef SOLARIS
 149     sact[sig].sa_flags = SA_NODEFER;
 150     if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
 151       sact[sig].sa_flags |= SA_RESETHAND;
 152     }
 153 #else
 154     sact[sig].sa_flags = 0;
 155 #endif
 156   } else {
 157     sact[sig].sa_flags = 0;
 158   }
 159 }
 160 
 161 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
 162   sa_handler_t oldhandler;
 163   bool sigused;
 164   bool sigblocked;
 165 
 166   signal_lock();
 167 
 168   sigused = sigismember(&jvmsigs, sig);
 169   if (jvm_signal_installed && sigused) {
 170     /* jvm has installed its signal handler for this signal. */
 171     /* Save the handler. Don't really install it. */
 172     if (is_sigset) {
 173       sigblocked = sigismember(&(sact[sig].sa_mask), sig);
 174     }
 175     oldhandler = sact[sig].sa_handler;
 176     save_signal_handler(sig, disp, is_sigset);
 177 
 178 #ifdef SOLARIS
 179     if (is_sigset && sigblocked) {
 180       /* We won't honor the SIG_HOLD request to change the signal mask */
 181       oldhandler = SIG_HOLD;
 182     }
 183 #endif
 184 
 185     signal_unlock();
 186     return oldhandler;
 187   } else if (jvm_signal_installing) {
 188     /* jvm is installing its signal handlers. Install the new
 189      * handlers and save the old ones. jvm uses sigaction().
 190      * Leave the piece here just in case. */
 191     oldhandler = call_os_signal(sig, disp, is_sigset);
 192     save_signal_handler(sig, oldhandler, is_sigset);
 193 
 194     /* Record the signals used by jvm */
 195     sigaddset(&jvmsigs, sig);
 196 
 197     signal_unlock();
 198     return oldhandler;
 199   } else {
 200     /* jvm has no relation with this signal (yet). Install the
 201      * the handler. */
 202     oldhandler = call_os_signal(sig, disp, is_sigset);
 203 
 204     signal_unlock();
 205     return oldhandler;
 206   }
 207 }
 208 
 209 sa_handler_t signal(int sig, sa_handler_t disp) {
 210   return set_signal(sig, disp, false);
 211 }
 212 
 213 sa_handler_t sigset(int sig, sa_handler_t disp) {
 214 #ifdef _ALLBSD_SOURCE
 215   printf("sigset() is not supported by BSD");
 216   exit(0);
 217 #else
 218   return set_signal(sig, disp, true);
 219 #endif
 220 }
 221 
 222 static int call_os_sigaction(int sig, const struct sigaction  *act,
 223                              struct sigaction *oact) {
 224   if (os_sigaction == NULL) {
 225 #ifdef AIX
 226     os_sigaction = sigaction;
 227 #else
 228     os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
 229 #endif
 230     if (os_sigaction == NULL) {
 231       printf("%s\n", dlerror());
 232       exit(0);
 233     }
 234   }
 235   return (*os_sigaction)(sig, act, oact);
 236 }
 237 
 238 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
 239   int res;
 240   bool sigused;
 241   struct sigaction oldAct;
 242 
 243   if (reentry) {
 244     return call_os_sigaction(sig, act, oact);
 245   }
 246 
 247   signal_lock();
 248 
 249   sigused = sigismember(&jvmsigs, sig);
 250   if (jvm_signal_installed && sigused) {
 251     /* jvm has installed its signal handler for this signal. */
 252     /* Save the handler. Don't really install it. */
 253     if (oact != NULL) {
 254       *oact = sact[sig];
 255     }
 256     if (act != NULL) {
 257       sact[sig] = *act;
 258     }
 259 
 260     signal_unlock();
 261     return 0;
 262   } else if (jvm_signal_installing) {
 263     /* jvm is installing its signal handlers. Install the new
 264      * handlers and save the old ones. */
 265     res = call_os_sigaction(sig, act, &oldAct);
 266     sact[sig] = oldAct;


 271     /* Record the signals used by jvm. */
 272     sigaddset(&jvmsigs, sig);
 273 
 274     signal_unlock();
 275     return res;
 276   } else {
 277     /* jvm has no relation with this signal (yet). Install the
 278      * the handler. */
 279     res = call_os_sigaction(sig, act, oact);
 280 
 281     signal_unlock();
 282     return res;
 283   }
 284 }
 285 
 286 /* The three functions for the jvm to call into. */
 287 void JVM_begin_signal_setting() {
 288   signal_lock();
 289   sigemptyset(&jvmsigs);
 290   jvm_signal_installing = true;
 291 #ifdef SOLARIS
 292   tid = thr_self();
 293 #else
 294   tid = pthread_self();
 295 #endif
 296   signal_unlock();
 297 }
 298 
 299 void JVM_end_signal_setting() {
 300   signal_lock();
 301   jvm_signal_installed = true;
 302   jvm_signal_installing = false;
 303 #ifdef SOLARIS
 304   cond_broadcast(&cond);
 305 #else
 306   pthread_cond_broadcast(&cond);
 307 #endif
 308   signal_unlock();
 309 }
 310 
 311 struct sigaction *JVM_get_signal_action(int sig) {
 312   /* Does race condition make sense here? */
 313   if (sigismember(&jvmsigs, sig)) {
 314     return &sact[sig];
 315   }
 316   return NULL;
 317 }
< prev index next >