blob: a494e223920b7a39dd41564dc5abd3d0eaf944be [file] [log] [blame]
Dianne Hackborn5da5ca52013-02-12 15:12:21 -08001/*
2 * Copyright (C) 2013 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
Christopher Wiley5975c002016-02-12 15:41:08 -080017#include <mutex>
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080018#include <binder/AppOpsManager.h>
Dianne Hackborn913b63d2013-07-17 17:26:15 -070019#include <binder/Binder.h>
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080020#include <binder/IServiceManager.h>
21
22#include <utils/SystemClock.h>
23
24namespace android {
25
Christopher Wiley8ed42702016-02-05 09:08:23 -080026namespace {
27
28#if defined(__BRILLO__)
29// Because Brillo has no application model, security policy is managed
30// statically (at build time) with SELinux controls.
31// As a consequence, it also never runs the AppOpsManager service.
32const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_ALLOWED;
33#else
34const int APP_OPS_MANAGER_UNAVAILABLE_MODE = AppOpsManager::MODE_IGNORED;
35#endif // defined(__BRILLO__)
36
37} // namespace
38
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080039static String16 _appops("appops");
Dianne Hackborn913b63d2013-07-17 17:26:15 -070040static pthread_mutex_t gTokenMutex = PTHREAD_MUTEX_INITIALIZER;
41static sp<IBinder> gToken;
42
43static const sp<IBinder>& getToken(const sp<IAppOpsService>& service) {
44 pthread_mutex_lock(&gTokenMutex);
Yi Kongfdd8da92018-06-07 17:52:27 -070045 if (gToken == nullptr || gToken->pingBinder() != NO_ERROR) {
Dianne Hackborn913b63d2013-07-17 17:26:15 -070046 gToken = service->getToken(new BBinder());
47 }
Zhijun He20d03802013-07-22 17:09:35 -070048 pthread_mutex_unlock(&gTokenMutex);
Dianne Hackborn913b63d2013-07-17 17:26:15 -070049 return gToken;
50}
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080051
52AppOpsManager::AppOpsManager()
53{
54}
55
Christopher Wiley8ed42702016-02-05 09:08:23 -080056#if defined(__BRILLO__)
57// There is no AppOpsService on Brillo
58sp<IAppOpsService> AppOpsManager::getService() { return NULL; }
59#else
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080060sp<IAppOpsService> AppOpsManager::getService()
61{
Christopher Wiley8ed42702016-02-05 09:08:23 -080062
Christopher Wiley5975c002016-02-12 15:41:08 -080063 std::lock_guard<Mutex> scoped_lock(mLock);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080064 int64_t startTime = 0;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080065 sp<IAppOpsService> service = mService;
Yi Kongfdd8da92018-06-07 17:52:27 -070066 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080067 sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
Yi Kongfdd8da92018-06-07 17:52:27 -070068 if (binder == nullptr) {
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080069 // Wait for the app ops service to come back...
70 if (startTime == 0) {
71 startTime = uptimeMillis();
72 ALOGI("Waiting for app ops service");
73 } else if ((uptimeMillis()-startTime) > 10000) {
74 ALOGW("Waiting too long for app ops service, giving up");
Yi Kongfdd8da92018-06-07 17:52:27 -070075 service = nullptr;
Christopher Wiley6dd45522016-02-05 09:06:30 -080076 break;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080077 }
Eino-Ville Talvalae88a85e2013-02-19 12:54:57 -080078 sleep(1);
79 } else {
80 service = interface_cast<IAppOpsService>(binder);
81 mService = service;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080082 }
83 }
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080084 return service;
85}
Christopher Wiley8ed42702016-02-05 09:08:23 -080086#endif // defined(__BRILLO__)
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080087
88int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
89{
90 sp<IAppOpsService> service = getService();
Yi Kongfdd8da92018-06-07 17:52:27 -070091 return service != nullptr
Christopher Wiley8ed42702016-02-05 09:08:23 -080092 ? service->checkOperation(op, uid, callingPackage)
93 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -080094}
95
96int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
97 sp<IAppOpsService> service = getService();
Yi Kongfdd8da92018-06-07 17:52:27 -070098 return service != nullptr
Christopher Wiley8ed42702016-02-05 09:08:23 -080099 ? service->noteOperation(op, uid, callingPackage)
100 : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800101}
102
Svet Ganov616554c2018-02-26 13:27:26 -0800103int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
104 bool startIfModeDefault) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800105 sp<IAppOpsService> service = getService();
Yi Kongfdd8da92018-06-07 17:52:27 -0700106 return service != nullptr
Svet Ganov616554c2018-02-26 13:27:26 -0800107 ? service->startOperation(getToken(service), op, uid, callingPackage,
108 startIfModeDefault) : APP_OPS_MANAGER_UNAVAILABLE_MODE;
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800109}
110
111void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
112 sp<IAppOpsService> service = getService();
Yi Kongfdd8da92018-06-07 17:52:27 -0700113 if (service != nullptr) {
Dianne Hackborn913b63d2013-07-17 17:26:15 -0700114 service->finishOperation(getToken(service), op, uid, callingPackage);
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800115 }
116}
117
118void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
119 const sp<IAppOpsCallback>& callback) {
120 sp<IAppOpsService> service = getService();
Yi Kongfdd8da92018-06-07 17:52:27 -0700121 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800122 service->startWatchingMode(op, packageName, callback);
123 }
124}
125
126void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
127 sp<IAppOpsService> service = getService();
Yi Kongfdd8da92018-06-07 17:52:27 -0700128 if (service != nullptr) {
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800129 service->stopWatchingMode(callback);
130 }
131}
132
Svetoslavb412f6e2015-04-29 16:50:41 -0700133int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
134 sp<IAppOpsService> service = getService();
Yi Kongfdd8da92018-06-07 17:52:27 -0700135 if (service != nullptr) {
Svetoslavb412f6e2015-04-29 16:50:41 -0700136 return service->permissionToOpCode(permission);
137 }
138 return -1;
139}
140
141
Dianne Hackborn5da5ca52013-02-12 15:12:21 -0800142}; // namespace android