Index: trunk/configure.ac
===================================================================
--- trunk/configure.ac	(revision 251)
+++ trunk/configure.ac	(revision 252)
@@ -12,5 +12,5 @@
 dnl start
 dnl
-AM_INIT_AUTOMAKE(samhain, 2.5.9c)
+AM_INIT_AUTOMAKE(samhain, 2.5.10)
 AC_DEFINE([SAMHAIN], 1, [Application is samhain])
 AC_CANONICAL_HOST
Index: trunk/docs/Changelog
===================================================================
--- trunk/docs/Changelog	(revision 251)
+++ trunk/docs/Changelog	(revision 252)
@@ -1,2 +1,6 @@
+2.5.10:
+	* sh_suidchk.c: handle $HOME/.gvfs mount gracefully
+	* slib.c: fix race condition caused by closing a stream and the fd
+
 2.5.9c:
 	* move stale file record error message closer to problem zone
Index: trunk/include/slib.h
===================================================================
--- trunk/include/slib.h	(revision 251)
+++ trunk/include/slib.h	(revision 252)
@@ -329,9 +329,16 @@
 #define SL_OFILE_SIZE 32
 
+  char * sl_check_badfd();
   char * sl_check_stale();
 
+  /* Create a file record for an open file
+   */
   SL_TICKET sl_make_ticket (const char * ofile, int oline,
-			    int fd, const char * filename);
+			    int fd, const char * filename, FILE * stream);
  
+  /* Get the pointer to a stream. If none exists yet, open it
+   */
+  FILE * sl_stream (SL_TICKET ticket, char * mode);
+
   /* Open for writing.
    */
@@ -388,4 +395,12 @@
    */
   int sl_close (SL_TICKET ticket);
+
+  /* Close file descriptor.
+   */
+  int sl_close_fd (const char * file, int line, int fd);
+
+  /* Close stream.
+   */
+  int sl_fclose (const char * file, int line, FILE * fp);
 
   /* Unlink file.
Index: trunk/src/samhain.c
===================================================================
--- trunk/src/samhain.c	(revision 251)
+++ trunk/src/samhain.c	(revision 252)
@@ -920,6 +920,6 @@
     { if (errno != ENOENT) perror(_("fopen")); return 0; }
   if (NULL == fgets(line, sizeof(line), fp))
-    { perror(_("fgets")); (void) fclose(fp); return 0; }
-  (void) fclose(fp); 
+    { perror(_("fgets")); (void) sl_fclose(FIL__, __LINE__, fp); return 0; }
+  (void) sl_fclose(FIL__, __LINE__, fp); 
   p = line; 
   while (*p == ' '  || *p == '\f' || *p == '\n' || 
@@ -1003,5 +1003,5 @@
 	exit (1);
       case  0:
-	if (0 != close (0))
+	if (0 != sl_close_fd (FIL__, __LINE__, 0))
 	  {
 	    _exit(4);
@@ -2100,6 +2100,14 @@
 
       {
-	char * stale = sl_check_stale();
-
+	char * stale;
+
+	stale = sl_check_stale();
+	if (stale)
+	  {
+	    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			    stale, _("sl_check_stale"));
+	  }
+
+	stale = sl_check_badfd();
 	if (stale)
 	  {
Index: trunk/src/sh_entropy.c
===================================================================
--- trunk/src/sh_entropy.c	(revision 251)
+++ trunk/src/sh_entropy.c	(revision 252)
@@ -138,5 +138,5 @@
     if( do_restart ) {
         if( fd != -1 ) {
-            close( fd );
+            sl_close_fd(FIL__, __LINE__,  fd );
             fd = -1;
         }
@@ -195,5 +195,5 @@
 			     _("sh_entropy") ); 
 	    SH_FREE(name);
-	    close(fd);
+	    sl_close_fd(FIL__, __LINE__, fd);
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -212,5 +212,5 @@
 			     _("cannot write to EGD"),
 			     _("sh_entropy") );
-	    close(fd);
+	    sl_close_fd(FIL__, __LINE__, fd);
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -254,5 +254,5 @@
 			     _("cannot write to EGD"),
 			     _("sh_entropy") );
-	    close(fd);
+	    sl_close_fd(FIL__, __LINE__, fd);
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -274,5 +274,5 @@
     }
     memset(buffer, 0, sizeof(buffer) );
-    close(fd);
+    sl_close_fd(FIL__, __LINE__, fd);
     SL_RETURN( 0, _("sh_entropy") ); /* success */
 }
@@ -310,5 +310,5 @@
     m_count = 0;
 
-  close(fd2);
+  sl_close_fd(FIL__, __LINE__, fd2);
 
   TPT((0, FIL__, __LINE__, _("msg=<read_mbytes: OK>\n"))); 
