Merge "libhidl_test: fatal -> error warning"
diff --git a/base/include/hidl/HidlInternal.h b/base/include/hidl/HidlInternal.h
index 3cd246a..3d1a444 100644
--- a/base/include/hidl/HidlInternal.h
+++ b/base/include/hidl/HidlInternal.h
@@ -201,7 +201,10 @@
     // A list of registered instrumentation callbacks.
     std::vector<InstrumentationCallback> mInstrumentationCallbacks;
     // Flag whether to enable instrumentation.
-    bool mEnableInstrumentation;
+    union {
+        bool mEnableInstrumentation;
+        void* mReserved0;
+    };
     // Prefix to lookup the instrumentation libraries.
     std::string mInstrumentationLibPackage;
     // Used for dlsym to load the profiling method for given interface.
@@ -209,6 +212,12 @@
 
 };
 
+#ifdef __LP64__
+static_assert(sizeof(HidlInstrumentor) == 88, "HidlInstrumentor size frozen by prebuilts");
+#else
+static_assert(sizeof(HidlInstrumentor) == 44, "HidlInstrumentor size frozen by prebuilts");
+#endif
+
 }  // namespace details
 }  // namespace hardware
 }  // namespace android
diff --git a/transport/HidlLazyUtils.cpp b/transport/HidlLazyUtils.cpp
index ce37dd0..be7470f 100644
--- a/transport/HidlLazyUtils.cpp
+++ b/transport/HidlLazyUtils.cpp
@@ -40,8 +40,7 @@
 
     void reRegister();
 
-    void setActiveServicesCountCallback(
-            const std::function<bool(int)>& activeServicesCountCallback);
+    void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
 
   protected:
     Return<void> onClients(const sp<IBase>& service, bool clients) override;
@@ -80,7 +79,12 @@
     /**
      * Callback for reporting the number of services with clients.
      */
-    std::function<bool(int)> mActiveServicesCountCallback;
+    std::function<bool(bool)> mActiveServicesCallback;
+
+    /**
+     * Previous value passed to the active services callback.
+     */
+    std::optional<bool> mPreviousHasClients;
 };
 
 class LazyServiceRegistrarImpl {
@@ -91,8 +95,7 @@
                              const std::string& name);
     bool tryUnregister();
     void reRegister();
-    void setActiveServicesCountCallback(
-            const std::function<bool(int)>& activeServicesCountCallback);
+    void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
 
   private:
     sp<ClientCounterCallback> mClientCallback;
@@ -166,8 +169,12 @@
               << "/" << registered.name << " has clients: " << clients;
 
     bool handledInCallback = false;
-    if (mActiveServicesCountCallback != nullptr) {
-        handledInCallback = mActiveServicesCountCallback(numWithClients);
+    if (mActiveServicesCallback != nullptr) {
+        bool hasClients = numWithClients != 0;
+        if (hasClients != mPreviousHasClients) {
+            handledInCallback = mActiveServicesCallback(hasClients);
+            mPreviousHasClients = hasClients;
+        }
     }
 
     // If there is no callback defined or the callback did not handle this
@@ -229,9 +236,9 @@
     reRegister();
 }
 
-void ClientCounterCallback::setActiveServicesCountCallback(
-        const std::function<bool(int)>& activeServicesCountCallback) {
-    mActiveServicesCountCallback = activeServicesCountCallback;
+void ClientCounterCallback::setActiveServicesCallback(
+        const std::function<bool(bool)>& activeServicesCallback) {
+    mActiveServicesCallback = activeServicesCallback;
 }
 
 status_t LazyServiceRegistrarImpl::registerService(
@@ -251,9 +258,9 @@
     mClientCallback->reRegister();
 }
 
-void LazyServiceRegistrarImpl::setActiveServicesCountCallback(
-        const std::function<bool(int)>& activeServicesCountCallback) {
-    mClientCallback->setActiveServicesCountCallback(activeServicesCountCallback);
+void LazyServiceRegistrarImpl::setActiveServicesCallback(
+        const std::function<bool(bool)>& activeServicesCallback) {
+    mClientCallback->setActiveServicesCallback(activeServicesCallback);
 }
 
 }  // namespace details
@@ -280,9 +287,9 @@
     mImpl->reRegister();
 }
 
-void LazyServiceRegistrar::setActiveServicesCountCallback(
-        const std::function<bool(int)>& activeServicesCountCallback) {
-    mImpl->setActiveServicesCountCallback(activeServicesCountCallback);
+void LazyServiceRegistrar::setActiveServicesCallback(
+        const std::function<bool(bool)>& activeServicesCallback) {
+    mImpl->setActiveServicesCallback(activeServicesCallback);
 }
 
 }  // namespace hardware
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index 8122324..b51c600 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -183,70 +183,8 @@
     return *getTrebleTestingOverridePtr();
 }
 
