1 /* 2 * Copyright (C) 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 package android.system.virtualmachine; 18 19 import android.annotation.NonNull; 20 import android.os.IBinder; 21 import android.os.ParcelFileDescriptor; 22 import android.system.virtualizationservice.IVirtualizationService; 23 24 import com.android.internal.annotations.GuardedBy; 25 26 import java.lang.ref.WeakReference; 27 28 /** A running instance of virtmgr that is hosting a VirtualizationService AIDL service. */ 29 class VirtualizationService { 30 static { 31 System.loadLibrary("virtualizationservice_jni"); 32 } 33 34 /* Soft reference caching the last created instance of this class. */ 35 @GuardedBy("VirtualMachineManager.sCreateLock") 36 private static WeakReference<VirtualizationService> sInstance; 37 38 /* 39 * Client FD for UDS connection to virtmgr's RpcBinder server. Closing it 40 * will make virtmgr shut down. 41 */ 42 private final ParcelFileDescriptor mClientFd; 43 44 /* Persistent connection to IVirtualizationService. */ 45 private final IVirtualizationService mBinder; 46 nativeSpawn()47 private static native int nativeSpawn(); 48 nativeConnect(int clientFd)49 private native IBinder nativeConnect(int clientFd); 50 nativeIsOk(int clientFd)51 private native boolean nativeIsOk(int clientFd); 52 53 /* 54 * Retrieve boolean value whether RELEASE_AVF_ENABLE_VENDOR_MODULES build flag is enabled or 55 * not. 56 */ nativeIsVendorModulesFlagEnabled()57 static native boolean nativeIsVendorModulesFlagEnabled(); 58 59 /* 60 * Spawns a new virtmgr subprocess that will host a VirtualizationService 61 * AIDL service. 62 */ VirtualizationService()63 private VirtualizationService() throws VirtualMachineException { 64 int clientFd = nativeSpawn(); 65 if (clientFd < 0) { 66 throw new VirtualMachineException("Could not spawn Virtualization Manager"); 67 } 68 mClientFd = ParcelFileDescriptor.adoptFd(clientFd); 69 70 IBinder binder = nativeConnect(mClientFd.getFd()); 71 if (binder == null) { 72 throw new VirtualMachineException("Could not connect to Virtualization Manager"); 73 } 74 mBinder = IVirtualizationService.Stub.asInterface(binder); 75 } 76 77 /* Returns the IVirtualizationService binder. */ 78 @NonNull getBinder()79 IVirtualizationService getBinder() { 80 return mBinder; 81 } 82 83 /* 84 * Checks the state of the client FD. Returns false if the FD is in erroneous state 85 * or if the other endpoint had closed its FD. 86 */ isOk()87 private boolean isOk() { 88 return nativeIsOk(mClientFd.getFd()); 89 } 90 91 /* 92 * Returns an instance of this class. Might spawn a new instance if one doesn't exist, or 93 * if the previous instance had crashed. 94 */ 95 @GuardedBy("VirtualMachineManager.sCreateLock") 96 @NonNull getInstance()97 static VirtualizationService getInstance() throws VirtualMachineException { 98 VirtualizationService service = (sInstance == null) ? null : sInstance.get(); 99 if (service == null || !service.isOk()) { 100 service = new VirtualizationService(); 101 sInstance = new WeakReference<>(service); 102 } 103 return service; 104 } 105 } 106