Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 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 | |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 17 | //! Manages running instances of the CompOS VM. At most one instance should be running at |
| 18 | //! a time, started on demand. |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 19 | |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 20 | use crate::instance_starter::{CompOsInstance, InstanceStarter}; |
| 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 22 | use anyhow::{bail, Context, Result}; |
| 23 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 24 | use compos_aidl_interface::binder::Strong; |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame^] | 25 | use compos_common::{CURRENT_INSTANCE_DIR, TEST_INSTANCE_DIR}; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 26 | use std::sync::{Arc, Mutex, Weak}; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 27 | use virtualizationservice::IVirtualizationService::IVirtualizationService; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 28 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 29 | pub struct InstanceManager { |
| 30 | service: Strong<dyn IVirtualizationService>, |
| 31 | state: Mutex<State>, |
| 32 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 33 | |
| 34 | impl InstanceManager { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 35 | pub fn new(service: Strong<dyn IVirtualizationService>) -> Self { |
| 36 | Self { service, state: Default::default() } |
| 37 | } |
| 38 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 39 | pub fn get_running_service(&self) -> Result<Strong<dyn ICompOsService>> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 40 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 41 | let instance = state.get_running_instance().context("No running instance")?; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 42 | Ok(instance.get_service()) |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame^] | 45 | #[allow(dead_code)] // TODO: Make use of this |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 46 | pub fn start_current_instance(&self) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame^] | 47 | self.start_instance(CURRENT_INSTANCE_DIR) |
| 48 | } |
| 49 | |
| 50 | pub fn start_test_instance(&self) -> Result<Arc<CompOsInstance>> { |
| 51 | self.start_instance(TEST_INSTANCE_DIR) |
| 52 | } |
| 53 | |
| 54 | fn start_instance(&self, instance_name: &str) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 55 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 56 | state.mark_starting()?; |
| 57 | // Don't hold the lock while we start the instance to avoid blocking other callers. |
| 58 | drop(state); |
| 59 | |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame^] | 60 | let instance = self.try_start_instance(instance_name); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 61 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 62 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 63 | if let Ok(ref instance) = instance { |
| 64 | state.mark_started(instance)?; |
| 65 | } else { |
| 66 | state.mark_stopped(); |
| 67 | } |
| 68 | instance |
| 69 | } |
| 70 | |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame^] | 71 | fn try_start_instance(&self, instance_name: &str) -> Result<Arc<CompOsInstance>> { |
| 72 | let instance_starter = InstanceStarter::new(instance_name); |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 73 | let compos_instance = instance_starter.create_or_start_instance(&*self.service)?; |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 74 | |
| 75 | Ok(Arc::new(compos_instance)) |
| 76 | } |
| 77 | } |
| 78 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 79 | // Ensures we only run one instance at a time. |
| 80 | // Valid states: |
| 81 | // Starting: is_starting is true, running_instance is None. |
| 82 | // Started: is_starting is false, running_instance is Some(x) and there is a strong ref to x. |
| 83 | // Stopped: is_starting is false and running_instance is None or a weak ref to a dropped instance. |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 84 | // The panic calls here should never happen, unless the code above in InstanceManager is buggy. |
| 85 | // In particular nothing the client does should be able to trigger them. |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 86 | #[derive(Default)] |
| 87 | struct State { |
| 88 | running_instance: Option<Weak<CompOsInstance>>, |
| 89 | is_starting: bool, |
| 90 | } |
| 91 | |
| 92 | impl State { |
| 93 | // Move to Starting iff we are Stopped. |
| 94 | fn mark_starting(&mut self) -> Result<()> { |
| 95 | if self.is_starting { |
| 96 | bail!("An instance is already starting"); |
| 97 | } |
| 98 | if let Some(weak) = &self.running_instance { |
| 99 | if weak.strong_count() != 0 { |
| 100 | bail!("An instance is already running"); |
| 101 | } |
| 102 | } |
| 103 | self.running_instance = None; |
| 104 | self.is_starting = true; |
| 105 | Ok(()) |
| 106 | } |
| 107 | |
| 108 | // Move from Starting to Stopped. |
| 109 | fn mark_stopped(&mut self) { |
| 110 | if !self.is_starting || self.running_instance.is_some() { |
| 111 | panic!("Tried to mark stopped when not starting"); |
| 112 | } |
| 113 | self.is_starting = false; |
| 114 | } |
| 115 | |
| 116 | // Move from Starting to Started. |
| 117 | fn mark_started(&mut self, instance: &Arc<CompOsInstance>) -> Result<()> { |
| 118 | if !self.is_starting { |
| 119 | panic!("Tried to mark started when not starting") |
| 120 | } |
| 121 | if self.running_instance.is_some() { |
| 122 | panic!("Attempted to mark started when already started"); |
| 123 | } |
| 124 | self.is_starting = false; |
| 125 | self.running_instance = Some(Arc::downgrade(instance)); |
| 126 | Ok(()) |
| 127 | } |
| 128 | |
| 129 | // Return the running instance if we are in the Started state. |
| 130 | fn get_running_instance(&mut self) -> Option<Arc<CompOsInstance>> { |
| 131 | if self.is_starting { |
| 132 | return None; |
| 133 | } |
| 134 | let instance = self.running_instance.as_ref()?.upgrade(); |
| 135 | if instance.is_none() { |
| 136 | // No point keeping an orphaned weak reference |
| 137 | self.running_instance = None; |
| 138 | } |
| 139 | instance |
| 140 | } |
| 141 | } |