setTrebleTestingOverride

This is being added in preparation of removing TREBLE_TESTING_OVERRIDE
since some clients may use setenv/etc in a multithreaded environment
which break the use of getenv to retrieve the value of this flag.

Bug: 156668058
Test: tests using either flag still work
Change-Id: I53e3fafa373e19814d9533ade0587e32278dfec7
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index a69cf3e..014958b 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -153,6 +153,36 @@
 
 namespace details {
 
+#ifdef ENFORCE_VINTF_MANIFEST
+static constexpr bool kEnforceVintfManifest = true;
+#else
+static constexpr bool kEnforceVintfManifest = false;
+#endif
+
+#ifdef LIBHIDL_TARGET_DEBUGGABLE
+static constexpr bool kDebuggable = true;
+#else
+static constexpr bool kDebuggable = false;
+#endif
+
+static bool gTrebleTestingOverride = false;
+
+void setTrebleTestingOverride(bool testingOverride) {
+    gTrebleTestingOverride = testingOverride;
+}
+
+static inline bool isTrebleTestingOverride() {
+    if (kEnforceVintfManifest && !kDebuggable) {
+        // don't allow testing override in production
+        return false;
+    }
+
+    if (gTrebleTestingOverride) return gTrebleTestingOverride;
+
+    const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
+    return env && !strcmp(env, "true");
+}
+
 /*
  * Returns the age of the current process by reading /proc/self/stat and comparing starttime to the
  * current time. This is useful for measuring how long it took a HAL to register itself.
@@ -373,10 +403,7 @@
 #endif
         };
 
-#ifdef LIBHIDL_TARGET_DEBUGGABLE
-        const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
-        const bool trebleTestingOverride = env && !strcmp(env, "true");
-        if (trebleTestingOverride) {
+        if (details::isTrebleTestingOverride()) {
             // Load HAL implementations that are statically linked
             handle = dlopen(nullptr, dlMode);
             if (handle == nullptr) {
@@ -387,7 +414,6 @@
                 return;
             }
         }
-#endif
 
         for (const std::string& path : paths) {
             std::vector<std::string> libs = findFiles(path, prefix, ".so");
@@ -738,28 +764,6 @@
     return false;
 }
 
-#ifdef ENFORCE_VINTF_MANIFEST
-static constexpr bool kEnforceVintfManifest = true;
-#else
-static constexpr bool kEnforceVintfManifest = false;
-#endif
-
-#ifdef LIBHIDL_TARGET_DEBUGGABLE
-static constexpr bool kDebuggable = true;
-#else
-static constexpr bool kDebuggable = false;
-#endif
-
-static inline bool isTrebleTestingOverride() {
-    if (kEnforceVintfManifest && !kDebuggable) {
-        // don't allow testing override in production
-        return false;
-    }
-
-    const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
-    return env && !strcmp(env, "true");
-}
-
 sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
                                                              const std::string& instance,
                                                              bool retry, bool getStub) {