1 /*
2 * Copyright 2022 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 "VirtualizationService"
18
19 #include <android-base/unique_fd.h>
20 #include <android/avf_cc_flags.h>
21 #include <android/binder_ibinder_jni.h>
22 #include <errno.h>
23 #include <jni.h>
24 #include <log/log.h>
25 #include <poll.h>
26
27 #include <string>
28
29 #include "common.h"
30
31 using namespace android::base;
32
33 static constexpr size_t VIRTMGR_THREADS = 2;
34
error_callback(int code,const char * msg,void * ctx)35 void error_callback(int code, const char* msg, void* ctx) {
36 JNIEnv* env = reinterpret_cast<JNIEnv*>(ctx);
37 if (code == EPERM || code == EACCES) {
38 env->ThrowNew(env->FindClass("java/lang/SecurityException"),
39 "Virtmgr didn't send any data through pipe. Please consider checking if "
40 "android.permission.MANAGE_VIRTUAL_MACHINE permission is granted");
41 return;
42 }
43 env->ThrowNew(env->FindClass("android/system/virtualmachine/VirtualMachineException"), msg);
44 }
45
46 extern "C" int get_virtualization_service(decltype(error_callback)*, void*);
47
48 extern "C" JNIEXPORT jint JNICALL
Java_android_system_virtualmachine_VirtualizationService_nativeSpawn(JNIEnv * env,jclass clazz)49 Java_android_system_virtualmachine_VirtualizationService_nativeSpawn(
50 JNIEnv* env, [[maybe_unused]] jclass clazz) {
51 return get_virtualization_service(error_callback, env);
52 }
53
54 extern "C" JNIEXPORT jobject JNICALL
Java_android_system_virtualmachine_VirtualizationService_nativeConnect(JNIEnv * env,jobject obj,int clientFd)55 Java_android_system_virtualmachine_VirtualizationService_nativeConnect(JNIEnv* env,
56 [[maybe_unused]] jobject obj,
57 int clientFd) {
58 RpcSessionHandle session;
59 ARpcSession_setFileDescriptorTransportMode(session.get(),
60 ARpcSession_FileDescriptorTransportMode::Unix);
61 ARpcSession_setMaxIncomingThreads(session.get(), VIRTMGR_THREADS);
62 // SAFETY - ARpcSession_setupUnixDomainBootstrapClient does not take ownership of clientFd.
63 auto client = ARpcSession_setupUnixDomainBootstrapClient(session.get(), clientFd);
64 return AIBinder_toJavaBinder(env, client);
65 }
66
67 extern "C" JNIEXPORT jboolean JNICALL
Java_android_system_virtualmachine_VirtualizationService_nativeIsOk(JNIEnv * env,jobject obj,int clientFd)68 Java_android_system_virtualmachine_VirtualizationService_nativeIsOk(JNIEnv* env,
69 [[maybe_unused]] jobject obj,
70 int clientFd) {
71 /* Setting events=0 only returns POLLERR, POLLHUP or POLLNVAL. */
72 struct pollfd pfds[] = {{.fd = clientFd, .events = 0}};
73 if (poll(pfds, /*nfds*/ 1, /*timeout*/ 0) < 0) {
74 env->ThrowNew(env->FindClass("android/system/virtualmachine/VirtualMachineException"),
75 ("Failed to poll client FD: " + std::string(strerror(errno))).c_str());
76 return false;
77 }
78 return pfds[0].revents == 0;
79 }
80