@@ -559,6 +559,6 @@
    */
   if (source->pid == (pid_t) - 1) {
-    close(pipedes[0]);
-    close(pipedes[1]);
+    sl_close_fd(FIL__, __LINE__, pipedes[0]);
+    sl_close_fd(FIL__, __LINE__, pipedes[1]);
     if (envp[0] != NULL) free(envp[0]);
     SL_RETURN(NULL, _("sh_popen"));
@@ -580,6 +580,6 @@
       /* close the pipe descriptors 
        */
-      close   (pipedes[STDIN_FILENO]);
-      close   (pipedes[STDOUT_FILENO]);
+      sl_close_fd   (FIL__, __LINE__, pipedes[STDIN_FILENO]);
+      sl_close_fd   (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
 
       /* don't leak file descriptors
@@ -649,5 +649,5 @@
       free(envp[0]);
 
-    close (pipedes[STDOUT_FILENO]);
+    sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
     retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
 
@@ -657,5 +657,5 @@
       {
         aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
-	close (pipedes[STDOUT_FILENO]);
+	sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
         waitpid (source->pid, NULL, 0);
         source->pid = 0;
@@ -676,5 +676,5 @@
     SL_ENTER(_("sh_pclose"));
 
-    retval = fclose(source->pipe);
+    retval = sl_fclose(FIL__, __LINE__, source->pipe);
     if (retval)
       {
Index: trunk/src/sh_err_console.c
===================================================================
--- trunk/src/sh_err_console.c	(revision 251)
+++ trunk/src/sh_err_console.c	(revision 252)
@@ -378,5 +378,5 @@
 		val_return = write(fd[cc], "\r\n",              2);
 	      } while (val_return < 0 && errno == EINTR);
-	      (void) close(fd[cc]);
+	      (void) sl_close_fd(FIL__, __LINE__, fd[cc]);
 	      service_failure[cc] = 0;
 	    }
Index: trunk/src/sh_extern.c
===================================================================
--- trunk/src/sh_extern.c	(revision 251)
+++ trunk/src/sh_extern.c	(revision 252)
@@ -40,5 +40,5 @@
 static FILE * pdbgc = NULL;
 #define PDBG_OPEN    if (pdbg == NULL) pdbg = fopen(PDGBFILE"main",  "a")  
-#define PDBG_CLOSE   fclose (pdbg); pdbg = NULL
+#define PDBG_CLOSE   sl_fclose (FIL__, __LINE__, pdbg); pdbg = NULL
 #define PDBG(arg)    fprintf(pdbg,  "PDBG: step %d\n", arg); fflush(pdbg)
 #define PDBG_D(arg)  fprintf(pdbg,  "PDBG: %d\n", arg); fflush(pdbg)
@@ -46,5 +46,5 @@
 
 #define PDBGC_OPEN   if (pdbgc == NULL) pdbgc = fopen(PDGBFILE"child", "a")  
-#define PDBGC_CLOSE  fclose (pdbgc); pdbgc = NULL
+#define PDBGC_CLOSE  sl_fclose (FIL__, __LINE__, pdbgc); pdbgc = NULL
 #define PDBGC(arg)   fprintf(pdbgc, "PDBGC: step %d\n", arg); fflush(pdbgc)
 #define PDBGC_D(arg) fprintf(pdbgc, "PDBGC: %d\n", arg); fflush(pdbgc)
@@ -238,6 +238,6 @@
       PDBG_S("fork() failure");
       /*@-usedef@*/
-      (void) close(pipedes[0]);
-      (void) close(pipedes[1]);
+      (void) sl_close_fd(FIL__, __LINE__, pipedes[0]);
+      (void) sl_close_fd(FIL__, __LINE__, pipedes[1]);
       /*@+usedef@*/
       errnum = errno;
@@ -297,6 +297,6 @@
 	  /* close the pipe descriptors 
 	   */
-	  (void) close   (pipedes[STDIN_FILENO]);
-	  (void) close   (pipedes[STDOUT_FILENO]);
+	  (void) sl_close_fd   (FIL__, __LINE__, pipedes[STDIN_FILENO]);
+	  (void) sl_close_fd   (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
 	  
 	  /* don't leak file descriptors
@@ -442,5 +442,5 @@
 		sl_close(fd);
 	      else if (pfd != -1)
-		close(pfd);
+		sl_close_fd(FIL__, __LINE__, pfd);
 	    }
 #endif
@@ -496,5 +496,5 @@
     {
       PDBG_S("is w");
-      (void) close (pipedes[STDIN_FILENO]);
+      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
       (void) retry_fcntl (FIL__, __LINE__, pipedes[STDOUT_FILENO], 
 			  F_SETFD, FD_CLOEXEC);
@@ -504,5 +504,5 @@
     {
       PDBG_S("is r");
-      (void) close (pipedes[STDOUT_FILENO]);
+      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
       (void) retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], 
 			  F_SETFD, FD_CLOEXEC);
@@ -526,6 +526,6 @@
 
       (void) aud_kill (FIL__, __LINE__, task->pid, SIGKILL);
-      (void) close (pipedes[STDOUT_FILENO]);
-      (void) close (pipedes[STDIN_FILENO]);
+      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
+      (void) sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
       (void) waitpid (task->pid, NULL, 0);
       task->pid = 0;
@@ -541,5 +541,5 @@
   PDBG_D(task->pipeFD);
 
-  task->pipeTI = sl_make_ticket(FIL__, __LINE__, task->pipeFD, _("pipe"));
+  task->pipeTI = sl_make_ticket(FIL__, __LINE__, task->pipeFD, _("pipe"), outf);
 
   flags = (int) retry_fcntl (FIL__, __LINE__, task->pipeFD, F_GETFL, 0);
@@ -572,5 +572,4 @@
   PDBG_S(" -> pclose");
   (void) fflush(task->pipe);
-  (void) fclose(task->pipe);
   if (!SL_ISERROR(task->pipeTI))
     (void) sl_close(task->pipeTI);
Index: trunk/src/sh_forward.c
===================================================================
--- trunk/src/sh_forward.c	(revision 251)
+++ trunk/src/sh_forward.c	(revision 252)
@@ -1753,5 +1753,5 @@
 		}
 
-	      (void) close (sockfd);
+	      (void) sl_close_fd (FIL__, __LINE__, sockfd);
 	      memset(answer, 0, TRANS_BYTES + 256);
 	      MUNLOCK(answer, TRANS_BYTES + 256);
@@ -1763,5 +1763,5 @@
 	}
 
-      (void) close (sockfd);
+      (void) sl_close_fd (FIL__, __LINE__, sockfd);
       memset(answer, 0, TRANS_BYTES + 256);
       MUNLOCK(answer, TRANS_BYTES + 256);
@@ -1775,5 +1775,5 @@
       
  
-  (void) close (sockfd);
+  (void) sl_close_fd (FIL__, __LINE__, sockfd);
   memset(answer, 0, 512);
   MUNLOCK(answer, 512);
@@ -2203,5 +2203,5 @@
   if (conn->fd != (-1))
     {
-      close (conn->fd);
+      sl_close_fd (FIL__, __LINE__, conn->fd);
       conn->fd            = -1;
     }
@@ -4681,5 +4681,5 @@
 	  newconn->fd    = -1;
 	  newconn->state = CONN_FREE;
-	  close(rc);
+	  sl_close_fd(FIL__, __LINE__, rc);
 	  SL_RETURN( (-1), _("sh_forward_accept"));
 	}
@@ -4698,5 +4698,5 @@
 	  newconn->fd    = -1;
 	  newconn->state = CONN_FREE;
-	  close(rc);
+	  sl_close_fd(FIL__, __LINE__, rc);
 	  SL_RETURN( (-1), _("sh_forward_accept"));
 	}
@@ -5780,5 +5780,5 @@
 	   */
 	  TPT(( 0, FIL__, __LINE__, _("msg=<close syslog socket>\n")));
-	  close(syslog_sock);
+	  sl_close_fd(FIL__, __LINE__, syslog_sock);
 	  syslog_sock = -1;
 	}
@@ -5830,5 +5830,5 @@
       errnum = errno;
       sh_forward_printerr (_("syslog bind"), errnum, 514, __LINE__);
-      close(sock);
+      sl_close_fd(FIL__, __LINE__, sock);
       SL_RETURN((-1), _("create_syslog_socket"));
     }
Index: trunk/src/sh_gpg.c
===================================================================
--- trunk/src/sh_gpg.c	(revision 251)
+++ trunk/src/sh_gpg.c	(revision 252)
@@ -88,5 +88,5 @@
 FILE * pdbgc;
 #define PDBG_OPEN    pdbg = fopen(PDGBFILE"main",  "a")  
-#define PDBG_CLOSE   fclose (pdbg)
+#define PDBG_CLOSE   sl_fclose (FIL__, __LINE__, pdbg)
 #define PDBG(arg)    fprintf(pdbg,  "PDBG: step %d\n", arg); fflush(pdbg)
 #define PDBG_D(arg)  fprintf(pdbg,  "PDBG: %d\n", arg); fflush(pdbg)
@@ -94,5 +94,5 @@
 
 #define PDBGC_OPEN   pdbgc = fopen(PDGBFILE"child", "a")  
-#define PDBGC_CLOSE  fclose (pdbgc)
+#define PDBGC_CLOSE  sl_fclose (FIL__, __LINE__, pdbgc)
 #define PDBGC(arg)   fprintf(pdbgc, "PDBG: step %d\n", arg); fflush(pdbgc)
 #define PDBGC_D(arg) fprintf(pdbgc, "PDBG: %d\n", arg); fflush(pdbgc)
@@ -446,6 +446,6 @@
   if (source->pid == (pid_t) - 1) 
     {
-      close(pipedes[0]);
-      close(pipedes[1]);
+      sl_close_fd(FIL__, __LINE__, pipedes[0]);
+      sl_close_fd(FIL__, __LINE__, pipedes[1]);
       if (envp[0] != NULL) 
 	free(envp[0]);
@@ -468,6 +468,6 @@
       /* close the pipe descriptors 
        */
-      close (pipedes[STDIN_FILENO]);
-      close (pipedes[STDOUT_FILENO]);
+      sl_close_fd (FIL__, __LINE__, pipedes[STDIN_FILENO]);
+      sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
       
 
@@ -618,5 +618,5 @@
     free(envp[0]);
 
-  close (pipedes[STDOUT_FILENO]);
+  sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
   retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFD, FD_CLOEXEC);
   retry_fcntl (FIL__, __LINE__, pipedes[STDIN_FILENO], F_SETFL,  O_NONBLOCK);
@@ -627,5 +627,5 @@
     {
       aud_kill (FIL__, __LINE__, source->pid, SIGKILL);
-      close (pipedes[STDOUT_FILENO]);
+      sl_close_fd (FIL__, __LINE__, pipedes[STDOUT_FILENO]);
       waitpid (source->pid, NULL, 0);
       source->pid = 0;
@@ -643,5 +643,5 @@
   SL_ENTER(_("sh_gpg_pclose"));
 
-  status = fclose(source->pipe);
+  status = sl_fclose(FIL__, __LINE__, source->pipe);
   if (status)
     SL_RETURN( (-1), _("sh_gpg_pclose"));
Index: trunk/src/sh_hash.c
===================================================================
--- trunk/src/sh_hash.c	(revision 251)
+++ trunk/src/sh_hash.c	(revision 252)
@@ -786,5 +786,5 @@
 static void sh_hash_getline_end (void)
 {
-  fclose (sh_fin_fd);
+  sl_fclose (FIL__, __LINE__, sh_fin_fd);
   sh_fin_fd = NULL;
   return;
@@ -816,5 +816,5 @@
   if (sh_fin_fd != NULL)
     {
-      fclose (sh_fin_fd);
+      sl_fclose (FIL__, __LINE__, sh_fin_fd);
       sh_fin_fd = NULL;
     }
@@ -1307,5 +1307,5 @@
   SH_FREE(buf);
   sl_close(fd);
-  fclose(fin_cp);
+  sl_fclose(FIL__, __LINE__, fin_cp); /* fin_cp = fdopen(dup(), "rb"); */
 
   fd = fdTmp;
Index: trunk/src/sh_kern.c
===================================================================
--- trunk/src/sh_kern.c	(revision 251)
+++ trunk/src/sh_kern.c	(revision 252)
@@ -559,5 +559,5 @@
   struct proc_dir_entry   proc_root_dir;
 
-  int status = close(mpipe[0]);
+  int status = sl_close_fd(FIL__, __LINE__, mpipe[0]);
 
   setpgid(0, 0);
@@ -710,5 +710,5 @@
   /* Close reading side of pipe, and wait some milliseconds
    */
-  close (mpipe[1]);
+  sl_close_fd (FIL__, __LINE__, mpipe[1]);
   retry_msleep (0, ShKernDelay); /* milliseconds */
 
@@ -789,5 +789,5 @@
 	status = WEXITSTATUS(status);
     }
-  close (mpipe[0]);
+  sl_close_fd (FIL__, __LINE__, mpipe[0]);
   if (res <= 0)
     {
@@ -989,5 +989,5 @@
 	    status = write( fd, "1", 1 );
 	  } while (status < 0 && errno == EINTR);
-	  close ( fd );
+	  sl_close_fd (FIL__, __LINE__,  fd );
 
 	  if (status > 0)
@@ -1001,5 +1001,5 @@
 		    status = write( fd, "0", 1 );
 		  } while (status < 0 && errno == EINTR);
-		  close ( fd );
+		  sl_close_fd (FIL__, __LINE__,  fd );
 		}
 	    }
