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" |
| 18 | |
| 19 | #include "StatsService.h" |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 20 | #include "DropboxReader.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 21 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 22 | #include <android-base/file.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <cutils/log.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 | |
| 31 | #include <unistd.h> |
| 32 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 33 | #include <stdlib.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace android; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 36 | using android::os::statsd::StatsdConfig; |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 37 | |
| 38 | // ================================================================================ |
| 39 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
| 40 | { |
| 41 | ALOGD("stats service constructed"); |
| 42 | } |
| 43 | |
| 44 | StatsService::~StatsService() |
| 45 | { |
| 46 | } |
| 47 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 48 | status_t |
| 49 | StatsService::setProcessor(const sp<StatsLogProcessor>& main_processor) { |
| 50 | m_processor = main_processor; |
| 51 | ALOGD("stats service set to processor %p", m_processor.get()); |
| 52 | return NO_ERROR; |
| 53 | } |
| 54 | |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 55 | // Implement our own because the default binder implementation isn't |
| 56 | // properly handling SHELL_COMMAND_TRANSACTION |
| 57 | status_t |
| 58 | StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 59 | { |
| 60 | status_t err; |
| 61 | |
| 62 | switch (code) { |
| 63 | case SHELL_COMMAND_TRANSACTION: { |
| 64 | int in = data.readFileDescriptor(); |
| 65 | int out = data.readFileDescriptor(); |
| 66 | int err = data.readFileDescriptor(); |
| 67 | int argc = data.readInt32(); |
| 68 | Vector<String8> args; |
| 69 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 70 | args.add(String8(data.readString16())); |
| 71 | } |
| 72 | sp<IShellCallback> shellCallback = IShellCallback::asInterface( |
| 73 | data.readStrongBinder()); |
| 74 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( |
| 75 | data.readStrongBinder()); |
| 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 | } |
| 103 | default: { |
| 104 | return BnStatsManager::onTransact(code, data, reply, flags); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 109 | status_t |
| 110 | StatsService::dump(int fd, const Vector<String16>& args) |
| 111 | { |
| 112 | FILE* out = fdopen(fd, "w"); |
| 113 | if (out == NULL) { |
| 114 | return NO_MEMORY; // the fd is already open |
| 115 | } |
| 116 | |
| 117 | fprintf(out, "StatsService::dump:"); |
| 118 | ALOGD("StatsService::dump:"); |
| 119 | const int N = args.size(); |
| 120 | for (int i=0; i<N; i++) { |
| 121 | fprintf(out, " %s", String8(args[i]).string()); |
| 122 | ALOGD(" %s", String8(args[i]).string()); |
| 123 | } |
| 124 | fprintf(out, "\n"); |
| 125 | |
| 126 | fclose(out); |
| 127 | return NO_ERROR; |
| 128 | } |
| 129 | |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 130 | status_t |
| 131 | StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) |
| 132 | { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 133 | if (args.size() > 0) { |
| 134 | if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) { |
| 135 | return doPrintStatsLog(out, args); |
| 136 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 137 | if (!args[0].compare(String8("config"))) { |
| 138 | return doLoadConfig(in); |
| 139 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 140 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 141 | |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 142 | printCmdHelp(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 143 | return NO_ERROR; |
| 144 | } |
| 145 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 146 | status_t |
| 147 | StatsService::doLoadConfig(FILE* in) |
| 148 | { |
| 149 | string content; |
| 150 | if (!android::base::ReadFdToString(fileno(in), &content)) { |
| 151 | return UNKNOWN_ERROR; |
| 152 | } |
| 153 | StatsdConfig config; |
| 154 | if (config.ParseFromString(content)) { |
| 155 | ALOGD("Config parsed from command line: %s", config.SerializeAsString().c_str()); |
| 156 | m_processor->UpdateConfig(0, config); |
| 157 | return NO_ERROR; |
| 158 | } else { |
| 159 | ALOGD("Config failed to be parsed"); |
| 160 | return UNKNOWN_ERROR; |
| 161 | } |
| 162 | } |
| 163 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 164 | Status |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame^] | 165 | StatsService::informAnomalyAlarmFired() |
| 166 | { |
| 167 | ALOGD("StatsService::informAnomalyAlarmFired was called"); |
| 168 | |
| 169 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 170 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 171 | "Only system uid can call informAnomalyAlarmFired"); |
| 172 | } |
| 173 | |
| 174 | ALOGD("StatsService::informAnomalyAlarmFired succeeded"); |
| 175 | // TODO: check through all counters/timers and see if an anomaly has indeed occurred. |
| 176 | |
| 177 | return Status::ok(); |
| 178 | } |
| 179 | |
| 180 | Status |
| 181 | StatsService::informPollAlarmFired() |
| 182 | { |
| 183 | ALOGD("StatsService::informPollAlarmFired was called"); |
| 184 | |
| 185 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 186 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 187 | "Only system uid can call informPollAlarmFired"); |
| 188 | } |
| 189 | |
| 190 | ALOGD("StatsService::informPollAlarmFired succeeded"); |
| 191 | // TODO: determine what services to poll and poll (or ask StatsCompanionService to poll) them. |
| 192 | |
| 193 | return Status::ok(); |
| 194 | } |
| 195 | |
| 196 | Status |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 197 | StatsService::systemRunning() |
| 198 | { |
| 199 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 200 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 201 | "Only system uid can call systemRunning"); |
| 202 | } |
| 203 | |
| 204 | // When system_server is up and running, schedule the dropbox task to run. |
| 205 | ALOGD("StatsService::systemRunning"); |
| 206 | |
| 207 | return Status::ok(); |
| 208 | } |
| 209 | |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 210 | status_t |
| 211 | StatsService::doPrintStatsLog(FILE* out, const Vector<String8>& args) { |
| 212 | long msec = 0; |
| 213 | |
| 214 | if (args.size() > 2) { |
| 215 | msec = strtol(args[2].string(), NULL, 10); |
| 216 | } |
| 217 | return DropboxReader::readStatsLogs(out, args[1].string(), msec); |
| 218 | } |
| 219 | |
| 220 | void |
| 221 | StatsService::printCmdHelp(FILE* out) { |
| 222 | fprintf(out, "Usage:\n"); |
| 223 | fprintf(out, "\t print-stats-log [tag_required] [timestamp_nsec_optional]\n"); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 224 | 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] | 225 | } |