blob: a4e75a717f232edb766353c8752eabb4ce671491 [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};
Jeff Vander Stoep57da1572024-01-31 10:52:16 +010030use log::{info, LevelFilter};
David Brazdil1f530702022-10-03 12:18:10 +010031use rpcbinder::{FileDescriptorTransportMode, RpcServer};
Jiyong Parkba3099e2024-08-20 18:19:12 +090032use std::os::unix::io::{AsFd, RawFd};
Andrew Walbran9c03a3a2024-09-03 12:12:59 +010033use std::sync::LazyLock;
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
Andrew Walbran9c03a3a2024-09-03 12:12:59 +010041static PID_CURRENT: LazyLock<Pid> = LazyLock::new(Pid::this);
42static PID_PARENT: LazyLock<Pid> = LazyLock::new(Pid::parent);
43static UID_CURRENT: LazyLock<Uid> = LazyLock::new(Uid::current);
David Brazdil1f530702022-10-03 12:18:10 +010044
Seungjae Yoo13af0b62024-05-20 14:15:13 +090045fn get_this_pid() -> pid_t {
46 // Return the process ID of this process.
47 PID_CURRENT.as_raw()
48}
49
David Brazdil1f530702022-10-03 12:18:10 +010050fn get_calling_pid() -> pid_t {
51 // The caller is the parent of this process.
52 PID_PARENT.as_raw()
53}
54
55fn get_calling_uid() -> uid_t {
56 // The caller and this process share the same UID.
57 UID_CURRENT.as_raw()
58}
59
60#[derive(Parser)]
61struct Args {
62 /// File descriptor inherited from the caller to run RpcBinder server on.
63 /// This should be one end of a socketpair() compatible with RpcBinder's
64 /// UDS bootstrap transport.
65 #[clap(long)]
66 rpc_server_fd: RawFd,
67 /// File descriptor inherited from the caller to signal RpcBinder server
68 /// readiness. This should be one end of pipe() and the caller should be
69 /// waiting for HUP on the other end.
70 #[clap(long)]
71 ready_fd: RawFd,
72}
73
Alan Stokesc4d5def2023-02-14 17:01:59 +000074fn check_vm_support() -> Result<()> {
75 if hypervisor_props::is_any_vm_supported()? {
76 Ok(())
77 } else {
78 // This should never happen, it indicates a misconfigured device where the virt APEX
79 // is present but VMs are not supported. If it does happen, fail fast to avoid wasting
80 // resources trying.
81 bail!("Device doesn't support protected or non-protected VMs")
82 }
Alan Stokes8d39a9b2023-01-10 15:01:00 +000083}
84
David Brazdil1f530702022-10-03 12:18:10 +010085fn main() {
86 android_logger::init_once(
87 android_logger::Config::default()
88 .with_tag(LOG_TAG)
Jeff Vander Stoep57da1572024-01-31 10:52:16 +010089 .with_max_level(LevelFilter::Info)
90 .with_log_buffer(android_logger::LogId::System),
David Brazdil1f530702022-10-03 12:18:10 +010091 );
92
Alan Stokesc4d5def2023-02-14 17:01:59 +000093 check_vm_support().unwrap();
Alan Stokes8d39a9b2023-01-10 15:01:00 +000094
David Brazdil1f530702022-10-03 12:18:10 +010095 let args = Args::parse();
96
Jiyong Parkba3099e2024-08-20 18:19:12 +090097 let rpc_server_fd =
98 take_fd_ownership(args.rpc_server_fd).expect("Failed to take ownership of rpc_server_fd");
99 let ready_fd = take_fd_ownership(args.ready_fd).expect("Failed to take ownership of ready_fd");
David Brazdil1f530702022-10-03 12:18:10 +0100100
David Brazdil4b4c5102022-12-19 22:56:20 +0000101 // Start thread pool for kernel Binder connection to VirtualizationServiceInternal.
102 ProcessState::start_thread_pool();
103
Inseob Kimb198f6e2024-07-22 18:06:15 +0900104 if cfg!(early) {
105 panic!("Early VM not implemented");
106 } else {
107 GLOBAL_SERVICE.removeMemlockRlimit().expect("Failed to remove memlock rlimit");
108 }
David Brazdil4b4c5102022-12-19 22:56:20 +0000109
David Brazdil1f530702022-10-03 12:18:10 +0100110 let service = VirtualizationService::init();
111 let service =
112 BnVirtualizationService::new_binder(service, BinderFeatures::default()).as_binder();
113
114 let server = RpcServer::new_unix_domain_bootstrap(service, rpc_server_fd)
115 .expect("Failed to start RpcServer");
116 server.set_supported_file_descriptor_transport_modes(&[FileDescriptorTransportMode::Unix]);
117
118 info!("Started VirtualizationService RpcServer. Ready to accept connections");
119
120 // Signal readiness to the caller by closing our end of the pipe.
Seungjae Yoodaf396a2024-04-15 12:58:07 +0900121 write(ready_fd.as_fd(), "o".as_bytes())
122 .expect("Failed to write a single character through ready_fd");
David Brazdil1f530702022-10-03 12:18:10 +0100123 drop(ready_fd);
124
125 server.join();
126 info!("Shutting down VirtualizationService RpcServer");
127}