Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/bsd/vm/jsig.c
32284 views
/*1* Copyright (c) 2001, 2015, 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>37#include <stdbool.h>38#include <string.h>3940#define MAXSIGNUM 3241#define MASK(sig) ((unsigned int)1 << sig)4243static struct sigaction sact[MAXSIGNUM]; /* saved signal handlers */44static unsigned int jvmsigs = 0; /* signals used by jvm */4546static pthread_key_t reentry_flag_key;47static pthread_once_t reentry_key_init_once = PTHREAD_ONCE_INIT;4849/* used to synchronize the installation of signal handlers */50static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;51static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;52static pthread_t tid = 0;5354typedef void (*sa_handler_t)(int);55typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);56typedef sa_handler_t (*signal_t)(int, sa_handler_t);57typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);5859static signal_t os_signal = 0; /* os's version of signal()/sigset() */60static sigaction_t os_sigaction = 0; /* os's version of sigaction() */6162static bool jvm_signal_installing = false;63static bool jvm_signal_installed = false;6465#define check_status(cmd) \66do { \67int status = (cmd); \68if (status != 0) { \69printf("error %s (%d) in " #cmd "\n", strerror(status), status); \70exit(1); \71} \72} while (0)7374static void signal_lock() {75pthread_mutex_lock(&mutex);76/* When the jvm is installing its set of signal handlers, threads77* other than the jvm thread should wait */78if (jvm_signal_installing) {79if (tid != pthread_self()) {80pthread_cond_wait(&cond, &mutex);81}82}83}8485static void signal_unlock() {86pthread_mutex_unlock(&mutex);87}8889static void reentry_tls_init() {90// value for reentry_flag_key will default to NULL (false)91check_status(pthread_key_create(&reentry_flag_key, NULL));92}9394static sa_handler_t call_os_signal(int sig, sa_handler_t disp,95bool is_sigset) {96sa_handler_t res;9798if (os_signal == NULL) {99if (!is_sigset) {100os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");101} else {102os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");103}104if (os_signal == NULL) {105printf("%s\n", dlerror());106exit(0);107}108}109check_status(pthread_once(&reentry_key_init_once, reentry_tls_init));110// set reentry_flag_key to non-NULL to show reentry111check_status(pthread_setspecific(reentry_flag_key, &res));112res = (*os_signal)(sig, disp);113check_status(pthread_setspecific(reentry_flag_key, NULL));114return res;115}116117static void save_signal_handler(int sig, sa_handler_t disp) {118sigset_t set;119sact[sig].sa_handler = disp;120sigemptyset(&set);121sact[sig].sa_mask = set;122sact[sig].sa_flags = 0;123}124125static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {126sa_handler_t oldhandler;127bool sigused;128129signal_lock();130131sigused = (MASK(sig) & jvmsigs) != 0;132if (jvm_signal_installed && sigused) {133/* jvm has installed its signal handler for this signal. */134/* Save the handler. Don't really install it. */135oldhandler = sact[sig].sa_handler;136save_signal_handler(sig, disp);137138signal_unlock();139return oldhandler;140} else if (jvm_signal_installing) {141/* jvm is installing its signal handlers. Install the new142* handlers and save the old ones. jvm uses sigaction().143* Leave the piece here just in case. */144oldhandler = call_os_signal(sig, disp, is_sigset);145save_signal_handler(sig, oldhandler);146147/* Record the signals used by jvm */148jvmsigs |= MASK(sig);149150signal_unlock();151return oldhandler;152} else {153/* jvm has no relation with this signal (yet). Install the154* the handler. */155oldhandler = call_os_signal(sig, disp, is_sigset);156157signal_unlock();158return oldhandler;159}160}161162sa_handler_t signal(int sig, sa_handler_t disp) {163return set_signal(sig, disp, false);164}165166sa_handler_t sigset(int sig, sa_handler_t disp) {167printf("sigset() is not supported by BSD");168exit(0);169}170171static int call_os_sigaction(int sig, const struct sigaction *act,172struct sigaction *oact) {173if (os_sigaction == NULL) {174os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");175if (os_sigaction == NULL) {176printf("%s\n", dlerror());177exit(0);178}179}180return (*os_sigaction)(sig, act, oact);181}182183int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {184int res;185bool sigused;186struct sigaction oldAct;187188check_status(pthread_once(&reentry_key_init_once, reentry_tls_init));189if (pthread_getspecific(reentry_flag_key) != NULL) {190return call_os_sigaction(sig, act, oact);191}192193signal_lock();194195sigused = (MASK(sig) & jvmsigs) != 0;196if (jvm_signal_installed && sigused) {197/* jvm has installed its signal handler for this signal. */198/* Save the handler. Don't really install it. */199if (oact != NULL) {200*oact = sact[sig];201}202if (act != NULL) {203sact[sig] = *act;204}205206signal_unlock();207return 0;208} else if (jvm_signal_installing) {209/* jvm is installing its signal handlers. Install the new210* handlers and save the old ones. */211res = call_os_sigaction(sig, act, &oldAct);212sact[sig] = oldAct;213if (oact != NULL) {214*oact = oldAct;215}216217/* Record the signals used by jvm */218jvmsigs |= MASK(sig);219220signal_unlock();221return res;222} else {223/* jvm has no relation with this signal (yet). Install the224* the handler. */225res = call_os_sigaction(sig, act, oact);226227signal_unlock();228return res;229}230}231232/* The three functions for the jvm to call into */233void JVM_begin_signal_setting() {234signal_lock();235jvm_signal_installing = true;236tid = pthread_self();237signal_unlock();238}239240void JVM_end_signal_setting() {241signal_lock();242jvm_signal_installed = true;243jvm_signal_installing = false;244pthread_cond_broadcast(&cond);245signal_unlock();246}247248struct sigaction *JVM_get_signal_action(int sig) {249/* Does race condition make sense here? */250if ((MASK(sig) & jvmsigs) != 0) {251return &sact[sig];252}253return NULL;254}255256257