blob: 9bcfa67e735bcb90f46e5ebc7a064155e605201e [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
Jooyung Han634e2d72021-06-10 16:27:38 +090020use anyhow::{anyhow, bail, Result};
21use keystore2_system_property::PropertyWatcher;
Jooyung Han607191c2021-06-16 10:32:02 +090022use log::info;
Jooyung Han634e2d72021-06-10 16:27:38 +090023use microdroid_payload_config::{Task, TaskType, VmPayloadConfig};
24use std::fs;
Jooyung Hanf48ceb42021-06-01 18:00:04 +090025use std::path::Path;
Jooyung Han634e2d72021-06-10 16:27:38 +090026use std::process::Command;
27use std::time::Duration;
28
29const WAIT_TIMEOUT: Duration = Duration::from_secs(10);
Jooyung Han347d9f22021-05-28 00:05:14 +090030
Jooyung Han634e2d72021-06-10 16:27:38 +090031fn main() -> Result<()> {
Jiyong Park79b88012021-06-25 13:06:25 +090032 kernlog::init()?;
Jooyung Han347d9f22021-05-28 00:05:14 +090033 info!("started.");
34
Jooyung Han74573482021-06-08 17:10:21 +090035 let metadata = metadata::load()?;
36 if !metadata.payload_config_path.is_empty() {
Jooyung Han634e2d72021-06-10 16:27:38 +090037 let config = load_config(Path::new(&metadata.payload_config_path))?;
38
39 // TODO(jooyung): wait until sys.boot_completed?
Jooyung Han347d9f22021-05-28 00:05:14 +090040 if let Some(main_task) = &config.task {
Jooyung Han634e2d72021-06-10 16:27:38 +090041 exec_task(main_task)?;
Jooyung Han347d9f22021-05-28 00:05:14 +090042 }
43 }
44
45 Ok(())
46}
47
Jooyung Han634e2d72021-06-10 16:27:38 +090048fn load_config(path: &Path) -> Result<VmPayloadConfig> {
49 info!("loading config from {:?}...", path);
50 let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?;
51 Ok(serde_json::from_reader(file)?)
52}
53
54fn exec_task(task: &Task) -> Result<()> {
55 info!("executing main task {:?}...", task);
Jiyong Park038b73e2021-06-16 01:57:02 +090056 let exit_status = build_command(task)?.spawn()?.wait()?;
57 if exit_status.success() {
58 Ok(())
59 } else {
60 match exit_status.code() {
61 Some(code) => bail!("task exited with exit code: {}", code),
62 None => bail!("task terminated by signal"),
63 }
64 }
Jooyung Han347d9f22021-05-28 00:05:14 +090065}
Jooyung Han634e2d72021-06-10 16:27:38 +090066
67fn build_command(task: &Task) -> Result<Command> {
68 Ok(match task.type_ {
69 TaskType::Executable => {
70 let mut command = Command::new(&task.command);
71 command.args(&task.args);
72 command
73 }
74 TaskType::MicrodroidLauncher => {
75 let mut command = Command::new("/system/bin/microdroid_launcher");
76 command.arg(find_library_path(&task.command)?).args(&task.args);
77 command
78 }
79 })
80}
81
82fn find_library_path(name: &str) -> Result<String> {
83 let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?;
84 let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?;
85 let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?;
86 let path = format!("/mnt/apk/lib/{}/{}", abi, name);
87
88 let metadata = fs::metadata(&path)?;
89 if !metadata.is_file() {
90 bail!("{} is not a file", &path);
91 }
92
93 Ok(path)
94}