blob: 7d29685b06f35b510c751191a7b815510bc18eb8 [file] [log] [blame]
David Brazdil1f530702022-10-03 12:18:10 +01001// Copyright 2022, 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//! Android Virtualization Manager
16
17mod aidl;
18mod atom;
19mod composite;
20mod crosvm;
Jaewan Kimc03f6612023-02-20 00:06:26 +090021mod debug_config;
Shikha Panwar55e10ec2024-02-13 12:53:49 +000022mod dt_overlay;
David Brazdil1f530702022-10-03 12:18:10 +010023mod payload;
24mod selinux;
25
David Brazdil4b4c5102022-12-19 22:56:20 +000026use crate::aidl::{GLOBAL_SERVICE, VirtualizationService};
David Brazdil1f530702022-10-03 12:18:10 +010027use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::BnVirtualizationService;
Jiyong Parkba3099e2024-08-20 18:19:12 +090028use anyhow::{bail, Result};
David Brazdil4b4c5102022-12-19 22:56:20 +000029use binder::{BinderFeatures, ProcessState};
David Brazdil1f530702022-10-03 12:18:10 +010030use lazy_static::lazy_static;
Jeff Vander Stoep57da1572024-01-31 10:52:16 +010031use log::{info, LevelFilter};
David Brazdil1f530702022-10-03 12:18:10 +010032use rpcbinder::{FileDescriptorTransportMode, RpcServer};
Jiyong Parkba3099e2024-08-20 18:19:12 +090033use std::os::unix::io::{AsFd, RawFd};
David Brazdil1f530702022-10-03 12:18:10 +010034use clap::Parser;
Seungjae Yoodaf396a2024-04-15 12:58:07 +090035use nix::unistd::{write, Pid, Uid};
David Brazdil1f530702022-10-03 12:18:10 +010036use std::os::unix::raw::{pid_t, uid_t};
Jiyong Parkba3099e2024-08-20 18:19:12 +090037use safe_ownedfd::take_fd_ownership;
David Brazdil1f530702022-10-03 12:18:10 +010038
39const LOG_TAG: &str = "virtmgr";
40
41lazy_static! {
Seungjae Yoo13af0b62024-05-20 14:15:13 +090042 static ref PID_CURRENT: Pid = Pid::this();
David Brazdil1f530702022-10-03 12:18:10 +010043 static ref PID_PARENT: Pid = Pid::parent();
44 static ref UID_CURRENT: Uid = Uid::current();
45}
46
Seungjae Yoo13af0b62024-05-20 14:15:13 +090047fn get_this_pid() -> pid_t {
48 // Return the process ID of this process.
49 PID_CURRENT.as_raw()
50}
51
David Brazdil1f530702022-10-03 12:18:10 +010052fn get_calling_pid() -> pid_t {
53 // The caller is the parent of this process.
54 PID_PARENT.as_raw()
55}
56
57fn get_calling_uid() -> uid_t {
58 // The caller and this process share the same UID.
59 UID_CURRENT.as_raw()
60}
61
62#[derive(Parser)]
63struct Args {
64 /// File descriptor inherited from the caller to run RpcBinder server on.
65 /// This should be one end of a socketpair() compatible with RpcBinder's
66 /// UDS bootstrap transport.
67 #[clap(long)]
68 rpc_server_fd: RawFd,
69 /// File descriptor inherited from the caller to signal RpcBinder server
70 /// readiness. This should be one end of pipe() and the caller should be
71 /// waiting for HUP on the other end.
72 #[clap(long)]
73 ready_fd: RawFd,
74}
75
Alan Stokesc4d5def2023-02-14 17:01:59 +000076fn check_vm_support() -> Result<()> {
77 if hypervisor_props::is_any_vm_supported()? {
78 Ok(())
79 } else {
80 // This should never happen, it indicates a misconfigured device where the virt APEX
81 // is present but VMs are not supported. If it does happen, fail fast to avoid wasting
82 // resources trying.
83 bail!("Device doesn't support protected or non-protected VMs")
84 }
Alan Stokes8d39a9b2023-01-10 15:01:00 +000085}
86
David Brazdil1f530702022-10-03 12:18:10 +010087fn main() {
88 android_logger::init_once(
89 android_logger::Config::default()
90 .with_tag(LOG_TAG)
Jeff Vander Stoep57da1572024-01-31 10:52:16 +010091 .with_max_level(LevelFilter::Info)
92 .with_log_buffer(android_logger::LogId::System),
David Brazdil1f530702022-10-03 12:18:10 +010093 );
94
Alan Stokesc4d5def2023-02-14 17:01:59 +000095 check_vm_support().unwrap();
Alan Stokes8d39a9b2023-01-10 15:01:00 +000096
David Brazdil1f530702022-10-03 12:18:10 +010097 let args = Args::parse();
98
Jiyong Parkba3099e2024-08-20 18:19:12 +090099 let rpc_server_fd =
100 take_fd_ownership(args.rpc_server_fd).expect("Failed to take ownership of rpc_server_fd");
101 let ready_fd = take_fd_ownership(args.ready_fd).expect("Failed to take ownership of ready_fd");
David Brazdil1f530702022-10-03 12:18:10 +0100102
David Brazdil4b4c5102022-12-19 22:56:20 +0000103 // Start thread pool for kernel Binder connection to VirtualizationServiceInternal.
104 ProcessState::start_thread_pool();
105
Inseob Kimb198f6e2024-07-22 18:06:15 +0900106 if cfg!(early) {
107 panic!("Early VM not implemented");
108 } else {
109 GLOBAL_SERVICE.removeMemlockRlimit().expect("Failed to remove memlock rlimit");
110 }
David Brazdil4b4c5102022-12-19 22:56:20 +0000111
David Brazdil1f530702022-10-03 12:18:10 +0100112 let service = VirtualizationService::init();
113 let service =
114 BnVirtualizationService::new_binder(service, BinderFeatures::default()).as_binder();
115
116 let server = RpcServer::new_unix_domain_bootstrap(service, rpc_server_fd)
117 .expect("Failed to start RpcServer");
118 server.set_supported_file_descriptor_transport_modes(&[FileDescriptorTransportMode::Unix]);
119
120 info!("Started VirtualizationService RpcServer. Ready to accept connections");
121
122 // Signal readiness to the caller by closing our end of the pipe.
Seungjae Yoodaf396a2024-04-15 12:58:07 +0900123 write(ready_fd.as_fd(), "o".as_bytes())
124 .expect("Failed to write a single character through ready_fd");
David Brazdil1f530702022-10-03 12:18:10 +0100125 drop(ready_fd);
126
127 server.join();
128 info!("Shutting down VirtualizationService RpcServer");
129}