2796N/A/*
2796N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2796N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2796N/A *
2796N/A * This code is free software; you can redistribute it and/or modify it
2796N/A * under the terms of the GNU General Public License version 2 only, as
2796N/A * published by the Free Software Foundation.
2796N/A *
2796N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2796N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2796N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2796N/A * version 2 for more details (a copy is included in the LICENSE file that
2796N/A * accompanied this code).
2796N/A *
2796N/A * You should have received a copy of the GNU General Public License version
2796N/A * 2 along with this work; if not, write to the Free Software Foundation,
2796N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2796N/A *
2796N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2796N/A * or visit www.oracle.com if you need additional information or have any
2796N/A * questions.
2796N/A *
2796N/A */
2796N/A
2796N/A/* CopyrightVersion 1.2 */
2796N/A
2796N/A/* This is a special library that should be loaded before libc &
2796N/A * libthread to interpose the signal handler installation functions:
2796N/A * sigaction(), signal(), sigset().
2796N/A * Used for signal-chaining. See RFE 4381843.
2796N/A */
2796N/A
2796N/A#include <signal.h>
2796N/A#include <dlfcn.h>
2796N/A#include <pthread.h>
2796N/A#include <stdio.h>
2796N/A#include <stdlib.h>
2796N/A#include <stdbool.h>
2796N/A
2796N/A#define MAXSIGNUM 32
2796N/A#define MASK(sig) ((unsigned int)1 << sig)
2796N/A
2796N/Astatic struct sigaction sact[MAXSIGNUM]; /* saved signal handlers */
2796N/Astatic unsigned int jvmsigs = 0; /* signals used by jvm */
2796N/A
2796N/A/* used to synchronize the installation of signal handlers */
2796N/Astatic pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
2796N/Astatic pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
2796N/Astatic pthread_t tid = 0;
2796N/A
2796N/Atypedef void (*sa_handler_t)(int);
2796N/Atypedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
2796N/Atypedef sa_handler_t (*signal_t)(int, sa_handler_t);
2796N/Atypedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
2796N/A
2796N/Astatic signal_t os_signal = 0; /* os's version of signal()/sigset() */
2796N/Astatic sigaction_t os_sigaction = 0; /* os's version of sigaction() */
2796N/A
2796N/Astatic bool jvm_signal_installing = false;
2796N/Astatic bool jvm_signal_installed = false;
2796N/A
2796N/Astatic void signal_lock() {
2796N/A pthread_mutex_lock(&mutex);
2796N/A /* When the jvm is installing its set of signal handlers, threads
2796N/A * other than the jvm thread should wait */
2796N/A if (jvm_signal_installing) {
2796N/A if (tid != pthread_self()) {
2796N/A pthread_cond_wait(&cond, &mutex);
2796N/A }
2796N/A }
2796N/A}
2796N/A
2796N/Astatic void signal_unlock() {
2796N/A pthread_mutex_unlock(&mutex);
2796N/A}
2796N/A
2796N/Astatic sa_handler_t call_os_signal(int sig, sa_handler_t disp,
2796N/A bool is_sigset) {
2796N/A if (os_signal == NULL) {
2796N/A if (!is_sigset) {
2796N/A os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
2796N/A } else {
2796N/A os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
2796N/A }
2796N/A if (os_signal == NULL) {
2796N/A printf("%s\n", dlerror());
2796N/A exit(0);
2796N/A }
2796N/A }
2796N/A return (*os_signal)(sig, disp);
2796N/A}
2796N/A
2796N/Astatic void save_signal_handler(int sig, sa_handler_t disp) {
2796N/A sigset_t set;
2796N/A sact[sig].sa_handler = disp;
2796N/A sigemptyset(&set);
2796N/A sact[sig].sa_mask = set;
2796N/A sact[sig].sa_flags = 0;
2796N/A}
2796N/A
2796N/Astatic sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
2796N/A sa_handler_t oldhandler;
2796N/A bool sigused;
2796N/A
2796N/A signal_lock();
2796N/A
2796N/A sigused = (MASK(sig) & jvmsigs) != 0;
2796N/A if (jvm_signal_installed && sigused) {
2796N/A /* jvm has installed its signal handler for this signal. */
2796N/A /* Save the handler. Don't really install it. */
2796N/A oldhandler = sact[sig].sa_handler;
2796N/A save_signal_handler(sig, disp);
2796N/A
2796N/A signal_unlock();
2796N/A return oldhandler;
2796N/A } else if (jvm_signal_installing) {
2796N/A /* jvm is installing its signal handlers. Install the new
2796N/A * handlers and save the old ones. jvm uses sigaction().
2796N/A * Leave the piece here just in case. */
2796N/A oldhandler = call_os_signal(sig, disp, is_sigset);
2796N/A save_signal_handler(sig, oldhandler);
2796N/A
2796N/A /* Record the signals used by jvm */
2796N/A jvmsigs |= MASK(sig);
2796N/A
2796N/A signal_unlock();
2796N/A return oldhandler;
2796N/A } else {
2796N/A /* jvm has no relation with this signal (yet). Install the
2796N/A * the handler. */
2796N/A oldhandler = call_os_signal(sig, disp, is_sigset);
2796N/A
2796N/A signal_unlock();
2796N/A return oldhandler;
2796N/A }
2796N/A}
2796N/A
2796N/Asa_handler_t signal(int sig, sa_handler_t disp) {
2796N/A return set_signal(sig, disp, false);
2796N/A}
2796N/A
2796N/Asa_handler_t sigset(int sig, sa_handler_t disp) {
2796N/A printf("sigset() is not supported by BSD");
2796N/A exit(0);
2796N/A }
2796N/A
2796N/Astatic int call_os_sigaction(int sig, const struct sigaction *act,
2796N/A struct sigaction *oact) {
2796N/A if (os_sigaction == NULL) {
2796N/A os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
2796N/A if (os_sigaction == NULL) {
2796N/A printf("%s\n", dlerror());
2796N/A exit(0);
2796N/A }
2796N/A }
2796N/A return (*os_sigaction)(sig, act, oact);
2796N/A}
2796N/A
2796N/Aint sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
2796N/A int res;
2796N/A bool sigused;
2796N/A struct sigaction oldAct;
2796N/A
2796N/A signal_lock();
2796N/A
2796N/A sigused = (MASK(sig) & jvmsigs) != 0;
2796N/A if (jvm_signal_installed && sigused) {
2796N/A /* jvm has installed its signal handler for this signal. */
2796N/A /* Save the handler. Don't really install it. */
2796N/A if (oact != NULL) {
2796N/A *oact = sact[sig];
2796N/A }
2796N/A if (act != NULL) {
2796N/A sact[sig] = *act;
2796N/A }
2796N/A
2796N/A signal_unlock();
2796N/A return 0;
2796N/A } else if (jvm_signal_installing) {
2796N/A /* jvm is installing its signal handlers. Install the new
2796N/A * handlers and save the old ones. */
2796N/A res = call_os_sigaction(sig, act, &oldAct);
2796N/A sact[sig] = oldAct;
2796N/A if (oact != NULL) {
2796N/A *oact = oldAct;
2796N/A }
2796N/A
2796N/A /* Record the signals used by jvm */
2796N/A jvmsigs |= MASK(sig);
2796N/A
2796N/A signal_unlock();
2796N/A return res;
2796N/A } else {
2796N/A /* jvm has no relation with this signal (yet). Install the
2796N/A * the handler. */
2796N/A res = call_os_sigaction(sig, act, oact);
2796N/A
2796N/A signal_unlock();
2796N/A return res;
2796N/A }
2796N/A}
2796N/A
2796N/A/* The three functions for the jvm to call into */
2796N/Avoid JVM_begin_signal_setting() {
2796N/A signal_lock();
2796N/A jvm_signal_installing = true;
2796N/A tid = pthread_self();
2796N/A signal_unlock();
2796N/A}
2796N/A
2796N/Avoid JVM_end_signal_setting() {
2796N/A signal_lock();
2796N/A jvm_signal_installed = true;
2796N/A jvm_signal_installing = false;
2796N/A pthread_cond_broadcast(&cond);
2796N/A signal_unlock();
2796N/A}
2796N/A
2796N/Astruct sigaction *JVM_get_signal_action(int sig) {
2796N/A /* Does race condition make sense here? */
2796N/A if ((MASK(sig) & jvmsigs) != 0) {
2796N/A return &sact[sig];
2796N/A }
2796N/A return NULL;
2796N/A}