-/*
- * 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.
- */
-__attribute__((noinline)) static long getProcessAgeMs() {
-    constexpr const int PROCFS_STAT_STARTTIME_INDEX = 21;
-    std::string content;
-    if (!android::base::ReadFileToString("/proc/self/stat", &content, false)) {
-        LOG(ERROR) << "Process age: Could not read /proc/self/stat";
-        return -1;
-    }
-
-    std::vector<std::string> stats = android::base::Split(content, " ");
-    if (PROCFS_STAT_STARTTIME_INDEX >= stats.size()) {
-        LOG(ERROR) << "Process age: Could not read starttime from /proc/self/stat";
-        return -1;
-    }
-
-    const std::string& startTimeString = stats.at(PROCFS_STAT_STARTTIME_INDEX);
-    unsigned long long startTimeInClockTicks = 0;
-    if (!android::base::ParseUint(startTimeString, &startTimeInClockTicks)) {
-        LOG(ERROR) << "Process age: Could not parse start time: " << startTimeString;
-        return -1;
-    }
-
-    const int64_t ticksPerSecond = sysconf(_SC_CLK_TCK);
-    if (ticksPerSecond <= 0) {
-        LOG(ERROR) << "Process age: Invalid _SC_CLK_TCK: " << ticksPerSecond;
-        return -1;
-    }
-
-    const int64_t uptime = android::uptimeMillis();
-    if (uptime < 0) {
-        LOG(ERROR) << "Process age: Invalid uptime: " << uptime;
-        return -1;
-    }
-
-    unsigned long long startTimeTicks;
-    if (__builtin_umulll_overflow(1000ULL, startTimeInClockTicks, &startTimeTicks)) {
-        LOG(ERROR) << "Process age: Too many ticks, overflow: " << startTimeInClockTicks;
-        return -1;
-    }
-
-    long startTimeMs = startTimeTicks / ticksPerSecond;
-    if (startTimeMs >= uptime) {
-        LOG(ERROR) << "Process age: process started in future: " << startTimeMs << " after "
-                   << uptime;
-        return -1;
-    }
-
-    return uptime - startTimeMs;
-}
-
 static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {
-    long halStartDelay = getProcessAgeMs();
-    if (halStartDelay >= 0) {
-        // The "start delay" printed here is an estimate of how long it took the HAL to go from
-        // process creation to registering itself as a HAL.  Actual start time could be longer
-        // because the process might not have joined the threadpool yet, so it might not be ready to
-        // process transactions.
-        LOG(INFO) << "Registered " << descriptor << "/" << instanceName << " (start delay of "
-                  << halStartDelay << "ms)";
-    }
-
+    LOG(INFO) << "Registered " << descriptor << "/" << instanceName;
     tryShortenProcessName(descriptor);
 }
 
@@ -901,7 +839,13 @@
 
     if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
         using Transport = IServiceManager1_0::Transport;
-        Transport transport = sm->getTransport(descriptor, name);
+        Return<Transport> transport = sm->getTransport(descriptor, name);
+
+        if (!transport.isOk()) {
+            LOG(ERROR) << "Could not get transport for " << descriptor << "/" << name << ": "
+                       << transport.description();
+            return UNKNOWN_ERROR;
+        }
 
         if (transport != Transport::HWBINDER) {
             LOG(ERROR) << "Service " << descriptor << "/" << name
diff --git a/transport/include/hidl/HidlLazyUtils.h b/transport/include/hidl/HidlLazyUtils.h
index 44fbcb2..427611b 100644
--- a/transport/include/hidl/HidlLazyUtils.h
+++ b/transport/include/hidl/HidlLazyUtils.h
@@ -44,10 +44,10 @@
      status_t registerService(const sp<::android::hidl::base::V1_0::IBase>& service,
                               const std::string& name = "default");
      /**
-      * Set a callback that is executed when the total number of services with
-      * clients changes.
-      * The callback takes an argument, which is the number of registered
-      * lazy HALs for this process which have clients.
+      * Set a callback that is invoked when the active HAL count (i.e. HALs with clients)
+      * registered with this process drops to zero (or becomes nonzero).
+      * The callback takes a boolean argument, which is 'true' if there is
+      * at least one HAL with clients.
       *
       * Callback return value:
       * - false: Default behavior for lazy HALs (shut down the process if there
@@ -61,8 +61,7 @@
       *
       * This method should be called before 'registerService' to avoid races.
       */
-     void setActiveServicesCountCallback(
-             const std::function<bool(int)>& activeServicesCountCallback);
+     void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
 
      /**
       * Try to unregister all services previously registered with 'registerService'.