8200298: Unify all unix versions of libjsig/jsig.c
Reviewed-by: dholmes, stuefe
This commit is contained in:
parent
e855c767d2
commit
70e23d4ded
@ -155,10 +155,10 @@ endif
|
||||
################################################################################
|
||||
# Create the jsig library
|
||||
|
||||
ifneq ($(OPENJDK_TARGET_OS), windows)
|
||||
ifeq ($(OPENJDK_TARGET_OS_TYPE), unix)
|
||||
ifeq ($(STATIC_BUILD), false)
|
||||
|
||||
LIBJSIG_SRC_DIR := $(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS)/native/libjsig
|
||||
LIBJSIG_SRC_DIR := $(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS_TYPE)/native/libjsig
|
||||
LIBJSIG_MAPFILE := $(wildcard $(TOPDIR)/make/mapfiles/libjsig/mapfile-vers-$(OPENJDK_TARGET_OS))
|
||||
|
||||
ifeq ($(OPENJDK_TARGET_OS), linux)
|
||||
|
@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
# Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
#
|
||||
# This code is free software; you can redistribute it and/or modify it
|
||||
@ -28,7 +28,6 @@ SUNWprivate_1.1 {
|
||||
global:
|
||||
JVM_begin_signal_setting;
|
||||
JVM_end_signal_setting;
|
||||
JVM_get_libjsig_version;
|
||||
JVM_get_signal_action;
|
||||
sigaction;
|
||||
signal;
|
||||
|
@ -2101,8 +2101,6 @@ static jint *pending_signals = NULL;
|
||||
static int *preinstalled_sigs = NULL;
|
||||
static struct sigaction *chainedsigactions = NULL;
|
||||
static Semaphore* sig_sem = NULL;
|
||||
typedef int (*version_getting_t)();
|
||||
version_getting_t os::Solaris::get_libjsig_version = NULL;
|
||||
|
||||
int os::sigexitnum_pd() {
|
||||
assert(Sigexit > 0, "signal memory not yet initialized");
|
||||
@ -3968,13 +3966,7 @@ void os::Solaris::install_signal_handlers() {
|
||||
dlsym(RTLD_DEFAULT, "JVM_end_signal_setting"));
|
||||
get_signal_action = CAST_TO_FN_PTR(get_signal_t,
|
||||
dlsym(RTLD_DEFAULT, "JVM_get_signal_action"));
|
||||
get_libjsig_version = CAST_TO_FN_PTR(version_getting_t,
|
||||
dlsym(RTLD_DEFAULT, "JVM_get_libjsig_version"));
|
||||
libjsig_is_loaded = true;
|
||||
if (os::Solaris::get_libjsig_version != NULL) {
|
||||
int libjsigversion = (*os::Solaris::get_libjsig_version)();
|
||||
assert(libjsigversion == JSIG_VERSION_1_4_1, "libjsig version mismatch");
|
||||
}
|
||||
assert(UseSignalChaining, "should enable signal-chaining");
|
||||
}
|
||||
if (libjsig_is_loaded) {
|
||||
|
@ -1,230 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*
|
||||
*/
|
||||
|
||||
/* CopyrightVersion 1.2 */
|
||||
|
||||
/* This is a special library that should be loaded before libc &
|
||||
* libthread to interpose the signal handler installation functions:
|
||||
* sigaction(), signal(), sigset().
|
||||
* Used for signal-chaining. See RFE 4381843.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <dlfcn.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define bool int
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
#define MASK(sig) ((uint64_t)1 << (sig-1)) // 0 is not a signal.
|
||||
// Check whether all signals fit into jvmsigs. -1 as MASK shifts by -1.
|
||||
#if (64 < NSIG-1)
|
||||
#error "Not all signals can be encoded in jvmsigs. Adapt its type!"
|
||||
#endif
|
||||
static struct sigaction sact[NSIG]; /* saved signal handlers */
|
||||
static uint64_t jvmsigs = 0; /* signals used by jvm */
|
||||
|
||||
/* used to synchronize the installation of signal handlers */
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
||||
static pthread_t tid = 0;
|
||||
|
||||
typedef void (*sa_handler_t)(int);
|
||||
typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
|
||||
typedef sa_handler_t (*signal_t)(int, sa_handler_t);
|
||||
typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
|
||||
|
||||
static signal_t os_signal = 0; /* os's version of signal()/sigset() */
|
||||
static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
|
||||
|
||||
static bool jvm_signal_installing = false;
|
||||
static bool jvm_signal_installed = false;
|
||||
|
||||
static void signal_lock() {
|
||||
pthread_mutex_lock(&mutex);
|
||||
/* When the jvm is installing its set of signal handlers, threads
|
||||
* other than the jvm thread should wait */
|
||||
if (jvm_signal_installing) {
|
||||
if (tid != pthread_self()) {
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void signal_unlock() {
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
|
||||
bool is_sigset) {
|
||||
if (os_signal == NULL) {
|
||||
if (!is_sigset) {
|
||||
os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
|
||||
} else {
|
||||
os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
|
||||
}
|
||||
if (os_signal == NULL) {
|
||||
printf("%s\n", dlerror());
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
return (*os_signal)(sig, disp);
|
||||
}
|
||||
|
||||
static void save_signal_handler(int sig, sa_handler_t disp) {
|
||||
sigset_t set;
|
||||
sact[sig].sa_handler = disp;
|
||||
sigemptyset(&set);
|
||||
sact[sig].sa_mask = set;
|
||||
sact[sig].sa_flags = 0;
|
||||
}
|
||||
|
||||
static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
|
||||
sa_handler_t oldhandler;
|
||||
bool sigused;
|
||||
|
||||
signal_lock();
|
||||
|
||||
sigused = (sig < NSIG) && ((MASK(sig) & jvmsigs) != 0);
|
||||
if (jvm_signal_installed && sigused) {
|
||||
/* jvm has installed its signal handler for this signal. */
|
||||
/* Save the handler. Don't really install it. */
|
||||
oldhandler = sact[sig].sa_handler;
|
||||
save_signal_handler(sig, disp);
|
||||
|
||||
signal_unlock();
|
||||
return oldhandler;
|
||||
} else if (sig < NSIG && jvm_signal_installing) {
|
||||
/* jvm is installing its signal handlers. Install the new
|
||||
* handlers and save the old ones. jvm uses sigaction().
|
||||
* Leave the piece here just in case. */
|
||||
oldhandler = call_os_signal(sig, disp, is_sigset);
|
||||
save_signal_handler(sig, oldhandler);
|
||||
|
||||
/* Record the signals used by jvm */
|
||||
jvmsigs |= MASK(sig);
|
||||
|
||||
signal_unlock();
|
||||
return oldhandler;
|
||||
} else {
|
||||
/* jvm has no relation with this signal (yet). Install the
|
||||
* the handler. */
|
||||
oldhandler = call_os_signal(sig, disp, is_sigset);
|
||||
|
||||
signal_unlock();
|
||||
return oldhandler;
|
||||
}
|
||||
}
|
||||
|
||||
sa_handler_t signal(int sig, sa_handler_t disp) {
|
||||
return set_signal(sig, disp, false);
|
||||
}
|
||||
|
||||
sa_handler_t sigset(int sig, sa_handler_t disp) {
|
||||
return set_signal(sig, disp, true);
|
||||
}
|
||||
|
||||
static int call_os_sigaction(int sig, const struct sigaction *act,
|
||||
struct sigaction *oact) {
|
||||
if (os_sigaction == NULL) {
|
||||
os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
|
||||
if (os_sigaction == NULL) {
|
||||
printf("%s\n", dlerror());
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
return (*os_sigaction)(sig, act, oact);
|
||||
}
|
||||
|
||||
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
|
||||
int res;
|
||||
bool sigused;
|
||||
struct sigaction oldAct;
|
||||
|
||||
signal_lock();
|
||||
|
||||
sigused = (sig < NSIG) && ((MASK(sig) & jvmsigs) != 0);
|
||||
if (jvm_signal_installed && sigused) {
|
||||
/* jvm has installed its signal handler for this signal. */
|
||||
/* Save the handler. Don't really install it. */
|
||||
if (oact != NULL) {
|
||||
*oact = sact[sig];
|
||||
}
|
||||
if (act != NULL) {
|
||||
sact[sig] = *act;
|
||||
}
|
||||
|
||||
signal_unlock();
|
||||
return 0;
|
||||
} else if (sig < NSIG && jvm_signal_installing) {
|
||||
/* jvm is installing its signal handlers. Install the new
|
||||
* handlers and save the old ones. */
|
||||
res = call_os_sigaction(sig, act, &oldAct);
|
||||
sact[sig] = oldAct;
|
||||
if (oact != NULL) {
|
||||
*oact = oldAct;
|
||||
}
|
||||
|
||||
/* Record the signals used by jvm */
|
||||
jvmsigs |= MASK(sig);
|
||||
|
||||
signal_unlock();
|
||||
return res;
|
||||
} else {
|
||||
/* jvm has no relation with this signal (yet). Install the
|
||||
* the handler. */
|
||||
res = call_os_sigaction(sig, act, oact);
|
||||
|
||||
signal_unlock();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* The three functions for the jvm to call into */
|
||||
void JVM_begin_signal_setting() {
|
||||
signal_lock();
|
||||
jvm_signal_installing = true;
|
||||
tid = pthread_self();
|
||||
signal_unlock();
|
||||
}
|
||||
|
||||
void JVM_end_signal_setting() {
|
||||
signal_lock();
|
||||
jvm_signal_installed = true;
|
||||
jvm_signal_installing = false;
|
||||
pthread_cond_broadcast(&cond);
|
||||
signal_unlock();
|
||||
}
|
||||
|
||||
struct sigaction *JVM_get_signal_action(int sig) {
|
||||
/* Does race condition make sense here? */
|
||||
if ((MASK(sig) & jvmsigs) != 0) {
|
||||
return &sact[sig];
|
||||
}
|
||||
return NULL;
|
||||
}
|
@ -1,237 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*
|
||||
*/
|
||||
|
||||
/* CopyrightVersion 1.2 */
|
||||
|
||||
/* This is a special library that should be loaded before libc &
|
||||
* libthread to interpose the signal handler installation functions:
|
||||
* sigaction(), signal(), sigset().
|
||||
* Used for signal-chaining. See RFE 4381843.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <dlfcn.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define MASK(sig) ((uint32_t)1 << (sig-1)) // 0 is not a signal.
|
||||
#if (32 < NSIG-1)
|
||||
#error "Not all signals can be encoded in jvmsigs. Adapt its type!"
|
||||
#endif
|
||||
static struct sigaction sact[NSIG]; /* saved signal handlers */
|
||||
static uint32_t jvmsigs = 0; /* signals used by jvm */
|
||||
static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */
|
||||
|
||||
/* used to synchronize the installation of signal handlers */
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
||||
static pthread_t tid = 0;
|
||||
|
||||
typedef void (*sa_handler_t)(int);
|
||||
typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
|
||||
typedef sa_handler_t (*signal_t)(int, sa_handler_t);
|
||||
typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
|
||||
|
||||
static signal_t os_signal = 0; /* os's version of signal()/sigset() */
|
||||
static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
|
||||
|
||||
static bool jvm_signal_installing = false;
|
||||
static bool jvm_signal_installed = false;
|
||||
|
||||
static void signal_lock() {
|
||||
pthread_mutex_lock(&mutex);
|
||||
/* When the jvm is installing its set of signal handlers, threads
|
||||
* other than the jvm thread should wait */
|
||||
if (jvm_signal_installing) {
|
||||
if (tid != pthread_self()) {
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void signal_unlock() {
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
|
||||
bool is_sigset) {
|
||||
sa_handler_t res;
|
||||
|
||||
if (os_signal == NULL) {
|
||||
if (!is_sigset) {
|
||||
os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
|
||||
} else {
|
||||
os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
|
||||
}
|
||||
if (os_signal == NULL) {
|
||||
printf("%s\n", dlerror());
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
reentry = true;
|
||||
res = (*os_signal)(sig, disp);
|
||||
reentry = false;
|
||||
return res;
|
||||
}
|
||||
|
||||
static void save_signal_handler(int sig, sa_handler_t disp) {
|
||||
sigset_t set;
|
||||
sact[sig].sa_handler = disp;
|
||||
sigemptyset(&set);
|
||||
sact[sig].sa_mask = set;
|
||||
sact[sig].sa_flags = 0;
|
||||
}
|
||||
|
||||
static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
|
||||
sa_handler_t oldhandler;
|
||||
bool sigused;
|
||||
|
||||
signal_lock();
|
||||
|
||||
sigused = (MASK(sig) & jvmsigs) != 0;
|
||||
if (jvm_signal_installed && sigused) {
|
||||
/* jvm has installed its signal handler for this signal. */
|
||||
/* Save the handler. Don't really install it. */
|
||||
oldhandler = sact[sig].sa_handler;
|
||||
save_signal_handler(sig, disp);
|
||||
|
||||
signal_unlock();
|
||||
return oldhandler;
|
||||
} else if (jvm_signal_installing) {
|
||||
/* jvm is installing its signal handlers. Install the new
|
||||
* handlers and save the old ones. jvm uses sigaction().
|
||||
* Leave the piece here just in case. */
|
||||
oldhandler = call_os_signal(sig, disp, is_sigset);
|
||||
save_signal_handler(sig, oldhandler);
|
||||
|
||||
/* Record the signals used by jvm */
|
||||
jvmsigs |= MASK(sig);
|
||||
|
||||
signal_unlock();
|
||||
return oldhandler;
|
||||
} else {
|
||||
/* jvm has no relation with this signal (yet). Install the
|
||||
* the handler. */
|
||||
oldhandler = call_os_signal(sig, disp, is_sigset);
|
||||
|
||||
signal_unlock();
|
||||
return oldhandler;
|
||||
}
|
||||
}
|
||||
|
||||
sa_handler_t signal(int sig, sa_handler_t disp) {
|
||||
return set_signal(sig, disp, false);
|
||||
}
|
||||
|
||||
sa_handler_t sigset(int sig, sa_handler_t disp) {
|
||||
printf("sigset() is not supported by BSD");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static int call_os_sigaction(int sig, const struct sigaction *act,
|
||||
struct sigaction *oact) {
|
||||
if (os_sigaction == NULL) {
|
||||
os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
|
||||
if (os_sigaction == NULL) {
|
||||
printf("%s\n", dlerror());
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
return (*os_sigaction)(sig, act, oact);
|
||||
}
|
||||
|
||||
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
|
||||
int res;
|
||||
bool sigused;
|
||||
struct sigaction oldAct;
|
||||
|
||||
if (reentry) {
|
||||
return call_os_sigaction(sig, act, oact);
|
||||
}
|
||||
|
||||
signal_lock();
|
||||
|
||||
sigused = (MASK(sig) & jvmsigs) != 0;
|
||||
if (jvm_signal_installed && sigused) {
|
||||
/* jvm has installed its signal handler for this signal. */
|
||||
/* Save the handler. Don't really install it. */
|
||||
if (oact != NULL) {
|
||||
*oact = sact[sig];
|
||||
}
|
||||
if (act != NULL) {
|
||||
sact[sig] = *act;
|
||||
}
|
||||
|
||||
signal_unlock();
|
||||
return 0;
|
||||
} else if (jvm_signal_installing) {
|
||||
/* jvm is installing its signal handlers. Install the new
|
||||
* handlers and save the old ones. */
|
||||
res = call_os_sigaction(sig, act, &oldAct);
|
||||
sact[sig] = oldAct;
|
||||
if (oact != NULL) {
|
||||
*oact = oldAct;
|
||||
}
|
||||
|
||||
/* Record the signals used by jvm */
|
||||
jvmsigs |= MASK(sig);
|
||||
|
||||
signal_unlock();
|
||||
return res;
|
||||
} else {
|
||||
/* jvm has no relation with this signal (yet). Install the
|
||||
* the handler. */
|
||||
res = call_os_sigaction(sig, act, oact);
|
||||
|
||||
signal_unlock();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* The three functions for the jvm to call into */
|
||||
void JVM_begin_signal_setting() {
|
||||
signal_lock();
|
||||
jvm_signal_installing = true;
|
||||
tid = pthread_self();
|
||||
signal_unlock();
|
||||
}
|
||||
|
||||
void JVM_end_signal_setting() {
|
||||
signal_lock();
|
||||
jvm_signal_installed = true;
|
||||
jvm_signal_installing = false;
|
||||
pthread_cond_broadcast(&cond);
|
||||
signal_unlock();
|
||||
}
|
||||
|
||||
struct sigaction *JVM_get_signal_action(int sig) {
|
||||
/* Does race condition make sense here? */
|
||||
if ((MASK(sig) & jvmsigs) != 0) {
|
||||
return &sact[sig];
|
||||
}
|
||||
return NULL;
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 2015 SAP SE. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -22,42 +23,56 @@
|
||||
*
|
||||
*/
|
||||
|
||||
/* CopyrightVersion 1.2 */
|
||||
|
||||
/* This is a special library that should be loaded before libc &
|
||||
* libthread to interpose the signal handler installation functions:
|
||||
* sigaction(), signal(), sigset().
|
||||
* Used for signal-chaining. See RFE 4381843.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <dlfcn.h>
|
||||
#include <thread.h>
|
||||
#include <synch.h>
|
||||
#include "jni.h"
|
||||
#include "jvm_md.h"
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define bool int
|
||||
#define true 1
|
||||
#define false 0
|
||||
#if (__STDC_VERSION__ >= 199901L)
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#define bool int
|
||||
#define true 1
|
||||
#define false 0
|
||||
#endif
|
||||
|
||||
#ifdef SOLARIS
|
||||
#define MAX_SIGNALS (SIGRTMAX+1)
|
||||
|
||||
/* On solaris, MAX_SIGNALS is a macro, not a constant, so we must allocate sact dynamically. */
|
||||
static struct sigaction *sact = (struct sigaction *)NULL; /* saved signal handlers */
|
||||
static sigset_t jvmsigs;
|
||||
#else
|
||||
#define MAX_SIGNALS NSIG
|
||||
|
||||
/* used to synchronize the installation of signal handlers */
|
||||
static mutex_t mutex = DEFAULTMUTEX;
|
||||
static cond_t cond = DEFAULTCV;
|
||||
static thread_t tid = 0;
|
||||
static struct sigaction sact[MAX_SIGNALS]; /* saved signal handlers */
|
||||
#endif
|
||||
|
||||
static sigset_t jvmsigs; /* Signals used by jvm. */
|
||||
|
||||
#ifdef MACOSX
|
||||
static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */
|
||||
#endif
|
||||
|
||||
/* Used to synchronize the installation of signal handlers. */
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
||||
static pthread_t tid = 0;
|
||||
|
||||
typedef void (*sa_handler_t)(int);
|
||||
typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
|
||||
typedef sa_handler_t (*signal_t)(int, sa_handler_t);
|
||||
typedef sa_handler_t (*signal_function_t)(int, sa_handler_t);
|
||||
typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
|
||||
|
||||
static signal_t os_signal = 0; /* os's version of signal()/sigset() */
|
||||
static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */
|
||||
static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
|
||||
|
||||
static bool jvm_signal_installing = false;
|
||||
@ -66,65 +81,79 @@ static bool jvm_signal_installed = false;
|
||||
|
||||
/* assume called within signal_lock */
|
||||
static void allocate_sact() {
|
||||
size_t maxsignum;
|
||||
maxsignum = SIGRTMAX;
|
||||
#ifdef SOLARIS
|
||||
if (sact == NULL) {
|
||||
sact = (struct sigaction *)malloc((maxsignum+1) * (size_t)sizeof(struct sigaction));
|
||||
memset(sact, 0, (maxsignum+1) * (size_t)sizeof(struct sigaction));
|
||||
sact = (struct sigaction *)malloc((MAX_SIGNALS) * (size_t)sizeof(struct sigaction));
|
||||
if (sact == NULL) {
|
||||
printf("%s\n", "libjsig.so unable to allocate memory");
|
||||
exit(0);
|
||||
}
|
||||
memset(sact, 0, (MAX_SIGNALS) * (size_t)sizeof(struct sigaction));
|
||||
}
|
||||
|
||||
if (sact == NULL) {
|
||||
printf("%s\n", "libjsig.so unable to allocate memory");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
sigemptyset(&jvmsigs);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void signal_lock() {
|
||||
mutex_lock(&mutex);
|
||||
pthread_mutex_lock(&mutex);
|
||||
/* When the jvm is installing its set of signal handlers, threads
|
||||
* other than the jvm thread should wait */
|
||||
* other than the jvm thread should wait. */
|
||||
if (jvm_signal_installing) {
|
||||
if (tid != thr_self()) {
|
||||
cond_wait(&cond, &mutex);
|
||||
if (tid != pthread_self()) {
|
||||
pthread_cond_wait(&cond, &mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void signal_unlock() {
|
||||
mutex_unlock(&mutex);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
|
||||
bool is_sigset) {
|
||||
sa_handler_t res;
|
||||
|
||||
if (os_signal == NULL) {
|
||||
if (!is_sigset) {
|
||||
os_signal = (signal_t)dlsym(RTLD_NEXT, "signal");
|
||||
os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal");
|
||||
} else {
|
||||
os_signal = (signal_t)dlsym(RTLD_NEXT, "sigset");
|
||||
os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset");
|
||||
}
|
||||
if (os_signal == NULL) {
|
||||
printf("%s\n", dlerror());
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
return (*os_signal)(sig, disp);
|
||||
|
||||
#ifdef MACOSX
|
||||
/* On macosx, the OS implementation of signal calls sigaction.
|
||||
* Make sure we do not deadlock with ourself. (See JDK-8072147). */
|
||||
reentry = true;
|
||||
#endif
|
||||
|
||||
res = (*os_signal)(sig, disp);
|
||||
|
||||
#ifdef MACOSX
|
||||
reentry = false;
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
|
||||
sigset_t set;
|
||||
if (sact == NULL) {
|
||||
allocate_sact();
|
||||
}
|
||||
|
||||
sact[sig].sa_handler = disp;
|
||||
sigemptyset(&set);
|
||||
sact[sig].sa_mask = set;
|
||||
if (!is_sigset) {
|
||||
#ifdef SOLARIS
|
||||
sact[sig].sa_flags = SA_NODEFER;
|
||||
if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
|
||||
sact[sig].sa_flags |= SA_RESETHAND;
|
||||
}
|
||||
#else
|
||||
sact[sig].sa_flags = 0;
|
||||
#endif
|
||||
} else {
|
||||
sact[sig].sa_flags = 0;
|
||||
}
|
||||
@ -132,26 +161,28 @@ static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
|
||||
|
||||
static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
|
||||
sa_handler_t oldhandler;
|
||||
bool sigused;
|
||||
bool sigblocked;
|
||||
|
||||
signal_lock();
|
||||
if (sact == NULL) {
|
||||
allocate_sact();
|
||||
}
|
||||
allocate_sact();
|
||||
|
||||
if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {
|
||||
sigused = sigismember(&jvmsigs, sig);
|
||||
if (jvm_signal_installed && sigused) {
|
||||
/* jvm has installed its signal handler for this signal. */
|
||||
/* Save the handler. Don't really install it. */
|
||||
if (is_sigset) {
|
||||
/* We won't honor the SIG_HOLD request to change the signal mask */
|
||||
sigblocked = sigismember(&(sact[sig].sa_mask), sig);
|
||||
}
|
||||
oldhandler = sact[sig].sa_handler;
|
||||
save_signal_handler(sig, disp, is_sigset);
|
||||
|
||||
#ifdef SOLARIS
|
||||
if (is_sigset && sigblocked) {
|
||||
/* We won't honor the SIG_HOLD request to change the signal mask */
|
||||
oldhandler = SIG_HOLD;
|
||||
}
|
||||
#endif
|
||||
|
||||
signal_unlock();
|
||||
return oldhandler;
|
||||
@ -178,11 +209,26 @@ static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
|
||||
}
|
||||
|
||||
sa_handler_t signal(int sig, sa_handler_t disp) {
|
||||
if (sig < 0 || sig >= MAX_SIGNALS) {
|
||||
errno = EINVAL;
|
||||
return SIG_ERR;
|
||||
}
|
||||
|
||||
return set_signal(sig, disp, false);
|
||||
}
|
||||
|
||||
sa_handler_t sigset(int sig, sa_handler_t disp) {
|
||||
#ifdef _ALLBSD_SOURCE
|
||||
printf("sigset() is not supported by BSD");
|
||||
exit(0);
|
||||
#else
|
||||
if (sig < 0 || sig >= MAX_SIGNALS) {
|
||||
errno = EINVAL;
|
||||
return (sa_handler_t)-1;
|
||||
}
|
||||
|
||||
return set_signal(sig, disp, true);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int call_os_sigaction(int sig, const struct sigaction *act,
|
||||
@ -199,14 +245,25 @@ static int call_os_sigaction(int sig, const struct sigaction *act,
|
||||
|
||||
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
|
||||
int res;
|
||||
bool sigused;
|
||||
struct sigaction oldAct;
|
||||
|
||||
if (sig < 0 || sig >= MAX_SIGNALS) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef MACOSX
|
||||
if (reentry) {
|
||||
return call_os_sigaction(sig, act, oact);
|
||||
}
|
||||
#endif
|
||||
|
||||
signal_lock();
|
||||
|
||||
if (sact == NULL ) {
|
||||
allocate_sact();
|
||||
}
|
||||
if (jvm_signal_installed && sigismember(&jvmsigs, sig)) {
|
||||
allocate_sact();
|
||||
sigused = sigismember(&jvmsigs, sig);
|
||||
if (jvm_signal_installed && sigused) {
|
||||
/* jvm has installed its signal handler for this signal. */
|
||||
/* Save the handler. Don't really install it. */
|
||||
if (oact != NULL) {
|
||||
@ -227,7 +284,7 @@ int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
|
||||
*oact = oldAct;
|
||||
}
|
||||
|
||||
/* Record the signals used by jvm */
|
||||
/* Record the signals used by jvm. */
|
||||
sigaddset(&jvmsigs, sig);
|
||||
|
||||
signal_unlock();
|
||||
@ -242,37 +299,28 @@ int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
|
||||
}
|
||||
}
|
||||
|
||||
/* The four functions for the jvm to call into */
|
||||
JNIEXPORT void JNICALL
|
||||
JVM_begin_signal_setting() {
|
||||
/* The three functions for the jvm to call into. */
|
||||
void JVM_begin_signal_setting() {
|
||||
signal_lock();
|
||||
sigemptyset(&jvmsigs);
|
||||
jvm_signal_installing = true;
|
||||
tid = thr_self();
|
||||
tid = pthread_self();
|
||||
signal_unlock();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
JVM_end_signal_setting() {
|
||||
void JVM_end_signal_setting() {
|
||||
signal_lock();
|
||||
jvm_signal_installed = true;
|
||||
jvm_signal_installing = false;
|
||||
cond_broadcast(&cond);
|
||||
pthread_cond_broadcast(&cond);
|
||||
signal_unlock();
|
||||
}
|
||||
|
||||
JNIEXPORT struct sigaction * JNICALL
|
||||
JVM_get_signal_action(int sig) {
|
||||
if (sact == NULL) {
|
||||
allocate_sact();
|
||||
}
|
||||
struct sigaction *JVM_get_signal_action(int sig) {
|
||||
allocate_sact();
|
||||
/* Does race condition make sense here? */
|
||||
if (sigismember(&jvmsigs, sig)) {
|
||||
return &sact[sig];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
JNIEXPORT int JNICALL
|
||||
JVM_get_libjsig_version() {
|
||||
return JSIG_VERSION_1_4_1;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user