blob: 73ac9686889e8e8b2b305d23db1ea3ac3e6d21cf [file] [log] [blame]
David Chende701692017-10-05 13:16:02 -07001/*
yro0feae942017-11-15 14:38:48 -08002 * Copyright (C) 2017 The Android Open Source Project
David Chende701692017-10-05 13:16:02 -07003 *
4 * Licensed under the Apache License, versionCode 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 */
David Chenf384b902018-03-14 18:36:45 -070016#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
18
Yangster-mac9def8e32018-04-17 13:55:51 -070019#include "hash.h"
Yangster-mac330af582018-02-08 15:24:38 -080020#include "stats_log_util.h"
David Chenc136f452017-11-27 11:52:26 -080021#include "guardrail/StatsdStats.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070022#include "packages/UidMap.h"
23
David Chenc136f452017-11-27 11:52:26 -080024#include <android/os/IStatsCompanionService.h>
25#include <binder/IServiceManager.h>
David Chende701692017-10-05 13:16:02 -070026#include <utils/Errors.h>
27
Dianne Hackborn3accca02013-09-20 09:32:11 -070028#include <inttypes.h>
29
David Chende701692017-10-05 13:16:02 -070030using namespace android;
31
David Chenf384b902018-03-14 18:36:45 -070032using android::base::StringPrintf;
33using android::util::FIELD_COUNT_REPEATED;
34using android::util::FIELD_TYPE_BOOL;
35using android::util::FIELD_TYPE_FLOAT;
36using android::util::FIELD_TYPE_INT32;
37using android::util::FIELD_TYPE_INT64;
Yangster-mac9def8e32018-04-17 13:55:51 -070038using android::util::FIELD_TYPE_UINT64;
David Chenf384b902018-03-14 18:36:45 -070039using android::util::FIELD_TYPE_MESSAGE;
40using android::util::FIELD_TYPE_STRING;
41using android::util::ProtoOutputStream;
42
David Chende701692017-10-05 13:16:02 -070043namespace android {
44namespace os {
45namespace statsd {
46
David Chenf384b902018-03-14 18:36:45 -070047const int FIELD_ID_SNAPSHOT_PACKAGE_NAME = 1;
48const int FIELD_ID_SNAPSHOT_PACKAGE_VERSION = 2;
49const int FIELD_ID_SNAPSHOT_PACKAGE_UID = 3;
David Chenbd125272018-04-04 19:02:50 -070050const int FIELD_ID_SNAPSHOT_PACKAGE_DELETED = 4;
Yangster-mac9def8e32018-04-17 13:55:51 -070051const int FIELD_ID_SNAPSHOT_PACKAGE_NAME_HASH = 5;
David Chenf384b902018-03-14 18:36:45 -070052const int FIELD_ID_SNAPSHOT_TIMESTAMP = 1;
53const int FIELD_ID_SNAPSHOT_PACKAGE_INFO = 2;
54const int FIELD_ID_SNAPSHOTS = 1;
55const int FIELD_ID_CHANGES = 2;
56const int FIELD_ID_CHANGE_DELETION = 1;
57const int FIELD_ID_CHANGE_TIMESTAMP = 2;
58const int FIELD_ID_CHANGE_PACKAGE = 3;
59const int FIELD_ID_CHANGE_UID = 4;
David Chenbd125272018-04-04 19:02:50 -070060const int FIELD_ID_CHANGE_NEW_VERSION = 5;
61const int FIELD_ID_CHANGE_PREV_VERSION = 6;
Yangster-mac9def8e32018-04-17 13:55:51 -070062const int FIELD_ID_CHANGE_PACKAGE_HASH = 7;
David Chenf384b902018-03-14 18:36:45 -070063
Yangster9df9a7f2017-12-18 13:33:05 -080064UidMap::UidMap() : mBytesUsed(0) {}
65
66UidMap::~UidMap() {}
David Chenc136f452017-11-27 11:52:26 -080067
David Chende701692017-10-05 13:16:02 -070068bool UidMap::hasApp(int uid, const string& packageName) const {
69 lock_guard<mutex> lock(mMutex);
70
David Chenbd125272018-04-04 19:02:50 -070071 auto it = mMap.find(std::make_pair(uid, packageName));
72 return it != mMap.end() && !it->second.deleted;
David Chende701692017-10-05 13:16:02 -070073}
74
Yangster9df9a7f2017-12-18 13:33:05 -080075string UidMap::normalizeAppName(const string& appName) const {
76 string normalizedName = appName;
77 std::transform(normalizedName.begin(), normalizedName.end(), normalizedName.begin(), ::tolower);
78 return normalizedName;
79}
80
81std::set<string> UidMap::getAppNamesFromUid(const int32_t& uid, bool returnNormalized) const {
82 lock_guard<mutex> lock(mMutex);
83 return getAppNamesFromUidLocked(uid,returnNormalized);
84}
85
86std::set<string> UidMap::getAppNamesFromUidLocked(const int32_t& uid, bool returnNormalized) const {
87 std::set<string> names;
David Chenbd125272018-04-04 19:02:50 -070088 for (const auto& kv : mMap) {
89 if (kv.first.first == uid && !kv.second.deleted) {
90 names.insert(returnNormalized ? normalizeAppName(kv.first.second) : kv.first.second);
91 }
Yangster9df9a7f2017-12-18 13:33:05 -080092 }
93 return names;
94}
95
Dianne Hackborn3accca02013-09-20 09:32:11 -070096int64_t UidMap::getAppVersion(int uid, const string& packageName) const {
David Chende701692017-10-05 13:16:02 -070097 lock_guard<mutex> lock(mMutex);
98
David Chenbd125272018-04-04 19:02:50 -070099 auto it = mMap.find(std::make_pair(uid, packageName));
100 if (it == mMap.end() || it->second.deleted) {
101 return 0;
David Chende701692017-10-05 13:16:02 -0700102 }
David Chenbd125272018-04-04 19:02:50 -0700103 return it->second.versionCode;
David Chend6896892017-10-25 11:49:03 -0700104}
105
106void UidMap::updateMap(const int64_t& timestamp, const vector<int32_t>& uid,
Dianne Hackborn3accca02013-09-20 09:32:11 -0700107 const vector<int64_t>& versionCode, const vector<String16>& packageName) {
Yao Chend10f7b12017-12-18 12:53:50 -0800108 vector<wp<PackageInfoListener>> broadcastList;
109 {
110 lock_guard<mutex> lock(mMutex); // Exclusively lock for updates.
David Chende701692017-10-05 13:16:02 -0700111
David Chenbd125272018-04-04 19:02:50 -0700112 std::unordered_map<std::pair<int, string>, AppData, PairHash> deletedApps;
113
114 // Copy all the deleted apps.
115 for (const auto& kv : mMap) {
116 if (kv.second.deleted) {
117 deletedApps[kv.first] = kv.second;
118 }
Yao Chend10f7b12017-12-18 12:53:50 -0800119 }
David Chende701692017-10-05 13:16:02 -0700120
David Chenbd125272018-04-04 19:02:50 -0700121 mMap.clear();
122 for (size_t j = 0; j < uid.size(); j++) {
123 string package = string(String8(packageName[j]).string());
124 mMap[std::make_pair(uid[j], package)] = AppData(versionCode[j]);
125 }
David Chenf384b902018-03-14 18:36:45 -0700126
David Chenbd125272018-04-04 19:02:50 -0700127 for (const auto& kv : deletedApps) {
128 auto mMapIt = mMap.find(kv.first);
129 if (mMapIt != mMap.end()) {
130 // Insert this deleted app back into the current map.
131 mMap[kv.first] = kv.second;
132 }
133 }
134
David Chenc0f6f632018-01-18 16:02:42 -0800135 ensureBytesUsedBelowLimit();
Yao Chend10f7b12017-12-18 12:53:50 -0800136 StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed);
Yao Chend10f7b12017-12-18 12:53:50 -0800137 getListenerListCopyLocked(&broadcastList);
David Chende701692017-10-05 13:16:02 -0700138 }
Yao Chend10f7b12017-12-18 12:53:50 -0800139 // To avoid invoking callback while holding the internal lock. we get a copy of the listener
140 // list and invoke the callback. It's still possible that after we copy the list, a
141 // listener removes itself before we call it. It's then the listener's job to handle it (expect
142 // the callback to be called after listener is removed, and the listener should properly
143 // ignore it).
144 for (auto weakPtr : broadcastList) {
145 auto strongPtr = weakPtr.promote();
146 if (strongPtr != NULL) {
David Chen27785a82018-01-19 17:06:45 -0800147 strongPtr->onUidMapReceived(timestamp);
Yao Chend10f7b12017-12-18 12:53:50 -0800148 }
149 }
David Chende701692017-10-05 13:16:02 -0700150}
151
David Chend6896892017-10-25 11:49:03 -0700152void UidMap::updateApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid,
Dianne Hackborn3accca02013-09-20 09:32:11 -0700153 const int64_t& versionCode) {
Yao Chend10f7b12017-12-18 12:53:50 -0800154 vector<wp<PackageInfoListener>> broadcastList;
Yangster9df9a7f2017-12-18 13:33:05 -0800155 string appName = string(String8(app_16).string());
Yao Chend10f7b12017-12-18 12:53:50 -0800156 {
157 lock_guard<mutex> lock(mMutex);
David Chenbd125272018-04-04 19:02:50 -0700158 int32_t prevVersion = 0;
159 bool found = false;
160 auto it = mMap.find(std::make_pair(uid, appName));
161 if (it != mMap.end()) {
162 found = true;
163 prevVersion = it->second.versionCode;
164 it->second.versionCode = versionCode;
165 it->second.deleted = false;
166 }
167 if (!found) {
168 // Otherwise, we need to add an app at this uid.
169 mMap[std::make_pair(uid, appName)] = AppData(versionCode);
170 } else {
171 // Only notify the listeners if this is an app upgrade. If this app is being installed
172 // for the first time, then we don't notify the listeners.
David Chen81245fd2018-04-12 14:33:37 -0700173 // It's also OK to split again if we're forming a partial bucket after re-installing an
174 // app after deletion.
David Chenbd125272018-04-04 19:02:50 -0700175 getListenerListCopyLocked(&broadcastList);
176 }
177 mChanges.emplace_back(false, timestamp, appName, uid, versionCode, prevVersion);
David Chenf384b902018-03-14 18:36:45 -0700178 mBytesUsed += kBytesChangeRecord;
David Chenc0f6f632018-01-18 16:02:42 -0800179 ensureBytesUsedBelowLimit();
Yao Chend10f7b12017-12-18 12:53:50 -0800180 StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed);
David Chenf384b902018-03-14 18:36:45 -0700181 StatsdStats::getInstance().setUidMapChanges(mChanges.size());
David Chende701692017-10-05 13:16:02 -0700182 }
183
Yao Chend10f7b12017-12-18 12:53:50 -0800184 for (auto weakPtr : broadcastList) {
185 auto strongPtr = weakPtr.promote();
186 if (strongPtr != NULL) {
David Chen27785a82018-01-19 17:06:45 -0800187 strongPtr->notifyAppUpgrade(timestamp, appName, uid, versionCode);
David Chende701692017-10-05 13:16:02 -0700188 }
David Chende701692017-10-05 13:16:02 -0700189 }
David Chende701692017-10-05 13:16:02 -0700190}
191
David Chenc136f452017-11-27 11:52:26 -0800192void UidMap::ensureBytesUsedBelowLimit() {
193 size_t limit;
194 if (maxBytesOverride <= 0) {
195 limit = StatsdStats::kMaxBytesUsedUidMap;
196 } else {
197 limit = maxBytesOverride;
198 }
199 while (mBytesUsed > limit) {
David Chenf384b902018-03-14 18:36:45 -0700200 ALOGI("Bytes used %zu is above limit %zu, need to delete something", mBytesUsed, limit);
David Chenbd125272018-04-04 19:02:50 -0700201 if (mChanges.size() > 0) {
David Chenf384b902018-03-14 18:36:45 -0700202 mBytesUsed -= kBytesChangeRecord;
203 mChanges.pop_front();
David Chenbd125272018-04-04 19:02:50 -0700204 StatsdStats::getInstance().noteUidMapDropped(1);
David Chenc136f452017-11-27 11:52:26 -0800205 }
David Chenc136f452017-11-27 11:52:26 -0800206 }
207}
208
Yao Chend10f7b12017-12-18 12:53:50 -0800209void UidMap::getListenerListCopyLocked(vector<wp<PackageInfoListener>>* output) {
210 for (auto weakIt = mSubscribers.begin(); weakIt != mSubscribers.end();) {
211 auto strongPtr = weakIt->promote();
212 if (strongPtr != NULL) {
213 output->push_back(*weakIt);
214 weakIt++;
215 } else {
216 weakIt = mSubscribers.erase(weakIt);
217 VLOG("The UidMap listener is gone, remove it now");
David Chende701692017-10-05 13:16:02 -0700218 }
219 }
David Chende701692017-10-05 13:16:02 -0700220}
221
Yao Chend10f7b12017-12-18 12:53:50 -0800222void UidMap::removeApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid) {
223 vector<wp<PackageInfoListener>> broadcastList;
224 string app = string(String8(app_16).string());
225 {
226 lock_guard<mutex> lock(mMutex);
227
Chenjie Yue36018b2018-04-16 15:18:30 -0700228 int64_t prevVersion = 0;
David Chenbd125272018-04-04 19:02:50 -0700229 auto key = std::make_pair(uid, app);
230 auto it = mMap.find(key);
231 if (it != mMap.end() && !it->second.deleted) {
232 prevVersion = it->second.versionCode;
233 it->second.deleted = true;
234 mDeletedApps.push_back(key);
235 }
236 if (mDeletedApps.size() > StatsdStats::kMaxDeletedAppsInUidMap) {
237 // Delete the oldest one.
238 auto oldest = mDeletedApps.front();
239 mDeletedApps.pop_front();
240 mMap.erase(oldest);
241 StatsdStats::getInstance().noteUidMapAppDeletionDropped();
242 }
243 mChanges.emplace_back(true, timestamp, app, uid, 0, prevVersion);
David Chenf384b902018-03-14 18:36:45 -0700244 mBytesUsed += kBytesChangeRecord;
David Chenc0f6f632018-01-18 16:02:42 -0800245 ensureBytesUsedBelowLimit();
Yao Chend10f7b12017-12-18 12:53:50 -0800246 StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed);
David Chenf384b902018-03-14 18:36:45 -0700247 StatsdStats::getInstance().setUidMapChanges(mChanges.size());
Yao Chend10f7b12017-12-18 12:53:50 -0800248 getListenerListCopyLocked(&broadcastList);
249 }
250
251 for (auto weakPtr : broadcastList) {
252 auto strongPtr = weakPtr.promote();
253 if (strongPtr != NULL) {
David Chen27785a82018-01-19 17:06:45 -0800254 strongPtr->notifyAppRemoved(timestamp, app, uid);
Yao Chend10f7b12017-12-18 12:53:50 -0800255 }
256 }
257}
258
259void UidMap::addListener(wp<PackageInfoListener> producer) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700260 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700261 mSubscribers.insert(producer);
262}
263
Yao Chend10f7b12017-12-18 12:53:50 -0800264void UidMap::removeListener(wp<PackageInfoListener> producer) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700265 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700266 mSubscribers.erase(producer);
267}
268
David Chen21582962017-11-01 17:32:46 -0700269void UidMap::assignIsolatedUid(int isolatedUid, int parentUid) {
270 lock_guard<mutex> lock(mIsolatedMutex);
271
272 mIsolatedUidMap[isolatedUid] = parentUid;
273}
274
275void UidMap::removeIsolatedUid(int isolatedUid, int parentUid) {
276 lock_guard<mutex> lock(mIsolatedMutex);
277
278 auto it = mIsolatedUidMap.find(isolatedUid);
279 if (it != mIsolatedUidMap.end()) {
280 mIsolatedUidMap.erase(it);
281 }
282}
283
Yangster-macd40053e2018-01-09 16:29:22 -0800284int UidMap::getHostUidOrSelf(int uid) const {
David Chen21582962017-11-01 17:32:46 -0700285 lock_guard<mutex> lock(mIsolatedMutex);
286
287 auto it = mIsolatedUidMap.find(uid);
288 if (it != mIsolatedUidMap.end()) {
289 return it->second;
290 }
291 return uid;
292}
293
David Chend6896892017-10-25 11:49:03 -0700294void UidMap::clearOutput() {
David Chenf384b902018-03-14 18:36:45 -0700295 mChanges.clear();
David Chenc136f452017-11-27 11:52:26 -0800296 // Also update the guardrail trackers.
297 StatsdStats::getInstance().setUidMapChanges(0);
David Chencfc311d2018-01-23 17:55:54 -0800298 mBytesUsed = 0;
David Chenc136f452017-11-27 11:52:26 -0800299 StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed);
David Chend6896892017-10-25 11:49:03 -0700300}
David Chende701692017-10-05 13:16:02 -0700301
David Chend6896892017-10-25 11:49:03 -0700302int64_t UidMap::getMinimumTimestampNs() {
303 int64_t m = 0;
David Chenbd125272018-04-04 19:02:50 -0700304 for (const auto& kv : mLastUpdatePerConfigKey) {
David Chend6896892017-10-25 11:49:03 -0700305 if (m == 0) {
David Chenbd125272018-04-04 19:02:50 -0700306 m = kv.second;
307 } else if (kv.second < m) {
308 m = kv.second;
David Chend6896892017-10-25 11:49:03 -0700309 }
310 }
311 return m;
312}
313
Yao Chend10f7b12017-12-18 12:53:50 -0800314size_t UidMap::getBytesUsed() const {
David Chenc136f452017-11-27 11:52:26 -0800315 return mBytesUsed;
316}
317
yro4beccbe2018-03-15 19:42:05 -0700318void UidMap::appendUidMap(const int64_t& timestamp, const ConfigKey& key,
Yangster-mac9def8e32018-04-17 13:55:51 -0700319 std::set<string> *str_set, ProtoOutputStream* proto) {
David Chend6896892017-10-25 11:49:03 -0700320 lock_guard<mutex> lock(mMutex); // Lock for updates
321
David Chenf384b902018-03-14 18:36:45 -0700322 for (const ChangeRecord& record : mChanges) {
323 if (record.timestampNs > mLastUpdatePerConfigKey[key]) {
324 uint64_t changesToken =
yro4beccbe2018-03-15 19:42:05 -0700325 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CHANGES);
326 proto->write(FIELD_TYPE_BOOL | FIELD_ID_CHANGE_DELETION, (bool)record.deletion);
327 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CHANGE_TIMESTAMP,
328 (long long)record.timestampNs);
Yangster-mac9def8e32018-04-17 13:55:51 -0700329 if (str_set != nullptr) {
330 str_set->insert(record.package);
331 proto->write(FIELD_TYPE_UINT64 | FIELD_ID_CHANGE_PACKAGE_HASH,
332 (long long)Hash64(record.package));
333 } else {
334 proto->write(FIELD_TYPE_STRING | FIELD_ID_CHANGE_PACKAGE, record.package);
335 }
336
yro4beccbe2018-03-15 19:42:05 -0700337 proto->write(FIELD_TYPE_INT32 | FIELD_ID_CHANGE_UID, (int)record.uid);
Chenjie Yue36018b2018-04-16 15:18:30 -0700338 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CHANGE_NEW_VERSION, (long long)record.version);
339 proto->write(FIELD_TYPE_INT64 | FIELD_ID_CHANGE_PREV_VERSION,
340 (long long)record.prevVersion);
yro4beccbe2018-03-15 19:42:05 -0700341 proto->end(changesToken);
David Chenf384b902018-03-14 18:36:45 -0700342 }
343 }
344
David Chenbd125272018-04-04 19:02:50 -0700345 // Write snapshot from current uid map state.
346 uint64_t snapshotsToken =
347 proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_SNAPSHOTS);
348 proto->write(FIELD_TYPE_INT64 | FIELD_ID_SNAPSHOT_TIMESTAMP, (long long)timestamp);
349 for (const auto& kv : mMap) {
350 uint64_t token = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
351 FIELD_ID_SNAPSHOT_PACKAGE_INFO);
Yangster-mac9def8e32018-04-17 13:55:51 -0700352
353 if (str_set != nullptr) {
354 str_set->insert(kv.first.second);
355 proto->write(FIELD_TYPE_UINT64 | FIELD_ID_SNAPSHOT_PACKAGE_NAME_HASH,
356 (long long)Hash64(kv.first.second));
357 } else {
358 proto->write(FIELD_TYPE_STRING | FIELD_ID_SNAPSHOT_PACKAGE_NAME, kv.first.second);
359 }
360
Chenjie Yue36018b2018-04-16 15:18:30 -0700361 proto->write(FIELD_TYPE_INT64 | FIELD_ID_SNAPSHOT_PACKAGE_VERSION,
362 (long long)kv.second.versionCode);
David Chenbd125272018-04-04 19:02:50 -0700363 proto->write(FIELD_TYPE_INT32 | FIELD_ID_SNAPSHOT_PACKAGE_UID, kv.first.first);
364 proto->write(FIELD_TYPE_BOOL | FIELD_ID_SNAPSHOT_PACKAGE_DELETED, kv.second.deleted);
365 proto->end(token);
David Chenf384b902018-03-14 18:36:45 -0700366 }
David Chenbd125272018-04-04 19:02:50 -0700367 proto->end(snapshotsToken);
David Chenf384b902018-03-14 18:36:45 -0700368
David Chend6896892017-10-25 11:49:03 -0700369 int64_t prevMin = getMinimumTimestampNs();
370 mLastUpdatePerConfigKey[key] = timestamp;
371 int64_t newMin = getMinimumTimestampNs();
372
David Chenf384b902018-03-14 18:36:45 -0700373 if (newMin > prevMin) { // Delete anything possible now that the minimum has
374 // moved forward.
David Chend6896892017-10-25 11:49:03 -0700375 int64_t cutoff_nanos = newMin;
Yao Chen34900c32018-03-19 13:43:29 -0700376 for (auto it_changes = mChanges.begin(); it_changes != mChanges.end();) {
David Chenf384b902018-03-14 18:36:45 -0700377 if (it_changes->timestampNs < cutoff_nanos) {
378 mBytesUsed -= kBytesChangeRecord;
Yao Chen34900c32018-03-19 13:43:29 -0700379 it_changes = mChanges.erase(it_changes);
380 } else {
381 ++it_changes;
David Chend6896892017-10-25 11:49:03 -0700382 }
383 }
384 }
David Chenc136f452017-11-27 11:52:26 -0800385 StatsdStats::getInstance().setCurrentUidMapMemory(mBytesUsed);
David Chenf384b902018-03-14 18:36:45 -0700386 StatsdStats::getInstance().setUidMapChanges(mChanges.size());
David Chende701692017-10-05 13:16:02 -0700387}
388
Yao Chend10f7b12017-12-18 12:53:50 -0800389void UidMap::printUidMap(FILE* out) const {
David Chende701692017-10-05 13:16:02 -0700390 lock_guard<mutex> lock(mMutex);
391
David Chenbd125272018-04-04 19:02:50 -0700392 for (const auto& kv : mMap) {
393 if (!kv.second.deleted) {
394 fprintf(out, "%s, v%" PRId64 " (%i)\n", kv.first.second.c_str(), kv.second.versionCode,
395 kv.first.first);
396 }
David Chende701692017-10-05 13:16:02 -0700397 }
398}
399
David Chend6896892017-10-25 11:49:03 -0700400void UidMap::OnConfigUpdated(const ConfigKey& key) {
401 mLastUpdatePerConfigKey[key] = -1;
402}
403
404void UidMap::OnConfigRemoved(const ConfigKey& key) {
405 mLastUpdatePerConfigKey.erase(key);
406}
407
Yao Chend10f7b12017-12-18 12:53:50 -0800408set<int32_t> UidMap::getAppUid(const string& package) const {
409 lock_guard<mutex> lock(mMutex);
410
411 set<int32_t> results;
David Chenbd125272018-04-04 19:02:50 -0700412 for (const auto& kv : mMap) {
413 if (kv.first.second == package && !kv.second.deleted) {
414 results.insert(kv.first.first);
Yao Chend10f7b12017-12-18 12:53:50 -0800415 }
416 }
417 return results;
418}
419
Yao Chen147ce602017-12-22 14:35:34 -0800420// Note not all the following AIDs are used as uids. Some are used only for gids.
421// It's ok to leave them in the map, but we won't ever see them in the log's uid field.
422// App's uid starts from 10000, and will not overlap with the following AIDs.
423const std::map<string, uint32_t> UidMap::sAidToUidMapping = {{"AID_ROOT", 0},
424 {"AID_SYSTEM", 1000},
425 {"AID_RADIO", 1001},
426 {"AID_BLUETOOTH", 1002},
427 {"AID_GRAPHICS", 1003},
428 {"AID_INPUT", 1004},
429 {"AID_AUDIO", 1005},
430 {"AID_CAMERA", 1006},
431 {"AID_LOG", 1007},
432 {"AID_COMPASS", 1008},
433 {"AID_MOUNT", 1009},
434 {"AID_WIFI", 1010},
435 {"AID_ADB", 1011},
436 {"AID_INSTALL", 1012},
437 {"AID_MEDIA", 1013},
438 {"AID_DHCP", 1014},
439 {"AID_SDCARD_RW", 1015},
440 {"AID_VPN", 1016},
441 {"AID_KEYSTORE", 1017},
442 {"AID_USB", 1018},
443 {"AID_DRM", 1019},
444 {"AID_MDNSR", 1020},
445 {"AID_GPS", 1021},
446 // {"AID_UNUSED1", 1022},
447 {"AID_MEDIA_RW", 1023},
448 {"AID_MTP", 1024},
449 // {"AID_UNUSED2", 1025},
450 {"AID_DRMRPC", 1026},
451 {"AID_NFC", 1027},
452 {"AID_SDCARD_R", 1028},
453 {"AID_CLAT", 1029},
454 {"AID_LOOP_RADIO", 1030},
455 {"AID_MEDIA_DRM", 1031},
456 {"AID_PACKAGE_INFO", 1032},
457 {"AID_SDCARD_PICS", 1033},
458 {"AID_SDCARD_AV", 1034},
459 {"AID_SDCARD_ALL", 1035},
460 {"AID_LOGD", 1036},
461 {"AID_SHARED_RELRO", 1037},
462 {"AID_DBUS", 1038},
463 {"AID_TLSDATE", 1039},
464 {"AID_MEDIA_EX", 1040},
465 {"AID_AUDIOSERVER", 1041},
466 {"AID_METRICS_COLL", 1042},
467 {"AID_METRICSD", 1043},
468 {"AID_WEBSERV", 1044},
469 {"AID_DEBUGGERD", 1045},
470 {"AID_MEDIA_CODEC", 1046},
471 {"AID_CAMERASERVER", 1047},
472 {"AID_FIREWALL", 1048},
473 {"AID_TRUNKS", 1049},
474 {"AID_NVRAM", 1050},
475 {"AID_DNS", 1051},
476 {"AID_DNS_TETHER", 1052},
477 {"AID_WEBVIEW_ZYGOTE", 1053},
478 {"AID_VEHICLE_NETWORK", 1054},
479 {"AID_MEDIA_AUDIO", 1055},
480 {"AID_MEDIA_VIDEO", 1056},
481 {"AID_MEDIA_IMAGE", 1057},
482 {"AID_TOMBSTONED", 1058},
483 {"AID_MEDIA_OBB", 1059},
484 {"AID_ESE", 1060},
485 {"AID_OTA_UPDATE", 1061},
486 {"AID_AUTOMOTIVE_EVS", 1062},
487 {"AID_LOWPAN", 1063},
488 {"AID_HSM", 1064},
Yao Chen29f79b52018-01-17 10:56:48 -0800489 {"AID_RESERVED_DISK", 1065},
490 {"AID_STATSD", 1066},
491 {"AID_INCIDENTD", 1067},
Igor Murashkina1980ae2018-10-04 14:30:48 -0700492 {"AID_SECURE_ELEMENT", 1068},
493 {"AID_LMKD", 1069},
494 {"AID_LLKD", 1070},
Igor Murashkin12bb16c2018-10-05 16:25:21 -0700495 {"AID_IORAPD", 1071},
Yao Chen147ce602017-12-22 14:35:34 -0800496 {"AID_SHELL", 2000},
497 {"AID_CACHE", 2001},
498 {"AID_DIAG", 2002}};
499
David Chende701692017-10-05 13:16:02 -0700500} // namespace statsd
501} // namespace os
Yao Chen29f79b52018-01-17 10:56:48 -0800502} // namespace android