swrap: Wrap fopen to detect stale file descriptors.
authorAndreas Schneider <asn@samba.org>
Thu, 2 Oct 2014 05:18:48 +0000 (07:18 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Thu, 2 Oct 2014 07:35:10 +0000 (09:35 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
lib/socket_wrapper/socket_wrapper.c

index 96490041c7aafbb320f625870e19e4c36d383239..574f5ba71af359eca03df16d6d5bd2746f0ba6bc 100644 (file)
@@ -337,6 +337,7 @@ struct swrap_libc_fns {
                            socklen_t addrlen);
        int (*libc_dup)(int fd);
        int (*libc_dup2)(int oldfd, int newfd);
+       FILE *(*libc_fopen)(const char *name, const char *mode);
 #ifdef HAVE_EVENTFD
        int (*libc_eventfd)(int count, int flags);
 #endif
@@ -645,6 +646,13 @@ static int libc_listen(int sockfd, int backlog)
        return swrap.fns.libc_listen(sockfd, backlog);
 }
 
+static FILE *libc_fopen(const char *name, const char *mode)
+{
+       swrap_load_lib_function(SWRAP_LIBC, fopen);
+
+       return swrap.fns.libc_fopen(name, mode);
+}
+
 static int libc_vopen(const char *pathname, int flags, va_list ap)
 {
        long int mode = 0;
@@ -3065,6 +3073,29 @@ int listen(int s, int backlog)
        return swrap_listen(s, backlog);
 }
 
+/****************************************************************************
+ *   FOPEN
+ ***************************************************************************/
+
+static FILE *swrap_fopen(const char *name, const char *mode)
+{
+       FILE *fp;
+
+       fp = libc_fopen(name, mode);
+       if (fp != NULL) {
+               int fd = fileno(fp);
+
+               swrap_remove_stale(fd);
+       }
+
+       return fp;
+}
+
+FILE *fopen(const char *name, const char *mode)
+{
+       return swrap_fopen(name, mode);
+}
+
 /****************************************************************************
  *   OPEN
  ***************************************************************************/