Index: trunk/src/samhain.c
===================================================================
--- trunk/src/samhain.c	(revision 254)
+++ trunk/src/samhain.c	(revision 256)
@@ -376,4 +376,5 @@
   sh.flag.update          = S_FALSE;
   sh.flag.opts            = S_FALSE;
+  sh.flag.started         = S_FALSE;
   if (is_samhainctl_init == S_FALSE)
     sh.flag.isdaemon        = S_FALSE;
@@ -741,4 +742,5 @@
   sh_files_deldirstack ();
   sh_files_delfilestack ();
+  sh_files_delglobstack ();
   sh_hash_hashdelete();
   sh_files_hle_reg (NULL);
@@ -1558,4 +1560,6 @@
   sh_error_only_stderr (S_FALSE);
 
+  sh.flag.started = S_TRUE;
+
   /****************************************************
    *
@@ -1761,4 +1765,5 @@
 	      (void) sh_files_deldirstack ();
 	      (void) sh_files_delfilestack ();
+	      (void) sh_files_delglobstack ();
 	      (void) sh_ignore_clean ();
 	      (void) hash_full_tree ();
@@ -1963,4 +1968,9 @@
 	  (flag_check_1 == 1 || flag_check_2 == 1))
 	{
+	  /* Refresh list files matching glob patterns.
+	   */
+	  if (sh.flag.checkSum != SH_CHECK_INIT)
+	    sh_files_check_globPatterns();
+
 	  /* 
 	   * check directories and files
Index: trunk/src/sh_files.c
===================================================================
--- trunk/src/sh_files.c	(revision 254)
+++ trunk/src/sh_files.c	(revision 256)
@@ -149,4 +149,10 @@
 }
 
+#define SH_LIST_FILE 0
+#define SH_LIST_DIR1 1
+#define SH_LIST_DIR2 2
+
+
+static int which_dirList = SH_LIST_DIR1;
 
 static zAVLTree * zdirListOne   = NULL;
@@ -783,11 +789,189 @@
       aud__exit(FIL__, __LINE__, EXIT_FAILURE);
     }
+
   if (3 == ret)
-    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
-		     fileName);
+    { 
+      if (sh.flag.started != S_TRUE)
+	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
+			 fileName);
+      SH_FREE(fileName);
+      SH_FREE(new_item_ptr);
+    }
 
   SL_RETURN(0, _("sh_files_push_file_int"));
 }
 
+int sh_files_push_dir_int (int class, char * tail, size_t len, int rdepth);
+
+#ifdef HAVE_GLOB_H
+
+typedef struct globstack_entry {
+  char                  * name;
+  int                     class;
+  int                     rdepth;
+  short                   type;
+  /* struct dirstack_entry * next; */
+} sh_globstack_t;
+
+static zAVLTree * zglobList   = NULL;
+
+static void sh_files_pushglob (int class, int type, const char * p, int rdepth)
+{
+  int     globstatus = -1;
+  unsigned int     gloop;
+  glob_t  pglob;
+  
+  SL_ENTER(_("sh_files_pushglob"));
+
+  pglob.gl_offs = 0;
+  globstatus    = glob (p, 0, sh_files_globerr, &pglob);
+  
+  if (globstatus == 0 && pglob.gl_pathc > 0)
+    {
+
+      if (sh.flag.checkSum != SH_CHECK_INIT)
+	{
+	  sh_globstack_t * new_item_ptr;
+	  char  * fileName;
+	  int     ret;
+	  
+	  fileName = sh_util_strdup (p);
+	  
+	  new_item_ptr = (sh_globstack_t *) SH_ALLOC (sizeof(sh_globstack_t));
+	  
+	  new_item_ptr->name           = fileName;
+	  new_item_ptr->class          = class;
+	  new_item_ptr->rdepth         = rdepth;
+	  new_item_ptr->type           = type;
+	  
+	  if (zglobList == NULL)
+	    {
+	      zglobList = zAVLAllocTree (zdirstack_key);
+	      if (zglobList == NULL) 
+		{
+		  (void) safe_logger (0, 0, NULL);
+		  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+		}
+	    }
+      
+	  ret = zAVLInsert (zglobList, new_item_ptr);
+
+	  if (ret != 0) /* already in list */
+	    {
+	      SH_FREE(fileName);
+	      SH_FREE(new_item_ptr);
+	    }
+	}
+
+      for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
+	{
+	  if (type == SH_LIST_FILE)
+	    {
+	      sh_files_push_file_int (class, pglob.gl_pathv[gloop], 
+				      sl_strlen(pglob.gl_pathv[gloop]));
+	    }
+	  else
+	    {
+	      which_dirList = type;
+
+	      sh_files_push_dir_int  (class, pglob.gl_pathv[gloop], 
+				      sl_strlen(pglob.gl_pathv[gloop]), rdepth);
+	    }
+	}
+    }
+  else
+    {
+      char * tmp = sh_util_safe_name (p);
+      
+      if (pglob.gl_pathc == 0
+#ifdef GLOB_NOMATCH
+	  || globstatus == GLOB_NOMATCH
+#endif
+	  )
+	sh_error_handle ((sh.flag.started != S_TRUE) ? SH_ERR_ERR : SH_ERR_NOTICE, 
+			 FIL__, __LINE__, 
+			 globstatus, MSG_FI_GLOB,
+			 _("No matches found"), tmp);
+#ifdef GLOB_NOSPACE
+      else if (globstatus == GLOB_NOSPACE)
+	sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			 globstatus, MSG_FI_GLOB,
+			 _("Out of memory"), tmp);
+#endif
+#ifdef GLOB_ABORTED
+      else if (globstatus == GLOB_ABORTED)
+	sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			 globstatus, MSG_FI_GLOB,
+			 _("Read error"), tmp);
+#endif
+      else 
+	sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
+			 globstatus, MSG_FI_GLOB,
+			 _("Unknown error"), tmp);
+      
+      SH_FREE(tmp);
+      
+    }
+  
+  globfree(&pglob);
+  SL_RET0(_("sh_files_pushglob"));
+  return;
+}
+
+void sh_files_check_globPatterns()
+{
+  sh_globstack_t * testPattern;
+  zAVLCursor   cursor;
+
+  SL_ENTER(_("sh_files_check_globPatterns"));
+
+  for (testPattern = (sh_globstack_t *) zAVLFirst (&cursor, zglobList); testPattern;
+       testPattern = (sh_globstack_t *) zAVLNext  (&cursor))
+    {
+      sh_files_pushglob(testPattern->class, testPattern->type, 
+			testPattern->name, testPattern->rdepth);
+    }
+  SL_RET0(_("sh_files_check_globPatterns"));
+}
+
+/* the destructor
+ */
+void free_globstack (void * inptr)
+{
+  sh_globstack_t * here;
+
+  SL_ENTER(_("free_globstack"));
+  if (inptr == NULL)
+    SL_RET0(_("free_globstack"));
+  else
+    here = (sh_globstack_t *) inptr;
+
+  if (here->name != NULL)
+    SH_FREE(here->name);
+  SH_FREE(here);
+  SL_RET0(_("free_globstack"));
+}
+
+int sh_files_delglobstack ()
+{
+  SL_ENTER(_("sh_files_delfilestack"));
+
+  zAVLFreeTree (zglobList, free_globstack);
+  zglobList = NULL;
+
+  SL_RETURN(0, _("sh_files_delfilestack"));
+}
+  
+
+#else
+void sh_files_check_globPatterns()
+{
+  return;
+}
+int sh_files_delglobstack ()
+{
+  return 0;
+}
+#endif
 
 static int sh_files_pushfile (int class, const char * str_s)
