blob: 10952a97b1639e91af4578722a14c8f49d1bea43 [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 Chen5154a3792017-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));
David Chenadaf8b32017-11-03 15:42:08 -0700323 auto sc = getStatsCompanionService();
David Chen1d7b0cd2017-11-15 14:20:04 -0800324 sc->sendBroadcast(String16(receiver.first.c_str()), String16(receiver.second.c_str()));
325 ALOGD("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), args[2].c_str());
David Chenadaf8b32017-11-03 15:42:08 -0700326 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700327}
328
329status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
330 const int argCount = args.size();
331 if (argCount >= 2) {
332 if (args[1] == "update" || args[1] == "remove") {
333 bool good = false;
334 int uid = -1;
335 string name;
336
337 if (argCount == 3) {
338 // Automatically pick the UID
339 uid = IPCThreadState::self()->getCallingUid();
340 // TODO: What if this isn't a binder call? Should we fail?
341 name.assign(args[2].c_str(), args[2].size());
342 good = true;
343 } else if (argCount == 4) {
344 // If it's a userdebug or eng build, then the shell user can
345 // impersonate other uids.
346 if (mEngBuild) {
347 const char* s = args[2].c_str();
348 if (*s != '\0') {
349 char* end = NULL;
350 uid = strtol(s, &end, 0);
351 if (*end == '\0') {
352 name.assign(args[3].c_str(), args[3].size());
353 good = true;
354 }
355 }
356 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700357 fprintf(err,
358 "The config can only be set for other UIDs on eng or userdebug "
359 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700360 }
361 }
362
363 if (!good) {
364 // If arg parsing failed, print the help text and return an error.
365 print_cmd_help(out);
366 return UNKNOWN_ERROR;
367 }
368
369 if (args[1] == "update") {
370 // Read stream into buffer.
371 string buffer;
372 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
373 fprintf(err, "Error reading stream for StatsConfig.\n");
374 return UNKNOWN_ERROR;
375 }
376
377 // Parse buffer.
378 StatsdConfig config;
379 if (!config.ParseFromString(buffer)) {
380 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
381 return UNKNOWN_ERROR;
382 }
383
384 // Add / update the config.
385 mConfigManager->UpdateConfig(ConfigKey(uid, name), config);
386 } else {
387 // Remove the config.
388 mConfigManager->RemoveConfig(ConfigKey(uid, name));
389 }
390
391 return NO_ERROR;
392 }
David Chen0656b7a2017-09-13 15:53:39 -0700393 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700394 print_cmd_help(out);
395 return UNKNOWN_ERROR;
396}
397
Yao Chen729093d2017-10-16 10:33:26 -0700398status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
399 if (mProcessor != nullptr) {
400 const int argCount = args.size();
401 bool good = false;
402 int uid;
403 string name;
404 if (argCount == 2) {
405 // Automatically pick the UID
406 uid = IPCThreadState::self()->getCallingUid();
407 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a3792017-10-30 22:57:06 -0700408 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700409 good = true;
410 } else if (argCount == 3) {
411 // If it's a userdebug or eng build, then the shell user can
412 // impersonate other uids.
413 if (mEngBuild) {
414 const char* s = args[1].c_str();
415 if (*s != '\0') {
416 char* end = NULL;
417 uid = strtol(s, &end, 0);
418 if (*end == '\0') {
419 name.assign(args[2].c_str(), args[2].size());
420 good = true;
421 }
422 }
423 } else {
424 fprintf(out,
425 "The metrics can only be dumped for other UIDs on eng or userdebug "
426 "builds.\n");
427 }
428 }
429 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800430 vector<uint8_t> data;
431 mProcessor->onDumpReport(ConfigKey(uid, name), &data);
Yao Chen729093d2017-10-16 10:33:26 -0700432 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
433 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
434 fprintf(out, "See the StatsLogReport in logcat...\n");
435 return android::OK;
436 } else {
437 // If arg parsing failed, print the help text and return an error.
438 print_cmd_help(out);
439 return UNKNOWN_ERROR;
440 }
441 } else {
442 fprintf(out, "Log processor does not exist...\n");
443 return UNKNOWN_ERROR;
444 }
445}
446
David Chen1d7b0cd2017-11-15 14:20:04 -0800447status_t StatsService::cmd_print_stats(FILE* out) {
448 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
449 for (const ConfigKey& key : configs) {
450 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
451 mProcessor->GetMetricsSize(key));
452 }
453 return NO_ERROR;
454}
455
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700456status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) {
457 long msec = 0;
458
459 if (args.size() > 2) {
460 msec = strtol(args[2].string(), NULL, 10);
David Chen0656b7a2017-09-13 15:53:39 -0700461 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700462 return DropboxReader::readStatsLogs(out, args[1].string(), msec);
463}
464
465status_t StatsService::cmd_print_uid_map(FILE* out) {
466 mUidMap->printUidMap(out);
467 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700468}
469
David Chen1481fe12017-10-16 13:16:34 -0700470status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
471 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700472 vector<shared_ptr<LogEvent> > stats;
473 if (mStatsPullerManager.Pull(s, &stats)) {
474 for (const auto& it : stats) {
475 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
476 }
477 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
478 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700479 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700480 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700481}
482
yro87d983c2017-11-14 21:31:43 -0800483status_t StatsService::cmd_remove_config_files(FILE* out) {
484 fprintf(out, "Trying to remove config files...\n");
485 unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir);
486 if (dir == NULL) {
487 fprintf(out, "No existing config files found exiting...\n");
488 return NO_ERROR;
489 }
490
491 dirent* de;
492 while ((de = readdir(dir.get()))) {
493 char* name = de->d_name;
494 if (name[0] == '.') continue;
495 string file_name = StringPrintf("%s/%s", STATS_SERVICE_DIR, name);
496 fprintf(out, "Deleting file %s\n", file_name.c_str());
497 if (remove(file_name.c_str())) {
498 fprintf(out, "Error deleting file %s\n", file_name.c_str());
499 }
500 }
501 return NO_ERROR;
502}
503
David Chende701692017-10-05 13:16:02 -0700504Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
505 const vector<String16>& app) {
506 if (DEBUG) ALOGD("StatsService::informAllUidData was called");
507
508 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
509 return Status::fromExceptionCode(Status::EX_SECURITY,
510 "Only system uid can call informAllUidData");
511 }
512
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700513 mUidMap->updateMap(uid, version, app);
David Chende701692017-10-05 13:16:02 -0700514 if (DEBUG) ALOGD("StatsService::informAllUidData succeeded");
515
516 return Status::ok();
517}
518
519Status StatsService::informOnePackage(const String16& app, int32_t uid, int32_t version) {
520 if (DEBUG) ALOGD("StatsService::informOnePackage was called");
521
522 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
523 return Status::fromExceptionCode(Status::EX_SECURITY,
524 "Only system uid can call informOnePackage");
525 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700526 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700527 return Status::ok();
528}
529
530Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
531 if (DEBUG) ALOGD("StatsService::informOnePackageRemoved was called");
532
533 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
534 return Status::fromExceptionCode(Status::EX_SECURITY,
535 "Only system uid can call informOnePackageRemoved");
536 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700537 mUidMap->removeApp(app, uid);
David Chende701692017-10-05 13:16:02 -0700538 return Status::ok();
539}
540
Yao Chenef99c4f2017-09-22 16:26:54 -0700541Status StatsService::informAnomalyAlarmFired() {
Bookatzb487b552017-09-18 11:26:01 -0700542 if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700543
544 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
545 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700546 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700547 }
548
Bookatzb487b552017-09-18 11:26:01 -0700549 if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700550 // TODO: check through all counters/timers and see if an anomaly has indeed occurred.
551
552 return Status::ok();
553}
554
Yao Chenef99c4f2017-09-22 16:26:54 -0700555Status StatsService::informPollAlarmFired() {
Bookatzb487b552017-09-18 11:26:01 -0700556 if (DEBUG) ALOGD("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700557
558 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
559 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700560 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700561 }
562
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700563 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700564
Bookatzb487b552017-09-18 11:26:01 -0700565 if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700566
567 return Status::ok();
568}
569
Yao Chenef99c4f2017-09-22 16:26:54 -0700570Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700571 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
572 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700573 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700574 }
575
576 // When system_server is up and running, schedule the dropbox task to run.
577 ALOGD("StatsService::systemRunning");
578
Bookatzb487b552017-09-18 11:26:01 -0700579 sayHiToStatsCompanion();
580
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700581 return Status::ok();
582}
583
Yao Chenef99c4f2017-09-22 16:26:54 -0700584void StatsService::sayHiToStatsCompanion() {
585 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
586 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700587 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
588 if (statsCompanion != nullptr) {
589 if (DEBUG) ALOGD("Telling statsCompanion that statsd is ready");
590 statsCompanion->statsdReady();
591 } else {
592 if (DEBUG) ALOGD("Could not access statsCompanion");
593 }
594}
595
Yao Chenef99c4f2017-09-22 16:26:54 -0700596sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700597 sp<IStatsCompanionService> statsCompanion = nullptr;
598 // Get statscompanion service from service manager
599 const sp<IServiceManager> sm(defaultServiceManager());
600 if (sm != nullptr) {
601 const String16 name("statscompanion");
602 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
603 if (statsCompanion == nullptr) {
604 ALOGW("statscompanion service unavailable!");
605 return nullptr;
606 }
607 }
608 return statsCompanion;
609}
610
Yao Chenef99c4f2017-09-22 16:26:54 -0700611Status StatsService::statsCompanionReady() {
Bookatzb487b552017-09-18 11:26:01 -0700612 if (DEBUG) ALOGD("StatsService::statsCompanionReady was called");
613
614 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
615 return Status::fromExceptionCode(Status::EX_SECURITY,
616 "Only system uid can call statsCompanionReady");
617 }
618
619 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
620 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700621 return Status::fromExceptionCode(
622 Status::EX_NULL_POINTER,
623 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700624 }
625 if (DEBUG) ALOGD("StatsService::statsCompanionReady linking to statsCompanion.");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700626 IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor));
Bookatzb487b552017-09-18 11:26:01 -0700627 mAnomalyMonitor->setStatsCompanionService(statsCompanion);
628
629 return Status::ok();
630}
631
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700632void StatsService::Startup() {
633 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700634}
635
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700636void StatsService::OnLogEvent(const LogEvent& event) {
637 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700638}
639
David Chen1d7b0cd2017-11-15 14:20:04 -0800640Status StatsService::getData(const String16& key, vector <uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700641 IPCThreadState* ipc = IPCThreadState::self();
David Chen1d7b0cd2017-11-15 14:20:04 -0800642 ALOGD("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(),
643 ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800644 if (checkCallingPermission(String16(kPermissionDump))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800645 string keyStr = string(String8(key).string());
646 ConfigKey configKey(ipc->getCallingUid(), keyStr);
647 mProcessor->onDumpReport(configKey, output);
David Chenadaf8b32017-11-03 15:42:08 -0700648 return Status::ok();
649 } else {
650 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
651 }
yro31eb67b2017-10-24 13:33:21 -0700652}
653
David Chenadaf8b32017-11-03 15:42:08 -0700654Status StatsService::addConfiguration(const String16& key,
655 const vector <uint8_t>& config,
656 const String16& package, const String16& cls,
657 bool* success) {
658 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800659 if (checkCallingPermission(String16(kPermissionDump))) {
David Chenadaf8b32017-11-03 15:42:08 -0700660 string keyString = string(String8(key).string());
David Chen1d7b0cd2017-11-15 14:20:04 -0800661 ConfigKey configKey(ipc->getCallingUid(), keyString);
David Chenadaf8b32017-11-03 15:42:08 -0700662 StatsdConfig cfg;
663 cfg.ParseFromArray(&config[0], config.size());
664 mConfigManager->UpdateConfig(configKey, cfg);
665 mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()),
666 string(String8(cls).string()));
667 *success = true;
668 return Status::ok();
669 } else {
670 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700671 }
yro31eb67b2017-10-24 13:33:21 -0700672}
673
David Chenadaf8b32017-11-03 15:42:08 -0700674Status StatsService::removeConfiguration(const String16& key, bool* success) {
675 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800676 if (checkCallingPermission(String16(kPermissionDump))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800677 string keyStr = string(String8(key).string());
678 mConfigManager->RemoveConfig(ConfigKey(ipc->getCallingUid(), keyStr));
David Chenadaf8b32017-11-03 15:42:08 -0700679 return Status::ok();
680 } else {
681 *success = false;
682 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700683 }
yro31eb67b2017-10-24 13:33:21 -0700684}
685
David Chen1d7b0cd2017-11-15 14:20:04 -0800686void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700687 for (size_t i = 0; i < mCallbacks.size(); i++) {
688 if (IInterface::asBinder(mCallbacks[i]) == who) {
689 mCallbacks.removeAt(i);
690 break;
691 }
692 }
693}
694
Yao Chenef99c4f2017-09-22 16:26:54 -0700695} // namespace statsd
696} // namespace os
697} // namespace android