forwarder_host terminates when terminating VM
Bug: 340126051
Test: Check port forwarding with starting & stopping VM multiple times
with killing & restarting terminal app.
Change-Id: If4a6ce8c6d61ad97302f2a4c174af59026564a53
diff --git a/android/forwarder_host/src/forwarder_host.rs b/android/forwarder_host/src/forwarder_host.rs
index 78f3555..26bae89 100644
--- a/android/forwarder_host/src/forwarder_host.rs
+++ b/android/forwarder_host/src/forwarder_host.rs
@@ -24,7 +24,7 @@
use std::net::{Ipv4Addr, Ipv6Addr, TcpListener};
use std::os::unix::io::AsRawFd;
use std::result;
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, LazyLock, Mutex};
use std::time::Duration;
use forwarder::forwarder::ForwarderSession;
@@ -42,6 +42,9 @@
const VMADDR_PORT_ANY: u32 = u32::MAX;
+static SHUTDOWN_EVT: LazyLock<EventFd> =
+ LazyLock::new(|| EventFd::new().expect("Could not create shutdown eventfd"));
+
#[remain::sorted]
#[derive(Debug)]
enum Error {
@@ -111,6 +114,7 @@
/// Implements PollToken for chunneld's main poll loop.
#[derive(Clone, Copy, PollToken)]
enum Token {
+ Shutdown,
UpdatePorts,
Ipv4Listener(u16),
Ipv6Listener(u16),
@@ -290,12 +294,16 @@
fn run(&mut self) -> Result<()> {
let poll_ctx: PollContext<Token> = PollContext::new().map_err(Error::PollContextNew)?;
poll_ctx.add(&self.update_evt, Token::UpdatePorts).map_err(Error::PollContextAdd)?;
+ poll_ctx.add(&*SHUTDOWN_EVT, Token::Shutdown).map_err(Error::PollContextAdd)?;
loop {
let events = poll_ctx.wait().map_err(Error::PollWait)?;
for event in events.iter_readable() {
match event.token() {
+ Token::Shutdown => {
+ return Ok(());
+ }
Token::UpdatePorts => {
if let Err(e) = self.process_update_queue(&poll_ctx) {
error!("error updating listening ports: {}", e);
@@ -425,3 +433,12 @@
}
}
}
+
+/// JNI function for terminating forwarder_host.
+#[no_mangle]
+pub extern "C" fn Java_com_android_virtualization_vmlauncher_DebianServiceImpl_terminateForwarderHost(
+ _env: JNIEnv,
+ _class: JObject,
+) {
+ SHUTDOWN_EVT.write(1).expect("Failed to write shutdown event FD");
+}
diff --git a/libs/vm_launcher_lib/java/com/android/virtualization/vmlauncher/DebianServiceImpl.java b/libs/vm_launcher_lib/java/com/android/virtualization/vmlauncher/DebianServiceImpl.java
index dcc8152..61679f2 100644
--- a/libs/vm_launcher_lib/java/com/android/virtualization/vmlauncher/DebianServiceImpl.java
+++ b/libs/vm_launcher_lib/java/com/android/virtualization/vmlauncher/DebianServiceImpl.java
@@ -28,7 +28,7 @@
import io.grpc.stub.StreamObserver;
-class DebianServiceImpl extends DebianServiceGrpc.DebianServiceImplBase {
+final class DebianServiceImpl extends DebianServiceGrpc.DebianServiceImplBase {
public static final String TAG = "DebianService";
private final DebianServiceCallback mCallback;
@@ -79,6 +79,8 @@
private static native void runForwarderHost(int cid, ForwarderHostCallback callback);
+ public static native void terminateForwarderHost();
+
protected interface DebianServiceCallback {
void onIpAddressAvailable(String ipAddr);
}
diff --git a/libs/vm_launcher_lib/java/com/android/virtualization/vmlauncher/VmLauncherService.java b/libs/vm_launcher_lib/java/com/android/virtualization/vmlauncher/VmLauncherService.java
index b9ad4fb..2bd85e1 100644
--- a/libs/vm_launcher_lib/java/com/android/virtualization/vmlauncher/VmLauncherService.java
+++ b/libs/vm_launcher_lib/java/com/android/virtualization/vmlauncher/VmLauncherService.java
@@ -200,6 +200,7 @@
private void stopDebianServer() {
if (mServer != null) {
+ DebianServiceImpl.terminateForwarderHost();
mServer.shutdown();
}
}