blob: 10731c5e238655988761f52f8bd46e808f9e3628 [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<()> {
Jooyung Han607191c2021-06-16 10:32:02 +090032 // TODO(b/189805435) use kernlog
33 env_logger::init();
Jooyung Han347d9f22021-05-28 00:05:14 +090034
35 info!("started.");
36
Jooyung Han74573482021-06-08 17:10:21 +090037 let metadata = metadata::load()?;
38 if !metadata.payload_config_path.is_empty() {
Jooyung Han634e2d72021-06-10 16:27:38 +090039 let config = load_config(Path::new(&metadata.payload_config_path))?;
40
41 // TODO(jooyung): wait until sys.boot_completed?
Jooyung Han347d9f22021-05-28 00:05:14 +090042 if let Some(main_task) = &config.task {
Jooyung Han634e2d72021-06-10 16:27:38 +090043 exec_task(main_task)?;
Jooyung Han347d9f22021-05-28 00:05:14 +090044 }
45 }
46
47 Ok(())
48}
49
Jooyung Han634e2d72021-06-10 16:27:38 +090050fn load_config(path: &Path) -> Result<VmPayloadConfig> {
51 info!("loading config from {:?}...", path);
52 let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?;
53 Ok(serde_json::from_reader(file)?)
54}
55
56fn exec_task(task: &Task) -> Result<()> {
57 info!("executing main task {:?}...", task);
Jiyong Park038b73e2021-06-16 01:57:02 +090058 let exit_status = build_command(task)?.spawn()?.wait()?;
59 if exit_status.success() {
60 Ok(())
61 } else {
62 match exit_status.code() {
63 Some(code) => bail!("task exited with exit code: {}", code),
64 None => bail!("task terminated by signal"),
65 }
66 }
Jooyung Han347d9f22021-05-28 00:05:14 +090067}
Jooyung Han634e2d72021-06-10 16:27:38 +090068
69fn build_command(task: &Task) -> Result<Command> {
70 Ok(match task.type_ {
71 TaskType::Executable => {
72 let mut command = Command::new(&task.command);
73 command.args(&task.args);
74 command
75 }
76 TaskType::MicrodroidLauncher => {
77 let mut command = Command::new("/system/bin/microdroid_launcher");
78 command.arg(find_library_path(&task.command)?).args(&task.args);
79 command
80 }
81 })
82}
83
84fn find_library_path(name: &str) -> Result<String> {
85 let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?;
86 let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?;
87 let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?;
88 let path = format!("/mnt/apk/lib/{}/{}", abi, name);
89
90 let metadata = fs::metadata(&path)?;
91 if !metadata.is_file() {
92 bail!("{} is not a file", &path);
93 }
94
95 Ok(path)
96}