blob: 747a5714db6a5803c7dad240907a00a0c3305900 [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
yro0feae942017-11-15 14:38:48 -08002 * Copyright (C) 2017 The Android Open Source Project
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07003 *
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
Bookatzb487b552017-09-18 11:26:01 -070017#define DEBUG true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070019
yro87d983c2017-11-14 21:31:43 -080020#include "android-base/stringprintf.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070021#include "StatsService.h"
David Chenadaf8b32017-11-03 15:42:08 -070022#include "config/ConfigKey.h"
23#include "config/ConfigManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024#include "storage/DropboxReader.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070025
David Chen0656b7a2017-09-13 15:53:39 -070026#include <android-base/file.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070027#include <binder/IPCThreadState.h>
28#include <binder/IServiceManager.h>
yro87d983c2017-11-14 21:31:43 -080029#include <dirent.h>
David Chen0656b7a2017-09-13 15:53:39 -070030#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070031#include <private/android_filesystem_config.h>
32#include <utils/Looper.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070033#include <utils/String16.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070034#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070035#include <stdlib.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070036#include <sys/system_properties.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070037#include <unistd.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070038
39using namespace android;
40
Bookatz906a35c2017-09-20 15:26:44 -070041namespace android {
42namespace os {
43namespace statsd {
44
David Chenadaf8b32017-11-03 15:42:08 -070045constexpr const char* kPermissionDump = "android.permission.DUMP";
yro87d983c2017-11-14 21:31:43 -080046#define STATS_SERVICE_DIR "/data/system/stats-service"
David Chenadaf8b32017-11-03 15:42:08 -070047
Joe Onorato9fc9edf2017-10-15 20:08:52 -070048// ======================================================================
49/**
50 * Watches for the death of the stats companion (system process).
51 */
52class CompanionDeathRecipient : public IBinder::DeathRecipient {
53public:
54 CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor);
55 virtual void binderDied(const wp<IBinder>& who);
56
57private:
58 const sp<AnomalyMonitor> mAnomalyMonitor;
59};
60
61CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor)
62 : mAnomalyMonitor(anomalyMonitor) {
63}
64
65void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) {
66 ALOGW("statscompanion service died");
67 mAnomalyMonitor->setStatsCompanionService(nullptr);
68}
69
70// ======================================================================
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070071StatsService::StatsService(const sp<Looper>& handlerLooper)
David Chen1481fe12017-10-16 13:16:34 -070072 : mAnomalyMonitor(new AnomalyMonitor(2)) // TODO: Put this comment somewhere better
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070073{
Joe Onorato9fc9edf2017-10-15 20:08:52 -070074 mUidMap = new UidMap();
75 mConfigManager = new ConfigManager();
David Chenadaf8b32017-11-03 15:42:08 -070076 mProcessor = new StatsLogProcessor(mUidMap, [](const vector<uint8_t>& log) {
77 // TODO: Update how we send data out of StatsD.
yro31eb67b2017-10-24 13:33:21 -070078 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -070079
80 mConfigManager->AddListener(mProcessor);
81
82 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070083}
84
Yao Chenef99c4f2017-09-22 16:26:54 -070085StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070086}
87
Joe Onorato9fc9edf2017-10-15 20:08:52 -070088void StatsService::init_system_properties() {
89 mEngBuild = false;
90 const prop_info* buildType = __system_property_find("ro.build.type");
91 if (buildType != NULL) {
92 __system_property_read_callback(buildType, init_build_type_callback, this);
93 }
David Chen0656b7a2017-09-13 15:53:39 -070094}
95
Joe Onorato9fc9edf2017-10-15 20:08:52 -070096void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
97 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -070098 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070099 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
100 }
101}
102
103/**
104 * Implement our own because the default binder implementation isn't
105 * properly handling SHELL_COMMAND_TRANSACTION.
106 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700107status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
108 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700109 status_t err;
110
111 switch (code) {
112 case SHELL_COMMAND_TRANSACTION: {
113 int in = data.readFileDescriptor();
114 int out = data.readFileDescriptor();
115 int err = data.readFileDescriptor();
116 int argc = data.readInt32();
117 Vector<String8> args;
118 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
119 args.add(String8(data.readString16()));
120 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700121 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
122 sp<IResultReceiver> resultReceiver =
123 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700124
125 FILE* fin = fdopen(in, "r");
126 FILE* fout = fdopen(out, "w");
127 FILE* ferr = fdopen(err, "w");
128
129 if (fin == NULL || fout == NULL || ferr == NULL) {
130 resultReceiver->send(NO_MEMORY);
131 } else {
132 err = command(fin, fout, ferr, args);
133 resultReceiver->send(err);
134 }
135
136 if (fin != NULL) {
137 fflush(fin);
138 fclose(fin);
139 }
140 if (fout != NULL) {
141 fflush(fout);
142 fclose(fout);
143 }
144 if (fout != NULL) {
145 fflush(ferr);
146 fclose(ferr);
147 }
148
149 return NO_ERROR;
150 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700151 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700152 }
153}
154
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700155/**
156 * Write debugging data about statsd.
157 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700158status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700159 FILE* out = fdopen(fd, "w");
160 if (out == NULL) {
161 return NO_MEMORY; // the fd is already open
162 }
163
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700164 // TODO: Proto format for incident reports
165 dump_impl(out);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700166
167 fclose(out);
168 return NO_ERROR;
169}
170
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700171/**
172 * Write debugging data about statsd in text format.
173 */
174void StatsService::dump_impl(FILE* out) {
175 mConfigManager->Dump(out);
176}
177
178/**
179 * Implementation of the adb shell cmd stats command.
180 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700181status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700182 // TODO: Permission check
183
184 const int argCount = args.size();
185 if (argCount >= 1) {
186 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700187 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700188 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700189 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700190
191 // adb shell cmd stats print-stats-log
192 if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) {
193 return cmd_print_stats_log(out, args);
194 }
195
David Chende701692017-10-05 13:16:02 -0700196 if (!args[0].compare(String8("print-uid-map"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700197 return cmd_print_uid_map(out);
David Chende701692017-10-05 13:16:02 -0700198 }
Yao Chen729093d2017-10-16 10:33:26 -0700199
200 if (!args[0].compare(String8("dump-report"))) {
201 return cmd_dump_report(out, err, args);
202 }
David Chen1481fe12017-10-16 13:16:34 -0700203
204 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
205 return cmd_print_pulled_metrics(out, args);
206 }
David Chenadaf8b32017-11-03 15:42:08 -0700207
208 if (!args[0].compare(String8("send-broadcast"))) {
209 return cmd_trigger_broadcast(args);
210 }
yro87d983c2017-11-14 21:31:43 -0800211
212 if (!args[0].compare(String8("clear-config"))) {
213 return cmd_remove_config_files(out);
214 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700215 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700216
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700217 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700218 return NO_ERROR;
219}
220
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700221void StatsService::print_cmd_help(FILE* out) {
222 fprintf(out,
223 "usage: adb shell cmd stats print-stats-log [tag_required] "
224 "[timestamp_nsec_optional]\n");
225 fprintf(out, "\n");
226 fprintf(out, "\n");
227 fprintf(out, "usage: adb shell cmd stats print-uid-map \n");
228 fprintf(out, "\n");
229 fprintf(out, " Prints the UID, app name, version mapping.\n");
230 fprintf(out, "\n");
231 fprintf(out, "\n");
yro87d983c2017-11-14 21:31:43 -0800232 fprintf(out, "usage: adb shell cmd stats clear-config \n");
233 fprintf(out, "\n");
234 fprintf(out, " Removes all configs from disk.\n");
235 fprintf(out, "\n");
236 fprintf(out, "\n");
David Chen1481fe12017-10-16 13:16:34 -0700237 fprintf(out, "usage: adb shell cmds stats pull-source [int] \n");
238 fprintf(out, "\n");
239 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
240 fprintf(out, "\n");
241 fprintf(out, "\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700242 fprintf(out, "usage: adb shell cmd stats config remove [UID] NAME\n");
243 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
244 fprintf(out, "\n");
245 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
246 fprintf(out, " wire-encoded protobuf format and passed via stdin.\n");
247 fprintf(out, "\n");
248 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
249 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
250 fprintf(out, " uid is used.\n");
251 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700252 fprintf(out, "\n");
253 fprintf(out, "\n");
254 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME\n");
255 fprintf(out, " Dump all metric data for a configuration.\n");
256 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
257 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
258 fprintf(out, " calling uid is used.\n");
259 fprintf(out, " NAME The name of the configuration\n");
David Chenadaf8b32017-11-03 15:42:08 -0700260 fprintf(out, "\n");
261 fprintf(out, "\n");
262 fprintf(out, "usage: adb shell cmd stats send-broadcast PACKAGE CLASS\n");
263 fprintf(out, " Send a broadcast that triggers one subscriber to fetch metrics.\n");
264 fprintf(out, " PACKAGE The name of the package to receive the broadcast.\n");
265 fprintf(out, " CLASS The name of the class to receive the broadcast.\n");
266}
267
268status_t StatsService::cmd_trigger_broadcast(Vector<String8>& args) {
269 auto sc = getStatsCompanionService();
270 sc->sendBroadcast(String16(args[1]), String16(args[2]));
271 ALOGD("StatsService::trigger broadcast succeeded");
272 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700273}
274
275status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
276 const int argCount = args.size();
277 if (argCount >= 2) {
278 if (args[1] == "update" || args[1] == "remove") {
279 bool good = false;
280 int uid = -1;
281 string name;
282
283 if (argCount == 3) {
284 // Automatically pick the UID
285 uid = IPCThreadState::self()->getCallingUid();
286 // TODO: What if this isn't a binder call? Should we fail?
287 name.assign(args[2].c_str(), args[2].size());
288 good = true;
289 } else if (argCount == 4) {
290 // If it's a userdebug or eng build, then the shell user can
291 // impersonate other uids.
292 if (mEngBuild) {
293 const char* s = args[2].c_str();
294 if (*s != '\0') {
295 char* end = NULL;
296 uid = strtol(s, &end, 0);
297 if (*end == '\0') {
298 name.assign(args[3].c_str(), args[3].size());
299 good = true;
300 }
301 }
302 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700303 fprintf(err,
304 "The config can only be set for other UIDs on eng or userdebug "
305 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700306 }
307 }
308
309 if (!good) {
310 // If arg parsing failed, print the help text and return an error.
311 print_cmd_help(out);
312 return UNKNOWN_ERROR;
313 }
314
315 if (args[1] == "update") {
316 // Read stream into buffer.
317 string buffer;
318 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
319 fprintf(err, "Error reading stream for StatsConfig.\n");
320 return UNKNOWN_ERROR;
321 }
322
323 // Parse buffer.
324 StatsdConfig config;
325 if (!config.ParseFromString(buffer)) {
326 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
327 return UNKNOWN_ERROR;
328 }
329
330 // Add / update the config.
331 mConfigManager->UpdateConfig(ConfigKey(uid, name), config);
332 } else {
333 // Remove the config.
334 mConfigManager->RemoveConfig(ConfigKey(uid, name));
335 }
336
337 return NO_ERROR;
338 }
David Chen0656b7a2017-09-13 15:53:39 -0700339 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700340 print_cmd_help(out);
341 return UNKNOWN_ERROR;
342}
343
Yao Chen729093d2017-10-16 10:33:26 -0700344status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
345 if (mProcessor != nullptr) {
346 const int argCount = args.size();
347 bool good = false;
348 int uid;
349 string name;
350 if (argCount == 2) {
351 // Automatically pick the UID
352 uid = IPCThreadState::self()->getCallingUid();
353 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a3792017-10-30 22:57:06 -0700354 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700355 good = true;
356 } else if (argCount == 3) {
357 // If it's a userdebug or eng build, then the shell user can
358 // impersonate other uids.
359 if (mEngBuild) {
360 const char* s = args[1].c_str();
361 if (*s != '\0') {
362 char* end = NULL;
363 uid = strtol(s, &end, 0);
364 if (*end == '\0') {
365 name.assign(args[2].c_str(), args[2].size());
366 good = true;
367 }
368 }
369 } else {
370 fprintf(out,
371 "The metrics can only be dumped for other UIDs on eng or userdebug "
372 "builds.\n");
373 }
374 }
375 if (good) {
376 mProcessor->onDumpReport(ConfigKey(uid, name));
377 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
378 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
379 fprintf(out, "See the StatsLogReport in logcat...\n");
380 return android::OK;
381 } else {
382 // If arg parsing failed, print the help text and return an error.
383 print_cmd_help(out);
384 return UNKNOWN_ERROR;
385 }
386 } else {
387 fprintf(out, "Log processor does not exist...\n");
388 return UNKNOWN_ERROR;
389 }
390}
391
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700392status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) {
393 long msec = 0;
394
395 if (args.size() > 2) {
396 msec = strtol(args[2].string(), NULL, 10);
David Chen0656b7a2017-09-13 15:53:39 -0700397 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700398 return DropboxReader::readStatsLogs(out, args[1].string(), msec);
399}
400
401status_t StatsService::cmd_print_uid_map(FILE* out) {
402 mUidMap->printUidMap(out);
403 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700404}
405
David Chen1481fe12017-10-16 13:16:34 -0700406status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
407 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700408 vector<shared_ptr<LogEvent> > stats;
409 if (mStatsPullerManager.Pull(s, &stats)) {
410 for (const auto& it : stats) {
411 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
412 }
413 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
414 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700415 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700416 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700417}
418
yro87d983c2017-11-14 21:31:43 -0800419status_t StatsService::cmd_remove_config_files(FILE* out) {
420 fprintf(out, "Trying to remove config files...\n");
421 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
422 if (dir == NULL) {
423 fprintf(out, "No existing config files found exiting...\n");
424 return NO_ERROR;
425 }
426
427 dirent* de;
428 while ((de = readdir(dir.get()))) {
429 char* name = de->d_name;
430 if (name[0] == '.') continue;
431 string file_name = StringPrintf("%s/%s", STATS_SERVICE_DIR, name);
432 fprintf(out, "Deleting file %s\n", file_name.c_str());
433 if (remove(file_name.c_str())) {
434 fprintf(out, "Error deleting file %s\n", file_name.c_str());
435 }
436 }
437 return NO_ERROR;
438}
439
David Chende701692017-10-05 13:16:02 -0700440Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
441 const vector<String16>& app) {
442 if (DEBUG) ALOGD("StatsService::informAllUidData was called");
443
444 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
445 return Status::fromExceptionCode(Status::EX_SECURITY,
446 "Only system uid can call informAllUidData");
447 }
448
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700449 mUidMap->updateMap(uid, version, app);
David Chende701692017-10-05 13:16:02 -0700450 if (DEBUG) ALOGD("StatsService::informAllUidData succeeded");
451
452 return Status::ok();
453}
454
455Status StatsService::informOnePackage(const String16& app, int32_t uid, int32_t version) {
456 if (DEBUG) ALOGD("StatsService::informOnePackage was called");
457
458 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
459 return Status::fromExceptionCode(Status::EX_SECURITY,
460 "Only system uid can call informOnePackage");
461 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700462 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700463 return Status::ok();
464}
465
466Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
467 if (DEBUG) ALOGD("StatsService::informOnePackageRemoved was called");
468
469 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
470 return Status::fromExceptionCode(Status::EX_SECURITY,
471 "Only system uid can call informOnePackageRemoved");
472 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700473 mUidMap->removeApp(app, uid);
David Chende701692017-10-05 13:16:02 -0700474 return Status::ok();
475}
476
Yao Chenef99c4f2017-09-22 16:26:54 -0700477Status StatsService::informAnomalyAlarmFired() {
Bookatzb487b552017-09-18 11:26:01 -0700478 if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700479
480 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
481 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700482 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700483 }
484
Bookatzb487b552017-09-18 11:26:01 -0700485 if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700486 // TODO: check through all counters/timers and see if an anomaly has indeed occurred.
487
488 return Status::ok();
489}
490
Yao Chenef99c4f2017-09-22 16:26:54 -0700491Status StatsService::informPollAlarmFired() {
Bookatzb487b552017-09-18 11:26:01 -0700492 if (DEBUG) ALOGD("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700493
494 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
495 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700496 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700497 }
498
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700499 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700500
Bookatzb487b552017-09-18 11:26:01 -0700501 if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700502
503 return Status::ok();
504}
505
Yao Chenef99c4f2017-09-22 16:26:54 -0700506Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700507 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
508 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700509 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700510 }
511
512 // When system_server is up and running, schedule the dropbox task to run.
513 ALOGD("StatsService::systemRunning");
514
Bookatzb487b552017-09-18 11:26:01 -0700515 sayHiToStatsCompanion();
516
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700517 return Status::ok();
518}
519
Yao Chenef99c4f2017-09-22 16:26:54 -0700520void StatsService::sayHiToStatsCompanion() {
521 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
522 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700523 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
524 if (statsCompanion != nullptr) {
525 if (DEBUG) ALOGD("Telling statsCompanion that statsd is ready");
526 statsCompanion->statsdReady();
527 } else {
528 if (DEBUG) ALOGD("Could not access statsCompanion");
529 }
530}
531
Yao Chenef99c4f2017-09-22 16:26:54 -0700532sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700533 sp<IStatsCompanionService> statsCompanion = nullptr;
534 // Get statscompanion service from service manager
535 const sp<IServiceManager> sm(defaultServiceManager());
536 if (sm != nullptr) {
537 const String16 name("statscompanion");
538 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
539 if (statsCompanion == nullptr) {
540 ALOGW("statscompanion service unavailable!");
541 return nullptr;
542 }
543 }
544 return statsCompanion;
545}
546
Yao Chenef99c4f2017-09-22 16:26:54 -0700547Status StatsService::statsCompanionReady() {
Bookatzb487b552017-09-18 11:26:01 -0700548 if (DEBUG) ALOGD("StatsService::statsCompanionReady was called");
549
550 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
551 return Status::fromExceptionCode(Status::EX_SECURITY,
552 "Only system uid can call statsCompanionReady");
553 }
554
555 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
556 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700557 return Status::fromExceptionCode(
558 Status::EX_NULL_POINTER,
559 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700560 }
561 if (DEBUG) ALOGD("StatsService::statsCompanionReady linking to statsCompanion.");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700562 IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor));
Bookatzb487b552017-09-18 11:26:01 -0700563 mAnomalyMonitor->setStatsCompanionService(statsCompanion);
564
565 return Status::ok();
566}
567
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700568void StatsService::Startup() {
569 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700570}
571
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700572void StatsService::OnLogEvent(const LogEvent& event) {
573 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700574}
575
David Chenadaf8b32017-11-03 15:42:08 -0700576Status StatsService::getData(const String16& key, vector<uint8_t>* output) {
577 IPCThreadState* ipc = IPCThreadState::self();
578 if (checkCallingPermission(String16(kPermissionDump),
579 reinterpret_cast<int32_t*>(ipc->getCallingPid()),
580 reinterpret_cast<int32_t*>(ipc->getCallingUid()))) {
581 // TODO: Implement this.
582 return Status::ok();
583 } else {
584 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
585 }
yro31eb67b2017-10-24 13:33:21 -0700586}
587
David Chenadaf8b32017-11-03 15:42:08 -0700588Status StatsService::addConfiguration(const String16& key,
589 const vector <uint8_t>& config,
590 const String16& package, const String16& cls,
591 bool* success) {
592 IPCThreadState* ipc = IPCThreadState::self();
593 int32_t* uid = reinterpret_cast<int32_t*>(ipc->getCallingUid());
594 if (checkCallingPermission(String16(kPermissionDump),
595 reinterpret_cast<int32_t*>(ipc->getCallingPid()), uid)) {
596 string keyString = string(String8(key).string());
597 ConfigKey configKey(*uid, keyString);
598 StatsdConfig cfg;
599 cfg.ParseFromArray(&config[0], config.size());
600 mConfigManager->UpdateConfig(configKey, cfg);
601 mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()),
602 string(String8(cls).string()));
603 *success = true;
604 return Status::ok();
605 } else {
606 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700607 }
yro31eb67b2017-10-24 13:33:21 -0700608}
609
David Chenadaf8b32017-11-03 15:42:08 -0700610Status StatsService::removeConfiguration(const String16& key, bool* success) {
611 IPCThreadState* ipc = IPCThreadState::self();
612 if (checkCallingPermission(String16(kPermissionDump),
613 reinterpret_cast<int32_t*>(ipc->getCallingPid()),
614 reinterpret_cast<int32_t*>(ipc->getCallingUid()))) {
615 // TODO: Implement this.
616 return Status::ok();
617 } else {
618 *success = false;
619 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700620 }
yro31eb67b2017-10-24 13:33:21 -0700621}
622
623void StatsService::binderDied(const wp<IBinder>& who) {
624 for (size_t i = 0; i < mCallbacks.size(); i++) {
625 if (IInterface::asBinder(mCallbacks[i]) == who) {
626 mCallbacks.removeAt(i);
627 break;
628 }
629 }
630}
631
Yao Chenef99c4f2017-09-22 16:26:54 -0700632} // namespace statsd
633} // namespace os
634} // namespace android