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; |
| 24 | mod common; |
| 25 | |
| 26 | use anyhow::{bail, Context, Result}; |
| 27 | use log::*; |
| 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 | |
| 32 | use crate::common::new_binder_exception; |
| 33 | use authfs_aidl_interface::aidl::com::android::virt::fs::AuthFsConfig::AuthFsConfig; |
| 34 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFs::IAuthFs; |
| 35 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::{ |
| 36 | BnAuthFsService, IAuthFsService, |
| 37 | }; |
| 38 | use authfs_aidl_interface::binder::{ |
| 39 | self, add_service, BinderFeatures, ExceptionCode, Interface, ProcessState, Strong, |
| 40 | }; |
| 41 | |
| 42 | const SERVICE_NAME: &str = "authfs_service"; |
Victor Hsieh | 8bb67b6 | 2021-08-04 12:10:58 -0700 | [diff] [blame^] | 43 | const SERVICE_ROOT: &str = "/data/misc/authfs"; |
Victor Hsieh | 045f1e6 | 2021-08-03 12:04:34 -0700 | [diff] [blame] | 44 | |
| 45 | /// Implementation of `IAuthFsService`. |
| 46 | pub struct AuthFsService { |
| 47 | serial_number: AtomicUsize, |
| 48 | debuggable: bool, |
| 49 | } |
| 50 | |
| 51 | impl Interface for AuthFsService {} |
| 52 | |
| 53 | impl IAuthFsService for AuthFsService { |
| 54 | fn mount(&self, config: &AuthFsConfig) -> binder::Result<Strong<dyn IAuthFs>> { |
| 55 | self.validate(config)?; |
| 56 | |
| 57 | let mountpoint = self.get_next_mount_point(); |
| 58 | |
| 59 | // The directory is supposed to be deleted when `AuthFs` is dropped. |
| 60 | create_dir(&mountpoint).map_err(|e| { |
| 61 | new_binder_exception( |
| 62 | ExceptionCode::SERVICE_SPECIFIC, |
| 63 | format!("Cannot create mount directory {:?}: {}", &mountpoint, e), |
| 64 | ) |
| 65 | })?; |
| 66 | |
| 67 | authfs::AuthFs::mount_and_wait(mountpoint, config, self.debuggable).map_err(|e| { |
| 68 | new_binder_exception( |
| 69 | ExceptionCode::SERVICE_SPECIFIC, |
| 70 | format!("mount_and_wait failed: {:?}", e), |
| 71 | ) |
| 72 | }) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl AuthFsService { |
| 77 | fn new_binder(debuggable: bool) -> Strong<dyn IAuthFsService> { |
| 78 | let service = AuthFsService { serial_number: AtomicUsize::new(1), debuggable }; |
| 79 | BnAuthFsService::new_binder(service, BinderFeatures::default()) |
| 80 | } |
| 81 | |
| 82 | fn validate(&self, config: &AuthFsConfig) -> binder::Result<()> { |
| 83 | if config.port < 0 { |
| 84 | return Err(new_binder_exception( |
| 85 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 86 | format!("Invalid port: {}", config.port), |
| 87 | )); |
| 88 | } |
| 89 | Ok(()) |
| 90 | } |
| 91 | |
| 92 | fn get_next_mount_point(&self) -> OsString { |
| 93 | let previous = self.serial_number.fetch_add(1, Ordering::Relaxed); |
| 94 | OsString::from(format!("{}/{}", SERVICE_ROOT, previous)) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fn clean_up_working_directory() -> Result<()> { |
| 99 | for entry in read_dir(SERVICE_ROOT)? { |
| 100 | let entry = entry?; |
| 101 | let path = entry.path(); |
| 102 | if path.is_dir() { |
| 103 | remove_dir_all(path)?; |
| 104 | } else if path.is_file() { |
| 105 | remove_file(path)?; |
| 106 | } else { |
| 107 | bail!("Unrecognized path type: {:?}", path); |
| 108 | } |
| 109 | } |
| 110 | Ok(()) |
| 111 | } |
| 112 | |
| 113 | fn main() -> Result<()> { |
| 114 | let debuggable = env!("TARGET_BUILD_VARIANT") != "user"; |
| 115 | let log_level = if debuggable { log::Level::Trace } else { log::Level::Info }; |
| 116 | android_logger::init_once( |
| 117 | android_logger::Config::default().with_tag("authfs_service").with_min_level(log_level), |
| 118 | ); |
| 119 | |
| 120 | clean_up_working_directory()?; |
| 121 | |
| 122 | ProcessState::start_thread_pool(); |
| 123 | |
| 124 | let service = AuthFsService::new_binder(debuggable).as_binder(); |
| 125 | add_service(SERVICE_NAME, service) |
| 126 | .with_context(|| format!("Failed to register service {}", SERVICE_NAME))?; |
| 127 | debug!("{} is running", SERVICE_NAME); |
| 128 | |
| 129 | ProcessState::join_thread_pool(); |
| 130 | bail!("Unexpected exit after join_thread_pool") |
| 131 | } |