Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Command to run a VM. |
| 16 | |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 17 | use crate::config::VmConfig; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 18 | use crate::sync::AtomicFlag; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 19 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService; |
| 20 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualMachine::IVirtualMachine; |
| 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualMachineCallback::{ |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 22 | BnVirtualMachineCallback, IVirtualMachineCallback, |
| 23 | }; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 24 | use android_system_virtualizationservice::binder::{ |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 25 | BinderFeatures, DeathRecipient, IBinder, ParcelFileDescriptor, Strong, |
| 26 | }; |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 27 | use android_system_virtualizationservice::binder::{Interface, Result as BinderResult}; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 28 | use anyhow::{Context, Error}; |
| 29 | use std::fs::File; |
| 30 | use std::io; |
| 31 | use std::os::unix::io::{AsRawFd, FromRawFd}; |
| 32 | use std::path::Path; |
| 33 | |
| 34 | /// Run a VM from the given configuration file. |
| 35 | pub fn command_run( |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 36 | service: Strong<dyn IVirtualizationService>, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 37 | config_path: &Path, |
| 38 | daemonize: bool, |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame^] | 39 | log_path: Option<&Path>, |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 40 | ) -> Result<(), Error> { |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 41 | let config_file = File::open(config_path).context("Failed to open config file")?; |
| 42 | let config = |
| 43 | VmConfig::load(&config_file).context("Failed to parse config file")?.to_parcelable()?; |
Andrew Walbran | be42924 | 2021-06-28 12:22:54 +0000 | [diff] [blame^] | 44 | let stdout = if let Some(log_path) = log_path { |
| 45 | Some(ParcelFileDescriptor::new( |
| 46 | File::create(log_path) |
| 47 | .with_context(|| format!("Failed to open log file {:?}", log_path))?, |
| 48 | )) |
| 49 | } else if daemonize { |
| 50 | None |
| 51 | } else { |
| 52 | Some(ParcelFileDescriptor::new(duplicate_stdout()?)) |
| 53 | }; |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 54 | let vm = service.startVm(&config, stdout.as_ref()).context("Failed to start VM")?; |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 55 | |
| 56 | let cid = vm.getCid().context("Failed to get CID")?; |
Andrew Walbran | 3a5a921 | 2021-05-04 17:09:08 +0000 | [diff] [blame] | 57 | println!("Started VM from {:?} with CID {}.", config_path, cid); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 58 | |
| 59 | if daemonize { |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 60 | // Pass the VM reference back to VirtualizationService and have it hold it in the |
| 61 | // background. |
Andrew Walbran | 17de24f | 2021-05-27 13:27:30 +0000 | [diff] [blame] | 62 | service.debugHoldVmRef(&vm).context("Failed to pass VM to VirtualizationService") |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 63 | } else { |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 64 | // Wait until the VM or VirtualizationService dies. If we just returned immediately then the |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 65 | // IVirtualMachine Binder object would be dropped and the VM would be killed. |
| 66 | wait_for_vm(vm) |
| 67 | } |
| 68 | } |
| 69 | |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 70 | /// Wait until the given VM or the VirtualizationService itself dies. |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 71 | fn wait_for_vm(vm: Strong<dyn IVirtualMachine>) -> Result<(), Error> { |
| 72 | let dead = AtomicFlag::default(); |
| 73 | let callback = BnVirtualMachineCallback::new_binder( |
| 74 | VirtualMachineCallback { dead: dead.clone() }, |
| 75 | BinderFeatures::default(), |
| 76 | ); |
| 77 | vm.registerCallback(&callback)?; |
| 78 | let death_recipient = wait_for_death(&mut vm.as_binder(), dead.clone())?; |
| 79 | dead.wait(); |
| 80 | // Ensure that death_recipient isn't dropped before we wait on the flag, as it is removed |
| 81 | // from the Binder when it's dropped. |
| 82 | drop(death_recipient); |
| 83 | Ok(()) |
| 84 | } |
| 85 | |
| 86 | /// Raise the given flag when the given Binder object dies. |
| 87 | /// |
| 88 | /// If the returned DeathRecipient is dropped then this will no longer do anything. |
| 89 | fn wait_for_death(binder: &mut impl IBinder, dead: AtomicFlag) -> Result<DeathRecipient, Error> { |
| 90 | let mut death_recipient = DeathRecipient::new(move || { |
Andrew Walbran | f6bf686 | 2021-05-21 12:41:13 +0000 | [diff] [blame] | 91 | println!("VirtualizationService died"); |
Andrew Walbran | f395b82 | 2021-05-05 10:38:59 +0000 | [diff] [blame] | 92 | dead.raise(); |
| 93 | }); |
| 94 | binder.link_to_death(&mut death_recipient)?; |
| 95 | Ok(death_recipient) |
| 96 | } |
| 97 | |
| 98 | #[derive(Debug)] |
| 99 | struct VirtualMachineCallback { |
| 100 | dead: AtomicFlag, |
| 101 | } |
| 102 | |
| 103 | impl Interface for VirtualMachineCallback {} |
| 104 | |
| 105 | impl IVirtualMachineCallback for VirtualMachineCallback { |
| 106 | fn onDied(&self, _cid: i32) -> BinderResult<()> { |
| 107 | println!("VM died"); |
| 108 | self.dead.raise(); |
| 109 | Ok(()) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Safely duplicate the standard output file descriptor. |
| 114 | fn duplicate_stdout() -> io::Result<File> { |
| 115 | let stdout_fd = io::stdout().as_raw_fd(); |
| 116 | // Safe because this just duplicates a file descriptor which we know to be valid, and we check |
| 117 | // for an error. |
| 118 | let dup_fd = unsafe { libc::dup(stdout_fd) }; |
| 119 | if dup_fd < 0 { |
| 120 | Err(io::Error::last_os_error()) |
| 121 | } else { |
| 122 | // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd` |
| 123 | // takes ownership of it. |
| 124 | Ok(unsafe { File::from_raw_fd(dup_fd) }) |
| 125 | } |
| 126 | } |