From ff5189996cc87f7b7c3b8a82edcab233761ac26e Mon Sep 17 00:00:00 2001 From: hunbernd Date: Tue, 15 Mar 2016 19:02:34 +0100 Subject: [PATCH 01/12] Fixed display of SQLCipher version RS used to show SQLite's version --- libretroshare/src/rsserver/p3face-info.cc | 46 ++++++++++++++++++++++- libretroshare/src/rsserver/p3face.h | 2 + 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/rsserver/p3face-info.cc b/libretroshare/src/rsserver/p3face-info.cc index b4041df31..62882d861 100644 --- a/libretroshare/src/rsserver/p3face-info.cc +++ b/libretroshare/src/rsserver/p3face-info.cc @@ -35,10 +35,54 @@ #include #endif +std::string RsServer::getSQLCipherVersion() +{ + sqlite3* mDb; + std::string versionstring(""); + const char* version; + int rc = sqlite3_open_v2("", &mDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE , NULL); //create DB in a temp file + + if(rc){ + std::cerr << "Can't open database, Error code: " << sqlite3_errmsg(mDb) + << std::endl; + sqlite3_close(mDb); + mDb = NULL; + return ""; + } + + std::string sqlQuery = "PRAGMA cipher_version;"; + sqlite3_stmt* stmt = NULL; + rc = sqlite3_prepare_v2(mDb, sqlQuery.c_str(), sqlQuery.length(), &stmt, NULL); + if (rc == SQLITE_OK) { + rc = sqlite3_step(stmt); + switch (rc) { + case SQLITE_ROW: + version = (const char *)sqlite3_column_text(stmt, 0); //not needed to free + versionstring.append(version); + break; + case SQLITE_DONE: + break; + default: + std::cerr << "RetroDb::tableExists(): Error executing statement (code: " << rc << ")" + << std::endl; + break; + } + } + + if (stmt) { + sqlite3_finalize(stmt); + } + sqlite3_close(mDb); // no-op if mDb is NULL (https://www.sqlite.org/c3ref/close.html) + return versionstring; +} + void RsServer::getLibraries(std::list &libraries) { libraries.push_back(RsLibraryInfo("bzip2", BZ2_bzlibVersion())); libraries.push_back(RsLibraryInfo("OpenSSL", SSLeay_version(SSLEAY_VERSION))); - libraries.push_back(RsLibraryInfo("SQLCipher", SQLITE_VERSION)); + libraries.push_back(RsLibraryInfo("SQLite", SQLITE_VERSION)); +#ifndef NO_SQLCIPHER + libraries.push_back(RsLibraryInfo("SQLCipher", getSQLCipherVersion())); +#endif libraries.push_back(RsLibraryInfo("Zlib", ZLIB_VERSION)); } diff --git a/libretroshare/src/rsserver/p3face.h b/libretroshare/src/rsserver/p3face.h index 23efa5866..4cc0b595a 100644 --- a/libretroshare/src/rsserver/p3face.h +++ b/libretroshare/src/rsserver/p3face.h @@ -139,6 +139,8 @@ class RsServer: public RsControl, public RsTickingThread private: + std::string getSQLCipherVersion(); + // The real Server Parts. //filedexserver *server; From 78f7ca81a966c81dd716d096e89091bdba85340f Mon Sep 17 00:00:00 2001 From: hunbernd Date: Tue, 15 Mar 2016 22:04:56 +0100 Subject: [PATCH 02/12] Make it possible to open databases using SQLCipher v2 that were created with v3 --- libretroshare/src/util/retrodb.cc | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/libretroshare/src/util/retrodb.cc b/libretroshare/src/util/retrodb.cc index 291287973..844053eca 100644 --- a/libretroshare/src/util/retrodb.cc +++ b/libretroshare/src/util/retrodb.cc @@ -67,7 +67,6 @@ RetroDb::RetroDb(const std::string &dbPath, int flags, const std::string& key) : return; } } -#endif char *err = NULL; rc = sqlite3_exec(mDb, "PRAGMA cipher_migrate;", NULL, NULL, &err); @@ -81,8 +80,32 @@ RetroDb::RetroDb(const std::string &dbPath, int flags, const std::string& key) : std::cerr << std::endl; sqlite3_free(err); } -} + //Test DB for correct sqlcipher version + if (sqlite3_exec(mDb, "PRAGMA user_version;", NULL, NULL, NULL) != SQLITE_OK) + { + std::cerr << "RetroDb::RetroDb(): Failed to open database: " << dbPath << std::endl << "Trying with settings for sqlcipher version 3..."; + //Reopening the database with correct settings + rc = sqlite3_close(mDb); + mDb = NULL; + if(!rc) + rc = sqlite3_open_v2(dbPath.c_str(), &mDb, flags, NULL); + if(!rc && !mKey.empty()) + rc = sqlite3_key(mDb, mKey.c_str(), mKey.size()); + if(!rc) + rc = sqlite3_exec(mDb, "PRAGMA kdf_iter = 64000;", NULL, NULL, NULL); + if (!rc && (sqlite3_exec(mDb, "PRAGMA user_version;", NULL, NULL, NULL) == SQLITE_OK)) + { + std::cerr << "\tSuccess" << std::endl; + } else { + std::cerr << "\tFailed, giving up" << std::endl; + sqlite3_close(mDb); + mDb = NULL; + return; + } + } +#endif +} RetroDb::~RetroDb(){ From f3bc97873b35cdc1ba90ba8dec679799104fe0c1 Mon Sep 17 00:00:00 2001 From: Nyfor Date: Wed, 27 Apr 2016 18:32:59 +0200 Subject: [PATCH 03/12] Updated Travis file. --- .travis.yml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 80cfe3747..80b719c06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,13 @@ +sudo: required +dist: trusty language: cpp compiler: - gcc before_install: - - echo $LANG - - echo $LC_ALL - sudo apt-get update - - sudo apt-get install build-essential checkinstall cmake g++ git libavutil-dev libavcodec-dev libavformat-dev libbz2-dev libcurl4-openssl-dev libdc1394-22-dev libglib2.0-dev libcv-dev libopencv-highgui-dev libhighgui-dev - - sudo apt-get install libgnome-keyring-dev libgstreamer-plugins-base0.10-dev libgstreamer0.10-dev libjasper-dev libjpeg-dev libmicrohttpd-dev libopencv-dev libprotobuf-dev libqt4-dev - - sudo apt-get install libspeex-dev libspeexdsp-dev libsqlite3-dev libssl-dev libswscale-dev - - sudo apt-get install libtbb-dev libtiff4-dev libupnp-dev libv4l-dev libxine-dev libxslt1-dev libxss-dev make pkg-config protobuf-compiler python-dev python-numpy subversion git yasm qtmobility-dev + - sudo apt-get install -y build-essential checkinstall cmake libavutil-dev libavcodec-dev libavformat-dev libbz2-dev libcurl4-openssl-dev libcv-dev libopencv-highgui-dev libhighgui-dev libgnome-keyring-dev libgstreamer-plugins-base0.10-dev libgstreamer0.10-dev libjasper-dev libjpeg-dev libmicrohttpd-dev libopencv-dev libprotobuf-dev libqt4-dev libspeex-dev libspeexdsp-dev libsqlite3-dev libssl-dev libswscale-dev libtbb-dev libtiff4-dev libupnp-dev libv4l-dev libxine-dev libxslt1-dev libxss-dev pkg-config protobuf-compiler python-dev qtmobility-dev # - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update && sudo apt-get install -y llvm-3.4 llvm-3.4-dev; fi # - rvm use $RVM --install --binary --fuzzy # - gem update --system @@ -32,7 +29,7 @@ addons: branch_pattern: coverity_scan before_script: - - qmake CONFIG+=NO_SQLCIPHER CONFIG+=tests + - qmake QMAKE_CC=$CC QMAKE_CXX=$CXX CONFIG+=NO_SQLCIPHER CONFIG+=tests #script: make script: if [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make && tests/unittests/unittests >/dev/null 2>&1 ; fi @@ -64,6 +61,3 @@ notifications: #env: # - RVM=2.0.0 LANG="en_US.UTF-8" - -os: - - linux From ca003c5f9e4787596f6a718cb61047d651e00c61 Mon Sep 17 00:00:00 2001 From: csoler Date: Tue, 3 May 2016 20:55:36 -0400 Subject: [PATCH 04/12] added missing initialisation of deleted memory before calling realloc --- libretroshare/src/pqi/pqistreamer.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/libretroshare/src/pqi/pqistreamer.cc b/libretroshare/src/pqi/pqistreamer.cc index 3cfa64de5..65312158d 100644 --- a/libretroshare/src/pqi/pqistreamer.cc +++ b/libretroshare/src/pqi/pqistreamer.cc @@ -1024,6 +1024,7 @@ RsItem *pqistreamer::addPartialPacket(const void *block, uint32_t len, uint32_t { std::cerr << "(WW) dropping unfinished existing packet that gets to be replaced by new starting packet." << std::endl; free(rec.mem); + rec.mem = NULL ; rec.size = 0 ; } // make sure this is a continuing packet, otherwise this is an error. From e50e686dd258879d3e74a0f329c5c2dc97462d43 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 4 May 2016 18:01:18 -0400 Subject: [PATCH 05/12] updated the debug message for incoming data with no registered tunnel --- libretroshare/src/turtle/p3turtle.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libretroshare/src/turtle/p3turtle.cc b/libretroshare/src/turtle/p3turtle.cc index cd0d94238..b3bcb1630 100644 --- a/libretroshare/src/turtle/p3turtle.cc +++ b/libretroshare/src/turtle/p3turtle.cc @@ -1192,7 +1192,7 @@ bool p3turtle::getTunnelServiceInfo(TurtleTunnelId tunnel_id,RsPeerId& vpid,RsFi if(it == _incoming_file_hashes.end()) { - std::cerr << "p3turtle::handleRecvGenericTunnelItem(): hash " << hash << " for tunnel " << std::hex << it2->first << std::dec << " has no attached service! Dropping the item. This is a serious consistency error." << std::endl; + std::cerr << "p3turtle::handleRecvGenericTunnelItem(): hash " << hash << " for client side tunnel endpoint " << std::hex << tunnel_id << std::dec << " has been removed (probably a late response)! Dropping the item. " << std::endl; return false; } @@ -1204,7 +1204,7 @@ bool p3turtle::getTunnelServiceInfo(TurtleTunnelId tunnel_id,RsPeerId& vpid,RsFi if(it == _outgoing_file_hashes.end()) { - std::cerr << "p3turtle::handleRecvGenericTunnelItem(): hash " << hash << " for tunnel " << std::hex << it2->first << std::dec<< " has no attached service! Dropping the item. This is a serious consistency error." << std::endl; + std::cerr << "p3turtle::handleRecvGenericTunnelItem(): hash " << hash << " for server side tunnel endpoint " << std::hex << tunnel_id << std::dec << " has been removed (probably a late response)! Dropping the item. " << std::endl; return false; } From 2e02a89874161609ff60c7ed7d3c2aacf8e06135 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 4 May 2016 22:23:36 -0400 Subject: [PATCH 06/12] improved debugging output of rsthreads.cc --- libretroshare/src/util/rsthreads.cc | 36 +++++++++++++---------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 2341a36c5..dc7bd4953 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -40,6 +40,7 @@ * #define DEBUG_THREADS 1 * #define RSMUTEX_ABORT 1 // Catch wrong pthreads mode. *******/ +#define THREAD_DEBUG std::cerr << "[caller thread ID: " << std::hex << pthread_self() << ", thread ID: " << mTid << std::dec << "] " #ifdef RSMUTEX_ABORT #include @@ -94,7 +95,7 @@ bool RsThread::shouldStop() void RsTickingThread::shutdown() { #ifdef DEBUG_THREADS - std::cerr << "pqithreadstreamer::stop()" << std::endl; + THREAD_DEBUG << "pqithreadstreamer::shutdown()" << std::endl; #endif int sval =0; @@ -103,7 +104,7 @@ void RsTickingThread::shutdown() if(sval > 0) { #ifdef DEBUG_THREADS - std::cerr << " thread not running. Quit." << std::endl; + THREAD_DEBUG << " thread not running. Quit." << std::endl; #endif return ; } @@ -114,7 +115,7 @@ void RsTickingThread::shutdown() void RsThread::ask_for_stop() { #ifdef DEBUG_THREADS - std::cerr << " calling stop" << std::endl; + THREAD_DEBUG << " calling stop" << std::endl; #endif sem_post(&mShouldStopSemaphore) ; } @@ -124,11 +125,11 @@ void RsTickingThread::fullstop() shutdown() ; #ifdef DEBUG_THREADS - std::cerr << " waiting stop" << std::endl; + THREAD_DEBUG << " waiting stop" << std::endl; #endif sem_wait(&mHasStoppedSemaphore) ; #ifdef DEBUG_THREADS - std::cerr << " finished!" << std::endl; + THREAD_DEBUG << " finished!" << std::endl; #endif } void RsThread::start() @@ -139,9 +140,7 @@ void RsThread::start() RS_STACK_MUTEX(mMutex) ; #ifdef DEBUG_THREADS - std::cerr << "pqithreadstreamer::run()" << std::endl; - std::cerr << " initing should_stop=0" << std::endl; - std::cerr << " initing has_stopped=1" << std::endl; + THREAD_DEBUG << "pqithreadstreamer::start() initing should_stop=0, has_stopped=1" << std::endl; #endif sem_init(&mHasStoppedSemaphore,0,0) ; @@ -154,15 +153,18 @@ void RsThread::start() mTid = tid; else { - std::cerr << "Fatal error: pthread_create could not create a thread. Error returned: " << err << " !!!!!!!" << std::endl; + THREAD_DEBUG << "Fatal error: pthread_create could not create a thread. Error returned: " << err << " !!!!!!!" << std::endl; sem_init(&mHasStoppedSemaphore,0,1) ; } } -RsTickingThread::RsTickingThread () +RsTickingThread::RsTickingThread() { +#ifdef DEBUG_THREADS + THREAD_DEBUG << "RsTickingThread::RsTickingThread()" << std::endl; +#endif sem_init(&mShouldStopSemaphore,0,0) ; } @@ -176,8 +178,7 @@ void RsSingleJobThread::runloop() void RsTickingThread::runloop() { #ifdef DEBUG_THREADS - std::cerr << "pqithreadstream::run()"; - std::cerr << std::endl; + THREAD_DEBUG << "pqithreadstream::runloop()" << std::endl; #endif sem_init(&mShouldStopSemaphore,0,0) ; @@ -186,8 +187,7 @@ void RsTickingThread::runloop() if(shouldStop()) { #ifdef DEBUG_THREADS - std::cerr << "pqithreadstreamer::run(): asked to stop." << std::endl; - std::cerr << " setting hasStopped=1" << std::endl; + THREAD_DEBUG << "pqithreadstreamer::runloop(): asked to stop. setting hasStopped=1, and returning. Thread ends." << std::endl; #endif sem_post(&mHasStoppedSemaphore) ; return ; @@ -217,9 +217,7 @@ void RsQueueThread::data_tick() mLastWork = now; mLastSleep = (uint32_t) (mMinSleep + (mLastSleep - mMinSleep) / 2.0); #ifdef DEBUG_THREADS - std::cerr << "RsQueueThread::run() done work: sleeping for: " << mLastSleep; - std::cerr << " ms"; - std::cerr << std::endl; + THREAD_DEBUG << "RsQueueThread::data_tick() done work: sleeping for: " << mLastSleep << " ms" << std::endl; #endif } @@ -235,9 +233,7 @@ void RsQueueThread::data_tick() mLastSleep = mMaxSleep; } #ifdef DEBUG_THREADS - std::cerr << "RsQueueThread::run() no work: sleeping for: " << mLastSleep; - std::cerr << " ms"; - std::cerr << std::endl; + THREAD_DEBUG << "RsQueueThread::data_tick() no work: sleeping for: " << mLastSleep << " ms" << std::endl; #endif } usleep(mLastSleep * 1000); // mLastSleep msec From bb00e39972eaf705c7cab64c10e6d4d670cb1245 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 4 May 2016 22:34:03 -0400 Subject: [PATCH 07/12] improved debugging output of rsthreads.cc (part 2) --- libretroshare/src/util/rsthreads.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index dc7bd4953..c4fa2355c 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -61,6 +61,10 @@ void *RsThread::rsthread_init(void* p) // it is a replacement for pthread_join() pthread_detach(pthread_self()); +#ifdef DEBUG_THREADS + THREAD_DEBUG << " thread is started. Calling runloop()..." << std::endl; +#endif + thread -> runloop(); return NULL; } @@ -81,6 +85,9 @@ bool RsThread::isRunning() int sval =0; sem_getvalue(&mHasStoppedSemaphore,&sval) ; +#ifdef DEBUG_THREADS + THREAD_DEBUG << " isRunning(): returning " << !sval << std::endl; +#endif return !sval ; } @@ -89,6 +96,9 @@ bool RsThread::shouldStop() int sval =0; sem_getvalue(&mShouldStopSemaphore,&sval) ; +#ifdef DEBUG_THREADS + THREAD_DEBUG << " shouldStop(): returning " << (sval > 0) << " (sval=" << sval << ") " << std::endl; +#endif return sval > 0; } From 0817aad0f9b9cf5ca79a3eba4eea93728ee6bf2e Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 4 May 2016 22:51:33 -0400 Subject: [PATCH 08/12] improved debugging output of rsthreads.cc (part 3) --- libretroshare/src/util/rsthreads.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index c4fa2355c..d3f87b34a 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -62,7 +62,7 @@ void *RsThread::rsthread_init(void* p) pthread_detach(pthread_self()); #ifdef DEBUG_THREADS - THREAD_DEBUG << " thread is started. Calling runloop()..." << std::endl; + std::cerr << "[Thread ID:" << std::hex << pthread_self() << std::dec << "] thread is started. Calling runloop()..." << std::endl; #endif thread -> runloop(); From dbd0418971e526f9c19312b3f09bdbe2e416c73e Mon Sep 17 00:00:00 2001 From: Nyfor Date: Wed, 4 May 2016 18:48:13 +0200 Subject: [PATCH 09/12] Fix segfault on shutdown. --- libretroshare/src/util/smallobject.cc | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libretroshare/src/util/smallobject.cc b/libretroshare/src/util/smallobject.cc index ff9beeb48..c299d8743 100644 --- a/libretroshare/src/util/smallobject.cc +++ b/libretroshare/src/util/smallobject.cc @@ -279,10 +279,6 @@ void *SmallObject::operator new(size_t size) #endif RsStackMutex m(_mtx) ; - - if(!_allocator._active) - return (void*)NULL; - void *p = _allocator.allocate(size) ; #ifdef DEBUG_MEMORY std::cerr << "new RsItem: " << p << ", size=" << size << std::endl; From 9847a7a45ffb92788f07b130413b6793d4af7653 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 5 May 2016 20:52:10 -0400 Subject: [PATCH 10/12] implemented a new semaphore class that should be cross plateform --- libretroshare/src/util/rsthreads.cc | 38 +++++++------------ libretroshare/src/util/rsthreads.h | 59 +++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 28 deletions(-) diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index d3f87b34a..896ecebd0 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -40,6 +40,7 @@ * #define DEBUG_THREADS 1 * #define RSMUTEX_ABORT 1 // Catch wrong pthreads mode. *******/ +#define DEBUG_THREADS 1 #define THREAD_DEBUG std::cerr << "[caller thread ID: " << std::hex << pthread_self() << ", thread ID: " << mTid << std::dec << "] " #ifdef RSMUTEX_ABORT @@ -68,11 +69,8 @@ void *RsThread::rsthread_init(void* p) thread -> runloop(); return NULL; } -RsThread::RsThread () : mMutex("RsThread") +RsThread::RsThread() { - sem_init(&mHasStoppedSemaphore,0,1) ; - sem_init(&mShouldStopSemaphore,0,0) ; - #ifdef WINDOWS_SYS memset (&mTid, 0, sizeof(mTid)); #else @@ -82,8 +80,7 @@ RsThread::RsThread () : mMutex("RsThread") bool RsThread::isRunning() { // do we need a mutex for this ? - int sval =0; - sem_getvalue(&mHasStoppedSemaphore,&sval) ; + int sval = mHasStoppedSemaphore.value() ; #ifdef DEBUG_THREADS THREAD_DEBUG << " isRunning(): returning " << !sval << std::endl; @@ -93,12 +90,7 @@ bool RsThread::isRunning() bool RsThread::shouldStop() { - int sval =0; - sem_getvalue(&mShouldStopSemaphore,&sval) ; - -#ifdef DEBUG_THREADS - THREAD_DEBUG << " shouldStop(): returning " << (sval > 0) << " (sval=" << sval << ") " << std::endl; -#endif + int sval = mShouldStopSemaphore.value() ; return sval > 0; } @@ -108,8 +100,7 @@ void RsTickingThread::shutdown() THREAD_DEBUG << "pqithreadstreamer::shutdown()" << std::endl; #endif - int sval =0; - sem_getvalue(&mHasStoppedSemaphore,&sval) ; + int sval = mHasStoppedSemaphore.value() ; if(sval > 0) { @@ -127,7 +118,7 @@ void RsThread::ask_for_stop() #ifdef DEBUG_THREADS THREAD_DEBUG << " calling stop" << std::endl; #endif - sem_post(&mShouldStopSemaphore) ; + mShouldStopSemaphore.post(); } void RsTickingThread::fullstop() @@ -137,7 +128,7 @@ void RsTickingThread::fullstop() #ifdef DEBUG_THREADS THREAD_DEBUG << " waiting stop" << std::endl; #endif - sem_wait(&mHasStoppedSemaphore) ; + mHasStoppedSemaphore.wait(); #ifdef DEBUG_THREADS THREAD_DEBUG << " finished!" << std::endl; #endif @@ -147,12 +138,11 @@ void RsThread::start() pthread_t tid; void *data = (void *)this ; - RS_STACK_MUTEX(mMutex) ; - #ifdef DEBUG_THREADS THREAD_DEBUG << "pqithreadstreamer::start() initing should_stop=0, has_stopped=1" << std::endl; #endif - sem_init(&mHasStoppedSemaphore,0,0) ; + mHasStoppedSemaphore.set(0) ; + mShouldStopSemaphore.set(0) ; int err ; @@ -164,7 +154,7 @@ void RsThread::start() else { THREAD_DEBUG << "Fatal error: pthread_create could not create a thread. Error returned: " << err << " !!!!!!!" << std::endl; - sem_init(&mHasStoppedSemaphore,0,1) ; + mHasStoppedSemaphore.set(1) ; } } @@ -175,13 +165,11 @@ RsTickingThread::RsTickingThread() #ifdef DEBUG_THREADS THREAD_DEBUG << "RsTickingThread::RsTickingThread()" << std::endl; #endif - sem_init(&mShouldStopSemaphore,0,0) ; } void RsSingleJobThread::runloop() { - sem_init(&mShouldStopSemaphore,0,0) ; - + mShouldStopSemaphore.set(0) ; run() ; } @@ -190,7 +178,7 @@ void RsTickingThread::runloop() #ifdef DEBUG_THREADS THREAD_DEBUG << "pqithreadstream::runloop()" << std::endl; #endif - sem_init(&mShouldStopSemaphore,0,0) ; + mShouldStopSemaphore.set(0) ; while(1) { @@ -199,7 +187,7 @@ void RsTickingThread::runloop() #ifdef DEBUG_THREADS THREAD_DEBUG << "pqithreadstreamer::runloop(): asked to stop. setting hasStopped=1, and returning. Thread ends." << std::endl; #endif - sem_post(&mHasStoppedSemaphore) ; + mHasStoppedSemaphore.post(); return ; } diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index ffce99891..19d48b66f 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -29,7 +29,9 @@ #include #include #include +#include #include +#include /* RsIface Thread Wrappers */ @@ -168,6 +170,58 @@ class RsStackMutex // #define RS_STACK_MUTEX(m) RsStackMutex __local_retroshare_mutex(m,__PRETTY_FUNCTION__,__FILE__,__LINE__) +// This class handles a Mutex-based semaphore, that makes it cross plateform. +class RsSemaphore +{ + class RsSemStruct + { + public: + RsSemStruct() : mtx("Semaphore mutex"), val(0) {} + + RsMutex mtx ; + uint32_t val ; + }; + +public: + RsSemaphore() + { + s = new RsSemStruct ; + } + + ~RsSemaphore() + { + delete s ; + } + + void set(uint32_t i) + { + RS_STACK_MUTEX(s->mtx) ; + s->val = i ; + } + + void post() + { + RS_STACK_MUTEX(s->mtx) ; + ++(s->val) ; + } + + uint32_t value() + { + RS_STACK_MUTEX(s->mtx) ; + return s->val ; + } + + void wait() + { + while(value() == 0) + usleep(1000) ; + + post() ; + } +private: + RsSemStruct *s ; +}; + class RsThread; /* to create a thread! */ @@ -198,11 +252,10 @@ class RsThread protected: virtual void runloop() =0; /* called once the thread is started. Should be overloaded by subclasses. */ - sem_t mHasStoppedSemaphore; - sem_t mShouldStopSemaphore; + RsSemaphore mHasStoppedSemaphore; + RsSemaphore mShouldStopSemaphore; static void *rsthread_init(void*) ; - RsMutex mMutex; pthread_t mTid; }; From b5ace406b9a72d6348535d2a3784f4e09028fb1f Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 5 May 2016 21:21:45 -0400 Subject: [PATCH 11/12] attempt to fix socket problem on 32bits/64bits windows, by using the proper SOCKET type instead of int --- libretroshare/src/pqi/pqissllistener.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libretroshare/src/pqi/pqissllistener.cc b/libretroshare/src/pqi/pqissllistener.cc index 7426d9799..8ce1a5dbd 100644 --- a/libretroshare/src/pqi/pqissllistener.cc +++ b/libretroshare/src/pqi/pqissllistener.cc @@ -322,11 +322,12 @@ int pqissllistenbase::acceptconnection() // can't be arsed making them all the time. struct sockaddr_storage remote_addr; socklen_t addrlen = sizeof(remote_addr); - int fd = accept(lsock, (struct sockaddr *) &remote_addr, &addrlen); - int err = 0; /********************************** WINDOWS/UNIX SPECIFIC PART ******************/ #ifndef WINDOWS_SYS // ie UNIX + int fd = accept(lsock, (struct sockaddr *) &remote_addr, &addrlen); + int err = 0; + if (fd < 0) { pqioutput(PQL_DEBUG_ALL, pqissllistenzone, @@ -347,7 +348,10 @@ int pqissllistenbase::acceptconnection() /********************************** WINDOWS/UNIX SPECIFIC PART ******************/ #else //WINDOWS_SYS - if ((unsigned) fd == INVALID_SOCKET) + SOCKET fd = accept(lsock, (struct sockaddr *) &remote_addr, &addrlen); + int err = 0; + + if (fd == INVALID_SOCKET) { pqioutput(PQL_DEBUG_ALL, pqissllistenzone, "pqissllistenbase::acceptconnnection() Nothing to Accept!"); From 67e976057d14568b9e93a0d946912a4318f5ab6d Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 5 May 2016 22:03:46 -0400 Subject: [PATCH 12/12] removed debug output from threads --- libretroshare/src/util/rsthreads.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 896ecebd0..46021947d 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -40,7 +40,6 @@ * #define DEBUG_THREADS 1 * #define RSMUTEX_ABORT 1 // Catch wrong pthreads mode. *******/ -#define DEBUG_THREADS 1 #define THREAD_DEBUG std::cerr << "[caller thread ID: " << std::hex << pthread_self() << ", thread ID: " << mTid << std::dec << "] " #ifdef RSMUTEX_ABORT