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