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 | |
satayev | 134c97a | 2022-02-09 12:45:25 +0000 | [diff] [blame] | 17 | #define STATSD_DEBUG false // STOPSHIP if true |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 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" |
Yangster-mac | e9b52eb | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 24 | #include "stats_log_util.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | #include "stats_util.h" |
Yangster-mac | f50f12b | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 26 | #include "stats_log_util.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 27 | |
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::pair; |
Yao Chen | a277da3 | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 37 | using std::string; |
| 38 | using std::vector; |
| 39 | |
yro | 0684915 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 40 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 41 | |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| 43 | using std::unique_ptr; |
| 44 | |
Tej Singh | 81e38f4 | 2021-10-19 23:40:07 -0700 | [diff] [blame] | 45 | ConfigManager::ConfigManager() { |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ConfigManager::~ConfigManager() { |
| 49 | } |
| 50 | |
| 51 | void ConfigManager::Startup() { |
Yao Chen | a277da3 | 2017-12-13 17:00:51 -0800 | [diff] [blame] | 52 | map<ConfigKey, StatsdConfig> configsFromDisk; |
| 53 | StorageManager::readConfigFromDisk(configsFromDisk); |
yro | 1d5bb08 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 54 | for (const auto& pair : configsFromDisk) { |
| 55 | UpdateConfig(pair.first, pair.second); |
| 56 | } |
| 57 | } |
Yao Chen | 0cf6189 | 2017-12-16 14:34:20 -0800 | [diff] [blame] | 58 | |
yro | 1d5bb08 | 2018-01-04 14:57:45 -0800 | [diff] [blame] | 59 | void ConfigManager::StartupForTest() { |
Kelly Rossmoyer | a685f90 | 2020-07-29 21:22:15 +0000 | [diff] [blame] | 60 | // No-op function to avoid reading configs from disks for tests. |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void ConfigManager::AddListener(const sp<ConfigListener>& listener) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 64 | lock_guard<mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 65 | mListeners.push_back(listener); |
| 66 | } |
| 67 | |
| 68 | void ConfigManager::UpdateConfig(const ConfigKey& key, const StatsdConfig& config) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 69 | vector<sp<ConfigListener>> broadcastList; |
| 70 | { |
| 71 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 72 | |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 73 | const int numBytes = config.ByteSize(); |
| 74 | vector<uint8_t> buffer(numBytes); |
Muhammad Qureshi | f4951fb | 2022-03-29 07:00:28 -0700 | [diff] [blame^] | 75 | config.SerializeToArray(buffer.data(), numBytes); |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 76 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 77 | 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 | } |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 87 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 88 | // 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. |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 100 | update_saved_configs_locked(key, buffer, numBytes); |
| 101 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 102 | // Add to set. |
| 103 | mConfigs[key.GetUid()].insert(key); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 104 | |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 105 | for (const sp<ConfigListener>& listener : mListeners) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 106 | broadcastList.push_back(listener); |
| 107 | } |
| 108 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 109 | |
Yangster-mac | e9b52eb | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 110 | const int64_t timestampNs = getElapsedRealtimeNs(); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 111 | // Tell everyone |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 112 | for (const sp<ConfigListener>& listener : broadcastList) { |
Yangster-mac | e9b52eb | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 113 | listener->OnConfigUpdated(timestampNs, key, config); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Jeffrey Huang | d0c3d34 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 117 | void ConfigManager::SetConfigReceiver(const ConfigKey& key, |
Ruchir Rastogi | d36dd0b | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 118 | const shared_ptr<IPendingIntentRef>& pir) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 119 | lock_guard<mutex> lock(mMutex); |
Jeffrey Huang | d0c3d34 | 2019-12-16 13:50:06 -0800 | [diff] [blame] | 120 | mConfigReceivers[key] = pir; |
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 | |
Tej Singh | 81e38f4 | 2021-10-19 23:40:07 -0700 | [diff] [blame] | 128 | void ConfigManager::RemoveConfigReceiver(const ConfigKey& key, |
| 129 | const shared_ptr<IPendingIntentRef>& pir) { |
| 130 | lock_guard<mutex> lock(mMutex); |
| 131 | auto it = mConfigReceivers.find(key); |
| 132 | if (it != mConfigReceivers.end() && it->second == pir) { |
| 133 | mConfigReceivers.erase(key); |
| 134 | } |
| 135 | } |
| 136 | |
Tej Singh | fad59e3 | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 137 | void ConfigManager::SetActiveConfigsChangedReceiver(const int uid, |
Ruchir Rastogi | d36dd0b | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 138 | const shared_ptr<IPendingIntentRef>& pir) { |
Tej Singh | 81e38f4 | 2021-10-19 23:40:07 -0700 | [diff] [blame] | 139 | lock_guard<mutex> lock(mMutex); |
| 140 | mActiveConfigsChangedReceivers[uid] = pir; |
Tej Singh | fad59e3 | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void ConfigManager::RemoveActiveConfigsChangedReceiver(const int uid) { |
| 144 | lock_guard<mutex> lock(mMutex); |
| 145 | mActiveConfigsChangedReceivers.erase(uid); |
| 146 | } |
| 147 | |
Tej Singh | 81e38f4 | 2021-10-19 23:40:07 -0700 | [diff] [blame] | 148 | void ConfigManager::RemoveActiveConfigsChangedReceiver(const int uid, |
| 149 | const shared_ptr<IPendingIntentRef>& pir) { |
| 150 | lock_guard<mutex> lock(mMutex); |
| 151 | auto it = mActiveConfigsChangedReceivers.find(uid); |
| 152 | if (it != mActiveConfigsChangedReceivers.end() && it->second == pir) { |
| 153 | mActiveConfigsChangedReceivers.erase(uid); |
| 154 | } |
| 155 | } |
| 156 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 157 | void ConfigManager::RemoveConfig(const ConfigKey& key) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 158 | vector<sp<ConfigListener>> broadcastList; |
| 159 | { |
| 160 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 161 | |
Tej Singh | a783d52 | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 162 | auto uid = key.GetUid(); |
| 163 | auto uidIt = mConfigs.find(uid); |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 164 | if (uidIt != mConfigs.end() && uidIt->second.find(key) != uidIt->second.end()) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 165 | // Remove from map |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 166 | uidIt->second.erase(key); |
Tej Singh | a783d52 | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 167 | |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 168 | for (const sp<ConfigListener>& listener : mListeners) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 169 | broadcastList.push_back(listener); |
| 170 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 171 | } |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 172 | |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 173 | // Remove from disk. There can still be a lingering file on disk so we check |
| 174 | // whether or not the config was on memory. |
| 175 | remove_saved_configs(key); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 176 | } |
yro | 9bb4f7d | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 177 | |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 178 | for (const sp<ConfigListener>& listener:broadcastList) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 179 | listener->OnConfigRemoved(key); |
| 180 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 181 | } |
| 182 | |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 183 | void ConfigManager::remove_saved_configs(const ConfigKey& key) { |
yro | 0bd9aa1 | 2018-02-13 22:06:34 -0800 | [diff] [blame] | 184 | string suffix = StringPrintf("%d_%lld", key.GetUid(), (long long)key.GetId()); |
yro | a8c4ebf | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 185 | StorageManager::deleteSuffixedFiles(STATS_SERVICE_DIR, suffix.c_str()); |
yro | d3a2436 | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 188 | void ConfigManager::RemoveConfigs(int uid) { |
| 189 | vector<ConfigKey> removed; |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 190 | vector<sp<ConfigListener>> broadcastList; |
| 191 | { |
| 192 | lock_guard <mutex> lock(mMutex); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 193 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 194 | auto uidIt = mConfigs.find(uid); |
| 195 | if (uidIt == mConfigs.end()) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | for (auto it = uidIt->second.begin(); it != uidIt->second.end(); ++it) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 200 | // Remove from map |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 201 | remove_saved_configs(*it); |
| 202 | removed.push_back(*it); |
Tej Singh | fad59e3 | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 205 | mConfigs.erase(uidIt); |
| 206 | |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 207 | for (const sp<ConfigListener>& listener : mListeners) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 208 | broadcastList.push_back(listener); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 213 | for (auto& key : removed) { |
| 214 | // Tell everyone |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 215 | for (const sp<ConfigListener>& listener:broadcastList) { |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 216 | listener->OnConfigRemoved(key); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 221 | void ConfigManager::RemoveAllConfigs() { |
| 222 | vector<ConfigKey> removed; |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 223 | vector<sp<ConfigListener>> broadcastList; |
| 224 | { |
| 225 | lock_guard <mutex> lock(mMutex); |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 226 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 227 | for (auto uidIt = mConfigs.begin(); uidIt != mConfigs.end();) { |
| 228 | for (auto it = uidIt->second.begin(); it != uidIt->second.end();) { |
| 229 | // Remove from map |
| 230 | removed.push_back(*it); |
| 231 | it = uidIt->second.erase(it); |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 232 | } |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 233 | uidIt = mConfigs.erase(uidIt); |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 234 | } |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 235 | |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 236 | for (const sp<ConfigListener>& listener : mListeners) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 237 | broadcastList.push_back(listener); |
| 238 | } |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Remove separately so if they do anything in the callback they can't mess up our iteration. |
| 242 | for (auto& key : removed) { |
| 243 | // Tell everyone |
Chih-Hung Hsieh | fa35ea8 | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 244 | for (const sp<ConfigListener>& listener:broadcastList) { |
yro | 11e2da5 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 245 | listener->OnConfigRemoved(key); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Yangster | d1df8ed | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 250 | vector<ConfigKey> ConfigManager::GetAllConfigKeys() const { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 251 | lock_guard<mutex> lock(mMutex); |
| 252 | |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 253 | vector<ConfigKey> ret; |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 254 | for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) { |
| 255 | for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) { |
| 256 | ret.push_back(*it); |
| 257 | } |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 258 | } |
| 259 | return ret; |
| 260 | } |
| 261 | |
Ruchir Rastogi | d36dd0b | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 262 | const shared_ptr<IPendingIntentRef> ConfigManager::GetConfigReceiver(const ConfigKey& key) const { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 263 | lock_guard<mutex> lock(mMutex); |
| 264 | |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 265 | auto it = mConfigReceivers.find(key); |
| 266 | if (it == mConfigReceivers.end()) { |
David Chen | 29ada1e | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 267 | return nullptr; |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 268 | } else { |
| 269 | return it->second; |
| 270 | } |
| 271 | } |
| 272 | |
Ruchir Rastogi | d36dd0b | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 273 | const shared_ptr<IPendingIntentRef> ConfigManager::GetActiveConfigsChangedReceiver(const int uid) |
| 274 | const { |
Tej Singh | fad59e3 | 2019-01-22 11:33:51 -0800 | [diff] [blame] | 275 | lock_guard<mutex> lock(mMutex); |
| 276 | |
| 277 | auto it = mActiveConfigsChangedReceivers.find(uid); |
| 278 | if (it == mActiveConfigsChangedReceivers.end()) { |
| 279 | return nullptr; |
| 280 | } else { |
| 281 | return it->second; |
| 282 | } |
| 283 | } |
| 284 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 285 | void ConfigManager::Dump(FILE* out) { |
David Chen | 687dbd1 | 2018-02-09 17:21:48 -0800 | [diff] [blame] | 286 | lock_guard<mutex> lock(mMutex); |
| 287 | |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 288 | fprintf(out, "CONFIGURATIONS\n"); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 289 | fprintf(out, " uid name\n"); |
Yao Chen | 341db89 | 2018-03-27 10:59:45 -0700 | [diff] [blame] | 290 | for (auto uidIt = mConfigs.cbegin(); uidIt != mConfigs.cend(); ++uidIt) { |
| 291 | for (auto it = uidIt->second.cbegin(); it != uidIt->second.cend(); ++it) { |
| 292 | fprintf(out, " %6d %lld\n", it->GetUid(), (long long)it->GetId()); |
| 293 | auto receiverIt = mConfigReceivers.find(*it); |
| 294 | if (receiverIt != mConfigReceivers.end()) { |
| 295 | fprintf(out, " -> received by PendingIntent as binder\n"); |
| 296 | } |
David Chen | dd4e803 | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 297 | } |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 301 | void ConfigManager::update_saved_configs_locked(const ConfigKey& key, |
| 302 | const vector<uint8_t>& buffer, |
| 303 | const int numBytes) { |
yro | 9bb4f7d | 2017-11-19 14:33:56 -0800 | [diff] [blame] | 304 | // If there is a pre-existing config with same key we should first delete it. |
| 305 | remove_saved_configs(key); |
| 306 | |
| 307 | // Then we save the latest config. |
yro | 2918b55 | 2018-03-12 20:44:05 -0700 | [diff] [blame] | 308 | string file_name = |
| 309 | StringPrintf("%s/%ld_%d_%lld", STATS_SERVICE_DIR, time(nullptr), |
| 310 | key.GetUid(), (long long)key.GetId()); |
yro | 1528e0f | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 311 | StorageManager::writeFile(file_name.c_str(), &buffer[0], numBytes); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 314 | } // namespace statsd |
| 315 | } // namespace os |
| 316 | } // namespace android |