1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "VirtualMachine"
18
19 #include <aidl/android/system/virtualizationservice/IVirtualMachine.h>
20 #include <android/binder_auto_utils.h>
21 #include <android/binder_ibinder_jni.h>
22 #include <jni.h>
23 #include <log/log.h>
24
25 #include <binder_rpc_unstable.hpp>
26 #include <tuple>
27
28 #include "common.h"
29
30 extern "C" JNIEXPORT jobject JNICALL
Java_android_system_virtualmachine_VirtualMachine_nativeConnectToVsockServer(JNIEnv * env,jclass clazz,jobject vmBinder,jint port)31 Java_android_system_virtualmachine_VirtualMachine_nativeConnectToVsockServer(
32 JNIEnv* env, [[maybe_unused]] jclass clazz, jobject vmBinder, jint port) {
33 using aidl::android::system::virtualizationservice::IVirtualMachine;
34 using ndk::ScopedFileDescriptor;
35 using ndk::SpAIBinder;
36
37 auto vm = IVirtualMachine::fromBinder(SpAIBinder{AIBinder_fromJavaBinder(env, vmBinder)});
38
39 std::tuple args{env, vm.get(), port};
40 using Args = decltype(args);
41
42 auto requestFunc = [](void* param) {
43 auto [env, vm, port] = *static_cast<Args*>(param);
44
45 ScopedFileDescriptor fd;
46 if (auto status = vm->connectVsock(port, &fd); !status.isOk()) {
47 env->ThrowNew(env->FindClass("android/system/virtualmachine/VirtualMachineException"),
48 ("Failed to connect vsock: " + status.getDescription()).c_str());
49 return -1;
50 }
51
52 // take ownership
53 int ret = fd.get();
54 *fd.getR() = -1;
55
56 return ret;
57 };
58
59 RpcSessionHandle session;
60 // We need a thread pool to be able to support linkToDeath, or callbacks
61 // (b/268335700). These threads are currently created eagerly, so we don't
62 // want too many. The number 1 is chosen after some discussion, and to match
63 // the server-side default (mMaxThreads on RpcServer).
64 ARpcSession_setMaxIncomingThreads(session.get(), 1);
65 auto client = ARpcSession_setupPreconnectedClient(session.get(), requestFunc, &args);
66 return AIBinder_toJavaBinder(env, client);
67 }
68