@@ -796,9 +980,4 @@
   char  * tmp;
   char  * p;
-#ifdef HAVE_GLOB_H
-  int     globstatus = -1;
-  unsigned int     gloop;
-  glob_t  pglob;
-#endif
 
   static int reject = 0;
@@ -816,4 +995,5 @@
       sh_files_delfilestack ();
       sh_files_deldirstack ();
+      sh_files_delglobstack ();
       reject = 1;
     }
@@ -871,47 +1051,5 @@
   else
     {
-      pglob.gl_offs = 0;
-      globstatus    = glob (p, 0, sh_files_globerr, &pglob);
-
-      if (globstatus == 0 && pglob.gl_pathc > 0)
-	{
-	  for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
-	    sh_files_push_file_int (class, pglob.gl_pathv[gloop], 
-				    sl_strlen(pglob.gl_pathv[gloop]));
-	}
-      else
-	{
-	  tmp = sh_util_safe_name (p);
-
-	  if (pglob.gl_pathc == 0
-#ifdef GLOB_NOMATCH
-	      || globstatus == GLOB_NOMATCH
-#endif
-	      )
-	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
-			     globstatus, MSG_FI_GLOB,
-			     _("No matches found"), tmp);
-#ifdef GLOB_NOSPACE
-	  else if (globstatus == GLOB_NOSPACE)
-	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			     globstatus, MSG_FI_GLOB,
-			     _("Out of memory"), tmp);
-#endif
-#ifdef GLOB_ABORTED
-	  else if (globstatus == GLOB_ABORTED)
-	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			     globstatus, MSG_FI_GLOB,
-			     _("Read error"), tmp);
-#endif
-	  else 
-	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			     globstatus, MSG_FI_GLOB,
-			     _("Unknown error"), tmp);
-
-	  SH_FREE(tmp);
-
-	}
-
-      globfree(&pglob);
+      sh_files_pushglob (class, SH_LIST_FILE, p, 0);
     }
 
