PermissionController.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <mutex>
  17. #include <binder/PermissionController.h>
  18. #include <binder/Binder.h>
  19. #include <binder/IServiceManager.h>
  20. #include <utils/SystemClock.h>
  21. namespace android {
  22. PermissionController::PermissionController()
  23. {
  24. }
  25. sp<IPermissionController> PermissionController::getService()
  26. {
  27. std::lock_guard<Mutex> scoped_lock(mLock);
  28. int64_t startTime = 0;
  29. sp<IPermissionController> service = mService;
  30. while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
  31. sp<IBinder> binder = defaultServiceManager()->checkService(String16("permission"));
  32. if (binder == nullptr) {
  33. // Wait for the activity service to come back...
  34. if (startTime == 0) {
  35. startTime = uptimeMillis();
  36. ALOGI("Waiting for permission service");
  37. } else if ((uptimeMillis() - startTime) > 10000) {
  38. ALOGW("Waiting too long for permission service, giving up");
  39. service = nullptr;
  40. break;
  41. }
  42. sleep(1);
  43. } else {
  44. service = interface_cast<IPermissionController>(binder);
  45. mService = service;
  46. }
  47. }
  48. return service;
  49. }
  50. bool PermissionController::checkPermission(const String16& permission, int32_t pid, int32_t uid)
  51. {
  52. sp<IPermissionController> service = getService();
  53. return service != nullptr ? service->checkPermission(permission, pid, uid) : false;
  54. }
  55. int32_t PermissionController::noteOp(const String16& op, int32_t uid, const String16& packageName)
  56. {
  57. sp<IPermissionController> service = getService();
  58. return service != nullptr ? service->noteOp(op, uid, packageName) : MODE_ERRORED;
  59. }
  60. void PermissionController::getPackagesForUid(const uid_t uid, Vector<String16> &packages)
  61. {
  62. sp<IPermissionController> service = getService();
  63. if (service != nullptr) {
  64. service->getPackagesForUid(uid, packages);
  65. }
  66. }
  67. bool PermissionController::isRuntimePermission(const String16& permission)
  68. {
  69. sp<IPermissionController> service = getService();
  70. return service != nullptr ? service->isRuntimePermission(permission) : false;
  71. }
  72. int PermissionController::getPackageUid(const String16& package, int flags)
  73. {
  74. sp<IPermissionController> service = getService();
  75. return service != nullptr ? service->getPackageUid(package, flags) : -1;
  76. }
  77. }; // namespace android