blob: 13020e06dc5d4b907b84513b55f280da80367c28 [file] [log] [blame]
Joe Onorato9fc9edf2017-10-15 20:08:52 -07001/*
2 * Copyright (C) 2017 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
Yao Chen52b478b2018-03-27 10:59:45 -070017#define DEBUG false // STOPSHIP if true
18#include "Log.h"
19
Joe Onorato9fc9edf2017-10-15 20:08:52 -070020#include "config/ConfigManager.h"
yro947fbce2017-11-15 22:50:23 -080021#include "storage/StorageManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070022
Yao Chen52b478b2018-03-27 10:59:45 -070023#include "guardrail/StatsdStats.h"
Yangster-macc04feba2018-04-02 14:37:33 -070024#include "stats_log_util.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025#include "stats_util.h"
Yangster-macb142cc82018-03-30 15:22:08 -070026#include "stats_log_util.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070027
Joe Onorato9fc9edf2017-10-15 20:08:52 -070028#include <stdio.h>
yro87d983c2017-11-14 21:31:43 -080029#include <vector>
30#include "android-base/stringprintf.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070031
32namespace android {
33namespace os {
34namespace statsd {
35
Yao Chenf09569f2017-12-13 17:00:51 -080036using std::pair;
Yao Chenf09569f2017-12-13 17:00:51 -080037using std::string;
38using std::vector;
39
yro03faf092017-12-12 00:17:50 -080040#define STATS_SERVICE_DIR "/data/misc/stats-service"
yro87d983c2017-11-14 21:31:43 -080041
yro87d983c2017-11-14 21:31:43 -080042using android::base::StringPrintf;
43using std::unique_ptr;
44
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080045struct ConfigReceiverDeathCookie {
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070046 ConfigReceiverDeathCookie(const wp<ConfigManager>& configManager, const ConfigKey& configKey,
47 const shared_ptr<IPendingIntentRef>& pir) :
48 mConfigManager(configManager), mConfigKey(configKey), mPir(pir) {
49 }
Jeffrey Huangad213742019-12-16 13:50:06 -080050
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070051 wp<ConfigManager> mConfigManager;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080052 ConfigKey mConfigKey;
53 shared_ptr<IPendingIntentRef> mPir;
Jeffrey Huangad213742019-12-16 13:50:06 -080054};
55
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -080056void ConfigManager::configReceiverDied(void* cookie) {
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080057 auto cookie_ = static_cast<ConfigReceiverDeathCookie*>(cookie);
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070058 sp<ConfigManager> thiz = cookie_->mConfigManager.promote();
59 if (!thiz) {
60 return;
61 }
62
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -080063 ConfigKey& configKey = cookie_->mConfigKey;
64 shared_ptr<IPendingIntentRef>& pir = cookie_->mPir;
Jeffrey Huang47537a12020-01-06 15:35:34 -080065
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -080066 // Erase the mapping from the config key to the config receiver (pir) if the
67 // mapping still exists.
68 lock_guard<mutex> lock(thiz->mMutex);
69 auto it = thiz->mConfigReceivers.find(configKey);
70 if (it != thiz->mConfigReceivers.end() && it->second == pir) {
71 thiz->mConfigReceivers.erase(configKey);
Jeffrey Huang47537a12020-01-06 15:35:34 -080072 }
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -080073
74 // The death recipient corresponding to this specific pir can never be
75 // triggered again, so free up resources.
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080076 delete cookie_;
77}
78
79struct ActiveConfigChangedReceiverDeathCookie {
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070080 ActiveConfigChangedReceiverDeathCookie(const wp<ConfigManager>& configManager, const int uid,
81 const shared_ptr<IPendingIntentRef>& pir) :
82 mConfigManager(configManager), mUid(uid), mPir(pir) {
83 }
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080084
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070085 wp<ConfigManager> mConfigManager;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080086 int mUid;
87 shared_ptr<IPendingIntentRef> mPir;
Jeffrey Huang47537a12020-01-06 15:35:34 -080088};
89
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -080090void ConfigManager::activeConfigChangedReceiverDied(void* cookie) {
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080091 auto cookie_ = static_cast<ActiveConfigChangedReceiverDeathCookie*>(cookie);
Ruchir Rastogie20c53e2020-04-05 21:44:31 -070092 sp<ConfigManager> thiz = cookie_->mConfigManager.promote();
93 if (!thiz) {
94 return;
95 }
96
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080097 int uid = cookie_->mUid;
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -080098 shared_ptr<IPendingIntentRef>& pir = cookie_->mPir;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080099
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800100 // Erase the mapping from the config key to the active config changed
101 // receiver (pir) if the mapping still exists.
102 lock_guard<mutex> lock(thiz->mMutex);
103 auto it = thiz->mActiveConfigsChangedReceivers.find(uid);
104 if (it != thiz->mActiveConfigsChangedReceivers.end() && it->second == pir) {
105 thiz->mActiveConfigsChangedReceivers.erase(uid);
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800106 }
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800107
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800108 // The death recipient corresponding to this specific pir can never
109 // be triggered again, so free up resources.
110 delete cookie_;
111}
112
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800113ConfigManager::ConfigManager() :
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800114 mConfigReceiverDeathRecipient(AIBinder_DeathRecipient_new(configReceiverDied)),
115 mActiveConfigChangedReceiverDeathRecipient(
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800116 AIBinder_DeathRecipient_new(activeConfigChangedReceiverDied)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700117}
118
119ConfigManager::~ConfigManager() {
120}
121
122void ConfigManager::Startup() {
Yao Chenf09569f2017-12-13 17:00:51 -0800123 map<ConfigKey, StatsdConfig> configsFromDisk;
124 StorageManager::readConfigFromDisk(configsFromDisk);
yro469cd802018-01-04 14:57:45 -0800125 for (const auto& pair : configsFromDisk) {
126 UpdateConfig(pair.first, pair.second);
127 }
128}
Yao Chen3c0b95c2017-12-16 14:34:20 -0800129
yro469cd802018-01-04 14:57:45 -0800130void ConfigManager::StartupForTest() {
Kelly Rossmoyer6f37c912020-07-29 21:22:15 +0000131 // No-op function to avoid reading configs from disks for tests.
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700132}
133
134void ConfigManager::AddListener(const sp<ConfigListener>& listener) {
David Chenfdc123b2018-02-09 17:21:48 -0800135 lock_guard<mutex> lock(mMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700136 mListeners.push_back(listener);
137}
138
139void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
David Chenfdc123b2018-02-09 17:21:48 -0800140 vector<sp<ConfigListener>> broadcastList;
141 {
142 lock_guard <mutex> lock(mMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700143
yro44907652018-03-12 20:44:05 -0700144 const int numBytes = config.ByteSize();
145 vector<uint8_t> buffer(numBytes);
146 config.SerializeToArray(&buffer[0], numBytes);
147
Yao Chen52b478b2018-03-27 10:59:45 -0700148 auto uidIt = mConfigs.find(key.GetUid());
149 // GuardRail: Limit the number of configs per uid.
150 if (uidIt != mConfigs.end()) {
151 auto it = uidIt->second.find(key);
152 if (it == uidIt->second.end() &&
153 uidIt->second.size() >= StatsdStats::kMaxConfigCountPerUid) {
154 ALOGE("ConfigManager: uid %d has exceeded the config count limit", key.GetUid());
155 return;
156 }
157 }
yro44907652018-03-12 20:44:05 -0700158
Yao Chen52b478b2018-03-27 10:59:45 -0700159 // Check if it's a duplicate config.
160 if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end() &&
161 StorageManager::hasIdenticalConfig(key, buffer)) {
162 // This is a duplicate config.
163 ALOGI("ConfigManager This is a duplicate config %s", key.ToString().c_str());
164 // Update saved file on disk. We still update timestamp of file when
165 // there exists a duplicate configuration to avoid garbage collection.
166 update_saved_configs_locked(key, buffer, numBytes);
167 return;
168 }
169
170 // Update saved file on disk.
yro44907652018-03-12 20:44:05 -0700171 update_saved_configs_locked(key, buffer, numBytes);
172
Yao Chen52b478b2018-03-27 10:59:45 -0700173 // Add to set.
174 mConfigs[key.GetUid()].insert(key);
David Chenfdc123b2018-02-09 17:21:48 -0800175
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800176 for (const sp<ConfigListener>& listener : mListeners) {
David Chenfdc123b2018-02-09 17:21:48 -0800177 broadcastList.push_back(listener);
178 }
179 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700180
Yangster-macc04feba2018-04-02 14:37:33 -0700181 const int64_t timestampNs = getElapsedRealtimeNs();
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700182 // Tell everyone
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800183 for (const sp<ConfigListener>& listener : broadcastList) {
Yangster-macc04feba2018-04-02 14:37:33 -0700184 listener->OnConfigUpdated(timestampNs, key, config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700185 }
186}
187
Jeffrey Huangad213742019-12-16 13:50:06 -0800188void ConfigManager::SetConfigReceiver(const ConfigKey& key,
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800189 const shared_ptr<IPendingIntentRef>& pir) {
David Chenfdc123b2018-02-09 17:21:48 -0800190 lock_guard<mutex> lock(mMutex);
Jeffrey Huangad213742019-12-16 13:50:06 -0800191 mConfigReceivers[key] = pir;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800192 AIBinder_linkToDeath(pir->asBinder().get(), mConfigReceiverDeathRecipient.get(),
193 new ConfigReceiverDeathCookie(this, key, pir));
David Chenadaf8b32017-11-03 15:42:08 -0700194}
195
196void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) {
David Chenfdc123b2018-02-09 17:21:48 -0800197 lock_guard<mutex> lock(mMutex);
David Chenadaf8b32017-11-03 15:42:08 -0700198 mConfigReceivers.erase(key);
199}
200
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800201void ConfigManager::SetActiveConfigsChangedReceiver(const int uid,
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800202 const shared_ptr<IPendingIntentRef>& pir) {
Ruchir Rastogi1497e8f2020-03-03 16:04:42 -0800203 {
204 lock_guard<mutex> lock(mMutex);
205 mActiveConfigsChangedReceivers[uid] = pir;
206 }
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800207 AIBinder_linkToDeath(pir->asBinder().get(), mActiveConfigChangedReceiverDeathRecipient.get(),
208 new ActiveConfigChangedReceiverDeathCookie(this, uid, pir));
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800209}
210
211void ConfigManager::RemoveActiveConfigsChangedReceiver(const int uid) {
212 lock_guard<mutex> lock(mMutex);
213 mActiveConfigsChangedReceivers.erase(uid);
214}
215
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700216void ConfigManager::RemoveConfig(const ConfigKey& key) {
David Chenfdc123b2018-02-09 17:21:48 -0800217 vector<sp<ConfigListener>> broadcastList;
218 {
219 lock_guard <mutex> lock(mMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700220
Tej Singh6ede28b2019-01-29 17:06:54 -0800221 auto uid = key.GetUid();
222 auto uidIt = mConfigs.find(uid);
Yao Chen52b478b2018-03-27 10:59:45 -0700223 if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end()) {
David Chenfdc123b2018-02-09 17:21:48 -0800224 // Remove from map
Yao Chen52b478b2018-03-27 10:59:45 -0700225 uidIt->second.erase(key);
Tej Singh6ede28b2019-01-29 17:06:54 -0800226
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800227 for (const sp<ConfigListener>& listener : mListeners) {
David Chenfdc123b2018-02-09 17:21:48 -0800228 broadcastList.push_back(listener);
229 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700230 }
David Chenfdc123b2018-02-09 17:21:48 -0800231
David Chenfdc123b2018-02-09 17:21:48 -0800232 // Remove from disk. There can still be a lingering file on disk so we check
233 // whether or not the config was on memory.
234 remove_saved_configs(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700235 }
yro9c98c052017-11-19 14:33:56 -0800236
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800237 for (const sp<ConfigListener>& listener:broadcastList) {
David Chenfdc123b2018-02-09 17:21:48 -0800238 listener->OnConfigRemoved(key);
239 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700240}
241
yro87d983c2017-11-14 21:31:43 -0800242void ConfigManager::remove_saved_configs(const ConfigKey& key) {
yrof4ae56b2018-02-13 22:06:34 -0800243 string suffix = StringPrintf("%d_%lld", key.GetUid(), (long long)key.GetId());
yroe5f82922018-01-22 18:37:27 -0800244 StorageManager::deleteSuffixedFiles(STATS_SERVICE_DIR, suffix.c_str());
yro87d983c2017-11-14 21:31:43 -0800245}
246
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700247void ConfigManager::RemoveConfigs(int uid) {
248 vector<ConfigKey> removed;
David Chenfdc123b2018-02-09 17:21:48 -0800249 vector<sp<ConfigListener>> broadcastList;
250 {
251 lock_guard <mutex> lock(mMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700252
Yao Chen52b478b2018-03-27 10:59:45 -0700253 auto uidIt = mConfigs.find(uid);
254 if (uidIt == mConfigs.end()) {
255 return;
256 }
257
258 for (auto it = uidIt->second.begin(); it != uidIt->second.end(); ++it) {
David Chenfdc123b2018-02-09 17:21:48 -0800259 // Remove from map
David Chenfdc123b2018-02-09 17:21:48 -0800260 remove_saved_configs(*it);
261 removed.push_back(*it);
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800262 }
263
Yao Chen52b478b2018-03-27 10:59:45 -0700264 mConfigs.erase(uidIt);
265
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800266 for (const sp<ConfigListener>& listener : mListeners) {
David Chenfdc123b2018-02-09 17:21:48 -0800267 broadcastList.push_back(listener);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700268 }
269 }
270
271 // Remove separately so if they do anything in the callback they can't mess up our iteration.
272 for (auto& key : removed) {
273 // Tell everyone
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800274 for (const sp<ConfigListener>& listener:broadcastList) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700275 listener->OnConfigRemoved(key);
276 }
277 }
278}
279
yro74fed972017-11-27 14:42:42 -0800280void ConfigManager::RemoveAllConfigs() {
281 vector<ConfigKey> removed;
David Chenfdc123b2018-02-09 17:21:48 -0800282 vector<sp<ConfigListener>> broadcastList;
283 {
284 lock_guard <mutex> lock(mMutex);
yro74fed972017-11-27 14:42:42 -0800285
Yao Chen52b478b2018-03-27 10:59:45 -0700286 for (auto uidIt = mConfigs.begin(); uidIt != mConfigs.end();) {
287 for (auto it = uidIt->second.begin(); it != uidIt->second.end();) {
288 // Remove from map
289 removed.push_back(*it);
290 it = uidIt->second.erase(it);
David Chenfdc123b2018-02-09 17:21:48 -0800291 }
Yao Chen52b478b2018-03-27 10:59:45 -0700292 uidIt = mConfigs.erase(uidIt);
yro74fed972017-11-27 14:42:42 -0800293 }
David Chenfdc123b2018-02-09 17:21:48 -0800294
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800295 for (const sp<ConfigListener>& listener : mListeners) {
David Chenfdc123b2018-02-09 17:21:48 -0800296 broadcastList.push_back(listener);
297 }
yro74fed972017-11-27 14:42:42 -0800298 }
299
300 // Remove separately so if they do anything in the callback they can't mess up our iteration.
301 for (auto& key : removed) {
302 // Tell everyone
Chih-Hung Hsieha1b644e2018-12-11 11:09:20 -0800303 for (const sp<ConfigListener>& listener:broadcastList) {
yro74fed972017-11-27 14:42:42 -0800304 listener->OnConfigRemoved(key);
305 }
306 }
307}
308
Yangster7c334a12017-11-22 14:24:24 -0800309vector<ConfigKey> ConfigManager::GetAllConfigKeys() const {
David Chenfdc123b2018-02-09 17:21:48 -0800310 lock_guard<mutex> lock(mMutex);
311
David Chen1d7b0cd2017-11-15 14:20:04 -0800312 vector<ConfigKey> ret;
Yao Chen52b478b2018-03-27 10:59:45 -0700313 for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) {
314 for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) {
315 ret.push_back(*it);
316 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800317 }
318 return ret;
319}
320
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800321const shared_ptr<IPendingIntentRef> ConfigManager::GetConfigReceiver(const ConfigKey& key) const {
David Chenfdc123b2018-02-09 17:21:48 -0800322 lock_guard<mutex> lock(mMutex);
323
David Chen1d7b0cd2017-11-15 14:20:04 -0800324 auto it = mConfigReceivers.find(key);
325 if (it == mConfigReceivers.end()) {
David Chen661f7912018-01-22 17:46:24 -0800326 return nullptr;
David Chen1d7b0cd2017-11-15 14:20:04 -0800327 } else {
328 return it->second;
329 }
330}
331
Ruchir Rastogie449b0c2020-02-10 17:40:09 -0800332const shared_ptr<IPendingIntentRef> ConfigManager::GetActiveConfigsChangedReceiver(const int uid)
333 const {
Tej Singh2c9ef2a2019-01-22 11:33:51 -0800334 lock_guard<mutex> lock(mMutex);
335
336 auto it = mActiveConfigsChangedReceivers.find(uid);
337 if (it == mActiveConfigsChangedReceivers.end()) {
338 return nullptr;
339 } else {
340 return it->second;
341 }
342}
343
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700344void ConfigManager::Dump(FILE* out) {
David Chenfdc123b2018-02-09 17:21:48 -0800345 lock_guard<mutex> lock(mMutex);
346
Yao Chen52b478b2018-03-27 10:59:45 -0700347 fprintf(out, "CONFIGURATIONS\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700348 fprintf(out, " uid name\n");
Yao Chen52b478b2018-03-27 10:59:45 -0700349 for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) {
350 for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) {
351 fprintf(out, " %6d %lld\n", it->GetUid(), (long long)it->GetId());
352 auto receiverIt = mConfigReceivers.find(*it);
353 if (receiverIt != mConfigReceivers.end()) {
354 fprintf(out, " -> received by PendingIntent as binder\n");
355 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800356 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700357 }
358}
359
yro44907652018-03-12 20:44:05 -0700360void ConfigManager::update_saved_configs_locked(const ConfigKey& key,
361 const vector<uint8_t>& buffer,
362 const int numBytes) {
yro9c98c052017-11-19 14:33:56 -0800363 // If there is a pre-existing config with same key we should first delete it.
364 remove_saved_configs(key);
365
366 // Then we save the latest config.
yro44907652018-03-12 20:44:05 -0700367 string file_name =
368 StringPrintf("%s/%ld_%d_%lld", STATS_SERVICE_DIR, time(nullptr),
369 key.GetUid(), (long long)key.GetId());
yro947fbce2017-11-15 22:50:23 -0800370 StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700371}
372
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700373} // namespace statsd
374} // namespace os
375} // namespace android