blob: a33ed97704b475a5505ef1f4f502169d4ba6e537 [file] [log] [blame]
Glenn Kasten44deb052012-02-05 18:09:08 -08001/*
2 * Copyright (C) 2012 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
Eric Laurent9b11c022018-06-06 19:19:22 -070017#define LOG_TAG "ServiceUtilities"
18
Andy Hunga85efab2019-12-23 11:41:29 -080019#include <audio_utils/clock.h>
Svet Ganovbe71aa22015-04-28 12:06:02 -070020#include <binder/AppOpsManager.h>
Glenn Kasten44deb052012-02-05 18:09:08 -080021#include <binder/IPCThreadState.h>
22#include <binder/IServiceManager.h>
23#include <binder/PermissionCache.h>
Andy Hungab7ef302018-05-15 19:35:29 -070024#include "mediautils/ServiceUtilities.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080025
Kevin Rocard8be94972019-02-22 13:26:25 -080026#include <iterator>
27#include <algorithm>
Andy Hunga85efab2019-12-23 11:41:29 -080028#include <pwd.h>
Kevin Rocard8be94972019-02-22 13:26:25 -080029
Svet Ganovbe71aa22015-04-28 12:06:02 -070030/* When performing permission checks we do not use permission cache for
31 * runtime permissions (protection level dangerous) as they may change at
32 * runtime. All other permissions (protection level normal and dangerous)
33 * can be cached as they never change. Of course all permission checked
34 * here are platform defined.
35 */
36
Glenn Kasten44deb052012-02-05 18:09:08 -080037namespace android {
38
Svet Ganov5b81f552018-03-02 09:21:30 -080039static const String16 sAndroidPermissionRecordAudio("android.permission.RECORD_AUDIO");
Eric Laurent42984412019-05-09 17:57:03 -070040static const String16 sModifyPhoneState("android.permission.MODIFY_PHONE_STATE");
41static const String16 sModifyAudioRouting("android.permission.MODIFY_AUDIO_ROUTING");
Svet Ganov5b81f552018-03-02 09:21:30 -080042
Svet Ganov5b81f552018-03-02 09:21:30 -080043static String16 resolveCallingPackage(PermissionController& permissionController,
44 const String16& opPackageName, uid_t uid) {
45 if (opPackageName.size() > 0) {
46 return opPackageName;
Svet Ganovbe71aa22015-04-28 12:06:02 -070047 }
Svet Ganovbe71aa22015-04-28 12:06:02 -070048 // In some cases the calling code has no access to the package it runs under.
49 // For example, code using the wilhelm framework's OpenSL-ES APIs. In this
50 // case we will get the packages for the calling UID and pick the first one
51 // for attributing the app op. This will work correctly for runtime permissions
52 // as for legacy apps we will toggle the app op for all packages in the UID.
53 // The caveat is that the operation may be attributed to the wrong package and
54 // stats based on app ops may be slightly off.
Svet Ganov5b81f552018-03-02 09:21:30 -080055 Vector<String16> packages;
56 permissionController.getPackagesForUid(uid, packages);
57 if (packages.isEmpty()) {
58 ALOGE("No packages for uid %d", uid);
59 return opPackageName; // empty string
60 }
61 return packages[0];
62}
Svetoslav Ganov599ec462018-03-01 22:19:55 +000063
Svet Ganov5b81f552018-03-02 09:21:30 -080064static bool checkRecordingInternal(const String16& opPackageName, pid_t pid,
65 uid_t uid, bool start) {
Eric Laurent58a0dd82019-10-24 12:42:17 -070066 // Okay to not track in app ops as audio server or media server is us and if
Svet Ganov5b81f552018-03-02 09:21:30 -080067 // device is rooted security model is considered compromised.
Jeffrey Carlyle62cc92b2019-09-17 11:15:15 -070068 // system_server loses its RECORD_AUDIO permission when a secondary
69 // user is active, but it is a core system service so let it through.
70 // TODO(b/141210120): UserManager.DISALLOW_RECORD_AUDIO should not affect system user 0
Eric Laurent58a0dd82019-10-24 12:42:17 -070071 if (isAudioServerOrMediaServerOrSystemServerOrRootUid(uid)) return true;
Svetoslav Ganov599ec462018-03-01 22:19:55 +000072
Svet Ganov5b81f552018-03-02 09:21:30 -080073 // We specify a pid and uid here as mediaserver (aka MediaRecorder or StageFrightRecorder)
74 // may open a record track on behalf of a client. Note that pid may be a tid.
75 // IMPORTANT: DON'T USE PermissionCache - RUNTIME PERMISSIONS CHANGE.
76 PermissionController permissionController;
77 const bool ok = permissionController.checkPermission(sAndroidPermissionRecordAudio, pid, uid);
78 if (!ok) {
79 ALOGE("Request requires %s", String8(sAndroidPermissionRecordAudio).c_str());
80 return false;
81 }
82
83 String16 resolvedOpPackageName = resolveCallingPackage(
84 permissionController, opPackageName, uid);
85 if (resolvedOpPackageName.size() == 0) {
86 return false;
Svet Ganovbe71aa22015-04-28 12:06:02 -070087 }
Svetoslav Ganov599ec462018-03-01 22:19:55 +000088
89 AppOpsManager appOps;
Svet Ganov5b81f552018-03-02 09:21:30 -080090 const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio);
91 if (start) {
92 if (appOps.startOpNoThrow(op, uid, resolvedOpPackageName, /*startIfModeDefault*/ false)
93 != AppOpsManager::MODE_ALLOWED) {
94 ALOGE("Request denied by app op: %d", op);
95 return false;
96 }
97 } else {
Eric Laurent5c5c8a12019-02-15 17:36:00 -080098 if (appOps.checkOp(op, uid, resolvedOpPackageName) != AppOpsManager::MODE_ALLOWED) {
Svet Ganov5b81f552018-03-02 09:21:30 -080099 ALOGE("Request denied by app op: %d", op);
100 return false;
101 }
Svetoslav Ganov599ec462018-03-01 22:19:55 +0000102 }
103
104 return true;
Glenn Kasten44deb052012-02-05 18:09:08 -0800105}
106
Svet Ganov5b81f552018-03-02 09:21:30 -0800107bool recordingAllowed(const String16& opPackageName, pid_t pid, uid_t uid) {
108 return checkRecordingInternal(opPackageName, pid, uid, /*start*/ false);
109}
110
111bool startRecording(const String16& opPackageName, pid_t pid, uid_t uid) {
112 return checkRecordingInternal(opPackageName, pid, uid, /*start*/ true);
113}
114
115void finishRecording(const String16& opPackageName, uid_t uid) {
116 // Okay to not track in app ops as audio server is us and if
117 // device is rooted security model is considered compromised.
Andy Hung4ef19fa2018-05-15 19:35:29 -0700118 if (isAudioServerOrRootUid(uid)) return;
Svet Ganov5b81f552018-03-02 09:21:30 -0800119
120 PermissionController permissionController;
121 String16 resolvedOpPackageName = resolveCallingPackage(
122 permissionController, opPackageName, uid);
123 if (resolvedOpPackageName.size() == 0) {
124 return;
125 }
126
127 AppOpsManager appOps;
128 const int32_t op = appOps.permissionToOpCode(sAndroidPermissionRecordAudio);
129 appOps.finishOp(op, uid, resolvedOpPackageName);
130}
131
Eric Laurentb2379ba2016-05-23 17:42:12 -0700132bool captureAudioOutputAllowed(pid_t pid, uid_t uid) {
Andy Hung4ef19fa2018-05-15 19:35:29 -0700133 if (isAudioServerOrRootUid(uid)) return true;
Jeff Brown893a5642013-08-16 20:19:26 -0700134 static const String16 sCaptureAudioOutput("android.permission.CAPTURE_AUDIO_OUTPUT");
Svet Ganov5b81f552018-03-02 09:21:30 -0800135 bool ok = PermissionCache::checkPermission(sCaptureAudioOutput, pid, uid);
Eric Laurent6ede98f2019-06-11 14:50:30 -0700136 if (!ok) ALOGV("Request requires android.permission.CAPTURE_AUDIO_OUTPUT");
Jeff Brown893a5642013-08-16 20:19:26 -0700137 return ok;
138}
139
Kevin Rocard36b17552019-03-07 18:48:07 -0800140bool captureMediaOutputAllowed(pid_t pid, uid_t uid) {
141 if (isAudioServerOrRootUid(uid)) return true;
142 static const String16 sCaptureMediaOutput("android.permission.CAPTURE_MEDIA_OUTPUT");
143 bool ok = PermissionCache::checkPermission(sCaptureMediaOutput, pid, uid);
144 if (!ok) ALOGE("Request requires android.permission.CAPTURE_MEDIA_OUTPUT");
145 return ok;
146}
147
jiabin68e0df72019-03-18 17:55:35 -0700148bool captureHotwordAllowed(const String16& opPackageName, pid_t pid, uid_t uid) {
Eric Laurent7504b9e2017-08-15 18:17:26 -0700149 // CAPTURE_AUDIO_HOTWORD permission implies RECORD_AUDIO permission
jiabin68e0df72019-03-18 17:55:35 -0700150 bool ok = recordingAllowed(opPackageName, pid, uid);
Eric Laurent7504b9e2017-08-15 18:17:26 -0700151
152 if (ok) {
153 static const String16 sCaptureHotwordAllowed("android.permission.CAPTURE_AUDIO_HOTWORD");
154 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
jiabin68e0df72019-03-18 17:55:35 -0700155 ok = PermissionCache::checkPermission(sCaptureHotwordAllowed, pid, uid);
Eric Laurent7504b9e2017-08-15 18:17:26 -0700156 }
Eric Laurent6ede98f2019-06-11 14:50:30 -0700157 if (!ok) ALOGV("android.permission.CAPTURE_AUDIO_HOTWORD");
Eric Laurent9a54bc22013-09-09 09:08:44 -0700158 return ok;
159}
160
Glenn Kasten44deb052012-02-05 18:09:08 -0800161bool settingsAllowed() {
Andy Hung4ef19fa2018-05-15 19:35:29 -0700162 // given this is a permission check, could this be isAudioServerOrRootUid()?
163 if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true;
Glenn Kasten44deb052012-02-05 18:09:08 -0800164 static const String16 sAudioSettings("android.permission.MODIFY_AUDIO_SETTINGS");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700165 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
166 bool ok = PermissionCache::checkCallingPermission(sAudioSettings);
Glenn Kasten44deb052012-02-05 18:09:08 -0800167 if (!ok) ALOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
168 return ok;
169}
170
Eric Laurent5284ed52014-05-29 14:37:38 -0700171bool modifyAudioRoutingAllowed() {
Eric Laurent8a1095a2019-11-08 14:44:16 -0800172 return modifyAudioRoutingAllowed(
173 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid());
174}
175
176bool modifyAudioRoutingAllowed(pid_t pid, uid_t uid) {
177 if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true;
Svet Ganovbe71aa22015-04-28 12:06:02 -0700178 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
Eric Laurent8a1095a2019-11-08 14:44:16 -0800179 bool ok = PermissionCache::checkPermission(sModifyAudioRouting, pid, uid);
180 if (!ok) ALOGE("%s(): android.permission.MODIFY_AUDIO_ROUTING denied for uid %d",
181 __func__, uid);
Eric Laurent5284ed52014-05-29 14:37:38 -0700182 return ok;
183}
184
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700185bool modifyDefaultAudioEffectsAllowed() {
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800186 return modifyDefaultAudioEffectsAllowed(
187 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid());
188}
189
190bool modifyDefaultAudioEffectsAllowed(pid_t pid, uid_t uid) {
191 if (isAudioServerUid(IPCThreadState::self()->getCallingUid())) return true;
192
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700193 static const String16 sModifyDefaultAudioEffectsAllowed(
194 "android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS");
195 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800196 bool ok = PermissionCache::checkPermission(sModifyDefaultAudioEffectsAllowed, pid, uid);
197 ALOGE_IF(!ok, "%s(): android.permission.MODIFY_DEFAULT_AUDIO_EFFECTS denied for uid %d",
198 __func__, uid);
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700199 return ok;
200}
201
Glenn Kasten44deb052012-02-05 18:09:08 -0800202bool dumpAllowed() {
Glenn Kasten44deb052012-02-05 18:09:08 -0800203 static const String16 sDump("android.permission.DUMP");
Svet Ganovbe71aa22015-04-28 12:06:02 -0700204 // IMPORTANT: Use PermissionCache - not a runtime permission and may not change.
Glenn Kasten44deb052012-02-05 18:09:08 -0800205 bool ok = PermissionCache::checkCallingPermission(sDump);
206 // convention is for caller to dump an error message to fd instead of logging here
207 //if (!ok) ALOGE("Request requires android.permission.DUMP");
208 return ok;
209}
210
Nadav Bar766fb022018-01-07 12:18:03 +0200211bool modifyPhoneStateAllowed(pid_t pid, uid_t uid) {
Svet Ganov5b81f552018-03-02 09:21:30 -0800212 bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid);
Eric Laurent42984412019-05-09 17:57:03 -0700213 ALOGE_IF(!ok, "Request requires %s", String8(sModifyPhoneState).c_str());
214 return ok;
215}
216
217// privileged behavior needed by Dialer, Settings, SetupWizard and CellBroadcastReceiver
218bool bypassInterruptionPolicyAllowed(pid_t pid, uid_t uid) {
219 static const String16 sWriteSecureSettings("android.permission.WRITE_SECURE_SETTINGS");
220 bool ok = PermissionCache::checkPermission(sModifyPhoneState, pid, uid)
221 || PermissionCache::checkPermission(sWriteSecureSettings, pid, uid)
222 || PermissionCache::checkPermission(sModifyAudioRouting, pid, uid);
223 ALOGE_IF(!ok, "Request requires %s or %s",
224 String8(sModifyPhoneState).c_str(), String8(sWriteSecureSettings).c_str());
Nadav Bar766fb022018-01-07 12:18:03 +0200225 return ok;
226}
227
Eric Laurent9b11c022018-06-06 19:19:22 -0700228status_t checkIMemory(const sp<IMemory>& iMemory)
229{
230 if (iMemory == 0) {
231 ALOGE("%s check failed: NULL IMemory pointer", __FUNCTION__);
232 return BAD_VALUE;
233 }
234
235 sp<IMemoryHeap> heap = iMemory->getMemory();
236 if (heap == 0) {
237 ALOGE("%s check failed: NULL heap pointer", __FUNCTION__);
238 return BAD_VALUE;
239 }
240
241 off_t size = lseek(heap->getHeapID(), 0, SEEK_END);
242 lseek(heap->getHeapID(), 0, SEEK_SET);
243
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700244 if (iMemory->unsecurePointer() == NULL || size < (off_t)iMemory->size()) {
Eric Laurent9b11c022018-06-06 19:19:22 -0700245 ALOGE("%s check failed: pointer %p size %zu fd size %u",
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700246 __FUNCTION__, iMemory->unsecurePointer(), iMemory->size(), (uint32_t)size);
Eric Laurent9b11c022018-06-06 19:19:22 -0700247 return BAD_VALUE;
248 }
249
250 return NO_ERROR;
251}
252
Kevin Rocard8be94972019-02-22 13:26:25 -0800253sp<content::pm::IPackageManagerNative> MediaPackageManager::retreivePackageManager() {
254 const sp<IServiceManager> sm = defaultServiceManager();
255 if (sm == nullptr) {
256 ALOGW("%s: failed to retrieve defaultServiceManager", __func__);
257 return nullptr;
258 }
259 sp<IBinder> packageManager = sm->checkService(String16(nativePackageManagerName));
260 if (packageManager == nullptr) {
261 ALOGW("%s: failed to retrieve native package manager", __func__);
262 return nullptr;
263 }
264 return interface_cast<content::pm::IPackageManagerNative>(packageManager);
265}
266
267std::optional<bool> MediaPackageManager::doIsAllowed(uid_t uid) {
268 if (mPackageManager == nullptr) {
269 /** Can not fetch package manager at construction it may not yet be registered. */
270 mPackageManager = retreivePackageManager();
271 if (mPackageManager == nullptr) {
272 ALOGW("%s: Playback capture is denied as package manager is not reachable", __func__);
273 return std::nullopt;
274 }
275 }
276
277 std::vector<std::string> packageNames;
278 auto status = mPackageManager->getNamesForUids({(int32_t)uid}, &packageNames);
279 if (!status.isOk()) {
280 ALOGW("%s: Playback capture is denied for uid %u as the package names could not be "
281 "retrieved from the package manager: %s", __func__, uid, status.toString8().c_str());
282 return std::nullopt;
283 }
284 if (packageNames.empty()) {
285 ALOGW("%s: Playback capture for uid %u is denied as no package name could be retrieved "
286 "from the package manager: %s", __func__, uid, status.toString8().c_str());
287 return std::nullopt;
288 }
289 std::vector<bool> isAllowed;
290 status = mPackageManager->isAudioPlaybackCaptureAllowed(packageNames, &isAllowed);
291 if (!status.isOk()) {
292 ALOGW("%s: Playback capture is denied for uid %u as the manifest property could not be "
293 "retrieved from the package manager: %s", __func__, uid, status.toString8().c_str());
294 return std::nullopt;
295 }
296 if (packageNames.size() != isAllowed.size()) {
297 ALOGW("%s: Playback capture is denied for uid %u as the package manager returned incoherent"
298 " response size: %zu != %zu", __func__, uid, packageNames.size(), isAllowed.size());
299 return std::nullopt;
300 }
301
302 // Zip together packageNames and isAllowed for debug logs
303 Packages& packages = mDebugLog[uid];
304 packages.resize(packageNames.size()); // Reuse all objects
305 std::transform(begin(packageNames), end(packageNames), begin(isAllowed),
306 begin(packages), [] (auto& name, bool isAllowed) -> Package {
307 return {std::move(name), isAllowed};
308 });
309
310 // Only allow playback record if all packages in this UID allow it
311 bool playbackCaptureAllowed = std::all_of(begin(isAllowed), end(isAllowed),
312 [](bool b) { return b; });
313
314 return playbackCaptureAllowed;
315}
316
317void MediaPackageManager::dump(int fd, int spaces) const {
318 dprintf(fd, "%*sAllow playback capture log:\n", spaces, "");
319 if (mPackageManager == nullptr) {
320 dprintf(fd, "%*sNo package manager\n", spaces + 2, "");
321 }
322 dprintf(fd, "%*sPackage manager errors: %u\n", spaces + 2, "", mPackageManagerErrors);
323
324 for (const auto& uidCache : mDebugLog) {
325 for (const auto& package : std::get<Packages>(uidCache)) {
326 dprintf(fd, "%*s- uid=%5u, allowPlaybackCapture=%s, packageName=%s\n", spaces + 2, "",
327 std::get<const uid_t>(uidCache),
328 package.playbackCaptureAllowed ? "true " : "false",
329 package.name.c_str());
330 }
331 }
332}
333
Andy Hunga85efab2019-12-23 11:41:29 -0800334// How long we hold info before we re-fetch it (24 hours) if we found it previously.
335static constexpr nsecs_t INFO_EXPIRATION_NS = 24 * 60 * 60 * NANOS_PER_SECOND;
336// Maximum info records we retain before clearing everything.
337static constexpr size_t INFO_CACHE_MAX = 1000;
338
339// The original code is from MediaMetricsService.cpp.
340mediautils::UidInfo::Info mediautils::UidInfo::getInfo(uid_t uid)
341{
342 const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
343 struct mediautils::UidInfo::Info info;
344 {
345 std::lock_guard _l(mLock);
346 auto it = mInfoMap.find(uid);
347 if (it != mInfoMap.end()) {
348 info = it->second;
349 ALOGV("%s: uid %d expiration %lld now %lld",
350 __func__, uid, (long long)info.expirationNs, (long long)now);
351 if (info.expirationNs <= now) {
352 // purge the stale entry and fall into re-fetching
353 ALOGV("%s: entry for uid %d expired, now %lld",
354 __func__, uid, (long long)now);
355 mInfoMap.erase(it);
356 info.uid = (uid_t)-1; // this is always fully overwritten
357 }
358 }
359 }
360
361 // if we did not find it in our map, look it up
362 if (info.uid == (uid_t)(-1)) {
363 sp<IServiceManager> sm = defaultServiceManager();
364 sp<content::pm::IPackageManagerNative> package_mgr;
365 if (sm.get() == nullptr) {
366 ALOGE("%s: Cannot find service manager", __func__);
367 } else {
368 sp<IBinder> binder = sm->getService(String16("package_native"));
369 if (binder.get() == nullptr) {
370 ALOGE("%s: Cannot find package_native", __func__);
371 } else {
372 package_mgr = interface_cast<content::pm::IPackageManagerNative>(binder);
373 }
374 }
375
376 // find package name
377 std::string pkg;
378 if (package_mgr != nullptr) {
379 std::vector<std::string> names;
380 binder::Status status = package_mgr->getNamesForUids({(int)uid}, &names);
381 if (!status.isOk()) {
382 ALOGE("%s: getNamesForUids failed: %s",
383 __func__, status.exceptionMessage().c_str());
384 } else {
385 if (!names[0].empty()) {
386 pkg = names[0].c_str();
387 }
388 }
389 }
390
391 if (pkg.empty()) {
392 struct passwd pw{}, *result;
393 char buf[8192]; // extra buffer space - should exceed what is
394 // required in struct passwd_pw (tested),
395 // and even then this is only used in backup
396 // when the package manager is unavailable.
397 if (getpwuid_r(uid, &pw, buf, sizeof(buf), &result) == 0
398 && result != nullptr
399 && result->pw_name != nullptr) {
400 pkg = result->pw_name;
401 }
402 }
403
404 // strip any leading "shared:" strings that came back
405 if (pkg.compare(0, 7, "shared:") == 0) {
406 pkg.erase(0, 7);
407 }
408
409 // determine how pkg was installed and the versionCode
410 std::string installer;
411 int64_t versionCode = 0;
412 bool notFound = false;
413 if (pkg.empty()) {
414 pkg = std::to_string(uid); // not found
415 notFound = true;
416 } else if (strchr(pkg.c_str(), '.') == nullptr) {
417 // not of form 'com.whatever...'; assume internal
418 // so we don't need to look it up in package manager.
419 } else if (package_mgr.get() != nullptr) {
420 String16 pkgName16(pkg.c_str());
421 binder::Status status = package_mgr->getInstallerForPackage(pkgName16, &installer);
422 if (!status.isOk()) {
423 ALOGE("%s: getInstallerForPackage failed: %s",
424 __func__, status.exceptionMessage().c_str());
425 }
426
427 // skip if we didn't get an installer
428 if (status.isOk()) {
429 status = package_mgr->getVersionCodeForPackage(pkgName16, &versionCode);
430 if (!status.isOk()) {
431 ALOGE("%s: getVersionCodeForPackage failed: %s",
432 __func__, status.exceptionMessage().c_str());
433 }
434 }
435
436 ALOGV("%s: package '%s' installed by '%s' versioncode %lld",
437 __func__, pkg.c_str(), installer.c_str(), (long long)versionCode);
438 }
439
440 // add it to the map, to save a subsequent lookup
441 std::lock_guard _l(mLock);
442 // first clear if we have too many cached elements. This would be rare.
443 if (mInfoMap.size() >= INFO_CACHE_MAX) mInfoMap.clear();
444
445 // always overwrite
446 info.uid = uid;
447 info.package = std::move(pkg);
448 info.installer = std::move(installer);
449 info.versionCode = versionCode;
450 info.expirationNs = now + (notFound ? 0 : INFO_EXPIRATION_NS);
451 ALOGV("%s: adding uid %d package '%s' expirationNs: %lld",
452 __func__, uid, info.package.c_str(), (long long)info.expirationNs);
453 mInfoMap[uid] = info;
454 }
455 return info;
456}
457
Glenn Kasten44deb052012-02-05 18:09:08 -0800458} // namespace android