@@ -1385,5 +1385,5 @@
 	   */
 	default:
-	  close(kd);
+	  sl_close_fd(FIL__, __LINE__, kd);
 	  status = read_from_child(mpid, mpipe, &kinfo);
 	  break;
Index: trunk/src/sh_log_check.c
===================================================================
--- trunk/src/sh_log_check.c	(revision 251)
+++ trunk/src/sh_log_check.c	(revision 252)
@@ -108,10 +108,10 @@
 	  if (1 != fwrite(&save_rec, sizeof(struct logfile_record), 1, fd))
 	    {
-	      (void) fclose(fd);
+	      (void) sl_fclose(FIL__, __LINE__, fd);
 	      (void) remove(path);
 	    }
 	  else
 	    {
-	      (void) fclose(fd);
+	      (void) sl_fclose(FIL__, __LINE__, fd);
 	    }
 	}
@@ -144,5 +144,5 @@
 		}
 	    }
-	  (void) fclose(fd);
+	  (void) sl_fclose(FIL__, __LINE__, fd);
 	}
       SH_FREE(path);
@@ -329,5 +329,5 @@
 
       if (thisfile->fp)
-	fclose(thisfile->fp);
+	sl_fclose(FIL__, __LINE__, thisfile->fp);
       if (thisfile->filename)
 	SH_FREE(thisfile->filename);
@@ -614,5 +614,5 @@
 	{
 	  fgetpos(logfile->fp, &(logfile->offset));
-	  fclose(logfile->fp);
+	  sl_fclose(FIL__, __LINE__, logfile->fp);
 	  logfile->fp = NULL;
 	  sh_string_destroy(&s);
@@ -690,5 +690,5 @@
 	{
 	  fgetpos(logfile->fp, &(logfile->offset));
-	  fclose(logfile->fp);
+	  sl_fclose(FIL__, __LINE__, logfile->fp);
 	  logfile->fp = NULL;
 	  sh_string_destroy(&s);
@@ -744,5 +744,5 @@
 	    }
 	  fgetpos(logfile->fp, &(logfile->offset));
-	  fclose(logfile->fp);
+	  sl_fclose(FIL__, __LINE__, logfile->fp);
 	  logfile->fp = NULL;
 	  memset(s, '\0', size);
@@ -1168,5 +1168,5 @@
     }
 
