blob: d1f93c72a86db895a143ff237e6db995d0ec8c06 [file] [log] [blame]
Joe Onoratoda8cca72017-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
satayev134c97a2022-02-09 12:45:25 +000017#define STATSD_DEBUG false // STOPSHIP if true
Yao Chen341db892018-03-27 10:59:45 -070018#include "Log.h"
19
Joe Onoratoda8cca72017-10-15 20:08:52 -070020#include "config/ConfigManager.h"
yro1528e0f2017-11-15 22:50:23 -080021#include "storage/StorageManager.h"
Joe Onoratoda8cca72017-10-15 20:08:52 -070022
Yao Chen341db892018-03-27 10:59:45 -070023#include "guardrail/StatsdStats.h"
Yangster-mace9b52eb2018-04-02 14:37:33 -070024#include "stats_log_util.h"
Joe Onoratoda8cca72017-10-15 20:08:52 -070025#include "stats_util.h"
Yangster-macf50f12b2018-03-30 15:22:08 -070026#include "stats_log_util.h"
Joe Onoratoda8cca72017-10-15 20:08:52 -070027
Joe Onoratoda8cca72017-10-15 20:08:52 -070028#include <stdio.h>
yrod3a24362017-11-14 21:31:43 -080029#include <vector>
30#include "android-base/stringprintf.h"
Joe Onoratoda8cca72017-10-15 20:08:52 -070031
32namespace android {
33namespace os {
34namespace statsd {
35
Yao Chena277da32017-12-13 17:00:51 -080036using std::string;
37using std::vector;
38
Tej Singh07407682023-02-15 02:09:54 -080039using Status = ::ndk::ScopedAStatus;
40
yro06849152017-12-12 00:17:50 -080041#define STATS_SERVICE_DIR "/data/misc/stats-service"
yrod3a24362017-11-14 21:31:43 -080042
yrod3a24362017-11-14 21:31:43 -080043using android::base::StringPrintf;
yrod3a24362017-11-14 21:31:43 -080044
Tej Singh81e38f42021-10-19 23:40:07 -070045ConfigManager::ConfigManager() {
Joe Onoratoda8cca72017-10-15 20:08:52 -070046}
47
48ConfigManager::~ConfigManager() {
49}
50
51void ConfigManager::Startup() {
Yao Chena277da32017-12-13 17:00:51 -080052 map<ConfigKey, StatsdConfig> configsFromDisk;
53 StorageManager::readConfigFromDisk(configsFromDisk);
Vova Sharaienko8d42bc32024-01-04 19:08:27 +000054 for (const auto& config : configsFromDisk) {
55 UpdateConfig(config.first, config.second);
yro1d5bb082018-01-04 14:57:45 -080056 }
57}
Yao Chen0cf61892017-12-16 14:34:20 -080058
yro1d5bb082018-01-04 14:57:45 -080059void ConfigManager::StartupForTest() {
Kelly Rossmoyera685f902020-07-29 21:22:15 +000060 // No-op function to avoid reading configs from disks for tests.
Joe Onoratoda8cca72017-10-15 20:08:52 -070061}
62
63void ConfigManager::AddListener(const sp<ConfigListener>& listener) {
David Chen687dbd12018-02-09 17:21:48 -080064 lock_guard<mutex> lock(mMutex);
Joe Onoratoda8cca72017-10-15 20:08:52 -070065 mListeners.push_back(listener);
66}
67
68void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
David Chen687dbd12018-02-09 17:21:48 -080069 vector<sp<ConfigListener>> broadcastList;
70 {
71 lock_guard <mutex> lock(mMutex);
Joe Onoratoda8cca72017-10-15 20:08:52 -070072
yro2918b552018-03-12 20:44:05 -070073 const int numBytes = config.ByteSize();
74 vector<uint8_t> buffer(numBytes);
Muhammad Qureshif4951fb2022-03-29 07:00:28 -070075 config.SerializeToArray(buffer.data(), numBytes);
yro2918b552018-03-12 20:44:05 -070076
Yao Chen341db892018-03-27 10:59:45 -070077 auto uidIt = mConfigs.find(key.GetUid());
78 // GuardRail: Limit the number of configs per uid.
79 if (uidIt != mConfigs.end()) {
80 auto it = uidIt->second.find(key);
81 if (it == uidIt->second.end() &&
82 uidIt->second.size() >= StatsdStats::kMaxConfigCountPerUid) {
83 ALOGE("ConfigManager: uid %d has exceeded the config count limit", key.GetUid());
84 return;
85 }
86 }
yro2918b552018-03-12 20:44:05 -070087
Yao Chen341db892018-03-27 10:59:45 -070088 // Check if it's a duplicate config.
89 if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end() &&
90 StorageManager::hasIdenticalConfig(key, buffer)) {
91 // This is a duplicate config.
92 ALOGI("ConfigManager This is a duplicate config %s", key.ToString().c_str());
93 // Update saved file on disk. We still update timestamp of file when
94 // there exists a duplicate configuration to avoid garbage collection.
95 update_saved_configs_locked(key, buffer, numBytes);
96 return;
97 }
98
99 // Update saved file on disk.
yro2918b552018-03-12 20:44:05 -0700100 update_saved_configs_locked(key, buffer, numBytes);
101
Yao Chen341db892018-03-27 10:59:45 -0700102 // Add to set.
103 mConfigs[key.GetUid()].insert(key);
David Chen687dbd12018-02-09 17:21:48 -0800104
Vova Sharaienkod9a033d2022-10-27 23:19:37 +0000105 broadcastList = mListeners;
David Chen687dbd12018-02-09 17:21:48 -0800106 }
Joe Onoratoda8cca72017-10-15 20:08:52 -0700107
Yangster-mace9b52eb2018-04-02 14:37:33 -0700108 const int64_t timestampNs = getElapsedRealtimeNs();
Joe Onoratoda8cca72017-10-15 20:08:52 -0700109 // Tell everyone
Chih-Hung Hsiehfa35ea82018-12-11 11:09:20 -0800110 for (const sp<ConfigListener>& listener : broadcastList) {
Yangster-mace9b52eb2018-04-02 14:37:33 -0700111 listener->OnConfigUpdated(timestampNs, key, config);
Joe Onoratoda8cca72017-10-15 20:08:52 -0700112 }
113}
114
Jeffrey Huangd0c3d342019-12-16 13:50:06 -0800115void ConfigManager::SetConfigReceiver(const ConfigKey& key,
Ruchir Rastogid36dd0b2020-02-10 17:40:09 -0800116 const shared_ptr<IPendingIntentRef>& pir) {
David Chen687dbd12018-02-09 17:21:48 -0800117 lock_guard<mutex> lock(mMutex);
Jeffrey Huangd0c3d342019-12-16 13:50:06 -0800118 mConfigReceivers[key] = pir;
David Chenec84d132017-11-03 15:42:08 -0700119}
120
121void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) {
David Chen687dbd12018-02-09 17:21:48 -0800122 lock_guard<mutex> lock(mMutex);
David Chenec84d132017-11-03 15:42:08 -0700123 mConfigReceivers.erase(key);
124}
125
Tej Singh81e38f42021-10-19 23:40:07 -0700126void ConfigManager::RemoveConfigReceiver(const ConfigKey& key,
127 const shared_ptr<IPendingIntentRef>& pir) {
128 lock_guard<mutex> lock(mMutex);
129 auto it = mConfigReceivers.find(key);
130 if (it != mConfigReceivers.end() && it->second == pir) {
131 mConfigReceivers.erase(key);
132 }
133}
134
Tej Singhfad59e32019-01-22 11:33:51 -0800135void ConfigManager::SetActiveConfigsChangedReceiver(const int uid,
Ruchir Rastogid36dd0b2020-02-10 17:40:09 -0800136 const shared_ptr<IPendingIntentRef>& pir) {
Tej Singh81e38f42021-10-19 23:40:07 -0700137 lock_guard<mutex> lock(mMutex);
138 mActiveConfigsChangedReceivers[uid] = pir;
Tej Singhfad59e32019-01-22 11:33:51 -0800139}
140
141void ConfigManager::RemoveActiveConfigsChangedReceiver(const int uid) {
142 lock_guard<mutex> lock(mMutex);
143 mActiveConfigsChangedReceivers.erase(uid);
144}
145
Tej Singh81e38f42021-10-19 23:40:07 -0700146void ConfigManager::RemoveActiveConfigsChangedReceiver(const int uid,
147 const shared_ptr<IPendingIntentRef>& pir) {
148 lock_guard<mutex> lock(mMutex);
149 auto it = mActiveConfigsChangedReceivers.find(uid);
150 if (it != mActiveConfigsChangedReceivers.end() && it->second == pir) {
151 mActiveConfigsChangedReceivers.erase(uid);
152 }
153}
154
Tej Singh07407682023-02-15 02:09:54 -0800155void ConfigManager::SetRestrictedMetricsChangedReceiver(const string& configPackage,
156 const int64_t configId,
157 const int32_t callingUid,
158 const shared_ptr<IPendingIntentRef>& pir) {
159 lock_guard<mutex> lock(mMutex);
160 ConfigKeyWithPackage configKey(configPackage, configId);
161 mRestrictedMetricsChangedReceivers[configKey][callingUid] = pir;
162}
163
164void ConfigManager::RemoveRestrictedMetricsChangedReceiver(const string& configPackage,
165 const int64_t configId,
166 const int32_t callingUid) {
167 lock_guard<mutex> lock(mMutex);
168 ConfigKeyWithPackage configKey(configPackage, configId);
169 const auto& it = mRestrictedMetricsChangedReceivers.find(configKey);
170 if (it != mRestrictedMetricsChangedReceivers.end()) {
171 it->second.erase(callingUid);
172 if (it->second.empty()) {
173 mRestrictedMetricsChangedReceivers.erase(it);
174 }
175 }
176}
177
Tej Singhd6efe1f2023-03-16 23:56:37 -0700178void ConfigManager::RemoveRestrictedMetricsChangedReceiver(
179 const ConfigKeyWithPackage& key, const int32_t delegateUid,
180 const shared_ptr<IPendingIntentRef>& pir) {
181 lock_guard<mutex> lock(mMutex);
182 const auto& it = mRestrictedMetricsChangedReceivers.find(key);
183 if (it != mRestrictedMetricsChangedReceivers.end()) {
184 const auto& pirIt = it->second.find(delegateUid);
185 if (pirIt != it->second.end() && pirIt->second == pir) {
186 it->second.erase(delegateUid);
187 if (it->second.empty()) {
188 mRestrictedMetricsChangedReceivers.erase(it);
189 }
190 }
191 }
192}
193
Tej Singh07407682023-02-15 02:09:54 -0800194void ConfigManager::SendRestrictedMetricsBroadcast(const set<string>& configPackages,
195 const int64_t configId,
196 const set<int32_t>& delegateUids,
197 const vector<int64_t>& metricIds) {
Tej Singhd6efe1f2023-03-16 23:56:37 -0700198 map<ConfigKeyWithPackage, map<int32_t, shared_ptr<IPendingIntentRef>>> intentsToSend;
Tej Singh07407682023-02-15 02:09:54 -0800199 {
200 lock_guard<mutex> lock(mMutex);
201 // Invoke the pending intent for all matching configs, as long as the listening delegates
202 // match the allowed delegate uids specified by the config.
203 for (const string& configPackage : configPackages) {
204 ConfigKeyWithPackage key(configPackage, configId);
205 const auto& it = mRestrictedMetricsChangedReceivers.find(key);
206 if (it != mRestrictedMetricsChangedReceivers.end()) {
207 for (const auto& [delegateUid, pir] : it->second) {
208 if (delegateUids.find(delegateUid) != delegateUids.end()) {
Tej Singhd6efe1f2023-03-16 23:56:37 -0700209 intentsToSend[key][delegateUid] = pir;
Tej Singh07407682023-02-15 02:09:54 -0800210 }
211 }
212 }
213 }
214 }
215
216 // Invoke the pending intents without holding the lock.
Tej Singhd6efe1f2023-03-16 23:56:37 -0700217 for (const auto& [key, innerMap] : intentsToSend) {
218 for (const auto& [delegateUid, pir] : innerMap) {
219 Status status = pir->sendRestrictedMetricsChangedBroadcast(metricIds);
220 if (status.isOk()) {
221 VLOG("ConfigManager::SendRestrictedMetricsBroadcast succeeded");
222 }
223 if (status.getExceptionCode() == EX_TRANSACTION_FAILED &&
224 status.getStatus() == STATUS_DEAD_OBJECT) {
225 // Must also be called without the lock, since remove will acquire the lock.
226 RemoveRestrictedMetricsChangedReceiver(key, delegateUid, pir);
227 }
Tej Singh07407682023-02-15 02:09:54 -0800228 }
Tej Singh07407682023-02-15 02:09:54 -0800229 }
230}
231
Joe Onoratoda8cca72017-10-15 20:08:52 -0700232void ConfigManager::RemoveConfig(const ConfigKey& key) {
David Chen687dbd12018-02-09 17:21:48 -0800233 vector<sp<ConfigListener>> broadcastList;
234 {
235 lock_guard <mutex> lock(mMutex);
Joe Onoratoda8cca72017-10-15 20:08:52 -0700236
Tej Singha783d522019-01-29 17:06:54 -0800237 auto uid = key.GetUid();
238 auto uidIt = mConfigs.find(uid);
Yao Chen341db892018-03-27 10:59:45 -0700239 if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end()) {
David Chen687dbd12018-02-09 17:21:48 -0800240 // Remove from map
Yao Chen341db892018-03-27 10:59:45 -0700241 uidIt->second.erase(key);
Tej Singha783d522019-01-29 17:06:54 -0800242
Vova Sharaienkod9a033d2022-10-27 23:19:37 +0000243 broadcastList = mListeners;
Joe Onoratoda8cca72017-10-15 20:08:52 -0700244 }
David Chen687dbd12018-02-09 17:21:48 -0800245
David Chen687dbd12018-02-09 17:21:48 -0800246 // Remove from disk. There can still be a lingering file on disk so we check
247 // whether or not the config was on memory.
248 remove_saved_configs(key);
Joe Onoratoda8cca72017-10-15 20:08:52 -0700249 }
yro9bb4f7d2017-11-19 14:33:56 -0800250
Chih-Hung Hsiehfa35ea82018-12-11 11:09:20 -0800251 for (const sp<ConfigListener>& listener:broadcastList) {
David Chen687dbd12018-02-09 17:21:48 -0800252 listener->OnConfigRemoved(key);
253 }
Joe Onoratoda8cca72017-10-15 20:08:52 -0700254}
255
yrod3a24362017-11-14 21:31:43 -0800256void ConfigManager::remove_saved_configs(const ConfigKey& key) {
yro0bd9aa12018-02-13 22:06:34 -0800257 string suffix = StringPrintf("%d_%lld", key.GetUid(), (long long)key.GetId());
yroa8c4ebf2018-01-22 18:37:27 -0800258 StorageManager::deleteSuffixedFiles(STATS_SERVICE_DIR, suffix.c_str());
yrod3a24362017-11-14 21:31:43 -0800259}
260
Tej Singh07407682023-02-15 02:09:54 -0800261// TODO(b/xxx): consider removing all receivers associated with this uid.
Joe Onoratoda8cca72017-10-15 20:08:52 -0700262void ConfigManager::RemoveConfigs(int uid) {
263 vector<ConfigKey> removed;
David Chen687dbd12018-02-09 17:21:48 -0800264 vector<sp<ConfigListener>> broadcastList;
265 {
266 lock_guard <mutex> lock(mMutex);
Joe Onoratoda8cca72017-10-15 20:08:52 -0700267
Yao Chen341db892018-03-27 10:59:45 -0700268 auto uidIt = mConfigs.find(uid);
269 if (uidIt == mConfigs.end()) {
270 return;
271 }
272
273 for (auto it = uidIt->second.begin(); it != uidIt->second.end(); ++it) {
David Chen687dbd12018-02-09 17:21:48 -0800274 // Remove from map
David Chen687dbd12018-02-09 17:21:48 -0800275 remove_saved_configs(*it);
276 removed.push_back(*it);
Tej Singhfad59e32019-01-22 11:33:51 -0800277 }
278
Yao Chen341db892018-03-27 10:59:45 -0700279 mConfigs.erase(uidIt);
280
Vova Sharaienkod9a033d2022-10-27 23:19:37 +0000281 broadcastList = mListeners;
Joe Onoratoda8cca72017-10-15 20:08:52 -0700282 }
283
284 // Remove separately so if they do anything in the callback they can't mess up our iteration.
285 for (auto& key : removed) {
286 // Tell everyone
Chih-Hung Hsiehfa35ea82018-12-11 11:09:20 -0800287 for (const sp<ConfigListener>& listener:broadcastList) {
Joe Onoratoda8cca72017-10-15 20:08:52 -0700288 listener->OnConfigRemoved(key);
289 }
290 }
291}
292
yro11e2da52017-11-27 14:42:42 -0800293void ConfigManager::RemoveAllConfigs() {
294 vector<ConfigKey> removed;
David Chen687dbd12018-02-09 17:21:48 -0800295 vector<sp<ConfigListener>> broadcastList;
296 {
297 lock_guard <mutex> lock(mMutex);
yro11e2da52017-11-27 14:42:42 -0800298
Yao Chen341db892018-03-27 10:59:45 -0700299 for (auto uidIt = mConfigs.begin(); uidIt != mConfigs.end();) {
300 for (auto it = uidIt->second.begin(); it != uidIt->second.end();) {
301 // Remove from map
302 removed.push_back(*it);
303 it = uidIt->second.erase(it);
David Chen687dbd12018-02-09 17:21:48 -0800304 }
Yao Chen341db892018-03-27 10:59:45 -0700305 uidIt = mConfigs.erase(uidIt);
yro11e2da52017-11-27 14:42:42 -0800306 }
David Chen687dbd12018-02-09 17:21:48 -0800307
Vova Sharaienkod9a033d2022-10-27 23:19:37 +0000308 broadcastList = mListeners;
yro11e2da52017-11-27 14:42:42 -0800309 }
310
311 // Remove separately so if they do anything in the callback they can't mess up our iteration.
312 for (auto& key : removed) {
313 // Tell everyone
Chih-Hung Hsiehfa35ea82018-12-11 11:09:20 -0800314 for (const sp<ConfigListener>& listener:broadcastList) {
yro11e2da52017-11-27 14:42:42 -0800315 listener->OnConfigRemoved(key);
316 }
317 }
318}
319
Yangsterd1df8ed2017-11-22 14:24:24 -0800320vector<ConfigKey> ConfigManager::GetAllConfigKeys() const {
David Chen687dbd12018-02-09 17:21:48 -0800321 lock_guard<mutex> lock(mMutex);
322
David Chendd4e8032017-11-15 14:20:04 -0800323 vector<ConfigKey> ret;
Yao Chen341db892018-03-27 10:59:45 -0700324 for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) {
325 for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) {
326 ret.push_back(*it);
327 }
David Chendd4e8032017-11-15 14:20:04 -0800328 }
329 return ret;
330}
331
Ruchir Rastogid36dd0b2020-02-10 17:40:09 -0800332const shared_ptr<IPendingIntentRef> ConfigManager::GetConfigReceiver(const ConfigKey& key) const {
David Chen687dbd12018-02-09 17:21:48 -0800333 lock_guard<mutex> lock(mMutex);
334
David Chendd4e8032017-11-15 14:20:04 -0800335 auto it = mConfigReceivers.find(key);
336 if (it == mConfigReceivers.end()) {
David Chen29ada1e2018-01-22 17:46:24 -0800337 return nullptr;
David Chendd4e8032017-11-15 14:20:04 -0800338 } else {
339 return it->second;
340 }
341}
342
Ruchir Rastogid36dd0b2020-02-10 17:40:09 -0800343const shared_ptr<IPendingIntentRef> ConfigManager::GetActiveConfigsChangedReceiver(const int uid)
344 const {
Tej Singhfad59e32019-01-22 11:33:51 -0800345 lock_guard<mutex> lock(mMutex);
346
347 auto it = mActiveConfigsChangedReceivers.find(uid);
348 if (it == mActiveConfigsChangedReceivers.end()) {
349 return nullptr;
350 } else {
351 return it->second;
352 }
353}
354
Joe Onoratoda8cca72017-10-15 20:08:52 -0700355void ConfigManager::Dump(FILE* out) {
David Chen687dbd12018-02-09 17:21:48 -0800356 lock_guard<mutex> lock(mMutex);
357
Yao Chen341db892018-03-27 10:59:45 -0700358 fprintf(out, "CONFIGURATIONS\n");
Joe Onoratoda8cca72017-10-15 20:08:52 -0700359 fprintf(out, " uid name\n");
Yao Chen341db892018-03-27 10:59:45 -0700360 for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) {
361 for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) {
362 fprintf(out, " %6d %lld\n", it->GetUid(), (long long)it->GetId());
363 auto receiverIt = mConfigReceivers.find(*it);
364 if (receiverIt != mConfigReceivers.end()) {
365 fprintf(out, " -> received by PendingIntent as binder\n");
366 }
David Chendd4e8032017-11-15 14:20:04 -0800367 }
Joe Onoratoda8cca72017-10-15 20:08:52 -0700368 }
369}
370
yro2918b552018-03-12 20:44:05 -0700371void ConfigManager::update_saved_configs_locked(const ConfigKey& key,
372 const vector<uint8_t>& buffer,
373 const int numBytes) {
yro9bb4f7d2017-11-19 14:33:56 -0800374 // If there is a pre-existing config with same key we should first delete it.
375 remove_saved_configs(key);
376
377 // Then we save the latest config.
yro2918b552018-03-12 20:44:05 -0700378 string file_name =
379 StringPrintf("%s/%ld_%d_%lld", STATS_SERVICE_DIR, time(nullptr),
380 key.GetUid(), (long long)key.GetId());
yro1528e0f2017-11-15 22:50:23 -0800381 StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes);
Joe Onoratoda8cca72017-10-15 20:08:52 -0700382}
383
Joe Onoratoda8cca72017-10-15 20:08:52 -0700384} // namespace statsd
385} // namespace os
386} // namespace android