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