src/java.base/macosx/native/libjsig/jsig.c
branchepsilon-gc-branch
changeset 56422 b09629f4b243
parent 56407 dddf5c49f4fc
parent 49681 4beba2c2a329
child 56448 76d86de267b9
equal deleted inserted replaced
56407:dddf5c49f4fc 56422:b09629f4b243
     1 /*
       
     2  * Copyright (c) 2001, 2015, 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 <stdbool.h>
       
    39 #include <stdint.h>
       
    40 
       
    41 #define MASK(sig) ((uint32_t)1 << (sig-1))  // 0 is not a signal.
       
    42 #if (32 < NSIG-1)
       
    43 #error "Not all signals can be encoded in jvmsigs. Adapt its type!"
       
    44 #endif
       
    45 static struct sigaction sact[NSIG]; /* saved signal handlers */
       
    46 static uint32_t jvmsigs = 0; /* signals used by jvm */
       
    47 static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */
       
    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 typedef sa_handler_t (*signal_t)(int, sa_handler_t);
       
    57 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
       
    58 
       
    59 static signal_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   sa_handler_t res;
       
    83 
       
    84   if (os_signal == NULL) {
       
    85     if (!is_sigset) {
       
    86       os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
       
    87     } else {
       
    88       os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
       
    89     }
       
    90     if (os_signal == NULL) {
       
    91       printf("%s\n", dlerror());
       
    92       exit(0);
       
    93     }
       
    94   }
       
    95   reentry = true;
       
    96   res = (*os_signal)(sig, disp);
       
    97   reentry = false;
       
    98   return res;
       
    99 }
       
   100 
       
   101 static void save_signal_handler(int sig, sa_handler_t disp) {
       
   102   sigset_t set;
       
   103   sact[sig].sa_handler = disp;
       
   104   sigemptyset(&set);
       
   105   sact[sig].sa_mask = set;
       
   106   sact[sig].sa_flags = 0;
       
   107 }
       
   108 
       
   109 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
       
   110   sa_handler_t oldhandler;
       
   111   bool sigused;
       
   112 
       
   113   signal_lock();
       
   114 
       
   115   sigused = (MASK(sig) & jvmsigs) != 0;
       
   116   if (jvm_signal_installed && sigused) {
       
   117     /* jvm has installed its signal handler for this signal. */
       
   118     /* Save the handler. Don't really install it. */
       
   119     oldhandler = sact[sig].sa_handler;
       
   120     save_signal_handler(sig, disp);
       
   121 
       
   122     signal_unlock();
       
   123     return oldhandler;
       
   124   } else if (jvm_signal_installing) {
       
   125     /* jvm is installing its signal handlers. Install the new
       
   126      * handlers and save the old ones. jvm uses sigaction().
       
   127      * Leave the piece here just in case. */
       
   128     oldhandler = call_os_signal(sig, disp, is_sigset);
       
   129     save_signal_handler(sig, oldhandler);
       
   130 
       
   131     /* Record the signals used by jvm */
       
   132     jvmsigs |= MASK(sig);
       
   133 
       
   134     signal_unlock();
       
   135     return oldhandler;
       
   136   } else {
       
   137     /* jvm has no relation with this signal (yet). Install the
       
   138      * the handler. */
       
   139     oldhandler = call_os_signal(sig, disp, is_sigset);
       
   140 
       
   141     signal_unlock();
       
   142     return oldhandler;
       
   143   }
       
   144 }
       
   145 
       
   146 sa_handler_t signal(int sig, sa_handler_t disp) {
       
   147   return set_signal(sig, disp, false);
       
   148 }
       
   149 
       
   150 sa_handler_t sigset(int sig, sa_handler_t disp) {
       
   151   printf("sigset() is not supported by BSD");
       
   152   exit(0);
       
   153  }
       
   154 
       
   155 static int call_os_sigaction(int sig, const struct sigaction  *act,
       
   156                              struct sigaction *oact) {
       
   157   if (os_sigaction == NULL) {
       
   158     os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "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 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
       
   168   int res;
       
   169   bool sigused;
       
   170   struct sigaction oldAct;
       
   171 
       
   172   if (reentry) {
       
   173     return call_os_sigaction(sig, act, oact);
       
   174   }
       
   175 
       
   176   signal_lock();
       
   177 
       
   178   sigused = (MASK(sig) & jvmsigs) != 0;
       
   179   if (jvm_signal_installed && sigused) {
       
   180     /* jvm has installed its signal handler for this signal. */
       
   181     /* Save the handler. Don't really install it. */
       
   182     if (oact != NULL) {
       
   183       *oact = sact[sig];
       
   184     }
       
   185     if (act != NULL) {
       
   186       sact[sig] = *act;
       
   187     }
       
   188 
       
   189     signal_unlock();
       
   190     return 0;
       
   191   } else if (jvm_signal_installing) {
       
   192     /* jvm is installing its signal handlers. Install the new
       
   193      * handlers and save the old ones. */
       
   194     res = call_os_sigaction(sig, act, &oldAct);
       
   195     sact[sig] = oldAct;
       
   196     if (oact != NULL) {
       
   197       *oact = oldAct;
       
   198     }
       
   199 
       
   200     /* Record the signals used by jvm */
       
   201     jvmsigs |= MASK(sig);
       
   202 
       
   203     signal_unlock();
       
   204     return res;
       
   205   } else {
       
   206     /* jvm has no relation with this signal (yet). Install the
       
   207      * the handler. */
       
   208     res = call_os_sigaction(sig, act, oact);
       
   209 
       
   210     signal_unlock();
       
   211     return res;
       
   212   }
       
   213 }
       
   214 
       
   215 /* The three functions for the jvm to call into */
       
   216 void JVM_begin_signal_setting() {
       
   217   signal_lock();
       
   218   jvm_signal_installing = true;
       
   219   tid = pthread_self();
       
   220   signal_unlock();
       
   221 }
       
   222 
       
   223 void 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 struct sigaction *JVM_get_signal_action(int sig) {
       
   232   /* Does race condition make sense here? */
       
   233   if ((MASK(sig) & jvmsigs) != 0) {
       
   234     return &sact[sig];
       
   235   }
       
   236   return NULL;
       
   237 }