tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: possible bug in fseek of buffered files shared between processes (example uses stdin)



On Tue, 2 Nov 2021, Christos Zoulas wrote:

In article <a91717c9-3b97-2756-5b73-81d5dee4e7%SDF.ORG@localhost>,
RVP  <rvp%SDF.ORG@localhost> wrote:

This looks like a useful behaviour to add to the libc close routines.

I agree!


A first cut is something like this:

---START PATCH---
diff -urN stdio.orig/fclose.c stdio/fclose.c
--- stdio.orig/fclose.c	2012-03-15 18:22:30.000000000 +0000
+++ stdio/fclose.c	2021-11-02 22:00:31.604704902 +0000
@@ -62,6 +62,15 @@
 	FLOCKFILE(fp);
 	WCIO_FREE(fp);
 	r = fp->_flags & __SWR ? __sflush(fp) : 0;
+	if (fp->_seek != NULL && fp->_flags & __SRD) {
+		if (fp->_flags & __SOFF)
+			fp->_seek(fp->_cookie, fp->_offset, SEEK_SET);
+		else if (fp->_bf._base != NULL) {
+			off_t o = -fp->_r;
+			_DIAGASSERT(o <= 0);
+			fp->_seek(fp->_cookie, o, SEEK_CUR);
+		}
+	}
 	if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)
 		r = EOF;
 	if (fp->_flags & __SMBF)
diff -urN stdio.orig/findfp.c stdio/findfp.c
--- stdio.orig/findfp.c	2012-03-27 15:05:42.000000000 +0000
+++ stdio/findfp.c	2021-11-02 21:46:29.767257736 +0000
@@ -207,8 +207,10 @@
 void
 _cleanup(void)
 {
-	/* (void) _fwalk(fclose); */
+	(void) _fwalk(fclose);
+#if 0
 	(void) fflush(NULL);			/* `cheating' */
+#endif
 }

 /*
---END PATCH---

Carlo, can you see if that works with your use-case?

-RVP


Home | Main Index | Thread Index | Old Index