< 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);


   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 <pthread.h>
  34 #include <signal.h>
  35 #include <stdio.h>
  36 #include <stdlib.h>

  37 
  38 #if (__STDC_VERSION__ >= 199901L)
  39   #include <stdbool.h>
  40 #else
  41   #define bool int
  42   #define true 1
  43   #define false 0
  44 #endif
  45 
  46 static struct sigaction sact[NSIG]; /* saved signal handlers */
  47 static sigset_t jvmsigs; /* Signals used by jvm. */
  48 
  49 #ifdef MACOSX
  50 static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */
  51 #endif
  52 
  53 /* Used to synchronize the installation of signal handlers. */
  54 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  55 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  56 static pthread_t tid = 0;
  57 
  58 typedef void (*sa_handler_t)(int);
  59 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  60 typedef sa_handler_t (*signal_function_t)(int, sa_handler_t);

  61 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
  62 
  63 static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */
  64 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
  65 
  66 static bool jvm_signal_installing = false;
  67 static bool jvm_signal_installed = false;
  68 
  69 static void signal_lock() {
  70   pthread_mutex_lock(&mutex);
  71   /* When the jvm is installing its set of signal handlers, threads
  72    * other than the jvm thread should wait. */
  73   if (jvm_signal_installing) {
  74     if (tid != pthread_self()) {
  75       pthread_cond_wait(&cond, &mutex);
  76     }
  77   }
  78 }
  79 
  80 static void signal_unlock() {
  81   pthread_mutex_unlock(&mutex);
  82 }
  83 
  84 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
  85                                    bool is_sigset) {
  86   sa_handler_t res;
  87 
  88   if (os_signal == NULL) {
  89     if (!is_sigset) {
  90       os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal");

  91     } else {
  92       os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset");

  93     }
  94     if (os_signal == NULL) {
  95       printf("%s\n", dlerror());
  96       exit(0);
  97     }
  98   }
  99 
 100 #ifdef MACOSX
 101   /* On macosx, the OS implementation of signal calls sigaction.
 102    * Make sure we do not deadlock with ourself. (See JDK-8072147). */
 103   reentry = true;
 104 #endif
 105 
 106   res = (*os_signal)(sig, disp);
 107 
 108 #ifdef MACOSX
 109   reentry = false;
 110 #endif
 111 
 112   return res;
 113 }
 114 
 115 static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
 116   sigset_t set;
 117   sact[sig].sa_handler = disp;
 118   sigemptyset(&set);
 119   sact[sig].sa_mask = set;
 120   if (!is_sigset) {
 121 #ifdef SOLARIS
 122     sact[sig].sa_flags = SA_NODEFER;
 123     if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
 124       sact[sig].sa_flags |= SA_RESETHAND;
 125     }
 126 #else
 127     sact[sig].sa_flags = 0;
 128 #endif
 129   } else {
 130     sact[sig].sa_flags = 0;
 131   }
 132 }
 133 
 134 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
 135   sa_handler_t oldhandler;
 136   bool sigused;
 137   bool sigblocked;
 138 
 139   signal_lock();
 140 
 141   sigused = sigismember(&jvmsigs, sig);
 142   if (jvm_signal_installed && sigused) {
 143     /* jvm has installed its signal handler for this signal. */
 144     /* Save the handler. Don't really install it. */
 145     if (is_sigset) {
 146       sigblocked = sigismember(&(sact[sig].sa_mask), sig);
 147     }
 148     oldhandler = sact[sig].sa_handler;
 149     save_signal_handler(sig, disp, is_sigset);
 150 
 151 #ifdef SOLARIS
 152     if (is_sigset && sigblocked) {
 153       /* We won't honor the SIG_HOLD request to change the signal mask */
 154       oldhandler = SIG_HOLD;
 155     }
 156 #endif
 157 
 158     signal_unlock();
 159     return oldhandler;
 160   } else if (jvm_signal_installing) {
 161     /* jvm is installing its signal handlers. Install the new
 162      * handlers and save the old ones. jvm uses sigaction().
 163      * Leave the piece here just in case. */
 164     oldhandler = call_os_signal(sig, disp, is_sigset);
 165     save_signal_handler(sig, oldhandler, is_sigset);
 166 
 167     /* Record the signals used by jvm */
 168     sigaddset(&jvmsigs, sig);
 169 
 170     signal_unlock();
 171     return oldhandler;
 172   } else {
 173     /* jvm has no relation with this signal (yet). Install the
 174      * the handler. */
 175     oldhandler = call_os_signal(sig, disp, is_sigset);
 176 
 177     signal_unlock();
 178     return oldhandler;
 179   }
 180 }
 181 
 182 sa_handler_t signal(int sig, sa_handler_t disp) {
 183   return set_signal(sig, disp, false);
 184 }
 185 
 186 sa_handler_t sigset(int sig, sa_handler_t disp) {
 187 #ifdef _ALLBSD_SOURCE
 188   printf("sigset() is not supported by BSD");
 189   exit(0);
 190 #else
 191   return set_signal(sig, disp, true);
 192 #endif
 193 }
 194 
 195 static int call_os_sigaction(int sig, const struct sigaction  *act,
 196                              struct sigaction *oact) {
 197   if (os_sigaction == NULL) {
 198     os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");

 199     if (os_sigaction == NULL) {
 200       printf("%s\n", dlerror());
 201       exit(0);
 202     }
 203   }
 204   return (*os_sigaction)(sig, act, oact);
 205 }
 206 
 207 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
 208   int res;
 209   bool sigused;
 210   struct sigaction oldAct;
 211 
 212 #ifdef MACOSX
 213   if (reentry) {
 214     return call_os_sigaction(sig, act, oact);
 215   }
 216 #endif
 217 
 218   signal_lock();
 219 
 220   sigused = sigismember(&jvmsigs, sig);
 221   if (jvm_signal_installed && sigused) {
 222     /* jvm has installed its signal handler for this signal. */
 223     /* Save the handler. Don't really install it. */
 224     if (oact != NULL) {
 225       *oact = sact[sig];
 226     }
 227     if (act != NULL) {
 228       sact[sig] = *act;
 229     }
 230 
 231     signal_unlock();
 232     return 0;
 233   } else if (jvm_signal_installing) {
 234     /* jvm is installing its signal handlers. Install the new
 235      * handlers and save the old ones. */
 236     res = call_os_sigaction(sig, act, &oldAct);


< prev index next >