-  fclose(fp); remove(template);
+  sl_fclose(FIL__, __LINE__, fp); remove(template);
 
 
Index: trunk/src/sh_mail.c
===================================================================
--- trunk/src/sh_mail.c	(revision 251)
+++ trunk/src/sh_mail.c	(revision 252)
@@ -734,5 +734,5 @@
 		if (connfile != NULL)
 		  {
-		    (void) fclose (connfile);
+		    (void) sl_fclose (FIL__, __LINE__, connfile);
 		    connfile = NULL;
 		  }
@@ -795,5 +795,5 @@
 	if (connfile != NULL)
 	  {
-	    (void) fclose (connfile);
+	    (void) sl_fclose (FIL__, __LINE__, connfile);
 	    connfile = NULL;
 	  }
@@ -1091,5 +1091,5 @@
     {
       TPT(( 0, FIL__, __LINE__, _("msg=<fdopen() failed>\n")));
-      (void) close(fd);
+      (void) sl_close_fd(FIL__, __LINE__, fd);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1105,5 +1105,5 @@
 		      _("mail"), sh.host.name);
       TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
-      (void) fclose(connFile);
+      (void) sl_fclose(FIL__, __LINE__, connFile);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1135,5 +1135,5 @@
 
       TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
-      (void) fclose(connFile);
+      (void) sl_fclose(FIL__, __LINE__, connFile);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1167,5 +1167,5 @@
 		      _("mail"), this_address);
       TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
-      (void) fclose(connFile);
+      (void) sl_fclose(FIL__, __LINE__, connFile);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
@@ -1188,5 +1188,5 @@
 			  _("mail"), address);
 	  TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
-	  (void) fclose(connFile);
+	  (void) sl_fclose(FIL__, __LINE__, connFile);
 	  SL_RETURN( NULL, _("sh_mail_start_conn"));
 	}
@@ -1234,5 +1234,5 @@
       if (ecount == address_num)
 	{
-	  (void) fclose(connFile);
+	  (void) sl_fclose(FIL__, __LINE__, connFile);
 	  SL_RETURN( NULL, _("sh_mail_start_conn"));
 	}
@@ -1253,5 +1253,5 @@
 		      _("mail"), address);
       TPT(( 0, FIL__, __LINE__, _("msg=<Timeout.>\n")));
-      (void) fclose(connFile);
+      (void) sl_fclose(FIL__, __LINE__, connFile);
       SL_RETURN( NULL, _("sh_mail_start_conn"));
     }
Index: trunk/src/sh_mem.c
===================================================================
--- trunk/src/sh_mem.c	(revision 251)
+++ trunk/src/sh_mem.c	(revision 252)
@@ -100,5 +100,5 @@
       this = this->next;
     }
-  fclose(fd);
+  sl_fclose(FIL__, __LINE__, fd);
 
   SH_MUTEX_RECURSIVE_UNLOCK(mutex_mem);
Index: trunk/src/sh_mounts.c
===================================================================
--- trunk/src/sh_mounts.c	(revision 251)
+++ trunk/src/sh_mounts.c	(revision 252)
@@ -749,5 +749,5 @@
 	}
 
-	fclose(tab);
+	sl_fclose(FIL__, __LINE__, tab);
 }
 #endif
@@ -806,5 +806,5 @@
         }
 
-        (void) fclose(tab);
+        (void) sl_fclose(FIL__, __LINE__, tab);
 	aix_fs_get (NULL, NULL); /* reset */
 }
Index: trunk/src/sh_port2proc.c
===================================================================
--- trunk/src/sh_port2proc.c	(revision 251)
+++ trunk/src/sh_port2proc.c	(revision 252)
@@ -182,10 +182,10 @@
               if (*ptr == '\0' || *ptr == '\n')
                 {
-                  fclose(fd);
+                  sl_fclose(FIL__, __LINE__, fd);
                   *procpid = (size_t) pid;
                   SL_RETURN(0, _("proc_max_pid"));
                 }
             }
-          fclose(fd);
+          sl_fclose(FIL__, __LINE__, fd);
         }
     }
@@ -415,5 +415,5 @@
 			  }
 #endif
-			  fclose(fd);
+			  sl_fclose(FIL__, __LINE__, fd);
 			  *pid = (unsigned long) new->pid;
 			  if (new->path)
@@ -432,5 +432,5 @@
 	    }
 	}
-      fclose(fd);
+      sl_fclose(FIL__, __LINE__, fd);
     }
  err_out:
Index: trunk/src/sh_portcheck.c
===================================================================
--- trunk/src/sh_portcheck.c	(revision 251)
+++ trunk/src/sh_portcheck.c	(revision 252)
@@ -312,5 +312,5 @@
 		      SH_FREE(splits);
 		      sh_string_destroy(&s);
-		      fclose(fp);
+		      sl_fclose(FIL__, __LINE__, fp);
 		      return buf;
 		    }
@@ -320,5 +320,5 @@
 	}
       sh_string_destroy(&s);
-      fclose(fp);
+      sl_fclose(FIL__, __LINE__, fp);
     }
   return NULL;
@@ -360,5 +360,5 @@
 			  SH_FREE(splits);
 			  sh_string_destroy(&s);
-			  fclose(fp);
+			  sl_fclose(FIL__, __LINE__, fp);
 			  return buf;
 			}
@@ -369,5 +369,5 @@
 	}
       sh_string_destroy(&s);
-      fclose(fp);
+      sl_fclose(FIL__, __LINE__, fp);
     }
   return NULL;
@@ -874,5 +874,5 @@
 	}
     }
-  close (fd);
+  sl_close_fd (FIL__, __LINE__, fd);
   return 0;
 }
@@ -988,5 +988,5 @@
 		port);
      }
-  close (fd);
+  sl_close_fd (FIL__, __LINE__, fd);
   return 0;
 }
@@ -1226,5 +1226,4 @@
   for (port = min_port; port <= max_port; ++port) 
     {
-
       if ((sock = socket(AF_INET, type, protocol)) < 0 )
 	{
@@ -1265,5 +1264,5 @@
 	  /* we can bind the port, thus it is unused
 	   */
-	  close (sock);
+	  sl_close_fd (FIL__, __LINE__, sock);
 	}
       else
@@ -1290,5 +1289,5 @@
 #endif
 	    }
-	  close (sock);
+	  sl_close_fd (FIL__, __LINE__, sock);
 	}
     }
Index: trunk/src/sh_processcheck.c
===================================================================
--- trunk/src/sh_processcheck.c	(revision 251)
+++ trunk/src/sh_processcheck.c	(revision 252)
@@ -683,10 +683,10 @@
 	      if (*ptr == '\0' || *ptr == '\n')
 		{
-		  fclose(fd);
+		  sl_fclose(FIL__, __LINE__, fd);
 		  *procpid = (size_t) pid;
 		  SL_RETURN(0, _("proc_max_pid"));
 		}
 	    }
