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