This old native code for dispatching signals needs to die die die. It's long

since been replaced by using the secret undocumented JDK APIs for doing the
same thing. AFAIK that works on all of our servers and we're not shipping
libsignal.so with anything.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6225 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2010-10-22 21:59:18 +00:00
parent 26bdfaba19
commit 3b54853878
14 changed files with 3 additions and 700 deletions
+2 -12
View File
@@ -132,15 +132,6 @@
<delete file="${deploy.dir}/aslib-config.xml"/>
</target>
<condition property="isunix"><os family="unix"/></condition>
<target name="ninstall" depends="-prepare" if="isunix"
description="Builds the native libraries.">
<echo level="info" message="Doing native compilation on ${os.name}..."/>
<exec dir="${src.dir}/com/threerings/util/signal/${os.name}" executable="make">
<arg line="install"/>
</exec>
</target>
<target name="javadoc" depends="-prepare" description="Builds the Java documentation">
<mkdir dir="${deploy.dir}/docs"/>
<javadoc sourcepath="${src.dir}" packagenames="com.threerings.*"
@@ -186,10 +177,10 @@
</target>
<!-- a target for rebuilding everything -->
<target name="all" depends="clean,compile,aslib,ninstall,javadoc,dist"/>
<target name="all" depends="clean,compile,aslib,javadoc,dist"/>
<!-- builds our distribution files (war and jar) -->
<target name="dist" depends="compile,procstream,aslib,ninstall">
<target name="dist" depends="compile,procstream,aslib">
<!-- build our various jar files -->
<jar destfile="${deploy.dir}/${lib.name}-base.jar">
<fileset dir="${classes.dir}">
@@ -391,7 +382,6 @@
<fileset dir=".">
<include name="**"/>
<exclude name="${deploy.dir}/**"/>
<exclude name="tests/${deploy.dir}/**"/>
<exclude name="${lib.name}-*.zip"/>
</fileset>
</copy>
@@ -1,69 +0,0 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.presents.server;
import com.threerings.util.signal.SignalManager;
import static com.threerings.presents.Log.log;
/**
* Handles signals using Narya's native libsignal.
*/
public class NativeSignalHandler extends AbstractSignalHandler
implements SignalManager.SignalHandler
{
// from interface SignalManager.SignalHandler
public boolean signalReceived (int signo)
{
switch (signo) {
case SignalManager.SIGTERM:
termReceived();
break;
case SignalManager.SIGINT:
intReceived();
break;
case SignalManager.SIGHUP:
hupReceived();
break;
case SignalManager.SIGUSR2:
usr2Received();
break;
default:
log.warning("Received unknown signal", "signo", signo);
break;
}
return true;
}
@Override
protected boolean registerHandlers ()
{
if (!SignalManager.servicesAvailable()) {
return false;
}
SignalManager.registerSignalHandler(SignalManager.SIGTERM, this);
SignalManager.registerSignalHandler(SignalManager.SIGINT, this);
SignalManager.registerSignalHandler(SignalManager.SIGHUP, this);
SignalManager.registerSignalHandler(SignalManager.SIGUSR2, this);
return true;
}
}
@@ -179,15 +179,11 @@ public class PresentsServer
protected void registerSignalHandlers (Injector injector)
{
// register SIGTERM, SIGINT (ctrl-c) and a SIGHUP handlers
boolean registered = false;
try {
registered = injector.getInstance(SunSignalHandler.class).init();
injector.getInstance(SunSignalHandler.class).init();
} catch (Throwable t) {
log.warning("Unable to register Sun signal handlers", "error", t);
}
if (!registered) {
injector.getInstance(NativeSignalHandler.class).init();
}
}
/**
@@ -1,45 +0,0 @@
#
# $Id$
#
# Executable definitions
CC=gcc
RM=rm
CP=cp
MKDIR=mkdir
#
# Directory definitions
ROOT=../../../../../../..
LIBRARIES_PATH=
OSINCDIR!=uname -s | tr 'A-Z' 'a-z'
INSTALL_PATH=${ROOT}/dist/lib/i386-FreeBSD
#
# Parameter and file definitions
INCLUDES=-I.. -I${JAVA_HOME}/include -I${JAVA_HOME}/include/${OSINCDIR}
LIBRARIES=
TARGET=libsignal.so
#
# Target definitions
all: ${TARGET}
install:
@${MKDIR} -p ${INSTALL_PATH}
cp ${TARGET} ${INSTALL_PATH}
${TARGET}: com_threerings_util_signal_SignalManager.c
@echo "Compiling SignalManager.c"
@${CC} ${INCLUDES} -c com_threerings_util_signal_SignalManager.c \
-o com_threerings_util_signal_SignalManager.o
@echo "Creating libsignal.so"
@${CC} -o ${TARGET} com_threerings_util_signal_SignalManager.o \
${LIBRARIES} ${LIBRARIES_PATH} -shared
clean:
-${RM} -f *.o ${TARGET}
@@ -1,96 +0,0 @@
/**
* $Id$
*/
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <jni.h>
#include "com_threerings_util_signal_SignalManager.h"
static int writefd;
typedef void (*sighandler_t)(int);
static sighandler_t old_handlers[64];
static sighandler_t handlers[64];
static void
signal_handler (int signo)
{
write(writefd, &signo, sizeof(signo));
signal(SIGINT, signal_handler);
}
/**
* Class: com_threerings_util_signal_SignalManager
* Method: activateHandler
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_com_threerings_util_signal_SignalManager_activateHandler (
JNIEnv* env, jclass clazz, jint signo)
{
old_handlers[signo] = signal(signo, signal_handler);
if (old_handlers[signo] == SIG_ERR) {
fprintf(stderr, "Error setting signal handler (%d): %s\n",
signo, strerror(errno));
old_handlers[signo] = 0;
} else {
handlers[signo] = signal_handler;
}
}
/**
* Class: com_threerings_util_signal_SignalManager
* Method: deactivateHandler
* Signature: (I)V
*/
JNIEXPORT void JNICALL
Java_com_threerings_util_signal_SignalManager_deactivateHandler (
JNIEnv* env, jclass clazz, jint signo)
{
if (old_handlers[signo] != 0) {
signal(signo, old_handlers[signo]);
old_handlers[signo] = 0;
handlers[signo] = 0;
}
}
/**
* Class: com_threerings_util_signal_SignalManager
* Method: dispatchSignals
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_com_threerings_util_signal_SignalManager_dispatchSignals (
JNIEnv* env, jclass clazz)
{
int filedes[2], readfd;
jint signo;
jmethodID mid = (*env)->GetStaticMethodID(
env, clazz, "signalReceived", "(I)V");
if (pipe(filedes) < 0) {
fprintf(stderr, "Failed to create signal pipe: %s\n", strerror(errno));
return;
}
readfd = filedes[0];
writefd = filedes[1];
while (1) {
int got = read(readfd, &signo, sizeof(int));
if (got < 0) {
fprintf(stderr, "Signal pipe read failed: %s\n", strerror(errno));
} else if (got == 0) {
fprintf(stderr, "Signal pipe read returned zero bytes.\n");
} else {
if (signo < 0 || signo >= 64 || handlers[signo] == 0) {
fprintf(stderr, "Received bogus signal (%d).\n", signo);
} else {
(*env)->CallStaticVoidMethod(env, clazz, mid, signo);
}
}
}
}
@@ -1,45 +0,0 @@
#
# $Id: Makefile 3328 2005-02-03 01:16:12Z mdb $
#
# Executable definitions
CC=gcc
RM=rm
CP=cp
MKDIR=mkdir
#
# Directory definitions
ROOT=../../../../../../..
LIBRARIES_PATH=
OSINCDIR:=$(shell uname -s | tr 'A-Z' 'a-z')
INSTALL_PATH=${ROOT}/dist/lib/i686-Linux
#
# Parameter and file definitions
INCLUDES=-I.. -I${JAVA_HOME}/include -I${JAVA_HOME}/include/${OSINCDIR}
LIBRARIES=
TARGET=libsignal.so
#
# Target definitions
all: ${TARGET}
install:
@${MKDIR} -p ${INSTALL_PATH}
cp ${TARGET} ${INSTALL_PATH}
${TARGET}: com_threerings_util_signal_SignalManager.c
@echo "Compiling SignalManager.c"
@${CC} ${INCLUDES} -c com_threerings_util_signal_SignalManager.c \
-o com_threerings_util_signal_SignalManager.o
@echo "Creating libsignal.so"
@${CC} -o ${TARGET} com_threerings_util_signal_SignalManager.o \
${LIBRARIES} ${LIBRARIES_PATH} -shared
clean:
-${RM} -f *.o ${TARGET}
@@ -1 +0,0 @@
../FreeBSD/com_threerings_util_signal_SignalManager.c
@@ -1,45 +0,0 @@
#
# $Id: Makefile 3332 2005-02-03 01:30:33Z mdb $
#
# Executable definitions
CC=gcc
RM=rm
CP=cp
MKDIR=mkdir
#
# Directory definitions
ROOT=../../../../../../..
LIBRARIES_PATH=
OSINCDIR!=uname -s | tr 'A-Z' 'a-z'
INSTALL_PATH=${ROOT}/dist/lib/`uname -m`-Darwin
#
# Parameter and file definitions
INCLUDES=-I.. -I/System/Library/Frameworks/JavaVM.framework/Headers
LIBRARIES=
TARGET=libsignal.jnilib
#
# Target definitions
all: ${TARGET}
install:
@${MKDIR} -p ${INSTALL_PATH}
cp ${TARGET} ${INSTALL_PATH}
${TARGET}: com_threerings_util_signal_SignalManager.c
@echo "Compiling $<"
@${CC} ${INCLUDES} -c com_threerings_util_signal_SignalManager.c \
-o com_threerings_util_signal_SignalManager.o
@echo "Creating $@"
@${CC} -o ${TARGET} com_threerings_util_signal_SignalManager.o \
${LIBRARIES} ${LIBRARIES_PATH} -dynamiclib
clean:
-${RM} -f *.o ${TARGET}
@@ -1 +0,0 @@
../FreeBSD/com_threerings_util_signal_SignalManager.c
@@ -1,277 +0,0 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.util.signal;
import com.samskivert.util.HashIntMap;
import com.samskivert.util.ObserverList;
import static com.threerings.NaryaLog.log;
/**
* Uses native code to catch Unix signals and invoke callbacks on a separate signal handler thread.
* If the native library cannot be loaded, signal handlers will be allowed to be registered but
* will never be called.
*/
public class SignalManager
{
/* Hangup (POSIX). */
public static final int SIGHUP = 1;
/* Interrupt (ANSI). */
public static final int SIGINT = 2;
/* Quit (POSIX). */
public static final int SIGQUIT = 3;
/* Illegal instruction (ANSI). */
public static final int SIGILL = 4;
/* Trace trap (POSIX). */
public static final int SIGTRAP = 5;
/* Abort (ANSI). */
public static final int SIGABRT = 6;
/* IOT trap (4.2 BSD). */
public static final int SIGIOT = 6;
/* BUS error (4.2 BSD). */
public static final int SIGBUS = 7;
/* Floating-point exception (ANSI). */
public static final int SIGFPE = 8;
/* Kill, unblockable (POSIX). */
public static final int SIGKILL = 9;
/* User-defined signal 1 (POSIX). */
public static final int SIGUSR1 = 10;
/* Segmentation violation (ANSI). */
public static final int SIGSEGV = 11;
/* User-defined signal 2 (POSIX). */
public static final int SIGUSR2 = 12;
/* Broken pipe (POSIX). */
public static final int SIGPIPE = 13;
/* Alarm clock (POSIX). */
public static final int SIGALRM = 14;
/* Termination (ANSI). */
public static final int SIGTERM = 15;
/* Stack fault. */
public static final int SIGSTKFLT = 16;
/* Child status has changed (POSIX). */
public static final int SIGCHLD = 17;
/* Continue (POSIX). */
public static final int SIGCONT = 18;
/* Stop, unblockable (POSIX). */
public static final int SIGSTOP = 19;
/* Keyboard stop (POSIX). */
public static final int SIGTSTP = 20;
/* Background read from tty (POSIX). */
public static final int SIGTTIN = 21;
/* Background write to tty (POSIX). */
public static final int SIGTTOU = 22;
/* Urgent condition on socket (4.2 BSD). */
public static final int SIGURG = 23;
/* CPU limit exceeded (4.2 BSD). */
public static final int SIGXCPU = 24;
/* File size limit exceeded (4.2 BSD). */
public static final int SIGXFSZ = 25;
/* Virtual alarm clock (4.2 BSD). */
public static final int SIGVTALRM = 26;
/* Profiling alarm clock (4.2 BSD). */
public static final int SIGPROF = 27;
/* Window size change (4.3 BSD, Sun). */
public static final int SIGWINCH = 28;
/* I/O now possible (4.2 BSD). */
public static final int SIGIO = 29;
/* Pollable event occurred (System V). */
public static final int SIGPOLL = SIGIO;
/* Power failure restart (System V). */
public static final int SIGPWR = 30;
/* Bad system call. */
public static final int SIGSYS = 31;
/** Used to dispatch signal notifications. */
public static interface SignalHandler
{
/**
* Called when the specified signal is received.
*
* @return true if the signal handler should remain registered, false if it should be
* removed.
*/
boolean signalReceived (int signal);
}
/**
* Returns true if signal dispatching services are available, false if we could not load our
* native library.
*/
public static boolean servicesAvailable ()
{
return _haveLibrary;
}
/**
* Registers a signal handler for the specified signal.
*/
public synchronized static void registerSignalHandler (int signal, SignalHandler handler)
{
ObserverList<SignalHandler> list = _handlers.get(signal);
if (list == null) {
_handlers.put(signal, list = new ObserverList<SignalHandler>(
ObserverList.SAFE_IN_ORDER_NOTIFY));
if (_haveLibrary) {
activateHandler(signal);
}
}
list.add(handler);
// make sure the signal dispatcher thread is started
if (_haveLibrary && _sigdis == null) {
_sigdis = new Thread("SignalDispatcher") {
@Override
public void run () {
log.info("Dispatching signals...");
dispatchSignals();
}
};
_sigdis.setDaemon(true);
_sigdis.start();
}
}
/**
* Removes the registration for the specified signal handler.
*/
public synchronized static void removeSignalHandler (int signal, SignalHandler handler)
{
ObserverList<SignalHandler> list = _handlers.get(signal);
if (list == null || !list.contains(handler)) {
log.warning("Requested to remove non-registered handler", "signal", signal,
"handler", handler);
return;
}
list.remove(handler);
checkEmpty(signal);
}
/**
* Called by native code when a signal is received.
*/
protected synchronized static void signalReceived (final int signal)
{
// this is hack, but we seem to get a call to our signal handler for each thread if the
// user presses ctrl-c in the terminal, so we "collapse" those into one call back
long now = System.currentTimeMillis();
if (signal == SIGINT) {
if (now - _lastINTed < 10) {
return;
} else {
_lastINTed = now;
}
}
ObserverList<SignalHandler> list = _handlers.get(signal);
if (list != null) {
list.apply(new ObserverList.ObserverOp<SignalHandler>() {
public boolean apply (SignalHandler handler) {
return handler.signalReceived(signal);
}
});
}
checkEmpty(signal);
}
/**
* Called when a signal handler is removed.
*/
protected static void checkEmpty (int signal)
{
ObserverList<SignalHandler> list = _handlers.get(signal);
if (list != null && list.size() == 0) {
_handlers.remove(signal);
if (_haveLibrary) {
deactivateHandler(signal);
}
}
}
/**
* Activates the signal handler for the specified signal.
*/
protected static native void activateHandler (int signal);
/**
* Deactivates the signal handler for the specified signal.
*/
protected static native void deactivateHandler (int signal);
/**
* Consigns a Java thread to the task of dispatching signal handler callbacks.
*/
protected static native void dispatchSignals ();
/** A mapping from signal number to a list of handlers. */
protected static HashIntMap<ObserverList<SignalHandler>> _handlers =
new HashIntMap<ObserverList<SignalHandler>>();
/** Our signal dispatcher thread. */
protected static Thread _sigdis;
/** Set to true if we successfully load our native library. */
protected static boolean _haveLibrary;
/** Used to collapse bogus multiple delivery of SIGINT when the user pressed ctrl-c in the
* console. */
protected static long _lastINTed = 0L;
static {
try {
System.loadLibrary("signal");
_haveLibrary = true;
} catch (Throwable t) {
log.info("Could not load libsignal.so; signal handling disabled.");
}
}
}
@@ -1,104 +0,0 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_threerings_util_signal_SignalManager */
#ifndef _Included_com_threerings_util_signal_SignalManager
#define _Included_com_threerings_util_signal_SignalManager
#ifdef __cplusplus
extern "C" {
#endif
#undef com_threerings_util_signal_SignalManager_SIGHUP
#define com_threerings_util_signal_SignalManager_SIGHUP 1L
#undef com_threerings_util_signal_SignalManager_SIGINT
#define com_threerings_util_signal_SignalManager_SIGINT 2L
#undef com_threerings_util_signal_SignalManager_SIGQUIT
#define com_threerings_util_signal_SignalManager_SIGQUIT 3L
#undef com_threerings_util_signal_SignalManager_SIGILL
#define com_threerings_util_signal_SignalManager_SIGILL 4L
#undef com_threerings_util_signal_SignalManager_SIGTRAP
#define com_threerings_util_signal_SignalManager_SIGTRAP 5L
#undef com_threerings_util_signal_SignalManager_SIGABRT
#define com_threerings_util_signal_SignalManager_SIGABRT 6L
#undef com_threerings_util_signal_SignalManager_SIGIOT
#define com_threerings_util_signal_SignalManager_SIGIOT 6L
#undef com_threerings_util_signal_SignalManager_SIGBUS
#define com_threerings_util_signal_SignalManager_SIGBUS 7L
#undef com_threerings_util_signal_SignalManager_SIGFPE
#define com_threerings_util_signal_SignalManager_SIGFPE 8L
#undef com_threerings_util_signal_SignalManager_SIGKILL
#define com_threerings_util_signal_SignalManager_SIGKILL 9L
#undef com_threerings_util_signal_SignalManager_SIGUSR1
#define com_threerings_util_signal_SignalManager_SIGUSR1 10L
#undef com_threerings_util_signal_SignalManager_SIGSEGV
#define com_threerings_util_signal_SignalManager_SIGSEGV 11L
#undef com_threerings_util_signal_SignalManager_SIGUSR2
#define com_threerings_util_signal_SignalManager_SIGUSR2 12L
#undef com_threerings_util_signal_SignalManager_SIGPIPE
#define com_threerings_util_signal_SignalManager_SIGPIPE 13L
#undef com_threerings_util_signal_SignalManager_SIGALRM
#define com_threerings_util_signal_SignalManager_SIGALRM 14L
#undef com_threerings_util_signal_SignalManager_SIGTERM
#define com_threerings_util_signal_SignalManager_SIGTERM 15L
#undef com_threerings_util_signal_SignalManager_SIGSTKFLT
#define com_threerings_util_signal_SignalManager_SIGSTKFLT 16L
#undef com_threerings_util_signal_SignalManager_SIGCHLD
#define com_threerings_util_signal_SignalManager_SIGCHLD 17L
#undef com_threerings_util_signal_SignalManager_SIGCONT
#define com_threerings_util_signal_SignalManager_SIGCONT 18L
#undef com_threerings_util_signal_SignalManager_SIGSTOP
#define com_threerings_util_signal_SignalManager_SIGSTOP 19L
#undef com_threerings_util_signal_SignalManager_SIGTSTP
#define com_threerings_util_signal_SignalManager_SIGTSTP 20L
#undef com_threerings_util_signal_SignalManager_SIGTTIN
#define com_threerings_util_signal_SignalManager_SIGTTIN 21L
#undef com_threerings_util_signal_SignalManager_SIGTTOU
#define com_threerings_util_signal_SignalManager_SIGTTOU 22L
#undef com_threerings_util_signal_SignalManager_SIGURG
#define com_threerings_util_signal_SignalManager_SIGURG 23L
#undef com_threerings_util_signal_SignalManager_SIGXCPU
#define com_threerings_util_signal_SignalManager_SIGXCPU 24L
#undef com_threerings_util_signal_SignalManager_SIGXFSZ
#define com_threerings_util_signal_SignalManager_SIGXFSZ 25L
#undef com_threerings_util_signal_SignalManager_SIGVTALRM
#define com_threerings_util_signal_SignalManager_SIGVTALRM 26L
#undef com_threerings_util_signal_SignalManager_SIGPROF
#define com_threerings_util_signal_SignalManager_SIGPROF 27L
#undef com_threerings_util_signal_SignalManager_SIGWINCH
#define com_threerings_util_signal_SignalManager_SIGWINCH 28L
#undef com_threerings_util_signal_SignalManager_SIGIO
#define com_threerings_util_signal_SignalManager_SIGIO 29L
#undef com_threerings_util_signal_SignalManager_SIGPOLL
#define com_threerings_util_signal_SignalManager_SIGPOLL 29L
#undef com_threerings_util_signal_SignalManager_SIGPWR
#define com_threerings_util_signal_SignalManager_SIGPWR 30L
#undef com_threerings_util_signal_SignalManager_SIGSYS
#define com_threerings_util_signal_SignalManager_SIGSYS 31L
/* Inaccessible static: _handlers */
/*
* Class: com_threerings_util_signal_SignalManager
* Method: activateHandler
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_threerings_util_signal_SignalManager_activateHandler
(JNIEnv *, jclass, jint);
/*
* Class: com_threerings_util_signal_SignalManager
* Method: deactivateHandler
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_threerings_util_signal_SignalManager_deactivateHandler
(JNIEnv *, jclass, jint);
/*
* Class: com_threerings_util_signal_SignalManager
* Method: dispatchSignals
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_threerings_util_signal_SignalManager_dispatchSignals
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif