Split CompOS interface in two

One service for system server (which will become stable), one for
internal calls from odrefresh (which will probably go away soon).

Test: compos_cmd forced-compile-test
Bug: 199147668
Change-Id: Ia7024e18fe1708eafb673a68b944c3f011715f11
diff --git a/compos/Android.bp b/compos/Android.bp
index b1e5f89..b9fcfff 100644
--- a/compos/Android.bp
+++ b/compos/Android.bp
@@ -6,7 +6,7 @@
     name: "pvm_exec",
     srcs: ["src/pvm_exec.rs"],
     rustlibs: [
-        "android.system.composd-rust",
+        "android.system.composd.internal-rust",
         "compos_aidl_interface-rust",
         "libandroid_logger",
         "libanyhow",
diff --git a/compos/apex/composd.rc b/compos/apex/composd.rc
index 3e2efb1..bf34335 100644
--- a/compos/apex/composd.rc
+++ b/compos/apex/composd.rc
@@ -17,5 +17,6 @@
     user root
     group system
     interface aidl android.system.composd
+    interface aidl android.system.composd.internal
     disabled
     oneshot
diff --git a/compos/composd/Android.bp b/compos/composd/Android.bp
index 8391ed6..a76d96c 100644
--- a/compos/composd/Android.bp
+++ b/compos/composd/Android.bp
@@ -9,6 +9,7 @@
     prefer_rlib: true,
     rustlibs: [
         "android.system.composd-rust",
+        "android.system.composd.internal-rust",
         "android.system.virtualizationservice-rust",
         "compos_aidl_interface-rust",
         "libandroid_logger",
diff --git a/compos/composd/aidl/Android.bp b/compos/composd/aidl/Android.bp
index 8116632..62c1b40 100644
--- a/compos/composd/aidl/Android.bp
+++ b/compos/composd/aidl/Android.bp
@@ -5,7 +5,6 @@
 aidl_interface {
     name: "android.system.composd",
     srcs: ["android/system/composd/*.aidl"],
-    imports: ["compos_aidl_interface"],
     // TODO: Make this stable when the APEX becomes updatable.
     unstable: true,
     backend: {
@@ -18,7 +17,17 @@
                 "com.android.compos",
             ],
         },
-        ndk: {
+    },
+}
+
+aidl_interface {
+    name: "android.system.composd.internal",
+    srcs: ["android/system/composd/internal/*.aidl"],
+    imports: ["compos_aidl_interface"],
+    // TODO: Make this stable when the APEX becomes updatable.
+    unstable: true,
+    backend: {
+        rust: {
             enabled: true,
             apex_available: [
                 "com.android.compos",
diff --git a/compos/composd/aidl/android/system/composd/IIsolatedCompilationService.aidl b/compos/composd/aidl/android/system/composd/IIsolatedCompilationService.aidl
index 3d28894..6b3a6bb 100644
--- a/compos/composd/aidl/android/system/composd/IIsolatedCompilationService.aidl
+++ b/compos/composd/aidl/android/system/composd/IIsolatedCompilationService.aidl
@@ -17,8 +17,6 @@
 
 import android.system.composd.ICompilationTask;
 import android.system.composd.ICompilationTaskCallback;
-import com.android.compos.CompilationResult;
-import com.android.compos.FdAnnotation;
 
 interface IIsolatedCompilationService {
     /**
@@ -31,22 +29,4 @@
      * a reference to the ICompilationTask until compilation completes or is cancelled.
      */
     ICompilationTask startTestCompile(ICompilationTaskCallback callback);
-
-    /**
-     * Run dex2oat in the currently running instance of the CompOS VM. This is a simple proxy
-     * to ICompOsService#compile_cmd.
-     *
-     * This method can only be called from odrefresh. If there is no currently running instance
-     * an error is returned.
-     */
-    CompilationResult compile_cmd(in String[] args, in FdAnnotation fd_annotation);
-
-    /**
-     * Run dex2oat in the currently running instance of the CompOS VM. This is a simple proxy
-     * to ICompOsService#compile.
-     *
-     * This method can only be called from libcompos_client. If there is no currently running
-     * instance an error is returned.
-     */
-    byte compile(in byte[] marshaledArguments, in FdAnnotation fd_annotation);
 }
diff --git a/compos/composd/aidl/android/system/composd/internal/ICompilationInternal.aidl b/compos/composd/aidl/android/system/composd/internal/ICompilationInternal.aidl
new file mode 100644
index 0000000..182094b
--- /dev/null
+++ b/compos/composd/aidl/android/system/composd/internal/ICompilationInternal.aidl
@@ -0,0 +1,39 @@
+/*
+ * Copyright 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.system.composd.internal;
+
+import com.android.compos.CompilationResult;
+import com.android.compos.FdAnnotation;
+
+interface ICompilationInternal {
+    /**
+     * Run dex2oat in the currently running instance of the CompOS VM. This is a simple proxy
+     * to ICompOsService#compile_cmd.
+     *
+     * This method can only be called from odrefresh. If there is no currently running instance
+     * an error is returned.
+     */
+    CompilationResult compile_cmd(in String[] args, in FdAnnotation fd_annotation);
+
+    /**
+     * Run dex2oat in the currently running instance of the CompOS VM. This is a simple proxy
+     * to ICompOsService#compile.
+     *
+     * This method can only be called from libcompos_client. If there is no currently running
+     * instance an error is returned.
+     */
+    byte compile(in byte[] marshaledArguments, in FdAnnotation fd_annotation);
+}
diff --git a/compos/composd/src/composd_main.rs b/compos/composd/src/composd_main.rs
index 671ed16..f9751c3 100644
--- a/compos/composd/src/composd_main.rs
+++ b/compos/composd/src/composd_main.rs
@@ -21,6 +21,7 @@
 mod compilation_task;
 mod instance_manager;
 mod instance_starter;
+mod internal_service;
 mod odrefresh;
 mod service;
 mod util;
@@ -30,6 +31,7 @@
 use anyhow::{Context, Result};
 use compos_common::compos_client::VmInstance;
 use log::{error, info};
+use std::sync::Arc;
 
 fn try_main() -> Result<()> {
     android_logger::init_once(
@@ -39,12 +41,16 @@
     ProcessState::start_thread_pool();
 
     let virtualization_service = VmInstance::connect_to_virtualization_service()?;
-    let instance_manager = InstanceManager::new(virtualization_service);
-    let composd_service = service::new_binder(instance_manager);
+    let instance_manager = Arc::new(InstanceManager::new(virtualization_service));
+    let composd_service = service::new_binder(instance_manager.clone());
     register_lazy_service("android.system.composd", composd_service.as_binder())
-        .context("Registering service")?;
+        .context("Registering composd service")?;
 
-    info!("Registered service, joining threadpool");
+    let internal_service = internal_service::new_binder(instance_manager);
+    register_lazy_service("android.system.composd.internal", internal_service.as_binder())
+        .context("Registering internal service")?;
+
+    info!("Registered services, joining threadpool");
     ProcessState::join_thread_pool();
 
     info!("Exiting");
diff --git a/compos/composd/src/internal_service.rs b/compos/composd/src/internal_service.rs
new file mode 100644
index 0000000..ebebaae
--- /dev/null
+++ b/compos/composd/src/internal_service.rs
@@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+
+//! Implementation of ICompilationInternal, called from odrefresh during compilation.
+
+use crate::instance_manager::InstanceManager;
+use crate::util::to_binder_result;
+use android_system_composd_internal::aidl::android::system::composd::internal::ICompilationInternal::{
+    BnCompilationInternal, ICompilationInternal,
+};
+use android_system_composd::binder::{
+    self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
+};
+use anyhow::{Context, Result};
+use binder_common::new_binder_service_specific_error;
+use compos_aidl_interface::aidl::com::android::compos::{
+    CompilationResult::CompilationResult, FdAnnotation::FdAnnotation,
+};
+use rustutils::users::AID_ROOT;
+use std::sync::Arc;
+
+pub struct CompilationInternalService {
+    instance_manager: Arc<InstanceManager>,
+}
+
+pub fn new_binder(instance_manager: Arc<InstanceManager>) -> Strong<dyn ICompilationInternal> {
+    let service = CompilationInternalService { instance_manager };
+    BnCompilationInternal::new_binder(service, BinderFeatures::default())
+}
+
+impl Interface for CompilationInternalService {}
+
+impl ICompilationInternal for CompilationInternalService {
+    fn compile_cmd(
+        &self,
+        args: &[String],
+        fd_annotation: &FdAnnotation,
+    ) -> binder::Result<CompilationResult> {
+        let calling_uid = ThreadState::get_calling_uid();
+        // This should only be called by odrefresh, which runs as root
+        if calling_uid != AID_ROOT {
+            return Err(Status::new_exception(ExceptionCode::SECURITY, None));
+        }
+        to_binder_result(self.do_compile_cmd(args, fd_annotation))
+    }
+
+    fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> binder::Result<i8> {
+        Err(new_binder_service_specific_error(-1, "Not yet implemented"))
+    }
+}
+
+impl CompilationInternalService {
+    fn do_compile_cmd(
+        &self,
+        args: &[String],
+        fd_annotation: &FdAnnotation,
+    ) -> Result<CompilationResult> {
+        let compos = self.instance_manager.get_running_service()?;
+        compos.compile_cmd(args, fd_annotation).context("Compiling")
+    }
+}
diff --git a/compos/composd/src/service.rs b/compos/composd/src/service.rs
index 4d9dc58..d9963d1 100644
--- a/compos/composd/src/service.rs
+++ b/compos/composd/src/service.rs
@@ -29,17 +29,16 @@
     self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
 };
 use anyhow::{Context, Result};
-use binder_common::new_binder_service_specific_error;
-use compos_aidl_interface::aidl::com::android::compos::{
-    CompilationResult::CompilationResult, FdAnnotation::FdAnnotation,
-};
 use rustutils::users::{AID_ROOT, AID_SYSTEM};
+use std::sync::Arc;
 
 pub struct IsolatedCompilationService {
-    instance_manager: InstanceManager,
+    instance_manager: Arc<InstanceManager>,
 }
 
-pub fn new_binder(instance_manager: InstanceManager) -> Strong<dyn IIsolatedCompilationService> {
+pub fn new_binder(
+    instance_manager: Arc<InstanceManager>,
+) -> Strong<dyn IIsolatedCompilationService> {
     let service = IsolatedCompilationService { instance_manager };
     BnIsolatedCompilationService::new_binder(service, BinderFeatures::default())
 }
@@ -58,23 +57,6 @@
         }
         to_binder_result(self.do_start_test_compile(callback))
     }
-
-    fn compile_cmd(
-        &self,
-        args: &[String],
-        fd_annotation: &FdAnnotation,
-    ) -> binder::Result<CompilationResult> {
-        let calling_uid = ThreadState::get_calling_uid();
-        // This should only be called by odrefresh, which runs as root
-        if calling_uid != AID_ROOT {
-            return Err(Status::new_exception(ExceptionCode::SECURITY, None));
-        }
-        to_binder_result(self.do_compile_cmd(args, fd_annotation))
-    }
-
-    fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> binder::Result<i8> {
-        Err(new_binder_service_specific_error(-1, "Not yet implemented"))
-    }
 }
 
 impl IsolatedCompilationService {
@@ -88,13 +70,4 @@
 
         Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))
     }
-
-    fn do_compile_cmd(
-        &self,
-        args: &[String],
-        fd_annotation: &FdAnnotation,
-    ) -> Result<CompilationResult> {
-        let compos = self.instance_manager.get_running_service()?;
-        compos.compile_cmd(args, fd_annotation).context("Compiling")
-    }
 }
