1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/os/linux/vm/jvm_linux.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,199 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "prims/jvm.h" 1.30 +#include "runtime/interfaceSupport.hpp" 1.31 +#include "runtime/osThread.hpp" 1.32 + 1.33 +#include <signal.h> 1.34 + 1.35 + 1.36 +// sun.misc.Signal /////////////////////////////////////////////////////////// 1.37 +// Signal code is mostly copied from classic vm, signals_md.c 1.4 98/08/23 1.38 +/* 1.39 + * This function is included primarily as a debugging aid. If Java is 1.40 + * running in a console window, then pressing <CTRL-\\> will cause 1.41 + * the current state of all active threads and monitors to be written 1.42 + * to the console window. 1.43 + */ 1.44 + 1.45 +JVM_ENTRY_NO_ENV(void*, JVM_RegisterSignal(jint sig, void* handler)) 1.46 + // Copied from classic vm 1.47 + // signals_md.c 1.4 98/08/23 1.48 + void* newHandler = handler == (void *)2 1.49 + ? os::user_handler() 1.50 + : handler; 1.51 + switch (sig) { 1.52 + /* The following are already used by the VM. */ 1.53 + case INTERRUPT_SIGNAL: 1.54 + case SIGFPE: 1.55 + case SIGILL: 1.56 + case SIGSEGV: 1.57 + 1.58 + /* The following signal is used by the VM to dump thread stacks unless 1.59 + ReduceSignalUsage is set, in which case the user is allowed to set 1.60 + his own _native_ handler for this signal; thus, in either case, 1.61 + we do not allow JVM_RegisterSignal to change the handler. */ 1.62 + case BREAK_SIGNAL: 1.63 + return (void *)-1; 1.64 + 1.65 + /* The following signals are used for Shutdown Hooks support. However, if 1.66 + ReduceSignalUsage (-Xrs) is set, Shutdown Hooks must be invoked via 1.67 + System.exit(), Java is not allowed to use these signals, and the the 1.68 + user is allowed to set his own _native_ handler for these signals and 1.69 + invoke System.exit() as needed. Terminator.setup() is avoiding 1.70 + registration of these signals when -Xrs is present. 1.71 + - If the HUP signal is ignored (from the nohup) command, then Java 1.72 + is not allowed to use this signal. 1.73 + */ 1.74 + 1.75 + case SHUTDOWN1_SIGNAL: 1.76 + case SHUTDOWN2_SIGNAL: 1.77 + case SHUTDOWN3_SIGNAL: 1.78 + if (ReduceSignalUsage) return (void*)-1; 1.79 + if (os::Linux::is_sig_ignored(sig)) return (void*)1; 1.80 + } 1.81 + 1.82 + void* oldHandler = os::signal(sig, newHandler); 1.83 + if (oldHandler == os::user_handler()) { 1.84 + return (void *)2; 1.85 + } else { 1.86 + return oldHandler; 1.87 + } 1.88 +JVM_END 1.89 + 1.90 + 1.91 +JVM_ENTRY_NO_ENV(jboolean, JVM_RaiseSignal(jint sig)) 1.92 + if (ReduceSignalUsage) { 1.93 + // do not allow SHUTDOWN1_SIGNAL,SHUTDOWN2_SIGNAL,SHUTDOWN3_SIGNAL, 1.94 + // BREAK_SIGNAL to be raised when ReduceSignalUsage is set, since 1.95 + // no handler for them is actually registered in JVM or via 1.96 + // JVM_RegisterSignal. 1.97 + if (sig == SHUTDOWN1_SIGNAL || sig == SHUTDOWN2_SIGNAL || 1.98 + sig == SHUTDOWN3_SIGNAL || sig == BREAK_SIGNAL) { 1.99 + return JNI_FALSE; 1.100 + } 1.101 + } 1.102 + else if ((sig == SHUTDOWN1_SIGNAL || sig == SHUTDOWN2_SIGNAL || 1.103 + sig == SHUTDOWN3_SIGNAL) && os::Linux::is_sig_ignored(sig)) { 1.104 + // do not allow SHUTDOWN1_SIGNAL to be raised when SHUTDOWN1_SIGNAL 1.105 + // is ignored, since no handler for them is actually registered in JVM 1.106 + // or via JVM_RegisterSignal. 1.107 + // This also applies for SHUTDOWN2_SIGNAL and SHUTDOWN3_SIGNAL 1.108 + return JNI_FALSE; 1.109 + } 1.110 + 1.111 + os::signal_raise(sig); 1.112 + return JNI_TRUE; 1.113 +JVM_END 1.114 + 1.115 +/* 1.116 + All the defined signal names for Linux. 1.117 + 1.118 + NOTE that not all of these names are accepted by our Java implementation 1.119 + 1.120 + Via an existing claim by the VM, sigaction restrictions, or 1.121 + the "rules of Unix" some of these names will be rejected at runtime. 1.122 + For example the VM sets up to handle USR1, sigaction returns EINVAL for 1.123 + STOP, and Linux simply doesn't allow catching of KILL. 1.124 + 1.125 + Here are the names currently accepted by a user of sun.misc.Signal with 1.126 + 1.4.1 (ignoring potential interaction with use of chaining, etc): 1.127 + 1.128 + HUP, INT, TRAP, ABRT, IOT, BUS, USR2, PIPE, ALRM, TERM, STKFLT, 1.129 + CLD, CHLD, CONT, TSTP, TTIN, TTOU, URG, XCPU, XFSZ, VTALRM, PROF, 1.130 + WINCH, POLL, IO, PWR, SYS 1.131 + 1.132 +*/ 1.133 + 1.134 +struct siglabel { 1.135 + const char *name; 1.136 + int number; 1.137 +}; 1.138 + 1.139 +struct siglabel siglabels[] = { 1.140 + /* derived from /usr/include/bits/signum.h on RH7.2 */ 1.141 + "HUP", SIGHUP, /* Hangup (POSIX). */ 1.142 + "INT", SIGINT, /* Interrupt (ANSI). */ 1.143 + "QUIT", SIGQUIT, /* Quit (POSIX). */ 1.144 + "ILL", SIGILL, /* Illegal instruction (ANSI). */ 1.145 + "TRAP", SIGTRAP, /* Trace trap (POSIX). */ 1.146 + "ABRT", SIGABRT, /* Abort (ANSI). */ 1.147 + "IOT", SIGIOT, /* IOT trap (4.2 BSD). */ 1.148 + "BUS", SIGBUS, /* BUS error (4.2 BSD). */ 1.149 + "FPE", SIGFPE, /* Floating-point exception (ANSI). */ 1.150 + "KILL", SIGKILL, /* Kill, unblockable (POSIX). */ 1.151 + "USR1", SIGUSR1, /* User-defined signal 1 (POSIX). */ 1.152 + "SEGV", SIGSEGV, /* Segmentation violation (ANSI). */ 1.153 + "USR2", SIGUSR2, /* User-defined signal 2 (POSIX). */ 1.154 + "PIPE", SIGPIPE, /* Broken pipe (POSIX). */ 1.155 + "ALRM", SIGALRM, /* Alarm clock (POSIX). */ 1.156 + "TERM", SIGTERM, /* Termination (ANSI). */ 1.157 +#ifdef SIGSTKFLT 1.158 + "STKFLT", SIGSTKFLT, /* Stack fault. */ 1.159 +#endif 1.160 + "CLD", SIGCLD, /* Same as SIGCHLD (System V). */ 1.161 + "CHLD", SIGCHLD, /* Child status has changed (POSIX). */ 1.162 + "CONT", SIGCONT, /* Continue (POSIX). */ 1.163 + "STOP", SIGSTOP, /* Stop, unblockable (POSIX). */ 1.164 + "TSTP", SIGTSTP, /* Keyboard stop (POSIX). */ 1.165 + "TTIN", SIGTTIN, /* Background read from tty (POSIX). */ 1.166 + "TTOU", SIGTTOU, /* Background write to tty (POSIX). */ 1.167 + "URG", SIGURG, /* Urgent condition on socket (4.2 BSD). */ 1.168 + "XCPU", SIGXCPU, /* CPU limit exceeded (4.2 BSD). */ 1.169 + "XFSZ", SIGXFSZ, /* File size limit exceeded (4.2 BSD). */ 1.170 + "VTALRM", SIGVTALRM, /* Virtual alarm clock (4.2 BSD). */ 1.171 + "PROF", SIGPROF, /* Profiling alarm clock (4.2 BSD). */ 1.172 + "WINCH", SIGWINCH, /* Window size change (4.3 BSD, Sun). */ 1.173 + "POLL", SIGPOLL, /* Pollable event occurred (System V). */ 1.174 + "IO", SIGIO, /* I/O now possible (4.2 BSD). */ 1.175 + "PWR", SIGPWR, /* Power failure restart (System V). */ 1.176 +#ifdef SIGSYS 1.177 + "SYS", SIGSYS /* Bad system call. Only on some Linuxen! */ 1.178 +#endif 1.179 + }; 1.180 + 1.181 +JVM_ENTRY_NO_ENV(jint, JVM_FindSignal(const char *name)) 1.182 + 1.183 + /* find and return the named signal's number */ 1.184 + 1.185 + for(uint i=0; i<ARRAY_SIZE(siglabels); i++) 1.186 + if(!strcmp(name, siglabels[i].name)) 1.187 + return siglabels[i].number; 1.188 + 1.189 + return -1; 1.190 + 1.191 +JVM_END 1.192 + 1.193 +// used by os::exception_name() 1.194 +extern bool signal_name(int signo, char* buf, size_t len) { 1.195 + for(uint i = 0; i < ARRAY_SIZE(siglabels); i++) { 1.196 + if (signo == siglabels[i].number) { 1.197 + jio_snprintf(buf, len, "SIG%s", siglabels[i].name); 1.198 + return true; 1.199 + } 1.200 + } 1.201 + return false; 1.202 +}