1 /*
   2  * Copyright (c) 2001, 2010, 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 <stdlib.h>
  34 #include <stdio.h>
  35 #include <string.h>
  36 #include <signal.h>
  37 #include <dlfcn.h>
  38 #include <thread.h>
  39 #include <synch.h>
  40 #include "jni.h"
  41 #include "jvm_md.h"
  42 
  43 #define bool int
  44 #define true 1
  45 #define false 0
  46 
  47 static struct sigaction *sact = (struct sigaction *)NULL; /* saved signal handlers */
  48 static sigset_t jvmsigs;
  49 
  50 /* used to synchronize the installation of signal handlers */
  51 static mutex_t mutex = DEFAULTMUTEX;
  52 static cond_t cond = DEFAULTCV;
  53 static thread_t tid = 0;
  54 
  55 typedef void (*sa_handler_t)(int);
  56 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  57 typedef sa_handler_t (*signal_t)(int, sa_handler_t);
  58 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
  59 
  60 static signal_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 
  67 /* assume called within signal_lock */
  68 static void allocate_sact() {
  69   size_t maxsignum;
  70   maxsignum = SIGRTMAX;
  71   if (sact == NULL) {
  72     sact = (struct sigaction *)malloc((maxsignum+1) * (size_t)sizeof(struct sigaction));
  73     memset(sact, 0, (maxsignum+1) * (size_t)sizeof(struct sigaction));
  74   }
  75 
  76   if (sact == NULL) {
  77     printf("%s\n", "libjsig.so unable to allocate memory");
  78     exit(0);
  79   }
  80 
  81   sigemptyset(&jvmsigs);
  82 }
  83 
  84 static void signal_lock() {
  85   mutex_lock(&mutex);
  86   /* When the jvm is installing its set of signal handlers, threads
  87    * other than the jvm thread should wait */
  88   if (jvm_signal_installing) {
  89     if (tid != thr_self()) {
  90       cond_wait(&cond, &mutex);
  91     }
  92   }
  93 }
  94 
  95 static void signal_unlock() {
  96   mutex_unlock(&mutex);
  97 }
  98 
  99 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
 100                                    bool is_sigset) {
 101   if (os_signal == NULL) {
 102     if (!is_sigset) {
 103       os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
 104     } else {
 105       os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
 106     }
 107     if (os_signal == NULL) {
 108       printf("%s\n", dlerror());
 109       exit(0);
 110     }
 111   }
 112   return (*os_signal)(sig, disp);
 113 }
 114 
 115 static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
 116   sigset_t set;
 117   if (sact == NULL) {
 118     allocate_sact();
 119   }
 120   sact[sig].sa_handler = disp;
 121   sigemptyset(&set);
 122   sact[sig].sa_mask = set;
 123   if (!is_sigset) {
 124     sact[sig].sa_flags = SA_NODEFER;
 125     if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
 126       sact[sig].sa_flags |= SA_RESETHAND;
 127     }
 128   } else {
 129     sact[sig].sa_flags = 0;
 130   }
 131 }
 132 
 133 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
 134   sa_handler_t oldhandler;
 135   bool sigblocked;
 136 
 137   signal_lock();
 138   if (sact == NULL) {
 139     allocate_sact();
 140   }
 141 
 142   if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {
 143     /* jvm has installed its signal handler for this signal. */
 144     /* Save the handler. Don't really install it. */
 145     if (is_sigset) {
 146       /* We won't honor the SIG_HOLD request to change the signal mask */
 147       sigblocked = sigismember(&(sact[sig].sa_mask), sig);
 148     }
 149     oldhandler = sact[sig].sa_handler;
 150     save_signal_handler(sig, disp, is_sigset);
 151 
 152     if (is_sigset && sigblocked) {
 153       oldhandler = SIG_HOLD;
 154     }
 155 
 156     signal_unlock();
 157     return oldhandler;
 158   } else if (jvm_signal_installing) {
 159     /* jvm is installing its signal handlers. Install the new
 160      * handlers and save the old ones. jvm uses sigaction().
 161      * Leave the piece here just in case. */
 162     oldhandler = call_os_signal(sig, disp, is_sigset);
 163     save_signal_handler(sig, oldhandler, is_sigset);
 164 
 165     /* Record the signals used by jvm */
 166     sigaddset(&jvmsigs, sig);
 167 
 168     signal_unlock();
 169     return oldhandler;
 170   } else {
 171     /* jvm has no relation with this signal (yet). Install the
 172      * the handler. */
 173     oldhandler = call_os_signal(sig, disp, is_sigset);
 174 
 175     signal_unlock();
 176     return oldhandler;
 177   }
 178 }
 179 
 180 sa_handler_t signal(int sig, sa_handler_t disp) {
 181   return set_signal(sig, disp, false);
 182 }
 183 
 184 sa_handler_t sigset(int sig, sa_handler_t disp) {
 185   return set_signal(sig, disp, true);
 186 }
 187 
 188 static int call_os_sigaction(int sig, const struct sigaction  *act,
 189                              struct sigaction *oact) {
 190   if (os_sigaction == NULL) {
 191     os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
 192     if (os_sigaction == NULL) {
 193       printf("%s\n", dlerror());
 194       exit(0);
 195     }
 196   }
 197   return (*os_sigaction)(sig, act, oact);
 198 }
 199 
 200 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
 201   int res;
 202   struct sigaction oldAct;
 203 
 204   signal_lock();
 205 
 206   if (sact == NULL ) {
 207     allocate_sact();
 208   }
 209   if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {
 210     /* jvm has installed its signal handler for this signal. */
 211     /* Save the handler. Don't really install it. */
 212     if (oact != NULL) {
 213       *oact = sact[sig];
 214     }
 215     if (act != NULL) {
 216       sact[sig] = *act;
 217     }
 218 
 219     signal_unlock();
 220     return 0;
 221   } else if (jvm_signal_installing) {
 222     /* jvm is installing its signal handlers. Install the new
 223      * handlers and save the old ones. */
 224     res = call_os_sigaction(sig, act, &oldAct);
 225     sact[sig] = oldAct;
 226     if (oact != NULL) {
 227       *oact = oldAct;
 228     }
 229 
 230     /* Record the signals used by jvm */
 231     sigaddset(&jvmsigs, sig);
 232 
 233     signal_unlock();
 234     return res;
 235   } else {
 236     /* jvm has no relation with this signal (yet). Install the
 237      * the handler. */
 238     res = call_os_sigaction(sig, act, oact);
 239 
 240     signal_unlock();
 241     return res;
 242   }
 243 }
 244 
 245 /* The four functions for the jvm to call into */
 246 JNIEXPORT void JNICALL
 247 JVM_begin_signal_setting() {
 248   signal_lock();
 249   jvm_signal_installing = true;
 250   tid = thr_self();
 251   signal_unlock();
 252 }
 253 
 254 JNIEXPORT void JNICALL
 255 JVM_end_signal_setting() {
 256   signal_lock();
 257   jvm_signal_installed = true;
 258   jvm_signal_installing = false;
 259   cond_broadcast(&cond);
 260   signal_unlock();
 261 }
 262 
 263 JNIEXPORT struct sigaction * JNICALL
 264 JVM_get_signal_action(int sig) {
 265   if (sact == NULL) {
 266     allocate_sact();
 267   }
 268   /* Does race condition make sense here? */
 269   if (sigismember(&jvmsigs, sig)) {
 270     return &sact[sig];
 271   }
 272   return NULL;
 273 }
 274 
 275 JNIEXPORT int JNICALL
 276 JVM_get_libjsig_version() {
 277   return JSIG_VERSION_1_4_1;
 278 }