diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/anytun.cpp | 50 | ||||
-rw-r--r-- | src/signalController.cpp | 12 | ||||
-rw-r--r-- | src/signalController.h | 6 |
4 files changed, 51 insertions, 19 deletions
diff --git a/src/Makefile b/src/Makefile index 423063b..4e14925 100644 --- a/src/Makefile +++ b/src/Makefile @@ -114,7 +114,7 @@ ANYCONFOBJS = log.o \ EXECUTABLE = anytun anytun-config anytun-controld anytun-showtables -all: $(EXECUTABLE) libAnysync.a anyrtpproxy +all: $(EXECUTABLE) libAnysync.a #anyrtpproxy anytun: $(OBJS) $(SYNCOBJS) anytun.o $(LD) $(OBJS) $(SYNCOBJS) anytun.o -o $@ $(LDFLAGS) diff --git a/src/anytun.cpp b/src/anytun.cpp index 5bd3214..f01ef4f 100644 --- a/src/anytun.cpp +++ b/src/anytun.cpp @@ -40,6 +40,7 @@ #include <unistd.h> #include <boost/bind.hpp> +#include <boost/thread/detail/lock.hpp> #include <gcrypt.h> #include <cerrno> // for ENOMEM @@ -325,21 +326,52 @@ void receiver(void* p) } } -#define MIN_GCRYPT_VERSION "1.2.0" -#if defined(__GNUC__) && !defined(__OpenBSD__) // TODO: thread-safety on OpenBSD -// make libgcrypt thread safe -extern "C" { -GCRY_THREAD_OPTION_PTHREAD_IMPL; +// boost thread callbacks for libgcrypt + +typedef boost::detail::thread::lock_ops<boost::mutex> mutex_ops; + +static int boost_mutex_init(void **priv) +{ + int err = 0; + boost::mutex *lock = new boost::mutex(); + + if (!lock) + err = ENOMEM; + if (!err) + *priv = lock; + return err; +} + +static int boost_mutex_destroy(void **lock) +{ + delete reinterpret_cast<boost::mutex*>(*lock); + return 0; } -#endif + +static int boost_mutex_lock(void **lock) +{ + mutex_ops::lock(*reinterpret_cast<boost::mutex*>(*lock)); + return 0; +} + +static int boost_mutex_unlock(void **lock) +{ + mutex_ops::unlock(*reinterpret_cast<boost::mutex*>(*lock)); + return 0; +} + +static struct gcry_thread_cbs gcry_threads_boost = +{ GCRY_THREAD_OPTION_USER, NULL, + boost_mutex_init, boost_mutex_destroy, + boost_mutex_lock, boost_mutex_unlock }; + +#define MIN_GCRYPT_VERSION "1.2.0" bool initLibGCrypt() { // make libgcrypt thread safe // this must be called before any other libgcrypt call -#if defined(__GNUC__) && !defined(__OpenBSD__) // TODO: thread-safety on OpenBSD - gcry_control( GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread ); -#endif + gcry_control( GCRYCTL_SET_THREAD_CBS, &gcry_threads_boost ); // this must be called right after the GCRYCTL_SET_THREAD_CBS command // no other function must be called till now diff --git a/src/signalController.cpp b/src/signalController.cpp index ebd6897..cef743f 100644 --- a/src/signalController.cpp +++ b/src/signalController.cpp @@ -34,7 +34,8 @@ #include <iostream> -//#include "threadUtils.hpp" +#include <boost/bind.hpp> +#include "threadUtils.hpp" #include "signalController.h" #include "log.h" @@ -85,9 +86,10 @@ SignalController::~SignalController() { for(HandlerMap::iterator it = handler.begin(); it != handler.end(); ++it) delete it->second; + if(thread) delete thread; } -void* SignalController::handle(void *s) +void SignalController::handle(void *s) { SignalController* self = reinterpret_cast<SignalController*>(s); sigset_t signal_set; @@ -103,7 +105,6 @@ void* SignalController::handle(void *s) } self->sigQueueSem.up(); } - pthread_exit(NULL); } void SignalController::init() @@ -115,10 +116,9 @@ void SignalController::init() sigdelset(&signal_set, SIGSEGV); sigdelset(&signal_set, SIGBUS); sigdelset(&signal_set, SIGFPE); - pthread_sigmask(SIG_BLOCK, &signal_set, NULL); + pthread_sigmask(SIG_BLOCK, &signal_set, NULL); // TODO: remove ugly workaround - pthread_create(&thread, NULL, handle, this); - pthread_detach(thread); + thread = new boost::thread(boost::bind(handle, this)); handler[SIGINT] = new SigIntHandler; handler[SIGQUIT] = new SigQuitHandler; diff --git a/src/signalController.h b/src/signalController.h index aa57b2c..d99e168 100644 --- a/src/signalController.h +++ b/src/signalController.h @@ -98,9 +98,9 @@ public: class SignalController { public: - SignalController() {} + SignalController() { thread = NULL; } ~SignalController(); - static void* handle(void* s); + static void handle(void* s); void init(); int run(); @@ -115,7 +115,7 @@ private: Mutex sigQueueMutex; Semaphore sigQueueSem; - pthread_t thread; + boost::thread* thread; HandlerMap handler; }; |