Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/linux/vm/jsig.c
32285 views
/*1* Copyright (c) 2001, 2013, 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 <signal.h>33#include <dlfcn.h>34#include <pthread.h>35#include <stdio.h>36#include <stdlib.h>3738#define bool int39#define true 140#define false 04142#define MAXSIGNUM 3243#define MASK(sig) ((unsigned int)1 << sig)4445static struct sigaction sact[MAXSIGNUM]; /* saved signal handlers */46static unsigned int jvmsigs = 0; /* signals used by jvm */4748/* used to synchronize the installation of signal handlers */49static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;50static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;51static pthread_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;6364static void signal_lock() {65pthread_mutex_lock(&mutex);66/* When the jvm is installing its set of signal handlers, threads67* other than the jvm thread should wait */68if (jvm_signal_installing) {69if (tid != pthread_self()) {70pthread_cond_wait(&cond, &mutex);71}72}73}7475static void signal_unlock() {76pthread_mutex_unlock(&mutex);77}7879static sa_handler_t call_os_signal(int sig, sa_handler_t disp,80bool is_sigset) {81if (os_signal == NULL) {82if (!is_sigset) {83os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");84} else {85os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");86}87if (os_signal == NULL) {88printf("%s\n", dlerror());89exit(0);90}91}92return (*os_signal)(sig, disp);93}9495static void save_signal_handler(int sig, sa_handler_t disp) {96sigset_t set;97sact[sig].sa_handler = disp;98sigemptyset(&set);99sact[sig].sa_mask = set;100sact[sig].sa_flags = 0;101}102103static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {104sa_handler_t oldhandler;105bool sigused;106107signal_lock();108109sigused = (sig < MAXSIGNUM) && ((MASK(sig) & jvmsigs) != 0);110if (jvm_signal_installed && sigused) {111/* jvm has installed its signal handler for this signal. */112/* Save the handler. Don't really install it. */113oldhandler = sact[sig].sa_handler;114save_signal_handler(sig, disp);115116signal_unlock();117return oldhandler;118} else if (sig < MAXSIGNUM && jvm_signal_installing) {119/* jvm is installing its signal handlers. Install the new120* handlers and save the old ones. jvm uses sigaction().121* Leave the piece here just in case. */122oldhandler = call_os_signal(sig, disp, is_sigset);123save_signal_handler(sig, oldhandler);124125/* Record the signals used by jvm */126jvmsigs |= MASK(sig);127128signal_unlock();129return oldhandler;130} else {131/* jvm has no relation with this signal (yet). Install the132* the handler. */133oldhandler = call_os_signal(sig, disp, is_sigset);134135signal_unlock();136return oldhandler;137}138}139140sa_handler_t signal(int sig, sa_handler_t disp) {141return set_signal(sig, disp, false);142}143144sa_handler_t sigset(int sig, sa_handler_t disp) {145return set_signal(sig, disp, true);146}147148static int call_os_sigaction(int sig, const struct sigaction *act,149struct sigaction *oact) {150if (os_sigaction == NULL) {151os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");152if (os_sigaction == NULL) {153printf("%s\n", dlerror());154exit(0);155}156}157return (*os_sigaction)(sig, act, oact);158}159160int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {161int res;162bool sigused;163struct sigaction oldAct;164165signal_lock();166167sigused = (sig < MAXSIGNUM) && ((MASK(sig) & jvmsigs) != 0);168if (jvm_signal_installed && sigused) {169/* jvm has installed its signal handler for this signal. */170/* Save the handler. Don't really install it. */171if (oact != NULL) {172*oact = sact[sig];173}174if (act != NULL) {175sact[sig] = *act;176}177178signal_unlock();179return 0;180} else if (sig < MAXSIGNUM && jvm_signal_installing) {181/* jvm is installing its signal handlers. Install the new182* handlers and save the old ones. */183res = call_os_sigaction(sig, act, &oldAct);184sact[sig] = oldAct;185if (oact != NULL) {186*oact = oldAct;187}188189/* Record the signals used by jvm */190jvmsigs |= MASK(sig);191192signal_unlock();193return res;194} else {195/* jvm has no relation with this signal (yet). Install the196* the handler. */197res = call_os_sigaction(sig, act, oact);198199signal_unlock();200return res;201}202}203204/* The three functions for the jvm to call into */205void JVM_begin_signal_setting() {206signal_lock();207jvm_signal_installing = true;208tid = pthread_self();209signal_unlock();210}211212void JVM_end_signal_setting() {213signal_lock();214jvm_signal_installed = true;215jvm_signal_installing = false;216pthread_cond_broadcast(&cond);217signal_unlock();218}219220struct sigaction *JVM_get_signal_action(int sig) {221/* Does race condition make sense here? */222if ((MASK(sig) & jvmsigs) != 0) {223return &sact[sig];224}225return NULL;226}227228229