bug-gnulib
[Top][All Lists]
Advanced

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

Re: [bug-gnulib] clean-temp usage question


From: Eric Blake
Subject: Re: [bug-gnulib] clean-temp usage question
Date: Fri, 06 Oct 2006 12:02:25 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Thunderbird/1.5.0.7 Mnenhy/0.7.4.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Bruno Haible on 10/6/2006 7:43 AM:
> 
> You are free to add a close_stream_temp function to the create-temp module
> if you protect it with #ifdef so that it doesn't introduce a module
> dependency.

May I apply the patch below to gnulib?  With it, all of my recent
additions to the M4 testsuite now pass when stressing the use of closed
file descriptors in combination with temporary files.

Also, I'm suspicious of your use of xallocsa with PATH_MAX, on platforms
like Hurd where PATH_MAX is intentionally undefined because there is no
limit (although you do use the pathmax.h header to ensure that you have a
definition, it is artificially limited in this case).  So,
create_temp_dir/tmpdir is either artificially limiting how large the name
in $TMPDIR can be, or you may be allocating so much memory in xallocsa as
to cause memory shortage even though you will never use that much memory
in the common case.  Perhaps it would be better if the tmpdir module were
updated to allocate its results, rather than use a pre-allocated buffer of
the caller, since then you have more control over how large the resulting
buffer needs to be without resorting to using artificially limited PATH_MAX.

lib/ChangeLog:
2006-10-06  Eric Blake  <address@hidden>

        * clean-temp.h (close_stream_temp): New declaration.
        * clean-temp.c (includes): Pull in headers according to what
        other modules are in use.
        (close_stream_temp) [GNULIB_CLOSE_STREAM]: New function.
m4/ChangeLog:
2006-10-06  Eric Blake  <address@hidden>

        Define several macros for use by the clean-temp module.
        * close-stream.m4 (gl_CLOSE_STREAM): Define GNULIB_CLOSE_STREAM.
        * fcntl-safer.m4 (gl_FCNTL_SAFER): Define GNULIB_FCNTL_SAFER.
        * stdio-safer.m4 (gl_FOPEN_SAFER): Define GNULIB_FOPEN_SAFER.

- --
Life is short - so eat dessert first!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFJpow84KuGfSFAYARAgXUAJ4wbfSMIPO0F1GKxpggvGL8Lu3LuQCgxaFf
UB/wyoxNipBTtI0jci1o/xs=
=3BuE
-----END PGP SIGNATURE-----
Index: lib/clean-temp.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/clean-temp.c,v
retrieving revision 1.3
diff -u -p -r1.3 clean-temp.c
--- lib/clean-temp.c    6 Oct 2006 12:17:22 -0000       1.3
+++ lib/clean-temp.c    6 Oct 2006 17:57:12 -0000
@@ -23,6 +23,7 @@
 #include "clean-temp.h"
 
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <stdbool.h>
 #include <stdlib.h>
@@ -38,6 +39,15 @@
 #include "xallocsa.h"
 #include "gl_linkedhash_list.h"
 #include "gettext.h"
+#if GNULIB_CLOSE_STREAM
+# include "close-stream.h"
+#endif
+#if GNULIB_FCNTL_SAFER
+# include "fcntl--.h"
+#endif
+#if GNULIB_FOPEN_SAFER
+# include "stdio--.h"
+#endif
 
 #define _(str) gettext (str)
 
@@ -644,3 +654,25 @@ fwriteerror_temp (FILE *fp)
   return result;
 }
 #endif
+
+#if GNULIB_CLOSE_STREAM
+/* Like close_stream.
+   Unregisters the previously registered file descriptor.  */
+int
+close_stream_temp (FILE *fp)
+{
+  int fd = fileno (fp);
+  /* No blocking of signals is needed here, since a double close of a
+     file descriptor is harmless.  */
+  int result = close_stream (fp);
+  int saved_errno = errno;
+
+  /* No race condition here: we assume a single-threaded program, hence
+     fd cannot be re-opened here.  */
+
+  unregister_fd (fd);
+
+  errno = saved_errno;
+  return result;
+}
+#endif
Index: lib/clean-temp.h
===================================================================
RCS file: /sources/gnulib/gnulib/lib/clean-temp.h,v
retrieving revision 1.2
diff -u -p -r1.2 clean-temp.h
--- lib/clean-temp.h    6 Oct 2006 12:17:22 -0000       1.2
+++ lib/clean-temp.h    6 Oct 2006 17:57:12 -0000
@@ -118,6 +118,10 @@ extern int fclose_temp (FILE *fp);
    Unregisters the previously registered file descriptor.  */
 extern int fwriteerror_temp (FILE *fp);
 
+/* Like close_stream.
+   Unregisters the previously registered file descriptor.  */
+extern int close_stream_temp (FILE *fp);
+
 
 #ifdef __cplusplus
 }
Index: m4/close-stream.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/close-stream.m4,v
retrieving revision 1.2
diff -u -p -r1.2 close-stream.m4
--- m4/close-stream.m4  21 Aug 2006 21:46:31 -0000      1.2
+++ m4/close-stream.m4  6 Oct 2006 17:57:12 -0000
@@ -1,3 +1,4 @@
+#serial 2
 dnl Copyright (C) 2006 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,6 +7,8 @@ dnl with or without modifications, as lo
 AC_DEFUN([gl_CLOSE_STREAM],
 [
   AC_LIBOBJ([close-stream])
+  AC_DEFINE([GNULIB_CLOSE_STREAM], [1],
+    [Define to 1 when using the gnulib close-stream module.])
 
   dnl Prerequisites of lib/close-stream.c.
   :
Index: m4/fcntl-safer.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/fcntl-safer.m4,v
retrieving revision 1.6
diff -u -p -r1.6 fcntl-safer.m4
--- m4/fcntl-safer.m4   21 Aug 2006 21:46:31 -0000      1.6
+++ m4/fcntl-safer.m4   6 Oct 2006 17:57:12 -0000
@@ -1,4 +1,4 @@
-#serial 3
+#serial 4
 dnl Copyright (C) 2005, 2006 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,4 +8,6 @@ AC_DEFUN([gl_FCNTL_SAFER],
 [
   AC_LIBOBJ([open-safer])
   AC_LIBOBJ([creat-safer])
+  AC_DEFINE([GNULIB_FCNTL_SAFER], [1],
+    [Define to 1 when using the gnulib fcntl-safer module.])
 ])
Index: m4/stdio-safer.m4
===================================================================
RCS file: /sources/gnulib/gnulib/m4/stdio-safer.m4,v
retrieving revision 1.9
diff -u -p -r1.9 stdio-safer.m4
--- m4/stdio-safer.m4   21 Aug 2006 21:46:31 -0000      1.9
+++ m4/stdio-safer.m4   6 Oct 2006 17:57:12 -0000
@@ -1,4 +1,4 @@
-#serial 8
+#serial 9
 dnl Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -7,6 +7,8 @@ dnl with or without modifications, as lo
 AC_DEFUN([gl_FOPEN_SAFER],
 [
   AC_LIBOBJ([fopen-safer])
+  AC_DEFINE([GNULIB_FOPEN_SAFER], [1],
+    [Define to 1 when using the gnulib fopen-safer module.])
 ])
 
 AC_DEFUN([gl_TMPFILE_SAFER],

reply via email to

[Prev in Thread] Current Thread [Next in Thread]