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 * Spawns a new virtmgr subprocess that will host a VirtualizationService 55 * AIDL service. 56 */ VirtualizationService()57 private VirtualizationService() throws VirtualMachineException { 58 int clientFd = nativeSpawn(); 59 if (clientFd < 0) { 60 throw new VirtualMachineException("Could not spawn VirtualizationService"); 61 } 62 mClientFd = ParcelFileDescriptor.adoptFd(clientFd); 63 64 IBinder binder = nativeConnect(mClientFd.getFd()); 65 if (binder == null) { 66 throw new VirtualMachineException("Could not connect to VirtualizationService"); 67 } 68 mBinder = IVirtualizationService.Stub.asInterface(binder); 69 } 70 71 /* Returns the IVirtualizationService binder. */ 72 @NonNull getBinder()73 IVirtualizationService getBinder() { 74 return mBinder; 75 } 76 77 /* 78 * Checks the state of the client FD. Returns false if the FD is in erroneous state 79 * or if the other endpoint had closed its FD. 80 */ isOk()81 private boolean isOk() { 82 return nativeIsOk(mClientFd.getFd()); 83 } 84 85 /* 86 * Returns an instance of this class. Might spawn a new instance if one doesn't exist, or 87 * if the previous instance had crashed. 88 */ 89 @GuardedBy("VirtualMachineManager.sCreateLock") 90 @NonNull getInstance()91 static VirtualizationService getInstance() throws VirtualMachineException { 92 VirtualizationService service = (sInstance == null) ? null : sInstance.get(); 93 if (service == null || !service.isOk()) { 94 service = new VirtualizationService(); 95 sInstance = new WeakReference<>(service); 96 } 97 return service; 98 } 99 } 100