Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [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 | //! Microdroid Manager |
| 16 | |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 17 | mod ioutil; |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 18 | mod metadata; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 19 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 20 | use anyhow::{anyhow, bail, Result}; |
| 21 | use keystore2_system_property::PropertyWatcher; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame^] | 22 | use log::{error, info}; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 23 | use microdroid_payload_config::{Task, TaskType, VmPayloadConfig}; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame^] | 24 | use std::fs::{self, File}; |
| 25 | use std::os::unix::io::{FromRawFd, IntoRawFd}; |
Jooyung Han | f48ceb4 | 2021-06-01 18:00:04 +0900 | [diff] [blame] | 26 | use std::path::Path; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame^] | 27 | use std::process::{Command, Stdio}; |
| 28 | use std::str; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 29 | use std::time::Duration; |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame^] | 30 | use vsock::VsockStream; |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 31 | |
| 32 | const WAIT_TIMEOUT: Duration = Duration::from_secs(10); |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 33 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 34 | fn main() -> Result<()> { |
Jiyong Park | 79b8801 | 2021-06-25 13:06:25 +0900 | [diff] [blame] | 35 | kernlog::init()?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 36 | info!("started."); |
| 37 | |
Jooyung Han | 7457348 | 2021-06-08 17:10:21 +0900 | [diff] [blame] | 38 | let metadata = metadata::load()?; |
| 39 | if !metadata.payload_config_path.is_empty() { |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 40 | let config = load_config(Path::new(&metadata.payload_config_path))?; |
| 41 | |
| 42 | // TODO(jooyung): wait until sys.boot_completed? |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 43 | if let Some(main_task) = &config.task { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame^] | 44 | exec_task(main_task).map_err(|e| { |
| 45 | error!("failed to execute task: {}", e); |
| 46 | e |
| 47 | })?; |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | Ok(()) |
| 52 | } |
| 53 | |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 54 | fn load_config(path: &Path) -> Result<VmPayloadConfig> { |
| 55 | info!("loading config from {:?}...", path); |
| 56 | let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?; |
| 57 | Ok(serde_json::from_reader(file)?) |
| 58 | } |
| 59 | |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame^] | 60 | /// Executes the given task. Stdout of the task is piped into the vsock stream to the |
| 61 | /// virtualizationservice in the host side. |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 62 | fn exec_task(task: &Task) -> Result<()> { |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame^] | 63 | const VMADDR_CID_HOST: u32 = 2; |
| 64 | const PORT_VIRT_SVC: u32 = 3000; |
| 65 | let stdout = match VsockStream::connect_with_cid_port(VMADDR_CID_HOST, PORT_VIRT_SVC) { |
| 66 | Ok(stream) => { |
| 67 | // SAFETY: the ownership of the underlying file descriptor is transferred from stream |
| 68 | // to the file object, and then into the Command object. When the command is finished, |
| 69 | // the file descriptor is closed. |
| 70 | let f = unsafe { File::from_raw_fd(stream.into_raw_fd()) }; |
| 71 | Stdio::from(f) |
Jiyong Park | 038b73e | 2021-06-16 01:57:02 +0900 | [diff] [blame] | 72 | } |
Jiyong Park | 8611a6c | 2021-07-09 18:17:44 +0900 | [diff] [blame^] | 73 | Err(e) => { |
| 74 | error!("failed to connect to virtualization service: {}", e); |
| 75 | // Don't fail hard here. Even if we failed to connect to the virtualizationservice, |
| 76 | // we keep executing the task. This can happen if the owner of the VM doesn't register |
| 77 | // callback to accept the stream. Use /dev/null as the stdout so that the task can |
| 78 | // make progress without waiting for someone to consume the output. |
| 79 | Stdio::null() |
| 80 | } |
| 81 | }; |
| 82 | info!("executing main task {:?}...", task); |
| 83 | // TODO(jiyong): consider piping the stream into stdio (and probably stderr) as well. |
| 84 | let mut child = build_command(task)?.stdout(stdout).spawn()?; |
| 85 | match child.wait()?.code() { |
| 86 | Some(0) => { |
| 87 | info!("task successfully finished"); |
| 88 | Ok(()) |
| 89 | } |
| 90 | Some(code) => bail!("task exited with exit code: {}", code), |
| 91 | None => bail!("task terminated by signal"), |
Jiyong Park | 038b73e | 2021-06-16 01:57:02 +0900 | [diff] [blame] | 92 | } |
Jooyung Han | 347d9f2 | 2021-05-28 00:05:14 +0900 | [diff] [blame] | 93 | } |
Jooyung Han | 634e2d7 | 2021-06-10 16:27:38 +0900 | [diff] [blame] | 94 | |
| 95 | fn build_command(task: &Task) -> Result<Command> { |
| 96 | Ok(match task.type_ { |
| 97 | TaskType::Executable => { |
| 98 | let mut command = Command::new(&task.command); |
| 99 | command.args(&task.args); |
| 100 | command |
| 101 | } |
| 102 | TaskType::MicrodroidLauncher => { |
| 103 | let mut command = Command::new("/system/bin/microdroid_launcher"); |
| 104 | command.arg(find_library_path(&task.command)?).args(&task.args); |
| 105 | command |
| 106 | } |
| 107 | }) |
| 108 | } |
| 109 | |
| 110 | fn find_library_path(name: &str) -> Result<String> { |
| 111 | let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?; |
| 112 | let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?; |
| 113 | let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?; |
| 114 | let path = format!("/mnt/apk/lib/{}/{}", abi, name); |
| 115 | |
| 116 | let metadata = fs::metadata(&path)?; |
| 117 | if !metadata.is_file() { |
| 118 | bail!("{} is not a file", &path); |
| 119 | } |
| 120 | |
| 121 | Ok(path) |
| 122 | } |