diff --git a/compos/libcompos_client/Android.bp b/compos/libcompos_client/Android.bp
index 5528ea1..db6294e 100644
--- a/compos/libcompos_client/Android.bp
+++ b/compos/libcompos_client/Android.bp
@@ -31,7 +31,7 @@
     srcs: ["libcompos_client.rs"],
     include_dirs: ["include"],
     rustlibs: [
-        "android.system.composd-rust",
+        "android.system.composd.internal-rust",
         "compos_aidl_interface-rust",
         "libandroid_logger",
         "libanyhow",
diff --git a/compos/libcompos_client/libcompos_client.rs b/compos/libcompos_client/libcompos_client.rs
index 55d70a4..701e515 100644
--- a/compos/libcompos_client/libcompos_client.rs
+++ b/compos/libcompos_client/libcompos_client.rs
@@ -29,8 +29,8 @@
 use std::path::Path;
 use std::slice::from_raw_parts;
 
-use android_system_composd::{
-    aidl::android::system::composd::IIsolatedCompilationService::IIsolatedCompilationService,
+use android_system_composd_internal::{
+    aidl::android::system::composd::internal::ICompilationInternal::ICompilationInternal,
     binder::wait_for_interface,
 };
 use compos_aidl_interface::aidl::com::android::compos::{
@@ -41,9 +41,9 @@
 
 const FD_SERVER_BIN: &str = "/apex/com.android.virt/bin/fd_server";
 
-fn get_composd() -> Result<Strong<dyn IIsolatedCompilationService>> {
-    wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
-        .context("Failed to find IIsolatedCompilationService")
+fn get_composd() -> Result<Strong<dyn ICompilationInternal>> {
+    wait_for_interface::<dyn ICompilationInternal>("android.system.composd.internal")
+        .context("Failed to find ICompilationInternal")
 }
 
 fn spawn_fd_server(fd_annotation: &FdAnnotation, ready_file: File) -> Result<Minijail> {
diff --git a/compos/src/pvm_exec.rs b/compos/src/pvm_exec.rs
index cae0702..3068c66 100644
--- a/compos/src/pvm_exec.rs
+++ b/compos/src/pvm_exec.rs
@@ -40,8 +40,8 @@
 use std::path::Path;
 use std::process::exit;
 
-use android_system_composd::{
-    aidl::android::system::composd::IIsolatedCompilationService::IIsolatedCompilationService,
+use android_system_composd_internal::{
+    aidl::android::system::composd::internal::ICompilationInternal::ICompilationInternal,
     binder::wait_for_interface,
 };
 use compos_aidl_interface::aidl::com::android::compos::{
@@ -52,9 +52,9 @@
 
 const FD_SERVER_BIN: &str = "/apex/com.android.virt/bin/fd_server";
 
-fn get_composd() -> Result<Strong<dyn IIsolatedCompilationService>> {
-    wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
-        .context("Failed to find IIsolatedCompilationService")
+fn get_composd() -> Result<Strong<dyn ICompilationInternal>> {
+    wait_for_interface::<dyn ICompilationInternal>("android.system.composd.internal")
+        .context("Failed to find ICompilationInternal")
 }
 
 fn get_rpc_binder(cid: u32) -> Result<Strong<dyn ICompOsService>> {