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 <errno.h>
  34 #include <pthread.h>
  35 #include <signal.h>
  36 #include <stdio.h>
  37 #include <stdlib.h>
  38 
  39 #if (__STDC_VERSION__ >= 199901L)
  40   #include <stdbool.h>
  41 #else
  42   #define bool int
  43   #define true 1
  44   #define false 0
  45 #endif
  46 
  47 #ifdef SOLARIS
  48   #define MAX_SIGNALS (SIGRTMAX+1)
  49 #else
  50   #define MAX_SIGNALS NSIG
  51 #endif
  52 
  53 static struct sigaction sact[MAX_SIGNALS]; /* saved signal handlers */
  54 static sigset_t jvmsigs; /* Signals used by jvm. */
  55 
  56 #ifdef MACOSX
  57 static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */
  58 #endif
  59 
  60 /* Used to synchronize the installation of signal handlers. */
  61 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  62 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  63 static pthread_t tid = 0;
  64 
  65 typedef void (*sa_handler_t)(int);
  66 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  67 typedef sa_handler_t (*signal_function_t)(int, sa_handler_t);
  68 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
  69 
  70 static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */
  71 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
  72 
  73 static bool jvm_signal_installing = false;
  74 static bool jvm_signal_installed = false;
  75 
  76 static void signal_lock() {
  77   pthread_mutex_lock(&mutex);
  78   /* When the jvm is installing its set of signal handlers, threads
  79    * other than the jvm thread should wait. */
  80   if (jvm_signal_installing) {
  81     if (tid != pthread_self()) {
  82       pthread_cond_wait(&cond, &mutex);
  83     }
  84   }
  85 }
  86 
  87 static void signal_unlock() {
  88   pthread_mutex_unlock(&mutex);
  89 }
  90 
  91 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
  92                                    bool is_sigset) {
  93   sa_handler_t res;
  94 
  95   if (os_signal == NULL) {
  96     if (!is_sigset) {
  97       os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal");
  98     } else {
  99       os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset");
 100     }
 101     if (os_signal == NULL) {
 102       printf("%s\n", dlerror());
 103       exit(0);
 104     }
 105   }
 106 
 107 #ifdef MACOSX
 108   /* On macosx, the OS implementation of signal calls sigaction.
 109    * Make sure we do not deadlock with ourself. (See JDK-8072147). */
 110   reentry = true;
 111 #endif
 112 
 113   res = (*os_signal)(sig, disp);
 114 
 115 #ifdef MACOSX
 116   reentry = false;
 117 #endif
 118 
 119   return res;
 120 }
 121 
 122 static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
 123   sigset_t set;
 124   sact[sig].sa_handler = disp;
 125   sigemptyset(&set);
 126   sact[sig].sa_mask = set;
 127   if (!is_sigset) {
 128 #ifdef SOLARIS
 129     sact[sig].sa_flags = SA_NODEFER;
 130     if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
 131       sact[sig].sa_flags |= SA_RESETHAND;
 132     }
 133 #else
 134     sact[sig].sa_flags = 0;
 135 #endif
 136   } else {
 137     sact[sig].sa_flags = 0;
 138   }
 139 }
 140 
 141 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
 142   sa_handler_t oldhandler;
 143   bool sigused;
 144   bool sigblocked;
 145 
 146   signal_lock();
 147 
 148   sigused = sigismember(&jvmsigs, sig);
 149   if (jvm_signal_installed && sigused) {
 150     /* jvm has installed its signal handler for this signal. */
 151     /* Save the handler. Don't really install it. */
 152     if (is_sigset) {
 153       sigblocked = sigismember(&(sact[sig].sa_mask), sig);
 154     }
 155     oldhandler = sact[sig].sa_handler;
 156     save_signal_handler(sig, disp, is_sigset);
 157 
 158 #ifdef SOLARIS
 159     if (is_sigset && sigblocked) {
 160       /* We won't honor the SIG_HOLD request to change the signal mask */
 161       oldhandler = SIG_HOLD;
 162     }
 163 #endif
 164 
 165     signal_unlock();
 166     return oldhandler;
 167   } else if (jvm_signal_installing) {
 168     /* jvm is installing its signal handlers. Install the new
 169      * handlers and save the old ones. jvm uses sigaction().
 170      * Leave the piece here just in case. */
 171     oldhandler = call_os_signal(sig, disp, is_sigset);
 172     save_signal_handler(sig, oldhandler, is_sigset);
 173 
 174     /* Record the signals used by jvm */
 175     sigaddset(&jvmsigs, sig);
 176 
 177     signal_unlock();
 178     return oldhandler;
 179   } else {
 180     /* jvm has no relation with this signal (yet). Install the
 181      * the handler. */
 182     oldhandler = call_os_signal(sig, disp, is_sigset);
 183 
 184     signal_unlock();
 185     return oldhandler;
 186   }
 187 }
 188 
 189 sa_handler_t signal(int sig, sa_handler_t disp) {
 190   if (sig < 0 || sig >= MAX_SIGNALS) {
 191     errno = EINVAL;
 192     return SIG_ERR;
 193   }
 194 
 195   return set_signal(sig, disp, false);
 196 }
 197 
 198 sa_handler_t sigset(int sig, sa_handler_t disp) {
 199 #ifdef _ALLBSD_SOURCE
 200   printf("sigset() is not supported by BSD");
 201   exit(0);
 202 #else
 203   if (sig < 0 || sig >= MAX_SIGNALS) {
 204     errno = EINVAL;
 205     return (sa_handler_t)-1;
 206   }
 207 
 208   return set_signal(sig, disp, true);
 209 #endif
 210 }
 211 
 212 static int call_os_sigaction(int sig, const struct sigaction  *act,
 213                              struct sigaction *oact) {
 214   if (os_sigaction == NULL) {
 215     os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
 216     if (os_sigaction == NULL) {
 217       printf("%s\n", dlerror());
 218       exit(0);
 219     }
 220   }
 221   return (*os_sigaction)(sig, act, oact);
 222 }
 223 
 224 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
 225   int res;
 226   bool sigused;
 227   struct sigaction oldAct;
 228 
 229   if (sig < 0 || sig >= MAX_SIGNALS) {
 230     errno = EINVAL;
 231     return -1;
 232   }
 233 
 234 #ifdef MACOSX
 235   if (reentry) {
 236     return call_os_sigaction(sig, act, oact);
 237   }
 238 #endif
 239 
 240   signal_lock();
 241 
 242   sigused = sigismember(&jvmsigs, sig);
 243   if (jvm_signal_installed && sigused) {
 244     /* jvm has installed its signal handler for this signal. */
 245     /* Save the handler. Don't really install it. */
 246     if (oact != NULL) {
 247       *oact = sact[sig];
 248     }
 249     if (act != NULL) {
 250       sact[sig] = *act;
 251     }
 252 
 253     signal_unlock();
 254     return 0;
 255   } else if (jvm_signal_installing) {
 256     /* jvm is installing its signal handlers. Install the new
 257      * handlers and save the old ones. */
 258     res = call_os_sigaction(sig, act, &oldAct);
 259     sact[sig] = oldAct;
 260     if (oact != NULL) {
 261       *oact = oldAct;
 262     }
 263 
 264     /* Record the signals used by jvm. */
 265     sigaddset(&jvmsigs, sig);
 266 
 267     signal_unlock();
 268     return res;
 269   } else {
 270     /* jvm has no relation with this signal (yet). Install the
 271      * the handler. */
 272     res = call_os_sigaction(sig, act, oact);
 273 
 274     signal_unlock();
 275     return res;
 276   }
 277 }
 278 
 279 /* The three functions for the jvm to call into. */
 280 void JVM_begin_signal_setting() {
 281   signal_lock();
 282   sigemptyset(&jvmsigs);
 283   jvm_signal_installing = true;
 284   tid = pthread_self();
 285   signal_unlock();
 286 }
 287 
 288 void JVM_end_signal_setting() {
 289   signal_lock();
 290   jvm_signal_installed = true;
 291   jvm_signal_installing = false;
 292   pthread_cond_broadcast(&cond);
 293   signal_unlock();
 294 }
 295 
 296 struct sigaction *JVM_get_signal_action(int sig) {
 297   /* Does race condition make sense here? */
 298   if (sigismember(&jvmsigs, sig)) {
 299     return &sact[sig];
 300   }
 301   return NULL;
 302 }