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 #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() */
  61 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
  62 
  63 static bool jvm_signal_installing = false;
  64 static bool jvm_signal_installed = false;
  65 
  66 static void signal_lock() {
  67   pthread_mutex_lock(&mutex);
  68   /* When the jvm is installing its set of signal handlers, threads
  69    * other than the jvm thread should wait. */
  70   if (jvm_signal_installing) {
  71     if (tid != pthread_self()) {
  72       pthread_cond_wait(&cond, &mutex);
  73     }
  74   }
  75 }
  76 
  77 static void signal_unlock() {
  78   pthread_mutex_unlock(&mutex);
  79 }
  80 
  81 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
  82                                    bool is_sigset) {
  83   if (os_signal == NULL) {
  84     if (!is_sigset) {
  85       // Aix: call functions directly instead of dlsym'ing them.
  86       os_signal = signal;
  87     } else {
  88       // Aix: call functions directly instead of dlsym'ing them.
  89       os_signal = 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 = sigismember(&jvmsigs, sig);
 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 (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     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) {
 189     /* jvm is installing its signal handlers. Install the new
 190      * handlers and save the old ones. */
 191     res = call_os_sigaction(sig, act, &oldAct);
 192     sact[sig] = oldAct;
 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 }