Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [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 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 17 | //! This program is a constrained file/FD server to serve file requests through a remote binder |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 18 | //! service. The file server is not designed to serve arbitrary file paths in the filesystem. On |
| 19 | //! the contrary, the server should be configured to start with already opened FDs, and serve the |
| 20 | //! client's request against the FDs |
| 21 | //! |
| 22 | //! For example, `exec 9</path/to/file fd_server --ro-fds 9` starts the binder service. A client |
| 23 | //! client can then request the content of file 9 by offset and size. |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 24 | |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 25 | mod fsverity; |
| 26 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 27 | use anyhow::{bail, Result}; |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 28 | use binder::unstable_api::AsNative; |
| 29 | use log::{debug, error}; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 30 | use std::cmp::min; |
| 31 | use std::collections::BTreeMap; |
| 32 | use std::convert::TryInto; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 33 | use std::fs::File; |
| 34 | use std::io; |
| 35 | use std::os::unix::fs::FileExt; |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 36 | use std::os::unix::io::{AsRawFd, FromRawFd}; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 37 | |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 38 | use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService::{ |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 39 | BnVirtFdService, IVirtFdService, ERROR_FILE_TOO_LARGE, ERROR_IO, ERROR_UNKNOWN_FD, |
| 40 | MAX_REQUESTING_DATA, |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 41 | }; |
| 42 | use authfs_aidl_interface::binder::{ |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 43 | BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, StatusCode, Strong, |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 44 | }; |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 45 | use binder_common::new_binder_exception; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 46 | |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 47 | const RPC_SERVICE_PORT: u32 = 3264; // TODO: support dynamic port for multiple fd_server instances |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 48 | |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 49 | fn validate_and_cast_offset(offset: i64) -> Result<u64, Status> { |
| 50 | offset.try_into().map_err(|_| { |
| 51 | new_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT, format!("Invalid offset: {}", offset)) |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | fn validate_and_cast_size(size: i32) -> Result<usize, Status> { |
| 56 | if size > MAX_REQUESTING_DATA { |
| 57 | Err(new_binder_exception( |
| 58 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 59 | format!("Unexpectedly large size: {}", size), |
| 60 | )) |
| 61 | } else { |
| 62 | size.try_into().map_err(|_| { |
| 63 | new_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT, format!("Invalid size: {}", size)) |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 68 | /// Configuration of a file descriptor to be served/exposed/shared. |
| 69 | enum FdConfig { |
| 70 | /// A read-only file to serve by this server. The file is supposed to be verifiable with the |
| 71 | /// associated fs-verity metadata. |
| 72 | Readonly { |
| 73 | /// The file to read from. fs-verity metadata can be retrieved from this file's FD. |
| 74 | file: File, |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 75 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 76 | /// Alternative Merkle tree stored in another file. |
| 77 | alt_merkle_tree: Option<File>, |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 78 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 79 | /// Alternative signature stored in another file. |
| 80 | alt_signature: Option<File>, |
| 81 | }, |
| 82 | |
| 83 | /// A readable/writable file to serve by this server. This backing file should just be a |
| 84 | /// regular file and does not have any specific property. |
| 85 | ReadWrite(File), |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | struct FdService { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 89 | /// A pool of opened files, may be readonly or read-writable. |
| 90 | fd_pool: BTreeMap<i32, FdConfig>, |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | impl FdService { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 94 | pub fn new_binder(fd_pool: BTreeMap<i32, FdConfig>) -> Strong<dyn IVirtFdService> { |
Andrew Walbran | 4de2878 | 2021-04-13 14:51:43 +0000 | [diff] [blame] | 95 | BnVirtFdService::new_binder(FdService { fd_pool }, BinderFeatures::default()) |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 98 | fn get_file_config(&self, id: i32) -> BinderResult<&FdConfig> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 99 | self.fd_pool.get(&id).ok_or_else(|| Status::from(ERROR_UNKNOWN_FD)) |
| 100 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | impl Interface for FdService {} |
| 104 | |
| 105 | impl IVirtFdService for FdService { |
| 106 | fn readFile(&self, id: i32, offset: i64, size: i32) -> BinderResult<Vec<u8>> { |
| 107 | let size: usize = validate_and_cast_size(size)?; |
| 108 | let offset: u64 = validate_and_cast_offset(offset)?; |
| 109 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 110 | match self.get_file_config(id)? { |
| 111 | FdConfig::Readonly { file, .. } | FdConfig::ReadWrite(file) => { |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 112 | read_into_buf(file, size, offset).map_err(|e| { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 113 | error!("readFile: read error: {}", e); |
| 114 | Status::from(ERROR_IO) |
| 115 | }) |
| 116 | } |
| 117 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | fn readFsverityMerkleTree(&self, id: i32, offset: i64, size: i32) -> BinderResult<Vec<u8>> { |
| 121 | let size: usize = validate_and_cast_size(size)?; |
| 122 | let offset: u64 = validate_and_cast_offset(offset)?; |
| 123 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 124 | match &self.get_file_config(id)? { |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 125 | FdConfig::Readonly { file, alt_merkle_tree, .. } => { |
| 126 | if let Some(tree_file) = &alt_merkle_tree { |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 127 | read_into_buf(tree_file, size, offset).map_err(|e| { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 128 | error!("readFsverityMerkleTree: read error: {}", e); |
| 129 | Status::from(ERROR_IO) |
| 130 | }) |
| 131 | } else { |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 132 | let mut buf = vec![0; size]; |
| 133 | let s = fsverity::read_merkle_tree(file.as_raw_fd(), offset, &mut buf) |
| 134 | .map_err(|e| { |
| 135 | error!("readFsverityMerkleTree: failed to retrieve merkle tree: {}", e); |
| 136 | Status::from(e.raw_os_error().unwrap_or(ERROR_IO)) |
| 137 | })?; |
| 138 | debug_assert!(s <= buf.len(), "Shouldn't return more bytes than asked"); |
| 139 | buf.truncate(s); |
| 140 | Ok(buf) |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | FdConfig::ReadWrite(_file) => { |
| 144 | // For a writable file, Merkle tree is not expected to be served since Auth FS |
| 145 | // doesn't trust it anyway. Auth FS may keep the Merkle tree privately for its own |
| 146 | // use. |
| 147 | Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Unsupported")) |
| 148 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | fn readFsveritySignature(&self, id: i32) -> BinderResult<Vec<u8>> { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 153 | match &self.get_file_config(id)? { |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 154 | FdConfig::Readonly { file, alt_signature, .. } => { |
| 155 | if let Some(sig_file) = &alt_signature { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 156 | // Supposedly big enough buffer size to store signature. |
| 157 | let size = MAX_REQUESTING_DATA as usize; |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 158 | let offset = 0; |
Chris Wailes | 68c39f8 | 2021-07-27 16:03:44 -0700 | [diff] [blame] | 159 | read_into_buf(sig_file, size, offset).map_err(|e| { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 160 | error!("readFsveritySignature: read error: {}", e); |
| 161 | Status::from(ERROR_IO) |
| 162 | }) |
| 163 | } else { |
Victor Hsieh | 4dc85c9 | 2021-03-15 11:01:23 -0700 | [diff] [blame] | 164 | let mut buf = vec![0; MAX_REQUESTING_DATA as usize]; |
| 165 | let s = fsverity::read_signature(file.as_raw_fd(), &mut buf).map_err(|e| { |
| 166 | error!("readFsverityMerkleTree: failed to retrieve merkle tree: {}", e); |
| 167 | Status::from(e.raw_os_error().unwrap_or(ERROR_IO)) |
| 168 | })?; |
| 169 | debug_assert!(s <= buf.len(), "Shouldn't return more bytes than asked"); |
| 170 | buf.truncate(s); |
| 171 | Ok(buf) |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | FdConfig::ReadWrite(_file) => { |
| 175 | // There is no signature for a writable file. |
| 176 | Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Unsupported")) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | fn writeFile(&self, id: i32, buf: &[u8], offset: i64) -> BinderResult<i32> { |
| 182 | match &self.get_file_config(id)? { |
| 183 | FdConfig::Readonly { .. } => Err(StatusCode::INVALID_OPERATION.into()), |
| 184 | FdConfig::ReadWrite(file) => { |
| 185 | let offset: u64 = offset.try_into().map_err(|_| { |
| 186 | new_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT, "Invalid offset") |
| 187 | })?; |
| 188 | // Check buffer size just to make `as i32` safe below. |
| 189 | if buf.len() > i32::MAX as usize { |
| 190 | return Err(new_binder_exception( |
| 191 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 192 | "Buffer size is too big", |
| 193 | )); |
| 194 | } |
| 195 | Ok(file.write_at(buf, offset).map_err(|e| { |
| 196 | error!("writeFile: write error: {}", e); |
| 197 | Status::from(ERROR_IO) |
| 198 | })? as i32) |
| 199 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
Victor Hsieh | 9d0ab62 | 2021-04-26 17:07:02 -0700 | [diff] [blame] | 202 | |
| 203 | fn resize(&self, id: i32, size: i64) -> BinderResult<()> { |
| 204 | match &self.get_file_config(id)? { |
| 205 | FdConfig::Readonly { .. } => Err(StatusCode::INVALID_OPERATION.into()), |
| 206 | FdConfig::ReadWrite(file) => { |
| 207 | if size < 0 { |
| 208 | return Err(new_binder_exception( |
| 209 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 210 | "Invalid size to resize to", |
| 211 | )); |
| 212 | } |
| 213 | file.set_len(size as u64).map_err(|e| { |
| 214 | error!("resize: set_len error: {}", e); |
| 215 | Status::from(ERROR_IO) |
| 216 | }) |
| 217 | } |
| 218 | } |
| 219 | } |
Victor Hsieh | 50d75ac | 2021-09-03 14:46:55 -0700 | [diff] [blame] | 220 | |
| 221 | fn getFileSize(&self, id: i32) -> BinderResult<i64> { |
| 222 | match &self.get_file_config(id)? { |
| 223 | FdConfig::Readonly { file, .. } => { |
| 224 | let size = file |
| 225 | .metadata() |
| 226 | .map_err(|e| { |
| 227 | error!("getFileSize error: {}", e); |
| 228 | Status::from(ERROR_IO) |
| 229 | })? |
| 230 | .len(); |
| 231 | Ok(size.try_into().map_err(|e| { |
| 232 | error!("getFileSize: File too large: {}", e); |
| 233 | Status::from(ERROR_FILE_TOO_LARGE) |
| 234 | })?) |
| 235 | } |
| 236 | FdConfig::ReadWrite(_file) => { |
| 237 | // Content and metadata of a writable file needs to be tracked by authfs, since |
| 238 | // fd_server isn't considered trusted. So there is no point to support getFileSize |
| 239 | // for a writable file. |
| 240 | Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Unsupported")) |
| 241 | } |
| 242 | } |
| 243 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | fn read_into_buf(file: &File, max_size: usize, offset: u64) -> io::Result<Vec<u8>> { |
| 247 | let remaining = file.metadata()?.len().saturating_sub(offset); |
| 248 | let buf_size = min(remaining, max_size as u64) as usize; |
| 249 | let mut buf = vec![0; buf_size]; |
| 250 | file.read_exact_at(&mut buf, offset)?; |
| 251 | Ok(buf) |
| 252 | } |
| 253 | |
| 254 | fn is_fd_valid(fd: i32) -> bool { |
| 255 | // SAFETY: a query-only syscall |
| 256 | let retval = unsafe { libc::fcntl(fd, libc::F_GETFD) }; |
| 257 | retval >= 0 |
| 258 | } |
| 259 | |
| 260 | fn fd_to_file(fd: i32) -> Result<File> { |
| 261 | if !is_fd_valid(fd) { |
| 262 | bail!("Bad FD: {}", fd); |
| 263 | } |
| 264 | // SAFETY: The caller is supposed to provide valid FDs to this process. |
| 265 | Ok(unsafe { File::from_raw_fd(fd) }) |
| 266 | } |
| 267 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 268 | fn parse_arg_ro_fds(arg: &str) -> Result<(i32, FdConfig)> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 269 | let result: Result<Vec<i32>, _> = arg.split(':').map(|x| x.parse::<i32>()).collect(); |
| 270 | let fds = result?; |
| 271 | if fds.len() > 3 { |
| 272 | bail!("Too many options: {}", arg); |
| 273 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 274 | Ok(( |
| 275 | fds[0], |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 276 | FdConfig::Readonly { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 277 | file: fd_to_file(fds[0])?, |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 278 | // Alternative Merkle tree, if provided |
| 279 | alt_merkle_tree: fds.get(1).map(|fd| fd_to_file(*fd)).transpose()?, |
| 280 | // Alternative signature, if provided |
| 281 | alt_signature: fds.get(2).map(|fd| fd_to_file(*fd)).transpose()?, |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 282 | }, |
| 283 | )) |
| 284 | } |
| 285 | |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 286 | fn parse_arg_rw_fds(arg: &str) -> Result<(i32, FdConfig)> { |
| 287 | let fd = arg.parse::<i32>()?; |
| 288 | let file = fd_to_file(fd)?; |
| 289 | if file.metadata()?.len() > 0 { |
| 290 | bail!("File is expected to be empty"); |
| 291 | } |
| 292 | Ok((fd, FdConfig::ReadWrite(file))) |
| 293 | } |
| 294 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 295 | fn parse_args() -> Result<BTreeMap<i32, FdConfig>> { |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 296 | #[rustfmt::skip] |
| 297 | let matches = clap::App::new("fd_server") |
| 298 | .arg(clap::Arg::with_name("ro-fds") |
| 299 | .long("ro-fds") |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 300 | .multiple(true) |
| 301 | .number_of_values(1)) |
| 302 | .arg(clap::Arg::with_name("rw-fds") |
| 303 | .long("rw-fds") |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 304 | .multiple(true) |
| 305 | .number_of_values(1)) |
| 306 | .get_matches(); |
| 307 | |
| 308 | let mut fd_pool = BTreeMap::new(); |
| 309 | if let Some(args) = matches.values_of("ro-fds") { |
| 310 | for arg in args { |
| 311 | let (fd, config) = parse_arg_ro_fds(arg)?; |
| 312 | fd_pool.insert(fd, config); |
| 313 | } |
| 314 | } |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 315 | if let Some(args) = matches.values_of("rw-fds") { |
| 316 | for arg in args { |
| 317 | let (fd, config) = parse_arg_rw_fds(arg)?; |
| 318 | fd_pool.insert(fd, config); |
| 319 | } |
| 320 | } |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 321 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 322 | Ok(fd_pool) |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | fn main() -> Result<()> { |
Victor Hsieh | 60acfd3 | 2021-02-23 13:08:13 -0800 | [diff] [blame] | 326 | android_logger::init_once( |
| 327 | android_logger::Config::default().with_tag("fd_server").with_min_level(log::Level::Debug), |
| 328 | ); |
| 329 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 330 | let fd_pool = parse_args()?; |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 331 | |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 332 | let mut service = FdService::new_binder(fd_pool).as_binder(); |
| 333 | debug!("fd_server is starting as a rpc service."); |
| 334 | // SAFETY: Service ownership is transferring to the server and won't be valid afterward. |
| 335 | // Plus the binder objects are threadsafe. |
| 336 | let retval = unsafe { |
| 337 | binder_rpc_unstable_bindgen::RunRpcServer( |
| 338 | service.as_native_mut() as *mut binder_rpc_unstable_bindgen::AIBinder, |
| 339 | RPC_SERVICE_PORT, |
| 340 | ) |
| 341 | }; |
| 342 | if retval { |
| 343 | debug!("RPC server has shut down gracefully"); |
| 344 | Ok(()) |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 345 | } else { |
Victor Hsieh | 1a8cd04 | 2021-09-03 16:29:45 -0700 | [diff] [blame] | 346 | bail!("Premature termination of RPC server"); |
Victor Hsieh | 2445e33 | 2021-06-04 16:44:53 -0700 | [diff] [blame] | 347 | } |
Victor Hsieh | 42cc776 | 2021-01-25 16:44:19 -0800 | [diff] [blame] | 348 | } |