blob: ef01ec7563f5cc4d763e251bd837eb266f5561ec [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 Chen1d7b0cd2017-11-15 14:20:04 -080076 mProcessor = new StatsLogProcessor(mUidMap, [this](const ConfigKey& key) {
77 auto sc = getStatsCompanionService();
78 auto receiver = mConfigManager->GetConfigReceiver(key);
79 if (sc == nullptr) {
80 ALOGD("Could not find StatsCompanionService");
81 } else if (receiver.first.size() == 0) {
82 ALOGD("Statscompanion could not find a broadcast receiver for %s",
83 key.ToString().c_str());
84 } else {
85 sc->sendBroadcast(String16(receiver.first.c_str()),
86 String16(receiver.second.c_str()));
87 }
yro31eb67b2017-10-24 13:33:21 -070088 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -070089
90 mConfigManager->AddListener(mProcessor);
91
92 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070093}
94
Yao Chenef99c4f2017-09-22 16:26:54 -070095StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070096}
97
Joe Onorato9fc9edf2017-10-15 20:08:52 -070098void StatsService::init_system_properties() {
99 mEngBuild = false;
100 const prop_info* buildType = __system_property_find("ro.build.type");
101 if (buildType != NULL) {
102 __system_property_read_callback(buildType, init_build_type_callback, this);
103 }
David Chen0656b7a2017-09-13 15:53:39 -0700104}
105
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700106void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
107 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700108 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700109 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
110 }
111}
112
113/**
114 * Implement our own because the default binder implementation isn't
115 * properly handling SHELL_COMMAND_TRANSACTION.
116 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700117status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
118 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700119 status_t err;
120
121 switch (code) {
122 case SHELL_COMMAND_TRANSACTION: {
123 int in = data.readFileDescriptor();
124 int out = data.readFileDescriptor();
125 int err = data.readFileDescriptor();
126 int argc = data.readInt32();
127 Vector<String8> args;
128 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
129 args.add(String8(data.readString16()));
130 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700131 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
132 sp<IResultReceiver> resultReceiver =
133 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700134
135 FILE* fin = fdopen(in, "r");
136 FILE* fout = fdopen(out, "w");
137 FILE* ferr = fdopen(err, "w");
138
139 if (fin == NULL || fout == NULL || ferr == NULL) {
140 resultReceiver->send(NO_MEMORY);
141 } else {
142 err = command(fin, fout, ferr, args);
143 resultReceiver->send(err);
144 }
145
146 if (fin != NULL) {
147 fflush(fin);
148 fclose(fin);
149 }
150 if (fout != NULL) {
151 fflush(fout);
152 fclose(fout);
153 }
154 if (fout != NULL) {
155 fflush(ferr);
156 fclose(ferr);
157 }
158
159 return NO_ERROR;
160 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700161 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700162 }
163}
164
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700165/**
166 * Write debugging data about statsd.
167 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700168status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700169 FILE* out = fdopen(fd, "w");
170 if (out == NULL) {
171 return NO_MEMORY; // the fd is already open
172 }
173
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700174 // TODO: Proto format for incident reports
175 dump_impl(out);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700176
177 fclose(out);
178 return NO_ERROR;
179}
180
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700181/**
182 * Write debugging data about statsd in text format.
183 */
184void StatsService::dump_impl(FILE* out) {
185 mConfigManager->Dump(out);
186}
187
188/**
189 * Implementation of the adb shell cmd stats command.
190 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700191status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700192 // TODO: Permission check
193
194 const int argCount = args.size();
195 if (argCount >= 1) {
196 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700197 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700198 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700199 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700200
201 // adb shell cmd stats print-stats-log
202 if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) {
203 return cmd_print_stats_log(out, args);
204 }
205
David Chende701692017-10-05 13:16:02 -0700206 if (!args[0].compare(String8("print-uid-map"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700207 return cmd_print_uid_map(out);
David Chende701692017-10-05 13:16:02 -0700208 }
Yao Chen729093d2017-10-16 10:33:26 -0700209
210 if (!args[0].compare(String8("dump-report"))) {
211 return cmd_dump_report(out, err, args);
212 }
David Chen1481fe12017-10-16 13:16:34 -0700213
214 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
215 return cmd_print_pulled_metrics(out, args);
216 }
David Chenadaf8b32017-11-03 15:42:08 -0700217
218 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800219 return cmd_trigger_broadcast(out, args);
220 }
221
222 if (!args[0].compare(String8("print-stats"))) {
223 return cmd_print_stats(out);
David Chenadaf8b32017-11-03 15:42:08 -0700224 }
yro87d983c2017-11-14 21:31:43 -0800225
226 if (!args[0].compare(String8("clear-config"))) {
227 return cmd_remove_config_files(out);
228 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700229 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700230
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700231 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700232 return NO_ERROR;
233}
234
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700235void StatsService::print_cmd_help(FILE* out) {
236 fprintf(out,
237 "usage: adb shell cmd stats print-stats-log [tag_required] "
238 "[timestamp_nsec_optional]\n");
239 fprintf(out, "\n");
240 fprintf(out, "\n");
241 fprintf(out, "usage: adb shell cmd stats print-uid-map \n");
242 fprintf(out, "\n");
243 fprintf(out, " Prints the UID, app name, version mapping.\n");
244 fprintf(out, "\n");
245 fprintf(out, "\n");
yro87d983c2017-11-14 21:31:43 -0800246 fprintf(out, "usage: adb shell cmd stats clear-config \n");
247 fprintf(out, "\n");
248 fprintf(out, " Removes all configs from disk.\n");
249 fprintf(out, "\n");
250 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800251 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700252 fprintf(out, "\n");
253 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
254 fprintf(out, "\n");
255 fprintf(out, "\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700256 fprintf(out, "usage: adb shell cmd stats config remove [UID] NAME\n");
257 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
258 fprintf(out, "\n");
259 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
260 fprintf(out, " wire-encoded protobuf format and passed via stdin.\n");
261 fprintf(out, "\n");
262 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
263 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
264 fprintf(out, " uid is used.\n");
265 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a372017-10-30 22:57:06 -0700266 fprintf(out, "\n");
267 fprintf(out, "\n");
268 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME\n");
269 fprintf(out, " Dump all metric data for a configuration.\n");
270 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
271 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
272 fprintf(out, " calling uid is used.\n");
273 fprintf(out, " NAME The name of the configuration\n");
David Chenadaf8b32017-11-03 15:42:08 -0700274 fprintf(out, "\n");
275 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800276 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
277 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
278 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
279 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
280 fprintf(out, " calling uid is used.\n");
281 fprintf(out, " NAME The name of the configuration\n");
282 fprintf(out, "\n");
283 fprintf(out, "\n");
284 fprintf(out, "usage: adb shell cmd stats print-stats\n");
285 fprintf(out, " Prints some basic stats.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700286}
287
David Chen1d7b0cd2017-11-15 14:20:04 -0800288status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
289 string name;
290 bool good = false;
291 int uid;
292 const int argCount = args.size();
293 if (argCount == 2) {
294 // Automatically pick the UID
295 uid = IPCThreadState::self()->getCallingUid();
296 // TODO: What if this isn't a binder call? Should we fail?
297 name.assign(args[1].c_str(), args[1].size());
298 good = true;
299 } else if (argCount == 3) {
300 // If it's a userdebug or eng build, then the shell user can
301 // impersonate other uids.
302 if (mEngBuild) {
303 const char* s = args[1].c_str();
304 if (*s != '\0') {
305 char* end = NULL;
306 uid = strtol(s, &end, 0);
307 if (*end == '\0') {
308 name.assign(args[2].c_str(), args[2].size());
309 good = true;
310 }
311 }
312 } else {
313 fprintf(out,
314 "The metrics can only be dumped for other UIDs on eng or userdebug "
315 "builds.\n");
316 }
317 }
318 if (!good) {
319 print_cmd_help(out);
320 return UNKNOWN_ERROR;
321 }
322 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, name));
yro4d889e62017-11-17 15:44:48 -0800323 sp<IStatsCompanionService> sc = getStatsCompanionService();
324 if (sc != nullptr) {
325 sc->sendBroadcast(String16(receiver.first.c_str()), String16(receiver.second.c_str()));
326 ALOGD("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
327 args[2].c_str());
328 } else {
329 ALOGD("Could not access statsCompanion");
330 }
331
David Chenadaf8b32017-11-03 15:42:08 -0700332 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700333}
334
335status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
336 const int argCount = args.size();
337 if (argCount >= 2) {
338 if (args[1] == "update" || args[1] == "remove") {
339 bool good = false;
340 int uid = -1;
341 string name;
342
343 if (argCount == 3) {
344 // Automatically pick the UID
345 uid = IPCThreadState::self()->getCallingUid();
346 // TODO: What if this isn't a binder call? Should we fail?
347 name.assign(args[2].c_str(), args[2].size());
348 good = true;
349 } else if (argCount == 4) {
350 // If it's a userdebug or eng build, then the shell user can
351 // impersonate other uids.
352 if (mEngBuild) {
353 const char* s = args[2].c_str();
354 if (*s != '\0') {
355 char* end = NULL;
356 uid = strtol(s, &end, 0);
357 if (*end == '\0') {
358 name.assign(args[3].c_str(), args[3].size());
359 good = true;
360 }
361 }
362 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700363 fprintf(err,
364 "The config can only be set for other UIDs on eng or userdebug "
365 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700366 }
367 }
368
369 if (!good) {
370 // If arg parsing failed, print the help text and return an error.
371 print_cmd_help(out);
372 return UNKNOWN_ERROR;
373 }
374
375 if (args[1] == "update") {
376 // Read stream into buffer.
377 string buffer;
378 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
379 fprintf(err, "Error reading stream for StatsConfig.\n");
380 return UNKNOWN_ERROR;
381 }
382
383 // Parse buffer.
384 StatsdConfig config;
385 if (!config.ParseFromString(buffer)) {
386 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
387 return UNKNOWN_ERROR;
388 }
389
390 // Add / update the config.
391 mConfigManager->UpdateConfig(ConfigKey(uid, name), config);
392 } else {
393 // Remove the config.
394 mConfigManager->RemoveConfig(ConfigKey(uid, name));
395 }
396
397 return NO_ERROR;
398 }
David Chen0656b7a2017-09-13 15:53:39 -0700399 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700400 print_cmd_help(out);
401 return UNKNOWN_ERROR;
402}
403
Yao Chen729093d2017-10-16 10:33:26 -0700404status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
405 if (mProcessor != nullptr) {
406 const int argCount = args.size();
407 bool good = false;
408 int uid;
409 string name;
410 if (argCount == 2) {
411 // Automatically pick the UID
412 uid = IPCThreadState::self()->getCallingUid();
413 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a372017-10-30 22:57:06 -0700414 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700415 good = true;
416 } else if (argCount == 3) {
417 // If it's a userdebug or eng build, then the shell user can
418 // impersonate other uids.
419 if (mEngBuild) {
420 const char* s = args[1].c_str();
421 if (*s != '\0') {
422 char* end = NULL;
423 uid = strtol(s, &end, 0);
424 if (*end == '\0') {
425 name.assign(args[2].c_str(), args[2].size());
426 good = true;
427 }
428 }
429 } else {
430 fprintf(out,
431 "The metrics can only be dumped for other UIDs on eng or userdebug "
432 "builds.\n");
433 }
434 }
435 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800436 vector<uint8_t> data;
437 mProcessor->onDumpReport(ConfigKey(uid, name), &data);
Yao Chen729093d2017-10-16 10:33:26 -0700438 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
439 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
440 fprintf(out, "See the StatsLogReport in logcat...\n");
441 return android::OK;
442 } else {
443 // If arg parsing failed, print the help text and return an error.
444 print_cmd_help(out);
445 return UNKNOWN_ERROR;
446 }
447 } else {
448 fprintf(out, "Log processor does not exist...\n");
449 return UNKNOWN_ERROR;
450 }
451}
452
David Chen1d7b0cd2017-11-15 14:20:04 -0800453status_t StatsService::cmd_print_stats(FILE* out) {
454 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
455 for (const ConfigKey& key : configs) {
456 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
457 mProcessor->GetMetricsSize(key));
458 }
459 return NO_ERROR;
460}
461
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700462status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) {
463 long msec = 0;
464
465 if (args.size() > 2) {
466 msec = strtol(args[2].string(), NULL, 10);
David Chen0656b7a2017-09-13 15:53:39 -0700467 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700468 return DropboxReader::readStatsLogs(out, args[1].string(), msec);
469}
470
471status_t StatsService::cmd_print_uid_map(FILE* out) {
472 mUidMap->printUidMap(out);
473 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700474}
475
David Chen1481fe12017-10-16 13:16:34 -0700476status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
477 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700478 vector<shared_ptr<LogEvent> > stats;
479 if (mStatsPullerManager.Pull(s, &stats)) {
480 for (const auto& it : stats) {
481 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
482 }
483 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
484 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700485 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700486 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700487}
488
yro87d983c2017-11-14 21:31:43 -0800489status_t StatsService::cmd_remove_config_files(FILE* out) {
490 fprintf(out, "Trying to remove config files...\n");
491 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
492 if (dir == NULL) {
493 fprintf(out, "No existing config files found exiting...\n");
494 return NO_ERROR;
495 }
496
497 dirent* de;
498 while ((de = readdir(dir.get()))) {
499 char* name = de->d_name;
500 if (name[0] == '.') continue;
501 string file_name = StringPrintf("%s/%s", STATS_SERVICE_DIR, name);
502 fprintf(out, "Deleting file %s\n", file_name.c_str());
503 if (remove(file_name.c_str())) {
504 fprintf(out, "Error deleting file %s\n", file_name.c_str());
505 }
506 }
507 return NO_ERROR;
508}
509
David Chende701692017-10-05 13:16:02 -0700510Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
511 const vector<String16>& app) {
512 if (DEBUG) ALOGD("StatsService::informAllUidData was called");
513
514 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
515 return Status::fromExceptionCode(Status::EX_SECURITY,
516 "Only system uid can call informAllUidData");
517 }
518
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700519 mUidMap->updateMap(uid, version, app);
David Chende701692017-10-05 13:16:02 -0700520 if (DEBUG) ALOGD("StatsService::informAllUidData succeeded");
521
522 return Status::ok();
523}
524
525Status StatsService::informOnePackage(const String16& app, int32_t uid, int32_t version) {
526 if (DEBUG) ALOGD("StatsService::informOnePackage was called");
527
528 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
529 return Status::fromExceptionCode(Status::EX_SECURITY,
530 "Only system uid can call informOnePackage");
531 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700532 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700533 return Status::ok();
534}
535
536Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
537 if (DEBUG) ALOGD("StatsService::informOnePackageRemoved was called");
538
539 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
540 return Status::fromExceptionCode(Status::EX_SECURITY,
541 "Only system uid can call informOnePackageRemoved");
542 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700543 mUidMap->removeApp(app, uid);
David Chende701692017-10-05 13:16:02 -0700544 return Status::ok();
545}
546
Yao Chenef99c4f2017-09-22 16:26:54 -0700547Status StatsService::informAnomalyAlarmFired() {
Bookatzb487b552017-09-18 11:26:01 -0700548 if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700549
550 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
551 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700552 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700553 }
554
Bookatzb487b552017-09-18 11:26:01 -0700555 if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700556 // TODO: check through all counters/timers and see if an anomaly has indeed occurred.
557
558 return Status::ok();
559}
560
Yao Chenef99c4f2017-09-22 16:26:54 -0700561Status StatsService::informPollAlarmFired() {
Bookatzb487b552017-09-18 11:26:01 -0700562 if (DEBUG) ALOGD("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700563
564 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
565 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700566 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700567 }
568
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700569 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700570
Bookatzb487b552017-09-18 11:26:01 -0700571 if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700572
573 return Status::ok();
574}
575
Yao Chenef99c4f2017-09-22 16:26:54 -0700576Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700577 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
578 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700579 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700580 }
581
582 // When system_server is up and running, schedule the dropbox task to run.
583 ALOGD("StatsService::systemRunning");
584
Bookatzb487b552017-09-18 11:26:01 -0700585 sayHiToStatsCompanion();
586
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700587 return Status::ok();
588}
589
Yao Chenef99c4f2017-09-22 16:26:54 -0700590void StatsService::sayHiToStatsCompanion() {
591 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
592 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700593 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
594 if (statsCompanion != nullptr) {
595 if (DEBUG) ALOGD("Telling statsCompanion that statsd is ready");
596 statsCompanion->statsdReady();
597 } else {
598 if (DEBUG) ALOGD("Could not access statsCompanion");
599 }
600}
601
Yao Chenef99c4f2017-09-22 16:26:54 -0700602sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700603 sp<IStatsCompanionService> statsCompanion = nullptr;
604 // Get statscompanion service from service manager
605 const sp<IServiceManager> sm(defaultServiceManager());
606 if (sm != nullptr) {
607 const String16 name("statscompanion");
608 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
609 if (statsCompanion == nullptr) {
610 ALOGW("statscompanion service unavailable!");
611 return nullptr;
612 }
613 }
614 return statsCompanion;
615}
616
Yao Chenef99c4f2017-09-22 16:26:54 -0700617Status StatsService::statsCompanionReady() {
Bookatzb487b552017-09-18 11:26:01 -0700618 if (DEBUG) ALOGD("StatsService::statsCompanionReady was called");
619
620 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
621 return Status::fromExceptionCode(Status::EX_SECURITY,
622 "Only system uid can call statsCompanionReady");
623 }
624
625 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
626 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700627 return Status::fromExceptionCode(
628 Status::EX_NULL_POINTER,
629 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700630 }
631 if (DEBUG) ALOGD("StatsService::statsCompanionReady linking to statsCompanion.");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700632 IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor));
Bookatzb487b552017-09-18 11:26:01 -0700633 mAnomalyMonitor->setStatsCompanionService(statsCompanion);
634
635 return Status::ok();
636}
637
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700638void StatsService::Startup() {
639 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700640}
641
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700642void StatsService::OnLogEvent(const LogEvent& event) {
643 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700644}
645
David Chen1d7b0cd2017-11-15 14:20:04 -0800646Status StatsService::getData(const String16& key, vector <uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700647 IPCThreadState* ipc = IPCThreadState::self();
David Chen1d7b0cd2017-11-15 14:20:04 -0800648 ALOGD("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(),
649 ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800650 if (checkCallingPermission(String16(kPermissionDump))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800651 string keyStr = string(String8(key).string());
652 ConfigKey configKey(ipc->getCallingUid(), keyStr);
653 mProcessor->onDumpReport(configKey, output);
David Chenadaf8b32017-11-03 15:42:08 -0700654 return Status::ok();
655 } else {
656 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
657 }
yro31eb67b2017-10-24 13:33:21 -0700658}
659
David Chenadaf8b32017-11-03 15:42:08 -0700660Status StatsService::addConfiguration(const String16& key,
661 const vector <uint8_t>& config,
662 const String16& package, const String16& cls,
663 bool* success) {
664 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800665 if (checkCallingPermission(String16(kPermissionDump))) {
David Chenadaf8b32017-11-03 15:42:08 -0700666 string keyString = string(String8(key).string());
David Chen1d7b0cd2017-11-15 14:20:04 -0800667 ConfigKey configKey(ipc->getCallingUid(), keyString);
David Chenadaf8b32017-11-03 15:42:08 -0700668 StatsdConfig cfg;
669 cfg.ParseFromArray(&config[0], config.size());
670 mConfigManager->UpdateConfig(configKey, cfg);
671 mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()),
672 string(String8(cls).string()));
673 *success = true;
674 return Status::ok();
675 } else {
676 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700677 }
yro31eb67b2017-10-24 13:33:21 -0700678}
679
David Chenadaf8b32017-11-03 15:42:08 -0700680Status StatsService::removeConfiguration(const String16& key, bool* success) {
681 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800682 if (checkCallingPermission(String16(kPermissionDump))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800683 string keyStr = string(String8(key).string());
684 mConfigManager->RemoveConfig(ConfigKey(ipc->getCallingUid(), keyStr));
David Chenadaf8b32017-11-03 15:42:08 -0700685 return Status::ok();
686 } else {
687 *success = false;
688 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700689 }
yro31eb67b2017-10-24 13:33:21 -0700690}
691
David Chen1d7b0cd2017-11-15 14:20:04 -0800692void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700693 for (size_t i = 0; i < mCallbacks.size(); i++) {
694 if (IInterface::asBinder(mCallbacks[i]) == who) {
695 mCallbacks.removeAt(i);
696 break;
697 }
698 }
699}
700
Yao Chenef99c4f2017-09-22 16:26:54 -0700701} // namespace statsd
702} // namespace os
703} // namespace android