-	  fclose(fd);
+	  sl_fclose(FIL__, __LINE__, fd);
 	}
     }
Index: trunk/src/sh_socket.c
===================================================================
--- trunk/src/sh_socket.c	(revision 251)
+++ trunk/src/sh_socket.c	(revision 252)
@@ -396,5 +396,5 @@
   if (sizeof(name.sun_path) < (1 + sl_strlen(sh_sockname)))
     {
-      close(pf_unix_fd); pf_unix_fd = -1;
+      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
 		       _("PID dir path too long"), 
@@ -418,5 +418,5 @@
       if (sh_socket_remove() < 0) 
 	{
-	  close(pf_unix_fd); pf_unix_fd = -1;
+	  sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
 	  sh_error_handle ((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
 			   _("Unlink of socket failed, maybe path not trusted"), 
@@ -428,5 +428,5 @@
   if (bind ((pf_unix_fd), (struct sockaddr *) &name, size) < 0)
     {
-      close(pf_unix_fd); pf_unix_fd = -1;
+      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
@@ -439,5 +439,5 @@
 		      &optval, sizeof(optval)))
     {
-      close(pf_unix_fd); pf_unix_fd = -1;
+      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
@@ -450,5 +450,5 @@
   if (flags < 0)
     {
-      close(pf_unix_fd); pf_unix_fd = -1;
+      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
@@ -460,5 +460,5 @@
   if (flags < 0)
     {
-      close(pf_unix_fd); pf_unix_fd = -1;
+      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
@@ -469,5 +469,5 @@
   if (0 != listen(pf_unix_fd, 5))
     {
-      close(pf_unix_fd); pf_unix_fd = -1;
+      sl_close_fd(FIL__, __LINE__, pf_unix_fd); pf_unix_fd = -1;
       sh_error_handle ((-1), FIL__, __LINE__, errno, MSG_E_SUBGEN,
 		       sh_error_message (errno, errbuf, sizeof(errbuf)), 
@@ -630,5 +630,5 @@
 			sh_error_message (errno, errbuf, sizeof(errbuf)), 
 			_("sh_socket_read: setsockopt"));
-	close(talkfd);
+	sl_close_fd(FIL__, __LINE__, talkfd);
 	return -1;
       }
@@ -643,5 +643,5 @@
 			sh_error_message (errno, errbuf, sizeof(errbuf)),
 			_("sh_socket_read: recvmsg"));
-	close(talkfd);	
+	sl_close_fd(FIL__, __LINE__, talkfd);	
 	return -1;
       }
@@ -666,5 +666,5 @@
 	{
 	  /* no data */
-	  close(talkfd);
+	  sl_close_fd(FIL__, __LINE__, talkfd);
 	  return 0;
 	}
@@ -672,5 +672,5 @@
 		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
 		      _("sh_socket_read: recvfrom"));
-      close(talkfd);
+      sl_close_fd(FIL__, __LINE__, talkfd);
       return -1;
     }
@@ -682,5 +682,5 @@
 		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
 		      _("sh_socket_read: getpeereid"));
-      close(talkfd);
+      sl_close_fd(FIL__, __LINE__, talkfd);
       return -1;
     }
@@ -693,5 +693,5 @@
 		      sh_error_message (errno, errbuf, sizeof(errbuf)), 
 		      _("sh_socket_read: getsockopt"));
-      close(talkfd);
+      sl_close_fd(FIL__, __LINE__, talkfd);
       return -1;
     }
@@ -744,9 +744,9 @@
 	      fdmax = data_size / sizeof(int);
 	      for (fdcount = 0; fdcount < fdmax; ++fdcount)
-		(void) close(fdsbuf[fdcount]);
+		(void) sl_close_fd(FIL__, __LINE__, fdsbuf[fdcount]);
 	    }
 	}
       
-      close(talkfd);
+      sl_close_fd(FIL__, __LINE__, talkfd);
       return -1;
     }
@@ -775,5 +775,5 @@
 		      _("sh_socket_read"));
       sh_set_sockpass();
-      close(talkfd);
+      sl_close_fd(FIL__, __LINE__, talkfd);
       return -1;
     }
@@ -782,5 +782,5 @@
 		  _("Socket credentials not supported on this OS"), 
 		  _("sh_socket_read"));
-  close(talkfd);
+  sl_close_fd(FIL__, __LINE__, talkfd);
   return -1;
 #endif
@@ -796,5 +796,5 @@
 		      _("client does not have required uid"), 
 		      _("sh_socket_read: getsockopt"));
-      close(talkfd);
+      sl_close_fd(FIL__, __LINE__, talkfd);
       return -1;
     }
@@ -816,5 +816,5 @@
 			  _("Bad message format: command too long"), 
 			  _("sh_socket_read"));
-	  close(talkfd);
+	  sl_close_fd(FIL__, __LINE__, talkfd);
 	  return -1;
 	}
@@ -824,5 +824,5 @@
 			  _("Bad message format: hostname too long"), 
 			  _("sh_socket_read"));
-	  close(talkfd);
+	  sl_close_fd(FIL__, __LINE__, talkfd);
 	  return -1;
 	}
@@ -848,5 +848,5 @@
 		      _("Bad message format"), 
 		      _("sh_socket_read"));
-      close(talkfd);
+      sl_close_fd(FIL__, __LINE__, talkfd);
       return -1;
     }
@@ -865,5 +865,5 @@
   */
   nbytes = send (talkfd, cmd, strlen(cmd) + 1, 0);
-  close(talkfd);
+  sl_close_fd(FIL__, __LINE__, talkfd);
   if (nbytes < 0)
     {
@@ -904,5 +904,5 @@
 			      sh_error_message (errno, errbuf, sizeof(errbuf)), 
 			      _("sh_socket_read: sendto"));
-	      close(talkfd);
+	      sl_close_fd(FIL__, __LINE__, talkfd);
 	      return -1;
 	    }
@@ -935,5 +935,5 @@
 			  sh_error_message (errno, errbuf, sizeof(errbuf)), 
 			  _("sh_socket_read: sendto"));
-	  close(talkfd);
+	  sl_close_fd(FIL__, __LINE__, talkfd);
 	  return -1;
 	}
@@ -946,5 +946,5 @@
   */
   nbytes = send (talkfd, _("END"), 4, 0);
-  close(talkfd);
+  sl_close_fd(FIL__, __LINE__, talkfd);
   return 0;
 }
Index: trunk/src/sh_static.c
===================================================================
--- trunk/src/sh_static.c	(revision 251)
+++ trunk/src/sh_static.c	(revision 252)
@@ -54,4 +54,8 @@
 #include "sh_pthread.h"
 
