blob: 67f6782089cefbf5e33a82f485fcac0eb2711606 [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
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 Chen482d2722017-09-12 13:25:43 -070020#include "DropboxReader.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070021
David Chen0656b7a2017-09-13 15:53:39 -070022#include <android-base/file.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070023#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
25#include <cutils/log.h>
David Chen0656b7a2017-09-13 15:53:39 -070026#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070027#include <private/android_filesystem_config.h>
28#include <utils/Looper.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070029#include <utils/String16.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070030
31#include <unistd.h>
32#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070033#include <stdlib.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070034
35using namespace android;
David Chen0656b7a2017-09-13 15:53:39 -070036using android::os::statsd::StatsdConfig;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070037
38// ================================================================================
39StatsService::StatsService(const sp<Looper>& handlerLooper)
40{
41 ALOGD("stats service constructed");
42}
43
44StatsService::~StatsService()
45{
46}
47
David Chen0656b7a2017-09-13 15:53:39 -070048status_t
49StatsService::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 Onorato2cbc2cc2017-08-30 17:03:23 -070055// Implement our own because the default binder implementation isn't
56// properly handling SHELL_COMMAND_TRANSACTION
57status_t
58StatsService::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 Onorato5dcbc6c2017-08-29 15:13:58 -0700109status_t
110StatsService::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 Onorato2cbc2cc2017-08-30 17:03:23 -0700130status_t
131StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args)
132{
Yao Chen482d2722017-09-12 13:25:43 -0700133 if (args.size() > 0) {
134 if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) {
135 return doPrintStatsLog(out, args);
136 }
David Chen0656b7a2017-09-13 15:53:39 -0700137 if (!args[0].compare(String8("config"))) {
138 return doLoadConfig(in);
139 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700140 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700141
Yao Chen482d2722017-09-12 13:25:43 -0700142 printCmdHelp(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700143 return NO_ERROR;
144}
145
David Chen0656b7a2017-09-13 15:53:39 -0700146status_t
147StatsService::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 Onorato5dcbc6c2017-08-29 15:13:58 -0700164Status
Bookatz1b0b1142017-09-08 11:58:42 -0700165StatsService::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
180Status
181StatsService::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
196Status
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700197StatsService::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 Chen482d2722017-09-12 13:25:43 -0700210status_t
211StatsService::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
220void
221StatsService::printCmdHelp(FILE* out) {
222 fprintf(out, "Usage:\n");
223 fprintf(out, "\t print-stats-log [tag_required] [timestamp_nsec_optional]\n");
David Chen0656b7a2017-09-13 15:53:39 -0700224 fprintf(out, "\t config\t Loads a new config from command-line (must be proto in wire-encoded format).\n");
Yao Chen482d2722017-09-12 13:25:43 -0700225}