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 | |
| 21 | #include <media/stagefright/ProcessInfo.h> |
| 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 | |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 33 | ProcessInfo::ProcessInfo() {} |
| 34 | |
| 35 | bool ProcessInfo::getPriority(int pid, int* priority) { |
| 36 | sp<IBinder> binder = defaultServiceManager()->getService(String16("processinfo")); |
| 37 | sp<IProcessInfoService> service = interface_cast<IProcessInfoService>(binder); |
| 38 | |
| 39 | size_t length = 1; |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 40 | int32_t state; |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 41 | int32_t score = INVALID_ADJ; |
| 42 | status_t err = service->getProcessStatesAndOomScoresFromPids(length, &pid, &state, &score); |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 43 | if (err != OK) { |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 44 | ALOGE("getProcessStatesAndOomScoresFromPids failed"); |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 45 | return false; |
| 46 | } |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 47 | ALOGV("pid %d state %d score %d", pid, state, score); |
| 48 | if (score <= NATIVE_ADJ) { |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 49 | std::scoped_lock lock{mOverrideLock}; |
| 50 | |
| 51 | // If this process if not tracked by ActivityManagerService, look for overrides. |
| 52 | auto it = mOverrideMap.find(pid); |
| 53 | if (it != mOverrideMap.end()) { |
| 54 | ALOGI("pid %d invalid OOM score %d, override to %d", pid, score, it->second.oomScore); |
| 55 | score = it->second.oomScore; |
| 56 | } else { |
| 57 | ALOGE("pid %d invalid OOM score %d", pid, score); |
| 58 | return false; |
| 59 | } |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 62 | // Use OOM adjustments value as the priority. Lower the value, higher the priority. |
| 63 | *priority = score; |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 67 | bool ProcessInfo::isPidTrusted(int pid) { |
| 68 | return isPidUidTrusted(pid, -1); |
| 69 | } |
| 70 | |
| 71 | bool ProcessInfo::isPidUidTrusted(int pid, int uid) { |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 72 | int callingPid = IPCThreadState::self()->getCallingPid(); |
Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 73 | int callingUid = IPCThreadState::self()->getCallingUid(); |
Brian Lindahl | cf3bafb | 2022-01-27 14:21:38 +0100 | [diff] [blame] | 74 | // Always trust when the caller is acting on their own behalf. |
| 75 | if (pid == callingPid && (uid == callingUid || uid == -1)) { // UID can be optional |
| 76 | return true; |
| 77 | } |
| 78 | // Implicitly trust when the caller is our own process. |
| 79 | if (callingPid == getpid()) { |
| 80 | return true; |
| 81 | } |
| 82 | // Implicitly trust when a media process is calling. |
| 83 | if (callingUid == AID_MEDIA) { |
| 84 | return true; |
| 85 | } |
| 86 | // Otherwise, allow the caller to act as another process when the caller has permissions. |
| 87 | return checkCallingPermission(String16("android.permission.MEDIA_RESOURCE_OVERRIDE_PID")); |
Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Chong Zhang | 97d367b | 2020-09-16 12:53:14 -0700 | [diff] [blame] | 90 | bool ProcessInfo::overrideProcessInfo(int pid, int procState, int oomScore) { |
| 91 | std::scoped_lock lock{mOverrideLock}; |
| 92 | |
| 93 | mOverrideMap.erase(pid); |
| 94 | |
| 95 | // Disable the override if oomScore is set to NATIVE_ADJ or below. |
| 96 | if (oomScore <= NATIVE_ADJ) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | mOverrideMap.emplace(pid, ProcessInfoOverride{procState, oomScore}); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | void ProcessInfo::removeProcessInfoOverride(int pid) { |
| 105 | std::scoped_lock lock{mOverrideLock}; |
| 106 | |
| 107 | mOverrideMap.erase(pid); |
| 108 | } |
| 109 | |
Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 110 | ProcessInfo::~ProcessInfo() {} |
| 111 | |
| 112 | } // namespace android |