aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-05-01 12:13:07 -0400
committerNick Mathewson <nickm@torproject.org>2014-05-01 12:13:07 -0400
commitb6c8a14bf35c4572fba49be9eb4aaaa78d776c4c (patch)
tree452e9c230c71131d20422d519ac05c95dd688c77
parent1bbd3811c1c98e6a630ab96f15a376910a576626 (diff)
parent1ebdaf5788cb7969ee3f853ede4e6e3364343fc0 (diff)
downloadtor-b6c8a14bf35c4572fba49be9eb4aaaa78d776c4c.tar
tor-b6c8a14bf35c4572fba49be9eb4aaaa78d776c4c.tar.gz
Merge remote-tracking branch 'public/bug4345a_024'
-rw-r--r--changes/bug434510
-rw-r--r--src/common/compat.c16
-rw-r--r--src/or/cpuworker.c7
3 files changed, 27 insertions, 6 deletions
diff --git a/changes/bug4345 b/changes/bug4345
new file mode 100644
index 000000000..5e650fc34
--- /dev/null
+++ b/changes/bug4345
@@ -0,0 +1,10 @@
+ o Minor bugfixes:
+ - Check return code on spawn_func() in cpuworker code, so that we don't
+ think we've spawned a nonworking cpuworker and write junk to it
+ forever. Fix related to bug 4345; bugfix on all released Tor versions.
+ Found by "skruffy".
+
+ - Use a pthread_attr to make sure that spawn_func() cannot return
+ an error while at the same time launching a thread. Fix related
+ to bug 4345; bugfix on all released Tor versions. Reported by
+ "cypherpunks".
diff --git a/src/common/compat.c b/src/common/compat.c
index 1ba264a0c..974f697e3 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -2429,6 +2429,12 @@ tor_pthread_helper_fn(void *_data)
func(arg);
return NULL;
}
+/**
+ * A pthread attribute to make threads start detached.
+ */
+static pthread_attr_t attr_detached;
+/** True iff we've called tor_threads_init() */
+static int threads_initialized = 0;
#endif
/** Minimalist interface to run a void function in the background. On
@@ -2452,12 +2458,12 @@ spawn_func(void (*func)(void *), void *data)
#elif defined(USE_PTHREADS)
pthread_t thread;
tor_pthread_data_t *d;
+ if (PREDICT_UNLIKELY(!threads_initialized))
+ tor_threads_init();
d = tor_malloc(sizeof(tor_pthread_data_t));
d->data = data;
d->func = func;
- if (pthread_create(&thread,NULL,tor_pthread_helper_fn,d))
- return -1;
- if (pthread_detach(thread))
+ if (pthread_create(&thread,&attr_detached,tor_pthread_helper_fn,d))
return -1;
return 0;
#else
@@ -2814,8 +2820,6 @@ tor_get_thread_id(void)
* "reentrant" mutexes (i.e., once we can re-lock if we're already holding
* them.) */
static pthread_mutexattr_t attr_reentrant;
-/** True iff we've called tor_threads_init() */
-static int threads_initialized = 0;
/** Initialize <b>mutex</b> so it can be locked. Every mutex must be set
* up with tor_mutex_init() or tor_mutex_new(); not both. */
void
@@ -2959,6 +2963,8 @@ tor_threads_init(void)
if (!threads_initialized) {
pthread_mutexattr_init(&attr_reentrant);
pthread_mutexattr_settype(&attr_reentrant, PTHREAD_MUTEX_RECURSIVE);
+ tor_assert(0==pthread_attr_init(&attr_detached));
+ tor_assert(0==pthread_attr_setdetachstate(&attr_detached, 1));
threads_initialized = 1;
set_main_thread();
}
diff --git a/src/or/cpuworker.c b/src/or/cpuworker.c
index 6b6a68afe..61b2c29b3 100644
--- a/src/or/cpuworker.c
+++ b/src/or/cpuworker.c
@@ -528,7 +528,12 @@ spawn_cpuworker(void)
tor_assert(SOCKET_OK(fdarray[1]));
fd = fdarray[0];
- spawn_func(cpuworker_main, (void*)fdarray);
+ if (spawn_func(cpuworker_main, (void*)fdarray) < 0) {
+ tor_close_socket(fdarray[0]);
+ tor_close_socket(fdarray[1]);
+ tor_free(fdarray);
+ return -1;
+ }
log_debug(LD_OR,"just spawned a cpu worker.");
#ifndef TOR_IS_MULTITHREADED
tor_close_socket(fdarray[1]); /* don't need the worker's side of the pipe */