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