Index: /trunk/configure.ac
===================================================================
--- /trunk/configure.ac	(revision 247)
+++ /trunk/configure.ac	(revision 248)
@@ -12,5 +12,5 @@
 dnl start
 dnl
-AM_INIT_AUTOMAKE(samhain, 2.5.9a)
+AM_INIT_AUTOMAKE(samhain, 2.5.9b)
 AC_DEFINE([SAMHAIN], 1, [Application is samhain])
 AC_CANONICAL_HOST
Index: /trunk/docs/Changelog
===================================================================
--- /trunk/docs/Changelog	(revision 247)
+++ /trunk/docs/Changelog	(revision 248)
@@ -1,2 +1,6 @@
+2.5.9b:
+	* remove stale file record when creating handle, and raise diagnostic
+	  error to find origin of stale record
+
 2.5.9a:
 	* fixed a race condition in closing of file handles
Index: /trunk/include/slib.h
===================================================================
--- /trunk/include/slib.h	(revision 247)
+++ /trunk/include/slib.h	(revision 248)
@@ -327,13 +327,20 @@
    * ---------------------------------------------------------------- */
 
-  SL_TICKET sl_make_ticket (int fd, const char * path);
-
+#define SL_OFILE_SIZE 32
+
+  char * sl_check_stale();
+
+  SL_TICKET sl_make_ticket (const char * ofile, int oline,
+			    int fd, const char * filename);
+ 
   /* Open for writing.
    */
-  SL_TICKET  sl_open_write       (const char * fname, int priviledge_mode);
+  SL_TICKET  sl_open_write       (const char * ofile, int oline,
+				  const char * fname, int priviledge_mode);
 
   /* Open for reading.
    */
-  SL_TICKET  sl_open_read        (const char * fname, int priviledge_mode);
+  SL_TICKET  sl_open_read        (const char * ofile, int oline,
+				  const char * fname, int priviledge_mode);
 
   /* Drop from cach when closing
@@ -343,21 +350,26 @@
   /* Open for reading w/minimum checking.
    */
-  SL_TICKET  sl_open_fastread    (const char * fname, int priviledge_mode);
+  SL_TICKET  sl_open_fastread    (const char * ofile, int oline,
+				  const char * fname, int priviledge_mode);
 
   /* Open for read and write.
    */
-  SL_TICKET  sl_open_rdwr        (const char * fname, int priviledge_mode);
+  SL_TICKET  sl_open_rdwr        (const char * ofile, int oline,
+				  const char * fname, int priviledge_mode);
 
   /* Open for read and write, fail if file exists.
    */
-  SL_TICKET sl_open_safe_rdwr    (const char * fname, int priv);
+  SL_TICKET sl_open_safe_rdwr    (const char * ofile, int oline,
+				  const char * fname, int priv);
 
   /* Open for write, truncate.
    */
-  SL_TICKET  sl_open_write_trunc (const char * fname, int priviledge_mode);
+  SL_TICKET  sl_open_write_trunc (const char * ofile, int oline,
+				  const char * fname, int priviledge_mode);
 
   /* Open for read and write, truncate.
    */
