blob: e710f073aa33b731153d79f6110ae1d87861528b [file] [log] [blame]
Victor Hsieh045f1e62021-08-03 12:04:34 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! AuthFsService facilitates authfs mounting (which is a privileged operation) for the client. The
18//! client will provide an `AuthFsConfig` which includes the backend address (only port for now) and
19//! the filesystem configuration. It is up to the client to ensure the backend server is running. On
20//! a successful mount, the client receives an `IAuthFs`, and through the binder object, the client
21//! is able to retrieve "remote file descriptors".
22
23mod authfs;
Victor Hsieh045f1e62021-08-03 12:04:34 -070024
Alice Wang15ae7b92022-10-24 14:36:59 +000025use anyhow::{bail, Result};
Victor Hsieh045f1e62021-08-03 12:04:34 -070026use log::*;
David Brazdil671e6142022-11-16 11:47:27 +000027use rpcbinder::RpcServer;
Victor Hsieh045f1e62021-08-03 12:04:34 -070028use std::ffi::OsString;
29use std::fs::{create_dir, read_dir, remove_dir_all, remove_file};
30use std::sync::atomic::{AtomicUsize, Ordering};
31
Victor Hsieh045f1e62021-08-03 12:04:34 -070032use authfs_aidl_interface::aidl::com::android::virt::fs::AuthFsConfig::AuthFsConfig;
33use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFs::IAuthFs;
34use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::{
Alice Wang15ae7b92022-10-24 14:36:59 +000035 BnAuthFsService, IAuthFsService, AUTHFS_SERVICE_SOCKET_NAME,
Victor Hsieh045f1e62021-08-03 12:04:34 -070036};
Alice Wang15ae7b92022-10-24 14:36:59 +000037use binder::{self, BinderFeatures, ExceptionCode, Interface, Status, Strong};
Victor Hsieh045f1e62021-08-03 12:04:34 -070038
Victor Hsieh8bb67b62021-08-04 12:10:58 -070039const SERVICE_ROOT: &str = "/data/misc/authfs";
Victor Hsieh045f1e62021-08-03 12:04:34 -070040
41/// Implementation of `IAuthFsService`.
42pub struct AuthFsService {
43 serial_number: AtomicUsize,
44 debuggable: bool,
45}
46
47impl Interface for AuthFsService {}
48
49impl IAuthFsService for AuthFsService {
50 fn mount(&self, config: &AuthFsConfig) -> binder::Result<Strong<dyn IAuthFs>> {
51 self.validate(config)?;
52
53 let mountpoint = self.get_next_mount_point();
54
55 // The directory is supposed to be deleted when `AuthFs` is dropped.
56 create_dir(&mountpoint).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +000057 Status::new_service_specific_error_str(
58 -1,
59 Some(format!("Cannot create mount directory {:?}: {:?}", &mountpoint, e)),
Victor Hsieh045f1e62021-08-03 12:04:34 -070060 )
61 })?;
62
63 authfs::AuthFs::mount_and_wait(mountpoint, config, self.debuggable).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +000064 Status::new_service_specific_error_str(
65 -1,
66 Some(format!("mount_and_wait failed: {:?}", e)),
Victor Hsieh045f1e62021-08-03 12:04:34 -070067 )
68 })
69 }
70}
71
72impl AuthFsService {
73 fn new_binder(debuggable: bool) -> Strong<dyn IAuthFsService> {
74 let service = AuthFsService { serial_number: AtomicUsize::new(1), debuggable };
75 BnAuthFsService::new_binder(service, BinderFeatures::default())
76 }
77
78 fn validate(&self, config: &AuthFsConfig) -> binder::Result<()> {
79 if config.port < 0 {
Andrew Walbrandcf9d582022-08-03 11:25:24 +000080 return Err(Status::new_exception_str(
Victor Hsieh045f1e62021-08-03 12:04:34 -070081 ExceptionCode::ILLEGAL_ARGUMENT,
Andrew Walbrandcf9d582022-08-03 11:25:24 +000082 Some(format!("Invalid port: {}", config.port)),
Victor Hsieh045f1e62021-08-03 12:04:34 -070083 ));
84 }
85 Ok(())
86 }
87
88 fn get_next_mount_point(&self) -> OsString {
89 let previous = self.serial_number.fetch_add(1, Ordering::Relaxed);
90 OsString::from(format!("{}/{}", SERVICE_ROOT, previous))
91 }
92}
93
94fn clean_up_working_directory() -> Result<()> {
95 for entry in read_dir(SERVICE_ROOT)? {
96 let entry = entry?;
97 let path = entry.path();
98 if path.is_dir() {
99 remove_dir_all(path)?;
100 } else if path.is_file() {
101 remove_file(path)?;
102 } else {
103 bail!("Unrecognized path type: {:?}", path);
104 }
105 }
106 Ok(())
107}
108
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100109fn try_main() -> Result<()> {
Victor Hsieh045f1e62021-08-03 12:04:34 -0700110 let debuggable = env!("TARGET_BUILD_VARIANT") != "user";
111 let log_level = if debuggable { log::Level::Trace } else { log::Level::Info };
112 android_logger::init_once(
113 android_logger::Config::default().with_tag("authfs_service").with_min_level(log_level),
114 );
115
116 clean_up_working_directory()?;
117
Victor Hsieh045f1e62021-08-03 12:04:34 -0700118 let service = AuthFsService::new_binder(debuggable).as_binder();
Alice Wang15ae7b92022-10-24 14:36:59 +0000119 debug!("{} is starting as a rpc service.", AUTHFS_SERVICE_SOCKET_NAME);
David Brazdil671e6142022-11-16 11:47:27 +0000120 let server = RpcServer::new_init_unix_domain(service, AUTHFS_SERVICE_SOCKET_NAME)?;
121 info!("The RPC server '{}' is running.", AUTHFS_SERVICE_SOCKET_NAME);
122 server.join();
123 info!("The RPC server at '{}' has shut down gracefully.", AUTHFS_SERVICE_SOCKET_NAME);
124 Ok(())
Victor Hsieh045f1e62021-08-03 12:04:34 -0700125}
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100126
127fn main() {
128 if let Err(e) = try_main() {
129 error!("failed with {:?}", e);
130 std::process::exit(1);
131 }
132}