blob: b71daa8eb6fe5938988a8817e05f7fb1f07a0314 [file] [log] [blame]
Jooyung Han347d9f22021-05-28 00:05:14 +09001// 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 Hanf48ceb42021-06-01 18:00:04 +090017mod ioutil;
Jooyung Han74573482021-06-08 17:10:21 +090018mod metadata;
Jooyung Han347d9f22021-05-28 00:05:14 +090019
20use android_logger::Config;
Jooyung Han634e2d72021-06-10 16:27:38 +090021use anyhow::{anyhow, bail, Result};
22use keystore2_system_property::PropertyWatcher;
Jooyung Han347d9f22021-05-28 00:05:14 +090023use log::{info, Level};
Jooyung Han634e2d72021-06-10 16:27:38 +090024use microdroid_payload_config::{Task, TaskType, VmPayloadConfig};
25use std::fs;
Jooyung Hanf48ceb42021-06-01 18:00:04 +090026use std::path::Path;
Jooyung Han634e2d72021-06-10 16:27:38 +090027use std::process::Command;
28use std::time::Duration;
29
30const WAIT_TIMEOUT: Duration = Duration::from_secs(10);
Jooyung Han347d9f22021-05-28 00:05:14 +090031
32const LOG_TAG: &str = "MicrodroidManager";
33
Jooyung Han634e2d72021-06-10 16:27:38 +090034fn main() -> Result<()> {
Jooyung Han347d9f22021-05-28 00:05:14 +090035 android_logger::init_once(Config::default().with_tag(LOG_TAG).with_min_level(Level::Debug));
36
37 info!("started.");
38
Jooyung Han74573482021-06-08 17:10:21 +090039 let metadata = metadata::load()?;
40 if !metadata.payload_config_path.is_empty() {
Jooyung Han634e2d72021-06-10 16:27:38 +090041 let config = load_config(Path::new(&metadata.payload_config_path))?;
42
43 // TODO(jooyung): wait until sys.boot_completed?
Jooyung Han347d9f22021-05-28 00:05:14 +090044 if let Some(main_task) = &config.task {
Jooyung Han634e2d72021-06-10 16:27:38 +090045 exec_task(main_task)?;
Jooyung Han347d9f22021-05-28 00:05:14 +090046 }
47 }
48
49 Ok(())
50}
51
Jooyung Han634e2d72021-06-10 16:27:38 +090052fn load_config(path: &Path) -> Result<VmPayloadConfig> {
53 info!("loading config from {:?}...", path);
54 let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?;
55 Ok(serde_json::from_reader(file)?)
56}
57
58fn exec_task(task: &Task) -> Result<()> {
59 info!("executing main task {:?}...", task);
Jiyong Park038b73e2021-06-16 01:57:02 +090060 let exit_status = build_command(task)?.spawn()?.wait()?;
61 if exit_status.success() {
62 Ok(())
63 } else {
64 match exit_status.code() {
65 Some(code) => bail!("task exited with exit code: {}", code),
66 None => bail!("task terminated by signal"),
67 }
68 }
Jooyung Han347d9f22021-05-28 00:05:14 +090069}
Jooyung Han634e2d72021-06-10 16:27:38 +090070
71fn build_command(task: &Task) -> Result<Command> {
72 Ok(match task.type_ {
73 TaskType::Executable => {
74 let mut command = Command::new(&task.command);
75 command.args(&task.args);
76 command
77 }
78 TaskType::MicrodroidLauncher => {
79 let mut command = Command::new("/system/bin/microdroid_launcher");
80 command.arg(find_library_path(&task.command)?).args(&task.args);
81 command
82 }
83 })
84}
85
86fn find_library_path(name: &str) -> Result<String> {
87 let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?;
88 let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?;
89 let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?;
90 let path = format!("/mnt/apk/lib/{}/{}", abi, name);
91
92 let metadata = fs::metadata(&path)?;
93 if !metadata.is_file() {
94 bail!("{} is not a file", &path);
95 }
96
97 Ok(path)
98}