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