| 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/IProcessInfoService.h> | 
|  | 25 | #include <binder/IServiceManager.h> | 
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> | 
| Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | ProcessInfo::ProcessInfo() {} | 
|  | 31 |  | 
|  | 32 | bool ProcessInfo::getPriority(int pid, int* priority) { | 
|  | 33 | sp<IBinder> binder = defaultServiceManager()->getService(String16("processinfo")); | 
|  | 34 | sp<IProcessInfoService> service = interface_cast<IProcessInfoService>(binder); | 
|  | 35 |  | 
|  | 36 | size_t length = 1; | 
| Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 37 | int32_t state; | 
|  | 38 | static const int32_t INVALID_ADJ = -10000; | 
|  | 39 | static const int32_t NATIVE_ADJ = -1000; | 
|  | 40 | int32_t score = INVALID_ADJ; | 
|  | 41 | status_t err = service->getProcessStatesAndOomScoresFromPids(length, &pid, &state, &score); | 
| Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 42 | if (err != OK) { | 
| Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 43 | ALOGE("getProcessStatesAndOomScoresFromPids failed"); | 
| Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 44 | return false; | 
|  | 45 | } | 
| Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 46 | ALOGV("pid %d state %d score %d", pid, state, score); | 
|  | 47 | if (score <= NATIVE_ADJ) { | 
|  | 48 | ALOGE("pid %d invalid OOM adjustments value %d", pid, score); | 
| Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 49 | return false; | 
|  | 50 | } | 
|  | 51 |  | 
| Ronghua Wu | 221761b | 2015-12-02 11:21:37 -0800 | [diff] [blame] | 52 | // Use OOM adjustments value as the priority. Lower the value, higher the priority. | 
|  | 53 | *priority = score; | 
| Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 54 | return true; | 
|  | 55 | } | 
|  | 56 |  | 
| Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 57 | bool ProcessInfo::isValidPid(int pid) { | 
|  | 58 | int callingPid = IPCThreadState::self()->getCallingPid(); | 
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 59 | int callingUid = IPCThreadState::self()->getCallingUid(); | 
| Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 60 | // Trust it if this is called from the same process otherwise pid has to match the calling pid. | 
| Robert Shih | c3af31b | 2019-09-20 21:45:01 -0700 | [diff] [blame] | 61 | return (callingPid == getpid()) || (callingPid == pid) || (callingUid == AID_MEDIA); | 
| Ronghua Wu | d11c43a | 2016-01-27 16:26:12 -0800 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Ronghua Wu | 14bcaca | 2015-03-16 11:24:30 -0700 | [diff] [blame] | 64 | ProcessInfo::~ProcessInfo() {} | 
|  | 65 |  | 
|  | 66 | }  // namespace android |