Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | mod authfs; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 24 | |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame^] | 25 | use anyhow::{bail, Result}; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 26 | use log::*; |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame^] | 27 | use rpcbinder::run_init_unix_domain_rpc_server; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 28 | use std::ffi::OsString; |
| 29 | use std::fs::{create_dir, read_dir, remove_dir_all, remove_file}; |
| 30 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 31 | |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 32 | use authfs_aidl_interface::aidl::com::android::virt::fs::AuthFsConfig::AuthFsConfig; |
| 33 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFs::IAuthFs; |
| 34 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::{ |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame^] | 35 | BnAuthFsService, IAuthFsService, AUTHFS_SERVICE_SOCKET_NAME, |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 36 | }; |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame^] | 37 | use binder::{self, BinderFeatures, ExceptionCode, Interface, Status, Strong}; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 38 | |
Victor Hsieh | 8bb67b6 | 2021-08-04 12:10:58 -0700 | [diff] [blame] | 39 | const SERVICE_ROOT: &str = "/data/misc/authfs"; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 40 | |
| 41 | /// Implementation of `IAuthFsService`. |
| 42 | pub struct AuthFsService { |
| 43 | serial_number: AtomicUsize, |
| 44 | debuggable: bool, |
| 45 | } |
| 46 | |
| 47 | impl Interface for AuthFsService {} |
| 48 | |
| 49 | impl 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 Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 57 | Status::new_service_specific_error_str( |
| 58 | -1, |
| 59 | Some(format!("Cannot create mount directory {:?}: {:?}", &mountpoint, e)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 60 | ) |
| 61 | })?; |
| 62 | |
| 63 | authfs::AuthFs::mount_and_wait(mountpoint, config, self.debuggable).map_err(|e| { |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 64 | Status::new_service_specific_error_str( |
| 65 | -1, |
| 66 | Some(format!("mount_and_wait failed: {:?}", e)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 67 | ) |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl 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 Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 80 | return Err(Status::new_exception_str( |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 81 | ExceptionCode::ILLEGAL_ARGUMENT, |
Andrew Walbran | dcf9d58 | 2022-08-03 11:25:24 +0000 | [diff] [blame] | 82 | Some(format!("Invalid port: {}", config.port)), |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 83 | )); |
| 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 | |
| 94 | fn 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 Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 109 | fn try_main() -> Result<()> { |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 110 | 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 Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 118 | let service = AuthFsService::new_binder(debuggable).as_binder(); |
Alice Wang | 15ae7b9 | 2022-10-24 14:36:59 +0000 | [diff] [blame^] | 119 | debug!("{} is starting as a rpc service.", AUTHFS_SERVICE_SOCKET_NAME); |
| 120 | let retval = run_init_unix_domain_rpc_server(service, AUTHFS_SERVICE_SOCKET_NAME, || { |
| 121 | info!("The RPC server '{}' is running.", AUTHFS_SERVICE_SOCKET_NAME); |
| 122 | }); |
| 123 | if retval { |
| 124 | info!("The RPC server at '{}' has shut down gracefully.", AUTHFS_SERVICE_SOCKET_NAME); |
| 125 | Ok(()) |
| 126 | } else { |
| 127 | bail!("Premature termination of the RPC server '{}'.", AUTHFS_SERVICE_SOCKET_NAME) |
| 128 | } |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 129 | } |
Alan Stokes | e1b6e1c | 2021-10-01 12:44:49 +0100 | [diff] [blame] | 130 | |
| 131 | fn main() { |
| 132 | if let Err(e) = try_main() { |
| 133 | error!("failed with {:?}", e); |
| 134 | std::process::exit(1); |
| 135 | } |
| 136 | } |