Add the ArtD binder interface

Test: None
Bug: 177273468
Change-Id: I7dab21636e41744dd29b43fb61aac390a94f5977
diff --git a/artd/Android.bp b/artd/Android.bp
index 56ec317..6338fa5 100644
--- a/artd/Android.bp
+++ b/artd/Android.bp
@@ -12,7 +12,6 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
-//
 
 package {
     // See: http://go/android-license-faq
@@ -27,24 +26,19 @@
     name: "artd",
     defaults: ["art_defaults"],
     min_sdk_version: "S",
-    host_supported: true,
 
     srcs: [
         "artd.cc",
     ],
 
     shared_libs: [
+        "artd-aidl-ndk",
         "libartbase",
         "libarttools",
         "libbase",
+        "libbinder_ndk",
     ],
 
-    target: {
-        host: {
-            stl: "c++_static",
-        },
-    },
-
     apex_available: [
         "com.android.art",
         "com.android.art.debug",
diff --git a/artd/artd.cc b/artd/artd.cc
index 614a2f1..fe0675b 100644
--- a/artd/artd.cc
+++ b/artd/artd.cc
@@ -14,40 +14,64 @@
 ** limitations under the License.
 */
 
+#include <string>
 #define LOG_TAG "artd"
 
+#include <android/binder_manager.h>
+#include <android/binder_process.h>
 #include <unistd.h>
+#include <utils/Errors.h>
 
+#include "aidl/android/os/BnArtd.h"
 #include "base/logging.h"
 #include "base/macros.h"
 #include "tools/tools.h"
 
-namespace {
+using ::ndk::ScopedAStatus;
 
-class Artd {
+namespace android {
+namespace artd {
+
+class ArtD : public aidl::android::os::BnArtd {
+  constexpr static const char* const SERVICE_NAME = "artd";
+
  public:
-  Artd(ATTRIBUTE_UNUSED const int argc, ATTRIBUTE_UNUSED char* argv[]) {}
 
-  NO_RETURN
-  void Run() {
-    LOG(DEBUG) << "Starting artd";
+  /*
+   * Server API
+   */
 
-    while (true) {
-      // This is a scaffolding CL.  This sleep is intended to keep the process
-      // alive for testing without it using too many system resources.  This
-      // will be removed and replaced with a server loop in a followup CL.
-      sleep(5);
+  ScopedAStatus Start() {
+    LOG(INFO) << "Starting artd";
+
+    status_t ret = AServiceManager_addService(this->asBinder().get(), SERVICE_NAME);
+    if (ret != android::OK) {
+      return ScopedAStatus::fromStatus(ret);
     }
+
+    ABinderProcess_startThreadPool();
+
+    return ScopedAStatus::ok();
   }
 };
 
-}  // namespace
+}  // namespace artd
+}  // namespace android
 
-int main(const int argc, char* argv[]) {
+int main(const int argc __attribute__((unused)), char* argv[]) {
   setenv("ANDROID_LOG_TAGS", "*:v", 1);
   android::base::InitLogging(argv);
 
-  Artd artd(argc, argv);
+  android::artd::ArtD artd;
 
-  artd.Run();
+  if (auto ret = artd.Start(); !ret.isOk()) {
+    LOG(ERROR) << "Unable to start artd: " << ret.getMessage();
+    exit(1);
+  }
+
+  ABinderProcess_joinThreadPool();
+
+  LOG(INFO) << "artd shutting down";
+
+  return 0;
 }
diff --git a/artd/binder/Android.bp b/artd/binder/Android.bp
new file mode 100644
index 0000000..2e69c78
--- /dev/null
+++ b/artd/binder/Android.bp
@@ -0,0 +1,52 @@
+//
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "art_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["art_license"],
+}
+
+aidl_interface {
+    name: "artd-aidl",
+    srcs: [
+        "android/os/IArtd.aidl",
+    ],
+    host_supported: true,
+    backend: {
+        java: {
+            enabled: false,
+        },
+        cpp: {
+            enabled: false,
+        },
+        ndk: {
+            enabled: true,
+            apex_available: [
+                "com.android.art",
+                "com.android.art.debug",
+            ],
+            // TODO(b/177273468): Increment to next version when possible
+            min_sdk_version: "S",
+        },
+    },
+    unstable: true,
+    visibility: [
+        "//system/tools/aidl/build",
+    ],
+}
diff --git a/artd/binder/android/os/IArtd.aidl b/artd/binder/android/os/IArtd.aidl
new file mode 100644
index 0000000..4f5f214
--- /dev/null
+++ b/artd/binder/android/os/IArtd.aidl
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.os;
+
+/** {@hide} */
+interface IArtd {
+}
diff --git a/build/apex/art_apex_test.py b/build/apex/art_apex_test.py
index 2f5e20d..eb83391 100755
--- a/build/apex/art_apex_test.py
+++ b/build/apex/art_apex_test.py
@@ -468,6 +468,7 @@
     self._checker.check_native_library('libnativeloader')
 
     # Check internal libraries for ART.
+    self._checker.check_prefer64_library('artd-aidl-ndk')
     self._checker.check_native_library('libadbconnection')
     self._checker.check_native_library('libart')
     self._checker.check_native_library('libart-compiler')