2796N/A/*
2796N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2796N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2796N/A *
2796N/A * This code is free software; you can redistribute it and/or modify it
2796N/A * under the terms of the GNU General Public License version 2 only, as
2796N/A * published by the Free Software Foundation.
2796N/A *
2796N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2796N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2796N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2796N/A * version 2 for more details (a copy is included in the LICENSE file that
2796N/A * accompanied this code).
2796N/A *
2796N/A * You should have received a copy of the GNU General Public License version
2796N/A * 2 along with this work; if not, write to the Free Software Foundation,
2796N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2796N/A *
2796N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2796N/A * or visit www.oracle.com if you need additional information or have any
2796N/A * questions.
2796N/A *
2796N/A */
2796N/A
2796N/A#include "precompiled.hpp"
2796N/A#include "prims/jvm.h"
2796N/A#include "runtime/interfaceSupport.hpp"
2796N/A#include "runtime/osThread.hpp"
2796N/A
2796N/A#include <signal.h>
2796N/A
2796N/A
2796N/A// sun.misc.Signal ///////////////////////////////////////////////////////////
2796N/A// Signal code is mostly copied from classic vm, signals_md.c 1.4 98/08/23
2796N/A/*
2796N/A * This function is included primarily as a debugging aid. If Java is
2796N/A * running in a console window, then pressing <CTRL-\\> will cause
2796N/A * the current state of all active threads and monitors to be written
2796N/A * to the console window.
2796N/A */
2796N/A
2796N/AJVM_ENTRY_NO_ENV(void*, JVM_RegisterSignal(jint sig, void* handler))
2796N/A // Copied from classic vm
2796N/A // signals_md.c 1.4 98/08/23
2796N/A void* newHandler = handler == (void *)2
2796N/A ? os::user_handler()
2796N/A : handler;
2796N/A switch (sig) {
2796N/A /* The following are already used by the VM. */
2796N/A case INTERRUPT_SIGNAL:
2796N/A case SIGFPE:
2796N/A case SIGILL:
2796N/A case SIGSEGV:
2796N/A
2796N/A /* The following signal is used by the VM to dump thread stacks unless
2796N/A ReduceSignalUsage is set, in which case the user is allowed to set
2796N/A his own _native_ handler for this signal; thus, in either case,
2796N/A we do not allow JVM_RegisterSignal to change the handler. */
2796N/A case BREAK_SIGNAL:
2796N/A return (void *)-1;
2796N/A
2796N/A /* The following signals are used for Shutdown Hooks support. However, if
2796N/A ReduceSignalUsage (-Xrs) is set, Shutdown Hooks must be invoked via
2796N/A System.exit(), Java is not allowed to use these signals, and the the
2796N/A user is allowed to set his own _native_ handler for these signals and
2796N/A invoke System.exit() as needed. Terminator.setup() is avoiding
2796N/A registration of these signals when -Xrs is present.
2796N/A - If the HUP signal is ignored (from the nohup) command, then Java
2796N/A is not allowed to use this signal.
2796N/A */
2796N/A
2796N/A case SHUTDOWN1_SIGNAL:
2796N/A case SHUTDOWN2_SIGNAL:
2796N/A case SHUTDOWN3_SIGNAL:
2796N/A if (ReduceSignalUsage) return (void*)-1;
2796N/A if (os::Bsd::is_sig_ignored(sig)) return (void*)1;
2796N/A }
2796N/A
2796N/A void* oldHandler = os::signal(sig, newHandler);
2796N/A if (oldHandler == os::user_handler()) {
2796N/A return (void *)2;
2796N/A } else {
2796N/A return oldHandler;
2796N/A }
2796N/AJVM_END
2796N/A
2796N/A
2796N/AJVM_ENTRY_NO_ENV(jboolean, JVM_RaiseSignal(jint sig))
2796N/A if (ReduceSignalUsage) {
2796N/A // do not allow SHUTDOWN1_SIGNAL,SHUTDOWN2_SIGNAL,SHUTDOWN3_SIGNAL,
2796N/A // BREAK_SIGNAL to be raised when ReduceSignalUsage is set, since
2796N/A // no handler for them is actually registered in JVM or via
2796N/A // JVM_RegisterSignal.
2796N/A if (sig == SHUTDOWN1_SIGNAL || sig == SHUTDOWN2_SIGNAL ||
2796N/A sig == SHUTDOWN3_SIGNAL || sig == BREAK_SIGNAL) {
2796N/A return JNI_FALSE;
2796N/A }
2796N/A }
2796N/A else if ((sig == SHUTDOWN1_SIGNAL || sig == SHUTDOWN2_SIGNAL ||
2796N/A sig == SHUTDOWN3_SIGNAL) && os::Bsd::is_sig_ignored(sig)) {
2796N/A // do not allow SHUTDOWN1_SIGNAL to be raised when SHUTDOWN1_SIGNAL
2796N/A // is ignored, since no handler for them is actually registered in JVM
2796N/A // or via JVM_RegisterSignal.
2796N/A // This also applies for SHUTDOWN2_SIGNAL and SHUTDOWN3_SIGNAL
2796N/A return JNI_FALSE;
2796N/A }
2796N/A
2796N/A os::signal_raise(sig);
2796N/A return JNI_TRUE;
2796N/AJVM_END
2796N/A
2796N/A/*
2796N/A All the defined signal names for Bsd.
2796N/A
2796N/A NOTE that not all of these names are accepted by our Java implementation
2796N/A
2796N/A Via an existing claim by the VM, sigaction restrictions, or
2796N/A the "rules of Unix" some of these names will be rejected at runtime.
2796N/A For example the VM sets up to handle USR1, sigaction returns EINVAL for
2796N/A STOP, and Bsd simply doesn't allow catching of KILL.
2796N/A
2796N/A Here are the names currently accepted by a user of sun.misc.Signal with
2796N/A 1.4.1 (ignoring potential interaction with use of chaining, etc):
2796N/A
2796N/A HUP, INT, TRAP, ABRT, IOT, BUS, USR2, PIPE, ALRM, TERM, STKFLT,
2796N/A CLD, CHLD, CONT, TSTP, TTIN, TTOU, URG, XCPU, XFSZ, VTALRM, PROF,
2796N/A WINCH, POLL, IO, PWR, SYS
2796N/A
2796N/A*/
2796N/A
2796N/Astruct siglabel {
2796N/A const char *name;
2796N/A int number;
2796N/A};
2796N/A
2796N/Astruct siglabel siglabels[] = {
2796N/A /* derived from /usr/include/bits/signum.h on RH7.2 */
2796N/A "HUP", SIGHUP, /* Hangup (POSIX). */
2796N/A "INT", SIGINT, /* Interrupt (ANSI). */
2796N/A "QUIT", SIGQUIT, /* Quit (POSIX). */
2796N/A "ILL", SIGILL, /* Illegal instruction (ANSI). */
2796N/A "TRAP", SIGTRAP, /* Trace trap (POSIX). */
2796N/A "ABRT", SIGABRT, /* Abort (ANSI). */
2796N/A "EMT", SIGEMT, /* EMT trap */
2796N/A "FPE", SIGFPE, /* Floating-point exception (ANSI). */
2796N/A "KILL", SIGKILL, /* Kill, unblockable (POSIX). */
2796N/A "BUS", SIGBUS, /* BUS error (4.2 BSD). */
2796N/A "SEGV", SIGSEGV, /* Segmentation violation (ANSI). */
2796N/A "SYS", SIGSYS, /* Bad system call. Only on some Bsden! */
2796N/A "PIPE", SIGPIPE, /* Broken pipe (POSIX). */
2796N/A "ALRM", SIGALRM, /* Alarm clock (POSIX). */
2796N/A "TERM", SIGTERM, /* Termination (ANSI). */
2796N/A "URG", SIGURG, /* Urgent condition on socket (4.2 BSD). */
2796N/A "STOP", SIGSTOP, /* Stop, unblockable (POSIX). */
2796N/A "TSTP", SIGTSTP, /* Keyboard stop (POSIX). */
2796N/A "CONT", SIGCONT, /* Continue (POSIX). */
2796N/A "CHLD", SIGCHLD, /* Child status has changed (POSIX). */
2796N/A "TTIN", SIGTTIN, /* Background read from tty (POSIX). */
2796N/A "TTOU", SIGTTOU, /* Background write to tty (POSIX). */
2796N/A "IO", SIGIO, /* I/O now possible (4.2 BSD). */
2796N/A "XCPU", SIGXCPU, /* CPU limit exceeded (4.2 BSD). */
2796N/A "XFSZ", SIGXFSZ, /* File size limit exceeded (4.2 BSD). */
2796N/A "VTALRM", SIGVTALRM, /* Virtual alarm clock (4.2 BSD). */
2796N/A "PROF", SIGPROF, /* Profiling alarm clock (4.2 BSD). */
2796N/A "WINCH", SIGWINCH, /* Window size change (4.3 BSD, Sun). */
2796N/A "INFO", SIGINFO, /* Information request. */
2796N/A "USR1", SIGUSR1, /* User-defined signal 1 (POSIX). */
2796N/A "USR2", SIGUSR2 /* User-defined signal 2 (POSIX). */
2796N/A };
2796N/A
2796N/AJVM_ENTRY_NO_ENV(jint, JVM_FindSignal(const char *name))
2796N/A
2796N/A /* find and return the named signal's number */
2796N/A
2796N/A for(uint i=0; i<ARRAY_SIZE(siglabels); i++)
2796N/A if(!strcmp(name, siglabels[i].name))
2796N/A return siglabels[i].number;
2796N/A
2796N/A return -1;
2796N/A
2796N/AJVM_END
2796N/A
2796N/A// used by os::exception_name()
2796N/Aextern bool signal_name(int signo, char* buf, size_t len) {
2796N/A for(uint i = 0; i < ARRAY_SIZE(siglabels); i++) {
2796N/A if (signo == siglabels[i].number) {
2796N/A jio_snprintf(buf, len, "SIG%s", siglabels[i].name);
2796N/A return true;
2796N/A }
2796N/A }
2796N/A return false;
2796N/A}