diff --git a/source/backend/plugin/CarlaPluginLV2.cpp b/source/backend/plugin/CarlaPluginLV2.cpp index a0c4d02a3..76f705562 100644 --- a/source/backend/plugin/CarlaPluginLV2.cpp +++ b/source/backend/plugin/CarlaPluginLV2.cpp @@ -25,6 +25,7 @@ #include "CarlaLv2Utils.hpp" #include "CarlaBase64Utils.hpp" +#include "CarlaEngineUtils.hpp" #include "CarlaPipeUtils.hpp" #include "CarlaPluginUI.hpp" #include "Lv2AtomRingBuffer.hpp" @@ -393,8 +394,9 @@ public: UiCrashed }; - CarlaPipeServerLV2(CarlaPluginLV2* const plugin) - : kPlugin(plugin), + CarlaPipeServerLV2(CarlaEngine* const engine, CarlaPluginLV2* const plugin) + : kEngine(engine), + kPlugin(plugin), fFilename(), fPluginURI(), fUiURI(), @@ -422,6 +424,12 @@ public: bool startPipeServer() noexcept { + const ScopedEngineEnvironmentLocker _seel(kEngine); + const ScopedEnvVar _sev1("LV2_PATH", kEngine->getOptions().pathLV2); +#ifdef CARLA_OS_LINUX + const ScopedEnvVar _sev2("LD_PRELOAD", nullptr); +#endif + return CarlaPipeServer::startPipeServer(fFilename, fPluginURI, fUiURI); } @@ -470,6 +478,7 @@ protected: bool msgReceived(const char* const msg) noexcept override; private: + CarlaEngine* const kEngine; CarlaPluginLV2* const kPlugin; CarlaString fFilename; @@ -506,7 +515,7 @@ public: fEventsIn(), fEventsOut(), fLv2Options(), - fPipeServer(this), + fPipeServer(engine, this), fCustomURIDs(), fFirstActive(true), fLastStateChunk(nullptr), diff --git a/source/utils/CarlaPipeUtils.cpp b/source/utils/CarlaPipeUtils.cpp index ba623e23b..f1fe0a8b0 100644 --- a/source/utils/CarlaPipeUtils.cpp +++ b/source/utils/CarlaPipeUtils.cpp @@ -1558,6 +1558,53 @@ void CarlaPipeClient::closePipeClient() noexcept // ----------------------------------------------------------------------- +ScopedEnvVar::ScopedEnvVar(const char* const key, const char* const value) noexcept + : fKey(nullptr), + fOrigValue(nullptr) +{ + CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',); + + fKey = carla_strdup_safe(key); + CARLA_SAFE_ASSERT_RETURN(fKey != nullptr,); + + if (const char* const origValue = std::getenv(key)) + { + fOrigValue = carla_strdup_safe(origValue); + CARLA_SAFE_ASSERT_RETURN(fOrigValue != nullptr,); + } + + if (value != nullptr) + carla_setenv(key, value); + else if (fOrigValue != nullptr) + carla_unsetenv(key); +} + +ScopedEnvVar::~ScopedEnvVar() noexcept +{ + bool hasOrigValue = false; + + if (fOrigValue != nullptr) + { + hasOrigValue = true; + + carla_setenv(fKey, fOrigValue); + + delete[] fOrigValue; + fOrigValue = nullptr; + } + + if (fKey != nullptr) + { + if (! hasOrigValue) + carla_unsetenv(fKey); + + delete[] fKey; + fKey = nullptr; + } +} + +// ----------------------------------------------------------------------- + ScopedLocale::ScopedLocale() noexcept : fLocale(carla_strdup_safe(::setlocale(LC_NUMERIC, nullptr))) { diff --git a/source/utils/CarlaPipeUtils.hpp b/source/utils/CarlaPipeUtils.hpp index fb9ffce6f..412237d9b 100644 --- a/source/utils/CarlaPipeUtils.hpp +++ b/source/utils/CarlaPipeUtils.hpp @@ -319,6 +319,22 @@ public: CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeClient) }; +// ----------------------------------------------------------------------- +// ScopedEnvVar class + +class ScopedEnvVar { +public: + ScopedEnvVar(const char* const key, const char* const value) noexcept; + ~ScopedEnvVar() noexcept; + +private: + const char* fKey; + const char* fOrigValue; + + CARLA_DECLARE_NON_COPY_CLASS(ScopedEnvVar) + CARLA_PREVENT_HEAP_ALLOCATION +}; + // ----------------------------------------------------------------------- // ScopedLocale class diff --git a/source/utils/CarlaUtils.hpp b/source/utils/CarlaUtils.hpp index b4119debb..40cc53861 100644 --- a/source/utils/CarlaUtils.hpp +++ b/source/utils/CarlaUtils.hpp @@ -258,6 +258,23 @@ void carla_setenv(const char* const key, const char* const value) noexcept #endif } +/* + * Unset environment variable 'key'. + */ +static inline +void carla_unsetenv(const char* const key) noexcept +{ + CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',); + +#ifdef CARLA_OS_WIN + try { + ::SetEnvironmentVariableA(key, nullptr); + } CARLA_SAFE_EXCEPTION("carla_unsetenv"); +#else + ::unsetenv(key); +#endif +} + // ----------------------------------------------------------------------- // carla_strdup