blob: ae72a59b7d58e45779d16bc24458753ad6e44582 [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);
60 build_command(task)?.spawn()?;
Jooyung Han347d9f22021-05-28 00:05:14 +090061 Ok(())
62}
Jooyung Han634e2d72021-06-10 16:27:38 +090063
64fn build_command(task: &Task) -> Result<Command> {
65 Ok(match task.type_ {
66 TaskType::Executable => {
67 let mut command = Command::new(&task.command);
68 command.args(&task.args);
69 command
70 }
71 TaskType::MicrodroidLauncher => {
72 let mut command = Command::new("/system/bin/microdroid_launcher");
73 command.arg(find_library_path(&task.command)?).args(&task.args);
74 command
75 }
76 })
77}
78
79fn find_library_path(name: &str) -> Result<String> {
80 let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?;
81 let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?;
82 let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?;
83 let path = format!("/mnt/apk/lib/{}/{}", abi, name);
84
85 let metadata = fs::metadata(&path)?;
86 if !metadata.is_file() {
87 bail!("{} is not a file", &path);
88 }
89
90 Ok(path)
91}