You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/04/12 18:50:41 UTC

svn commit: r1091496 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/Native.java native/include/acr/misc.h native/os/unix/arch_defs.h native/os/unix/init.c native/shared/native.c test/org/apache/commons/runtime/TestMain.java

Author: mturk
Date: Tue Apr 12 16:50:41 2011
New Revision: 1091496

URL: http://svn.apache.org/viewvc?rev=1091496&view=rev
Log:
Init signals and install shutdown hook. Installing SIGSEGV can still crash the VM on shutdown. Need to investigate the reason

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c
    commons/sandbox/runtime/trunk/src/main/native/shared/native.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java?rev=1091496&r1=1091495&r2=1091496&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Native.java Tue Apr 12 16:50:41 2011
@@ -24,18 +24,18 @@ import org.apache.commons.runtime.except
  * @since Runtime 1.0
  *
  */
-public final class Native
+public final class Native extends Thread
 {
 
     private Native()
-    {
-        // No instance.
+    {        
     }
 
     private static boolean initialized = false;
     private static native boolean init0()
         throws Throwable;
     private static native boolean isdbg0();
+    private static native void    fini0();
 
     /**
      * True if the native code compiled with debugging support.
@@ -70,7 +70,24 @@ public final class Native
                     "operating system"
                 );
             }
+            if (initialized) {
+                // Install Shutdown Hook.
+                Runtime.getRuntime().addShutdownHook(new Native());
+            }
         }
         return initialized;
     }
+    
+    public static void terminate()
+    {
+        if (initialized) {
+            initialized = false;
+            fini0();
+        }        
+    }
+
+    public void run()
+    {
+        terminate();
+    }
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h?rev=1091496&r1=1091495&r2=1091496&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/misc.h Tue Apr 12 16:50:41 2011
@@ -28,6 +28,9 @@ AcrLibLockAcquire(void);
 void
 AcrLibLockRelease(void);
 
+int
+AcrSignalsInit(void);
+
 #ifdef __cplusplus
 }
 #endif

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h?rev=1091496&r1=1091495&r2=1091496&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h Tue Apr 12 16:50:41 2011
@@ -40,6 +40,16 @@
 # endif
 #endif
 
+#if defined(NSIG)
+# define ACR_NUMSIG         NSIG
+#elif defined(_NSIG)
+# define ACR_NUMSIG         _NSIG
+#elif defined(__NSIG)
+# define ACR_NUMSIG         __NSIG
+#else
+# define ACR_NUMSIG         33
+#endif
+
 /*
  * Common unix utils
  */

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c?rev=1091496&r1=1091495&r2=1091496&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/init.c Tue Apr 12 16:50:41 2011
@@ -21,10 +21,12 @@
 #include "acr/clazz.h"
 #include "acr/misc.h"
 
-static JavaVM         *_java_vm = 0;
-static pthread_key_t   _threadkey;
-static volatile int    _threadkey_inited = 0;
-static pthread_mutex_t _lib_mutex  = PTHREAD_MUTEX_INITIALIZER;
+static JavaVM           *_java_vm = 0;
+static pthread_key_t     _threadkey;
+static volatile int      _threadkey_inited = 0;
+static pthread_mutex_t   _lib_mutex  = PTHREAD_MUTEX_INITIALIZER;
+static struct sigaction *_signalset;
+
 #if HAVE_THREAD_LOCAL
 ACR_THREAD acr_exc_t   acr_exception_frame = { 0, 0, 0, 0, 0, 0 };
 /*
@@ -152,6 +154,67 @@ static tlsd_t *_threadkey_get(void)
     return env;
 }
 
+static int _sigs_inited = 0;
+int
+AcrSignalsInit()
+{
+    int i, rc = 0;
+    struct sigaction *ssp;
+
+    if (_sigs_inited++)
+        return 0;
+    ssp = _signalset = malloc(ACR_NUMSIG * sizeof(struct sigaction));
+    for (i = 0; i < ACR_NUMSIG; i++) {
+        struct sigaction act;
+
+        memset(&act, 0, sizeof(struct sigaction));
+        sigemptyset(&act.sa_mask);
+        act.sa_handler = SIG_DFL;
+        act.sa_flags   = 0;
+#ifdef SA_INTERRUPT             /* SunOS */
+        act.sa_flags |= SA_INTERRUPT;
+#endif
+        if (sigaction(i, &act, ssp) == 0) {
+            /* Restore the original saved sigaction.
+             * ###: Is there a smarter way for getting the
+             * original signal handlers?
+             */
+            sigaction(i, ssp, &act);
+        }
+        else {
+            /* Those are for SIGKILL and SIGSTOP
+             * which cannot be caught or ignored.
+             */
+            ssp->sa_handler = SIG_ERR;
+        }
+        ++ssp;
+    }
+#if HAVE_THREAD_LOCAL
+    {
+        struct sigaction act;
+        memset(&act, 0, sizeof(struct sigaction));
+        sigemptyset(&act.sa_mask);
+        act.sa_handler = SIG_DFL; //AcrExceptionHandler;
+        act.sa_flags   = 0;
+#ifdef SA_INTERRUPT
+        act.sa_flags  |= SA_INTERRUPT;
+#endif
+        for (i = 0; sig_blocked[i] > 0; i++) {
+            /* Block most signals during SIGSEGV handling
+             */
+            sigaddset(&act.sa_mask, sig_blocked[i]);
+        }
+        /* Handle SIGSEGV signal
+         */
+        sigaction(SIGSEGV, &act, 0);
+#if defined(HPUX11) || defined(DARWIN)
+        sigaction(SIGBUS,  &act, 0);
+#endif
+    }        
+#endif
+    return rc;
+}
+
 void
 AcrLibLockAcquire()
 {
@@ -184,30 +247,6 @@ JNI_OnLoad(JavaVM *vm, void *reserved)
         return JNI_ERR;
     if (AcrInitCoreClasses(env) != 0)
         return JNI_ERR;
-#if HAVE_THREAD_LOCAL
-    {
-        struct sigaction act;
-        int i;
-        memset(&act, 0, sizeof(struct sigaction));
-        sigemptyset(&act.sa_mask);
-        act.sa_handler = AcrExceptionHandler;
-        act.sa_flags   = 0;
-#ifdef SA_INTERRUPT
-        act.sa_flags |= SA_INTERRUPT;
-#endif
-        for (i = 0; sig_blocked[i] > 0; i++) {
-            /* Block most signals during SIGSEGV handling
-             */
-            sigaddset(&act.sa_mask, sig_blocked[i]);
-        }
-        /* Handle SIGSEGV signal
-         */
-        sigaction(SIGSEGV, &act, (struct sigaction *)0);
-#if defined(HPUX11) || defined(DARWIN)
-        sigaction(SIGBUS,  &act, (struct sigaction *)0);
-#endif
-    }        
-#endif
     return JNI_VERSION_1_4;
 }
 
