Switch hwservicemanager to binder ioctl interface.

The poll interface is a bit slower and has warts, and this
code is a lot cleaner.

Test: sailfish boots, hwservicemanager in binder_thread_read()
Change-Id: I7bef3d2cf27c57ac42a6db5dfcf0d38d5e8c6453
diff --git a/service.cpp b/service.cpp
index 56fcb0d..776adff 100644
--- a/service.cpp
+++ b/service.cpp
@@ -11,6 +11,7 @@
 #include <cutils/properties.h>
 #include <hidl/Status.h>
 #include <hwbinder/IPCThreadState.h>
+#include <hwbinder/ProcessState.h>
 #include <utils/Errors.h>
 #include <utils/Looper.h>
 #include <utils/StrongPointer.h>
@@ -19,20 +20,18 @@
 #include "TokenManager.h"
 
 // libutils:
-using android::BAD_TYPE;
-using android::Looper;
-using android::LooperCallback;
-using android::OK;
 using android::sp;
 using android::status_t;
 
 // libhwbinder:
 using android::hardware::IPCThreadState;
+using android::hardware::ProcessState;
 
 // libhidl
 using android::hardware::configureRpcThreadpool;
 using android::hardware::hidl_string;
 using android::hardware::hidl_vec;
+using android::hardware::joinRpcThreadpool;
 
 // hidl types
 using android::hidl::manager::V1_0::BnHwServiceManager;
@@ -45,17 +44,6 @@
 
 static std::string serviceName = "default";
 
-class BinderCallback : public LooperCallback {
-public:
-    BinderCallback() {}
-    ~BinderCallback() override {}
-
-    int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
-        IPCThreadState::self()->handlePolledCommands();
-        return 1;  // Continue receiving callbacks.
-    }
-};
-
 int main() {
     configureRpcThreadpool(1, true /* callerWillJoin */);
 
@@ -71,31 +59,11 @@
         ALOGE("Failed to register ITokenManager with hwservicemanager.");
     }
 
-    sp<Looper> looper(Looper::prepare(0 /* opts */));
-
-    int binder_fd = -1;
-
-    IPCThreadState::self()->setupPolling(&binder_fd);
-    if (binder_fd < 0) {
-        ALOGE("Failed to aquire binder FD. Aborting...");
-        return -1;
-    }
-    // Flush after setupPolling(), to make sure the binder driver
-    // knows about this thread handling commands.
-    IPCThreadState::self()->flushCommands();
-
-    sp<BinderCallback> cb(new BinderCallback);
-    if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
-            nullptr) != 1) {
-        ALOGE("Failed to add hwbinder FD to Looper. Aborting...");
-        return -1;
-    }
-
     // Tell IPCThreadState we're the service manager
     sp<BnHwServiceManager> service = new BnHwServiceManager(manager);
     IPCThreadState::self()->setTheContextObject(service);
-    // Then tell binder kernel
-    ioctl(binder_fd, BINDER_SET_CONTEXT_MGR, 0);
+    // Then tell the kernel
+    ProcessState::self()->becomeContextManager(nullptr, nullptr);
 
     int rc = property_set("hwservicemanager.ready", "true");
     if (rc) {
@@ -103,9 +71,7 @@
               "HAL services will not start!\n", rc);
     }
 
-    while (true) {
-        looper->pollAll(-1 /* timeoutMillis */);
-    }
+    joinRpcThreadpool();
 
     return 0;
 }