-  SL_TICKET  sl_open_rdwr_trunc  (const char * fname, int priviledge_mode);
+  SL_TICKET  sl_open_rdwr_trunc  (const char * ofile, int oline,
+				  const char * fname, int priviledge_mode);
 
   /* Initialize the content sh_string.
Index: /trunk/src/bignum.c
===================================================================
--- /trunk/src/bignum.c	(revision 247)
+++ /trunk/src/bignum.c	(revision 248)
@@ -926,5 +926,5 @@
     if (a->sign == BIG_SIGN_MINUS)
     {
-	*n = -*n;;
+      *n = -*n;;
     }
     return FALSE;
Index: /trunk/src/cutest_sh_tiger0.c
===================================================================
--- /trunk/src/cutest_sh_tiger0.c	(revision 247)
+++ /trunk/src/cutest_sh_tiger0.c	(revision 248)
@@ -159,5 +159,5 @@
   CuAssertStrEquals(tc, expected, actual);
 
-  rval_open = sl_open_fastread ("cutest_foo", SL_YESPRIV);
+  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
   CuAssertTrue(tc, rval_open >= 0);
 
@@ -173,5 +173,5 @@
   CuAssertTrue(tc, result == 0);
 
-  rval_open = sl_open_fastread ("cutest_foo", SL_YESPRIV);
+  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
   CuAssertTrue(tc, rval_open >= 0);
 
@@ -189,5 +189,5 @@
   CuAssertTrue(tc, result == 0);
 
-  rval_open = sl_open_fastread ("cutest_foo", SL_YESPRIV);
+  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
   CuAssertTrue(tc, rval_open >= 0);
 
@@ -354,5 +354,5 @@
   CuAssertTrue(tc, result == 0);
 
-  rval_open = sl_open_fastread ("cutest_foo", SL_YESPRIV);
+  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
   CuAssertTrue(tc, rval_open >= 0);
 
Index: /trunk/src/cutest_slib.c
===================================================================
--- /trunk/src/cutest_slib.c	(revision 247)
+++ /trunk/src/cutest_slib.c	(revision 248)
@@ -5,4 +5,38 @@
 #include "CuTest.h"
 #include "samhain.h"
+
+void Test_sl_stale (CuTest *tc) {
+
+  extern int get_the_fd (SL_TICKET ticket);
+
+  int       fd1, fd2, ret, line, val;
+  SL_TICKET tfd1, tfd2;
+  char *    err1;
+  char      err2[128];
+
+  line = __LINE__; tfd1 = sl_open_read(__FILE__, __LINE__, "/etc/group", SL_NOPRIV);
+  CuAssertTrue(tc, tfd1 > 0);
+
+  fd1 = get_the_fd(tfd1);
+  CuAssertTrue(tc, fd1 >= 0);
+
+  ret = close(fd1);
+  CuAssertTrue(tc, ret == 0);
+
+  tfd2 = sl_open_read(__FILE__, __LINE__, "/etc/group", SL_NOPRIV);
+  CuAssertTrue(tc, tfd2 > 0);
+  CuAssertTrue(tc, tfd2 != tfd1);
+
+  fd2 = get_the_fd(tfd2);
+  CuAssertIntEquals(tc, fd1, fd2);
+
+  err1 = sl_check_stale();
+  CuAssertTrue(tc, err1 != NULL);
+
+  sl_snprintf(err2, sizeof(err2), 
+	      "stale handle, %s, %d", __FILE__, line);
+  val = strcmp(err1, err2);
+  CuAssertIntEquals(tc, 0, val);
+}
 
 void Test_sl_snprintf (CuTest *tc) {
Index: /trunk/src/samhain.c
===================================================================
--- /trunk/src/samhain.c	(revision 247)
+++ /trunk/src/samhain.c	(revision 248)
@@ -2099,4 +2099,14 @@
 #endif
 
+      {
+	char * stale = sl_check_stale();
+
+	if (stale)
+	  {
+	    sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, 0, MSG_E_SUBGEN,
+			    stale, _("sl_check_stale"));
+	  }
+      }
+
       /* no loop if not daemon
        */
Index: /trunk/src/sh_err_log.c
===================================================================
--- /trunk/src/sh_err_log.c	(revision 247)
+++ /trunk/src/sh_err_log.c	(revision 248)
@@ -69,5 +69,5 @@
   /* open the file, then check it 
    */
-  if ( SL_ISERROR(fd = sl_open_read (path, SL_NOPRIV)))
+  if ( SL_ISERROR(fd = sl_open_read (FIL__, __LINE__, path, SL_NOPRIV)))
     {
       fprintf(stderr, _("Could not open file <%s>\n"), path);
@@ -163,5 +163,5 @@
     }
 
