Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "ProcessInfo" |
| 19 | #include <utils/Log.h> |
| 20 | |
Atneya Nair | f5b6851 | 2022-05-23 20:02:49 -0400 | [diff] [blame] | 21 | #include <mediautils/ProcessInfo.h> |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 22 | |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 24 | #include <binder/IServiceManager.h> |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 25 | #include <private/android_filesystem_config.h> |
Steven Moreland | 886d732 | 2021-04-02 04:19:45 +0000 | [diff] [blame] | 26 | #include <processinfo/IProcessInfoService.h> |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 30 | static constexpr int32_t INVALID_ADJ = -10000; |
| 31 | static constexpr int32_t NATIVE_ADJ = -1000; |
| 32 | |
Girish | bb36184 | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 33 | /* Make sure this matches with ActivityManager::PROCESS_STATE_NONEXISTENT |
| 34 | * #include <binder/ActivityManager.h> |
| 35 | * using ActivityManager::PROCESS_STATE_NONEXISTENT; |
| 36 | */ |
| 37 | static constexpr int32_t PROCESS_STATE_NONEXISTENT = 20; |
| 38 | |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 39 | ProcessInfo::ProcessInfo() {} |
| 40 | |
Girish | bb36184 | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 41 | /* |
| 42 | * Checks whether the list of processes with given pids exist or not. |
| 43 | * |
| 44 | * Arguments: |
| 45 | * - pids (input): List of pids for which to check whether they are Existent or not. |
| 46 | * - existent (output): boolean vector corresponds to Existent state of each pids. |
| 47 | * |
| 48 | * On successful return: |
| 49 | * - existent[i] true corresponds to pids[i] still active and |
| 50 | * - existent[i] false corresponds to pids[i] already terminated (Nonexistent) |
| 51 | * On unsuccessful return, the output argument existent is invalid. |
| 52 | */ |
| 53 | bool ProcessInfo::checkProcessExistent(const std::vector<int32_t>& pids, |
| 54 | std::vector<bool>* existent) { |
| 55 | sp<IBinder> binder = defaultServiceManager()->waitForService(String16("processinfo")); |
| 56 | sp<IProcessInfoService> service = interface_cast<IProcessInfoService>(binder); |
| 57 | |
| 58 | // Get the process state of the applications managed/tracked by the ActivityManagerService. |
| 59 | // Don't have to look into the native processes. |
| 60 | // If we really need the state of native process, then we can use ==> mOverrideMap |
| 61 | size_t count = pids.size(); |
| 62 | std::vector<int32_t> states(count, PROCESS_STATE_NONEXISTENT); |
| 63 | status_t err = service->getProcessStatesFromPids(count, |
| 64 | const_cast<int32_t*>(pids.data()), |
| 65 | states.data()); |
| 66 | if (err != OK) { |
| 67 | ALOGE("%s: IProcessInfoService::getProcessStatesFromPids failed with %d", |
| 68 | __func__, err); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | existent->clear(); |
| 73 | for (size_t index = 0; index < states.size(); index++) { |
| 74 | // If this process is not tracked by ActivityManagerService, look for overrides. |
| 75 | if (states[index] == PROCESS_STATE_NONEXISTENT) { |
| 76 | std::scoped_lock lock{mOverrideLock}; |
| 77 | std::map<int, ProcessInfoOverride>::iterator it = |
| 78 | mOverrideMap.find(pids[index]); |
| 79 | if (it != mOverrideMap.end()) { |
| 80 | states[index] = it->second.procState; |
| 81 | } |
| 82 | } |
| 83 | existent->push_back(states[index] != PROCESS_STATE_NONEXISTENT); |
| 84 | } |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 89 | bool ProcessInfo::getPriority(int pid, int* priority) { |
Girish | bb36184 | 2023-02-17 00:36:29 +0000 | [diff] [blame] | 90 | sp<IBinder> binder = defaultServiceManager()->waitForService(String16("processinfo")); |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 91 | sp<IProcessInfoService> service = interface_cast<IProcessInfoService>(binder); |
| 92 | |
| 93 | size_t length = 1; |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 94 | int32_t state; |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 95 | int32_t score = INVALID_ADJ; |
| 96 | status_t err = service->getProcessStatesAndOomScoresFromPids(length, &pid, &state, &score); |
Girish | 9128e24 | 2022-11-23 20:52:29 +0000 | [diff] [blame] | 97 | ALOGV("%s: pid:%d state:%d score:%d err:%d", __FUNCTION__, pid, state, score, err); |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 98 | if (err != OK) { |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 99 | ALOGE("getProcessStatesAndOomScoresFromPids failed"); |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 102 | if (score <= NATIVE_ADJ) { |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 103 | std::scoped_lock lock{mOverrideLock}; |
| 104 | |
| 105 | // If this process if not tracked by ActivityManagerService, look for overrides. |
| 106 | auto it = mOverrideMap.find(pid); |
| 107 | if (it != mOverrideMap.end()) { |
| 108 | ALOGI("pid %d invalid OOM score %d, override to %d", pid, score, it->second.oomScore); |
| 109 | score = it->second.oomScore; |
| 110 | } else { |
| 111 | ALOGE("pid %d invalid OOM score %d", pid, score); |
| 112 | return false; |
| 113 | } |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 116 | // Use OOM adjustments value as the priority. Lower the value, higher the priority. |
| 117 | *priority = score; |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 121 | bool ProcessInfo::isPidTrusted(int pid) { |
| 122 | return isPidUidTrusted(pid, -1); |
| 123 | } |
| 124 | |
| 125 | bool ProcessInfo::isPidUidTrusted(int pid, int uid) { |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 126 | int callingPid = IPCThreadState::self()->getCallingPid(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 127 | int callingUid = IPCThreadState::self()->getCallingUid(); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 128 | // Always trust when the caller is acting on their own behalf. |
| 129 | if (pid == callingPid && (uid == callingUid || uid == -1)) { // UID can be optional |
| 130 | return true; |
| 131 | } |
| 132 | // Implicitly trust when the caller is our own process. |
| 133 | if (callingPid == getpid()) { |
| 134 | return true; |
| 135 | } |
| 136 | // Implicitly trust when a media process is calling. |
| 137 | if (callingUid == AID_MEDIA) { |
| 138 | return true; |
| 139 | } |
| 140 | // Otherwise, allow the caller to act as another process when the caller has permissions. |
| 141 | return checkCallingPermission(String16("android.permission.MEDIA_RESOURCE_OVERRIDE_PID")); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 144 | bool ProcessInfo::overrideProcessInfo(int pid, int procState, int oomScore) { |
| 145 | std::scoped_lock lock{mOverrideLock}; |
| 146 | |
| 147 | mOverrideMap.erase(pid); |
| 148 | |
| 149 | // Disable the override if oomScore is set to NATIVE_ADJ or below. |
| 150 | if (oomScore <= NATIVE_ADJ) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | mOverrideMap.emplace(pid, ProcessInfoOverride{procState, oomScore}); |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | void ProcessInfo::removeProcessInfoOverride(int pid) { |
| 159 | std::scoped_lock lock{mOverrideLock}; |
| 160 | |
| 161 | mOverrideMap.erase(pid); |
| 162 | } |
| 163 | |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 164 | ProcessInfo::~ProcessInfo() {} |
| 165 | |
| 166 | } // namespace android |