Fix keystore_backend_binder

Since the keystore AIDL interface became asynchronous we need a thread to handle the
callbacks.

With this patch keystore_backend_binder starts a thread pool when a
backend is created.

Also change keystore_cli_v2 to use startThreadPool instead of starting a
binder thread explicitely.

Bug: 111443219
Change-Id: Ic5b19d95f51a24d823825d5874ec85eeabd9ef5f
diff --git a/keystore-engine/keystore_backend_binder.cpp b/keystore-engine/keystore_backend_binder.cpp
index 0e38b50..f6ae7f5 100644
--- a/keystore-engine/keystore_backend_binder.cpp
+++ b/keystore-engine/keystore_backend_binder.cpp
@@ -25,6 +25,7 @@
 #include <android-base/logging.h>
 #include <android/security/keystore/IKeystoreService.h>
 #include <binder/IServiceManager.h>
+#include <binder/ProcessState.h>
 #include <keystore/KeyCharacteristics.h>
 #include <keystore/KeymasterArguments.h>
 #include <keystore/KeymasterBlob.h>
@@ -37,6 +38,7 @@
 #include <keystore/keystore_return_types.h>
 
 #include <future>
+#include <thread>
 
 using android::security::keystore::IKeystoreService;
 using namespace android;
@@ -87,6 +89,10 @@
     return {};
 }
 
+KeystoreBackendBinder::KeystoreBackendBinder() {
+    android::ProcessState::self()->startThreadPool();
+}
+
 int32_t KeystoreBackendBinder::sign(const char* key_id, const uint8_t* in, size_t len,
                                     uint8_t** reply, size_t* reply_len) {
     sp<IServiceManager> sm = defaultServiceManager();
diff --git a/keystore-engine/keystore_backend_binder.h b/keystore-engine/keystore_backend_binder.h
index 1db90f7..4c828c5 100644
--- a/keystore-engine/keystore_backend_binder.h
+++ b/keystore-engine/keystore_backend_binder.h
@@ -27,7 +27,7 @@
 
 class KeystoreBackendBinder : public KeystoreBackend {
   public:
-    KeystoreBackendBinder() {}
+    KeystoreBackendBinder();
     virtual ~KeystoreBackendBinder() {}
     int32_t sign(const char *key_id, const uint8_t* in, size_t len,
                  uint8_t** reply, size_t* reply_len) override;
diff --git a/keystore/keystore_cli_v2.cpp b/keystore/keystore_cli_v2.cpp
index a14800b..777db33 100644
--- a/keystore/keystore_cli_v2.cpp
+++ b/keystore/keystore_cli_v2.cpp
@@ -630,8 +630,7 @@
     CommandLine* command_line = CommandLine::ForCurrentProcess();
     CommandLine::StringVector args = command_line->GetArgs();
 
-    std::thread thread_pool([] { android::IPCThreadState::self()->joinThreadPool(false); });
-    thread_pool.detach();
+    android::ProcessState::self()->startThreadPool();
 
     if (args.empty()) {
         PrintUsageAndExit();