blob: 16b7e79c0ed7e16e9eb5ad2972859aca42247946 [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
yro87d983c2017-11-14 21:31:43 -080028#include <android-base/file.h>
29#include <dirent.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070030#include <stdio.h>
yro87d983c2017-11-14 21:31:43 -080031#include <vector>
32#include "android-base/stringprintf.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070033
34namespace android {
35namespace os {
36namespace statsd {
37
Yao Chenf09569f2017-12-13 17:00:51 -080038using std::map;
39using std::pair;
40using std::set;
41using std::string;
42using std::vector;
43
yro03faf092017-12-12 00:17:50 -080044#define STATS_SERVICE_DIR "/data/misc/stats-service"
yro87d983c2017-11-14 21:31:43 -080045
yro87d983c2017-11-14 21:31:43 -080046using android::base::StringPrintf;
47using std::unique_ptr;
48
Joe Onorato9fc9edf2017-10-15 20:08:52 -070049ConfigManager::ConfigManager() {
50}
51
52ConfigManager::~ConfigManager() {
53}
54
55void ConfigManager::Startup() {
Yao Chenf09569f2017-12-13 17:00:51 -080056 map<ConfigKey, StatsdConfig> configsFromDisk;
57 StorageManager::readConfigFromDisk(configsFromDisk);
yro469cd802018-01-04 14:57:45 -080058 for (const auto& pair : configsFromDisk) {
59 UpdateConfig(pair.first, pair.second);
60 }
61}
Yao Chen3c0b95c2017-12-16 14:34:20 -080062
yro469cd802018-01-04 14:57:45 -080063void ConfigManager::StartupForTest() {
64 // Dummy function to avoid reading configs from disks for tests.
Joe Onorato9fc9edf2017-10-15 20:08:52 -070065}
66
67void ConfigManager::AddListener(const sp<ConfigListener>& listener) {
David Chenfdc123b2018-02-09 17:21:48 -080068 lock_guard<mutex> lock(mMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070069 mListeners.push_back(listener);
70}
71
72void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) {
David Chenfdc123b2018-02-09 17:21:48 -080073 vector<sp<ConfigListener>> broadcastList;
74 {
75 lock_guard <mutex> lock(mMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070076
yro44907652018-03-12 20:44:05 -070077 const int numBytes = config.ByteSize();
78 vector<uint8_t> buffer(numBytes);
79 config.SerializeToArray(&buffer[0], numBytes);
80
Yao Chen52b478b2018-03-27 10:59:45 -070081 auto uidIt = mConfigs.find(key.GetUid());
82 // GuardRail: Limit the number of configs per uid.
83 if (uidIt != mConfigs.end()) {
84 auto it = uidIt->second.find(key);
85 if (it == uidIt->second.end() &&
86 uidIt->second.size() >= StatsdStats::kMaxConfigCountPerUid) {
87 ALOGE("ConfigManager: uid %d has exceeded the config count limit", key.GetUid());
88 return;
89 }
90 }
yro44907652018-03-12 20:44:05 -070091
Yao Chen52b478b2018-03-27 10:59:45 -070092 // Check if it's a duplicate config.
93 if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end() &&
94 StorageManager::hasIdenticalConfig(key, buffer)) {
95 // This is a duplicate config.
96 ALOGI("ConfigManager This is a duplicate config %s", key.ToString().c_str());
97 // Update saved file on disk. We still update timestamp of file when
98 // there exists a duplicate configuration to avoid garbage collection.
99 update_saved_configs_locked(key, buffer, numBytes);
100 return;
101 }
102
103 // Update saved file on disk.
yro44907652018-03-12 20:44:05 -0700104 update_saved_configs_locked(key, buffer, numBytes);
105
Yao Chen52b478b2018-03-27 10:59:45 -0700106 // Add to set.
107 mConfigs[key.GetUid()].insert(key);
David Chenfdc123b2018-02-09 17:21:48 -0800108
David Chenfdc123b2018-02-09 17:21:48 -0800109 for (sp<ConfigListener> listener : mListeners) {
110 broadcastList.push_back(listener);
111 }
112 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700113
Yangster-macc04feba2018-04-02 14:37:33 -0700114 const int64_t timestampNs = getElapsedRealtimeNs();
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700115 // Tell everyone
Yangster-macb142cc82018-03-30 15:22:08 -0700116 for (sp<ConfigListener> listener : broadcastList) {
Yangster-macc04feba2018-04-02 14:37:33 -0700117 listener->OnConfigUpdated(timestampNs, key, config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700118 }
119}
120
David Chen661f7912018-01-22 17:46:24 -0800121void ConfigManager::SetConfigReceiver(const ConfigKey& key, const sp<IBinder>& intentSender) {
David Chenfdc123b2018-02-09 17:21:48 -0800122 lock_guard<mutex> lock(mMutex);
David Chen661f7912018-01-22 17:46:24 -0800123 mConfigReceivers[key] = intentSender;
David Chenadaf8b32017-11-03 15:42:08 -0700124}
125
126void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) {
David Chenfdc123b2018-02-09 17:21:48 -0800127 lock_guard<mutex> lock(mMutex);
David Chenadaf8b32017-11-03 15:42:08 -0700128 mConfigReceivers.erase(key);
129}
130
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700131void ConfigManager::RemoveConfig(const ConfigKey& key) {
David Chenfdc123b2018-02-09 17:21:48 -0800132 vector<sp<ConfigListener>> broadcastList;
133 {
134 lock_guard <mutex> lock(mMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700135
Yao Chen52b478b2018-03-27 10:59:45 -0700136 auto uidIt = mConfigs.find(key.GetUid());
137 if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end()) {
David Chenfdc123b2018-02-09 17:21:48 -0800138 // Remove from map
Yao Chen52b478b2018-03-27 10:59:45 -0700139 uidIt->second.erase(key);
David Chenfdc123b2018-02-09 17:21:48 -0800140 for (sp<ConfigListener> listener : mListeners) {
141 broadcastList.push_back(listener);
142 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700143 }
David Chenfdc123b2018-02-09 17:21:48 -0800144
145 auto itReceiver = mConfigReceivers.find(key);
146 if (itReceiver != mConfigReceivers.end()) {
147 // Remove from map
148 mConfigReceivers.erase(itReceiver);
149 }
150
151 // Remove from disk. There can still be a lingering file on disk so we check
152 // whether or not the config was on memory.
153 remove_saved_configs(key);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700154 }
yro9c98c052017-11-19 14:33:56 -0800155
David Chenfdc123b2018-02-09 17:21:48 -0800156 for (sp<ConfigListener> listener:broadcastList) {
157 listener->OnConfigRemoved(key);
158 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700159}
160
yro87d983c2017-11-14 21:31:43 -0800161void ConfigManager::remove_saved_configs(const ConfigKey& key) {
yrof4ae56b2018-02-13 22:06:34 -0800162 string suffix = StringPrintf("%d_%lld", key.GetUid(), (long long)key.GetId());
yroe5f82922018-01-22 18:37:27 -0800163 StorageManager::deleteSuffixedFiles(STATS_SERVICE_DIR, suffix.c_str());
yro87d983c2017-11-14 21:31:43 -0800164}
165
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700166void ConfigManager::RemoveConfigs(int uid) {
167 vector<ConfigKey> removed;
David Chenfdc123b2018-02-09 17:21:48 -0800168 vector<sp<ConfigListener>> broadcastList;
169 {
170 lock_guard <mutex> lock(mMutex);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700171
Yao Chen52b478b2018-03-27 10:59:45 -0700172 auto uidIt = mConfigs.find(uid);
173 if (uidIt == mConfigs.end()) {
174 return;
175 }
176
177 for (auto it = uidIt->second.begin(); it != uidIt->second.end(); ++it) {
David Chenfdc123b2018-02-09 17:21:48 -0800178 // Remove from map
David Chenfdc123b2018-02-09 17:21:48 -0800179 remove_saved_configs(*it);
180 removed.push_back(*it);
181 mConfigReceivers.erase(*it);
David Chenfdc123b2018-02-09 17:21:48 -0800182 }
183
Yao Chen52b478b2018-03-27 10:59:45 -0700184 mConfigs.erase(uidIt);
185
David Chenfdc123b2018-02-09 17:21:48 -0800186 for (sp<ConfigListener> listener : mListeners) {
187 broadcastList.push_back(listener);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700188 }
189 }
190
191 // Remove separately so if they do anything in the callback they can't mess up our iteration.
192 for (auto& key : removed) {
193 // Tell everyone
David Chenfdc123b2018-02-09 17:21:48 -0800194 for (sp<ConfigListener> listener:broadcastList) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700195 listener->OnConfigRemoved(key);
196 }
197 }
198}
199
yro74fed972017-11-27 14:42:42 -0800200void ConfigManager::RemoveAllConfigs() {
201 vector<ConfigKey> removed;
David Chenfdc123b2018-02-09 17:21:48 -0800202 vector<sp<ConfigListener>> broadcastList;
203 {
204 lock_guard <mutex> lock(mMutex);
yro74fed972017-11-27 14:42:42 -0800205
Yao Chen52b478b2018-03-27 10:59:45 -0700206 for (auto uidIt = mConfigs.begin(); uidIt != mConfigs.end();) {
207 for (auto it = uidIt->second.begin(); it != uidIt->second.end();) {
208 // Remove from map
209 removed.push_back(*it);
210 it = uidIt->second.erase(it);
David Chenfdc123b2018-02-09 17:21:48 -0800211 }
Yao Chen52b478b2018-03-27 10:59:45 -0700212 uidIt = mConfigs.erase(uidIt);
yro74fed972017-11-27 14:42:42 -0800213 }
David Chenfdc123b2018-02-09 17:21:48 -0800214
Yao Chen52b478b2018-03-27 10:59:45 -0700215 mConfigReceivers.clear();
David Chenfdc123b2018-02-09 17:21:48 -0800216 for (sp<ConfigListener> listener : mListeners) {
217 broadcastList.push_back(listener);
218 }
yro74fed972017-11-27 14:42:42 -0800219 }
220
221 // Remove separately so if they do anything in the callback they can't mess up our iteration.
222 for (auto& key : removed) {
223 // Tell everyone
David Chenfdc123b2018-02-09 17:21:48 -0800224 for (sp<ConfigListener> listener:broadcastList) {
yro74fed972017-11-27 14:42:42 -0800225 listener->OnConfigRemoved(key);
226 }
227 }
228}
229
Yangster7c334a12017-11-22 14:24:24 -0800230vector<ConfigKey> ConfigManager::GetAllConfigKeys() const {
David Chenfdc123b2018-02-09 17:21:48 -0800231 lock_guard<mutex> lock(mMutex);
232
David Chen1d7b0cd2017-11-15 14:20:04 -0800233 vector<ConfigKey> ret;
Yao Chen52b478b2018-03-27 10:59:45 -0700234 for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) {
235 for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) {
236 ret.push_back(*it);
237 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800238 }
239 return ret;
240}
241
David Chen661f7912018-01-22 17:46:24 -0800242const sp<android::IBinder> ConfigManager::GetConfigReceiver(const ConfigKey& key) const {
David Chenfdc123b2018-02-09 17:21:48 -0800243 lock_guard<mutex> lock(mMutex);
244
David Chen1d7b0cd2017-11-15 14:20:04 -0800245 auto it = mConfigReceivers.find(key);
246 if (it == mConfigReceivers.end()) {
David Chen661f7912018-01-22 17:46:24 -0800247 return nullptr;
David Chen1d7b0cd2017-11-15 14:20:04 -0800248 } else {
249 return it->second;
250 }
251}
252
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700253void ConfigManager::Dump(FILE* out) {
David Chenfdc123b2018-02-09 17:21:48 -0800254 lock_guard<mutex> lock(mMutex);
255
Yao Chen52b478b2018-03-27 10:59:45 -0700256 fprintf(out, "CONFIGURATIONS\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700257 fprintf(out, " uid name\n");
Yao Chen52b478b2018-03-27 10:59:45 -0700258 for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) {
259 for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) {
260 fprintf(out, " %6d %lld\n", it->GetUid(), (long long)it->GetId());
261 auto receiverIt = mConfigReceivers.find(*it);
262 if (receiverIt != mConfigReceivers.end()) {
263 fprintf(out, " -> received by PendingIntent as binder\n");
264 }
David Chen1d7b0cd2017-11-15 14:20:04 -0800265 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700266 }
267}
268
yro44907652018-03-12 20:44:05 -0700269void ConfigManager::update_saved_configs_locked(const ConfigKey& key,
270 const vector<uint8_t>& buffer,
271 const int numBytes) {
yro9c98c052017-11-19 14:33:56 -0800272 // If there is a pre-existing config with same key we should first delete it.
273 remove_saved_configs(key);
274
275 // Then we save the latest config.
yro44907652018-03-12 20:44:05 -0700276 string file_name =
277 StringPrintf("%s/%ld_%d_%lld", STATS_SERVICE_DIR, time(nullptr),
278 key.GetUid(), (long long)key.GetId());
yro947fbce2017-11-15 22:50:23 -0800279 StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700280}
281
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700282} // namespace statsd
283} // namespace os
284} // namespace android