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 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 17 | #define DEBUG 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" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 22 | #include "config/ConfigKey.h" |
| 23 | #include "config/ConfigManager.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 24 | #include "guardrail/MemoryLeakTrackUtil.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | #include "guardrail/StatsdStats.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 26 | #include "storage/DropboxReader.h" |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 27 | #include "storage/StorageManager.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> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 37 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 38 | #include <stdlib.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 39 | #include <sys/system_properties.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 40 | #include <unistd.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 41 | |
| 42 | using namespace android; |
| 43 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 44 | namespace android { |
| 45 | namespace os { |
| 46 | namespace statsd { |
| 47 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 48 | constexpr const char* kPermissionDump = "android.permission.DUMP"; |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 49 | #define STATS_SERVICE_DIR "/data/system/stats-service" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 50 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 51 | // ====================================================================== |
| 52 | /** |
| 53 | * Watches for the death of the stats companion (system process). |
| 54 | */ |
| 55 | class CompanionDeathRecipient : public IBinder::DeathRecipient { |
| 56 | public: |
| 57 | CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor); |
| 58 | virtual void binderDied(const wp<IBinder>& who); |
| 59 | |
| 60 | private: |
| 61 | const sp<AnomalyMonitor> mAnomalyMonitor; |
| 62 | }; |
| 63 | |
| 64 | CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor) |
| 65 | : mAnomalyMonitor(anomalyMonitor) { |
| 66 | } |
| 67 | |
| 68 | void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 69 | ALOGW("statscompanion service died"); |
| 70 | mAnomalyMonitor->setStatsCompanionService(nullptr); |
| 71 | } |
| 72 | |
| 73 | // ====================================================================== |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 74 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 75 | : mAnomalyMonitor(new AnomalyMonitor(2)) // TODO: Put this comment somewhere better |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 76 | { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 77 | mUidMap = new UidMap(); |
| 78 | mConfigManager = new ConfigManager(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 79 | mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, [this](const ConfigKey& key) { |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 80 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 81 | auto receiver = mConfigManager->GetConfigReceiver(key); |
| 82 | if (sc == nullptr) { |
| 83 | ALOGD("Could not find StatsCompanionService"); |
| 84 | } else if (receiver.first.size() == 0) { |
| 85 | ALOGD("Statscompanion could not find a broadcast receiver for %s", |
| 86 | key.ToString().c_str()); |
| 87 | } else { |
| 88 | sc->sendBroadcast(String16(receiver.first.c_str()), |
| 89 | String16(receiver.second.c_str())); |
| 90 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 91 | }); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 92 | |
| 93 | mConfigManager->AddListener(mProcessor); |
| 94 | |
| 95 | init_system_properties(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 98 | StatsService::~StatsService() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 101 | void StatsService::init_system_properties() { |
| 102 | mEngBuild = false; |
| 103 | const prop_info* buildType = __system_property_find("ro.build.type"); |
| 104 | if (buildType != NULL) { |
| 105 | __system_property_read_callback(buildType, init_build_type_callback, this); |
| 106 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 109 | void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value, |
| 110 | uint32_t serial) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 111 | if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 112 | reinterpret_cast<StatsService*>(cookie)->mEngBuild = true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Implement our own because the default binder implementation isn't |
| 118 | * properly handling SHELL_COMMAND_TRANSACTION. |
| 119 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 120 | status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 121 | uint32_t flags) { |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 122 | status_t err; |
| 123 | |
| 124 | switch (code) { |
| 125 | case SHELL_COMMAND_TRANSACTION: { |
| 126 | int in = data.readFileDescriptor(); |
| 127 | int out = data.readFileDescriptor(); |
| 128 | int err = data.readFileDescriptor(); |
| 129 | int argc = data.readInt32(); |
| 130 | Vector<String8> args; |
| 131 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 132 | args.add(String8(data.readString16())); |
| 133 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 134 | sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder()); |
| 135 | sp<IResultReceiver> resultReceiver = |
| 136 | IResultReceiver::asInterface(data.readStrongBinder()); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 137 | |
| 138 | FILE* fin = fdopen(in, "r"); |
| 139 | FILE* fout = fdopen(out, "w"); |
| 140 | FILE* ferr = fdopen(err, "w"); |
| 141 | |
| 142 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 143 | resultReceiver->send(NO_MEMORY); |
| 144 | } else { |
| 145 | err = command(fin, fout, ferr, args); |
| 146 | resultReceiver->send(err); |
| 147 | } |
| 148 | |
| 149 | if (fin != NULL) { |
| 150 | fflush(fin); |
| 151 | fclose(fin); |
| 152 | } |
| 153 | if (fout != NULL) { |
| 154 | fflush(fout); |
| 155 | fclose(fout); |
| 156 | } |
| 157 | if (fout != NULL) { |
| 158 | fflush(ferr); |
| 159 | fclose(ferr); |
| 160 | } |
| 161 | |
| 162 | return NO_ERROR; |
| 163 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 164 | default: { return BnStatsManager::onTransact(code, data, reply, flags); } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Write debugging data about statsd. |
| 170 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 171 | status_t StatsService::dump(int fd, const Vector<String16>& args) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 172 | FILE* out = fdopen(fd, "w"); |
| 173 | if (out == NULL) { |
| 174 | return NO_MEMORY; // the fd is already open |
| 175 | } |
| 176 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 177 | // TODO: Proto format for incident reports |
| 178 | dump_impl(out); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 179 | |
| 180 | fclose(out); |
| 181 | return NO_ERROR; |
| 182 | } |
| 183 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Write debugging data about statsd in text format. |
| 186 | */ |
| 187 | void StatsService::dump_impl(FILE* out) { |
| 188 | mConfigManager->Dump(out); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Implementation of the adb shell cmd stats command. |
| 193 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 194 | 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] | 195 | // TODO: Permission check |
| 196 | |
| 197 | const int argCount = args.size(); |
| 198 | if (argCount >= 1) { |
| 199 | // adb shell cmd stats config ... |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 200 | if (!args[0].compare(String8("config"))) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 201 | return cmd_config(in, out, err, args); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 202 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 203 | |
| 204 | // adb shell cmd stats print-stats-log |
| 205 | if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) { |
| 206 | return cmd_print_stats_log(out, args); |
| 207 | } |
| 208 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 209 | if (!args[0].compare(String8("print-uid-map"))) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 210 | return cmd_print_uid_map(out); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 211 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 212 | |
| 213 | if (!args[0].compare(String8("dump-report"))) { |
| 214 | return cmd_dump_report(out, err, args); |
| 215 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 216 | |
| 217 | if (!args[0].compare(String8("pull-source")) && args.size() > 1) { |
| 218 | return cmd_print_pulled_metrics(out, args); |
| 219 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 220 | |
| 221 | if (!args[0].compare(String8("send-broadcast"))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 222 | return cmd_trigger_broadcast(out, args); |
| 223 | } |
| 224 | |
| 225 | if (!args[0].compare(String8("print-stats"))) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 226 | return cmd_print_stats(out, args); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 227 | } |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 228 | |
| 229 | if (!args[0].compare(String8("clear-config"))) { |
| 230 | return cmd_remove_config_files(out); |
| 231 | } |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 232 | |
| 233 | if (!args[0].compare(String8("meminfo"))) { |
| 234 | return cmd_dump_memory_info(out); |
| 235 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 236 | |
| 237 | if (!args[0].compare(String8("write-to-disk"))) { |
| 238 | return cmd_write_data_to_disk(out); |
| 239 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 240 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 241 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 242 | print_cmd_help(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 243 | return NO_ERROR; |
| 244 | } |
| 245 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 246 | void StatsService::print_cmd_help(FILE* out) { |
| 247 | fprintf(out, |
| 248 | "usage: adb shell cmd stats print-stats-log [tag_required] " |
| 249 | "[timestamp_nsec_optional]\n"); |
| 250 | fprintf(out, "\n"); |
| 251 | fprintf(out, "\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 252 | fprintf(out, "usage: adb shell cmd stats meminfo\n"); |
| 253 | fprintf(out, "\n"); |
| 254 | fprintf(out, " Prints the malloc debug information. You need to run the following first: \n"); |
| 255 | fprintf(out, " # adb shell stop\n"); |
| 256 | fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n"); |
| 257 | fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n"); |
| 258 | fprintf(out, " # adb shell start\n"); |
| 259 | fprintf(out, "\n"); |
| 260 | fprintf(out, "\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 261 | fprintf(out, "usage: adb shell cmd stats print-uid-map \n"); |
| 262 | fprintf(out, "\n"); |
| 263 | fprintf(out, " Prints the UID, app name, version mapping.\n"); |
| 264 | fprintf(out, "\n"); |
| 265 | fprintf(out, "\n"); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 266 | fprintf(out, "usage: adb shell cmd stats clear-config \n"); |
| 267 | fprintf(out, "\n"); |
| 268 | fprintf(out, " Removes all configs from disk.\n"); |
| 269 | fprintf(out, "\n"); |
| 270 | fprintf(out, "\n"); |
yro | 3fca5ba | 2017-11-17 13:22:52 -0800 | [diff] [blame] | 271 | fprintf(out, "usage: adb shell cmd stats pull-source [int] \n"); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 272 | fprintf(out, "\n"); |
| 273 | fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n"); |
| 274 | fprintf(out, "\n"); |
| 275 | fprintf(out, "\n"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 276 | fprintf(out, "usage: adb shell cmd stats write-to-disk \n"); |
| 277 | fprintf(out, "\n"); |
| 278 | fprintf(out, " Flushes all data on memory to disk.\n"); |
| 279 | fprintf(out, "\n"); |
| 280 | fprintf(out, "\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 281 | fprintf(out, "usage: adb shell cmd stats config remove [UID] NAME\n"); |
| 282 | fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n"); |
| 283 | fprintf(out, "\n"); |
| 284 | fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n"); |
| 285 | fprintf(out, " wire-encoded protobuf format and passed via stdin.\n"); |
| 286 | fprintf(out, "\n"); |
| 287 | fprintf(out, " UID The uid to use. It is only possible to pass the UID\n"); |
| 288 | fprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 289 | fprintf(out, " uid is used.\n"); |
| 290 | fprintf(out, " NAME The per-uid name to use\n"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 291 | fprintf(out, "\n"); |
| 292 | fprintf(out, "\n"); |
| 293 | fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME\n"); |
| 294 | fprintf(out, " Dump all metric data for a configuration.\n"); |
| 295 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 296 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 297 | fprintf(out, " calling uid is used.\n"); |
| 298 | fprintf(out, " NAME The name of the configuration\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 299 | fprintf(out, "\n"); |
| 300 | fprintf(out, "\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 301 | fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n"); |
| 302 | fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n"); |
| 303 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 304 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 305 | fprintf(out, " calling uid is used.\n"); |
| 306 | fprintf(out, " NAME The name of the configuration\n"); |
| 307 | fprintf(out, "\n"); |
| 308 | fprintf(out, "\n"); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 309 | fprintf(out, "usage: adb shell cmd stats print-stats [reset]\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 310 | fprintf(out, " Prints some basic stats.\n"); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 311 | fprintf(out, " reset: 1 to reset the statsd stats. default=0.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 312 | } |
| 313 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 314 | status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) { |
| 315 | string name; |
| 316 | bool good = false; |
| 317 | int uid; |
| 318 | const int argCount = args.size(); |
| 319 | if (argCount == 2) { |
| 320 | // Automatically pick the UID |
| 321 | uid = IPCThreadState::self()->getCallingUid(); |
| 322 | // TODO: What if this isn't a binder call? Should we fail? |
| 323 | name.assign(args[1].c_str(), args[1].size()); |
| 324 | good = true; |
| 325 | } else if (argCount == 3) { |
| 326 | // If it's a userdebug or eng build, then the shell user can |
| 327 | // impersonate other uids. |
| 328 | if (mEngBuild) { |
| 329 | const char* s = args[1].c_str(); |
| 330 | if (*s != '\0') { |
| 331 | char* end = NULL; |
| 332 | uid = strtol(s, &end, 0); |
| 333 | if (*end == '\0') { |
| 334 | name.assign(args[2].c_str(), args[2].size()); |
| 335 | good = true; |
| 336 | } |
| 337 | } |
| 338 | } else { |
| 339 | fprintf(out, |
| 340 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 341 | "builds.\n"); |
| 342 | } |
| 343 | } |
| 344 | if (!good) { |
| 345 | print_cmd_help(out); |
| 346 | return UNKNOWN_ERROR; |
| 347 | } |
| 348 | auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, name)); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 349 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
| 350 | if (sc != nullptr) { |
| 351 | sc->sendBroadcast(String16(receiver.first.c_str()), String16(receiver.second.c_str())); |
| 352 | ALOGD("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), |
| 353 | args[2].c_str()); |
| 354 | } else { |
| 355 | ALOGD("Could not access statsCompanion"); |
| 356 | } |
| 357 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 358 | return NO_ERROR; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 362 | const int argCount = args.size(); |
| 363 | if (argCount >= 2) { |
| 364 | if (args[1] == "update" || args[1] == "remove") { |
| 365 | bool good = false; |
| 366 | int uid = -1; |
| 367 | string name; |
| 368 | |
| 369 | if (argCount == 3) { |
| 370 | // Automatically pick the UID |
| 371 | uid = IPCThreadState::self()->getCallingUid(); |
| 372 | // TODO: What if this isn't a binder call? Should we fail? |
| 373 | name.assign(args[2].c_str(), args[2].size()); |
| 374 | good = true; |
| 375 | } else if (argCount == 4) { |
| 376 | // If it's a userdebug or eng build, then the shell user can |
| 377 | // impersonate other uids. |
| 378 | if (mEngBuild) { |
| 379 | const char* s = args[2].c_str(); |
| 380 | if (*s != '\0') { |
| 381 | char* end = NULL; |
| 382 | uid = strtol(s, &end, 0); |
| 383 | if (*end == '\0') { |
| 384 | name.assign(args[3].c_str(), args[3].size()); |
| 385 | good = true; |
| 386 | } |
| 387 | } |
| 388 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 389 | fprintf(err, |
| 390 | "The config can only be set for other UIDs on eng or userdebug " |
| 391 | "builds.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | if (!good) { |
| 396 | // If arg parsing failed, print the help text and return an error. |
| 397 | print_cmd_help(out); |
| 398 | return UNKNOWN_ERROR; |
| 399 | } |
| 400 | |
| 401 | if (args[1] == "update") { |
| 402 | // Read stream into buffer. |
| 403 | string buffer; |
| 404 | if (!android::base::ReadFdToString(fileno(in), &buffer)) { |
| 405 | fprintf(err, "Error reading stream for StatsConfig.\n"); |
| 406 | return UNKNOWN_ERROR; |
| 407 | } |
| 408 | |
| 409 | // Parse buffer. |
| 410 | StatsdConfig config; |
| 411 | if (!config.ParseFromString(buffer)) { |
| 412 | fprintf(err, "Error parsing proto stream for StatsConfig.\n"); |
| 413 | return UNKNOWN_ERROR; |
| 414 | } |
| 415 | |
| 416 | // Add / update the config. |
| 417 | mConfigManager->UpdateConfig(ConfigKey(uid, name), config); |
| 418 | } else { |
| 419 | // Remove the config. |
| 420 | mConfigManager->RemoveConfig(ConfigKey(uid, name)); |
| 421 | } |
| 422 | |
| 423 | return NO_ERROR; |
| 424 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 425 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 426 | print_cmd_help(out); |
| 427 | return UNKNOWN_ERROR; |
| 428 | } |
| 429 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 430 | status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) { |
| 431 | if (mProcessor != nullptr) { |
| 432 | const int argCount = args.size(); |
| 433 | bool good = false; |
| 434 | int uid; |
| 435 | string name; |
| 436 | if (argCount == 2) { |
| 437 | // Automatically pick the UID |
| 438 | uid = IPCThreadState::self()->getCallingUid(); |
| 439 | // TODO: What if this isn't a binder call? Should we fail? |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 440 | name.assign(args[1].c_str(), args[1].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 441 | good = true; |
| 442 | } else if (argCount == 3) { |
| 443 | // If it's a userdebug or eng build, then the shell user can |
| 444 | // impersonate other uids. |
| 445 | if (mEngBuild) { |
| 446 | const char* s = args[1].c_str(); |
| 447 | if (*s != '\0') { |
| 448 | char* end = NULL; |
| 449 | uid = strtol(s, &end, 0); |
| 450 | if (*end == '\0') { |
| 451 | name.assign(args[2].c_str(), args[2].size()); |
| 452 | good = true; |
| 453 | } |
| 454 | } |
| 455 | } else { |
| 456 | fprintf(out, |
| 457 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 458 | "builds.\n"); |
| 459 | } |
| 460 | } |
| 461 | if (good) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 462 | vector<uint8_t> data; |
| 463 | mProcessor->onDumpReport(ConfigKey(uid, name), &data); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 464 | // TODO: print the returned StatsLogReport to file instead of printing to logcat. |
| 465 | fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str()); |
| 466 | fprintf(out, "See the StatsLogReport in logcat...\n"); |
| 467 | return android::OK; |
| 468 | } else { |
| 469 | // If arg parsing failed, print the help text and return an error. |
| 470 | print_cmd_help(out); |
| 471 | return UNKNOWN_ERROR; |
| 472 | } |
| 473 | } else { |
| 474 | fprintf(out, "Log processor does not exist...\n"); |
| 475 | return UNKNOWN_ERROR; |
| 476 | } |
| 477 | } |
| 478 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 479 | status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 480 | vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys(); |
| 481 | for (const ConfigKey& key : configs) { |
| 482 | fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(), |
| 483 | mProcessor->GetMetricsSize(key)); |
| 484 | } |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 485 | fprintf(out, "Detailed statsd stats in logcat..."); |
| 486 | StatsdStats& statsdStats = StatsdStats::getInstance(); |
| 487 | bool reset = false; |
| 488 | if (args.size() > 1) { |
| 489 | reset = strtol(args[1].string(), NULL, 10); |
| 490 | } |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame^] | 491 | vector<uint8_t> output; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 492 | statsdStats.dumpStats(&output, reset); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 493 | return NO_ERROR; |
| 494 | } |
| 495 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 496 | status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) { |
| 497 | long msec = 0; |
| 498 | |
| 499 | if (args.size() > 2) { |
| 500 | msec = strtol(args[2].string(), NULL, 10); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 501 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 502 | return DropboxReader::readStatsLogs(out, args[1].string(), msec); |
| 503 | } |
| 504 | |
| 505 | status_t StatsService::cmd_print_uid_map(FILE* out) { |
| 506 | mUidMap->printUidMap(out); |
| 507 | return NO_ERROR; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 508 | } |
| 509 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 510 | status_t StatsService::cmd_write_data_to_disk(FILE* out) { |
| 511 | fprintf(out, "Writing data to disk\n"); |
| 512 | mProcessor->WriteDataToDisk(); |
| 513 | return NO_ERROR; |
| 514 | } |
| 515 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 516 | status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) { |
| 517 | int s = atoi(args[1].c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 518 | vector<shared_ptr<LogEvent> > stats; |
| 519 | if (mStatsPullerManager.Pull(s, &stats)) { |
| 520 | for (const auto& it : stats) { |
| 521 | fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); |
| 522 | } |
| 523 | fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); |
| 524 | return NO_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 525 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 526 | return UNKNOWN_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 527 | } |
| 528 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 529 | status_t StatsService::cmd_remove_config_files(FILE* out) { |
| 530 | fprintf(out, "Trying to remove config files...\n"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 531 | StorageManager::deleteAllFiles(STATS_SERVICE_DIR); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 532 | return NO_ERROR; |
| 533 | } |
| 534 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 535 | status_t StatsService::cmd_dump_memory_info(FILE* out) { |
| 536 | std::string s = dumpMemInfo(100); |
| 537 | fprintf(out, "Memory Info\n"); |
| 538 | fprintf(out, "%s", s.c_str()); |
| 539 | return NO_ERROR; |
| 540 | } |
| 541 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 542 | Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version, |
| 543 | const vector<String16>& app) { |
| 544 | if (DEBUG) ALOGD("StatsService::informAllUidData was called"); |
| 545 | |
| 546 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 547 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 548 | "Only system uid can call informAllUidData"); |
| 549 | } |
| 550 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 551 | mUidMap->updateMap(uid, version, app); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 552 | if (DEBUG) ALOGD("StatsService::informAllUidData succeeded"); |
| 553 | |
| 554 | return Status::ok(); |
| 555 | } |
| 556 | |
| 557 | Status StatsService::informOnePackage(const String16& app, int32_t uid, int32_t version) { |
| 558 | if (DEBUG) ALOGD("StatsService::informOnePackage was called"); |
| 559 | |
| 560 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 561 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 562 | "Only system uid can call informOnePackage"); |
| 563 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 564 | mUidMap->updateApp(app, uid, version); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 565 | return Status::ok(); |
| 566 | } |
| 567 | |
| 568 | Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) { |
| 569 | if (DEBUG) ALOGD("StatsService::informOnePackageRemoved was called"); |
| 570 | |
| 571 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 572 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 573 | "Only system uid can call informOnePackageRemoved"); |
| 574 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 575 | mUidMap->removeApp(app, uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 576 | return Status::ok(); |
| 577 | } |
| 578 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 579 | Status StatsService::informAnomalyAlarmFired() { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 580 | if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 581 | |
| 582 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 583 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 584 | "Only system uid can call informAnomalyAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 587 | if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 588 | // TODO: check through all counters/timers and see if an anomaly has indeed occurred. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 589 | uint64_t currentTimeNs = time(nullptr) * NS_PER_SEC; |
| 590 | std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet = |
| 591 | mAnomalyMonitor->onAlarmFired(currentTimeNs); |
| 592 | mProcessor->onAnomalyAlarmFired(currentTimeNs, anomalySet); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 593 | return Status::ok(); |
| 594 | } |
| 595 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 596 | Status StatsService::informPollAlarmFired() { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 597 | if (DEBUG) ALOGD("StatsService::informPollAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 598 | |
| 599 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 600 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 601 | "Only system uid can call informPollAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 604 | mStatsPullerManager.OnAlarmFired(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 605 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 606 | if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 607 | |
| 608 | return Status::ok(); |
| 609 | } |
| 610 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 611 | Status StatsService::systemRunning() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 612 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 613 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 614 | "Only system uid can call systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // When system_server is up and running, schedule the dropbox task to run. |
| 618 | ALOGD("StatsService::systemRunning"); |
| 619 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 620 | sayHiToStatsCompanion(); |
| 621 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 622 | return Status::ok(); |
| 623 | } |
| 624 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 625 | Status StatsService::writeDataToDisk() { |
| 626 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 627 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 628 | "Only system uid can call systemRunning"); |
| 629 | } |
| 630 | |
| 631 | ALOGD("StatsService::writeDataToDisk"); |
| 632 | |
| 633 | mProcessor->WriteDataToDisk(); |
| 634 | |
| 635 | return Status::ok(); |
| 636 | } |
| 637 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 638 | void StatsService::sayHiToStatsCompanion() { |
| 639 | // TODO: This method needs to be private. It is temporarily public and unsecured for testing |
| 640 | // purposes. |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 641 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 642 | if (statsCompanion != nullptr) { |
| 643 | if (DEBUG) ALOGD("Telling statsCompanion that statsd is ready"); |
| 644 | statsCompanion->statsdReady(); |
| 645 | } else { |
| 646 | if (DEBUG) ALOGD("Could not access statsCompanion"); |
| 647 | } |
| 648 | } |
| 649 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 650 | sp<IStatsCompanionService> StatsService::getStatsCompanionService() { |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 651 | sp<IStatsCompanionService> statsCompanion = nullptr; |
| 652 | // Get statscompanion service from service manager |
| 653 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 654 | if (sm != nullptr) { |
| 655 | const String16 name("statscompanion"); |
| 656 | statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name)); |
| 657 | if (statsCompanion == nullptr) { |
| 658 | ALOGW("statscompanion service unavailable!"); |
| 659 | return nullptr; |
| 660 | } |
| 661 | } |
| 662 | return statsCompanion; |
| 663 | } |
| 664 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 665 | Status StatsService::statsCompanionReady() { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 666 | if (DEBUG) ALOGD("StatsService::statsCompanionReady was called"); |
| 667 | |
| 668 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 669 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 670 | "Only system uid can call statsCompanionReady"); |
| 671 | } |
| 672 | |
| 673 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 674 | if (statsCompanion == nullptr) { |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 675 | return Status::fromExceptionCode( |
| 676 | Status::EX_NULL_POINTER, |
| 677 | "statscompanion unavailable despite it contacting statsd!"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 678 | } |
| 679 | if (DEBUG) ALOGD("StatsService::statsCompanionReady linking to statsCompanion."); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 680 | IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor)); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 681 | mAnomalyMonitor->setStatsCompanionService(statsCompanion); |
| 682 | |
| 683 | return Status::ok(); |
| 684 | } |
| 685 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 686 | void StatsService::Startup() { |
| 687 | mConfigManager->Startup(); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 690 | void StatsService::OnLogEvent(const LogEvent& event) { |
| 691 | mProcessor->OnLogEvent(event); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 694 | Status StatsService::getData(const String16& key, vector<uint8_t>* output) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 695 | IPCThreadState* ipc = IPCThreadState::self(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 696 | ALOGD("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), |
| 697 | ipc->getCallingUid()); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 698 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 699 | string keyStr = string(String8(key).string()); |
| 700 | ConfigKey configKey(ipc->getCallingUid(), keyStr); |
| 701 | mProcessor->onDumpReport(configKey, output); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 702 | return Status::ok(); |
| 703 | } else { |
| 704 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 705 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 706 | } |
| 707 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 708 | Status StatsService::addConfiguration(const String16& key, |
| 709 | const vector <uint8_t>& config, |
| 710 | const String16& package, const String16& cls, |
| 711 | bool* success) { |
| 712 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 713 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 714 | string keyString = string(String8(key).string()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 715 | ConfigKey configKey(ipc->getCallingUid(), keyString); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 716 | StatsdConfig cfg; |
| 717 | cfg.ParseFromArray(&config[0], config.size()); |
| 718 | mConfigManager->UpdateConfig(configKey, cfg); |
| 719 | mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()), |
| 720 | string(String8(cls).string())); |
| 721 | *success = true; |
| 722 | return Status::ok(); |
| 723 | } else { |
| 724 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 725 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 726 | } |
| 727 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 728 | Status StatsService::removeConfiguration(const String16& key, bool* success) { |
| 729 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 730 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 731 | string keyStr = string(String8(key).string()); |
| 732 | mConfigManager->RemoveConfig(ConfigKey(ipc->getCallingUid(), keyStr)); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 733 | return Status::ok(); |
| 734 | } else { |
| 735 | *success = false; |
| 736 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 737 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 738 | } |
| 739 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 740 | void StatsService::binderDied(const wp <IBinder>& who) { |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 743 | } // namespace statsd |
| 744 | } // namespace os |
| 745 | } // namespace android |