blob: 239881402880bfa911015bdf3f489b5c89a9421d [file] [log] [blame]
David Chende701692017-10-05 13:16:02 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
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 */
16
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
18
19#include "packages/UidMap.h"
20
David Chende701692017-10-05 13:16:02 -070021#include <utils/Errors.h>
22
23using namespace android;
24
25namespace android {
26namespace os {
27namespace statsd {
28
29bool UidMap::hasApp(int uid, const string& packageName) const {
30 lock_guard<mutex> lock(mMutex);
31
32 auto range = mMap.equal_range(uid);
33 for (auto it = range.first; it != range.second; ++it) {
34 if (it->second.packageName == packageName) {
35 return true;
36 }
37 }
38 return false;
39}
40
41int UidMap::getAppVersion(int uid, const string& packageName) const {
42 lock_guard<mutex> lock(mMutex);
43
44 auto range = mMap.equal_range(uid);
45 for (auto it = range.first; it != range.second; ++it) {
46 if (it->second.packageName == packageName) {
47 return it->second.versionCode;
48 }
49 }
50 return 0;
51}
52
Joe Onorato9fc9edf2017-10-15 20:08:52 -070053void UidMap::updateMap(const vector<int32_t>& uid, const vector<int32_t>& versionCode,
54 const vector<String16>& packageName) {
yrob0378b02017-11-09 20:36:25 -080055 updateMap(time(nullptr) * NS_PER_SEC, uid, versionCode, packageName);
David Chend6896892017-10-25 11:49:03 -070056}
57
58void UidMap::updateMap(const int64_t& timestamp, const vector<int32_t>& uid,
59 const vector<int32_t>& versionCode, const vector<String16>& packageName) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070060 lock_guard<mutex> lock(mMutex); // Exclusively lock for updates.
David Chende701692017-10-05 13:16:02 -070061
62 mMap.clear();
David Chend6896892017-10-25 11:49:03 -070063 for (size_t j = 0; j < uid.size(); j++) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070064 mMap.insert(make_pair(uid[j],
65 AppData(string(String8(packageName[j]).string()), versionCode[j])));
David Chende701692017-10-05 13:16:02 -070066 }
67
David Chend6896892017-10-25 11:49:03 -070068 auto snapshot = mOutput.add_snapshots();
69 snapshot->set_timestamp_nanos(timestamp);
70 for (size_t j = 0; j < uid.size(); j++) {
71 auto t = snapshot->add_package_info();
72 t->set_name(string(String8(packageName[j]).string()));
73 t->set_version(int(versionCode[j]));
74 t->set_uid(uid[j]);
David Chende701692017-10-05 13:16:02 -070075 }
76}
77
Joe Onorato9fc9edf2017-10-15 20:08:52 -070078void UidMap::updateApp(const String16& app_16, const int32_t& uid, const int32_t& versionCode) {
yrob0378b02017-11-09 20:36:25 -080079 updateApp(time(nullptr) * NS_PER_SEC, app_16, uid, versionCode);
David Chend6896892017-10-25 11:49:03 -070080}
81
82void UidMap::updateApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid,
83 const int32_t& versionCode) {
David Chende701692017-10-05 13:16:02 -070084 lock_guard<mutex> lock(mMutex);
85
86 string app = string(String8(app_16).string());
87
88 // Notify any interested producers that this app has updated
89 for (auto it : mSubscribers) {
90 it->notifyAppUpgrade(app, uid, versionCode);
91 }
92
93 auto log = mOutput.add_changes();
94 log->set_deletion(false);
David Chend6896892017-10-25 11:49:03 -070095 log->set_timestamp_nanos(timestamp);
David Chende701692017-10-05 13:16:02 -070096 log->set_app(app);
97 log->set_uid(uid);
98 log->set_version(versionCode);
99
100 auto range = mMap.equal_range(int(uid));
101 for (auto it = range.first; it != range.second; ++it) {
102 if (it->second.packageName == app) {
103 it->second.versionCode = int(versionCode);
104 return;
105 }
106 ALOGD("updateApp failed to find the app %s with uid %i to update", app.c_str(), uid);
107 return;
108 }
109
110 // Otherwise, we need to add an app at this uid.
111 mMap.insert(make_pair(uid, AppData(app, int(versionCode))));
112}
113
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700114void UidMap::removeApp(const String16& app_16, const int32_t& uid) {
yrob0378b02017-11-09 20:36:25 -0800115 removeApp(time(nullptr) * NS_PER_SEC, app_16, uid);
David Chend6896892017-10-25 11:49:03 -0700116}
117void UidMap::removeApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid) {
David Chende701692017-10-05 13:16:02 -0700118 lock_guard<mutex> lock(mMutex);
119
120 string app = string(String8(app_16).string());
121
David Chend6896892017-10-25 11:49:03 -0700122 for (auto it : mSubscribers) {
123 it->notifyAppRemoved(app, uid);
124 }
125
David Chende701692017-10-05 13:16:02 -0700126 auto log = mOutput.add_changes();
127 log->set_deletion(true);
David Chend6896892017-10-25 11:49:03 -0700128 log->set_timestamp_nanos(timestamp);
David Chende701692017-10-05 13:16:02 -0700129 log->set_app(app);
130 log->set_uid(uid);
131
132 auto range = mMap.equal_range(int(uid));
133 for (auto it = range.first; it != range.second; ++it) {
134 if (it->second.packageName == app) {
135 mMap.erase(it);
136 return;
137 }
138 }
139 ALOGD("removeApp failed to find the app %s with uid %i to remove", app.c_str(), uid);
140 return;
141}
142
143void UidMap::addListener(sp<PackageInfoListener> producer) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700144 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700145 mSubscribers.insert(producer);
146}
147
148void UidMap::removeListener(sp<PackageInfoListener> producer) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700149 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700150 mSubscribers.erase(producer);
151}
152
David Chen21582962017-11-01 17:32:46 -0700153void UidMap::assignIsolatedUid(int isolatedUid, int parentUid) {
154 lock_guard<mutex> lock(mIsolatedMutex);
155
156 mIsolatedUidMap[isolatedUid] = parentUid;
157}
158
159void UidMap::removeIsolatedUid(int isolatedUid, int parentUid) {
160 lock_guard<mutex> lock(mIsolatedMutex);
161
162 auto it = mIsolatedUidMap.find(isolatedUid);
163 if (it != mIsolatedUidMap.end()) {
164 mIsolatedUidMap.erase(it);
165 }
166}
167
168int UidMap::getParentUidOrSelf(int uid) {
169 lock_guard<mutex> lock(mIsolatedMutex);
170
171 auto it = mIsolatedUidMap.find(uid);
172 if (it != mIsolatedUidMap.end()) {
173 return it->second;
174 }
175 return uid;
176}
177
David Chend6896892017-10-25 11:49:03 -0700178void UidMap::clearOutput() {
David Chende701692017-10-05 13:16:02 -0700179 mOutput.Clear();
180
181 // Re-initialize the initial state for the outputs. This results in extra data being uploaded
David Chend6896892017-10-25 11:49:03 -0700182 // but helps ensure we can re-construct the UID->app name, versionCode mapping in server.
183 auto snapshot = mOutput.add_snapshots();
David Chende701692017-10-05 13:16:02 -0700184 for (auto it : mMap) {
David Chend6896892017-10-25 11:49:03 -0700185 auto t = snapshot->add_package_info();
186 t->set_name(it.second.packageName);
David Chende701692017-10-05 13:16:02 -0700187 t->set_version(it.second.versionCode);
188 t->set_uid(it.first);
189 }
David Chend6896892017-10-25 11:49:03 -0700190}
David Chende701692017-10-05 13:16:02 -0700191
David Chend6896892017-10-25 11:49:03 -0700192int64_t UidMap::getMinimumTimestampNs() {
193 int64_t m = 0;
194 for (auto it : mLastUpdatePerConfigKey) {
195 if (m == 0) {
196 m = it.second;
197 } else if (it.second < m) {
198 m = it.second;
199 }
200 }
201 return m;
202}
203
204UidMapping UidMap::getOutput(const ConfigKey& key) {
yrob0378b02017-11-09 20:36:25 -0800205 return getOutput(time(nullptr) * NS_PER_SEC, key);
David Chend6896892017-10-25 11:49:03 -0700206}
207
208UidMapping UidMap::getOutput(const int64_t& timestamp, const ConfigKey& key) {
209 lock_guard<mutex> lock(mMutex); // Lock for updates
210
211 auto ret = UidMapping(mOutput); // Copy that will be returned.
212 int64_t prevMin = getMinimumTimestampNs();
213 mLastUpdatePerConfigKey[key] = timestamp;
214 int64_t newMin = getMinimumTimestampNs();
215
216 if (newMin > prevMin) {
217 int64_t cutoff_nanos = newMin;
218 auto snapshots = mOutput.mutable_snapshots();
219 auto it_snapshots = snapshots->cbegin();
220 while (it_snapshots != snapshots->cend()) {
221 if (it_snapshots->timestamp_nanos() < cutoff_nanos) {
222 // it_snapshots now points to the following element.
223 it_snapshots = snapshots->erase(it_snapshots);
224 } else {
225 ++it_snapshots;
226 }
227 }
228 auto deltas = mOutput.mutable_changes();
229 auto it_deltas = deltas->cbegin();
230 while (it_deltas != deltas->cend()) {
231 if (it_deltas->timestamp_nanos() < cutoff_nanos) {
232 // it_deltas now points to the following element.
233 it_deltas = deltas->erase(it_deltas);
234 } else {
235 ++it_deltas;
236 }
237 }
238 }
David Chende701692017-10-05 13:16:02 -0700239 return ret;
240}
241
242void UidMap::printUidMap(FILE* out) {
243 lock_guard<mutex> lock(mMutex);
244
245 for (auto it : mMap) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700246 fprintf(out, "%s, v%d (%i)\n", it.second.packageName.c_str(), it.second.versionCode,
247 it.first);
David Chende701692017-10-05 13:16:02 -0700248 }
249}
250
David Chend6896892017-10-25 11:49:03 -0700251void UidMap::OnConfigUpdated(const ConfigKey& key) {
252 mLastUpdatePerConfigKey[key] = -1;
253}
254
255void UidMap::OnConfigRemoved(const ConfigKey& key) {
256 mLastUpdatePerConfigKey.erase(key);
257}
258
David Chende701692017-10-05 13:16:02 -0700259} // namespace statsd
260} // namespace os
261} // namespace android