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 |
| 165 | StatsService::systemRunning() |
| 166 | { |
| 167 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 168 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 169 | "Only system uid can call systemRunning"); |
| 170 | } |
| 171 | |
| 172 | // When system_server is up and running, schedule the dropbox task to run. |
| 173 | ALOGD("StatsService::systemRunning"); |
| 174 | |
| 175 | return Status::ok(); |
| 176 | } |
| 177 | |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 178 | status_t |
| 179 | StatsService::doPrintStatsLog(FILE* out, const Vector<String8>& args) { |
| 180 | long msec = 0; |
| 181 | |
| 182 | if (args.size() > 2) { |
| 183 | msec = strtol(args[2].string(), NULL, 10); |
| 184 | } |
| 185 | return DropboxReader::readStatsLogs(out, args[1].string(), msec); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | StatsService::printCmdHelp(FILE* out) { |
| 190 | fprintf(out, "Usage:\n"); |
| 191 | fprintf(out, "\t print-stats-log [tag_required] [timestamp_nsec_optional]\n"); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame^] | 192 | 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] | 193 | } |