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 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 17 | #define DEBUG true // 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" |
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) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 83 | VLOG("Could not find StatsCompanionService"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 84 | } else if (receiver.first.size() == 0) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 85 | VLOG("Statscompanion could not find a broadcast receiver for %s", |
| 86 | key.ToString().c_str()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 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 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 229 | if (!args[0].compare(String8("meminfo"))) { |
| 230 | return cmd_dump_memory_info(out); |
| 231 | } |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 232 | |
| 233 | if (!args[0].compare(String8("write-to-disk"))) { |
| 234 | return cmd_write_data_to_disk(out); |
| 235 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 236 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 237 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 238 | print_cmd_help(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 239 | return NO_ERROR; |
| 240 | } |
| 241 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 242 | void StatsService::print_cmd_help(FILE* out) { |
| 243 | fprintf(out, |
| 244 | "usage: adb shell cmd stats print-stats-log [tag_required] " |
| 245 | "[timestamp_nsec_optional]\n"); |
| 246 | fprintf(out, "\n"); |
| 247 | fprintf(out, "\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 248 | fprintf(out, "usage: adb shell cmd stats meminfo\n"); |
| 249 | fprintf(out, "\n"); |
| 250 | fprintf(out, " Prints the malloc debug information. You need to run the following first: \n"); |
| 251 | fprintf(out, " # adb shell stop\n"); |
| 252 | fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n"); |
| 253 | fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n"); |
| 254 | fprintf(out, " # adb shell start\n"); |
| 255 | fprintf(out, "\n"); |
| 256 | fprintf(out, "\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 257 | fprintf(out, "usage: adb shell cmd stats print-uid-map \n"); |
| 258 | fprintf(out, "\n"); |
| 259 | fprintf(out, " Prints the UID, app name, version mapping.\n"); |
| 260 | fprintf(out, "\n"); |
| 261 | fprintf(out, "\n"); |
yro | 3fca5ba | 2017-11-17 13:22:52 -0800 | [diff] [blame] | 262 | fprintf(out, "usage: adb shell cmd stats pull-source [int] \n"); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 263 | fprintf(out, "\n"); |
| 264 | fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n"); |
| 265 | fprintf(out, "\n"); |
| 266 | fprintf(out, "\n"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 267 | fprintf(out, "usage: adb shell cmd stats write-to-disk \n"); |
| 268 | fprintf(out, "\n"); |
| 269 | fprintf(out, " Flushes all data on memory to disk.\n"); |
| 270 | fprintf(out, "\n"); |
| 271 | fprintf(out, "\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 272 | fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 273 | fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n"); |
| 274 | fprintf(out, "\n"); |
| 275 | 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] | 276 | fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n"); |
| 277 | 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] | 278 | fprintf(out, "\n"); |
| 279 | 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] | 280 | 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] | 281 | fprintf(out, " uid is used.\n"); |
| 282 | fprintf(out, " NAME The per-uid name to use\n"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 283 | fprintf(out, "\n"); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 284 | fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n"); |
| 285 | fprintf(out, "\n be removed from memory and disk!\n"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 286 | fprintf(out, "\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 287 | fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 288 | fprintf(out, " Dump all metric data for a configuration.\n"); |
| 289 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 290 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 291 | fprintf(out, " calling uid is used.\n"); |
| 292 | fprintf(out, " NAME The name of the configuration\n"); |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 293 | fprintf(out, " --proto Print proto binary.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 294 | fprintf(out, "\n"); |
| 295 | fprintf(out, "\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 296 | fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n"); |
| 297 | fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n"); |
| 298 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 299 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 300 | fprintf(out, " calling uid is used.\n"); |
| 301 | fprintf(out, " NAME The name of the configuration\n"); |
| 302 | fprintf(out, "\n"); |
| 303 | fprintf(out, "\n"); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 304 | fprintf(out, "usage: adb shell cmd stats print-stats [reset]\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 305 | fprintf(out, " Prints some basic stats.\n"); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 306 | fprintf(out, " reset: 1 to reset the statsd stats. default=0.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 307 | } |
| 308 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 309 | status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) { |
| 310 | string name; |
| 311 | bool good = false; |
| 312 | int uid; |
| 313 | const int argCount = args.size(); |
| 314 | if (argCount == 2) { |
| 315 | // Automatically pick the UID |
| 316 | uid = IPCThreadState::self()->getCallingUid(); |
| 317 | // TODO: What if this isn't a binder call? Should we fail? |
| 318 | name.assign(args[1].c_str(), args[1].size()); |
| 319 | good = true; |
| 320 | } else if (argCount == 3) { |
| 321 | // If it's a userdebug or eng build, then the shell user can |
| 322 | // impersonate other uids. |
| 323 | if (mEngBuild) { |
| 324 | const char* s = args[1].c_str(); |
| 325 | if (*s != '\0') { |
| 326 | char* end = NULL; |
| 327 | uid = strtol(s, &end, 0); |
| 328 | if (*end == '\0') { |
| 329 | name.assign(args[2].c_str(), args[2].size()); |
| 330 | good = true; |
| 331 | } |
| 332 | } |
| 333 | } else { |
| 334 | fprintf(out, |
| 335 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 336 | "builds.\n"); |
| 337 | } |
| 338 | } |
| 339 | if (!good) { |
| 340 | print_cmd_help(out); |
| 341 | return UNKNOWN_ERROR; |
| 342 | } |
| 343 | auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, name)); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 344 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
| 345 | if (sc != nullptr) { |
| 346 | sc->sendBroadcast(String16(receiver.first.c_str()), String16(receiver.second.c_str())); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 347 | VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), |
| 348 | args[2].c_str()); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 349 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 350 | VLOG("Could not access statsCompanion"); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 351 | } |
| 352 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 353 | return NO_ERROR; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 357 | const int argCount = args.size(); |
| 358 | if (argCount >= 2) { |
| 359 | if (args[1] == "update" || args[1] == "remove") { |
| 360 | bool good = false; |
| 361 | int uid = -1; |
| 362 | string name; |
| 363 | |
| 364 | if (argCount == 3) { |
| 365 | // Automatically pick the UID |
| 366 | uid = IPCThreadState::self()->getCallingUid(); |
| 367 | // TODO: What if this isn't a binder call? Should we fail? |
| 368 | name.assign(args[2].c_str(), args[2].size()); |
| 369 | good = true; |
| 370 | } else if (argCount == 4) { |
| 371 | // If it's a userdebug or eng build, then the shell user can |
| 372 | // impersonate other uids. |
| 373 | if (mEngBuild) { |
| 374 | const char* s = args[2].c_str(); |
| 375 | if (*s != '\0') { |
| 376 | char* end = NULL; |
| 377 | uid = strtol(s, &end, 0); |
| 378 | if (*end == '\0') { |
| 379 | name.assign(args[3].c_str(), args[3].size()); |
| 380 | good = true; |
| 381 | } |
| 382 | } |
| 383 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 384 | fprintf(err, |
| 385 | "The config can only be set for other UIDs on eng or userdebug " |
| 386 | "builds.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | if (!good) { |
| 391 | // If arg parsing failed, print the help text and return an error. |
| 392 | print_cmd_help(out); |
| 393 | return UNKNOWN_ERROR; |
| 394 | } |
| 395 | |
| 396 | if (args[1] == "update") { |
| 397 | // Read stream into buffer. |
| 398 | string buffer; |
| 399 | if (!android::base::ReadFdToString(fileno(in), &buffer)) { |
| 400 | fprintf(err, "Error reading stream for StatsConfig.\n"); |
| 401 | return UNKNOWN_ERROR; |
| 402 | } |
| 403 | |
| 404 | // Parse buffer. |
| 405 | StatsdConfig config; |
| 406 | if (!config.ParseFromString(buffer)) { |
| 407 | fprintf(err, "Error parsing proto stream for StatsConfig.\n"); |
| 408 | return UNKNOWN_ERROR; |
| 409 | } |
| 410 | |
| 411 | // Add / update the config. |
| 412 | mConfigManager->UpdateConfig(ConfigKey(uid, name), config); |
| 413 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 414 | if (argCount == 2) { |
| 415 | cmd_remove_all_configs(out); |
| 416 | } else { |
| 417 | // Remove the config. |
| 418 | mConfigManager->RemoveConfig(ConfigKey(uid, name)); |
| 419 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return NO_ERROR; |
| 423 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 424 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 425 | print_cmd_help(out); |
| 426 | return UNKNOWN_ERROR; |
| 427 | } |
| 428 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 429 | status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) { |
| 430 | if (mProcessor != nullptr) { |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 431 | int argCount = args.size(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 432 | bool good = false; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 433 | bool proto = false; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 434 | int uid; |
| 435 | string name; |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 436 | if (!std::strcmp("--proto", args[argCount-1].c_str())) { |
| 437 | proto = true; |
| 438 | argCount -= 1; |
| 439 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 440 | if (argCount == 2) { |
| 441 | // Automatically pick the UID |
| 442 | uid = IPCThreadState::self()->getCallingUid(); |
| 443 | // TODO: What if this isn't a binder call? Should we fail? |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 444 | name.assign(args[1].c_str(), args[1].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 445 | good = true; |
| 446 | } else if (argCount == 3) { |
| 447 | // If it's a userdebug or eng build, then the shell user can |
| 448 | // impersonate other uids. |
| 449 | if (mEngBuild) { |
| 450 | const char* s = args[1].c_str(); |
| 451 | if (*s != '\0') { |
| 452 | char* end = NULL; |
| 453 | uid = strtol(s, &end, 0); |
| 454 | if (*end == '\0') { |
| 455 | name.assign(args[2].c_str(), args[2].size()); |
| 456 | good = true; |
| 457 | } |
| 458 | } |
| 459 | } else { |
| 460 | fprintf(out, |
| 461 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 462 | "builds.\n"); |
| 463 | } |
| 464 | } |
| 465 | if (good) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 466 | vector<uint8_t> data; |
| 467 | mProcessor->onDumpReport(ConfigKey(uid, name), &data); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 468 | // TODO: print the returned StatsLogReport to file instead of printing to logcat. |
Chenjie Yu | b236c86 | 2017-11-28 22:20:44 -0800 | [diff] [blame] | 469 | if (proto) { |
| 470 | for (size_t i = 0; i < data.size(); i ++) { |
| 471 | fprintf(out, "%c", data[i]); |
| 472 | } |
| 473 | } else { |
| 474 | fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str()); |
| 475 | fprintf(out, "See the StatsLogReport in logcat...\n"); |
| 476 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 477 | return android::OK; |
| 478 | } else { |
| 479 | // If arg parsing failed, print the help text and return an error. |
| 480 | print_cmd_help(out); |
| 481 | return UNKNOWN_ERROR; |
| 482 | } |
| 483 | } else { |
| 484 | fprintf(out, "Log processor does not exist...\n"); |
| 485 | return UNKNOWN_ERROR; |
| 486 | } |
| 487 | } |
| 488 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 489 | status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 490 | vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys(); |
| 491 | for (const ConfigKey& key : configs) { |
| 492 | fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(), |
| 493 | mProcessor->GetMetricsSize(key)); |
| 494 | } |
David Chen | c136f45a | 2017-11-27 11:52:26 -0800 | [diff] [blame] | 495 | fprintf(out, "Detailed statsd stats in logcat...\n"); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 496 | StatsdStats& statsdStats = StatsdStats::getInstance(); |
| 497 | bool reset = false; |
| 498 | if (args.size() > 1) { |
| 499 | reset = strtol(args[1].string(), NULL, 10); |
| 500 | } |
Yao Chen | 69f1baf | 2017-11-27 17:25:36 -0800 | [diff] [blame] | 501 | vector<uint8_t> output; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 502 | statsdStats.dumpStats(&output, reset); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 503 | return NO_ERROR; |
| 504 | } |
| 505 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 506 | status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) { |
| 507 | long msec = 0; |
| 508 | |
| 509 | if (args.size() > 2) { |
| 510 | msec = strtol(args[2].string(), NULL, 10); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 511 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 512 | return DropboxReader::readStatsLogs(out, args[1].string(), msec); |
| 513 | } |
| 514 | |
| 515 | status_t StatsService::cmd_print_uid_map(FILE* out) { |
| 516 | mUidMap->printUidMap(out); |
| 517 | return NO_ERROR; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 518 | } |
| 519 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 520 | status_t StatsService::cmd_write_data_to_disk(FILE* out) { |
| 521 | fprintf(out, "Writing data to disk\n"); |
| 522 | mProcessor->WriteDataToDisk(); |
| 523 | return NO_ERROR; |
| 524 | } |
| 525 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 526 | status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) { |
| 527 | int s = atoi(args[1].c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 528 | vector<shared_ptr<LogEvent> > stats; |
| 529 | if (mStatsPullerManager.Pull(s, &stats)) { |
| 530 | for (const auto& it : stats) { |
| 531 | fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); |
| 532 | } |
| 533 | fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); |
| 534 | return NO_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 535 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 536 | return UNKNOWN_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 537 | } |
| 538 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 539 | status_t StatsService::cmd_remove_all_configs(FILE* out) { |
| 540 | fprintf(out, "Removing all configs...\n"); |
| 541 | VLOG("StatsService::cmd_remove_all_configs was called"); |
| 542 | mConfigManager->RemoveAllConfigs(); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 543 | StorageManager::deleteAllFiles(STATS_SERVICE_DIR); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 544 | return NO_ERROR; |
| 545 | } |
| 546 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 547 | status_t StatsService::cmd_dump_memory_info(FILE* out) { |
| 548 | std::string s = dumpMemInfo(100); |
| 549 | fprintf(out, "Memory Info\n"); |
| 550 | fprintf(out, "%s", s.c_str()); |
| 551 | return NO_ERROR; |
| 552 | } |
| 553 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame^] | 554 | 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] | 555 | const vector<String16>& app) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 556 | VLOG("StatsService::informAllUidData was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 557 | |
| 558 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 559 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 560 | "Only system uid can call informAllUidData"); |
| 561 | } |
| 562 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 563 | mUidMap->updateMap(uid, version, app); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 564 | VLOG("StatsService::informAllUidData succeeded"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 565 | |
| 566 | return Status::ok(); |
| 567 | } |
| 568 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame^] | 569 | Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 570 | VLOG("StatsService::informOnePackage was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 571 | |
| 572 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 573 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 574 | "Only system uid can call informOnePackage"); |
| 575 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 576 | mUidMap->updateApp(app, uid, version); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 577 | return Status::ok(); |
| 578 | } |
| 579 | |
| 580 | Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 581 | VLOG("StatsService::informOnePackageRemoved was called"); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 582 | |
| 583 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 584 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 585 | "Only system uid can call informOnePackageRemoved"); |
| 586 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 587 | mUidMap->removeApp(app, uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 588 | return Status::ok(); |
| 589 | } |
| 590 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 591 | Status StatsService::informAnomalyAlarmFired() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 592 | VLOG("StatsService::informAnomalyAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 593 | |
| 594 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 595 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 596 | "Only system uid can call informAnomalyAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 597 | } |
| 598 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 599 | VLOG("StatsService::informAnomalyAlarmFired succeeded"); |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 600 | uint64_t currentTimeSec = time(nullptr); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 601 | std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet = |
Bookatz | cc5adef2 | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 602 | mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec)); |
| 603 | mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, anomalySet); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 604 | return Status::ok(); |
| 605 | } |
| 606 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 607 | Status StatsService::informPollAlarmFired() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 608 | VLOG("StatsService::informPollAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 609 | |
| 610 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 611 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 612 | "Only system uid can call informPollAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 615 | mStatsPullerManager.OnAlarmFired(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 616 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 617 | VLOG("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 618 | |
| 619 | return Status::ok(); |
| 620 | } |
| 621 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 622 | Status StatsService::systemRunning() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 623 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 624 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 625 | "Only system uid can call systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | // When system_server is up and running, schedule the dropbox task to run. |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 629 | VLOG("StatsService::systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 630 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 631 | sayHiToStatsCompanion(); |
| 632 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 633 | return Status::ok(); |
| 634 | } |
| 635 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 636 | Status StatsService::writeDataToDisk() { |
| 637 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 638 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 639 | "Only system uid can call systemRunning"); |
| 640 | } |
| 641 | |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 642 | VLOG("StatsService::writeDataToDisk"); |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 643 | |
| 644 | mProcessor->WriteDataToDisk(); |
| 645 | |
| 646 | return Status::ok(); |
| 647 | } |
| 648 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 649 | void StatsService::sayHiToStatsCompanion() { |
| 650 | // TODO: This method needs to be private. It is temporarily public and unsecured for testing |
| 651 | // purposes. |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 652 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 653 | if (statsCompanion != nullptr) { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 654 | VLOG("Telling statsCompanion that statsd is ready"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 655 | statsCompanion->statsdReady(); |
| 656 | } else { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 657 | VLOG("Could not access statsCompanion"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 661 | sp<IStatsCompanionService> StatsService::getStatsCompanionService() { |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 662 | sp<IStatsCompanionService> statsCompanion = nullptr; |
| 663 | // Get statscompanion service from service manager |
| 664 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 665 | if (sm != nullptr) { |
| 666 | const String16 name("statscompanion"); |
| 667 | statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name)); |
| 668 | if (statsCompanion == nullptr) { |
| 669 | ALOGW("statscompanion service unavailable!"); |
| 670 | return nullptr; |
| 671 | } |
| 672 | } |
| 673 | return statsCompanion; |
| 674 | } |
| 675 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 676 | Status StatsService::statsCompanionReady() { |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 677 | VLOG("StatsService::statsCompanionReady was called"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 678 | |
| 679 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 680 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 681 | "Only system uid can call statsCompanionReady"); |
| 682 | } |
| 683 | |
| 684 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 685 | if (statsCompanion == nullptr) { |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 686 | return Status::fromExceptionCode( |
| 687 | Status::EX_NULL_POINTER, |
| 688 | "statscompanion unavailable despite it contacting statsd!"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 689 | } |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 690 | VLOG("StatsService::statsCompanionReady linking to statsCompanion."); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 691 | IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor)); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 692 | mAnomalyMonitor->setStatsCompanionService(statsCompanion); |
| 693 | |
| 694 | return Status::ok(); |
| 695 | } |
| 696 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 697 | void StatsService::Startup() { |
| 698 | mConfigManager->Startup(); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 701 | void StatsService::OnLogEvent(const LogEvent& event) { |
| 702 | mProcessor->OnLogEvent(event); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 705 | Status StatsService::getData(const String16& key, vector<uint8_t>* output) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 706 | IPCThreadState* ipc = IPCThreadState::self(); |
yro | 74fed97 | 2017-11-27 14:42:42 -0800 | [diff] [blame] | 707 | VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid()); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 708 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 709 | string keyStr = string(String8(key).string()); |
| 710 | ConfigKey configKey(ipc->getCallingUid(), keyStr); |
| 711 | mProcessor->onDumpReport(configKey, output); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 712 | return Status::ok(); |
| 713 | } else { |
| 714 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 715 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 716 | } |
| 717 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 718 | Status StatsService::addConfiguration(const String16& key, |
| 719 | const vector <uint8_t>& config, |
| 720 | const String16& package, const String16& cls, |
| 721 | bool* success) { |
| 722 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 723 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 724 | string keyString = string(String8(key).string()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 725 | ConfigKey configKey(ipc->getCallingUid(), keyString); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 726 | StatsdConfig cfg; |
| 727 | cfg.ParseFromArray(&config[0], config.size()); |
| 728 | mConfigManager->UpdateConfig(configKey, cfg); |
| 729 | mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()), |
| 730 | string(String8(cls).string())); |
| 731 | *success = true; |
| 732 | return Status::ok(); |
| 733 | } else { |
| 734 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 735 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 736 | } |
| 737 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 738 | Status StatsService::removeConfiguration(const String16& key, bool* success) { |
| 739 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 740 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 741 | string keyStr = string(String8(key).string()); |
| 742 | mConfigManager->RemoveConfig(ConfigKey(ipc->getCallingUid(), keyStr)); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 743 | return Status::ok(); |
| 744 | } else { |
| 745 | *success = false; |
| 746 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 747 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 748 | } |
| 749 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 750 | void StatsService::binderDied(const wp <IBinder>& who) { |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 753 | } // namespace statsd |
| 754 | } // namespace os |
| 755 | } // namespace android |