From d5b9ae137ebd9e6c9d8ab8384bd80d4bdb373452 Mon Sep 17 00:00:00 2001 From: Hui Xu Date: Wed, 9 Aug 2023 15:47:24 +0800 Subject: [PATCH] ipc/android: Dup the fd from JVM and maintain it in native - dup the fd in native part to avoid fd_san error --- .../main/java/org/freedesktop/monado/ipc/MonadoImpl.java | 6 +++++- src/xrt/targets/service-lib/service_target.cpp | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/MonadoImpl.java b/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/MonadoImpl.java index aa5e2e9c8..7852d067c 100644 --- a/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/MonadoImpl.java +++ b/src/xrt/ipc/android/src/main/java/org/freedesktop/monado/ipc/MonadoImpl.java @@ -64,7 +64,11 @@ public class MonadoImpl extends IMonado.Stub { throw new IllegalStateException("server not available"); } else { Log.i(TAG, "connect: fd ownership transferred"); - parcelFileDescriptor.detachFd(); + try { + parcelFileDescriptor.close(); + } catch (IOException e) { + Log.e(TAG, "connect: close FileDescriptor fail!"); + } } } diff --git a/src/xrt/targets/service-lib/service_target.cpp b/src/xrt/targets/service-lib/service_target.cpp index 1a27d6818..9b2cd5209 100644 --- a/src/xrt/targets/service-lib/service_target.cpp +++ b/src/xrt/targets/service-lib/service_target.cpp @@ -157,8 +157,11 @@ Java_org_freedesktop_monado_ipc_MonadoImpl_nativeAddClient(JNIEnv *env, jobject jni::Object monadoImpl(thiz); U_LOG_D("service: Called nativeAddClient with fd %d", fd); + int native_fd = dup(fd); + U_LOG_D("service: transfer ownership to native and native_fd %d", native_fd); + // We try pushing the fd number to the server. If and only if we get a 0 return, has the server taken ownership. - return IpcServerHelper::instance().addClient(fd); + return IpcServerHelper::instance().addClient(native_fd); } extern "C" JNIEXPORT void JNICALL