+extern  int sl_close_fd (const char * file, int line, int fd);
+extern  int sl_fclose (const char * file, int line, FILE * fp);
+
+
 #ifndef _PATH_PASSWD
 #define _PATH_PASSWD "/etc/passwd"
@@ -60,4 +64,7 @@
 #define _PATH_GROUP "/etc/group"
 #endif
+
+#undef  FIL__
+#define FIL__  _("sh_static.c")
 
 extern  int sl_strlcpy(char * dst, /*@null@*/const char * src, size_t siz);
@@ -123,5 +130,5 @@
       }
     } while (1);
-    fclose(stream);
+    sl_fclose(FIL__, __LINE__, stream);
   }
   
@@ -171,5 +178,5 @@
       }
     } while (1);
-    fclose(stream);
+    sl_fclose(FIL__, __LINE__, stream);
   }
   
@@ -219,5 +226,5 @@
       }
     } while (1);
-    fclose(stream);
+    sl_fclose(FIL__, __LINE__, stream);
   }
   
@@ -267,5 +274,5 @@
       }
     } while (1);
-    fclose(stream);
+    sl_fclose(FIL__, __LINE__, stream);
   }
   
@@ -341,5 +348,5 @@
         SH_MUTEX_LOCK(pwf_lock);
 	if (pwf) {
-		fclose(pwf);
+		sl_fclose(FIL__, __LINE__, pwf);
 		pwf = NULL;
 	}
@@ -395,5 +402,5 @@
 	SH_MUTEX_LOCK(grf_lock);
 	if (grf) {
-		fclose(grf);
+		sl_fclose(FIL__, __LINE__, grf);
 		grf = NULL;
 	}
@@ -498,5 +505,5 @@
 		rv = setgroups(num_groups, group_list);
 	DO_CLOSE:
-		fclose(grf);
+		sl_fclose(FIL__, __LINE__, grf);
 	}
 
@@ -863,5 +870,5 @@
 #include <arpa/inet.h>
 
-/* close()
+/* sl_close_fd(FIL__, __LINE__, )
  */
 #include <unistd.h>
@@ -1185,5 +1192,5 @@
 	while (retries++ < MAX_RETRIES) {
 		if (fd != -1)
-			close(fd);
+			sl_close_fd(FIL__, __LINE__, fd);
 
 		memset(packet, 0, PACKETSZ);
@@ -1344,5 +1351,5 @@
 		DPRINTF("Answer type = |%d|\n", a->atype);
 
-		close(fd);
+		sl_close_fd(FIL__, __LINE__, fd);
 
 		if (outpacket)
@@ -1390,5 +1397,5 @@
 fail:
 	if (fd != -1)
-	    close(fd);
+	    sl_close_fd(FIL__, __LINE__, fd);
 	if (lookup)
 	    free(lookup);
@@ -1546,10 +1553,10 @@
         
 		if (action!=GETHOSTENT) {
-			fclose(fp);
+			sl_fclose(FIL__, __LINE__, fp);
 		}
 		return ret;
 	}
 	if (action!=GETHOSTENT) {
-		fclose(fp);
+		sl_fclose(FIL__, __LINE__, fp);
 	}
 	return ret;
@@ -1619,5 +1626,5 @@
 			}
 		}
-		fclose(fp);
+		sl_fclose(FIL__, __LINE__, fp);
 	} else {
 	    DPRINTF("failed to open %s\n", "resolv.conf");
Index: trunk/src/sh_suidchk.c
===================================================================
--- trunk/src/sh_suidchk.c	(revision 251)
+++ trunk/src/sh_suidchk.c	(revision 252)
@@ -294,5 +294,5 @@
 			   MSG_SUID_ERROR,
 			   sh_error_message(errno, errbuf, sizeof(errbuf)));
-	  (void) close(fd);
+	  (void) sl_close_fd(FIL__, __LINE__, fd);
 	  return -1; 
 	}
@@ -303,5 +303,5 @@
 			   MSG_SUID_ERROR,
 			   _("Possible race: lstat != fstat"));
-	  (void) close(fd); 
+	  (void) sl_close_fd(FIL__, __LINE__, fd); 
 	  return -1;
 	}
@@ -311,5 +311,5 @@
 			   MSG_SUID_ERROR,
 			   _("Possible race: not a regular file"));
-	  (void) close(fd); 
+	  (void) sl_close_fd(FIL__, __LINE__, fd); 
 	  return -1;
 	}
@@ -319,5 +319,5 @@
 			   MSG_SUID_ERROR,
 			   _("Possible race: not a suid/sgid file"));
-	  (void) close(fd); 
+	  (void) sl_close_fd(FIL__, __LINE__, fd); 
 	  return -1;
 	}
@@ -334,5 +334,5 @@
 			       MSG_SUID_ERROR,
 			       sh_error_message(errno, errbuf, sizeof(errbuf)));
-	      (void) close(fd); 
+	      (void) sl_close_fd(FIL__, __LINE__, fd); 
 	      return -1;
 	    }
@@ -341,5 +341,5 @@
 			   MSG_SUID_ERROR,
 			   _("The fchmod() function is not available"));
-	  (void) close(fd); 
+	  (void) sl_close_fd(FIL__, __LINE__, fd); 
 	  return -1;
 #endif
@@ -349,5 +349,5 @@
 			       MSG_SUID_ERROR,
 			       _("Not truncated because hardlink count gt 1"));
-	      (void) close(fd); 
+	      (void) sl_close_fd(FIL__, __LINE__, fd); 
 	      return -1;
 	    }
@@ -359,5 +359,5 @@
 			       MSG_SUID_ERROR,
 			       sh_error_message(errno, errbuf, sizeof(errbuf)));
-	      (void) close(fd); 
+	      (void) sl_close_fd(FIL__, __LINE__, fd); 
 	      return -1;
 	    }
@@ -370,9 +370,9 @@
 			       MSG_SUID_ERROR,
 			       sh_error_message(errno, errbuf, sizeof(errbuf)));
-	      (void) close(fd); 
+	      (void) sl_close_fd(FIL__, __LINE__, fd); 
 	      return -1;
 	    }
 	}
-      (void) close (fd);
+      (void) sl_close_fd (FIL__, __LINE__, fd);
       return (0);
     }
@@ -568,7 +568,7 @@
 	    }
 
-	  (void) close (readFile);
+	  (void) sl_close_fd (FIL__, __LINE__, readFile);
 	  (void) fchmod(writeFile, S_IRUSR | S_IWUSR | S_IXUSR);
-	  (void) close (writeFile);
+	  (void) sl_close_fd (FIL__, __LINE__, writeFile);
 
 	  if (do_truncate (fullpath) == -1)
@@ -613,5 +613,5 @@
 			  theFile->c_group, (int) theFile->group, 
 			  timestrc, timestra, timestrm);
-		  (void) fclose (filePtr);
+		  (void) sl_fclose (FIL__, __LINE__, filePtr);
 		}
 	      /*@+usedef@*/
