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 | |
| 17 | #define LOG_TAG "statsd" |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 18 | #define DEBUG true |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 19 | |
| 20 | #include "StatsService.h" |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 21 | #include "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> |
| 26 | #include <cutils/log.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 27 | #include <frameworks/base/cmds/statsd/src/statsd_config.pb.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
| 29 | #include <utils/Looper.h> |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 30 | #include <utils/String16.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 31 | |
| 32 | #include <unistd.h> |
| 33 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 34 | #include <stdlib.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 35 | |
| 36 | using namespace android; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 37 | using android::os::statsd::StatsdConfig; |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 38 | |
| 39 | // ================================================================================ |
| 40 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 41 | : mAnomalyMonitor(new AnomalyMonitor(2)) // TODO: Change this based on the config |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 42 | { |
| 43 | ALOGD("stats service constructed"); |
| 44 | } |
| 45 | |
| 46 | StatsService::~StatsService() |
| 47 | { |
| 48 | } |
| 49 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 50 | status_t |
| 51 | StatsService::setProcessor(const sp<StatsLogProcessor>& main_processor) { |
| 52 | m_processor = main_processor; |
| 53 | ALOGD("stats service set to processor %p", m_processor.get()); |
| 54 | return NO_ERROR; |
| 55 | } |
| 56 | |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 57 | // Implement our own because the default binder implementation isn't |
| 58 | // properly handling SHELL_COMMAND_TRANSACTION |
| 59 | status_t |
| 60 | StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 61 | { |
| 62 | status_t err; |
| 63 | |
| 64 | switch (code) { |
| 65 | case SHELL_COMMAND_TRANSACTION: { |
| 66 | int in = data.readFileDescriptor(); |
| 67 | int out = data.readFileDescriptor(); |
| 68 | int err = data.readFileDescriptor(); |
| 69 | int argc = data.readInt32(); |
| 70 | Vector<String8> args; |
| 71 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 72 | args.add(String8(data.readString16())); |
| 73 | } |
| 74 | sp<IShellCallback> shellCallback = IShellCallback::asInterface( |
| 75 | data.readStrongBinder()); |
| 76 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( |
| 77 | data.readStrongBinder()); |
| 78 | |
| 79 | FILE* fin = fdopen(in, "r"); |
| 80 | FILE* fout = fdopen(out, "w"); |
| 81 | FILE* ferr = fdopen(err, "w"); |
| 82 | |
| 83 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 84 | resultReceiver->send(NO_MEMORY); |
| 85 | } else { |
| 86 | err = command(fin, fout, ferr, args); |
| 87 | resultReceiver->send(err); |
| 88 | } |
| 89 | |
| 90 | if (fin != NULL) { |
| 91 | fflush(fin); |
| 92 | fclose(fin); |
| 93 | } |
| 94 | if (fout != NULL) { |
| 95 | fflush(fout); |
| 96 | fclose(fout); |
| 97 | } |
| 98 | if (fout != NULL) { |
| 99 | fflush(ferr); |
| 100 | fclose(ferr); |
| 101 | } |
| 102 | |
| 103 | return NO_ERROR; |
| 104 | } |
| 105 | default: { |
| 106 | return BnStatsManager::onTransact(code, data, reply, flags); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 111 | status_t |
| 112 | StatsService::dump(int fd, const Vector<String16>& args) |
| 113 | { |
| 114 | FILE* out = fdopen(fd, "w"); |
| 115 | if (out == NULL) { |
| 116 | return NO_MEMORY; // the fd is already open |
| 117 | } |
| 118 | |
| 119 | fprintf(out, "StatsService::dump:"); |
| 120 | ALOGD("StatsService::dump:"); |
| 121 | const int N = args.size(); |
| 122 | for (int i=0; i<N; i++) { |
| 123 | fprintf(out, " %s", String8(args[i]).string()); |
| 124 | ALOGD(" %s", String8(args[i]).string()); |
| 125 | } |
| 126 | fprintf(out, "\n"); |
| 127 | |
| 128 | fclose(out); |
| 129 | return NO_ERROR; |
| 130 | } |
| 131 | |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 132 | status_t |
| 133 | StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) |
| 134 | { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 135 | if (args.size() > 0) { |
| 136 | if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) { |
| 137 | return doPrintStatsLog(out, args); |
| 138 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 139 | if (!args[0].compare(String8("config"))) { |
| 140 | return doLoadConfig(in); |
| 141 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 142 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 143 | |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 144 | printCmdHelp(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 145 | return NO_ERROR; |
| 146 | } |
| 147 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 148 | status_t |
| 149 | StatsService::doLoadConfig(FILE* in) |
| 150 | { |
| 151 | string content; |
| 152 | if (!android::base::ReadFdToString(fileno(in), &content)) { |
| 153 | return UNKNOWN_ERROR; |
| 154 | } |
| 155 | StatsdConfig config; |
| 156 | if (config.ParseFromString(content)) { |
| 157 | ALOGD("Config parsed from command line: %s", config.SerializeAsString().c_str()); |
| 158 | m_processor->UpdateConfig(0, config); |
| 159 | return NO_ERROR; |
| 160 | } else { |
| 161 | ALOGD("Config failed to be parsed"); |
| 162 | return UNKNOWN_ERROR; |
| 163 | } |
| 164 | } |
| 165 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 166 | Status |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 167 | StatsService::informAnomalyAlarmFired() |
| 168 | { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 169 | if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 170 | |
| 171 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 172 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 173 | "Only system uid can call informAnomalyAlarmFired"); |
| 174 | } |
| 175 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 176 | if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 177 | // TODO: check through all counters/timers and see if an anomaly has indeed occurred. |
| 178 | |
| 179 | return Status::ok(); |
| 180 | } |
| 181 | |
| 182 | Status |
| 183 | StatsService::informPollAlarmFired() |
| 184 | { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 185 | if (DEBUG) ALOGD("StatsService::informPollAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 186 | |
| 187 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 188 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 189 | "Only system uid can call informPollAlarmFired"); |
| 190 | } |
| 191 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 192 | if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 193 | // TODO: determine what services to poll and poll (or ask StatsCompanionService to poll) them. |
| 194 | |
| 195 | return Status::ok(); |
| 196 | } |
| 197 | |
| 198 | Status |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 199 | StatsService::systemRunning() |
| 200 | { |
| 201 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 202 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 203 | "Only system uid can call systemRunning"); |
| 204 | } |
| 205 | |
| 206 | // When system_server is up and running, schedule the dropbox task to run. |
| 207 | ALOGD("StatsService::systemRunning"); |
| 208 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 209 | sayHiToStatsCompanion(); |
| 210 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 211 | return Status::ok(); |
| 212 | } |
| 213 | |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 214 | status_t |
| 215 | StatsService::doPrintStatsLog(FILE* out, const Vector<String8>& args) { |
| 216 | long msec = 0; |
| 217 | |
| 218 | if (args.size() > 2) { |
| 219 | msec = strtol(args[2].string(), NULL, 10); |
| 220 | } |
| 221 | return DropboxReader::readStatsLogs(out, args[1].string(), msec); |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | StatsService::printCmdHelp(FILE* out) { |
| 226 | fprintf(out, "Usage:\n"); |
| 227 | fprintf(out, "\t print-stats-log [tag_required] [timestamp_nsec_optional]\n"); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 228 | fprintf(out, "\t config\t Loads a new config from command-line (must be proto in wire-encoded format).\n"); |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 229 | } |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 230 | |
| 231 | void |
| 232 | StatsService::sayHiToStatsCompanion() |
| 233 | { |
| 234 | // TODO: This method needs to be private. It is temporarily public and unsecured for testing purposes. |
| 235 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 236 | if (statsCompanion != nullptr) { |
| 237 | if (DEBUG) ALOGD("Telling statsCompanion that statsd is ready"); |
| 238 | statsCompanion->statsdReady(); |
| 239 | } else { |
| 240 | if (DEBUG) ALOGD("Could not access statsCompanion"); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | Status |
| 245 | StatsService::statsCompanionReady() |
| 246 | { |
| 247 | if (DEBUG) ALOGD("StatsService::statsCompanionReady was called"); |
| 248 | |
| 249 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 250 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 251 | "Only system uid can call statsCompanionReady"); |
| 252 | } |
| 253 | |
| 254 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 255 | if (statsCompanion == nullptr) { |
| 256 | return Status::fromExceptionCode(Status::EX_NULL_POINTER, |
| 257 | "statscompanion unavailable despite it contacting statsd!"); |
| 258 | } |
| 259 | if (DEBUG) ALOGD("StatsService::statsCompanionReady linking to statsCompanion."); |
| 260 | IInterface::asBinder(statsCompanion)->linkToDeath(new StatsdDeathRecipient(mAnomalyMonitor)); |
| 261 | mAnomalyMonitor->setStatsCompanionService(statsCompanion); |
| 262 | |
| 263 | return Status::ok(); |
| 264 | } |
| 265 | |
| 266 | sp<IStatsCompanionService> |
| 267 | StatsService::getStatsCompanionService() { |
| 268 | sp<IStatsCompanionService> statsCompanion = nullptr; |
| 269 | // Get statscompanion service from service manager |
| 270 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 271 | if (sm != nullptr) { |
| 272 | const String16 name("statscompanion"); |
| 273 | statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name)); |
| 274 | if (statsCompanion == nullptr) { |
| 275 | ALOGW("statscompanion service unavailable!"); |
| 276 | return nullptr; |
| 277 | } |
| 278 | } |
| 279 | return statsCompanion; |
| 280 | } |
| 281 | |
| 282 | void |
| 283 | StatsdDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 284 | ALOGW("statscompanion service died"); |
| 285 | mAnmlyMntr->setStatsCompanionService(nullptr); |
| 286 | } |