1 /*
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
17 #include <mutex>
18 #include <binder/AppOpsManager.h>
19 #include <binder/Binder.h>
20 #include <binder/IServiceManager.h>
21
22 #include <utils/SystemClock.h>
23
24 #include <sys/types.h>
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29 #define LOG_TAG "AppOpsManager"
30
31 namespace android {
32
getClientId()33 static const sp<IBinder>& getClientId() {
34 static pthread_mutex_t gClientIdMutex = PTHREAD_MUTEX_INITIALIZER;
35 static sp<IBinder> gClientId;
36
37 pthread_mutex_lock(&gClientIdMutex);
38 if (gClientId == nullptr) {
39 gClientId = new BBinder();
40 }
41 pthread_mutex_unlock(&gClientIdMutex);
42 return gClientId;
43 }
44
AppOpsManager()45 AppOpsManager::AppOpsManager()
46 {
47 }
48
getService()49 sp<IAppOpsService> AppOpsManager::getService()
50 {
51 static String16 _appops("appops");
52
53 std::lock_guard<Mutex> scoped_lock(mLock);
54 int64_t startTime = 0;
55 sp<IAppOpsService> service = mService;
56 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
57 sp<IBinder> binder = defaultServiceManager()->checkService(_appops);
58 if (binder == nullptr) {
59 // Wait for the app ops service to come back...
60 if (startTime == 0) {
61 startTime = uptimeMillis();
62 ALOGI("Waiting for app ops service");
63 } else if ((uptimeMillis()-startTime) > 10000) {
64 ALOGW("Waiting too long for app ops service, giving up");
65 service = nullptr;
66 break;
67 }
68 sleep(1);
69 } else {
70 service = interface_cast<IAppOpsService>(binder);
71 mService = service;
72 }
73 }
74 return service;
75 }
76
checkOp(int32_t op,int32_t uid,const String16 & callingPackage)77 int32_t AppOpsManager::checkOp(int32_t op, int32_t uid, const String16& callingPackage)
78 {
79 sp<IAppOpsService> service = getService();
80 return service != nullptr
81 ? service->checkOperation(op, uid, callingPackage)
82 : AppOpsManager::MODE_IGNORED;
83 }
84
checkAudioOpNoThrow(int32_t op,int32_t usage,int32_t uid,const String16 & callingPackage)85 int32_t AppOpsManager::checkAudioOpNoThrow(int32_t op, int32_t usage, int32_t uid,
86 const String16& callingPackage) {
87 sp<IAppOpsService> service = getService();
88 return service != nullptr
89 ? service->checkAudioOperation(op, usage, uid, callingPackage)
90 : AppOpsManager::MODE_IGNORED;
91 }
92
noteOp(int32_t op,int32_t uid,const String16 & callingPackage)93 int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage) {
94 return noteOp(op, uid, callingPackage, std::unique_ptr<String16>(),
95 String16("Legacy AppOpsManager.noteOp call"));
96 }
97
noteOp(int32_t op,int32_t uid,const String16 & callingPackage,const std::unique_ptr<String16> & attributionTag,const String16 & message)98 int32_t AppOpsManager::noteOp(int32_t op, int32_t uid, const String16& callingPackage,
99 const std::unique_ptr<String16>& attributionTag, const String16& message) {
100 sp<IAppOpsService> service = getService();
101 int32_t mode = service != nullptr
102 ? service->noteOperation(op, uid, callingPackage, attributionTag,
103 shouldCollectNotes(op), message)
104 : AppOpsManager::MODE_IGNORED;
105
106 return mode;
107 }
108
startOpNoThrow(int32_t op,int32_t uid,const String16 & callingPackage,bool startIfModeDefault)109 int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
110 bool startIfModeDefault) {
111 return startOpNoThrow(op, uid, callingPackage, startIfModeDefault, std::unique_ptr<String16>(),
112 String16("Legacy AppOpsManager.startOpNoThrow call"));
113 }
114
startOpNoThrow(int32_t op,int32_t uid,const String16 & callingPackage,bool startIfModeDefault,const std::unique_ptr<String16> & attributionTag,const String16 & message)115 int32_t AppOpsManager::startOpNoThrow(int32_t op, int32_t uid, const String16& callingPackage,
116 bool startIfModeDefault, const std::unique_ptr<String16>& attributionTag,
117 const String16& message) {
118 sp<IAppOpsService> service = getService();
119 int32_t mode = service != nullptr
120 ? service->startOperation(getClientId(), op, uid, callingPackage,
121 attributionTag, startIfModeDefault, shouldCollectNotes(op), message)
122 : AppOpsManager::MODE_IGNORED;
123
124 return mode;
125 }
126
finishOp(int32_t op,int32_t uid,const String16 & callingPackage)127 void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage) {
128 finishOp(op, uid, callingPackage, std::unique_ptr<String16>());
129 }
130
finishOp(int32_t op,int32_t uid,const String16 & callingPackage,const std::unique_ptr<String16> & attributionTag)131 void AppOpsManager::finishOp(int32_t op, int32_t uid, const String16& callingPackage,
132 const std::unique_ptr<String16>& attributionTag) {
133 sp<IAppOpsService> service = getService();
134 if (service != nullptr) {
135 service->finishOperation(getClientId(), op, uid, callingPackage, attributionTag);
136 }
137 }
138
startWatchingMode(int32_t op,const String16 & packageName,const sp<IAppOpsCallback> & callback)139 void AppOpsManager::startWatchingMode(int32_t op, const String16& packageName,
140 const sp<IAppOpsCallback>& callback) {
141 sp<IAppOpsService> service = getService();
142 if (service != nullptr) {
143 service->startWatchingMode(op, packageName, callback);
144 }
145 }
146
stopWatchingMode(const sp<IAppOpsCallback> & callback)147 void AppOpsManager::stopWatchingMode(const sp<IAppOpsCallback>& callback) {
148 sp<IAppOpsService> service = getService();
149 if (service != nullptr) {
150 service->stopWatchingMode(callback);
151 }
152 }
153
permissionToOpCode(const String16 & permission)154 int32_t AppOpsManager::permissionToOpCode(const String16& permission) {
155 sp<IAppOpsService> service = getService();
156 if (service != nullptr) {
157 return service->permissionToOpCode(permission);
158 }
159 return -1;
160 }
161
setCameraAudioRestriction(int32_t mode)162 void AppOpsManager::setCameraAudioRestriction(int32_t mode) {
163 sp<IAppOpsService> service = getService();
164 if (service != nullptr) {
165 service->setCameraAudioRestriction(mode);
166 }
167 }
168
169 // check it the appops needs to be collected and cache result
shouldCollectNotes(int32_t opcode)170 bool AppOpsManager::shouldCollectNotes(int32_t opcode) {
171 // Whether an appop should be collected: 0 == not initialized, 1 == don't note, 2 == note
172 static uint8_t appOpsToNote[AppOpsManager::_NUM_OP] = {0};
173
174 if (appOpsToNote[opcode] == 0) {
175 if (getService()->shouldCollectNotes(opcode)) {
176 appOpsToNote[opcode] = 2;
177 } else {
178 appOpsToNote[opcode] = 1;
179 }
180 }
181
182 return appOpsToNote[opcode] == 2;
183 }
184
185 } // namespace android
186