Path: blob/master/src/java.base/unix/native/libjsig/jsig.c
41119 views
/*1* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2012, 2015 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* by Oracle in the LICENSE file that accompanied this code.10*11* This code is distributed in the hope that it will be useful, but WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 2 along with this work; if not, write to the Free Software Foundation,19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20*21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*25*/2627/* This is a special library that should be loaded before libc &28* libthread to interpose the signal handler installation functions:29* sigaction(), signal(), sigset().30* Used for signal-chaining. See RFE 4381843.31* Use of signal() and sigset() is now deprecated as these old API's should32* not be used - sigaction is the only truly supported API.33*/3435#include "jni.h"3637#include <dlfcn.h>38#include <errno.h>39#include <pthread.h>40#include <signal.h>41#include <stdio.h>42#include <stdlib.h>43#include <string.h>4445#if (__STDC_VERSION__ >= 199901L)46#include <stdbool.h>47#else48#define bool int49#define true 150#define false 051#endif5253#define MAX_SIGNALS NSIG5455static struct sigaction sact[MAX_SIGNALS]; /* saved signal handlers */5657static sigset_t jvmsigs; /* Signals used by jvm. */5859#ifdef MACOSX60static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */61#endif6263/* Used to synchronize the installation of signal handlers. */64static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;65static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;66static pthread_t tid;6768typedef void (*sa_handler_t)(int);69typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);70typedef sa_handler_t (*signal_function_t)(int, sa_handler_t);71typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);7273static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */74static sigaction_t os_sigaction = 0; /* os's version of sigaction() */7576static bool jvm_signal_installing = false;77static bool jvm_signal_installed = false;787980static void signal_lock() {81pthread_mutex_lock(&mutex);82/* When the jvm is installing its set of signal handlers, threads83* other than the jvm thread should wait. */84if (jvm_signal_installing) {85/* tid is not initialized until jvm_signal_installing is set to true. */86if (pthread_equal(tid, pthread_self()) == 0) {87do {88pthread_cond_wait(&cond, &mutex);89} while (jvm_signal_installing);90}91}92}9394static void signal_unlock() {95pthread_mutex_unlock(&mutex);96}9798static sa_handler_t call_os_signal(int sig, sa_handler_t disp,99bool is_sigset) {100sa_handler_t res;101102if (os_signal == NULL) {103// Deprecation warning first time through104printf(HOTSPOT_VM_DISTRO " VM warning: the use of signal() and sigset() "105"for signal chaining was deprecated in version 16.0 and will "106"be removed in a future release. Use sigaction() instead.\n");107if (!is_sigset) {108os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal");109} else {110os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset");111}112if (os_signal == NULL) {113printf("%s\n", dlerror());114exit(0);115}116}117118#ifdef MACOSX119/* On macosx, the OS implementation of signal calls sigaction.120* Make sure we do not deadlock with ourself. (See JDK-8072147). */121reentry = true;122#endif123124res = (*os_signal)(sig, disp);125126#ifdef MACOSX127reentry = false;128#endif129130return res;131}132133static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {134sigset_t set;135136sact[sig].sa_handler = disp;137sigemptyset(&set);138sact[sig].sa_mask = set;139sact[sig].sa_flags = 0;140}141142static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {143sa_handler_t oldhandler;144bool sigused;145bool sigblocked;146147signal_lock();148149sigused = sigismember(&jvmsigs, sig);150if (jvm_signal_installed && sigused) {151/* jvm has installed its signal handler for this signal. */152/* Save the handler. Don't really install it. */153if (is_sigset) {154sigblocked = sigismember(&(sact[sig].sa_mask), sig);155}156oldhandler = sact[sig].sa_handler;157save_signal_handler(sig, disp, is_sigset);158159signal_unlock();160return oldhandler;161} else if (jvm_signal_installing) {162/* jvm is installing its signal handlers. Install the new163* handlers and save the old ones. jvm uses sigaction().164* Leave the piece here just in case. */165oldhandler = call_os_signal(sig, disp, is_sigset);166save_signal_handler(sig, oldhandler, is_sigset);167168/* Record the signals used by jvm */169sigaddset(&jvmsigs, sig);170171signal_unlock();172return oldhandler;173} else {174/* jvm has no relation with this signal (yet). Install the175* the handler. */176oldhandler = call_os_signal(sig, disp, is_sigset);177178signal_unlock();179return oldhandler;180}181}182183JNIEXPORT sa_handler_t signal(int sig, sa_handler_t disp) {184if (sig < 0 || sig >= MAX_SIGNALS) {185errno = EINVAL;186return SIG_ERR;187}188189return set_signal(sig, disp, false);190}191192JNIEXPORT sa_handler_t sigset(int sig, sa_handler_t disp) {193#ifdef _ALLBSD_SOURCE194printf("sigset() is not supported by BSD");195exit(0);196#else197if (sig < 0 || sig >= MAX_SIGNALS) {198errno = EINVAL;199return (sa_handler_t)-1;200}201202return set_signal(sig, disp, true);203#endif204}205206static int call_os_sigaction(int sig, const struct sigaction *act,207struct sigaction *oact) {208if (os_sigaction == NULL) {209os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");210if (os_sigaction == NULL) {211printf("%s\n", dlerror());212exit(0);213}214}215return (*os_sigaction)(sig, act, oact);216}217218JNIEXPORT int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {219int res;220bool sigused;221struct sigaction oldAct;222223if (sig < 0 || sig >= MAX_SIGNALS) {224errno = EINVAL;225return -1;226}227228#ifdef MACOSX229if (reentry) {230return call_os_sigaction(sig, act, oact);231}232#endif233234signal_lock();235236sigused = sigismember(&jvmsigs, sig);237if (jvm_signal_installed && sigused) {238/* jvm has installed its signal handler for this signal. */239/* Save the handler. Don't really install it. */240if (oact != NULL) {241*oact = sact[sig];242}243if (act != NULL) {244sact[sig] = *act;245}246247signal_unlock();248return 0;249} else if (jvm_signal_installing) {250/* jvm is installing its signal handlers. Install the new251* handlers and save the old ones. */252res = call_os_sigaction(sig, act, &oldAct);253sact[sig] = oldAct;254if (oact != NULL) {255*oact = oldAct;256}257258/* Record the signals used by jvm. */259sigaddset(&jvmsigs, sig);260261signal_unlock();262return res;263} else {264/* jvm has no relation with this signal (yet). Install the265* the handler. */266res = call_os_sigaction(sig, act, oact);267268signal_unlock();269return res;270}271}272273/* The three functions for the jvm to call into. */274JNIEXPORT void JVM_begin_signal_setting() {275signal_lock();276sigemptyset(&jvmsigs);277jvm_signal_installing = true;278tid = pthread_self();279signal_unlock();280}281282JNIEXPORT void JVM_end_signal_setting() {283signal_lock();284jvm_signal_installed = true;285jvm_signal_installing = false;286pthread_cond_broadcast(&cond);287signal_unlock();288}289290JNIEXPORT struct sigaction *JVM_get_signal_action(int sig) {291/* Does race condition make sense here? */292if (sigismember(&jvmsigs, sig)) {293return &sact[sig];294}295return NULL;296}297298299