src/os/solaris/vm/jsig.c

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/os/solaris/vm/jsig.c	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,273 @@
     1.4 +/*
     1.5 + * Copyright 2001-2003 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any 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 <stdlib.h>
    1.37 +#include <stdio.h>
    1.38 +#include <string.h>
    1.39 +#include <signal.h>
    1.40 +#include <dlfcn.h>
    1.41 +#include <thread.h>
    1.42 +#include <synch.h>
    1.43 +#include "jvm_solaris.h"
    1.44 +
    1.45 +#define bool int
    1.46 +#define true 1
    1.47 +#define false 0
    1.48 +
    1.49 +static struct sigaction *sact = (struct sigaction *)NULL; /* saved signal handlers */
    1.50 +static sigset_t jvmsigs;
    1.51 +
    1.52 +/* used to synchronize the installation of signal handlers */
    1.53 +static mutex_t mutex = DEFAULTMUTEX;
    1.54 +static cond_t cond = DEFAULTCV;
    1.55 +static thread_t tid = 0;
    1.56 +
    1.57 +typedef void (*sa_handler_t)(int);
    1.58 +typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
    1.59 +typedef sa_handler_t (*signal_t)(int, sa_handler_t);
    1.60 +typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
    1.61 +
    1.62 +static signal_t os_signal = 0; /* os's version of signal()/sigset() */
    1.63 +static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
    1.64 +
    1.65 +static bool jvm_signal_installing = false;
    1.66 +static bool jvm_signal_installed = false;
    1.67 +
    1.68 +
    1.69 +/* assume called within signal_lock */
    1.70 +static void allocate_sact() {
    1.71 +  size_t maxsignum;
    1.72 +  maxsignum = SIGRTMAX;
    1.73 +  if (sact == NULL) {
    1.74 +    sact = (struct sigaction *)malloc((maxsignum+1) * (size_t)sizeof(struct sigaction));
    1.75 +    memset(sact, 0, (maxsignum+1) * (size_t)sizeof(struct sigaction));
    1.76 +  }
    1.77 +
    1.78 +  if (sact == NULL) {
    1.79 +    printf("%s\n", "libjsig.so unable to allocate memory");
    1.80 +    exit(0);
    1.81 +  }
    1.82 +
    1.83 +  sigemptyset(&jvmsigs);
    1.84 +}
    1.85 +
    1.86 +static void signal_lock() {
    1.87 +  mutex_lock(&mutex);
    1.88 +  /* When the jvm is installing its set of signal handlers, threads
    1.89 +   * other than the jvm thread should wait */
    1.90 +  if (jvm_signal_installing) {
    1.91 +    if (tid != thr_self()) {
    1.92 +      cond_wait(&cond, &mutex);
    1.93 +    }
    1.94 +  }
    1.95 +}
    1.96 +
    1.97 +static void signal_unlock() {
    1.98 +  mutex_unlock(&mutex);
    1.99 +}
   1.100 +
   1.101 +static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
   1.102 +                                   bool is_sigset) {
   1.103 +  if (os_signal == NULL) {
   1.104 +    if (!is_sigset) {
   1.105 +      os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
   1.106 +    } else {
   1.107 +      os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
   1.108 +    }
   1.109 +    if (os_signal == NULL) {
   1.110 +      printf("%s\n", dlerror());
   1.111 +      exit(0);
   1.112 +    }
   1.113 +  }
   1.114 +  return (*os_signal)(sig, disp);
   1.115 +}
   1.116 +
   1.117 +static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
   1.118 +  sigset_t set;
   1.119 +  if (sact == NULL) {
   1.120 +    allocate_sact();
   1.121 +  }
   1.122 +  sact[sig].sa_handler = disp;
   1.123 +  sigemptyset(&set);
   1.124 +  sact[sig].sa_mask = set;
   1.125 +  if (!is_sigset) {
   1.126 +    sact[sig].sa_flags = SA_NODEFER;
   1.127 +    if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
   1.128 +      sact[sig].sa_flags |= SA_RESETHAND;
   1.129 +    }
   1.130 +  } else {
   1.131 +    sact[sig].sa_flags = 0;
   1.132 +  }
   1.133 +}
   1.134 +
   1.135 +static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
   1.136 +  sa_handler_t oldhandler;
   1.137 +  bool sigblocked;
   1.138 +
   1.139 +  signal_lock();
   1.140 +  if (sact == NULL) {
   1.141 +    allocate_sact();
   1.142 +  }
   1.143 +
   1.144 +  if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {
   1.145 +    /* jvm has installed its signal handler for this signal. */
   1.146 +    /* Save the handler. Don't really install it. */
   1.147 +    if (is_sigset) {
   1.148 +      /* We won't honor the SIG_HOLD request to change the signal mask */
   1.149 +      sigblocked = sigismember(&(sact[sig].sa_mask), sig);
   1.150 +    }
   1.151 +    oldhandler = sact[sig].sa_handler;
   1.152 +    save_signal_handler(sig, disp, is_sigset);
   1.153 +
   1.154 +    if (is_sigset && sigblocked) {
   1.155 +      oldhandler = SIG_HOLD;
   1.156 +    }
   1.157 +
   1.158 +    signal_unlock();
   1.159 +    return oldhandler;
   1.160 +  } else if (jvm_signal_installing) {
   1.161 +    /* jvm is installing its signal handlers. Install the new
   1.162 +     * handlers and save the old ones. jvm uses sigaction().
   1.163 +     * Leave the piece here just in case. */
   1.164 +    oldhandler = call_os_signal(sig, disp, is_sigset);
   1.165 +    save_signal_handler(sig, oldhandler, is_sigset);
   1.166 +
   1.167 +    /* Record the signals used by jvm */
   1.168 +    sigaddset(&jvmsigs, sig);
   1.169 +
   1.170 +    signal_unlock();
   1.171 +    return oldhandler;
   1.172 +  } else {
   1.173 +    /* jvm has no relation with this signal (yet). Install the
   1.174 +     * the handler. */
   1.175 +    oldhandler = call_os_signal(sig, disp, is_sigset);
   1.176 +
   1.177 +    signal_unlock();
   1.178 +    return oldhandler;
   1.179 +  }
   1.180 +}
   1.181 +
   1.182 +sa_handler_t signal(int sig, sa_handler_t disp) {
   1.183 +  return set_signal(sig, disp, false);
   1.184 +}
   1.185 +
   1.186 +sa_handler_t sigset(int sig, sa_handler_t disp) {
   1.187 +  return set_signal(sig, disp, true);
   1.188 +}
   1.189 +
   1.190 +static int call_os_sigaction(int sig, const struct sigaction  *act,
   1.191 +                             struct sigaction *oact) {
   1.192 +  if (os_sigaction == NULL) {
   1.193 +    os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
   1.194 +    if (os_sigaction == NULL) {
   1.195 +      printf("%s\n", dlerror());
   1.196 +      exit(0);
   1.197 +    }
   1.198 +  }
   1.199 +  return (*os_sigaction)(sig, act, oact);
   1.200 +}
   1.201 +
   1.202 +int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
   1.203 +  int res;
   1.204 +  struct sigaction oldAct;
   1.205 +
   1.206 +  signal_lock();
   1.207 +
   1.208 +  if (sact == NULL ) {
   1.209 +    allocate_sact();
   1.210 +  }
   1.211 +  if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {
   1.212 +    /* jvm has installed its signal handler for this signal. */
   1.213 +    /* Save the handler. Don't really install it. */
   1.214 +    if (oact != NULL) {
   1.215 +      *oact = sact[sig];
   1.216 +    }
   1.217 +    if (act != NULL) {
   1.218 +      sact[sig] = *act;
   1.219 +    }
   1.220 +
   1.221 +    signal_unlock();
   1.222 +    return 0;
   1.223 +  } else if (jvm_signal_installing) {
   1.224 +    /* jvm is installing its signal handlers. Install the new
   1.225 +     * handlers and save the old ones. */
   1.226 +    res = call_os_sigaction(sig, act, &oldAct);
   1.227 +    sact[sig] = oldAct;
   1.228 +    if (oact != NULL) {
   1.229 +      *oact = oldAct;
   1.230 +    }
   1.231 +
   1.232 +    /* Record the signals used by jvm */
   1.233 +    sigaddset(&jvmsigs, sig);
   1.234 +
   1.235 +    signal_unlock();
   1.236 +    return res;
   1.237 +  } else {
   1.238 +    /* jvm has no relation with this signal (yet). Install the
   1.239 +     * the handler. */
   1.240 +    res = call_os_sigaction(sig, act, oact);
   1.241 +
   1.242 +    signal_unlock();
   1.243 +    return res;
   1.244 +  }
   1.245 +}
   1.246 +
   1.247 +/* The four functions for the jvm to call into */
   1.248 +void JVM_begin_signal_setting() {
   1.249 +  signal_lock();
   1.250 +  jvm_signal_installing = true;
   1.251 +  tid = thr_self();
   1.252 +  signal_unlock();
   1.253 +}
   1.254 +
   1.255 +void JVM_end_signal_setting() {
   1.256 +  signal_lock();
   1.257 +  jvm_signal_installed = true;
   1.258 +  jvm_signal_installing = false;
   1.259 +  cond_broadcast(&cond);
   1.260 +  signal_unlock();
   1.261 +}
   1.262 +
   1.263 +struct sigaction *JVM_get_signal_action(int sig) {
   1.264 +  if (sact == NULL) {
   1.265 +    allocate_sact();
   1.266 +  }
   1.267 +  /* Does race condition make sense here? */
   1.268 +  if (sigismember(&jvmsigs, sig)) {
   1.269 +    return &sact[sig];
   1.270 +  }
   1.271 +  return NULL;
   1.272 +}
   1.273 +
   1.274 +int JVM_get_libjsig_version() {
   1.275 +  return JSIG_VERSION_1_4_1;
   1.276 +}

mercurial