1 /*
   2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /* CopyrightVersion 1.2 */
  26 
  27 /* This is a special library that should be loaded before libc &
  28  * libthread to interpose the signal handler installation functions:
  29  * sigaction(), signal(), sigset().
  30  * Used for signal-chaining. See RFE 4381843.
  31  */
  32 
  33 #include <signal.h>
  34 #include <dlfcn.h>
  35 #include <pthread.h>
  36 #include <stdio.h>
  37 #include <stdlib.h>
  38 #include <stdint.h>
  39 
  40 #define bool int
  41 #define true 1
  42 #define false 0
  43 
  44 #define MASK(sig) ((uint64_t)1 << (sig-1))  // 0 is not a signal.
  45 // Check whether all signals fit into jvmsigs. -1 as MASK shifts by -1.
  46 #if (64 < NSIG-1)
  47 #error "Not all signals can be encoded in jvmsigs. Adapt its type!"
  48 #endif
  49 static struct sigaction sact[NSIG]; /* saved signal handlers */
  50 static uint64_t jvmsigs = 0; /* signals used by jvm */
  51 
  52 /* used to synchronize the installation of signal handlers */
  53 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  54 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  55 static pthread_t tid = 0;
  56 
  57 typedef void (*sa_handler_t)(int);
  58 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  59 typedef sa_handler_t (*signal_t)(int, sa_handler_t);
  60 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
  61 
  62 static signal_t os_signal = 0; /* os's version of signal()/sigset() */
  63 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
  64 
  65 static bool jvm_signal_installing = false;
  66 static bool jvm_signal_installed = false;
  67 
  68 static void signal_lock() {
  69   pthread_mutex_lock(&mutex);
  70   /* When the jvm is installing its set of signal handlers, threads
  71    * other than the jvm thread should wait */
  72   if (jvm_signal_installing) {
  73     if (tid != pthread_self()) {
  74       pthread_cond_wait(&cond, &mutex);
  75     }
  76   }
  77 }
  78 
  79 static void signal_unlock() {
  80   pthread_mutex_unlock(&mutex);
  81 }
  82 
  83 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
  84                                    bool is_sigset) {
  85   if (os_signal == NULL) {
  86     if (!is_sigset) {
  87       os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
  88     } else {
  89       os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
  90     }
  91     if (os_signal == NULL) {
  92       printf("%s\n", dlerror());
  93       exit(0);
  94     }
  95   }
  96   return (*os_signal)(sig, disp);
  97 }
  98 
  99 static void save_signal_handler(int sig, sa_handler_t disp) {
 100   sigset_t set;
 101   sact[sig].sa_handler = disp;
 102   sigemptyset(&set);
 103   sact[sig].sa_mask = set;
 104   sact[sig].sa_flags = 0;
 105 }
 106 
 107 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
 108   sa_handler_t oldhandler;
 109   bool sigused;
 110 
 111   signal_lock();
 112 
 113   sigused = (sig < NSIG) && ((MASK(sig) & jvmsigs) != 0);
 114   if (jvm_signal_installed && sigused) {
 115     /* jvm has installed its signal handler for this signal. */
 116     /* Save the handler. Don't really install it. */
 117     oldhandler = sact[sig].sa_handler;
 118     save_signal_handler(sig, disp);
 119 
 120     signal_unlock();
 121     return oldhandler;
 122   } else if (sig < NSIG && jvm_signal_installing) {
 123     /* jvm is installing its signal handlers. Install the new
 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     jvmsigs |= MASK(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 sa_handler_t signal(int sig, sa_handler_t disp) {
 145   return set_signal(sig, disp, false);
 146 }
 147 
 148 sa_handler_t sigset(int sig, sa_handler_t disp) {
 149   return set_signal(sig, disp, true);
 150  }
 151 
 152 static int call_os_sigaction(int sig, const struct sigaction  *act,
 153                              struct sigaction *oact) {
 154   if (os_sigaction == NULL) {
 155     os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "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 = (sig < NSIG) && ((MASK(sig) & jvmsigs) != 0);
 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 (sig < NSIG && 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;
 189     if (oact != NULL) {
 190       *oact = oldAct;
 191     }
 192 
 193     /* Record the signals used by jvm */
 194     jvmsigs |= MASK(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   jvm_signal_installing = true;
 212   tid = pthread_self();
 213   signal_unlock();
 214 }
 215 
 216 void JVM_end_signal_setting() {
 217   signal_lock();
 218   jvm_signal_installed = true;
 219   jvm_signal_installing = false;
 220   pthread_cond_broadcast(&cond);
 221   signal_unlock();
 222 }
 223 
 224 struct sigaction *JVM_get_signal_action(int sig) {
 225   /* Does race condition make sense here? */
 226   if ((MASK(sig) & jvmsigs) != 0) {
 227     return &sact[sig];
 228   }
 229   return NULL;
 230 }