-  if ( SL_ISERROR(fd = sl_open_read (s, SL_NOPRIV)) )
+  if ( SL_ISERROR(fd = sl_open_read (FIL__, __LINE__, s, SL_NOPRIV)) )
     {
       fprintf(stderr, 
@@ -540,5 +540,5 @@
   if (status == 0)
     {
-      fd = sl_open_write (logfile, SL_YESPRIV);
+      fd = sl_open_write (FIL__, __LINE__, logfile, SL_YESPRIV);
       if (SL_ISERROR(fd))
         {
@@ -761,4 +761,5 @@
   if (!sl_ok_adds(status, (2*KEY_LEN)) || !sl_ok_adds((2*KEY_LEN + status),32))
     {
+      sl_close(fd);
       SL_RETURN ((-1), _("sh_log_file"));
     }
Index: /trunk/src/sh_extern.c
===================================================================
--- /trunk/src/sh_extern.c	(revision 247)
+++ /trunk/src/sh_extern.c	(revision 248)
@@ -381,5 +381,5 @@
 
 		  fd = 
-		    sl_open_read(task->command, 
+		    sl_open_read(FIL__, __LINE__, task->command, 
 				 task->privileged==0 ? SL_NOPRIV : SL_YESPRIV);
 
@@ -389,4 +389,5 @@
 		    {
 		      PDBGC_S("fexecve: checksum mismatch");
+		      sl_close(fd);
 		      _exit(EXIT_FAILURE);
 		    }
@@ -398,4 +399,8 @@
 		  } while (val_return < 0 && errno == EINTR);
 		  pfd = val_return;
+
+		  sl_close(fd);
+		  fd = -1;
+
 		  if (pfd < 0)
 		    {
@@ -403,6 +408,4 @@
 		      _exit(EXIT_FAILURE);
 		    }
-		  sl_close(fd);
-		  fd = -1;
 		}
 #endif
@@ -538,5 +541,5 @@
   PDBG_D(task->pipeFD);
 
-  task->pipeTI = sl_make_ticket(task->pipeFD, _("pipe"));
+  task->pipeTI = sl_make_ticket(FIL__, __LINE__, task->pipeFD, _("pipe"));
 
   flags = (int) retry_fcntl (FIL__, __LINE__, task->pipeFD, F_GETFL, 0);
Index: /trunk/src/sh_forward.c
===================================================================
--- /trunk/src/sh_forward.c	(revision 247)
+++ /trunk/src/sh_forward.c	(revision 248)
@@ -3133,5 +3133,6 @@
 	      if (conn != NULL && conn->FileName != NULL)
 		{
-		  sfd = sl_open_read(conn->FileName, SL_YESPRIV);
+		  sfd = sl_open_read(FIL__, __LINE__, 
+				     conn->FileName, SL_YESPRIV);
 		  if (!SL_ISERROR(sfd))
 		    {
@@ -3229,5 +3230,6 @@
 		{
 		  bytes = -1;
-		  sfd = sl_open_read(conn->FileName, SL_YESPRIV);
+		  sfd = sl_open_read(FIL__, __LINE__, 
+				     conn->FileName, SL_YESPRIV);
 		  if (!SL_ISERROR(sfd))
 		    {
Index: /trunk/src/sh_gpg.c
===================================================================
--- /trunk/src/sh_gpg.c	(revision 247)
+++ /trunk/src/sh_gpg.c	(revision 248)
@@ -270,4 +270,5 @@
   char        pname[128];
   int         pfd;
+  int         val_return;
 #endif
 #endif
@@ -534,13 +535,23 @@
        */
 #if defined(WITH_PGP)
-      checkfd = sl_open_read(DEFAULT_PGP_PATH, SL_NOPRIV);
-#else
-      checkfd = sl_open_read(DEFAULT_GPG_PATH, SL_NOPRIV);
+      checkfd = sl_open_read(FIL__, __LINE__, DEFAULT_PGP_PATH, SL_NOPRIV);
+#else
+      checkfd = sl_open_read(FIL__, __LINE__, DEFAULT_GPG_PATH, SL_NOPRIV);
 #endif
 
       if (0 != sh_gpg_checksum(checkfd, 0))
-	aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+	{
+	  sl_close(checkfd);
+	  aud__exit(FIL__, __LINE__, EXIT_FAILURE);
+	}
 
       pfd = get_the_fd(checkfd);
+      do {
+	val_return = dup (pfd);
+      } while (val_return < 0 && errno == EINTR);
+      pfd = val_return;
+      sl_close(checkfd);
+      checkfd = -1;
+
       sl_snprintf(pname, sizeof(pname), _("/proc/self/fd/%d"), pfd);
       if (0 == access(pname, R_OK|X_OK))               /* flawfinder: ignore */
@@ -559,6 +570,4 @@
       /* procfs not working, go ahead 
        */
-      sl_close(checkfd);
-      checkfd = -1;
 #endif
 
@@ -576,7 +585,7 @@
 	{
 #if defined(WITH_PGP)
-	  checkfd = sl_open_fastread(DEFAULT_PGP_PATH, SL_NOPRIV);
-#else
-	  checkfd = sl_open_fastread(DEFAULT_GPG_PATH, SL_NOPRIV);
+	  checkfd = sl_open_fastread(FIL__, __LINE__, DEFAULT_PGP_PATH, SL_NOPRIV);
+#else
+	  checkfd = sl_open_fastread(FIL__, __LINE__, DEFAULT_GPG_PATH, SL_NOPRIV);
 #endif
 	  if (0 != sh_gpg_checksum(checkfd, 0)) {
@@ -685,5 +694,5 @@
 
 #ifdef HAVE_GPG_CHECKSUM
-  checkfd = sl_open_read(DEFAULT_GPG_PATH, SL_YESPRIV);
+  checkfd = sl_open_read(FIL__, __LINE__, DEFAULT_GPG_PATH, SL_YESPRIV);
 
   if (0 != sh_gpg_checksum(checkfd, 1))
@@ -716,5 +725,5 @@
 
 #ifdef HAVE_GPG_CHECKSUM
-  checkfd = sl_open_read(DEFAULT_PGP_PATH, SL_YESPRIV);
+  checkfd = sl_open_read(FIL__, __LINE__, DEFAULT_PGP_PATH, SL_YESPRIV);
 
   if (0 != sh_gpg_checksum(checkfd, 1))
Index: /trunk/src/sh_hash.c
===================================================================
--- /trunk/src/sh_hash.c	(revision 247)
+++ /trunk/src/sh_hash.c	(revision 248)
@@ -1191,5 +1191,6 @@
     if (fd == (-1))
       {
-	if ( SL_ISERROR(fd = sl_open_read(file_path('D', 'R'), SL_YESPRIV))) 
+	if ( SL_ISERROR(fd = sl_open_read(FIL__, __LINE__, 
+					  file_path('D', 'R'), SL_YESPRIV))) 
 	  {
 	    TPT(( 0, FIL__, __LINE__, _("msg=<Error opening: %s>\n"), 
@@ -1548,5 +1549,7 @@
       if (pushdata_stdout == S_FALSE && pushdata_fd == -1)
 	{
-	  if ( SL_ISERROR(pushdata_fd = sl_open_write(file_path('D', 'W'), SL_YESPRIV))) 
+	  if ( SL_ISERROR(pushdata_fd = sl_open_write(FIL__, __LINE__, 
+						      file_path('D', 'W'), 
+						      SL_YESPRIV))) 
 	    {
 	      SH_FREE(fullpath);
@@ -1572,5 +1575,6 @@
 	      SH_FREE(linkpath);
 	      sh_error_handle((-1), FIL__, __LINE__, status, MSG_E_SUBGPATH,
-			      _("Failed to seek to end of baseline database"), _("sh_hash_pushdata_int"),
+			      _("Failed to seek to end of baseline database"),
+			      _("sh_hash_pushdata_int"),
 			      file_path('D', 'W'));
 	      aud_exit(FIL__, __LINE__, EXIT_FAILURE);
@@ -1583,5 +1587,7 @@
 	{
 	  TPT((0, FIL__, __LINE__, _("msg=<Update.>\n")))
-	    if ( SL_ISERROR(pushdata_fd = sl_open_rdwr(file_path('D', 'W'), SL_YESPRIV))){
+	    if ( SL_ISERROR(pushdata_fd = sl_open_rdwr(FIL__, __LINE__, 
+						       file_path('D', 'W'), 
+						       SL_YESPRIV))){
 	      SH_FREE(fullpath);
 	      SH_FREE(linkpath);
@@ -4032,5 +4038,5 @@
   line = SH_ALLOC(MAX_PATH_STORE+2);
 
-  if ( SL_ISERROR(fd = sl_open_read(db_file, SL_YESPRIV))) 
+  if ( SL_ISERROR(fd = sl_open_read(FIL__, __LINE__, db_file, SL_YESPRIV))) 
     {
       fprintf(stderr, _("ERROR: can't open %s for read (errnum = %ld)\n"), 
Index: /trunk/src/sh_html.c
===================================================================
--- /trunk/src/sh_html.c	(revision 247)
+++ /trunk/src/sh_html.c	(revision 248)
@@ -106,5 +106,5 @@
   if (p)
     {
-      fd = sl_open_read (p, SL_YESPRIV);
+      fd = sl_open_read (FIL__, __LINE__, p, SL_YESPRIV);
       SH_FREE(p);
     }
@@ -227,5 +227,5 @@
   if (p)
     {
-      fd = sl_open_read (p, SL_YESPRIV);
+      fd = sl_open_read (FIL__, __LINE__, p, SL_YESPRIV);
       SH_FREE(p);
     }
@@ -300,5 +300,5 @@
   if (p)
     {
-      fd = sl_open_read (p, SL_YESPRIV);
+      fd = sl_open_read (FIL__, __LINE__, p, SL_YESPRIV);
       SH_FREE(p);
     }
@@ -481,5 +481,5 @@
 
 
-  fd = sl_open_write_trunc (DEFAULT_HTML_FILE, SL_YESPRIV);
+  fd = sl_open_write_trunc (FIL__, __LINE__, DEFAULT_HTML_FILE, SL_YESPRIV);
 
   if (SL_ISERROR(fd))
Index: /trunk/src/sh_mail.c
===================================================================
--- /trunk/src/sh_mail.c	(revision 247)
+++ /trunk/src/sh_mail.c	(revision 248)
@@ -115,5 +115,5 @@
       _exit (EXIT_FAILURE);
     }
-  if ( SL_ISERROR(fd = sl_open_read (s, SL_NOPRIV)))
+  if ( SL_ISERROR(fd = sl_open_read (FIL__, __LINE__, s, SL_NOPRIV)))
     {
       fprintf(stderr, _("Could not open file %s\n"), s);
Index: /trunk/src/sh_prelink.c
===================================================================
--- /trunk/src/sh_prelink.c	(revision 247)
+++ /trunk/src/sh_prelink.c	(revision 248)
@@ -120,5 +120,5 @@
       task->com_ti = -1;
     }
-  ticket = sl_open_read(task->command, 
+  ticket = sl_open_read(FIL__, __LINE__, task->command, 
 			task->privileged == 0 ? SL_NOPRIV : SL_YESPRIV);
   if (SL_ISERROR(ticket))
Index: /trunk/src/sh_readconf.c
===================================================================
--- /trunk/src/sh_readconf.c	(revision 247)
+++ /trunk/src/sh_readconf.c	(revision 248)
@@ -407,5 +407,6 @@
 	  aud_exit (FIL__, __LINE__, EXIT_FAILURE);
 	}
-      if (SL_ISERROR(fd = sl_open_read(file_path('C',local_flag),SL_YESPRIV)))
+      if (SL_ISERROR(fd = sl_open_read(FIL__, __LINE__, 
+				       file_path('C',local_flag),SL_YESPRIV)))
 	{
 	  sl_get_euid(&euid);
@@ -425,5 +426,6 @@
    */
   sl_strlcpy(sh.conf.hash, 
-	     sh_tiger_hash(file_path('C',local_flag), fd, TIGER_NOLIM, hashbuf, sizeof(hashbuf)),
+	     sh_tiger_hash(file_path('C',local_flag), fd, TIGER_NOLIM, 
+			   hashbuf, sizeof(hashbuf)),
 	     KEY_LEN+1);
   sl_rewind (fd);
Index: /trunk/src/sh_tiger0.c
===================================================================
--- /trunk/src/sh_tiger0.c	(revision 247)
+++ /trunk/src/sh_tiger0.c	(revision 248)
@@ -169,5 +169,5 @@
 	  TPT((0,FIL__, __LINE__, _("msg=<TIGER_FILE>, path=<%s>\n"),
 	       (filename == NULL ? _("(null)") : filename) ));
-	  fd = sl_open_read (filename, SL_YESPRIV);
+	  fd = sl_open_read (FIL__, __LINE__, filename, SL_YESPRIV);
 	}
 
Index: /trunk/src/sh_tools.c
===================================================================
--- /trunk/src/sh_tools.c	(revision 247)
+++ /trunk/src/sh_tools.c	(revision 248)
@@ -1737,5 +1737,5 @@
   } while (status == BAD);
 
-  fd = sl_open_safe_rdwr (file, SL_YESPRIV);
+  fd = sl_open_safe_rdwr (FIL__, __LINE__, file, SL_YESPRIV);
   if (SL_ISERROR(fd))
     {
Index: /trunk/src/sh_unix.c
===================================================================
--- /trunk/src/sh_unix.c	(revision 247)
+++ /trunk/src/sh_unix.c	(revision 248)
@@ -3472,5 +3472,6 @@
   if (stat_return == 0 && S_ISREG(buf.st_mode)) 
     {
-      rval_open = sl_open_fastread (path /* theFile->fullpath */, SL_YESPRIV);
+      rval_open = sl_open_fastread (FIL__, __LINE__, 
+				    path /* theFile->fullpath */, SL_YESPRIV);
       if (SL_ISERROR(rval_open))
 	err_open = errno;
@@ -4109,5 +4110,6 @@
     }
 
-  fd = sl_open_safe_rdwr (lockfile, SL_YESPRIV);       /* fails if file exists */
+  fd = sl_open_safe_rdwr (FIL__, __LINE__, 
+			  lockfile, SL_YESPRIV);      /* fails if file exists */
 
   if (!SL_ISERROR(fd))
@@ -4198,5 +4200,5 @@
   if (status >= 0)
     {
-       fd = sl_open_read (lockfile, SL_YESPRIV);
+       fd = sl_open_read (FIL__, __LINE__, lockfile, SL_YESPRIV);
        if (SL_ISERROR(fd))
 	 sh_error_handle ((-1), FIL__, __LINE__, fd,
Index: /trunk/src/sh_utils.c
===================================================================
--- /trunk/src/sh_utils.c	(revision 247)
+++ /trunk/src/sh_utils.c	(revision 248)
@@ -1170,5 +1170,5 @@
   sl_snprintf (outpath, len, _("%s.out"), path);
 
-  fp = sl_open_read(path, SL_NOPRIV);
+  fp = sl_open_read(FIL__, __LINE__, path, SL_NOPRIV);
   if (SL_ISERROR(fp))
     {
@@ -1181,5 +1181,5 @@
     }
   
-  fout = sl_open_write(outpath, SL_NOPRIV);
+  fout = sl_open_write(FIL__, __LINE__, outpath, SL_NOPRIV);
   if (SL_ISERROR(fout))
     {
Index: /trunk/src/slib.c
===================================================================
--- /trunk/src/slib.c	(revision 247)
+++ /trunk/src/slib.c	(revision 248)
@@ -1557,16 +1557,33 @@
 
 typedef struct openfiles {
-  SL_TICKET ticket;     /* The unique  ID.      */ 
-  int fd;               /* The file descriptor. */
-  char * path;          /* The file path.       */
-  int flush;            /* Whether we want to flush the cache */
-  sh_string * content;  /* The file content     */
+  SL_TICKET ticket;          /* The unique  ID.      */ 
+  int fd;                    /* The file descriptor. */
+  char * path;               /* The file path.       */
+  int flush;                 /* Whether we want to flush the cache */
+  char ofile[SL_OFILE_SIZE]; /* origin file */
+  int  oline;                /* origin line */
+  sh_string * content;       /* The file content     */
 } SL_OFILE; 
 
 static SL_OFILE * ofiles[MAXFD]; 
 
+static char stale_orig_file[64] = { '\0' };
+static int  stale_orig_line = -1;
+static char stale_orig_mesg[128];
+
 SH_MUTEX_STATIC(mutex_ticket, PTHREAD_MUTEX_INITIALIZER);
 
 static unsigned int nonce_counter = TOFFSET;
+
+char * sl_check_stale()
+{
+  if (stale_orig_line == -1)
+    return NULL;
+  sl_snprintf(stale_orig_mesg, sizeof(stale_orig_mesg), 
+	      _("stale handle, %s, %d"), stale_orig_file, stale_orig_line);
+  stale_orig_file[0] = '\0';
+  stale_orig_line    = -1;
+  return stale_orig_mesg;
+}
 
 static
@@ -1648,5 +1665,6 @@
 }
 
-SL_TICKET sl_make_ticket (int fd, const char * filename)
+SL_TICKET sl_make_ticket (const char * ofile, int oline,
+			  int fd, const char * filename)
 {
   size_t    len;
@@ -1660,7 +1678,15 @@
      }
 
-   if (ofiles[fd] != NULL)
-    {
-      SL_IRETURN(SL_EINTERNAL06, _("sl_make_ticket"));
+  if (ofiles[fd] != NULL) /* stale entry */
+    {
+      /* SL_IRETURN(SL_EINTERNAL06, _("sl_make_ticket")); */
+      sl_strlcpy(stale_orig_file, ofiles[fd]->ofile, sizeof(stale_orig_file));
+      stale_orig_line = ofiles[fd]->oline;
+
+      if (ofiles[fd]->content)
+	sh_string_destroy(&(ofiles[fd]->content));
+      (void) free (ofiles[fd]->path);
+      (void) free (ofiles[fd]);
+      ofiles[fd] = NULL;
     }
 
@@ -1696,4 +1722,7 @@
   ofiles[fd]->content = NULL;
   ofiles[fd]->flush   = SL_FALSE;
+
+  sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE);
+  ofiles[fd]->oline = oline;
 
   SL_IRETURN(ticket, _("sl_make_ticket"));
@@ -1726,5 +1755,6 @@
 
 static
-int sl_open_file (const char *filename, int mode, int priv)
+int sl_open_file (const char * ofile, int oline,
+		  const char *filename, int mode, int priv)
 {
   struct stat   lbuf;
@@ -1920,8 +1950,18 @@
      }
 
-   if (ofiles[fd] != NULL)
-    {
+  if (ofiles[fd] != NULL) /* stale entry */
+    {
+      /*
       close(fd);
       SL_IRETURN(SL_EINTERNAL09, _("sl_open_file"));
+      */
+      sl_strlcpy(stale_orig_file, ofiles[fd]->ofile, sizeof(stale_orig_file));
+      stale_orig_line = ofiles[fd]->oline;
+
+      if (ofiles[fd]->content)
+	sh_string_destroy(&(ofiles[fd]->content));
+      (void) free (ofiles[fd]->path);
+      (void) free (ofiles[fd]);
+      ofiles[fd] = NULL;
     }
 
@@ -1961,4 +2001,7 @@
   ofiles[fd]->flush   = SL_FALSE;
 
+  sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE);
+  ofiles[fd]->oline = oline;
+
   SL_IRETURN(ticket, _("sl_open_file"));
 }
@@ -1971,6 +2014,8 @@
     return (fd);
 
-  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || fd < 0)
+  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || 
+      ticket != ofiles[fd]->ticket || fd < 0)
     return (SL_EINTERNAL10);
+
   return (fd);
 }
@@ -1987,5 +2032,6 @@
 }
   
-SL_TICKET sl_open_write (const char * fname, int priv)
+SL_TICKET sl_open_write (const char * ofile, int oline,
+			 const char * fname, int priv)
 {
   long status;
@@ -1995,9 +2041,10 @@
     SL_IRETURN(status, _("sl_open_write"));
 
-  status = sl_open_file(fname, SL_OPEN_FOR_WRITE, priv);
+  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_WRITE, priv);
   SL_IRETURN(status, _("sl_open_write"));
 }
 
-SL_TICKET sl_open_read (const char * fname, int priv)
+SL_TICKET sl_open_read (const char * ofile, int oline,
+			const char * fname, int priv)
 {
   long status;
@@ -2012,5 +2059,5 @@
     }
 
-  status = sl_open_file(fname, SL_OPEN_FOR_READ, priv);
+  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_READ, priv);
   SL_IRETURN(status, _("sl_open_read"));
 }
@@ -2067,5 +2114,6 @@
 }
 
-SL_TICKET sl_open_fastread (const char * fname, int priv)
+SL_TICKET sl_open_fastread (const char * ofile, int oline,
+			    const char * fname, int priv)
 {
   long status;
@@ -2075,5 +2123,5 @@
     SL_IRETURN(status, _("sl_open_read"));
 
-  status = sl_open_file(fname, SL_OPEN_FOR_FASTREAD, priv);
+  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_FASTREAD, priv);
 
 #if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) && defined(POSIX_FADV_DONTNEED)
@@ -2094,5 +2142,6 @@
 }
 
-SL_TICKET sl_open_rdwr (const char * fname, int priv)
+SL_TICKET sl_open_rdwr (const char * ofile, int oline,
+			const char * fname, int priv)
 {
   long status;
@@ -2102,9 +2151,10 @@
     SL_IRETURN(status, _("sl_open_rdwr"));
 
-  status = sl_open_file(fname, SL_OPEN_FOR_RDWR, priv);
+  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_RDWR, priv);
   SL_IRETURN(status, _("sl_open_rdwr"));
 }
 
-SL_TICKET sl_open_safe_rdwr (const char * fname, int priv)
+SL_TICKET sl_open_safe_rdwr (const char * ofile, int oline,
+			     const char * fname, int priv)
 {
   long status;
@@ -2114,9 +2164,10 @@
     SL_IRETURN(status, _("sl_open_safe_rdwr"));
 
-  status = sl_open_file(fname, SL_OPEN_SAFE_RDWR, priv);
+  status = sl_open_file(ofile, oline, fname, SL_OPEN_SAFE_RDWR, priv);
   SL_IRETURN(status, _("sl_open_safe_rdwr"));
 }
 
-SL_TICKET sl_open_write_trunc (const char * fname, int priv)
+SL_TICKET sl_open_write_trunc (const char * ofile, int oline,
+			       const char * fname, int priv)
 {
   long status;
@@ -2126,9 +2177,10 @@
     SL_IRETURN(status, _("sl_open_write_trunc"));
 
-  status = sl_open_file(fname, SL_OPEN_FOR_WTRUNC, priv);
+  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_WTRUNC, priv);
   SL_IRETURN(status, _("sl_open_write_trunc"));
 }
 
-SL_TICKET sl_open_rdwr_trunc (const char * fname, int priv)
+SL_TICKET sl_open_rdwr_trunc (const char * ofile, int oline,
+			      const char * fname, int priv)
 {
   long status;
@@ -2138,5 +2190,5 @@
     SL_IRETURN(status, _("sl_open_rdwr_trunc"));
 
-  status = sl_open_file(fname, SL_OPEN_FOR_RWTRUNC, priv);
+  status = sl_open_file(ofile, oline, fname, SL_OPEN_FOR_RWTRUNC, priv);
   SL_IRETURN(status, _("sl_open_rdwr_trunc"));
 }
@@ -2150,5 +2202,6 @@
     return (fd);
 
-  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || fd < 0)
+  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || 
+      ticket != ofiles[fd]->ticket || fd < 0)
     return (SL_EINTERNAL12);
 
@@ -2167,5 +2220,6 @@
     return (NULL);
 
-  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || fd < 0)
+  if (ofiles[fd] == NULL || fd != ofiles[fd]->fd || 
+      ticket != ofiles[fd]->ticket || fd < 0)
     return (NULL);
 
