| Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2020 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 | use crate::sys; | 
|  | 18 |  | 
|  | 19 | use libc::{pid_t, uid_t}; | 
|  | 20 |  | 
|  | 21 | /// Static utility functions to manage Binder process state. | 
|  | 22 | pub struct ProcessState; | 
|  | 23 |  | 
|  | 24 | impl ProcessState { | 
|  | 25 | /// Start the Binder IPC thread pool | 
|  | 26 | pub fn start_thread_pool() { | 
|  | 27 | unsafe { | 
|  | 28 | // Safety: Safe FFI | 
|  | 29 | sys::ABinderProcess_startThreadPool(); | 
|  | 30 | } | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | /// Set the maximum number of threads that can be started in the threadpool. | 
|  | 34 | /// | 
|  | 35 | /// By default, after startThreadPool is called, this is 15. If it is called | 
|  | 36 | /// additional times, it will only prevent the kernel from starting new | 
|  | 37 | /// threads and will not delete already existing threads. | 
|  | 38 | pub fn set_thread_pool_max_thread_count(num_threads: u32) { | 
|  | 39 | unsafe { | 
|  | 40 | // Safety: Safe FFI | 
|  | 41 | sys::ABinderProcess_setThreadPoolMaxThreadCount(num_threads); | 
|  | 42 | } | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | /// Block on the Binder IPC thread pool | 
|  | 46 | pub fn join_thread_pool() { | 
|  | 47 | unsafe { | 
|  | 48 | // Safety: Safe FFI | 
|  | 49 | sys::ABinderProcess_joinThreadPool(); | 
|  | 50 | } | 
|  | 51 | } | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | /// Static utility functions to manage Binder thread state. | 
|  | 55 | pub struct ThreadState; | 
|  | 56 |  | 
|  | 57 | impl ThreadState { | 
|  | 58 | /// This returns the calling UID assuming that this thread is called from a | 
|  | 59 | /// thread that is processing a binder transaction (for instance, in the | 
|  | 60 | /// implementation of | 
|  | 61 | /// [`Remotable::on_transact`](crate::Remotable::on_transact)). | 
|  | 62 | /// | 
|  | 63 | /// This can be used with higher-level system services to determine the | 
|  | 64 | /// caller's identity and check permissions. | 
|  | 65 | /// | 
|  | 66 | /// Available since API level 29. | 
|  | 67 | /// | 
|  | 68 | /// \return calling uid or the current process's UID if this thread isn't | 
|  | 69 | /// processing a transaction. | 
|  | 70 | pub fn get_calling_uid() -> uid_t { | 
|  | 71 | unsafe { | 
|  | 72 | // Safety: Safe FFI | 
|  | 73 | sys::AIBinder_getCallingUid() | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | /// This returns the calling PID assuming that this thread is called from a | 
|  | 78 | /// thread that is processing a binder transaction (for instance, in the | 
|  | 79 | /// implementation of | 
|  | 80 | /// [`Remotable::on_transact`](crate::Remotable::on_transact)). | 
|  | 81 | /// | 
|  | 82 | /// This can be used with higher-level system services to determine the | 
|  | 83 | /// caller's identity and check permissions. However, when doing this, one | 
|  | 84 | /// should be aware of possible TOCTOU problems when the calling process | 
|  | 85 | /// dies and is replaced with another process with elevated permissions and | 
|  | 86 | /// the same PID. | 
|  | 87 | /// | 
|  | 88 | /// Available since API level 29. | 
|  | 89 | /// | 
|  | 90 | /// \return calling pid or the current process's PID if this thread isn't | 
|  | 91 | /// processing a transaction. | 
|  | 92 | /// | 
|  | 93 | /// If the transaction being processed is a oneway transaction, then this | 
|  | 94 | /// method will return 0. | 
|  | 95 | pub fn get_calling_pid() -> pid_t { | 
|  | 96 | unsafe { | 
|  | 97 | // Safety: Safe FFI | 
|  | 98 | sys::AIBinder_getCallingPid() | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 | } |