0N/A/*
1879N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/A/* CopyrightVersion 1.2 */
0N/A
0N/A/* This is a special library that should be loaded before libc &
0N/A * libthread to interpose the signal handler installation functions:
0N/A * sigaction(), signal(), sigset().
0N/A * Used for signal-chaining. See RFE 4381843.
0N/A */
0N/A
0N/A#include <stdlib.h>
0N/A#include <stdio.h>
0N/A#include <string.h>
0N/A#include <signal.h>
0N/A#include <dlfcn.h>
0N/A#include <thread.h>
0N/A#include <synch.h>
0N/A#include "jvm_solaris.h"
0N/A
0N/A#define bool int
0N/A#define true 1
0N/A#define false 0
0N/A
0N/Astatic struct sigaction *sact = (struct sigaction *)NULL; /* saved signal handlers */
0N/Astatic sigset_t jvmsigs;
0N/A
0N/A/* used to synchronize the installation of signal handlers */
0N/Astatic mutex_t mutex = DEFAULTMUTEX;
0N/Astatic cond_t cond = DEFAULTCV;
0N/Astatic thread_t tid = 0;
0N/A
0N/Atypedef void (*sa_handler_t)(int);
0N/Atypedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
0N/Atypedef sa_handler_t (*signal_t)(int, sa_handler_t);
0N/Atypedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
0N/A
0N/Astatic signal_t os_signal = 0; /* os's version of signal()/sigset() */
0N/Astatic sigaction_t os_sigaction = 0; /* os's version of sigaction() */
0N/A
0N/Astatic bool jvm_signal_installing = false;
0N/Astatic bool jvm_signal_installed = false;
0N/A
0N/A
0N/A/* assume called within signal_lock */
0N/Astatic void allocate_sact() {
0N/A size_t maxsignum;
0N/A maxsignum = SIGRTMAX;
0N/A if (sact == NULL) {
0N/A sact = (struct sigaction *)malloc((maxsignum+1) * (size_t)sizeof(struct sigaction));
0N/A memset(sact, 0, (maxsignum+1) * (size_t)sizeof(struct sigaction));
0N/A }
0N/A
0N/A if (sact == NULL) {
0N/A printf("%s\n", "libjsig.so unable to allocate memory");
0N/A exit(0);
0N/A }
0N/A
0N/A sigemptyset(&jvmsigs);
0N/A}
0N/A
0N/Astatic void signal_lock() {
0N/A mutex_lock(&mutex);
0N/A /* When the jvm is installing its set of signal handlers, threads
0N/A * other than the jvm thread should wait */
0N/A if (jvm_signal_installing) {
0N/A if (tid != thr_self()) {
0N/A cond_wait(&cond, &mutex);
0N/A }
0N/A }
0N/A}
0N/A
0N/Astatic void signal_unlock() {
0N/A mutex_unlock(&mutex);
0N/A}
0N/A
0N/Astatic sa_handler_t call_os_signal(int sig, sa_handler_t disp,
0N/A bool is_sigset) {
0N/A if (os_signal == NULL) {
0N/A if (!is_sigset) {
0N/A os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
0N/A } else {
0N/A os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
0N/A }
0N/A if (os_signal == NULL) {
0N/A printf("%s\n", dlerror());
0N/A exit(0);
0N/A }
0N/A }
0N/A return (*os_signal)(sig, disp);
0N/A}
0N/A
0N/Astatic void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
0N/A sigset_t set;
0N/A if (sact == NULL) {
0N/A allocate_sact();
0N/A }
0N/A sact[sig].sa_handler = disp;
0N/A sigemptyset(&set);
0N/A sact[sig].sa_mask = set;
0N/A if (!is_sigset) {
0N/A sact[sig].sa_flags = SA_NODEFER;
0N/A if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
0N/A sact[sig].sa_flags |= SA_RESETHAND;
0N/A }
0N/A } else {
0N/A sact[sig].sa_flags = 0;
0N/A }
0N/A}
0N/A
0N/Astatic sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
0N/A sa_handler_t oldhandler;
0N/A bool sigblocked;
0N/A
0N/A signal_lock();
0N/A if (sact == NULL) {
0N/A allocate_sact();
0N/A }
0N/A
0N/A if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {
0N/A /* jvm has installed its signal handler for this signal. */
0N/A /* Save the handler. Don't really install it. */
0N/A if (is_sigset) {
0N/A /* We won't honor the SIG_HOLD request to change the signal mask */
0N/A sigblocked = sigismember(&(sact[sig].sa_mask), sig);
0N/A }
0N/A oldhandler = sact[sig].sa_handler;
0N/A save_signal_handler(sig, disp, is_sigset);
0N/A
0N/A if (is_sigset && sigblocked) {
0N/A oldhandler = SIG_HOLD;
0N/A }
0N/A
0N/A signal_unlock();
0N/A return oldhandler;
0N/A } else if (jvm_signal_installing) {
0N/A /* jvm is installing its signal handlers. Install the new
0N/A * handlers and save the old ones. jvm uses sigaction().
0N/A * Leave the piece here just in case. */
0N/A oldhandler = call_os_signal(sig, disp, is_sigset);
0N/A save_signal_handler(sig, oldhandler, is_sigset);
0N/A
0N/A /* Record the signals used by jvm */
0N/A sigaddset(&jvmsigs, sig);
0N/A
0N/A signal_unlock();
0N/A return oldhandler;
0N/A } else {
0N/A /* jvm has no relation with this signal (yet). Install the
0N/A * the handler. */
0N/A oldhandler = call_os_signal(sig, disp, is_sigset);
0N/A
0N/A signal_unlock();
0N/A return oldhandler;
0N/A }
0N/A}
0N/A
0N/Asa_handler_t signal(int sig, sa_handler_t disp) {
0N/A return set_signal(sig, disp, false);
0N/A}
0N/A
0N/Asa_handler_t sigset(int sig, sa_handler_t disp) {
0N/A return set_signal(sig, disp, true);
0N/A}
0N/A
0N/Astatic int call_os_sigaction(int sig, const struct sigaction *act,
0N/A struct sigaction *oact) {
0N/A if (os_sigaction == NULL) {
0N/A os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
0N/A if (os_sigaction == NULL) {
0N/A printf("%s\n", dlerror());
0N/A exit(0);
0N/A }
0N/A }
0N/A return (*os_sigaction)(sig, act, oact);
0N/A}
0N/A
0N/Aint sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
0N/A int res;
0N/A struct sigaction oldAct;
0N/A
0N/A signal_lock();
0N/A
0N/A if (sact == NULL ) {
0N/A allocate_sact();
0N/A }
0N/A if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {
0N/A /* jvm has installed its signal handler for this signal. */
0N/A /* Save the handler. Don't really install it. */
0N/A if (oact != NULL) {
0N/A *oact = sact[sig];
0N/A }
0N/A if (act != NULL) {
0N/A sact[sig] = *act;
0N/A }
0N/A
0N/A signal_unlock();
0N/A return 0;
0N/A } else if (jvm_signal_installing) {
0N/A /* jvm is installing its signal handlers. Install the new
0N/A * handlers and save the old ones. */
0N/A res = call_os_sigaction(sig, act, &oldAct);
0N/A sact[sig] = oldAct;
0N/A if (oact != NULL) {
0N/A *oact = oldAct;
0N/A }
0N/A
0N/A /* Record the signals used by jvm */
0N/A sigaddset(&jvmsigs, sig);
0N/A
0N/A signal_unlock();
0N/A return res;
0N/A } else {
0N/A /* jvm has no relation with this signal (yet). Install the
0N/A * the handler. */
0N/A res = call_os_sigaction(sig, act, oact);
0N/A
0N/A signal_unlock();
0N/A return res;
0N/A }
0N/A}
0N/A
0N/A/* The four functions for the jvm to call into */
0N/Avoid JVM_begin_signal_setting() {
0N/A signal_lock();
0N/A jvm_signal_installing = true;
0N/A tid = thr_self();
0N/A signal_unlock();
0N/A}
0N/A
0N/Avoid JVM_end_signal_setting() {
0N/A signal_lock();
0N/A jvm_signal_installed = true;
0N/A jvm_signal_installing = false;
0N/A cond_broadcast(&cond);
0N/A signal_unlock();
0N/A}
0N/A
0N/Astruct sigaction *JVM_get_signal_action(int sig) {
0N/A if (sact == NULL) {
0N/A allocate_sact();
0N/A }
0N/A /* Does race condition make sense here? */
0N/A if (sigismember(&jvmsigs, sig)) {
0N/A return &sact[sig];
0N/A }
0N/A return NULL;
0N/A}
0N/A
0N/Aint JVM_get_libjsig_version() {
0N/A return JSIG_VERSION_1_4_1;
0N/A}