| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2018 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 | #include <mutex> | 
 | 18 | #include <binder/PermissionController.h> | 
 | 19 | #include <binder/Binder.h> | 
 | 20 | #include <binder/IServiceManager.h> | 
 | 21 |  | 
 | 22 | #include <utils/SystemClock.h> | 
 | 23 |  | 
 | 24 | namespace android { | 
 | 25 |  | 
 | 26 | PermissionController::PermissionController() | 
 | 27 | { | 
 | 28 | } | 
 | 29 |  | 
 | 30 | sp<IPermissionController> PermissionController::getService() | 
 | 31 | { | 
 | 32 |     std::lock_guard<Mutex> scoped_lock(mLock); | 
 | 33 |     int64_t startTime = 0; | 
 | 34 |     sp<IPermissionController> service = mService; | 
 | 35 |     while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { | 
 | 36 |         sp<IBinder> binder = defaultServiceManager()->checkService(String16("permission")); | 
 | 37 |         if (binder == nullptr) { | 
 | 38 |             // Wait for the activity service to come back... | 
 | 39 |             if (startTime == 0) { | 
 | 40 |                 startTime = uptimeMillis(); | 
 | 41 |                 ALOGI("Waiting for permission service"); | 
 | 42 |             } else if ((uptimeMillis() - startTime) > 10000) { | 
 | 43 |                 ALOGW("Waiting too long for permission service, giving up"); | 
| Yi Kong | 8ed24ce | 2018-06-08 17:15:29 -0700 | [diff] [blame] | 44 |                 service = nullptr; | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 45 |                 break; | 
 | 46 |             } | 
 | 47 |             sleep(1); | 
 | 48 |         } else { | 
 | 49 |             service = interface_cast<IPermissionController>(binder); | 
 | 50 |             mService = service; | 
 | 51 |         } | 
 | 52 |     } | 
 | 53 |     return service; | 
 | 54 | } | 
 | 55 |  | 
 | 56 | bool PermissionController::checkPermission(const String16& permission, int32_t pid, int32_t uid) | 
 | 57 | { | 
 | 58 |     sp<IPermissionController> service = getService(); | 
| Yi Kong | 8ed24ce | 2018-06-08 17:15:29 -0700 | [diff] [blame] | 59 |     return service != nullptr ? service->checkPermission(permission, pid, uid) : false; | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 60 | } | 
 | 61 |  | 
| Jeff Sharkey | 7afcb3f | 2018-04-09 12:58:40 -0600 | [diff] [blame] | 62 | int32_t PermissionController::noteOp(const String16& op, int32_t uid, const String16& packageName) | 
 | 63 | { | 
 | 64 |     sp<IPermissionController> service = getService(); | 
| Yi Kong | 8ed24ce | 2018-06-08 17:15:29 -0700 | [diff] [blame] | 65 |     return service != nullptr ? service->noteOp(op, uid, packageName) : MODE_ERRORED; | 
| Jeff Sharkey | 7afcb3f | 2018-04-09 12:58:40 -0600 | [diff] [blame] | 66 | } | 
 | 67 |  | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 68 | void PermissionController::getPackagesForUid(const uid_t uid, Vector<String16> &packages) | 
 | 69 | { | 
 | 70 |     sp<IPermissionController> service = getService(); | 
 | 71 |     if (service != nullptr) { | 
 | 72 |         service->getPackagesForUid(uid, packages); | 
 | 73 |     } | 
 | 74 | } | 
 | 75 |  | 
 | 76 | bool PermissionController::isRuntimePermission(const String16& permission) | 
 | 77 | { | 
 | 78 |     sp<IPermissionController> service = getService(); | 
 | 79 |     return service != nullptr ? service->isRuntimePermission(permission) : false; | 
 | 80 | } | 
 | 81 |  | 
 | 82 | int PermissionController::getPackageUid(const String16& package, int flags) | 
 | 83 | { | 
 | 84 |     sp<IPermissionController> service = getService(); | 
 | 85 |     return service != nullptr ? service->getPackageUid(package, flags) : -1; | 
 | 86 | } | 
 | 87 |  | 
 | 88 | }; // namespace android |