src/os/bsd/vm/jsig.c

changeset 0
f90c822e73f8
child 8034
79841fc03469
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/os/bsd/vm/jsig.c	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,225 @@
     1.4 +/*
     1.5 + * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +/* CopyrightVersion 1.2 */
    1.29 +
    1.30 +/* This is a special library that should be loaded before libc &
    1.31 + * libthread to interpose the signal handler installation functions:
    1.32 + * sigaction(), signal(), sigset().
    1.33 + * Used for signal-chaining. See RFE 4381843.
    1.34 + */
    1.35 +
    1.36 +#include <signal.h>
    1.37 +#include <dlfcn.h>
    1.38 +#include <pthread.h>
    1.39 +#include <stdio.h>
    1.40 +#include <stdlib.h>
    1.41 +#include <stdbool.h>
    1.42 +
    1.43 +#define MAXSIGNUM 32
    1.44 +#define MASK(sig) ((unsigned int)1 << sig)
    1.45 +
    1.46 +static struct sigaction sact[MAXSIGNUM]; /* saved signal handlers */
    1.47 +static unsigned int jvmsigs = 0; /* signals used by jvm */
    1.48 +
    1.49 +/* used to synchronize the installation of signal handlers */
    1.50 +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    1.51 +static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
    1.52 +static pthread_t tid = 0;
    1.53 +
    1.54 +typedef void (*sa_handler_t)(int);
    1.55 +typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
    1.56 +typedef sa_handler_t (*signal_t)(int, sa_handler_t);
    1.57 +typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
    1.58 +
    1.59 +static signal_t os_signal = 0; /* os's version of signal()/sigset() */
    1.60 +static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
    1.61 +
    1.62 +static bool jvm_signal_installing = false;
    1.63 +static bool jvm_signal_installed = false;
    1.64 +
    1.65 +static void signal_lock() {
    1.66 +  pthread_mutex_lock(&mutex);
    1.67 +  /* When the jvm is installing its set of signal handlers, threads
    1.68 +   * other than the jvm thread should wait */
    1.69 +  if (jvm_signal_installing) {
    1.70 +    if (tid != pthread_self()) {
    1.71 +      pthread_cond_wait(&cond, &mutex);
    1.72 +    }
    1.73 +  }
    1.74 +}
    1.75 +
    1.76 +static void signal_unlock() {
    1.77 +  pthread_mutex_unlock(&mutex);
    1.78 +}
    1.79 +
    1.80 +static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
    1.81 +                                   bool is_sigset) {
    1.82 +  if (os_signal == NULL) {
    1.83 +    if (!is_sigset) {
    1.84 +      os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
    1.85 +    } else {
    1.86 +      os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
    1.87 +    }
    1.88 +    if (os_signal == NULL) {
    1.89 +      printf("%s\n", dlerror());
    1.90 +      exit(0);
    1.91 +    }
    1.92 +  }
    1.93 +  return (*os_signal)(sig, disp);
    1.94 +}
    1.95 +
    1.96 +static void save_signal_handler(int sig, sa_handler_t disp) {
    1.97 +  sigset_t set;
    1.98 +  sact[sig].sa_handler = disp;
    1.99 +  sigemptyset(&set);
   1.100 +  sact[sig].sa_mask = set;
   1.101 +  sact[sig].sa_flags = 0;
   1.102 +}
   1.103 +
   1.104 +static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
   1.105 +  sa_handler_t oldhandler;
   1.106 +  bool sigused;
   1.107 +
   1.108 +  signal_lock();
   1.109 +
   1.110 +  sigused = (MASK(sig) & jvmsigs) != 0;
   1.111 +  if (jvm_signal_installed && sigused) {
   1.112 +    /* jvm has installed its signal handler for this signal. */
   1.113 +    /* Save the handler. Don't really install it. */
   1.114 +    oldhandler = sact[sig].sa_handler;
   1.115 +    save_signal_handler(sig, disp);
   1.116 +
   1.117 +    signal_unlock();
   1.118 +    return oldhandler;
   1.119 +  } else if (jvm_signal_installing) {
   1.120 +    /* jvm is installing its signal handlers. Install the new
   1.121 +     * handlers and save the old ones. jvm uses sigaction().
   1.122 +     * Leave the piece here just in case. */
   1.123 +    oldhandler = call_os_signal(sig, disp, is_sigset);
   1.124 +    save_signal_handler(sig, oldhandler);
   1.125 +
   1.126 +    /* Record the signals used by jvm */
   1.127 +    jvmsigs |= MASK(sig);
   1.128 +
   1.129 +    signal_unlock();
   1.130 +    return oldhandler;
   1.131 +  } else {
   1.132 +    /* jvm has no relation with this signal (yet). Install the
   1.133 +     * the handler. */
   1.134 +    oldhandler = call_os_signal(sig, disp, is_sigset);
   1.135 +
   1.136 +    signal_unlock();
   1.137 +    return oldhandler;
   1.138 +  }
   1.139 +}
   1.140 +
   1.141 +sa_handler_t signal(int sig, sa_handler_t disp) {
   1.142 +  return set_signal(sig, disp, false);
   1.143 +}
   1.144 +
   1.145 +sa_handler_t sigset(int sig, sa_handler_t disp) {
   1.146 +  printf("sigset() is not supported by BSD");
   1.147 +  exit(0);
   1.148 + }
   1.149 +
   1.150 +static int call_os_sigaction(int sig, const struct sigaction  *act,
   1.151 +                             struct sigaction *oact) {
   1.152 +  if (os_sigaction == NULL) {
   1.153 +    os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
   1.154 +    if (os_sigaction == NULL) {
   1.155 +      printf("%s\n", dlerror());
   1.156 +      exit(0);
   1.157 +    }
   1.158 +  }
   1.159 +  return (*os_sigaction)(sig, act, oact);
   1.160 +}
   1.161 +
   1.162 +int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
   1.163 +  int res;
   1.164 +  bool sigused;
   1.165 +  struct sigaction oldAct;
   1.166 +
   1.167 +  signal_lock();
   1.168 +
   1.169 +  sigused = (MASK(sig) & jvmsigs) != 0;
   1.170 +  if (jvm_signal_installed && sigused) {
   1.171 +    /* jvm has installed its signal handler for this signal. */
   1.172 +    /* Save the handler. Don't really install it. */
   1.173 +    if (oact != NULL) {
   1.174 +      *oact = sact[sig];
   1.175 +    }
   1.176 +    if (act != NULL) {
   1.177 +      sact[sig] = *act;
   1.178 +    }
   1.179 +
   1.180 +    signal_unlock();
   1.181 +    return 0;
   1.182 +  } else if (jvm_signal_installing) {
   1.183 +    /* jvm is installing its signal handlers. Install the new
   1.184 +     * handlers and save the old ones. */
   1.185 +    res = call_os_sigaction(sig, act, &oldAct);
   1.186 +    sact[sig] = oldAct;
   1.187 +    if (oact != NULL) {
   1.188 +      *oact = oldAct;
   1.189 +    }
   1.190 +
   1.191 +    /* Record the signals used by jvm */
   1.192 +    jvmsigs |= MASK(sig);
   1.193 +
   1.194 +    signal_unlock();
   1.195 +    return res;
   1.196 +  } else {
   1.197 +    /* jvm has no relation with this signal (yet). Install the
   1.198 +     * the handler. */
   1.199 +    res = call_os_sigaction(sig, act, oact);
   1.200 +
   1.201 +    signal_unlock();
   1.202 +    return res;
   1.203 +  }
   1.204 +}
   1.205 +
   1.206 +/* The three functions for the jvm to call into */
   1.207 +void JVM_begin_signal_setting() {
   1.208 +  signal_lock();
   1.209 +  jvm_signal_installing = true;
   1.210 +  tid = pthread_self();
   1.211 +  signal_unlock();
   1.212 +}
   1.213 +
   1.214 +void JVM_end_signal_setting() {
   1.215 +  signal_lock();
   1.216 +  jvm_signal_installed = true;
   1.217 +  jvm_signal_installing = false;
   1.218 +  pthread_cond_broadcast(&cond);
   1.219 +  signal_unlock();
   1.220 +}
   1.221 +
   1.222 +struct sigaction *JVM_get_signal_action(int sig) {
   1.223 +  /* Does race condition make sense here? */
   1.224 +  if ((MASK(sig) & jvmsigs) != 0) {
   1.225 +    return &sact[sig];
   1.226 +  }
   1.227 +  return NULL;
   1.228 +}

mercurial