[tor-commits] [torsocks/master] Add fclose() support

dgoulet at torproject.org dgoulet at torproject.org
Fri Apr 4 22:40:27 UTC 2014


commit 7dee92c5f0e29222258dd10074bbdf8a008bc749
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Sat Mar 8 11:20:07 2014 -0500

    Add fclose() support
    
    Fixes #29
    
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 src/lib/Makefile.am |    2 +-
 src/lib/fclose.c    |   85 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/torsocks.h  |   15 +++++++++
 3 files changed, 101 insertions(+), 1 deletion(-)

diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index cccecf1..d64b3f6 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -9,6 +9,6 @@ lib_LTLIBRARIES = libtorsocks.la
 libtorsocks_la_SOURCES = torsocks.c torsocks.h \
                          connect.c gethostbyname.c getaddrinfo.c close.c \
                          getpeername.c socket.c syscall.c socketpair.c recv.c \
-                         exit.c accept.c listen.c
+                         exit.c accept.c listen.c fclose.c
 
 libtorsocks_la_LIBADD = $(top_builddir)/src/common/libcommon.la
diff --git a/src/lib/fclose.c b/src/lib/fclose.c
new file mode 100644
index 0000000..8d371cd
--- /dev/null
+++ b/src/lib/fclose.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2014 - David Goulet <dgoulet at ev0ke.net>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License, version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <common/connection.h>
+#include <common/log.h>
+
+#include "torsocks.h"
+
+/* fclose(3) */
+TSOCKS_LIBC_DECL(fclose, LIBC_FCLOSE_RET_TYPE, LIBC_FCLOSE_SIG)
+
+/*
+ * Torsocks call for fclose(3).
+ */
+LIBC_FCLOSE_RET_TYPE tsocks_fclose(LIBC_FCLOSE_SIG)
+{
+	int fd;
+	struct connection *conn;
+
+	if (!fp) {
+		errno = EBADF;
+		goto error;
+	}
+
+	fd = fileno(fp);
+	if (fd < 0) {
+		/* errno is set to EBADF here by fileno(). */
+		goto error;
+	}
+
+	DBG("[fclose] Close catched for fd %d", fd);
+
+	connection_registry_lock();
+	conn = connection_find(fd);
+	if (conn) {
+		/*
+		 * Remove from the registry so it's not visible anymore and thus using
+		 * it without lock.
+		 */
+		connection_remove(conn);
+	}
+	connection_registry_unlock();
+
+	/*
+	 * Put back the connection reference. If the refcount get to 0, the
+	 * connection pointer is destroyed.
+	 */
+	if (conn) {
+		DBG("Close connection putting back ref");
+		connection_put_ref(conn);
+	}
+
+	/* Return the original libc fclose. */
+	return tsocks_libc_fclose(fp);
+
+error:
+	return -1;
+}
+
+/*
+ * Libc hijacked symbol fclose(3).
+ */
+LIBC_FCLOSE_DECL
+{
+	if (!tsocks_libc_fclose) {
+		tsocks_libc_fclose = tsocks_find_libc_symbol(
+				LIBC_FCLOSE_NAME_STR, TSOCKS_SYM_EXIT_NOT_FOUND);
+	}
+
+	return tsocks_fclose(LIBC_FCLOSE_ARGS);
+}
diff --git a/src/lib/torsocks.h b/src/lib/torsocks.h
index 1b95cb9..057a32e 100644
--- a/src/lib/torsocks.h
+++ b/src/lib/torsocks.h
@@ -74,6 +74,15 @@
 #define LIBC_CLOSE_SIG int fd
 #define LIBC_CLOSE_ARGS fd
 
+/* fclose(3) */
+#include <stdio.h>
+
+#define LIBC_FCLOSE_NAME fclose
+#define LIBC_FCLOSE_NAME_STR XSTR(LIBC_FCLOSE_NAME)
+#define LIBC_FCLOSE_RET_TYPE int
+#define LIBC_FCLOSE_SIG FILE *fp
+#define LIBC_FCLOSE_ARGS fp
+
 /* gethostbyname(3) - DEPRECATED in glibc. */
 #include <netdb.h>
 
@@ -297,6 +306,12 @@ TSOCKS_DECL(close, LIBC_CLOSE_RET_TYPE, LIBC_CLOSE_SIG)
 #define LIBC_CLOSE_DECL \
 		LIBC_CLOSE_RET_TYPE LIBC_CLOSE_NAME(LIBC_CLOSE_SIG)
 
+/* fclose(3) */
+extern TSOCKS_LIBC_DECL(fclose, LIBC_FCLOSE_RET_TYPE, LIBC_FCLOSE_SIG)
+TSOCKS_DECL(fclose, LIBC_FCLOSE_RET_TYPE, LIBC_FCLOSE_SIG)
+#define LIBC_FCLOSE_DECL \
+		LIBC_FCLOSE_RET_TYPE LIBC_FCLOSE_NAME(LIBC_FCLOSE_SIG)
+
 /* gethostbyname(3) */
 extern TSOCKS_LIBC_DECL(gethostbyname, LIBC_GETHOSTBYNAME_RET_TYPE,
 		LIBC_GETHOSTBYNAME_SIG)





More information about the tor-commits mailing list