[tor-commits] [torsocks/master] Add compat.c/.h defining a compat tsocks mutex

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


commit dbab1272a03ba4a5cd63ca641b6a0e8d55f37306
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Sun Jun 2 14:52:01 2013 -0400

    Add compat.c/.h defining a compat tsocks mutex
    
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 src/common/Makefile.am |    3 +-
 src/common/compat.c    |   74 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/common/compat.h    |   40 ++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 1 deletion(-)

diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index 5f325a2..d2123de 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -3,4 +3,5 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src
 AM_CFLAGS = -fno-strict-aliasing
 
 noinst_LTLIBRARIES = libcommon.la
-libcommon_la_SOURCES = log.c log.h config-file.c config-file.h utils.c utils.h
+libcommon_la_SOURCES = log.c log.h config-file.c config-file.h utils.c utils.h \
+                       compat.c compat.h
diff --git a/src/common/compat.c b/src/common/compat.c
new file mode 100644
index 0000000..03f70a8
--- /dev/null
+++ b/src/common/compat.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2013 - 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 <assert.h>
+
+#include "compat.h"
+
+#ifdef __linux__
+
+/*
+ * Initialize a pthread mutex. This never fails.
+ */
+void tsocks_mutex_init(tsocks_mutex_t *m)
+{
+	assert(m);
+	pthread_mutex_init(&m->mutex, NULL);
+}
+
+/*
+ * Destroy a pthread mutex. This never fails.
+ */
+void tsocks_mutex_destroy(tsocks_mutex_t *m)
+{
+	assert(m);
+	pthread_mutex_destroy(&m->mutex);
+}
+
+/*
+ * Use pthread mutex lock and assert on any error.
+ */
+void tsocks_mutex_lock(tsocks_mutex_t *m)
+{
+	int ret;
+
+	assert(m);
+	ret = pthread_mutex_lock(&m->mutex);
+	/*
+	 * Unable to lock the mutex could lead to undefined behavior and potential
+	 * security issues. Stop everything so torsocks can't continue.
+	 */
+	assert(!ret);
+}
+
+/*
+ * Use pthread mutex unlock and assert on any error.
+ */
+void tsocks_mutex_unlock(tsocks_mutex_t *m)
+{
+	int ret;
+
+	assert(m);
+	ret = pthread_mutex_unlock(&m->mutex);
+	/*
+	 * Unable to unlock the mutex could lead to undefined behavior and potential
+	 * security issues. Stop everything so torsocks can't continue.
+	 */
+	assert(!ret);
+}
+
+#endif /* __linux__ */
diff --git a/src/common/compat.h b/src/common/compat.h
new file mode 100644
index 0000000..3ac4941
--- /dev/null
+++ b/src/common/compat.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2013 - 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.
+ */
+
+#ifndef TORSOCKS_COMPAT_H
+#define TORSOCKS_COMPAT_H
+
+#ifdef __linux__
+
+#include <pthread.h>
+
+typedef struct tsocks_mutex_t {
+	pthread_mutex_t mutex;
+} tsocks_mutex_t;
+
+/* Define a tsock mutex variable with the mutex statically initialized. */
+#define TSOCKS_INIT_MUTEX(name) \
+	tsocks_mutex_t name = { .mutex = PTHREAD_MUTEX_INITIALIZER }
+
+void tsocks_mutex_init(tsocks_mutex_t *m);
+void tsocks_mutex_destroy(tsocks_mutex_t *m);
+void tsocks_mutex_lock(tsocks_mutex_t *m);
+void tsocks_mutex_unlock(tsocks_mutex_t *m);
+
+#endif /* __linux__ */
+
+#endif /* TORSOCKS_COMPAT_H */





More information about the tor-commits mailing list