Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/solaris/vm/jsig.c
32284 views
/*1* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/* CopyrightVersion 1.2 */2526/* This is a special library that should be loaded before libc &27* libthread to interpose the signal handler installation functions:28* sigaction(), signal(), sigset().29* Used for signal-chaining. See RFE 4381843.30*/3132#include <stdlib.h>33#include <stdio.h>34#include <string.h>35#include <signal.h>36#include <dlfcn.h>37#include <thread.h>38#include <synch.h>39#include "jvm_solaris.h"4041#define bool int42#define true 143#define false 04445static struct sigaction *sact = (struct sigaction *)NULL; /* saved signal handlers */46static sigset_t jvmsigs;4748/* used to synchronize the installation of signal handlers */49static mutex_t mutex = DEFAULTMUTEX;50static cond_t cond = DEFAULTCV;51static thread_t tid = 0;5253typedef void (*sa_handler_t)(int);54typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);55typedef sa_handler_t (*signal_t)(int, sa_handler_t);56typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);5758static signal_t os_signal = 0; /* os's version of signal()/sigset() */59static sigaction_t os_sigaction = 0; /* os's version of sigaction() */6061static bool jvm_signal_installing = false;62static bool jvm_signal_installed = false;636465/* assume called within signal_lock */66static void allocate_sact() {67size_t maxsignum;68maxsignum = SIGRTMAX;69if (sact == NULL) {70sact = (struct sigaction *)malloc((maxsignum+1) * (size_t)sizeof(struct sigaction));71memset(sact, 0, (maxsignum+1) * (size_t)sizeof(struct sigaction));72}7374if (sact == NULL) {75printf("%s\n", "libjsig.so unable to allocate memory");76exit(0);77}7879sigemptyset(&jvmsigs);80}8182static void signal_lock() {83mutex_lock(&mutex);84/* When the jvm is installing its set of signal handlers, threads85* other than the jvm thread should wait */86if (jvm_signal_installing) {87if (tid != thr_self()) {88cond_wait(&cond, &mutex);89}90}91}9293static void signal_unlock() {94mutex_unlock(&mutex);95}9697static sa_handler_t call_os_signal(int sig, sa_handler_t disp,98bool is_sigset) {99if (os_signal == NULL) {100if (!is_sigset) {101os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");102} else {103os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");104}105if (os_signal == NULL) {106printf("%s\n", dlerror());107exit(0);108}109}110return (*os_signal)(sig, disp);111}112113static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {114sigset_t set;115if (sact == NULL) {116allocate_sact();117}118sact[sig].sa_handler = disp;119sigemptyset(&set);120sact[sig].sa_mask = set;121if (!is_sigset) {122sact[sig].sa_flags = SA_NODEFER;123if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {124sact[sig].sa_flags |= SA_RESETHAND;125}126} else {127sact[sig].sa_flags = 0;128}129}130131static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {132sa_handler_t oldhandler;133bool sigblocked;134135signal_lock();136if (sact == NULL) {137allocate_sact();138}139140if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {141/* jvm has installed its signal handler for this signal. */142/* Save the handler. Don't really install it. */143if (is_sigset) {144/* We won't honor the SIG_HOLD request to change the signal mask */145sigblocked = sigismember(&(sact[sig].sa_mask), sig);146}147oldhandler = sact[sig].sa_handler;148save_signal_handler(sig, disp, is_sigset);149150if (is_sigset && sigblocked) {151oldhandler = SIG_HOLD;152}153154signal_unlock();155return oldhandler;156} else if (jvm_signal_installing) {157/* jvm is installing its signal handlers. Install the new158* handlers and save the old ones. jvm uses sigaction().159* Leave the piece here just in case. */160oldhandler = call_os_signal(sig, disp, is_sigset);161save_signal_handler(sig, oldhandler, is_sigset);162163/* Record the signals used by jvm */164sigaddset(&jvmsigs, sig);165166signal_unlock();167return oldhandler;168} else {169/* jvm has no relation with this signal (yet). Install the170* the handler. */171oldhandler = call_os_signal(sig, disp, is_sigset);172173signal_unlock();174return oldhandler;175}176}177178sa_handler_t signal(int sig, sa_handler_t disp) {179return set_signal(sig, disp, false);180}181182sa_handler_t sigset(int sig, sa_handler_t disp) {183return set_signal(sig, disp, true);184}185186static int call_os_sigaction(int sig, const struct sigaction *act,187struct sigaction *oact) {188if (os_sigaction == NULL) {189os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");190if (os_sigaction == NULL) {191printf("%s\n", dlerror());192exit(0);193}194}195return (*os_sigaction)(sig, act, oact);196}197198int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {199int res;200struct sigaction oldAct;201202signal_lock();203204if (sact == NULL ) {205allocate_sact();206}207if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {208/* jvm has installed its signal handler for this signal. */209/* Save the handler. Don't really install it. */210if (oact != NULL) {211*oact = sact[sig];212}213if (act != NULL) {214sact[sig] = *act;215}216217signal_unlock();218return 0;219} else if (jvm_signal_installing) {220/* jvm is installing its signal handlers. Install the new221* handlers and save the old ones. */222res = call_os_sigaction(sig, act, &oldAct);223sact[sig] = oldAct;224if (oact != NULL) {225*oact = oldAct;226}227228/* Record the signals used by jvm */229sigaddset(&jvmsigs, sig);230231signal_unlock();232return res;233} else {234/* jvm has no relation with this signal (yet). Install the235* the handler. */236res = call_os_sigaction(sig, act, oact);237238signal_unlock();239return res;240}241}242243/* The four functions for the jvm to call into */244void JVM_begin_signal_setting() {245signal_lock();246jvm_signal_installing = true;247tid = thr_self();248signal_unlock();249}250251void JVM_end_signal_setting() {252signal_lock();253jvm_signal_installed = true;254jvm_signal_installing = false;255cond_broadcast(&cond);256signal_unlock();257}258259struct sigaction *JVM_get_signal_action(int sig) {260if (sact == NULL) {261allocate_sact();262}263/* Does race condition make sense here? */264if (sigismember(&jvmsigs, sig)) {265return &sact[sig];266}267return NULL;268}269270int JVM_get_libjsig_version() {271return JSIG_VERSION_1_4_1;272}273274275