@@ -788,5 +788,5 @@
     {
       do {
-	status = close (file_d);
+	status = sl_close_fd (FIL__, __LINE__, file_d);
       } while (status == -1 && errno == EINTR);
       
@@ -985,9 +985,15 @@
     if (status != 0)
       {
+	int elevel = SH_ERR_ERR;
+	size_t tlen;
+
 	status = errno;
 	tmp = sh_util_safe_name(tmpcat);
-	sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, status, MSG_ERR_LSTAT,
+	tlen = strlen(tmp);
+	if (tlen >= 6 && 0 == strcmp(&tmp[tlen-6], _("/.gvfs")))
+	  elevel = SH_ERR_NOTICE;
+	sh_error_handle (elevel, FIL__, __LINE__, status, MSG_ERR_LSTAT,
 			 sh_error_message(status, errbuf, sizeof(errbuf)),
-			 tmpcat );
+			 tmp );
 	SH_FREE(tmp);
       }
@@ -2034,6 +2040,13 @@
 	  if (stat (mnt->mnt_dir, &disk_stats) == -1)
 	    {
-	      sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
-			       _("stat() failed"),
+	      char errmsg[256];
+	      int  elevel = SH_ERR_ERR;
+	      size_t tlen = strlen(mnt->mnt_dir);
+	      if (tlen >= 6 && 0 == strcmp(&((mnt->mnt_dir)[tlen-6]), _("/.gvfs")))
+		elevel = SH_ERR_NOTICE;
+	      sl_snprintf(errmsg, sizeof(errmsg), _("stat(%s) failed"),
+			  mnt->mnt_dir);
+	      sh_error_handle (elevel, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			       errmsg,
 			       _("filesystem_type_uncached") );
 	      return NULL;
Index: trunk/src/sh_tools.c
===================================================================
--- trunk/src/sh_tools.c	(revision 251)
+++ trunk/src/sh_tools.c	(revision 252)
@@ -149,5 +149,5 @@
 	  if (errno == EACCES || errno == EADDRINUSE)
 	    retval = 1;
-	  close (sd);
+	  sl_close_fd (FIL__, __LINE__, sd);
 	  return retval;
 	}
@@ -155,5 +155,5 @@
       /* bind() succeeds, iface exists
        */
-      close(sd);
+      sl_close_fd(FIL__, __LINE__, sd);
       return 1;
     }
@@ -702,5 +702,5 @@
 	sl_strlcat(errmsg, _(", address "), errsiz);
 	sl_strlcat(errmsg, address, errsiz);
-	close(fd);
+	sl_close_fd(FIL__, __LINE__, fd);
 	fail = (-1); 
       }
Index: trunk/src/sh_unix.c
===================================================================
--- trunk/src/sh_unix.c	(revision 251)
+++ trunk/src/sh_unix.c	(revision 252)
@@ -1293,5 +1293,5 @@
 	fd++;
       else
-	close(fd++);
+	sl_close_fd(FIL__, __LINE__, fd++);
     }
 
@@ -1971,7 +1971,7 @@
       /* Close first tree file descriptors 
        */ 
-      close (0);  /* if running as daemon */
-      close (1);  /* if running as daemon */
-      close (2);  /* if running as daemon */
+      sl_close_fd (FIL__, __LINE__, 0);  /* if running as daemon */
+      sl_close_fd (FIL__, __LINE__, 1);  /* if running as daemon */
+      sl_close_fd (FIL__, __LINE__, 2);  /* if running as daemon */
 
       /* Enable full error logging
@@ -2306,5 +2306,5 @@
 				   _("time"), sh.srvtime.name);
 		}
-	      close(fd);
+	      sl_close_fd(FIL__, __LINE__, fd);
 	    }
 	  else
@@ -2910,5 +2910,5 @@
   
   r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
-  /* close (fd); */
+  /* sl_close_fd (FIL__, __LINE__, fd); */
 
   if (r == -1)
@@ -3439,4 +3439,5 @@
   int           fstat_return;
   int           fstat_errno = 0;
+  int           try         = 0;
 
   sh_string   * content = NULL;
@@ -3464,10 +3465,12 @@
     stat_errno = errno;
 
+  theFile->link_path = NULL;
+
+ try_again:
+
   fd           = -1;
   fstat_return = -1;
   rval_open    = -1;
 
-  theFile->link_path = NULL;
-
   if (stat_return == 0 && S_ISREG(buf.st_mode)) 
     {
@@ -3475,15 +3478,20 @@
 				    path /* theFile->fullpath */, SL_YESPRIV);
       if (SL_ISERROR(rval_open))
-	err_open = errno;
-
-      {
-	char * stale = sl_check_stale();
-
-	if (stale)
-	  {
-	    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, err_open, MSG_E_SUBGEN,
-			    stale, _("sh_unix_getinfo_open"));
-	  }
-      }
+	{
+	  char * stale = sl_check_stale();
+	  
+	  if (stale)
+	    {
+	      sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, err_open, MSG_E_SUBGEN,
+			      stale, _("sh_unix_getinfo_open"));
+	    }
+
+	  if (errno == EBADF && try == 0) /* obsolete, but we keep this, just in case */
+	    {
+	      ++try;
+	      goto try_again;
+	    }
+	  err_open = errno;
+	}
 
       alert_timeout = 120; /* this is per 8K block now ! */
@@ -3529,4 +3537,11 @@
 			      MSG_E_SUBGEN,
 			      stale, _("sh_unix_getinfo_fstat"));
+	    }
+
+	  if (try == 0) /* obsolete, but we keep this, just in case */
+	    {
+	      ++try;
+	      sl_close(rval_open);
+	      goto try_again;
 	    }
 	}
Index: trunk/src/sh_utmp.c
===================================================================
--- trunk/src/sh_utmp.c	(revision 251)
+++ trunk/src/sh_utmp.c	(revision 252)
@@ -283,5 +283,5 @@
   if (sh_utmpfile != NULL)
     {
-      (void) fclose (sh_utmpfile);
+      (void) sl_fclose (FIL__, __LINE__, sh_utmpfile);
       sh_utmpfile = NULL;
     }
@@ -341,5 +341,5 @@
   SL_ENTER(_("sh_utmp_endutent"));
   if (NULL != sh_utmpfile)
-    (void) fclose(sh_utmpfile);
+    (void) sl_fclose(FIL__, __LINE__, sh_utmpfile);
   sh_utmpfile = NULL;
   SL_RET0(_("sh_utmp_endutent"));
Index: trunk/src/slib.c
===================================================================
--- trunk/src/slib.c	(revision 251)
+++ trunk/src/slib.c	(revision 252)
@@ -1559,4 +1559,5 @@
   SL_TICKET ticket;          /* The unique  ID.      */ 
   int fd;                    /* The file descriptor. */
+  FILE * stream;             /* The file descriptor. */
   char * path;               /* The file path.       */
   int flush;                 /* Whether we want to flush the cache */