@@ -1137,12 +1275,10 @@
 }
 
-static int which_dirList = 1;
-
 int set_dirList (int which)
 {
   if (which == 2)
-    which_dirList = 2;
+    which_dirList = SH_LIST_DIR2;
   else
-    which_dirList = 1;
+    which_dirList = SH_LIST_DIR1;
   return 0;
 }
@@ -1170,5 +1306,5 @@
   new_item_ptr->childs_checked = S_FALSE;
 
-  if (which_dirList == 1)
+  if (which_dirList == SH_LIST_DIR1)
     {
       tree = zdirListOne;
@@ -1187,5 +1323,5 @@
 	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
 	}
-      if (which_dirList == 1)
+      if (which_dirList == SH_LIST_DIR1)
 	zdirListOne = tree;
       else
@@ -1201,6 +1337,11 @@
     }
   if (3 == ret)
-    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
-		     dirName);
+    { 
+      if (sh.flag.started != S_TRUE)
+	sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_FI_DOUBLE,
+			 dirName);
+      SH_FREE(dirName);
+      SH_FREE(new_item_ptr);
+    }
 
   SL_RETURN(0, _("sh_files_push_dir_int"));
@@ -1215,10 +1356,4 @@
   char  * p;
 
-#ifdef HAVE_GLOB_H
-  glob_t  pglob;
-  int     globstatus = -1;
-  unsigned int     gloop;
-#endif
-
   SL_ENTER(_("sh_files_pushdir"));
 
@@ -1226,4 +1361,5 @@
     sh_files_delfilestack ();
     sh_files_deldirstack ();
+    sh_files_delglobstack ();
   }
 
