Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
yro | 0feae94 | 2017-11-15 14:38:48 -0800 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 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 | |
Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 19 | |
| 20 | #include "StatsService.h" |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 21 | #include "stats_log_util.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 23 | #include "config/ConfigKey.h" |
| 24 | #include "config/ConfigManager.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | #include "guardrail/StatsdStats.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 26 | #include "storage/StorageManager.h" |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 27 | #include "subscriber/SubscriberReporter.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 28 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
| 31 | #include <binder/IServiceManager.h> |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 32 | #include <dirent.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 33 | #include <frameworks/base/cmds/statsd/src/statsd_config.pb.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 34 | #include <private/android_filesystem_config.h> |
| 35 | #include <utils/Looper.h> |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 36 | #include <utils/String16.h> |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 37 | #include <statslog.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 38 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 39 | #include <stdlib.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 40 | #include <sys/system_properties.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 41 | #include <unistd.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 42 | |
| 43 | using namespace android; |
| 44 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace os { |
| 47 | namespace statsd { |
| 48 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 49 | constexpr const char* kPermissionDump = "android.permission.DUMP"; |
yro | 03faf09 | 2017-12-12 00:17:50 -0800 | [diff] [blame] | 50 | #define STATS_SERVICE_DIR "/data/misc/stats-service" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 51 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Watches for the death of the stats companion (system process). |
| 54 | */ |
| 55 | class CompanionDeathRecipient : public IBinder::DeathRecipient { |
| 56 | public: |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 57 | CompanionDeathRecipient(const sp<AlarmMonitor>& anomalyAlarmMonitor, |
| 58 | const sp<AlarmMonitor>& periodicAlarmMonitor) : |
| 59 | mAnomalyAlarmMonitor(anomalyAlarmMonitor), |
| 60 | mPeriodicAlarmMonitor(periodicAlarmMonitor) {} |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 61 | virtual void binderDied(const wp<IBinder>& who); |
| 62 | |
| 63 | private: |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 64 | sp<AlarmMonitor> mAnomalyAlarmMonitor; |
| 65 | sp<AlarmMonitor> mPeriodicAlarmMonitor; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 68 | void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 69 | ALOGW("statscompanion service died"); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 70 | mAnomalyAlarmMonitor->setStatsCompanionService(nullptr); |
| 71 | mPeriodicAlarmMonitor->setStatsCompanionService(nullptr); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 72 | SubscriberReporter::getInstance().setStatsCompanionService(nullptr); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 75 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 76 | : mAnomalyAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS, |
| 77 | [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) { |
| 78 | if (sc != nullptr) { |
| 79 | sc->setAnomalyAlarm(timeMillis); |
| 80 | StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged(); |
| 81 | } |
| 82 | }, |
| 83 | [](const sp<IStatsCompanionService>& sc) { |
| 84 | if (sc != nullptr) { |
| 85 | sc->cancelAnomalyAlarm(); |
| 86 | StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged(); |
| 87 | } |
| 88 | })), |
| 89 | mPeriodicAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS, |
| 90 | [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) { |
| 91 | if (sc != nullptr) { |
| 92 | sc->setAlarmForSubscriberTriggering(timeMillis); |
| 93 | StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged(); |
| 94 | } |
| 95 | }, |
| 96 | [](const sp<IStatsCompanionService>& sc) { |
| 97 | if (sc != nullptr) { |
| 98 | sc->cancelAlarmForSubscriberTriggering(); |
| 99 | StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged(); |
| 100 | } |
| 101 | |
| 102 | })) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 103 | mUidMap = new UidMap(); |
Chenjie Yu | 80f9112 | 2018-01-31 20:24:50 -0800 | [diff] [blame] | 104 | StatsPuller::SetUidMap(mUidMap); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 105 | mConfigManager = new ConfigManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 106 | mProcessor = new StatsLogProcessor(mUidMap, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor, |
| 107 | getElapsedRealtimeSec(), [this](const ConfigKey& key) { |
| 108 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
| 109 | auto receiver = mConfigManager->GetConfigReceiver(key); |
| 110 | if (sc == nullptr) { |
| 111 | VLOG("Could not find StatsCompanionService"); |
| 112 | } else if (receiver == nullptr) { |
| 113 | VLOG("Statscompanion could not find a broadcast receiver for %s", |
| 114 | key.ToString().c_str()); |
| 115 | } else { |
| 116 | sc->sendDataBroadcast(receiver); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 117 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 118 | } |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 119 | ); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 120 | |
| 121 | mConfigManager->AddListener(mProcessor); |
| 122 | |
| 123 | init_system_properties(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 126 | StatsService::~StatsService() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 129 | void StatsService::init_system_properties() { |
| 130 | mEngBuild = false; |
| 131 | const prop_info* buildType = __system_property_find("ro.build.type"); |
| 132 | if (buildType != NULL) { |
| 133 | __system_property_read_callback(buildType, init_build_type_callback, this); |
| 134 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 137 | void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value, |
| 138 | uint32_t serial) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 139 | if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 140 | reinterpret_cast<StatsService*>(cookie)->mEngBuild = true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Implement our own because the default binder implementation isn't |
| 146 | * properly handling SHELL_COMMAND_TRANSACTION. |
| 147 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 148 | status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 149 | uint32_t flags) { |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 150 | switch (code) { |
| 151 | case SHELL_COMMAND_TRANSACTION: { |
| 152 | int in = data.readFileDescriptor(); |
| 153 | int out = data.readFileDescriptor(); |
| 154 | int err = data.readFileDescriptor(); |
| 155 | int argc = data.readInt32(); |
| 156 | Vector<String8> args; |
| 157 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 158 | args.add(String8(data.readString16())); |
| 159 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 160 | sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder()); |
| 161 | sp<IResultReceiver> resultReceiver = |
| 162 | IResultReceiver::asInterface(data.readStrongBinder()); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 163 | |
| 164 | FILE* fin = fdopen(in, "r"); |
| 165 | FILE* fout = fdopen(out, "w"); |
| 166 | FILE* ferr = fdopen(err, "w"); |
| 167 | |
| 168 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 169 | resultReceiver->send(NO_MEMORY); |
| 170 | } else { |
| 171 | err = command(fin, fout, ferr, args); |
| 172 | resultReceiver->send(err); |
| 173 | } |
| 174 | |
| 175 | if (fin != NULL) { |
| 176 | fflush(fin); |
| 177 | fclose(fin); |
| 178 | } |
| 179 | if (fout != NULL) { |
| 180 | fflush(fout); |
| 181 | fclose(fout); |
| 182 | } |
| 183 | if (fout != NULL) { |
| 184 | fflush(ferr); |
| 185 | fclose(ferr); |
| 186 | } |
| 187 | |
| 188 | return NO_ERROR; |
| 189 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 190 | default: { return BnStatsManager::onTransact(code, data, reply, flags); } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Write debugging data about statsd. |
| 196 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 197 | status_t StatsService::dump(int fd, const Vector<String16>& args) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 198 | FILE* out = fdopen(fd, "w"); |
| 199 | if (out == NULL) { |
| 200 | return NO_MEMORY; // the fd is already open |
| 201 | } |
| 202 | |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 203 | bool verbose = false; |
| 204 | if (args.size() > 0 && !args[0].compare(String16("-v"))) { |
| 205 | verbose = true; |
| 206 | } |
| 207 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 208 | // TODO: Proto format for incident reports |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 209 | dump_impl(out, verbose); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 210 | |
| 211 | fclose(out); |
| 212 | return NO_ERROR; |
| 213 | } |
| 214 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Write debugging data about statsd in text format. |
| 217 | */ |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 218 | void StatsService::dump_impl(FILE* out, bool verbose) { |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 219 | StatsdStats::getInstance().dumpStats(out); |
Yao Chen | 884c8c1 | 2018-01-26 10:36:25 -0800 | [diff] [blame] | 220 | mProcessor->dumpStates(out, verbose); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Implementation of the adb shell cmd stats command. |
| 225 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 226 | status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 227 | // TODO: Permission check |
| 228 | |
| 229 | const int argCount = args.size(); |
| 230 | if (argCount >= 1) { |
| 231 | // adb shell cmd stats config ... |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 232 | if (!args[0].compare(String8("config"))) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 233 | return cmd_config(in, out, err, args); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 234 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 235 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 236 | if (!args[0].compare(String8("print-uid-map"))) { |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 237 | return cmd_print_uid_map(out, args); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 238 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 239 | |
| 240 | if (!args[0].compare(String8("dump-report"))) { |
| 241 | return cmd_dump_report(out, err, args); |
| 242 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 243 | |
| 244 | if (!args[0].compare(String8("pull-source")) && args.size() > 1) { |
| 245 | return cmd_print_pulled_metrics(out, args); |
| 246 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 247 | |
| 248 | if (!args[0].compare(String8("send-broadcast"))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 249 | return cmd_trigger_broadcast(out, args); |
| 250 | } |
| 251 | |
| 252 | if (!args[0].compare(String8("print-stats"))) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 253 | return cmd_print_stats(out, args); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 254 | } |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 255 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 256 | if (!args[0].compare(String8("meminfo"))) { |
| 257 | return cmd_dump_memory_info(out); |
| 258 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 259 | |
| 260 | if (!args[0].compare(String8("write-to-disk"))) { |
| 261 | return cmd_write_data_to_disk(out); |
| 262 | } |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 263 | |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame] | 264 | if (!args[0].compare(String8("log-app-breadcrumb"))) { |
| 265 | return cmd_log_app_breadcrumb(out, args); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 266 | } |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 267 | |
| 268 | if (!args[0].compare(String8("clear-puller-cache"))) { |
| 269 | return cmd_clear_puller_cache(out); |
| 270 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 271 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 272 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 273 | print_cmd_help(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 274 | return NO_ERROR; |
| 275 | } |
| 276 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 277 | void StatsService::print_cmd_help(FILE* out) { |
| 278 | fprintf(out, |
| 279 | "usage: adb shell cmd stats print-stats-log [tag_required] " |
| 280 | "[timestamp_nsec_optional]\n"); |
| 281 | fprintf(out, "\n"); |
| 282 | fprintf(out, "\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 283 | fprintf(out, "usage: adb shell cmd stats meminfo\n"); |
| 284 | fprintf(out, "\n"); |
| 285 | fprintf(out, " Prints the malloc debug information. You need to run the following first: \n"); |
| 286 | fprintf(out, " # adb shell stop\n"); |
| 287 | fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n"); |
| 288 | fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n"); |
| 289 | fprintf(out, " # adb shell start\n"); |
| 290 | fprintf(out, "\n"); |
| 291 | fprintf(out, "\n"); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 292 | fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 293 | fprintf(out, "\n"); |
| 294 | fprintf(out, " Prints the UID, app name, version mapping.\n"); |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 295 | fprintf(out, " PKG Optional package name to print the uids of the package\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 296 | fprintf(out, "\n"); |
| 297 | fprintf(out, "\n"); |
yro | 3fca5ba | 2017-11-17 13:22:52 -0800 | [diff] [blame] | 298 | fprintf(out, "usage: adb shell cmd stats pull-source [int] \n"); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 299 | fprintf(out, "\n"); |
| 300 | fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n"); |
| 301 | fprintf(out, "\n"); |
| 302 | fprintf(out, "\n"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 303 | fprintf(out, "usage: adb shell cmd stats write-to-disk \n"); |
| 304 | fprintf(out, "\n"); |
| 305 | fprintf(out, " Flushes all data on memory to disk.\n"); |
| 306 | fprintf(out, "\n"); |
| 307 | fprintf(out, "\n"); |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame] | 308 | fprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n"); |
| 309 | fprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n"); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 310 | fprintf(out, " UID The uid to use. It is only possible to pass a UID\n"); |
| 311 | fprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 312 | fprintf(out, " uid is used.\n"); |
| 313 | fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n"); |
| 314 | fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n"); |
| 315 | fprintf(out, "\n"); |
| 316 | fprintf(out, "\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 317 | fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 318 | fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n"); |
| 319 | fprintf(out, "\n"); |
| 320 | fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 321 | fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n"); |
| 322 | fprintf(out, " provided, then all configs will be removed from memory and disk.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 323 | fprintf(out, "\n"); |
| 324 | fprintf(out, " UID The uid to use. It is only possible to pass the UID\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 325 | fprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 326 | fprintf(out, " uid is used.\n"); |
| 327 | fprintf(out, " NAME The per-uid name to use\n"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 328 | fprintf(out, "\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 329 | fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n"); |
| 330 | fprintf(out, "\n be removed from memory and disk!\n"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 331 | fprintf(out, "\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 332 | fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 333 | fprintf(out, " Dump all metric data for a configuration.\n"); |
| 334 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 335 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 336 | fprintf(out, " calling uid is used.\n"); |
| 337 | fprintf(out, " NAME The name of the configuration\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 338 | fprintf(out, " --proto Print proto binary.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 339 | fprintf(out, "\n"); |
| 340 | fprintf(out, "\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 341 | fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n"); |
| 342 | fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n"); |
| 343 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 344 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 345 | fprintf(out, " calling uid is used.\n"); |
| 346 | fprintf(out, " NAME The name of the configuration\n"); |
| 347 | fprintf(out, "\n"); |
| 348 | fprintf(out, "\n"); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 349 | fprintf(out, "usage: adb shell cmd stats print-stats\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 350 | fprintf(out, " Prints some basic stats.\n"); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 351 | fprintf(out, "\n"); |
| 352 | fprintf(out, "\n"); |
| 353 | fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n"); |
| 354 | fprintf(out, " Clear cached puller data.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 355 | } |
| 356 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 357 | status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) { |
| 358 | string name; |
| 359 | bool good = false; |
| 360 | int uid; |
| 361 | const int argCount = args.size(); |
| 362 | if (argCount == 2) { |
| 363 | // Automatically pick the UID |
| 364 | uid = IPCThreadState::self()->getCallingUid(); |
| 365 | // TODO: What if this isn't a binder call? Should we fail? |
| 366 | name.assign(args[1].c_str(), args[1].size()); |
| 367 | good = true; |
| 368 | } else if (argCount == 3) { |
| 369 | // If it's a userdebug or eng build, then the shell user can |
| 370 | // impersonate other uids. |
| 371 | if (mEngBuild) { |
| 372 | const char* s = args[1].c_str(); |
| 373 | if (*s != '\0') { |
| 374 | char* end = NULL; |
| 375 | uid = strtol(s, &end, 0); |
| 376 | if (*end == '\0') { |
| 377 | name.assign(args[2].c_str(), args[2].size()); |
| 378 | good = true; |
| 379 | } |
| 380 | } |
| 381 | } else { |
| 382 | fprintf(out, |
| 383 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 384 | "builds.\n"); |
| 385 | } |
| 386 | } |
| 387 | if (!good) { |
| 388 | print_cmd_help(out); |
| 389 | return UNKNOWN_ERROR; |
| 390 | } |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 391 | auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name))); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 392 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 393 | if (sc == nullptr) { |
| 394 | VLOG("Could not access statsCompanion"); |
| 395 | } else if (receiver == nullptr) { |
| 396 | VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str()) |
| 397 | } else { |
| 398 | sc->sendDataBroadcast(receiver); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 399 | VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), |
| 400 | args[2].c_str()); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 401 | } |
| 402 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 403 | return NO_ERROR; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 407 | const int argCount = args.size(); |
| 408 | if (argCount >= 2) { |
| 409 | if (args[1] == "update" || args[1] == "remove") { |
| 410 | bool good = false; |
| 411 | int uid = -1; |
| 412 | string name; |
| 413 | |
| 414 | if (argCount == 3) { |
| 415 | // Automatically pick the UID |
| 416 | uid = IPCThreadState::self()->getCallingUid(); |
| 417 | // TODO: What if this isn't a binder call? Should we fail? |
| 418 | name.assign(args[2].c_str(), args[2].size()); |
| 419 | good = true; |
| 420 | } else if (argCount == 4) { |
| 421 | // If it's a userdebug or eng build, then the shell user can |
| 422 | // impersonate other uids. |
| 423 | if (mEngBuild) { |
| 424 | const char* s = args[2].c_str(); |
| 425 | if (*s != '\0') { |
| 426 | char* end = NULL; |
| 427 | uid = strtol(s, &end, 0); |
| 428 | if (*end == '\0') { |
| 429 | name.assign(args[3].c_str(), args[3].size()); |
| 430 | good = true; |
| 431 | } |
| 432 | } |
| 433 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 434 | fprintf(err, |
| 435 | "The config can only be set for other UIDs on eng or userdebug " |
| 436 | "builds.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 437 | } |
yro | e5f8292 | 2018-01-22 18:37:27 -0800 | [diff] [blame] | 438 | } else if (argCount == 2 && args[1] == "remove") { |
| 439 | good = true; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | if (!good) { |
| 443 | // If arg parsing failed, print the help text and return an error. |
| 444 | print_cmd_help(out); |
| 445 | return UNKNOWN_ERROR; |
| 446 | } |
| 447 | |
| 448 | if (args[1] == "update") { |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 449 | char* endp; |
| 450 | int64_t configID = strtoll(name.c_str(), &endp, 10); |
| 451 | if (endp == name.c_str() || *endp != '\0') { |
| 452 | fprintf(err, "Error parsing config ID.\n"); |
| 453 | return UNKNOWN_ERROR; |
| 454 | } |
| 455 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 456 | // Read stream into buffer. |
| 457 | string buffer; |
| 458 | if (!android::base::ReadFdToString(fileno(in), &buffer)) { |
| 459 | fprintf(err, "Error reading stream for StatsConfig.\n"); |
| 460 | return UNKNOWN_ERROR; |
| 461 | } |
| 462 | |
| 463 | // Parse buffer. |
| 464 | StatsdConfig config; |
| 465 | if (!config.ParseFromString(buffer)) { |
| 466 | fprintf(err, "Error parsing proto stream for StatsConfig.\n"); |
| 467 | return UNKNOWN_ERROR; |
| 468 | } |
| 469 | |
| 470 | // Add / update the config. |
yro | 255f72e | 2018-02-26 15:15:17 -0800 | [diff] [blame] | 471 | mConfigManager->UpdateConfig(ConfigKey(uid, configID), config); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 472 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 473 | if (argCount == 2) { |
| 474 | cmd_remove_all_configs(out); |
| 475 | } else { |
| 476 | // Remove the config. |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 477 | mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name))); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 478 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | return NO_ERROR; |
| 482 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 483 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 484 | print_cmd_help(out); |
| 485 | return UNKNOWN_ERROR; |
| 486 | } |
| 487 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 488 | status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) { |
| 489 | if (mProcessor != nullptr) { |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 490 | int argCount = args.size(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 491 | bool good = false; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 492 | bool proto = false; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 493 | int uid; |
| 494 | string name; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 495 | if (!std::strcmp("--proto", args[argCount-1].c_str())) { |
| 496 | proto = true; |
| 497 | argCount -= 1; |
| 498 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 499 | if (argCount == 2) { |
| 500 | // Automatically pick the UID |
| 501 | uid = IPCThreadState::self()->getCallingUid(); |
| 502 | // TODO: What if this isn't a binder call? Should we fail? |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 503 | name.assign(args[1].c_str(), args[1].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 504 | good = true; |
| 505 | } else if (argCount == 3) { |
| 506 | // If it's a userdebug or eng build, then the shell user can |
| 507 | // impersonate other uids. |
| 508 | if (mEngBuild) { |
| 509 | const char* s = args[1].c_str(); |
| 510 | if (*s != '\0') { |
| 511 | char* end = NULL; |
| 512 | uid = strtol(s, &end, 0); |
| 513 | if (*end == '\0') { |
| 514 | name.assign(args[2].c_str(), args[2].size()); |
| 515 | good = true; |
| 516 | } |
| 517 | } |
| 518 | } else { |
| 519 | fprintf(out, |
| 520 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 521 | "builds.\n"); |
| 522 | } |
| 523 | } |
| 524 | if (good) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 525 | vector<uint8_t> data; |
David Chen | 926fc75 | 2018-02-23 13:31:43 -0800 | [diff] [blame] | 526 | mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(), |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 527 | &data); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 528 | // TODO: print the returned StatsLogReport to file instead of printing to logcat. |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 529 | if (proto) { |
| 530 | for (size_t i = 0; i < data.size(); i ++) { |
| 531 | fprintf(out, "%c", data[i]); |
| 532 | } |
| 533 | } else { |
| 534 | fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str()); |
| 535 | fprintf(out, "See the StatsLogReport in logcat...\n"); |
| 536 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 537 | return android::OK; |
| 538 | } else { |
| 539 | // If arg parsing failed, print the help text and return an error. |
| 540 | print_cmd_help(out); |
| 541 | return UNKNOWN_ERROR; |
| 542 | } |
| 543 | } else { |
| 544 | fprintf(out, "Log processor does not exist...\n"); |
| 545 | return UNKNOWN_ERROR; |
| 546 | } |
| 547 | } |
| 548 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 549 | status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 550 | vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys(); |
| 551 | for (const ConfigKey& key : configs) { |
| 552 | fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(), |
| 553 | mProcessor->GetMetricsSize(key)); |
| 554 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 555 | StatsdStats& statsdStats = StatsdStats::getInstance(); |
Yao Chen | f5acabe | 2018-01-17 14:10:34 -0800 | [diff] [blame] | 556 | statsdStats.dumpStats(out); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 557 | return NO_ERROR; |
| 558 | } |
| 559 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 560 | status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) { |
| 561 | if (args.size() > 1) { |
| 562 | string pkg; |
| 563 | pkg.assign(args[1].c_str(), args[1].size()); |
| 564 | auto uids = mUidMap->getAppUid(pkg); |
| 565 | fprintf(out, "%s -> [ ", pkg.c_str()); |
| 566 | for (const auto& uid : uids) { |
| 567 | fprintf(out, "%d ", uid); |
| 568 | } |
| 569 | fprintf(out, "]\n"); |
| 570 | } else { |
| 571 | mUidMap->printUidMap(out); |
| 572 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 573 | return NO_ERROR; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 574 | } |
| 575 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 576 | status_t StatsService::cmd_write_data_to_disk(FILE* out) { |
| 577 | fprintf(out, "Writing data to disk\n"); |
| 578 | mProcessor->WriteDataToDisk(); |
| 579 | return NO_ERROR; |
| 580 | } |
| 581 | |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame] | 582 | status_t StatsService::cmd_log_app_breadcrumb(FILE* out, const Vector<String8>& args) { |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 583 | bool good = false; |
| 584 | int32_t uid; |
| 585 | int32_t label; |
| 586 | int32_t state; |
| 587 | const int argCount = args.size(); |
| 588 | if (argCount == 3) { |
| 589 | // Automatically pick the UID |
| 590 | uid = IPCThreadState::self()->getCallingUid(); |
| 591 | label = atoi(args[1].c_str()); |
| 592 | state = atoi(args[2].c_str()); |
| 593 | good = true; |
| 594 | } else if (argCount == 4) { |
| 595 | uid = atoi(args[1].c_str()); |
| 596 | // If it's a userdebug or eng build, then the shell user can impersonate other uids. |
| 597 | // Otherwise, the uid must match the actual caller's uid. |
| 598 | if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) { |
| 599 | label = atoi(args[2].c_str()); |
| 600 | state = atoi(args[3].c_str()); |
| 601 | good = true; |
| 602 | } else { |
| 603 | fprintf(out, |
David Chen | b639d14 | 2018-02-14 17:29:54 -0800 | [diff] [blame] | 604 | "Selecting a UID for writing AppBreadcrumb can only be done for other UIDs " |
| 605 | "on eng or userdebug builds.\n"); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | if (good) { |
David Chen | 0b5c90c | 2018-01-25 16:51:49 -0800 | [diff] [blame] | 609 | fprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state); |
| 610 | android::util::stats_write(android::util::APP_BREADCRUMB_REPORTED, uid, label, state); |
Bookatz | b223c4e | 2018-02-01 15:35:04 -0800 | [diff] [blame] | 611 | } else { |
| 612 | print_cmd_help(out); |
| 613 | return UNKNOWN_ERROR; |
| 614 | } |
| 615 | return NO_ERROR; |
| 616 | } |
| 617 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 618 | status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) { |
| 619 | int s = atoi(args[1].c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 620 | vector<shared_ptr<LogEvent> > stats; |
| 621 | if (mStatsPullerManager.Pull(s, &stats)) { |
| 622 | for (const auto& it : stats) { |
| 623 | fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); |
| 624 | } |
| 625 | fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); |
| 626 | return NO_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 627 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 628 | return UNKNOWN_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 629 | } |
| 630 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 631 | status_t StatsService::cmd_remove_all_configs(FILE* out) { |
| 632 | fprintf(out, "Removing all configs...\n"); |
| 633 | VLOG("StatsService::cmd_remove_all_configs was called"); |
| 634 | mConfigManager->RemoveAllConfigs(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 635 | StorageManager::deleteAllFiles(STATS_SERVICE_DIR); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 636 | return NO_ERROR; |
| 637 | } |
| 638 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 639 | status_t StatsService::cmd_dump_memory_info(FILE* out) { |
Yao Chen | 20e9e62 | 2018-02-28 11:18:51 -0800 | [diff] [blame^] | 640 | fprintf(out, "meminfo not available.\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 641 | return NO_ERROR; |
| 642 | } |
| 643 | |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 644 | status_t StatsService::cmd_clear_puller_cache(FILE* out) { |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 645 | IPCThreadState* ipc = IPCThreadState::self(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 646 | VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", |
| 647 | ipc->getCallingPid(), ipc->getCallingUid()); |
Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 648 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 649 | int cleared = mStatsPullerManager.ForceClearPullerCache(); |
| 650 | fprintf(out, "Puller removed %d cached data!\n", cleared); |
| 651 | return NO_ERROR; |
| 652 | } else { |
| 653 | return PERMISSION_DENIED; |
| 654 | } |
Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 657 | Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version, |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 658 | const vector<String16>& app) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 659 | VLOG("StatsService::informAllUidData was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 660 | |
| 661 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 662 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 663 | "Only system uid can call informAllUidData"); |
| 664 | } |
| 665 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 666 | mUidMap->updateMap(uid, version, app); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 667 | VLOG("StatsService::informAllUidData succeeded"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 668 | |
| 669 | return Status::ok(); |
| 670 | } |
| 671 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 672 | Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 673 | VLOG("StatsService::informOnePackage was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 674 | |
| 675 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 676 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 677 | "Only system uid can call informOnePackage"); |
| 678 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 679 | mUidMap->updateApp(app, uid, version); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 680 | return Status::ok(); |
| 681 | } |
| 682 | |
| 683 | Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 684 | VLOG("StatsService::informOnePackageRemoved was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 685 | |
| 686 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 687 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 688 | "Only system uid can call informOnePackageRemoved"); |
| 689 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 690 | mUidMap->removeApp(app, uid); |
yro | 0192402 | 2018-02-20 18:20:49 -0800 | [diff] [blame] | 691 | mConfigManager->RemoveConfigs(uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 692 | return Status::ok(); |
| 693 | } |
| 694 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 695 | Status StatsService::informAnomalyAlarmFired() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 696 | VLOG("StatsService::informAnomalyAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 697 | |
| 698 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 699 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 700 | "Only system uid can call informAnomalyAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 701 | } |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 702 | |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 703 | uint64_t currentTimeSec = getElapsedRealtimeSec(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 704 | std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet = |
| 705 | mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
| 706 | if (alarmSet.size() > 0) { |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 707 | VLOG("Found an anomaly alarm that fired."); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 708 | mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet); |
Bookatz | 66fe061 | 2018-02-07 18:51:48 -0800 | [diff] [blame] | 709 | } else { |
| 710 | VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled."); |
| 711 | } |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 712 | return Status::ok(); |
| 713 | } |
| 714 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 715 | Status StatsService::informAlarmForSubscriberTriggeringFired() { |
| 716 | VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called"); |
| 717 | |
| 718 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 719 | return Status::fromExceptionCode( |
| 720 | Status::EX_SECURITY, |
| 721 | "Only system uid can call informAlarmForSubscriberTriggeringFired"); |
| 722 | } |
| 723 | |
| 724 | uint64_t currentTimeSec = time(nullptr); |
| 725 | std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet = |
| 726 | mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
| 727 | if (alarmSet.size() > 0) { |
| 728 | VLOG("Found periodic alarm fired."); |
| 729 | mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet); |
| 730 | } else { |
| 731 | ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled."); |
| 732 | } |
| 733 | return Status::ok(); |
| 734 | } |
| 735 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 736 | Status StatsService::informPollAlarmFired() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 737 | VLOG("StatsService::informPollAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 738 | |
| 739 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 740 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 741 | "Only system uid can call informPollAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 744 | mStatsPullerManager.OnAlarmFired(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 745 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 746 | VLOG("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 747 | |
| 748 | return Status::ok(); |
| 749 | } |
| 750 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 751 | Status StatsService::systemRunning() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 752 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 753 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 754 | "Only system uid can call systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // When system_server is up and running, schedule the dropbox task to run. |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 758 | VLOG("StatsService::systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 759 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 760 | sayHiToStatsCompanion(); |
| 761 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 762 | return Status::ok(); |
| 763 | } |
| 764 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 765 | Status StatsService::writeDataToDisk() { |
| 766 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 767 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 768 | "Only system uid can call systemRunning"); |
| 769 | } |
| 770 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 771 | VLOG("StatsService::writeDataToDisk"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 772 | |
| 773 | mProcessor->WriteDataToDisk(); |
| 774 | |
| 775 | return Status::ok(); |
| 776 | } |
| 777 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 778 | void StatsService::sayHiToStatsCompanion() { |
| 779 | // TODO: This method needs to be private. It is temporarily public and unsecured for testing |
| 780 | // purposes. |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 781 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 782 | if (statsCompanion != nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 783 | VLOG("Telling statsCompanion that statsd is ready"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 784 | statsCompanion->statsdReady(); |
| 785 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 786 | VLOG("Could not access statsCompanion"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 790 | sp<IStatsCompanionService> StatsService::getStatsCompanionService() { |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 791 | sp<IStatsCompanionService> statsCompanion = nullptr; |
| 792 | // Get statscompanion service from service manager |
| 793 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 794 | if (sm != nullptr) { |
| 795 | const String16 name("statscompanion"); |
| 796 | statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name)); |
| 797 | if (statsCompanion == nullptr) { |
| 798 | ALOGW("statscompanion service unavailable!"); |
| 799 | return nullptr; |
| 800 | } |
| 801 | } |
| 802 | return statsCompanion; |
| 803 | } |
| 804 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 805 | Status StatsService::statsCompanionReady() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 806 | VLOG("StatsService::statsCompanionReady was called"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 807 | |
| 808 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 809 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 810 | "Only system uid can call statsCompanionReady"); |
| 811 | } |
| 812 | |
| 813 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 814 | if (statsCompanion == nullptr) { |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 815 | return Status::fromExceptionCode( |
| 816 | Status::EX_NULL_POINTER, |
| 817 | "statscompanion unavailable despite it contacting statsd!"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 818 | } |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 819 | VLOG("StatsService::statsCompanionReady linking to statsCompanion."); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 820 | IInterface::asBinder(statsCompanion)->linkToDeath( |
| 821 | new CompanionDeathRecipient(mAnomalyAlarmMonitor, mPeriodicAlarmMonitor)); |
| 822 | mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion); |
| 823 | mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion); |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 824 | SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 825 | return Status::ok(); |
| 826 | } |
| 827 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 828 | void StatsService::Startup() { |
| 829 | mConfigManager->Startup(); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 832 | void StatsService::OnLogEvent(LogEvent* event) { |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 833 | mProcessor->OnLogEvent(event); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 836 | Status StatsService::getData(int64_t key, vector<uint8_t>* output) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 837 | IPCThreadState* ipc = IPCThreadState::self(); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 838 | VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid()); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 839 | if (checkCallingPermission(String16(kPermissionDump))) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 840 | ConfigKey configKey(ipc->getCallingUid(), key); |
David Chen | 926fc75 | 2018-02-23 13:31:43 -0800 | [diff] [blame] | 841 | mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), output); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 842 | return Status::ok(); |
| 843 | } else { |
| 844 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 845 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 846 | } |
| 847 | |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 848 | Status StatsService::getMetadata(vector<uint8_t>* output) { |
| 849 | IPCThreadState* ipc = IPCThreadState::self(); |
| 850 | VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(), |
| 851 | ipc->getCallingUid()); |
| 852 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 853 | StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters. |
| 854 | return Status::ok(); |
| 855 | } else { |
| 856 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 857 | } |
| 858 | } |
| 859 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 860 | Status StatsService::addConfiguration(int64_t key, |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 861 | const vector <uint8_t>& config, |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 862 | bool* success) { |
| 863 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 864 | if (checkCallingPermission(String16(kPermissionDump))) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 865 | ConfigKey configKey(ipc->getCallingUid(), key); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 866 | StatsdConfig cfg; |
Yangster-mac | bbd056a | 2018-01-22 13:37:02 -0800 | [diff] [blame] | 867 | if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) { |
David Chen | 7d8aa4d | 2017-12-27 13:37:01 -0800 | [diff] [blame] | 868 | *success = false; |
| 869 | return Status::ok(); |
| 870 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 871 | mConfigManager->UpdateConfig(configKey, cfg); |
David Chen | 661f791 | 2018-01-22 17:46:24 -0800 | [diff] [blame] | 872 | *success = true; |
| 873 | return Status::ok(); |
| 874 | } else { |
| 875 | *success = false; |
| 876 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | Status StatsService::removeDataFetchOperation(int64_t key, bool* success) { |
| 881 | IPCThreadState* ipc = IPCThreadState::self(); |
| 882 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 883 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 884 | mConfigManager->RemoveConfigReceiver(configKey); |
| 885 | *success = true; |
| 886 | return Status::ok(); |
| 887 | } else { |
| 888 | *success = false; |
| 889 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender, |
| 894 | bool* success) { |
| 895 | IPCThreadState* ipc = IPCThreadState::self(); |
| 896 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 897 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 898 | mConfigManager->SetConfigReceiver(configKey, intentSender); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 899 | *success = true; |
| 900 | return Status::ok(); |
| 901 | } else { |
Yao Chen | aa39bc7 | 2017-12-01 11:16:50 -0800 | [diff] [blame] | 902 | *success = false; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 903 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 904 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 907 | Status StatsService::removeConfiguration(int64_t key, bool* success) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 908 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 909 | if (checkCallingPermission(String16(kPermissionDump))) { |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 910 | ConfigKey configKey(ipc->getCallingUid(), key); |
| 911 | mConfigManager->RemoveConfig(configKey); |
| 912 | SubscriberReporter::getInstance().removeConfig(configKey); |
Yao Chen | aa39bc7 | 2017-12-01 11:16:50 -0800 | [diff] [blame] | 913 | *success = true; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 914 | return Status::ok(); |
| 915 | } else { |
| 916 | *success = false; |
| 917 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 918 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Bookatz | c697797 | 2018-01-16 16:55:05 -0800 | [diff] [blame] | 921 | Status StatsService::setBroadcastSubscriber(int64_t configId, |
| 922 | int64_t subscriberId, |
| 923 | const sp<android::IBinder>& intentSender, |
| 924 | bool* success) { |
| 925 | VLOG("StatsService::setBroadcastSubscriber called."); |
| 926 | IPCThreadState* ipc = IPCThreadState::self(); |
| 927 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 928 | ConfigKey configKey(ipc->getCallingUid(), configId); |
| 929 | SubscriberReporter::getInstance() |
| 930 | .setBroadcastSubscriber(configKey, subscriberId, intentSender); |
| 931 | *success = true; |
| 932 | return Status::ok(); |
| 933 | } else { |
| 934 | *success = false; |
| 935 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | Status StatsService::unsetBroadcastSubscriber(int64_t configId, |
| 940 | int64_t subscriberId, |
| 941 | bool* success) { |
| 942 | VLOG("StatsService::unsetBroadcastSubscriber called."); |
| 943 | IPCThreadState* ipc = IPCThreadState::self(); |
| 944 | if (checkCallingPermission(String16(kPermissionDump))) { |
| 945 | ConfigKey configKey(ipc->getCallingUid(), configId); |
| 946 | SubscriberReporter::getInstance() |
| 947 | .unsetBroadcastSubscriber(configKey, subscriberId); |
| 948 | *success = true; |
| 949 | return Status::ok(); |
| 950 | } else { |
| 951 | *success = false; |
| 952 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 957 | void StatsService::binderDied(const wp <IBinder>& who) { |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 960 | } // namespace statsd |
| 961 | } // namespace os |
| 962 | } // namespace android |