src/os/bsd/vm/jsig.c

Wed, 27 Feb 2013 09:40:30 +0100

author
simonis
date
Wed, 27 Feb 2013 09:40:30 +0100
changeset 4675
63e54c37ac64
parent 0
f90c822e73f8
child 8034
79841fc03469
permissions
-rw-r--r--

8008959: Fix non-PCH build on Linux, Windows and MacOS X
Summary: Fix the build without precompiled headers by either including the missing ".inline.hpp" files into the appropriate files or by turning inline-functions declared in header files into ordinary functions in ".cpp" files.
Reviewed-by: coleenp, stefank, dholmes

     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  */
    25 /* CopyrightVersion 1.2 */
    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  */
    33 #include <signal.h>
    34 #include <dlfcn.h>
    35 #include <pthread.h>
    36 #include <stdio.h>
    37 #include <stdlib.h>
    38 #include <stdbool.h>
    40 #define MAXSIGNUM 32
    41 #define MASK(sig) ((unsigned int)1 << sig)
    43 static struct sigaction sact[MAXSIGNUM]; /* saved signal handlers */
    44 static unsigned int jvmsigs = 0; /* signals used by jvm */
    46 /* used to synchronize the installation of signal handlers */
    47 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    48 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
    49 static pthread_t tid = 0;
    51 typedef void (*sa_handler_t)(int);
    52 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
    53 typedef sa_handler_t (*signal_t)(int, sa_handler_t);
    54 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
    56 static signal_t os_signal = 0; /* os's version of signal()/sigset() */
    57 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
    59 static bool jvm_signal_installing = false;
    60 static bool jvm_signal_installed = false;
    62 static void signal_lock() {
    63   pthread_mutex_lock(&mutex);
    64   /* When the jvm is installing its set of signal handlers, threads
    65    * other than the jvm thread should wait */
    66   if (jvm_signal_installing) {
    67     if (tid != pthread_self()) {
    68       pthread_cond_wait(&cond, &mutex);
    69     }
    70   }
    71 }
    73 static void signal_unlock() {
    74   pthread_mutex_unlock(&mutex);
    75 }
    77 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
    78                                    bool is_sigset) {
    79   if (os_signal == NULL) {
    80     if (!is_sigset) {
    81       os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
    82     } else {
    83       os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
    84     }
    85     if (os_signal == NULL) {
    86       printf("%s\n", dlerror());
    87       exit(0);
    88     }
    89   }
    90   return (*os_signal)(sig, disp);
    91 }
    93 static void save_signal_handler(int sig, sa_handler_t disp) {
    94   sigset_t set;
    95   sact[sig].sa_handler = disp;
    96   sigemptyset(&set);
    97   sact[sig].sa_mask = set;
    98   sact[sig].sa_flags = 0;
    99 }
   101 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
   102   sa_handler_t oldhandler;
   103   bool sigused;
   105   signal_lock();
   107   sigused = (MASK(sig) & jvmsigs) != 0;
   108   if (jvm_signal_installed && sigused) {
   109     /* jvm has installed its signal handler for this signal. */
   110     /* Save the handler. Don't really install it. */
   111     oldhandler = sact[sig].sa_handler;
   112     save_signal_handler(sig, disp);
   114     signal_unlock();
   115     return oldhandler;
   116   } else if (jvm_signal_installing) {
   117     /* jvm is installing its signal handlers. Install the new
   118      * handlers and save the old ones. jvm uses sigaction().
   119      * Leave the piece here just in case. */
   120     oldhandler = call_os_signal(sig, disp, is_sigset);
   121     save_signal_handler(sig, oldhandler);
   123     /* Record the signals used by jvm */
   124     jvmsigs |= MASK(sig);
   126     signal_unlock();
   127     return oldhandler;
   128   } else {
   129     /* jvm has no relation with this signal (yet). Install the
   130      * the handler. */
   131     oldhandler = call_os_signal(sig, disp, is_sigset);
   133     signal_unlock();
   134     return oldhandler;
   135   }
   136 }
   138 sa_handler_t signal(int sig, sa_handler_t disp) {
   139   return set_signal(sig, disp, false);
   140 }
   142 sa_handler_t sigset(int sig, sa_handler_t disp) {
   143   printf("sigset() is not supported by BSD");
   144   exit(0);
   145  }
   147 static int call_os_sigaction(int sig, const struct sigaction  *act,
   148                              struct sigaction *oact) {
   149   if (os_sigaction == NULL) {
   150     os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
   151     if (os_sigaction == NULL) {
   152       printf("%s\n", dlerror());
   153       exit(0);
   154     }
   155   }
   156   return (*os_sigaction)(sig, act, oact);
   157 }
   159 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
   160   int res;
   161   bool sigused;
   162   struct sigaction oldAct;
   164   signal_lock();
   166   sigused = (MASK(sig) & jvmsigs) != 0;
   167   if (jvm_signal_installed && sigused) {
   168     /* jvm has installed its signal handler for this signal. */
   169     /* Save the handler. Don't really install it. */
   170     if (oact != NULL) {
   171       *oact = sact[sig];
   172     }
   173     if (act != NULL) {
   174       sact[sig] = *act;
   175     }
   177     signal_unlock();
   178     return 0;
   179   } else if (jvm_signal_installing) {
   180     /* jvm is installing its signal handlers. Install the new
   181      * handlers and save the old ones. */
   182     res = call_os_sigaction(sig, act, &oldAct);
   183     sact[sig] = oldAct;
   184     if (oact != NULL) {
   185       *oact = oldAct;
   186     }
   188     /* Record the signals used by jvm */
   189     jvmsigs |= MASK(sig);
   191     signal_unlock();
   192     return res;
   193   } else {
   194     /* jvm has no relation with this signal (yet). Install the
   195      * the handler. */
   196     res = call_os_sigaction(sig, act, oact);
   198     signal_unlock();
   199     return res;
   200   }
   201 }
   203 /* The three functions for the jvm to call into */
   204 void JVM_begin_signal_setting() {
   205   signal_lock();
   206   jvm_signal_installing = true;
   207   tid = pthread_self();
   208   signal_unlock();
   209 }
   211 void JVM_end_signal_setting() {
   212   signal_lock();
   213   jvm_signal_installed = true;
   214   jvm_signal_installing = false;
   215   pthread_cond_broadcast(&cond);
   216   signal_unlock();
   217 }
   219 struct sigaction *JVM_get_signal_action(int sig) {
   220   /* Does race condition make sense here? */
   221   if ((MASK(sig) & jvmsigs) != 0) {
   222     return &sact[sig];
   223   }
   224   return NULL;
   225 }

mercurial