annotate src/java.base/unix/native/libjsig/jsig.c @ 49568:a0f22265e29c

8200298: Unify all unix versions of libjsig/jsig.c Reviewed-by: dholmes, stuefe
author ihse
date Tue, 10 Apr 2018 19:46:02 +0200
parents
children ddc976897c75
rev   line source
ihse@49568 1 /*
ihse@49568 2 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
ihse@49568 3 * Copyright (c) 2012, 2015 SAP SE. All rights reserved.
ihse@49568 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ihse@49568 5 *
ihse@49568 6 * This code is free software; you can redistribute it and/or modify it
ihse@49568 7 * under the terms of the GNU General Public License version 2 only, as
ihse@49568 8 * published by the Free Software Foundation.
ihse@49568 9 *
ihse@49568 10 * This code is distributed in the hope that it will be useful, but WITHOUT
ihse@49568 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ihse@49568 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ihse@49568 13 * version 2 for more details (a copy is included in the LICENSE file that
ihse@49568 14 * accompanied this code).
ihse@49568 15 *
ihse@49568 16 * You should have received a copy of the GNU General Public License version
ihse@49568 17 * 2 along with this work; if not, write to the Free Software Foundation,
ihse@49568 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ihse@49568 19 *
ihse@49568 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ihse@49568 21 * or visit www.oracle.com if you need additional information or have any
ihse@49568 22 * questions.
ihse@49568 23 *
ihse@49568 24 */
ihse@49568 25
ihse@49568 26 /* This is a special library that should be loaded before libc &
ihse@49568 27 * libthread to interpose the signal handler installation functions:
ihse@49568 28 * sigaction(), signal(), sigset().
ihse@49568 29 * Used for signal-chaining. See RFE 4381843.
ihse@49568 30 */
ihse@49568 31
ihse@49568 32 #include <dlfcn.h>
ihse@49568 33 #include <errno.h>
ihse@49568 34 #include <pthread.h>
ihse@49568 35 #include <signal.h>
ihse@49568 36 #include <stdio.h>
ihse@49568 37 #include <stdlib.h>
ihse@49568 38 #include <string.h>
ihse@49568 39
ihse@49568 40 #if (__STDC_VERSION__ >= 199901L)
ihse@49568 41 #include <stdbool.h>
ihse@49568 42 #else
ihse@49568 43 #define bool int
ihse@49568 44 #define true 1
ihse@49568 45 #define false 0
ihse@49568 46 #endif
ihse@49568 47
ihse@49568 48 #ifdef SOLARIS
ihse@49568 49 #define MAX_SIGNALS (SIGRTMAX+1)
ihse@49568 50
ihse@49568 51 /* On solaris, MAX_SIGNALS is a macro, not a constant, so we must allocate sact dynamically. */
ihse@49568 52 static struct sigaction *sact = (struct sigaction *)NULL; /* saved signal handlers */
ihse@49568 53 #else
ihse@49568 54 #define MAX_SIGNALS NSIG
ihse@49568 55
ihse@49568 56 static struct sigaction sact[MAX_SIGNALS]; /* saved signal handlers */
ihse@49568 57 #endif
ihse@49568 58
ihse@49568 59 static sigset_t jvmsigs; /* Signals used by jvm. */
ihse@49568 60
ihse@49568 61 #ifdef MACOSX
ihse@49568 62 static __thread bool reentry = false; /* prevent reentry deadlock (per-thread) */
ihse@49568 63 #endif
ihse@49568 64
ihse@49568 65 /* Used to synchronize the installation of signal handlers. */
ihse@49568 66 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
ihse@49568 67 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
ihse@49568 68 static pthread_t tid = 0;
ihse@49568 69
ihse@49568 70 typedef void (*sa_handler_t)(int);
ihse@49568 71 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
ihse@49568 72 typedef sa_handler_t (*signal_function_t)(int, sa_handler_t);
ihse@49568 73 typedef int (*sigaction_t)(int, const struct sigaction *, struct sigaction *);
ihse@49568 74
ihse@49568 75 static signal_function_t os_signal = 0; /* os's version of signal()/sigset() */
ihse@49568 76 static sigaction_t os_sigaction = 0; /* os's version of sigaction() */
ihse@49568 77
ihse@49568 78 static bool jvm_signal_installing = false;
ihse@49568 79 static bool jvm_signal_installed = false;
ihse@49568 80
ihse@49568 81
ihse@49568 82 /* assume called within signal_lock */
ihse@49568 83 static void allocate_sact() {
ihse@49568 84 #ifdef SOLARIS
ihse@49568 85 if (sact == NULL) {
ihse@49568 86 sact = (struct sigaction *)malloc((MAX_SIGNALS) * (size_t)sizeof(struct sigaction));
ihse@49568 87 if (sact == NULL) {
ihse@49568 88 printf("%s\n", "libjsig.so unable to allocate memory");
ihse@49568 89 exit(0);
ihse@49568 90 }
ihse@49568 91 memset(sact, 0, (MAX_SIGNALS) * (size_t)sizeof(struct sigaction));
ihse@49568 92 }
ihse@49568 93 #endif
ihse@49568 94 }
ihse@49568 95
ihse@49568 96 static void signal_lock() {
ihse@49568 97 pthread_mutex_lock(&mutex);
ihse@49568 98 /* When the jvm is installing its set of signal handlers, threads
ihse@49568 99 * other than the jvm thread should wait. */
ihse@49568 100 if (jvm_signal_installing) {
ihse@49568 101 if (tid != pthread_self()) {
ihse@49568 102 pthread_cond_wait(&cond, &mutex);
ihse@49568 103 }
ihse@49568 104 }
ihse@49568 105 }
ihse@49568 106
ihse@49568 107 static void signal_unlock() {
ihse@49568 108 pthread_mutex_unlock(&mutex);
ihse@49568 109 }
ihse@49568 110
ihse@49568 111 static sa_handler_t call_os_signal(int sig, sa_handler_t disp,
ihse@49568 112 bool is_sigset) {
ihse@49568 113 sa_handler_t res;
ihse@49568 114
ihse@49568 115 if (os_signal == NULL) {
ihse@49568 116 if (!is_sigset) {
ihse@49568 117 os_signal = (signal_function_t)dlsym(RTLD_NEXT, "signal");
ihse@49568 118 } else {
ihse@49568 119 os_signal = (signal_function_t)dlsym(RTLD_NEXT, "sigset");
ihse@49568 120 }
ihse@49568 121 if (os_signal == NULL) {
ihse@49568 122 printf("%s\n", dlerror());
ihse@49568 123 exit(0);
ihse@49568 124 }
ihse@49568 125 }
ihse@49568 126
ihse@49568 127 #ifdef MACOSX
ihse@49568 128 /* On macosx, the OS implementation of signal calls sigaction.
ihse@49568 129 * Make sure we do not deadlock with ourself. (See JDK-8072147). */
ihse@49568 130 reentry = true;
ihse@49568 131 #endif
ihse@49568 132
ihse@49568 133 res = (*os_signal)(sig, disp);
ihse@49568 134
ihse@49568 135 #ifdef MACOSX
ihse@49568 136 reentry = false;
ihse@49568 137 #endif
ihse@49568 138
ihse@49568 139 return res;
ihse@49568 140 }
ihse@49568 141
ihse@49568 142 static void save_signal_handler(int sig, sa_handler_t disp, bool is_sigset) {
ihse@49568 143 sigset_t set;
ihse@49568 144
ihse@49568 145 sact[sig].sa_handler = disp;
ihse@49568 146 sigemptyset(&set);
ihse@49568 147 sact[sig].sa_mask = set;
ihse@49568 148 if (!is_sigset) {
ihse@49568 149 #ifdef SOLARIS
ihse@49568 150 sact[sig].sa_flags = SA_NODEFER;
ihse@49568 151 if (sig != SIGILL && sig != SIGTRAP && sig != SIGPWR) {
ihse@49568 152 sact[sig].sa_flags |= SA_RESETHAND;
ihse@49568 153 }
ihse@49568 154 #else
ihse@49568 155 sact[sig].sa_flags = 0;
ihse@49568 156 #endif
ihse@49568 157 } else {
ihse@49568 158 sact[sig].sa_flags = 0;
ihse@49568 159 }
ihse@49568 160 }
ihse@49568 161
ihse@49568 162 static sa_handler_t set_signal(int sig, sa_handler_t disp, bool is_sigset) {
ihse@49568 163 sa_handler_t oldhandler;
ihse@49568 164 bool sigused;
ihse@49568 165 bool sigblocked;
ihse@49568 166
ihse@49568 167 signal_lock();
ihse@49568 168 allocate_sact();
ihse@49568 169
ihse@49568 170 sigused = sigismember(&jvmsigs, sig);
ihse@49568 171 if (jvm_signal_installed && sigused) {
ihse@49568 172 /* jvm has installed its signal handler for this signal. */
ihse@49568 173 /* Save the handler. Don't really install it. */
ihse@49568 174 if (is_sigset) {
ihse@49568 175 sigblocked = sigismember(&(sact[sig].sa_mask), sig);
ihse@49568 176 }
ihse@49568 177 oldhandler = sact[sig].sa_handler;
ihse@49568 178 save_signal_handler(sig, disp, is_sigset);
ihse@49568 179
ihse@49568 180 #ifdef SOLARIS
ihse@49568 181 if (is_sigset && sigblocked) {
ihse@49568 182 /* We won't honor the SIG_HOLD request to change the signal mask */
ihse@49568 183 oldhandler = SIG_HOLD;
ihse@49568 184 }
ihse@49568 185 #endif
ihse@49568 186
ihse@49568 187 signal_unlock();
ihse@49568 188 return oldhandler;
ihse@49568 189 } else if (jvm_signal_installing) {
ihse@49568 190 /* jvm is installing its signal handlers. Install the new
ihse@49568 191 * handlers and save the old ones. jvm uses sigaction().
ihse@49568 192 * Leave the piece here just in case. */
ihse@49568 193 oldhandler = call_os_signal(sig, disp, is_sigset);
ihse@49568 194 save_signal_handler(sig, oldhandler, is_sigset);
ihse@49568 195
ihse@49568 196 /* Record the signals used by jvm */
ihse@49568 197 sigaddset(&jvmsigs, sig);
ihse@49568 198
ihse@49568 199 signal_unlock();
ihse@49568 200 return oldhandler;
ihse@49568 201 } else {
ihse@49568 202 /* jvm has no relation with this signal (yet). Install the
ihse@49568 203 * the handler. */
ihse@49568 204 oldhandler = call_os_signal(sig, disp, is_sigset);
ihse@49568 205
ihse@49568 206 signal_unlock();
ihse@49568 207 return oldhandler;
ihse@49568 208 }
ihse@49568 209 }
ihse@49568 210
ihse@49568 211 sa_handler_t signal(int sig, sa_handler_t disp) {
ihse@49568 212 if (sig < 0 || sig >= MAX_SIGNALS) {
ihse@49568 213 errno = EINVAL;
ihse@49568 214 return SIG_ERR;
ihse@49568 215 }
ihse@49568 216
ihse@49568 217 return set_signal(sig, disp, false);
ihse@49568 218 }
ihse@49568 219
ihse@49568 220 sa_handler_t sigset(int sig, sa_handler_t disp) {
ihse@49568 221 #ifdef _ALLBSD_SOURCE
ihse@49568 222 printf("sigset() is not supported by BSD");
ihse@49568 223 exit(0);
ihse@49568 224 #else
ihse@49568 225 if (sig < 0 || sig >= MAX_SIGNALS) {
ihse@49568 226 errno = EINVAL;
ihse@49568 227 return (sa_handler_t)-1;
ihse@49568 228 }
ihse@49568 229
ihse@49568 230 return set_signal(sig, disp, true);
ihse@49568 231 #endif
ihse@49568 232 }
ihse@49568 233
ihse@49568 234 static int call_os_sigaction(int sig, const struct sigaction *act,
ihse@49568 235 struct sigaction *oact) {
ihse@49568 236 if (os_sigaction == NULL) {
ihse@49568 237 os_sigaction = (sigaction_t)dlsym(RTLD_NEXT, "sigaction");
ihse@49568 238 if (os_sigaction == NULL) {
ihse@49568 239 printf("%s\n", dlerror());
ihse@49568 240 exit(0);
ihse@49568 241 }
ihse@49568 242 }
ihse@49568 243 return (*os_sigaction)(sig, act, oact);
ihse@49568 244 }
ihse@49568 245
ihse@49568 246 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
ihse@49568 247 int res;
ihse@49568 248 bool sigused;
ihse@49568 249 struct sigaction oldAct;
ihse@49568 250
ihse@49568 251 if (sig < 0 || sig >= MAX_SIGNALS) {
ihse@49568 252 errno = EINVAL;
ihse@49568 253 return -1;
ihse@49568 254 }
ihse@49568 255
ihse@49568 256 #ifdef MACOSX
ihse@49568 257 if (reentry) {
ihse@49568 258 return call_os_sigaction(sig, act, oact);
ihse@49568 259 }
ihse@49568 260 #endif
ihse@49568 261
ihse@49568 262 signal_lock();
ihse@49568 263
ihse@49568 264 allocate_sact();
ihse@49568 265 sigused = sigismember(&jvmsigs, sig);
ihse@49568 266 if (jvm_signal_installed && sigused) {
ihse@49568 267 /* jvm has installed its signal handler for this signal. */
ihse@49568 268 /* Save the handler. Don't really install it. */
ihse@49568 269 if (oact != NULL) {
ihse@49568 270 *oact = sact[sig];
ihse@49568 271 }
ihse@49568 272 if (act != NULL) {
ihse@49568 273 sact[sig] = *act;
ihse@49568 274 }
ihse@49568 275
ihse@49568 276 signal_unlock();
ihse@49568 277 return 0;
ihse@49568 278 } else if (jvm_signal_installing) {
ihse@49568 279 /* jvm is installing its signal handlers. Install the new
ihse@49568 280 * handlers and save the old ones. */
ihse@49568 281 res = call_os_sigaction(sig, act, &oldAct);
ihse@49568 282 sact[sig] = oldAct;
ihse@49568 283 if (oact != NULL) {
ihse@49568 284 *oact = oldAct;
ihse@49568 285 }
ihse@49568 286
ihse@49568 287 /* Record the signals used by jvm. */
ihse@49568 288 sigaddset(&jvmsigs, sig);
ihse@49568 289
ihse@49568 290 signal_unlock();
ihse@49568 291 return res;
ihse@49568 292 } else {
ihse@49568 293 /* jvm has no relation with this signal (yet). Install the
ihse@49568 294 * the handler. */
ihse@49568 295 res = call_os_sigaction(sig, act, oact);
ihse@49568 296
ihse@49568 297 signal_unlock();
ihse@49568 298 return res;
ihse@49568 299 }
ihse@49568 300 }
ihse@49568 301
ihse@49568 302 /* The three functions for the jvm to call into. */
ihse@49568 303 void JVM_begin_signal_setting() {
ihse@49568 304 signal_lock();
ihse@49568 305 sigemptyset(&jvmsigs);
ihse@49568 306 jvm_signal_installing = true;
ihse@49568 307 tid = pthread_self();
ihse@49568 308 signal_unlock();
ihse@49568 309 }
ihse@49568 310
ihse@49568 311 void JVM_end_signal_setting() {
ihse@49568 312 signal_lock();
ihse@49568 313 jvm_signal_installed = true;
ihse@49568 314 jvm_signal_installing = false;
ihse@49568 315 pthread_cond_broadcast(&cond);
ihse@49568 316 signal_unlock();
ihse@49568 317 }
ihse@49568 318
ihse@49568 319 struct sigaction *JVM_get_signal_action(int sig) {
ihse@49568 320 allocate_sact();
ihse@49568 321 /* Does race condition make sense here? */
ihse@49568 322 if (sigismember(&jvmsigs, sig)) {
ihse@49568 323 return &sact[sig];
ihse@49568 324 }
ihse@49568 325 return NULL;
ihse@49568 326 }