diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2abfab1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +*.o +*.a +*.d +*.swp +ws +test_adapter +.gnupg +.pEp* +lib +local.conf +build/ +# Default ignored files +?idea/ +# Windows +build-windows/Debug/ +build-windows/Release/ +build-windows/libpEpAdapter.vcxproj.user +/test/test_adapter.dSYM/Contents/Info.plist +/test/test_adapter_cxx.dSYM/Contents/Info.plist +/test/test_ensure_passphrase.dSYM/Contents/Info.plist +/test/test_leave_device_group.dSYM/Contents/Info.plist +/test/test_library.dSYM/Contents/Info.plist +/test/test_message_cache.dSYM/Contents/Info.plist +/test/test_passphrase_cache.dSYM/Contents/Info.plist +/test/test_semaphore.dSYM/Contents/Info.plist +/test/test_adapter_cxx.dSYM/Contents/Resources/DWARF/test_adapter_cxx +/test/test_adapter_cxx +/test/test_ensure_passphrase.dSYM/Contents/Resources/DWARF/test_ensure_passphrase +/test/test_ensure_passphrase +/test/test_leave_device_group.dSYM/Contents/Resources/DWARF/test_leave_device_group +/test/test_leave_device_group +/test/test_library.dSYM/Contents/Resources/DWARF/test_library +/test/test_library +/test/test_message_cache.dSYM/Contents/Resources/DWARF/test_message_cache +/test/test_message_cache +/test/test_passphrase_cache.dSYM/Contents/Resources/DWARF/test_passphrase_cache +/test/test_passphrase_cache +/test/test_semaphore.dSYM/Contents/Resources/DWARF/test_semaphore +/test/test_semaphore diff --git a/.gitlab-ci-files/common-prepare.yml b/.gitlab-ci-files/common-prepare.yml new file mode 100644 index 0000000..8053794 --- /dev/null +++ b/.gitlab-ci-files/common-prepare.yml @@ -0,0 +1,22 @@ +.ensure_docker: &ensure_docker + # Check for docker and install if missing + - 'which docker || ( sudo apt-get update -y && sudo apt-get install docker.io -y )' + +.ensure_rsync: &ensure_rsync + # Install rsync and deps if missing + - 'which ssh-agent || ( sudo apt-get update -y && sudo apt-get install openssh-client -y )' + - 'which rsync || ( sudo apt-get update -y && sudo apt-get install rsync -y )' + - 'which make || ( sudo apt-get update -y && sudo apt-get install make -y )' + +.standard_job: + tags: [kvm] + before_script: + - *ensure_docker + - *ensure_rsync + +.make_in_docker: + extends: .standard_job + script: + - docker login -u ${DOCKER_REGISTRY_USER} -p ${DOCKER_REGISTRY_PASS} ${DOCKER_REGISTRY_HOST} + - cd scripts/${CI_DISTRO_TARGET} + - make diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..4dc8459 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,27 @@ +include: + - '.gitlab-ci-files/common-prepare.yml' + +stages: + - build + + +# Debian + +debian10:build: + extends: .make_in_docker + stage: build + variables: + CI_DISTRO_TARGET: "debian10" + DEBIAN_FRONTEND: "noninteractive" + rules: + - if: '$CI_COMMIT_TAG !~ /^Release_[0-9]+\.[0-9]+\.[0-9]+$/' + +debian10:tagged-build: + extends: .make_in_docker + stage: build + variables: + CI_DISTRO_TARGET: "debian10" + DEBIAN_FRONTEND: "noninteractive" + TAGGED_BUILD: "true" + rules: + - if: '$CI_COMMIT_TAG =~ /^Release_[0-9]+\.[0-9]+\.[0-9]+$/' diff --git a/.hgignore b/.hgignore deleted file mode 100644 index e5cac49..0000000 --- a/.hgignore +++ /dev/null @@ -1,14 +0,0 @@ -syntax: glob -*.o -*.a -*.d -*.swp -ws -test_adapter -.gnupg -.pEp* -lib -local.conf -build/ -# Default ignored files -.idea/workspace.xml diff --git a/Adapter.cc b/Adapter.cc index f4a776d..dfcaef9 100644 --- a/Adapter.cc +++ b/Adapter.cc @@ -42,7 +42,7 @@ namespace pEp { notifyHandshake_t _notifyHandshake = nullptr; std::thread _sync_thread; - ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q; + ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > sync_evt_q; std::mutex m; std::thread::id sync_thread_id() @@ -54,11 +54,11 @@ namespace pEp { { try { if (ev == nullptr) { - q.clear(); - q.push_back(ev); + sync_evt_q.clear(); + sync_evt_q.push_back(ev); } else { - q.push_front(ev); + sync_evt_q.push_front(ev); } } catch (exception&) { @@ -76,10 +76,11 @@ namespace pEp { SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold) { SYNC_EVENT syncEvent = nullptr; - const bool success = q.try_pop_front(syncEvent, std::chrono::seconds(threshold)); + const bool success = sync_evt_q.try_pop_front(syncEvent, std::chrono::seconds(threshold)); - if (!success) + if (!success) { return new_sync_timeout_event(); + } return syncEvent; } @@ -92,14 +93,14 @@ namespace pEp { PEP_SESSION Session::operator()(session_action action) { std::lock_guard lock(m); - bool in_sync = on_sync_thread(); PEP_STATUS status = PEP_STATUS_OK; switch (action) { case release: - if (_session.get()) + if (_session.get()) { _session = nullptr; + } break; case init: @@ -138,15 +139,16 @@ namespace pEp { { SYNC_EVENT ev; try { - ev = q.back(); + ev = sync_evt_q.back(); } catch (std::underflow_error&) { return false; } - if (ev) + if (ev) { return false; - else + } else { return true; + } } } } diff --git a/Adapter.hh b/Adapter.hh index d136f25..43f0f0c 100644 --- a/Adapter.hh +++ b/Adapter.hh @@ -1,7 +1,8 @@ // This file is under GNU General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef LIBPEPADAPTER_ADAPTER_HH +#define LIBPEPADAPTER_ADAPTER_HH #include #include @@ -68,3 +69,5 @@ namespace pEp { } #include "Adapter.hxx" + +#endif //LIBPEPADAPTER_ADAPTER_HH \ No newline at end of file diff --git a/Adapter.hxx b/Adapter.hxx index 32af595..2b8b054 100644 --- a/Adapter.hxx +++ b/Adapter.hxx @@ -1,12 +1,14 @@ // This file is under GNU General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef LIBPEPADAPTER_ADAPTER_HXX +#define LIBPEPADAPTER_ADAPTER_HXX #include #include "locked_queue.hh" #include #include "pEpLog.hh" +#include namespace pEp { namespace Adapter { @@ -16,35 +18,41 @@ namespace pEp { extern notifyHandshake_t _notifyHandshake; extern std::thread _sync_thread; - extern ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > q; + extern ::utility::locked_queue< SYNC_EVENT, ::free_Sync_event > sync_evt_q; extern std::mutex m; SYNC_EVENT _retrieve_next_sync_event(void *management, unsigned threshold); static std::exception_ptr _ex; - static bool register_done = false; + static std::atomic_bool register_done{false}; template< class T > void sync_thread(T *obj, function< void(T *) > _startup, function< void(T *) > _shutdown) { + pEpLog("called"); _ex = nullptr; assert(_messageToSend); assert(_notifyHandshake); - if (obj && _startup) + if (obj && _startup) { _startup(obj); + } + pEpLog("creating session"); session(); { + //TODO: Do we need to use a passphraseWrap here??? + pEpLog("register_sync_callbacks()"); PEP_STATUS status = register_sync_callbacks(session(), nullptr, _notifyHandshake, _retrieve_next_sync_event); + pEpLog("register_sync_callbacks() return:" << status); try { throw_status(status); - register_done = true; + register_done.store(true); } catch (...) { _ex = std::current_exception(); - register_done = true; + register_done.store(true); return; } } @@ -56,8 +64,9 @@ namespace pEp { session(release); - if (obj && _shutdown) + if (obj && _shutdown) { _shutdown(obj); + } } template< class T > @@ -68,23 +77,34 @@ namespace pEp { function< void(T *) > _startup, function< void(T *) > _shutdown) { - if (messageToSend) + pEpLog("called"); + if (messageToSend) { _messageToSend = messageToSend; + } - if (notifyHandshake) + if (notifyHandshake) { _notifyHandshake = notifyHandshake; - + } + pEpLog("creating session"); session(); if (!_sync_thread.joinable()) { - register_done = false; + register_done.store(false); + pEpLog("creating sync-thread"); + _sync_thread = std::thread(sync_thread, obj, _startup, _shutdown); - while (!register_done) + while (register_done.load() == false) { + pEpLog("waiting for sync-thread to init..."); std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } - if (_ex) + if (_ex) { + pEpLog("exception pending, rethrowing"); std::rethrow_exception(_ex); + } } } } } + +#endif //LIBPEPADAPTER_ADAPTER_HXX \ No newline at end of file diff --git a/DEPENDENCIES b/DEPENDENCIES new file mode 100644 index 0000000..37f0507 --- /dev/null +++ b/DEPENDENCIES @@ -0,0 +1,5 @@ +# 1st Party Dependencies +## Prefer git tags instead of SHA hashes when possible. + +pEpEngine=Release_2.1.18 +sequoia=365d00a08bec6a5a48d48a7c7893d78c27092b59 diff --git a/Makefile b/Makefile index 3c72e78..6eac3c6 100644 --- a/Makefile +++ b/Makefile @@ -13,15 +13,10 @@ SOURCE=$(wildcard *.cc) HEADERS=$(wildcard *.hh *.hxx) OBJECTS=$(subst .cc,.o,$(SOURCE)) DEPENDS=$(subst .cc,.d,$(SOURCE)) +CXXFLAGS+= -MMD -MP all: $(TARGET) -%.d: %.cc - @set -e; rm -f $@; \ - $(CXX) -MM $(CPPFLAGS) $(CXXFLAGS) $< > $@.$$$$; \ - sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ - rm -f $@.$$$$ - ifneq ($(MAKECMDGOALS),clean) -include $(DEPENDS) endif diff --git a/Semaphore.hh b/Semaphore.hh index e7d3f06..a9c0593 100644 --- a/Semaphore.hh +++ b/Semaphore.hh @@ -1,11 +1,22 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_SEMAPHORE_HH +#define LIBPEPADAPTER_SEMAPHORE_HH + #include #include +#include namespace pEp { class Semaphore { std::mutex mtx; std::condition_variable cv; - bool _stop; + // Atomic types are types that encapsulate a value whose access is guaranteed + // to not cause data races and can be used to synchronize memory accesses among + // different threads. + // To synchronize threads, ALWAYS use types + std::atomic_bool _stop; public: Semaphore() : _stop(false) { } @@ -13,25 +24,30 @@ namespace pEp { void stop() { std::unique_lock lock(mtx); - _stop = true; + _stop.store(true); } void try_wait() { std::unique_lock lock(mtx); - if (!_stop) + if (!_stop.load()) { return; + } - while(_stop) + while(_stop.load()) { cv.wait(lock); + } } void go() { std::unique_lock lock(mtx); - _stop = false; + _stop.store(false); cv.notify_all(); } }; } +#endif // LIBPEPADAPTER_SEMAPHORE_HH + + diff --git a/build-android/jni/Android.mk b/build-android/jni/Android.mk index 2594a0b..9664823 100644 --- a/build-android/jni/Android.mk +++ b/build-android/jni/Android.mk @@ -25,12 +25,7 @@ LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../pEpEngine/build-android/include \ LOCAL_EXPORT_C_INCLUDES += $(LOCAL_PATH)../include -LOCAL_SRC_FILES += $(LOCAL_PATH)/../../Adapter.cc \ - $(LOCAL_PATH)/../../slurp.cc \ - $(LOCAL_PATH)/../../call_with_lock.cc \ - $(LOCAL_PATH)/../../passphrase_cache.cc \ - $(LOCAL_PATH)/../../callback_dispatcher.cc \ - $(LOCAL_PATH)/../../status_to_string.cc +LOCAL_SRC_FILES += $(wildcard $(LOCAL_PATH)/../../*.cc) include $(BUILD_STATIC_LIBRARY) diff --git a/libpEpAdapter/ReadMe.txt b/build-windows/ReadMe.txt similarity index 100% rename from libpEpAdapter/ReadMe.txt rename to build-windows/ReadMe.txt diff --git a/libpEpAdapter/libpEpAdapter.vcxproj b/build-windows/libpEpAdapter.vcxproj similarity index 90% rename from libpEpAdapter/libpEpAdapter.vcxproj rename to build-windows/libpEpAdapter.vcxproj index 1240878..eabd4a5 100644 --- a/libpEpAdapter/libpEpAdapter.vcxproj +++ b/build-windows/libpEpAdapter.vcxproj @@ -64,11 +64,6 @@ Windows - - XCOPY "$(SolutionDir)libpEpAdapter\*.hh" "$(SolutionDir)pEp\" /I/F/Y -XCOPY "$(SolutionDir)libpEpAdapter\*.hxx" "$(SolutionDir)pEp\" /I/F/Y - - @@ -86,11 +81,6 @@ XCOPY "$(SolutionDir)libpEpAdapter\*.hxx" "$(SolutionDir)pEp\" /I/F/Y true true - - XCOPY "$(SolutionDir)libpEpAdapter\*.hh" "$(SolutionDir)pEp\" /I/F/Y -XCOPY "$(SolutionDir)libpEpAdapter\*.hxx" "$(SolutionDir)pEp\" /I/F/Y - - @@ -102,6 +92,7 @@ XCOPY "$(SolutionDir)libpEpAdapter\*.hxx" "$(SolutionDir)pEp\" /I/F/Y + @@ -115,10 +106,16 @@ XCOPY "$(SolutionDir)libpEpAdapter\*.hxx" "$(SolutionDir)pEp\" /I/F/Y + + + + {146e69f8-e1da-456a-b048-6dd29d9acf6b} + + diff --git a/libpEpAdapter/libpEpAdapter.vcxproj.filters b/build-windows/libpEpAdapter.vcxproj.filters similarity index 91% rename from libpEpAdapter/libpEpAdapter.vcxproj.filters rename to build-windows/libpEpAdapter.vcxproj.filters index 386d89f..ccbd6b4 100644 --- a/libpEpAdapter/libpEpAdapter.vcxproj.filters +++ b/build-windows/libpEpAdapter.vcxproj.filters @@ -42,6 +42,9 @@ Source Files + + Source Files + @@ -80,5 +83,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/call_with_lock.cc b/call_with_lock.cc index cd04003..83ba416 100644 --- a/call_with_lock.cc +++ b/call_with_lock.cc @@ -1,4 +1,5 @@ -// this file is under GNU GPL 3.0, see LICENSE.txt +// This file is under GNU General Public License 3.0 +// see LICENSE.txt #include "call_with_lock.hh" diff --git a/call_with_lock.hh b/call_with_lock.hh index 1877bcc..c8ebd73 100644 --- a/call_with_lock.hh +++ b/call_with_lock.hh @@ -1,7 +1,8 @@ -// this file is under GNU GPL 3.0, see LICENSE.txt +// This file is under GNU General Public License 3.0 +// see LICENSE.txt -#ifndef PEP_CALL_WITH_LOCK_HH -#define PEP_CALL_WITH_LOCK_HH +#ifndef LIBPEPADAPTER_CALL_WITH_LOCK_HH +#define LIBPEPADAPTER_CALL_WITH_LOCK_HH #include @@ -22,4 +23,4 @@ namespace pEp } -#endif // PEP_CALL_WITH_LOCK_HH +#endif // LIBPEPADAPTER_CALL_WITH_LOCK_HH diff --git a/callback_dispatcher.cc b/callback_dispatcher.cc index 08bf769..42a743c 100644 --- a/callback_dispatcher.cc +++ b/callback_dispatcher.cc @@ -1,3 +1,6 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + #include "callback_dispatcher.hh" #include "passphrase_cache.hh" #include @@ -25,8 +28,9 @@ namespace pEp { ) { assert(messageToSend); - if (!messageToSend) + if (!messageToSend) { throw std::invalid_argument("messageToSend must be set"); + } targets.push_back({messageToSend, notifyHandshake, on_startup, shutdown}); } @@ -34,8 +38,9 @@ namespace pEp { void CallbackDispatcher::remove(::messageToSend_t messageToSend) { assert(messageToSend); - if (!messageToSend) + if (!messageToSend) { throw std::invalid_argument("messageToSend argument needed"); + } for (auto target = targets.begin(); target != targets.end(); ++target) { if (target->messageToSend == messageToSend) { @@ -43,28 +48,33 @@ namespace pEp { break; } } - if (targets.empty()) + + if (targets.empty()) { stop_sync(); + } } void CallbackDispatcher::on_startup() { for (auto target : targets) { - if (target.on_startup) + if (target.on_startup) { target.on_startup(); + } } } void CallbackDispatcher::on_shutdown() { for (auto target : targets) { - if (target.on_shutdown) + if (target.on_shutdown) { target.on_shutdown(); + } } } void CallbackDispatcher::start_sync() { + pEpLog("called"); callback_dispatcher.semaphore.go(); pEp::Adapter::startup(CallbackDispatcher::messageToSend, @@ -72,9 +82,11 @@ namespace pEp { &CallbackDispatcher::on_startup, &CallbackDispatcher::on_shutdown); + pEpLog("all targets signal: SYNC_NOTIFY_START"); for (auto target : callback_dispatcher.targets) { - if (target.notifyHandshake) + if (target.notifyHandshake) { target.notifyHandshake(nullptr, nullptr, SYNC_NOTIFY_START); + } } } @@ -85,8 +97,9 @@ namespace pEp { callback_dispatcher.semaphore.go(); for (auto target : callback_dispatcher.targets) { - if (target.notifyHandshake) + if (target.notifyHandshake) { target.notifyHandshake(nullptr, nullptr, SYNC_NOTIFY_STOP); + } } } @@ -95,14 +108,16 @@ namespace pEp { if (Adapter::on_sync_thread() && !msg) { semaphore.try_wait(); - if (Adapter::in_shutdown()) + if (Adapter::in_shutdown()) { return PEP_SYNC_NO_CHANNEL; + } PEP_STATUS status = PassphraseCache::config_next_passphrase(); // if the cache has no valid passphrase ask the app - if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE) + if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE) { semaphore.stop(); + } // the pEp engine must try again return status; @@ -117,8 +132,9 @@ namespace pEp { ::message *_msg = nullptr; if (msg) { _msg = ::message_dup(msg); - if (!_msg) + if (!_msg) { return PEP_OUT_OF_MEMORY; + } } assert(target.messageToSend); target.messageToSend(_msg); @@ -135,8 +151,9 @@ namespace pEp { ::pEp_identity *_me = nullptr; if (me) { _me = ::identity_dup(me); - if (!_me) + if (!_me) { return PEP_OUT_OF_MEMORY; + } } ::pEp_identity *_partner = nullptr; if (partner) { diff --git a/callback_dispatcher.hh b/callback_dispatcher.hh index 17f39e3..54248cf 100644 --- a/callback_dispatcher.hh +++ b/callback_dispatcher.hh @@ -1,4 +1,8 @@ -#pragma once +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_CALLBACK_DISPATCHER_HH +#define LIBPEPADAPTER_CALLBACK_DISPATCHER_HH #include #include @@ -27,30 +31,37 @@ namespace pEp { public: void add( - ::messageToSend_t messageToSend, - ::notifyHandshake_t notifyHandshake, - proc on_startup = nullptr, - proc on_shutdown = nullptr - ); + ::messageToSend_t messageToSend, + ::notifyHandshake_t notifyHandshake, + proc on_startup = nullptr, + proc on_shutdown = nullptr + ); void remove(::messageToSend_t messageToSend); static void start_sync(); static void stop_sync(); static PEP_STATUS messageToSend(::message *msg); - static PEP_STATUS notifyHandshake(::pEp_identity *me, - ::pEp_identity *partner, ::sync_handshake_signal signal); + static PEP_STATUS notifyHandshake( + ::pEp_identity *me, + ::pEp_identity *partner, + ::sync_handshake_signal signal + ); protected: void on_startup(); void on_shutdown(); PEP_STATUS _messageToSend(::message *msg); - PEP_STATUS _notifyHandshake(::pEp_identity *me, - ::pEp_identity *partner, ::sync_handshake_signal signal); + PEP_STATUS _notifyHandshake( + ::pEp_identity *me, + ::pEp_identity *partner, + ::sync_handshake_signal signal + ); friend const char *PassphraseCache::add(const std::string& passphrase); }; extern CallbackDispatcher callback_dispatcher; -}; +} +#endif // LIBPEPADAPTER_CALLBACK_DISPATCHER_HH diff --git a/constant_time_algo.cc b/constant_time_algo.cc index 6e4c2c4..8368b9e 100644 --- a/constant_time_algo.cc +++ b/constant_time_algo.cc @@ -1,3 +1,6 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + #include "constant_time_algo.hh" namespace pEp diff --git a/constant_time_algo.hh b/constant_time_algo.hh index d9b7ae2..59463ac 100644 --- a/constant_time_algo.hh +++ b/constant_time_algo.hh @@ -1,4 +1,8 @@ -#pragma once +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_CONSTANT_TIME_ALGO_HH +#define LIBPEPADAPTER_CONSTANT_TIME_ALGO_HH #include @@ -12,3 +16,5 @@ namespace pEp bool constant_time_equal(const std::string& a, const std::string& b); } // end of namespace pEp + +#endif // LIBPEPADAPTER_CONSTANT_TIME_ALGO_HH diff --git a/locked_queue.hh b/locked_queue.hh index ccf45ea..e440704 100644 --- a/locked_queue.hh +++ b/locked_queue.hh @@ -1,7 +1,8 @@ // This file is under GNU General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef LIBPEPADAPTER_LOCKED_QUEUE_HH +#define LIBPEPADAPTER_LOCKED_QUEUE_HH #include #include @@ -182,3 +183,5 @@ namespace utility }; } // end of namespace utility + +#endif // LIBPEPADAPTER_LOCKED_QUEUE_HH diff --git a/message_cache.cc b/message_cache.cc index fd8a28c..f507253 100644 --- a/message_cache.cc +++ b/message_cache.cc @@ -1,3 +1,6 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + #include "message_cache.hh" #include #include diff --git a/message_cache.hh b/message_cache.hh index 7c3326d..b5c06be 100644 --- a/message_cache.hh +++ b/message_cache.hh @@ -1,4 +1,8 @@ -#pragma once +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_MESSAGE_CACHE_HH +#define LIBPEPADAPTER_MESSAGE_CACHE_HH #include #include @@ -128,3 +132,4 @@ namespace pEp { extern MessageCache message_cache; }; +#endif // LIBPEPADAPTER_MESSAGE_CACHE_HH diff --git a/pEpLog.cc b/pEpLog.cc new file mode 100644 index 0000000..08af695 --- /dev/null +++ b/pEpLog.cc @@ -0,0 +1,40 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#include "pEpLog.hh" +#include +#include +#include +#include + + +namespace pEp { +namespace Adapter { +namespace pEpLog { + +std::mutex mtx; + +std::atomic_bool is_enabled{false}; + +void set_enabled(bool enabled) { + is_enabled.store(enabled); +} + +bool get_enabled() { + return is_enabled.load(); +} + +void log(std::string msg) { + if (is_enabled.load()) { + std::lock_guard l(mtx); + #ifdef ANDROID + __android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str()); + #else + std::cout << msg << std::endl; //std::endl also flushes + #endif + } +} + +} // pEpLog +} // Adapter +} // pEp diff --git a/pEpLog.hh b/pEpLog.hh index 12937e1..16854bf 100644 --- a/pEpLog.hh +++ b/pEpLog.hh @@ -1,7 +1,62 @@ -// TODO: put into not yet existing libpEpAdapter_utils.h, to be across whole libpEpAdapter +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_PEPLOG_HH +#define LIBPEPADAPTER_PEPLOG_HH + +#include +#include + +// pEpLog +// ====== +// a "to be kept ultra small and simple" logging unit. +// featuring: +// * pEpLog macro that will be eliminated in release-builds (-DNDEBUG=1) +// * thread safe (no interleave when logging from diff threads) +// * OS dependent backend switches: +// * android: __android_log_print +// * all other OS: cout +// * runtime enabled/disabled switch (global) +// +// You might want more and more features, but the feature-policy is very restrictive, and there is a +// primary design goal to keep it simple, maintainable and portable. +// +// How to use: +// include +// use the macro pEpLog(msg) to do logging +// use NDEBUG=1 to turn logging on/off at compile-time +// use set_enabled(bool) to turn logging on/off at runtime +// use set_enabled_(bool) to turn logging on/off per backend + #ifdef NDEBUG #define pEpLog(msg) do{}while(0) #else - #include - #define pEpLog(msg) do{std::cerr << __FILE__ << "::" << __FUNCTION__ << " - " << msg << '\n';} while(0) -#endif \ No newline at end of file + #ifdef ANDROID + #include + #endif + + #define pEpLog(msg) \ + do { \ + std::stringstream msg_ss; \ + msg_ss << std::this_thread::get_id() << " - " << __FILE__ << "::" << __FUNCTION__ << " - " << msg; \ + pEp::Adapter::pEpLog::log(msg_ss.str()); \ + } while(0) +#endif // NDEBUG + +namespace pEp { +namespace Adapter { +namespace pEpLog { + +void log(std::string msg); + +void set_enabled(bool is_enabled); + +bool get_enabled(); + +} // pEpLog +} // Adapter +} // pEp + + +#endif // LIBPEPADAPTER_PEPLOG_HH + diff --git a/passphrase_cache.cc b/passphrase_cache.cc index c3b46e9..ee170f2 100644 --- a/passphrase_cache.cc +++ b/passphrase_cache.cc @@ -1,3 +1,6 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + #include #include "Adapter.hh" #include "passphrase_cache.hh" diff --git a/passphrase_cache.hh b/passphrase_cache.hh index b857590..b514492 100644 --- a/passphrase_cache.hh +++ b/passphrase_cache.hh @@ -1,4 +1,8 @@ -#pragma once +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_PASSPHRASE_CACHE_HH +#define LIBPEPADAPTER_PASSPHRASE_CACHE_HH #include #include @@ -85,3 +89,4 @@ namespace pEp { #include "passphrase_cache.hxx" +#endif // LIBPEPADAPTER_PASSPHRASE_CACHE_HH diff --git a/passphrase_cache.hxx b/passphrase_cache.hxx index cb97d7a..ad5c616 100644 --- a/passphrase_cache.hxx +++ b/passphrase_cache.hxx @@ -1,3 +1,9 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_PASSPHRASE_CACHE_HXX +#define LIBPEPADAPTER_PASSPHRASE_CACHE_HXX + #include "passphrase_cache.hh" namespace pEp { @@ -20,3 +26,4 @@ namespace pEp { } }; +#endif // LIBPEPADAPTER_PASSPHRASE_CACHE_HXX diff --git a/pc_container.hh b/pc_container.hh index f470b92..d41e176 100644 --- a/pc_container.hh +++ b/pc_container.hh @@ -1,9 +1,10 @@ // This file is under GNU General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef LIBPEPADAPTER_PC_CONTAINER_HH +#define LIBPEPADAPTER_PC_CONTAINER_HH -// Conainer adapter that contains a container and a producer/consume queue +// Container adapter that contains a container and a producer/consume queue // that holds references to all changed elements #include @@ -82,4 +83,5 @@ private: }; } // end of namespace pEp +#endif // LIBPEPADAPTER_PC_CONTAINER_HH diff --git a/scripts/centos8/Makefile b/scripts/centos8/Makefile new file mode 100644 index 0000000..8e10078 --- /dev/null +++ b/scripts/centos8/Makefile @@ -0,0 +1,15 @@ +CURRENT_DISTRO=$(shell basename $(shell pwd)) +LIBPEPADAPTER_VERSION=$(shell git rev-parse --short=8 HEAD) +IMAGE_NAME=${DOCKER_REGISTRY_HOST}/pep-$(CURRENT_DISTRO)-libpepadapter +DOCKERFILE=libpEpAdapter.$(CURRENT_DISTRO).Dockerfile +all: + -docker pull $(IMAGE_NAME):latest + cd ../../ && docker build --build-arg CURRENT_DISTRO=$(CURRENT_DISTRO) \ + --build-arg DOCKER_REGISTRY_HOST=${DOCKER_REGISTRY_HOST} \ + --build-arg LIBPEPADAPTER_VERSION=$(LIBPEPADAPTER_VERSION) \ + --cache-from $(IMAGE_NAME):latest \ + --tag=$(IMAGE_NAME):$(LIBPEPADAPTER_VERSION) \ + --tag=$(IMAGE_NAME):latest \ + -f scripts/${CURRENT_DISTRO}/$(DOCKERFILE) . + docker push $(IMAGE_NAME):${LIBPEPADAPTER_VERSION} + docker push $(IMAGE_NAME):latest diff --git a/scripts/centos8/build_libpEpAdapter.sh b/scripts/centos8/build_libpEpAdapter.sh new file mode 100755 index 0000000..1f4a6c5 --- /dev/null +++ b/scripts/centos8/build_libpEpAdapter.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env sh +set -exo + +echo "ENGINE_LIB_PATH=${INSTPREFIX}/lib" >> local.conf +echo "ENGINE_INC_PATH=${INSTPREFIX}/include" >> local.conf + +make +make install PREFIX="${INSTPREFIX}" diff --git a/scripts/centos8/libpEpAdapter.centos8.Dockerfile b/scripts/centos8/libpEpAdapter.centos8.Dockerfile new file mode 100644 index 0000000..a2fe8a3 --- /dev/null +++ b/scripts/centos8/libpEpAdapter.centos8.Dockerfile @@ -0,0 +1,23 @@ +ARG DOCKER_REGISTRY_HOST +ARG CURRENT_DISTRO +ARG PEPENGINE_VERSION=latest +FROM ${DOCKER_REGISTRY_HOST}/pep-${CURRENT_DISTRO}-engine:${PEPENGINE_VERSION} + +ENV BUILDROOT /build +ENV INSTPREFIX /install +ENV OUTDIR /out + +### Setup working directory +RUN mkdir ${BUILDROOT}/libpEpAdapter +COPY . ${BUILDROOT}/libpEpAdapter +USER root +RUN chown -R pep-builder:pep-builder ${BUILDROOT}/libpEpAdapter +USER pep-builder +WORKDIR ${BUILDROOT}/libpEpAdapter + +ARG LIBPEPADAPTER_VERSION +ARG CURRENT_DISTRO + +### Build libpEpAdapter +RUN sh ./scripts/${CURRENT_DISTRO}/build_libpEpAdapter.sh && \ + rm -rf ${BUILDROOT}/* diff --git a/scripts/debian10/Makefile b/scripts/debian10/Makefile new file mode 100644 index 0000000..6d59421 --- /dev/null +++ b/scripts/debian10/Makefile @@ -0,0 +1,27 @@ +include ../../DEPENDENCIES +export +PEPENGINE_VERSION=${pEpEngine} +CURRENT_DISTRO=$(shell basename $(shell pwd)) +IMAGE_NAME=${DOCKER_REGISTRY_HOST}/pep-$(CURRENT_DISTRO)-libpepadapter +DOCKERFILE=libpEpAdapter.$(CURRENT_DISTRO).Dockerfile +IS_TAGGED=${TAGGED_BUILD} +ifeq ($(IS_TAGGED), true) +# $CI_COMMIT_TAG is a predefined environment variable from Gitlab + LIBPEPADAPTER_VERSION=${CI_COMMIT_TAG} +else + LIBPEPADAPTER_VERSION=$(shell git rev-parse --short=8 HEAD) +endif +all: + -docker pull $(IMAGE_NAME):latest + cd ../../ && docker build --build-arg CURRENT_DISTRO=$(CURRENT_DISTRO) \ + --build-arg DOCKER_REGISTRY_HOST=${DOCKER_REGISTRY_HOST} \ + --build-arg PEPENGINE_VERSION=$(PEPENGINE_VERSION) \ + --build-arg LIBPEPADAPTER_VERSION=$(LIBPEPADAPTER_VERSION) \ + --cache-from $(IMAGE_NAME):latest \ + --tag=$(IMAGE_NAME):$(LIBPEPADAPTER_VERSION) \ + --tag=$(IMAGE_NAME):${LIBPEPADAPTER_VERSION}_engine-${PEPENGINE_VERSION} \ + --tag=$(IMAGE_NAME):latest \ + -f scripts/${CURRENT_DISTRO}/$(DOCKERFILE) . + docker push $(IMAGE_NAME):${LIBPEPADAPTER_VERSION} + docker push $(IMAGE_NAME):${LIBPEPADAPTER_VERSION}_engine-${PEPENGINE_VERSION} + docker push $(IMAGE_NAME):latest diff --git a/scripts/debian10/build_libpEpAdapter.sh b/scripts/debian10/build_libpEpAdapter.sh new file mode 100755 index 0000000..1f4a6c5 --- /dev/null +++ b/scripts/debian10/build_libpEpAdapter.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env sh +set -exo + +echo "ENGINE_LIB_PATH=${INSTPREFIX}/lib" >> local.conf +echo "ENGINE_INC_PATH=${INSTPREFIX}/include" >> local.conf + +make +make install PREFIX="${INSTPREFIX}" diff --git a/scripts/debian10/libpEpAdapter.debian10.Dockerfile b/scripts/debian10/libpEpAdapter.debian10.Dockerfile new file mode 100644 index 0000000..a2fe8a3 --- /dev/null +++ b/scripts/debian10/libpEpAdapter.debian10.Dockerfile @@ -0,0 +1,23 @@ +ARG DOCKER_REGISTRY_HOST +ARG CURRENT_DISTRO +ARG PEPENGINE_VERSION=latest +FROM ${DOCKER_REGISTRY_HOST}/pep-${CURRENT_DISTRO}-engine:${PEPENGINE_VERSION} + +ENV BUILDROOT /build +ENV INSTPREFIX /install +ENV OUTDIR /out + +### Setup working directory +RUN mkdir ${BUILDROOT}/libpEpAdapter +COPY . ${BUILDROOT}/libpEpAdapter +USER root +RUN chown -R pep-builder:pep-builder ${BUILDROOT}/libpEpAdapter +USER pep-builder +WORKDIR ${BUILDROOT}/libpEpAdapter + +ARG LIBPEPADAPTER_VERSION +ARG CURRENT_DISTRO + +### Build libpEpAdapter +RUN sh ./scripts/${CURRENT_DISTRO}/build_libpEpAdapter.sh && \ + rm -rf ${BUILDROOT}/* diff --git a/slurp.cc b/slurp.cc index 15519ab..a5cb3c5 100644 --- a/slurp.cc +++ b/slurp.cc @@ -1,3 +1,6 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + #include "slurp.hh" #include #include diff --git a/slurp.hh b/slurp.hh index e990a99..7e65eaf 100644 --- a/slurp.hh +++ b/slurp.hh @@ -1,5 +1,8 @@ -#ifndef PEP_LIB_SLURP_HH -#define PEP_LIB_SLURP_HH +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_LIB_SLURP_HH +#define LIBPEPADAPTER_LIB_SLURP_HH #include @@ -11,4 +14,4 @@ namespace pEp } // end of namespace pEp -#endif // PEP_LIB_SLURP_HH \ No newline at end of file +#endif // LIBPEPADAPTER_LIB_SLURP_HH \ No newline at end of file diff --git a/status_to_string.cc b/status_to_string.cc index 123ed9e..c95e36e 100644 --- a/status_to_string.cc +++ b/status_to_string.cc @@ -1,3 +1,6 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + #include "status_to_string.hh" #include diff --git a/status_to_string.hh b/status_to_string.hh index e9ae835..5078ef3 100644 --- a/status_to_string.hh +++ b/status_to_string.hh @@ -1,4 +1,8 @@ -#pragma once +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef LIBPEPADAPTER_STATUS_TO_STRING_HH +#define LIBPEPADAPTER_STATUS_TO_STRING_HH #include #include @@ -11,3 +15,5 @@ namespace pEp std::string status_to_string(PEP_STATUS status); } // end of namespace pEp + +#endif // LIBPEPADAPTER_STATUS_TO_STRING_HH diff --git a/test/framework.hh b/test/framework.hh index f0b28f7..05c5711 100644 --- a/test/framework.hh +++ b/test/framework.hh @@ -1,4 +1,7 @@ -#pragma once +// This file is under GNU General Public License 3.0 +// see LICENSE.txt +#ifndef LIBPEPADAPTER_FRAMEWORK_HH +#define LIBPEPADAPTER_FRAMEWORK_HH #include #include @@ -57,3 +60,4 @@ namespace pEp { }; }; +#endif // LIBPEPADAPTER_FRAMEWORK_HH