Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 17 | #define DEBUG false // STOPSHIP if true |
| 18 | #include "Log.h" |
| 19 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "config/ConfigManager.h" |
yro | 1528e0f | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 21 | #include "storage/StorageManager.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 23 | #include "guardrail/StatsdStats.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 24 | #include "stats_util.h" |
| 25 | |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 26 | #include <android-base/file.h> |
| 27 | #include <dirent.h> |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 29 | #include <vector> |
| 30 | #include "android-base/stringprintf.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace statsd { |
| 35 | |
Yao Chen | a277da3 | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 36 | using std::map; |
| 37 | using std::pair; |
| 38 | using std::set; |
| 39 | using std::string; |
| 40 | using std::vector; |
| 41 | |
yro | 0684915 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 42 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 43 | |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 44 | using android::base::StringPrintf; |
| 45 | using std::unique_ptr; |
| 46 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 47 | ConfigManager::ConfigManager() { |
| 48 | } |
| 49 | |
| 50 | ConfigManager::~ConfigManager() { |
| 51 | } |
| 52 | |
| 53 | void ConfigManager::Startup() { |
Yao Chen | a277da3 | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 54 | map<ConfigKey, StatsdConfig> configsFromDisk; |
| 55 | StorageManager::readConfigFromDisk(configsFromDisk); |
yro | 1d5bb08 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 56 | for (const auto& pair : configsFromDisk) { |
| 57 | UpdateConfig(pair.first, pair.second); |
| 58 | } |
| 59 | } |
Yao Chen | 0cf6189 | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 60 | |
yro | 1d5bb08 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 61 | void ConfigManager::StartupForTest() { |
| 62 | // Dummy function to avoid reading configs from disks for tests. |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void ConfigManager::AddListener(const sp<ConfigListener>& listener) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 66 | lock_guard<mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 67 | mListeners.push_back(listener); |
| 68 | } |
| 69 | |
| 70 | void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 71 | vector<sp<ConfigListener>> broadcastList; |
| 72 | { |
| 73 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 74 | |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 75 | const int numBytes = config.ByteSize(); |
| 76 | vector<uint8_t> buffer(numBytes); |
| 77 | config.SerializeToArray(&buffer[0], numBytes); |
| 78 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 79 | auto uidIt = mConfigs.find(key.GetUid()); |
| 80 | // GuardRail: Limit the number of configs per uid. |
| 81 | if (uidIt != mConfigs.end()) { |
| 82 | auto it = uidIt->second.find(key); |
| 83 | if (it == uidIt->second.end() && |
| 84 | uidIt->second.size() >= StatsdStats::kMaxConfigCountPerUid) { |
| 85 | ALOGE("ConfigManager: uid %d has exceeded the config count limit", key.GetUid()); |
| 86 | return; |
| 87 | } |
| 88 | } |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 89 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 90 | // Check if it's a duplicate config. |
| 91 | if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end() && |
| 92 | StorageManager::hasIdenticalConfig(key, buffer)) { |
| 93 | // This is a duplicate config. |
| 94 | ALOGI("ConfigManager This is a duplicate config %s", key.ToString().c_str()); |
| 95 | // Update saved file on disk. We still update timestamp of file when |
| 96 | // there exists a duplicate configuration to avoid garbage collection. |
| 97 | update_saved_configs_locked(key, buffer, numBytes); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // Update saved file on disk. |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 102 | update_saved_configs_locked(key, buffer, numBytes); |
| 103 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 104 | // Add to set. |
| 105 | mConfigs[key.GetUid()].insert(key); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 106 | |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 107 | for (sp<ConfigListener> listener : mListeners) { |
| 108 | broadcastList.push_back(listener); |
| 109 | } |
| 110 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 111 | |
| 112 | // Tell everyone |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 113 | for (sp<ConfigListener> listener:broadcastList) { |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 114 | listener->OnConfigUpdated(key, config); |
| 115 | } |
| 116 | } |
| 117 | |
David Chen | 29ada1e | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 118 | void ConfigManager::SetConfigReceiver(const ConfigKey& key, const sp<IBinder>& intentSender) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 119 | lock_guard<mutex> lock(mMutex); |
David Chen | 29ada1e | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 120 | mConfigReceivers[key] = intentSender; |
David Chen | ec84d13 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 124 | lock_guard<mutex> lock(mMutex); |
David Chen | ec84d13 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 125 | mConfigReceivers.erase(key); |
| 126 | } |
| 127 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 128 | void ConfigManager::RemoveConfig(const ConfigKey& key) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 129 | vector<sp<ConfigListener>> broadcastList; |
| 130 | { |
| 131 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 132 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 133 | auto uidIt = mConfigs.find(key.GetUid()); |
| 134 | if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end()) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 135 | // Remove from map |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 136 | uidIt->second.erase(key); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 137 | for (sp<ConfigListener> listener : mListeners) { |
| 138 | broadcastList.push_back(listener); |
| 139 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 140 | } |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 141 | |
| 142 | auto itReceiver = mConfigReceivers.find(key); |
| 143 | if (itReceiver != mConfigReceivers.end()) { |
| 144 | // Remove from map |
| 145 | mConfigReceivers.erase(itReceiver); |
| 146 | } |
| 147 | |
| 148 | // Remove from disk. There can still be a lingering file on disk so we check |
| 149 | // whether or not the config was on memory. |
| 150 | remove_saved_configs(key); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 151 | } |
yro | 9bb4f7d | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 152 | |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 153 | for (sp<ConfigListener> listener:broadcastList) { |
| 154 | listener->OnConfigRemoved(key); |
| 155 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 156 | } |
| 157 | |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 158 | void ConfigManager::remove_saved_configs(const ConfigKey& key) { |
yro | 0bd9aa1 | 2018-02-13 22:06:34 -0800 | [diff] [blame] | 159 | string suffix = StringPrintf("%d_%lld", key.GetUid(), (long long)key.GetId()); |
yro | a8c4ebf | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 160 | StorageManager::deleteSuffixedFiles(STATS_SERVICE_DIR, suffix.c_str()); |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 163 | void ConfigManager::RemoveConfigs(int uid) { |
| 164 | vector<ConfigKey> removed; |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 165 | vector<sp<ConfigListener>> broadcastList; |
| 166 | { |
| 167 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 168 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 169 | auto uidIt = mConfigs.find(uid); |
| 170 | if (uidIt == mConfigs.end()) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | for (auto it = uidIt->second.begin(); it != uidIt->second.end(); ++it) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 175 | // Remove from map |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 176 | remove_saved_configs(*it); |
| 177 | removed.push_back(*it); |
| 178 | mConfigReceivers.erase(*it); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 181 | mConfigs.erase(uidIt); |
| 182 | |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 183 | for (sp<ConfigListener> listener : mListeners) { |
| 184 | broadcastList.push_back(listener); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 189 | for (auto& key : removed) { |
| 190 | // Tell everyone |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 191 | for (sp<ConfigListener> listener:broadcastList) { |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 192 | listener->OnConfigRemoved(key); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 197 | void ConfigManager::RemoveAllConfigs() { |
| 198 | vector<ConfigKey> removed; |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 199 | vector<sp<ConfigListener>> broadcastList; |
| 200 | { |
| 201 | lock_guard <mutex> lock(mMutex); |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 202 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 203 | for (auto uidIt = mConfigs.begin(); uidIt != mConfigs.end();) { |
| 204 | for (auto it = uidIt->second.begin(); it != uidIt->second.end();) { |
| 205 | // Remove from map |
| 206 | removed.push_back(*it); |
| 207 | it = uidIt->second.erase(it); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 208 | } |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 209 | uidIt = mConfigs.erase(uidIt); |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 210 | } |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 211 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 212 | mConfigReceivers.clear(); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 213 | for (sp<ConfigListener> listener : mListeners) { |
| 214 | broadcastList.push_back(listener); |
| 215 | } |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 219 | for (auto& key : removed) { |
| 220 | // Tell everyone |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 221 | for (sp<ConfigListener> listener:broadcastList) { |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 222 | listener->OnConfigRemoved(key); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Yangster | d1df8ed | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 227 | vector<ConfigKey> ConfigManager::GetAllConfigKeys() const { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 228 | lock_guard<mutex> lock(mMutex); |
| 229 | |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 230 | vector<ConfigKey> ret; |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 231 | for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) { |
| 232 | for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) { |
| 233 | ret.push_back(*it); |
| 234 | } |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 235 | } |
| 236 | return ret; |
| 237 | } |
| 238 | |
David Chen | 29ada1e | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 239 | const sp<android::IBinder> ConfigManager::GetConfigReceiver(const ConfigKey& key) const { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 240 | lock_guard<mutex> lock(mMutex); |
| 241 | |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 242 | auto it = mConfigReceivers.find(key); |
| 243 | if (it == mConfigReceivers.end()) { |
David Chen | 29ada1e | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 244 | return nullptr; |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 245 | } else { |
| 246 | return it->second; |
| 247 | } |
| 248 | } |
| 249 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 250 | void ConfigManager::Dump(FILE* out) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 251 | lock_guard<mutex> lock(mMutex); |
| 252 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 253 | fprintf(out, "CONFIGURATIONS\n"); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 254 | fprintf(out, " uid name\n"); |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame^] | 255 | for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) { |
| 256 | for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) { |
| 257 | fprintf(out, " %6d %lld\n", it->GetUid(), (long long)it->GetId()); |
| 258 | auto receiverIt = mConfigReceivers.find(*it); |
| 259 | if (receiverIt != mConfigReceivers.end()) { |
| 260 | fprintf(out, " -> received by PendingIntent as binder\n"); |
| 261 | } |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 262 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 266 | void ConfigManager::update_saved_configs_locked(const ConfigKey& key, |
| 267 | const vector<uint8_t>& buffer, |
| 268 | const int numBytes) { |
yro | 9bb4f7d | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 269 | // If there is a pre-existing config with same key we should first delete it. |
| 270 | remove_saved_configs(key); |
| 271 | |
| 272 | // Then we save the latest config. |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 273 | string file_name = |
| 274 | StringPrintf("%s/%ld_%d_%lld", STATS_SERVICE_DIR, time(nullptr), |
| 275 | key.GetUid(), (long long)key.GetId()); |
yro | 1528e0f | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 276 | StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 279 | } // namespace statsd |
| 280 | } // namespace os |
| 281 | } // namespace android |