@@ -1571,4 +1572,8 @@
 static int  stale_orig_line = -1;
 static char stale_orig_mesg[128];
+
+static char badfd_orig_file[64] = { '\0' };
+static int  badfd_orig_line = -1;
+static char badfd_orig_mesg[128];
 
 SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER);
@@ -1587,4 +1592,16 @@
 }
 
+char * sl_check_badfd()
+{
+  if (badfd_orig_line == -1)
+    return NULL;
+  sl_snprintf(badfd_orig_mesg, sizeof(badfd_orig_mesg), 
+	      _("close on file descriptor with allocated handle, %s, %d"), 
+	      badfd_orig_file, badfd_orig_line);
+  badfd_orig_file[0] = '\0';
+  badfd_orig_line    = -1;
+  return badfd_orig_mesg;
+}
+
 static
 SL_TICKET sl_create_ticket (unsigned int myindex) 
@@ -1666,5 +1683,5 @@
 
 SL_TICKET sl_make_ticket (const char * ofile, int oline,
-			  int fd, const char * filename)
+			  int fd, const char * filename, FILE * stream)
 {
   size_t    len;
@@ -1721,4 +1738,5 @@
   ofiles[fd]->fd      = fd;
   ofiles[fd]->content = NULL;
+  ofiles[fd]->stream  = stream;
   ofiles[fd]->flush   = SL_FALSE;
 
@@ -1786,5 +1804,5 @@
    * the mask is returned." 
    */
-  (void) umask (0); 
+  (void) umask (0);
 
   if (mode == SL_OPEN_FOR_FASTREAD)
@@ -1927,5 +1945,5 @@
   if (stat_return < 0)
     {
-      close (fd);
+      sl_close_fd (FIL__, __LINE__, fd);
       errno = errval;
       SL_IRETURN(SL_EFSTAT, _("sl_open_file"));
@@ -1936,5 +1954,5 @@
   if (lstat_return != ENOENT && buf.st_ino != lbuf.st_ino)
     {
-      close (fd);
+      sl_close_fd (FIL__, __LINE__, fd);
       SL_IRETURN(SL_EBOGUS, _("sl_open_file"));
     }
@@ -1946,5 +1964,5 @@
   if (fd >= MAXFD)
      {
-	close(fd);
+	sl_close_fd(FIL__, __LINE__, fd);
 	SL_IRETURN(SL_TOOMANY, _("sl_open_file"));
      }
@@ -1953,5 +1971,5 @@
     {
       /*
-      close(fd);
+      sl_close_fd(FIL__, __LINE__, fd);
       SL_IRETURN(SL_EINTERNAL09, _("sl_open_file"));
       */
@@ -1968,5 +1986,5 @@
   if ( (ofiles[fd] = (SL_OFILE *) malloc(sizeof(SL_OFILE))) == NULL)
     {
-      close(fd);
+      sl_close_fd(FIL__, __LINE__, fd);
       SL_IRETURN(SL_EMEM, _("sl_open_file"));
     }
@@ -1978,5 +1996,5 @@
       free (ofiles[fd]);
       ofiles[fd] = NULL;
-      close(fd);
+      sl_close_fd(FIL__, __LINE__, fd);
       SL_IRETURN(SL_EMEM, _("sl_open_file"));
     }
@@ -1991,5 +2009,5 @@
       (void) free (ofiles[fd]);
       ofiles[fd] = NULL;
-      close(fd);
+      sl_close_fd(FIL__, __LINE__, fd);
       SL_IRETURN(ticket, _("sl_open_file"));
     }
@@ -1999,4 +2017,5 @@
   ofiles[fd]->fd      = fd;
   ofiles[fd]->content = NULL;
+  ofiles[fd]->stream  = NULL;
   ofiles[fd]->flush   = SL_FALSE;
 
@@ -2005,4 +2024,21 @@
 
   SL_IRETURN(ticket, _("sl_open_file"));
+}
+
+FILE * sl_stream (SL_TICKET ticket, char * mode)
+{
+  int    fd;
+
+  if (SL_ISERROR(fd = sl_read_ticket(ticket)))
+    return (NULL);
+
+  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || 
+      ticket != ofiles[fd]->ticket || fd < 0)
+    return (NULL);
+
+  if (!ofiles[fd]->stream)
+    ofiles[fd]->stream = fdopen(fd, mode);
+
+  return ofiles[fd]->stream;
 }
 
@@ -2259,4 +2295,5 @@
 {
   register int fd;
+  FILE * fp = NULL;
 
   SL_ENTER(_("sl_close"));
@@ -2276,4 +2313,5 @@
 	sh_string_destroy(&(ofiles[fd]->content));
       (void) free (ofiles[fd]->path);
+      fp = ofiles[fd]->stream;
       (void) free (ofiles[fd]);
       ofiles[fd] = NULL;
@@ -2282,12 +2320,61 @@
   /* This may fail, but what to do then ?
    */
-  if (0 != close(fd))
-    {
-      TPT((0, FIL__, __LINE__, 
-	   _("msg=<Error closing file.>, fd=<%d>, err=<%s>\n"), 
-	   fd, strerror(errno)));
+  if (fp)
+    {
+      if (0 != fclose (fp)) /* within sl_close */
+	{
+	  TPT((0, FIL__, __LINE__, 
+	       _("msg=<Error fclosing file.>, fd=<%d>, err=<%s>\n"), 
+	       fd, strerror(errno)));
+	}
+    }
+  else
+    {
+      if (0 != close(fd)) /* within sl_close */
+	{
+	  TPT((0, FIL__, __LINE__, 
+	       _("msg=<Error closing file.>, fd=<%d>, err=<%s>\n"), 
+	       fd, strerror(errno)));
+	}
     }
 
   SL_IRETURN(SL_ENONE, _("sl_close")); 
+}
+
+int sl_close_fd (const char * file, int line, int fd)
+{
+  int ret = -1;
+
+  SL_ENTER(_("sl_close_fd"));
+
+  if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
+    {
+      sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
+      badfd_orig_line = line;
+    }
+
+  ret = close(fd); /* within sl_close_fd wrapper */
+
+  SL_IRETURN(ret, _("sl_close_fd")); 
+}
+
+int sl_fclose (const char * file, int line, FILE * fp)
+{
+  int ret = -1;
+  int fd;
+
+  SL_ENTER(_("sl_fclose"));
+
+  fd = fileno(fp);
+
+  if (fd >= 0 && fd < MAXFD && ofiles[fd] != NULL) /* stale ofiles[fd] handle */
+    {
+      sl_strlcpy(badfd_orig_file, file, sizeof(badfd_orig_file));
+      badfd_orig_line = line;
+    }
+
+  ret = fclose(fp); /* within sl_fclose wrapper */
+
+  SL_IRETURN(ret, _("sl_fclose")); 
 }
 