@@ -1292,47 +1428,5 @@
   else
     {
-      pglob.gl_offs = 0;
-      globstatus    = glob (tail, 0, sh_files_globerr, &pglob);
-
-      if (globstatus == 0 && pglob.gl_pathc > 0)
-	{
-	  for (gloop = 0; gloop < (unsigned int) pglob.gl_pathc; ++gloop)
-	    sh_files_push_dir_int (class, 
-				   pglob.gl_pathv[gloop], 
-				   sl_strlen(pglob.gl_pathv[gloop]), 
-				   rdepth);
-	}
-      else
-	{
-	  tmp = sh_util_safe_name (tail);
-
-	  if (pglob.gl_pathc == 0
-#ifdef GLOB_NOMATCH
-	      || globstatus == GLOB_NOMATCH
-#endif
-	      )
-	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, 
-			     globstatus, MSG_FI_GLOB,
-			     _("No matches found"), tmp);
-#ifdef GLOB_NOSPACE
-	  else if (globstatus == GLOB_NOSPACE)
-	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			     globstatus, MSG_FI_GLOB,
-			     _("Out of memory"), tmp);
-#endif
-#ifdef GLOB_ABORTED
-	  else if (globstatus == GLOB_ABORTED)
-	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			     globstatus, MSG_FI_GLOB,
-			     _("Read error"), tmp);
-#endif
-	  else 
-	    sh_error_handle (SH_ERR_ERR, FIL__, __LINE__,
-			     globstatus, MSG_FI_GLOB,
-			     _("Unknown error"), tmp);
-	  SH_FREE(tmp);
-	}
-
-      globfree(&pglob);
+      sh_files_pushglob (class, which_dirList, tail, rdepth);
     }
 #else  
Index: trunk/src/sh_mem.c
===================================================================
--- trunk/src/sh_mem.c	(revision 254)
+++ trunk/src/sh_mem.c	(revision 256)
@@ -119,6 +119,4 @@
   SL_ENTER(_("sh_mem_stat"));
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
 
   if (Alloc_Count == Free_Count) 
@@ -126,5 +124,5 @@
       sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MSTAMP,
 		       Mem_Max, Mem_Current);
-      goto out;
+      SL_RET0(_("sh_mem_stat"));
     }
     
@@ -134,4 +132,7 @@
 		   Mem_Max, Mem_Current);
 
+  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
+  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
+
   this = memlist;
 
@@ -142,6 +143,5 @@
       this = this->next;
     }
- out:
-  ; /* label at end of compound statement */
+
   SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
   SL_RET0(_("sh_mem_stat"));
@@ -155,9 +155,9 @@
   SL_ENTER(_("sh_mem_check"));
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
   sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_MSTAMP,
 		   Mem_Max, Mem_Current);
+
+  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
+  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
 
   this = memlist;
@@ -257,4 +257,5 @@
 }
 
+static void ** sh_mem_dummy_a;
 
 void sh_mem_free (void * aa, char * file, int line)
@@ -264,20 +265,25 @@
   unsigned long        size   = 0;
   void      * a;
+  volatile int         flag = 0;
+
   SL_ENTER(_("sh_mem_free"));
 
-  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
-  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
-
   a      = aa;
+  sh_mem_dummy_a = &a;
+
+
+  if ( a == NULL ) 
+    {
+      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL,
+		       file, line);
+      SL_RET0(_("sh_mem_free"));
+    }
+    
+  SH_MUTEX_RECURSIVE_INIT(mutex_mem);
+  SH_MUTEX_RECURSIVE_LOCK(mutex_mem);
+
   this   = memlist;
   before = memlist;
   
-  if ( a == NULL ) 
-    {
-      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MNULL,
-		       file, line);
-      goto out;
-    }
-    
   /* -- Find record. -- 
    */
@@ -292,6 +298,5 @@
   if (this == NULL) 
     {
-      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MREC,
-		       file, line);
+      flag = 1;
       goto out;
     } 
@@ -327,4 +332,7 @@
   ; /* label at end of compound statement */
   SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
+  if (flag != 0)
+    sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_MREC,
+		     file, line);
   SL_RET0(_("sh_mem_free"));
 }
