blob: 77cac9a21ef7cbed5e87ae1f279b1c164b7953f6 [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
25use anyhow::{bail, Context, Result};
26use log::*;
27use std::ffi::OsString;
28use std::fs::{create_dir, read_dir, remove_dir_all, remove_file};
29use std::sync::atomic::{AtomicUsize, Ordering};
30
Victor Hsieh045f1e62021-08-03 12:04:34 -070031use authfs_aidl_interface::aidl::com::android::virt::fs::AuthFsConfig::AuthFsConfig;
32use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFs::IAuthFs;
33use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::{
34 BnAuthFsService, IAuthFsService,
35};
Alan Stokes0e82b502022-08-08 14:44:48 +010036use binder::{
Andrew Walbrandcf9d582022-08-03 11:25:24 +000037 self, add_service, BinderFeatures, ExceptionCode, Interface, ProcessState, Status, Strong,
Victor Hsieh045f1e62021-08-03 12:04:34 -070038};
39
40const SERVICE_NAME: &str = "authfs_service";
Victor Hsieh8bb67b62021-08-04 12:10:58 -070041const SERVICE_ROOT: &str = "/data/misc/authfs";
Victor Hsieh045f1e62021-08-03 12:04:34 -070042
43/// Implementation of `IAuthFsService`.
44pub struct AuthFsService {
45 serial_number: AtomicUsize,
46 debuggable: bool,
47}
48
49impl Interface for AuthFsService {}
50
51impl IAuthFsService for AuthFsService {
52 fn mount(&self, config: &AuthFsConfig) -> binder::Result<Strong<dyn IAuthFs>> {
53 self.validate(config)?;
54
55 let mountpoint = self.get_next_mount_point();
56
57 // The directory is supposed to be deleted when `AuthFs` is dropped.
58 create_dir(&mountpoint).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +000059 Status::new_service_specific_error_str(
60 -1,
61 Some(format!("Cannot create mount directory {:?}: {:?}", &mountpoint, e)),
Victor Hsieh045f1e62021-08-03 12:04:34 -070062 )
63 })?;
64
65 authfs::AuthFs::mount_and_wait(mountpoint, config, self.debuggable).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +000066 Status::new_service_specific_error_str(
67 -1,
68 Some(format!("mount_and_wait failed: {:?}", e)),
Victor Hsieh045f1e62021-08-03 12:04:34 -070069 )
70 })
71 }
72}
73
74impl AuthFsService {
75 fn new_binder(debuggable: bool) -> Strong<dyn IAuthFsService> {
76 let service = AuthFsService { serial_number: AtomicUsize::new(1), debuggable };
77 BnAuthFsService::new_binder(service, BinderFeatures::default())
78 }
79
80 fn validate(&self, config: &AuthFsConfig) -> binder::Result<()> {
81 if config.port < 0 {
Andrew Walbrandcf9d582022-08-03 11:25:24 +000082 return Err(Status::new_exception_str(
Victor Hsieh045f1e62021-08-03 12:04:34 -070083 ExceptionCode::ILLEGAL_ARGUMENT,
Andrew Walbrandcf9d582022-08-03 11:25:24 +000084 Some(format!("Invalid port: {}", config.port)),
Victor Hsieh045f1e62021-08-03 12:04:34 -070085 ));
86 }
87 Ok(())
88 }
89
90 fn get_next_mount_point(&self) -> OsString {
91 let previous = self.serial_number.fetch_add(1, Ordering::Relaxed);
92 OsString::from(format!("{}/{}", SERVICE_ROOT, previous))
93 }
94}
95
96fn clean_up_working_directory() -> Result<()> {
97 for entry in read_dir(SERVICE_ROOT)? {
98 let entry = entry?;
99 let path = entry.path();
100 if path.is_dir() {
101 remove_dir_all(path)?;
102 } else if path.is_file() {
103 remove_file(path)?;
104 } else {
105 bail!("Unrecognized path type: {:?}", path);
106 }
107 }
108 Ok(())
109}
110
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100111fn try_main() -> Result<()> {
Victor Hsieh045f1e62021-08-03 12:04:34 -0700112 let debuggable = env!("TARGET_BUILD_VARIANT") != "user";
113 let log_level = if debuggable { log::Level::Trace } else { log::Level::Info };
114 android_logger::init_once(
115 android_logger::Config::default().with_tag("authfs_service").with_min_level(log_level),
116 );
117
118 clean_up_working_directory()?;
119
120 ProcessState::start_thread_pool();
121
122 let service = AuthFsService::new_binder(debuggable).as_binder();
123 add_service(SERVICE_NAME, service)
124 .with_context(|| format!("Failed to register service {}", SERVICE_NAME))?;
125 debug!("{} is running", SERVICE_NAME);
126
127 ProcessState::join_thread_pool();
128 bail!("Unexpected exit after join_thread_pool")
129}
Alan Stokese1b6e1c2021-10-01 12:44:49 +0100130
131fn main() {
132 if let Err(e) = try_main() {
133 error!("failed with {:?}", e);
134 std::process::exit(1);
135 }
136}