@@ -248,6 +287,7 @@ AcrGetJNIEnv()
 #if HAVE_THREAD_LOCAL
 void AcrExceptionHandler(int sig)
 {
+    printf("Exception Handler for %d\n", sig);
     if (sig == SIGSEGV || sig == SIGBUS) {
         if (acr_exception_frame.init) {
             acr_exception_frame.init = 0;
@@ -260,9 +300,22 @@ void AcrExceptionHandler(int sig)
 #endif
             siglongjmp(acr_exception_frame.jump, 1);
         }
-        else {
-            /* TODO: Call original signal handlers.
+        else if (_signalset[sig].sa_handler != SIG_DFL &&
+                 _signalset[sig].sa_handler != SIG_IGN &&
+                 _signalset[sig].sa_handler != SIG_ERR) {
+            AcrDebugPrintf(acr_exception_frame.file,
+                           acr_exception_frame.func,
+                           acr_exception_frame.line,
+                           "Executing default handler for %s", strsignal(sig));
+            /* This should invoke JVM crash report
              */
+            (*_signalset[sig].sa_handler)(sig);
+        }
+        else {
+            AcrDebugPrintf(acr_exception_frame.file,
+                           acr_exception_frame.func,
+                           acr_exception_frame.line,
+                           "Cannot find default handler for %s", strsignal(sig));
             abort();
         }
     }    
@@ -287,6 +340,21 @@ AcrExceptionDescription()
 
 #endif
 
+ACR_JNI_EXPORT(void, Native, fini0)(JNI_STDARGS)
+{
+    int i;
+    struct sigaction *ssp;
+
+    if (_sigs_inited != 0) {
+        _sigs_inited = 0;
+        ssp = _signalset;
+        for (i = 0; i < ACR_NUMSIG; i++) {
+            sigaction(i, ssp, 0);
+            ssp++;
+        }
+    }
+}
+
 #if defined(__GNUC__) || defined(__SUNPRO_C)
 void __attribute__ ((constructor))
 AcrLibraryAttach(void)
@@ -298,6 +366,5 @@ AcrLibraryAttach(void)
 void __attribute__ ((destructor))
 AcrLibraryDetach(void)
 {
-
 }
 #endif

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/native.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/native.c?rev=1091496&r1=1091495&r2=1091496&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/native.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/native.c Tue Apr 12 16:50:41 2011
@@ -15,6 +15,7 @@
  */
 
 #include "acr/clazz.h"
+#include "acr/misc.h"
 
 static jboolean _loaded = JNI_FALSE;
 ACR_JNI_EXPORT(jboolean, Native, isdbg0)(JNI_STDARGS)
@@ -32,14 +33,8 @@ ACR_JNI_EXPORT(jboolean, Native, init0)(
 	return JNI_TRUE;
     if (AcrLoadRuntimeClasses(env) != 0)
         return JNI_FALSE;
+    if (AcrSignalsInit() != 0)
+        return JNI_FALSE;
     _loaded = JNI_TRUE;
     return _loaded;
 }
-
-ACR_JNI_EXPORT(void, Native, finalize0)(JNI_STDARGS)
-{
-    if (_loaded) {
-        _loaded = JNI_FALSE;
-        AcrUnloadRuntimeClasses(env);
-    }
-}

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java?rev=1091496&r1=1091495&r2=1091496&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMain.java Tue Apr 12 16:50:41 2011
@@ -27,4 +27,10 @@ public class TestMain
         Assert.assertTrue(Loader.load());
     }
 
+    @AfterSuite
+    public void shutDown()
+    {
+        Native.terminate();
+    }
+
 }