Index: trunk/include/samhain.h
===================================================================
--- trunk/include/samhain.h	(revision 410)
+++ trunk/include/samhain.h	(revision 411)
@@ -331,4 +331,9 @@
   int    looptime;                 /* timing for main loop            */
   /*@null@*//*@out@*/ char   * timezone;
+
+#ifdef SCREW_IT_UP
+  int sigtrap_max_duration;
+#endif
+
 } sh_struct;
 
Index: trunk/include/sh_unix.h
===================================================================
--- trunk/include/sh_unix.h	(revision 410)
+++ trunk/include/sh_unix.h	(revision 411)
@@ -364,5 +364,4 @@
 
 void   sh_sigtrap_handler (int signum);
-extern volatile int sh_not_traced;
 
 #ifdef HAVE_GETTIMEOFDAY
@@ -377,8 +376,16 @@
 #endif
 #endif
-void sh_set_save_tv();
-void sh_set_untraced(int val);
-int  sh_get_untraced();
-#endif
+#endif
+
+struct sh_sigtrap_variables {
+  int not_traced;
+#ifdef HAVE_GETTIMEOFDAY
+  struct timeval save_tv;
+#endif
+};
+
+struct sh_sigtrap_variables * sh_sigtrap_variables_get();
+
+int sh_sigtrap_max_duration_set (const char * str);
 
 static inline
@@ -399,8 +406,16 @@
 int sh_derr(void)
 {
-  sh_set_untraced(0);
+  struct sh_sigtrap_variables * sigtrap_variables;
+  sigtrap_variables = sh_sigtrap_variables_get();
+  if (sigtrap_variables == NULL) {
+    /* Perhaps, its better to not die, and to continue using Samhain,
+       even if this part does not work. */
+    return (0);
+  }
+
+  sigtrap_variables->not_traced = 0;
 
 #ifdef HAVE_GETTIMEOFDAY
-  sh_set_save_tv();
+  gettimeofday(&sigtrap_variables->save_tv, NULL);
 #endif
 
@@ -409,8 +424,8 @@
   raise(SIGTRAP);
   
-  if (sh_get_untraced() == 0)
+  if (sigtrap_variables->not_traced == 0)
     _exit(5);
 
-  sh_set_untraced(0);
+  sigtrap_variables->not_traced = 0;
   return (0);
 }
Index: trunk/src/samhain.c
===================================================================
--- trunk/src/samhain.c	(revision 410)
+++ trunk/src/samhain.c	(revision 411)
@@ -633,4 +633,8 @@
 
   sh.looptime     = 60;
+
+#ifdef SCREW_IT_UP
+  sh.sigtrap_max_duration = 500000; /* 500ms */
+#endif
 
   /* The struct to hold privileged information.
@@ -1386,7 +1390,4 @@
   sh_error_only_stderr (S_TRUE);
 
-  BREAKEXIT(sh_derr);
-  (void) sh_derr();
-
   /* Check that first three descriptors are open.
    */
@@ -1405,4 +1406,10 @@
   sh.flag.isserver = S_TRUE;
 #endif
+
+  /* --- First check for an attached debugger (after setting
+         sh.sigtrap_max_duration which has to be done before). ---
+   */
+  BREAKEXIT(sh_derr);
+  (void) sh_derr();
 
   /* --- Get local hostname. ---
Index: trunk/src/sh_readconf.c
===================================================================
--- trunk/src/sh_readconf.c	(revision 410)
+++ trunk/src/sh_readconf.c	(revision 411)
@@ -1253,4 +1253,9 @@
     sh_calls_set_sub },
 
+#ifdef SCREW_IT_UP
+  { N_("setsigtrapmaxduration"),        SH_SECTION_MISC,  SH_SECTION_MISC, 
+    sh_sigtrap_max_duration_set },
+#endif
+
   { NULL,    0,   0,  NULL}
 };
Index: trunk/src/sh_unix.c
===================================================================
--- trunk/src/sh_unix.c	(revision 410)
+++ trunk/src/sh_unix.c	(revision 411)
@@ -5457,126 +5457,93 @@
  */
 #if defined(SCREW_IT_UP)
