Start NetdNativeService before CommandListener.

Starting CommandListener tells NetworkManagementService that netd
is ready to accept commands. Ensure that the binder service is
started (i.e., we have called startThreadPool) before we do this.

Bug: 27239233
Change-Id: Ica89e72f51eb4752cb5cea2e4096c6a9dc5776d7
diff --git a/server/NetdNativeService.cpp b/server/NetdNativeService.cpp
index 46f4315..678d19a 100644
--- a/server/NetdNativeService.cpp
+++ b/server/NetdNativeService.cpp
@@ -67,6 +67,18 @@
 }  // namespace
 
 
+status_t NetdNativeService::start() {
+    IPCThreadState::self()->disableBackgroundScheduling(true);
+    status_t ret = BinderService<NetdNativeService>::publish();
+    if (ret != android::OK) {
+        return ret;
+    }
+    sp<ProcessState> ps(ProcessState::self());
+    ps->startThreadPool();
+    ps->giveThreadPoolName();
+    return android::OK;
+}
+
 binder::Status NetdNativeService::isAlive(bool *alive) {
     NETD_BIG_LOCK_RPC(CONNECTIVITY_INTERNAL);
 
diff --git a/server/NetdNativeService.h b/server/NetdNativeService.h
index 872e833..f7c6013 100644
--- a/server/NetdNativeService.h
+++ b/server/NetdNativeService.h
@@ -28,6 +28,7 @@
 
 class NetdNativeService : public BinderService<NetdNativeService>, public BnNetd {
   public:
+    static status_t start();
     static char const* getServiceName() { return "netd"; }
     binder::Status isAlive(bool *alive) override;
     binder::Status firewallReplaceUidChain(
diff --git a/server/main.cpp b/server/main.cpp
index cf3d5ec..4a5f315 100644
--- a/server/main.cpp
+++ b/server/main.cpp
@@ -105,8 +105,15 @@
         exit(1);
     }
 
+    status_t ret;
+    if ((ret = NetdNativeService::start()) != android::OK) {
+        ALOGE("Unable to start NetdNativeService: %d", ret);
+        exit(1);
+    }
+
     /*
-     * Now that we're up, we can respond to commands
+     * Now that we're up, we can respond to commands. Starting the listener also tells
+     * NetworkManagementService that we are up and that our binder interface is ready.
      */
     if (cl.startListener()) {
         ALOGE("Unable to start CommandListener (%s)", strerror(errno));
@@ -115,8 +122,7 @@
 
     write_pid_file();
 
-    IPCThreadState::self()->disableBackgroundScheduling(true);
-    NetdNativeService::publishAndJoinThreadPool();
+    IPCThreadState::self()->joinThreadPool();
 
     ALOGI("Netd exiting");