-struct screw_it_up {
-  int not_traced;
+
+#if defined(HAVE_PTHREAD)
+
+static pthread_key_t  gSigtrapVariables_key;
+static pthread_once_t gSigtrapVariables_key_once = PTHREAD_ONCE_INIT;
+
+static inline void make_gSigtrapVariables_key()
+{
+    (void) pthread_key_create(&gSigtrapVariables_key, free);
+}
+
+struct sh_sigtrap_variables * sh_sigtrap_variables_get()
+{
+  void * ptr;
+
+  (void) pthread_once(&gSigtrapVariables_key_once, make_gSigtrapVariables_key);
+ 
+  ptr = pthread_getspecific(gSigtrapVariables_key);
+  if (ptr == NULL) {
+    ptr = malloc(sizeof(struct sh_sigtrap_variables));
+    if (ptr == NULL) {
+      return NULL;
+    }
+    (void) pthread_setspecific(gSigtrapVariables_key, ptr);
+  }
+
+  return (struct sh_sigtrap_variables *) ptr;
+}
+
+/* !defined(HAVE_PTHREAD) */
+#else
+
+static struct sh_sigtrap_variables global_sigtrap_variables;
+struct sh_sigtrap_variables * sh_sigtrap_variables_get()
+{
+  return &global_sigtrap_variables;
+}
+
+#endif
+
+int sh_sigtrap_max_duration_set (const char * str)
+{
+  /* For security (prevent reloading with larger value)
+   * this value can only be set once.
+   */
+  static int once = 0;
+  int i;
+
+  SL_ENTER(_("sh_sigtrap_max_duration_set"));
+
+  i = atoi (str);
+
+  if (i >= 0 && once == 0)
+    {
+      sh.sigtrap_max_duration = i;
+      once = 1;
+    }
+  else
+    {
+      SL_RETURN ((-1), _("sh_sigtrap_max_duration_set"));
+    }
+  SL_RETURN( (0), _("sh_sigtrap_max_duration_set"));
+}
+
+void sh_sigtrap_handler (int signum)
+{
+  struct sh_sigtrap_variables * sigtrap_variables;
+  sigtrap_variables = sh_sigtrap_variables_get();
+  if (sigtrap_variables == NULL) {
+    /* Perhaps, it's better to not die, and to continue using Samhain,
+       even if this part does not work. */
+    return;
+  }
+
 #ifdef HAVE_GETTIMEOFDAY
-  struct timeval save_tv;
-#endif
-};
-
-#if defined(HAVE_PTHREAD)
-
-static pthread_key_t  gSaveTv_key;
-static pthread_once_t gSaveTv_key_once = PTHREAD_ONCE_INIT;
-
-static inline void make_gSaveTv_key()
-{
-    (void) pthread_key_create(&gSaveTv_key, free);
-}
-
-static inline struct screw_it_up * sh_get_screw_it_up()
-{
-  void * ptr;
-  struct screw_it_up * screw;
-
-  (void) pthread_once(&gSaveTv_key_once, make_gSaveTv_key);
- 
-  if ((ptr = pthread_getspecific(gSaveTv_key)) == NULL) 
-    {
-      ptr = malloc(sizeof(struct screw_it_up));
-      if (ptr)
-	{
-	  screw = (struct screw_it_up *) ptr;
-	  (void) pthread_setspecific(gSaveTv_key, ptr);
-	}
-      else
-	{
-	  return NULL;
-	}
-    }
-  else 
-    {
-      screw = (struct screw_it_up *) ptr;
-    }
-  return screw;
-}
-
-#ifdef HAVE_GETTIMEOFDAY
-void sh_set_save_tv()
-{
-  struct screw_it_up * screw = sh_get_screw_it_up();
-  struct timeval * save_tv = &(screw->save_tv);
-  if (save_tv)
-    gettimeofday(save_tv, NULL);
+  {
+    struct timeval  tv;
+    long   difftv;
+    
+    gettimeofday(&tv, NULL);
+    difftv = (tv.tv_sec - sigtrap_variables->save_tv.tv_sec) * 1000000 + 
+      (tv.tv_usec - sigtrap_variables->save_tv.tv_usec);
+    if (difftv > sh.sigtrap_max_duration)
+      raise(SIGKILL);
+  }
+#endif
+
+  sigtrap_variables->not_traced = signum;
   return;
 }
-struct timeval * sh_get_save_tv()
-{
-  struct screw_it_up * screw = sh_get_screw_it_up();
-  struct timeval * save_tv = &(screw->save_tv);
-  return save_tv;
-}
-/* #ifdef HAVE_GETTIMEOFDAY */
-#endif
-
-void sh_set_untraced(int val)
-{
-  struct screw_it_up * screw = sh_get_screw_it_up();
-
-  screw->not_traced = val;
-  return;
-}
-int sh_get_untraced()
-{
-  struct screw_it_up * screw = sh_get_screw_it_up();
-
-  return screw->not_traced;
-}
-
-/* !defined(HAVE_PTHREAD) */
-#else
-
-#ifdef HAVE_GETTIMEOFDAY
-static struct timeval * sSaveTv = NULL;
-static inline struct timeval * sh_get_save_tv()
-{
-  return sSaveTv;
-}
-void sh_set_save_tv()
-{
-  gettimeofday(sSaveTv, NULL);
-}
-/* #ifdef HAVE_GETTIMEOFDAY */
-#endif
-
-volatile int sh_not_traced = 0;
-void sh_set_untraced(int val)
-{
-  sh_not_traced = val;
-  return;
-}
-int sh_get_untraced()
-{
-  return sh_not_traced;
-}
-
-#endif
-
-
-void sh_sigtrap_handler (int signum)
-{
-#ifdef HAVE_GETTIMEOFDAY
-  struct timeval  tv;
-  long   difftv;
-  struct timeval * save_tv = sh_get_save_tv();
-
-  gettimeofday(&tv, NULL);
-  
-  difftv = (tv.tv_sec - save_tv->tv_sec) * 1000000 + 
-    (tv.tv_usec - save_tv->tv_usec);
-  if (difftv > 500000)
-    raise(SIGKILL);
-#endif
-  sh_set_untraced(signum);
-  return;
-}
-#endif
+#endif
