blob: 280081ec5a3d962900cf22b9a379ce0b955719a4 [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
Tej Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070019
20#include "StatsService.h"
Yangster-mac330af582018-02-08 15:24:38 -080021#include "stats_log_util.h"
Yao Chen8d9989b2017-11-18 18:54:50 -080022#include "android-base/stringprintf.h"
David Chenadaf8b32017-11-03 15:42:08 -070023#include "config/ConfigKey.h"
24#include "config/ConfigManager.h"
Yao Chenb3561512017-11-21 18:07:17 -080025#include "guardrail/StatsdStats.h"
yro947fbce2017-11-15 22:50:23 -080026#include "storage/StorageManager.h"
Bookatzc6977972018-01-16 16:55:05 -080027#include "subscriber/SubscriberReporter.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070028
David Chen0656b7a2017-09-13 15:53:39 -070029#include <android-base/file.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
yro87d983c2017-11-14 21:31:43 -080032#include <dirent.h>
David Chen0656b7a2017-09-13 15:53:39 -070033#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070034#include <private/android_filesystem_config.h>
35#include <utils/Looper.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070036#include <utils/String16.h>
Bookatzb223c4e2018-02-01 15:35:04 -080037#include <statslog.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070038#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070039#include <stdlib.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070040#include <sys/system_properties.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070041#include <unistd.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070042
43using namespace android;
44
Bookatz906a35c2017-09-20 15:26:44 -070045namespace android {
46namespace os {
47namespace statsd {
48
David Chenadaf8b32017-11-03 15:42:08 -070049constexpr const char* kPermissionDump = "android.permission.DUMP";
yro03faf092017-12-12 00:17:50 -080050#define STATS_SERVICE_DIR "/data/misc/stats-service"
David Chenadaf8b32017-11-03 15:42:08 -070051
Joe Onorato9fc9edf2017-10-15 20:08:52 -070052/**
53 * Watches for the death of the stats companion (system process).
54 */
55class CompanionDeathRecipient : public IBinder::DeathRecipient {
56public:
Yangster-mac932ecec2018-02-01 10:23:52 -080057 CompanionDeathRecipient(const sp<AlarmMonitor>& anomalyAlarmMonitor,
58 const sp<AlarmMonitor>& periodicAlarmMonitor) :
59 mAnomalyAlarmMonitor(anomalyAlarmMonitor),
60 mPeriodicAlarmMonitor(periodicAlarmMonitor) {}
Joe Onorato9fc9edf2017-10-15 20:08:52 -070061 virtual void binderDied(const wp<IBinder>& who);
62
63private:
Yangster-mac932ecec2018-02-01 10:23:52 -080064 sp<AlarmMonitor> mAnomalyAlarmMonitor;
65 sp<AlarmMonitor> mPeriodicAlarmMonitor;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070066};
67
Joe Onorato9fc9edf2017-10-15 20:08:52 -070068void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) {
69 ALOGW("statscompanion service died");
Yangster-mac932ecec2018-02-01 10:23:52 -080070 mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
71 mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
Bookatzc6977972018-01-16 16:55:05 -080072 SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070073}
74
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070075StatsService::StatsService(const sp<Looper>& handlerLooper)
Yangster-mac932ecec2018-02-01 10:23:52 -080076 : mAnomalyAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
77 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
78 if (sc != nullptr) {
79 sc->setAnomalyAlarm(timeMillis);
80 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
81 }
82 },
83 [](const sp<IStatsCompanionService>& sc) {
84 if (sc != nullptr) {
85 sc->cancelAnomalyAlarm();
86 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
87 }
88 })),
89 mPeriodicAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
90 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
91 if (sc != nullptr) {
92 sc->setAlarmForSubscriberTriggering(timeMillis);
93 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
94 }
95 },
96 [](const sp<IStatsCompanionService>& sc) {
97 if (sc != nullptr) {
98 sc->cancelAlarmForSubscriberTriggering();
99 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
100 }
101
102 })) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700103 mUidMap = new UidMap();
Chenjie Yu80f91122018-01-31 20:24:50 -0800104 StatsPuller::SetUidMap(mUidMap);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700105 mConfigManager = new ConfigManager();
Yangster-mac932ecec2018-02-01 10:23:52 -0800106 mProcessor = new StatsLogProcessor(mUidMap, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor,
107 getElapsedRealtimeSec(), [this](const ConfigKey& key) {
108 sp<IStatsCompanionService> sc = getStatsCompanionService();
109 auto receiver = mConfigManager->GetConfigReceiver(key);
110 if (sc == nullptr) {
111 VLOG("Could not find StatsCompanionService");
112 } else if (receiver == nullptr) {
113 VLOG("Statscompanion could not find a broadcast receiver for %s",
114 key.ToString().c_str());
115 } else {
116 sc->sendDataBroadcast(receiver);
David Chen1d7b0cd2017-11-15 14:20:04 -0800117 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800118 }
Yangster-mac330af582018-02-08 15:24:38 -0800119 );
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700120
121 mConfigManager->AddListener(mProcessor);
122
123 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700124}
125
Yao Chenef99c4f2017-09-22 16:26:54 -0700126StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700127}
128
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700129void StatsService::init_system_properties() {
130 mEngBuild = false;
131 const prop_info* buildType = __system_property_find("ro.build.type");
132 if (buildType != NULL) {
133 __system_property_read_callback(buildType, init_build_type_callback, this);
134 }
David Chen0656b7a2017-09-13 15:53:39 -0700135}
136
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700137void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
138 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700139 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700140 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
141 }
142}
143
144/**
145 * Implement our own because the default binder implementation isn't
146 * properly handling SHELL_COMMAND_TRANSACTION.
147 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700148status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
149 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700150 switch (code) {
151 case SHELL_COMMAND_TRANSACTION: {
152 int in = data.readFileDescriptor();
153 int out = data.readFileDescriptor();
154 int err = data.readFileDescriptor();
155 int argc = data.readInt32();
156 Vector<String8> args;
157 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
158 args.add(String8(data.readString16()));
159 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700160 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
161 sp<IResultReceiver> resultReceiver =
162 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700163
164 FILE* fin = fdopen(in, "r");
165 FILE* fout = fdopen(out, "w");
166 FILE* ferr = fdopen(err, "w");
167
168 if (fin == NULL || fout == NULL || ferr == NULL) {
169 resultReceiver->send(NO_MEMORY);
170 } else {
171 err = command(fin, fout, ferr, args);
172 resultReceiver->send(err);
173 }
174
175 if (fin != NULL) {
176 fflush(fin);
177 fclose(fin);
178 }
179 if (fout != NULL) {
180 fflush(fout);
181 fclose(fout);
182 }
183 if (fout != NULL) {
184 fflush(ferr);
185 fclose(ferr);
186 }
187
188 return NO_ERROR;
189 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700190 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700191 }
192}
193
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700194/**
195 * Write debugging data about statsd.
196 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700197status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700198 FILE* out = fdopen(fd, "w");
199 if (out == NULL) {
200 return NO_MEMORY; // the fd is already open
201 }
202
Yao Chen884c8c12018-01-26 10:36:25 -0800203 bool verbose = false;
204 if (args.size() > 0 && !args[0].compare(String16("-v"))) {
205 verbose = true;
206 }
207
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700208 // TODO: Proto format for incident reports
Yao Chen884c8c12018-01-26 10:36:25 -0800209 dump_impl(out, verbose);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700210
211 fclose(out);
212 return NO_ERROR;
213}
214
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700215/**
216 * Write debugging data about statsd in text format.
217 */
Yao Chen884c8c12018-01-26 10:36:25 -0800218void StatsService::dump_impl(FILE* out, bool verbose) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800219 StatsdStats::getInstance().dumpStats(out);
Yao Chen884c8c12018-01-26 10:36:25 -0800220 mProcessor->dumpStates(out, verbose);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700221}
222
223/**
224 * Implementation of the adb shell cmd stats command.
225 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700226status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700227 // TODO: Permission check
228
229 const int argCount = args.size();
230 if (argCount >= 1) {
231 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700232 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700233 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700234 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700235
David Chende701692017-10-05 13:16:02 -0700236 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800237 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700238 }
Yao Chen729093d2017-10-16 10:33:26 -0700239
240 if (!args[0].compare(String8("dump-report"))) {
241 return cmd_dump_report(out, err, args);
242 }
David Chen1481fe12017-10-16 13:16:34 -0700243
244 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
245 return cmd_print_pulled_metrics(out, args);
246 }
David Chenadaf8b32017-11-03 15:42:08 -0700247
248 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800249 return cmd_trigger_broadcast(out, args);
250 }
251
252 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800253 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700254 }
yro87d983c2017-11-14 21:31:43 -0800255
Yao Chen8d9989b2017-11-18 18:54:50 -0800256 if (!args[0].compare(String8("meminfo"))) {
257 return cmd_dump_memory_info(out);
258 }
yro947fbce2017-11-15 22:50:23 -0800259
260 if (!args[0].compare(String8("write-to-disk"))) {
261 return cmd_write_data_to_disk(out);
262 }
Bookatzb223c4e2018-02-01 15:35:04 -0800263
David Chen0b5c90c2018-01-25 16:51:49 -0800264 if (!args[0].compare(String8("log-app-breadcrumb"))) {
265 return cmd_log_app_breadcrumb(out, args);
Bookatzb223c4e2018-02-01 15:35:04 -0800266 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800267
268 if (!args[0].compare(String8("clear-puller-cache"))) {
269 return cmd_clear_puller_cache(out);
270 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700271 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700272
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700273 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700274 return NO_ERROR;
275}
276
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700277void StatsService::print_cmd_help(FILE* out) {
278 fprintf(out,
279 "usage: adb shell cmd stats print-stats-log [tag_required] "
280 "[timestamp_nsec_optional]\n");
281 fprintf(out, "\n");
282 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800283 fprintf(out, "usage: adb shell cmd stats meminfo\n");
284 fprintf(out, "\n");
285 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
286 fprintf(out, " # adb shell stop\n");
287 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
288 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
289 fprintf(out, " # adb shell start\n");
290 fprintf(out, "\n");
291 fprintf(out, "\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800292 fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700293 fprintf(out, "\n");
294 fprintf(out, " Prints the UID, app name, version mapping.\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800295 fprintf(out, " PKG Optional package name to print the uids of the package\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700296 fprintf(out, "\n");
297 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800298 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700299 fprintf(out, "\n");
300 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
301 fprintf(out, "\n");
302 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800303 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
304 fprintf(out, "\n");
305 fprintf(out, " Flushes all data on memory to disk.\n");
306 fprintf(out, "\n");
307 fprintf(out, "\n");
David Chen0b5c90c2018-01-25 16:51:49 -0800308 fprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n");
309 fprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800310 fprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
311 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
312 fprintf(out, " uid is used.\n");
313 fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
314 fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
315 fprintf(out, "\n");
316 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800317 fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700318 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
319 fprintf(out, "\n");
320 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
yro74fed972017-11-27 14:42:42 -0800321 fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
322 fprintf(out, " provided, then all configs will be removed from memory and disk.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700323 fprintf(out, "\n");
324 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
yro74fed972017-11-27 14:42:42 -0800325 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700326 fprintf(out, " uid is used.\n");
327 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a372017-10-30 22:57:06 -0700328 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800329 fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
330 fprintf(out, "\n be removed from memory and disk!\n");
Yao Chen5154a372017-10-30 22:57:06 -0700331 fprintf(out, "\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800332 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n");
Yao Chen5154a372017-10-30 22:57:06 -0700333 fprintf(out, " Dump all metric data for a configuration.\n");
334 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
335 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
336 fprintf(out, " calling uid is used.\n");
337 fprintf(out, " NAME The name of the configuration\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800338 fprintf(out, " --proto Print proto binary.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700339 fprintf(out, "\n");
340 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800341 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
342 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
343 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
344 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
345 fprintf(out, " calling uid is used.\n");
346 fprintf(out, " NAME The name of the configuration\n");
347 fprintf(out, "\n");
348 fprintf(out, "\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800349 fprintf(out, "usage: adb shell cmd stats print-stats\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800350 fprintf(out, " Prints some basic stats.\n");
Chenjie Yufa22d652018-02-05 14:37:48 -0800351 fprintf(out, "\n");
352 fprintf(out, "\n");
353 fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
354 fprintf(out, " Clear cached puller data.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700355}
356
David Chen1d7b0cd2017-11-15 14:20:04 -0800357status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
358 string name;
359 bool good = false;
360 int uid;
361 const int argCount = args.size();
362 if (argCount == 2) {
363 // Automatically pick the UID
364 uid = IPCThreadState::self()->getCallingUid();
365 // TODO: What if this isn't a binder call? Should we fail?
366 name.assign(args[1].c_str(), args[1].size());
367 good = true;
368 } else if (argCount == 3) {
369 // If it's a userdebug or eng build, then the shell user can
370 // impersonate other uids.
371 if (mEngBuild) {
372 const char* s = args[1].c_str();
373 if (*s != '\0') {
374 char* end = NULL;
375 uid = strtol(s, &end, 0);
376 if (*end == '\0') {
377 name.assign(args[2].c_str(), args[2].size());
378 good = true;
379 }
380 }
381 } else {
382 fprintf(out,
383 "The metrics can only be dumped for other UIDs on eng or userdebug "
384 "builds.\n");
385 }
386 }
387 if (!good) {
388 print_cmd_help(out);
389 return UNKNOWN_ERROR;
390 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800391 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name)));
yro4d889e62017-11-17 15:44:48 -0800392 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen661f7912018-01-22 17:46:24 -0800393 if (sc == nullptr) {
394 VLOG("Could not access statsCompanion");
395 } else if (receiver == nullptr) {
396 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str())
397 } else {
398 sc->sendDataBroadcast(receiver);
yro74fed972017-11-27 14:42:42 -0800399 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
400 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800401 }
402
David Chenadaf8b32017-11-03 15:42:08 -0700403 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700404}
405
406status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
407 const int argCount = args.size();
408 if (argCount >= 2) {
409 if (args[1] == "update" || args[1] == "remove") {
410 bool good = false;
411 int uid = -1;
412 string name;
413
414 if (argCount == 3) {
415 // Automatically pick the UID
416 uid = IPCThreadState::self()->getCallingUid();
417 // TODO: What if this isn't a binder call? Should we fail?
418 name.assign(args[2].c_str(), args[2].size());
419 good = true;
420 } else if (argCount == 4) {
421 // If it's a userdebug or eng build, then the shell user can
422 // impersonate other uids.
423 if (mEngBuild) {
424 const char* s = args[2].c_str();
425 if (*s != '\0') {
426 char* end = NULL;
427 uid = strtol(s, &end, 0);
428 if (*end == '\0') {
429 name.assign(args[3].c_str(), args[3].size());
430 good = true;
431 }
432 }
433 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700434 fprintf(err,
435 "The config can only be set for other UIDs on eng or userdebug "
436 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700437 }
yroe5f82922018-01-22 18:37:27 -0800438 } else if (argCount == 2 && args[1] == "remove") {
439 good = true;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700440 }
441
442 if (!good) {
443 // If arg parsing failed, print the help text and return an error.
444 print_cmd_help(out);
445 return UNKNOWN_ERROR;
446 }
447
448 if (args[1] == "update") {
yro255f72e2018-02-26 15:15:17 -0800449 char* endp;
450 int64_t configID = strtoll(name.c_str(), &endp, 10);
451 if (endp == name.c_str() || *endp != '\0') {
452 fprintf(err, "Error parsing config ID.\n");
453 return UNKNOWN_ERROR;
454 }
455
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700456 // Read stream into buffer.
457 string buffer;
458 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
459 fprintf(err, "Error reading stream for StatsConfig.\n");
460 return UNKNOWN_ERROR;
461 }
462
463 // Parse buffer.
464 StatsdConfig config;
465 if (!config.ParseFromString(buffer)) {
466 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
467 return UNKNOWN_ERROR;
468 }
469
470 // Add / update the config.
yro255f72e2018-02-26 15:15:17 -0800471 mConfigManager->UpdateConfig(ConfigKey(uid, configID), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700472 } else {
yro74fed972017-11-27 14:42:42 -0800473 if (argCount == 2) {
474 cmd_remove_all_configs(out);
475 } else {
476 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800477 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800478 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700479 }
480
481 return NO_ERROR;
482 }
David Chen0656b7a2017-09-13 15:53:39 -0700483 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700484 print_cmd_help(out);
485 return UNKNOWN_ERROR;
486}
487
Yao Chen729093d2017-10-16 10:33:26 -0700488status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
489 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800490 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700491 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800492 bool proto = false;
Yao Chen729093d2017-10-16 10:33:26 -0700493 int uid;
494 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800495 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
496 proto = true;
497 argCount -= 1;
498 }
Yao Chen729093d2017-10-16 10:33:26 -0700499 if (argCount == 2) {
500 // Automatically pick the UID
501 uid = IPCThreadState::self()->getCallingUid();
502 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a372017-10-30 22:57:06 -0700503 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700504 good = true;
505 } else if (argCount == 3) {
506 // If it's a userdebug or eng build, then the shell user can
507 // impersonate other uids.
508 if (mEngBuild) {
509 const char* s = args[1].c_str();
510 if (*s != '\0') {
511 char* end = NULL;
512 uid = strtol(s, &end, 0);
513 if (*end == '\0') {
514 name.assign(args[2].c_str(), args[2].size());
515 good = true;
516 }
517 }
518 } else {
519 fprintf(out,
520 "The metrics can only be dumped for other UIDs on eng or userdebug "
521 "builds.\n");
522 }
523 }
524 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800525 vector<uint8_t> data;
David Chen926fc752018-02-23 13:31:43 -0800526 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(),
Yao Chen8a8d16c2018-02-08 14:50:40 -0800527 &data);
Yao Chen729093d2017-10-16 10:33:26 -0700528 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
Chenjie Yub236c862017-11-28 22:20:44 -0800529 if (proto) {
530 for (size_t i = 0; i < data.size(); i ++) {
531 fprintf(out, "%c", data[i]);
532 }
533 } else {
534 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
535 fprintf(out, "See the StatsLogReport in logcat...\n");
536 }
Yao Chen729093d2017-10-16 10:33:26 -0700537 return android::OK;
538 } else {
539 // If arg parsing failed, print the help text and return an error.
540 print_cmd_help(out);
541 return UNKNOWN_ERROR;
542 }
543 } else {
544 fprintf(out, "Log processor does not exist...\n");
545 return UNKNOWN_ERROR;
546 }
547}
548
Yao Chenb3561512017-11-21 18:07:17 -0800549status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800550 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
551 for (const ConfigKey& key : configs) {
552 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
553 mProcessor->GetMetricsSize(key));
554 }
Yao Chenb3561512017-11-21 18:07:17 -0800555 StatsdStats& statsdStats = StatsdStats::getInstance();
Yao Chenf5acabe2018-01-17 14:10:34 -0800556 statsdStats.dumpStats(out);
David Chen1d7b0cd2017-11-15 14:20:04 -0800557 return NO_ERROR;
558}
559
Yao Chend10f7b12017-12-18 12:53:50 -0800560status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) {
561 if (args.size() > 1) {
562 string pkg;
563 pkg.assign(args[1].c_str(), args[1].size());
564 auto uids = mUidMap->getAppUid(pkg);
565 fprintf(out, "%s -> [ ", pkg.c_str());
566 for (const auto& uid : uids) {
567 fprintf(out, "%d ", uid);
568 }
569 fprintf(out, "]\n");
570 } else {
571 mUidMap->printUidMap(out);
572 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700573 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700574}
575
yro947fbce2017-11-15 22:50:23 -0800576status_t StatsService::cmd_write_data_to_disk(FILE* out) {
577 fprintf(out, "Writing data to disk\n");
578 mProcessor->WriteDataToDisk();
579 return NO_ERROR;
580}
581
David Chen0b5c90c2018-01-25 16:51:49 -0800582status_t StatsService::cmd_log_app_breadcrumb(FILE* out, const Vector<String8>& args) {
Bookatzb223c4e2018-02-01 15:35:04 -0800583 bool good = false;
584 int32_t uid;
585 int32_t label;
586 int32_t state;
587 const int argCount = args.size();
588 if (argCount == 3) {
589 // Automatically pick the UID
590 uid = IPCThreadState::self()->getCallingUid();
591 label = atoi(args[1].c_str());
592 state = atoi(args[2].c_str());
593 good = true;
594 } else if (argCount == 4) {
595 uid = atoi(args[1].c_str());
596 // If it's a userdebug or eng build, then the shell user can impersonate other uids.
597 // Otherwise, the uid must match the actual caller's uid.
598 if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) {
599 label = atoi(args[2].c_str());
600 state = atoi(args[3].c_str());
601 good = true;
602 } else {
603 fprintf(out,
David Chenb639d142018-02-14 17:29:54 -0800604 "Selecting a UID for writing AppBreadcrumb can only be done for other UIDs "
605 "on eng or userdebug builds.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800606 }
607 }
608 if (good) {
David Chen0b5c90c2018-01-25 16:51:49 -0800609 fprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state);
610 android::util::stats_write(android::util::APP_BREADCRUMB_REPORTED, uid, label, state);
Bookatzb223c4e2018-02-01 15:35:04 -0800611 } else {
612 print_cmd_help(out);
613 return UNKNOWN_ERROR;
614 }
615 return NO_ERROR;
616}
617
David Chen1481fe12017-10-16 13:16:34 -0700618status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
619 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700620 vector<shared_ptr<LogEvent> > stats;
621 if (mStatsPullerManager.Pull(s, &stats)) {
622 for (const auto& it : stats) {
623 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
624 }
625 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
626 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700627 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700628 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700629}
630
yro74fed972017-11-27 14:42:42 -0800631status_t StatsService::cmd_remove_all_configs(FILE* out) {
632 fprintf(out, "Removing all configs...\n");
633 VLOG("StatsService::cmd_remove_all_configs was called");
634 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800635 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800636 return NO_ERROR;
637}
638
Yao Chen8d9989b2017-11-18 18:54:50 -0800639status_t StatsService::cmd_dump_memory_info(FILE* out) {
Yao Chen20e9e622018-02-28 11:18:51 -0800640 fprintf(out, "meminfo not available.\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800641 return NO_ERROR;
642}
643
Chenjie Yue72252b2018-02-01 13:19:35 -0800644status_t StatsService::cmd_clear_puller_cache(FILE* out) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800645 IPCThreadState* ipc = IPCThreadState::self();
Yangster-mac932ecec2018-02-01 10:23:52 -0800646 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i",
647 ipc->getCallingPid(), ipc->getCallingUid());
Chenjie Yufa22d652018-02-05 14:37:48 -0800648 if (checkCallingPermission(String16(kPermissionDump))) {
649 int cleared = mStatsPullerManager.ForceClearPullerCache();
650 fprintf(out, "Puller removed %d cached data!\n", cleared);
651 return NO_ERROR;
652 } else {
653 return PERMISSION_DENIED;
654 }
Chenjie Yue72252b2018-02-01 13:19:35 -0800655}
656
Dianne Hackborn3accca02013-09-20 09:32:11 -0700657Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
David Chende701692017-10-05 13:16:02 -0700658 const vector<String16>& app) {
yro74fed972017-11-27 14:42:42 -0800659 VLOG("StatsService::informAllUidData was called");
David Chende701692017-10-05 13:16:02 -0700660
661 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
662 return Status::fromExceptionCode(Status::EX_SECURITY,
663 "Only system uid can call informAllUidData");
664 }
665
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700666 mUidMap->updateMap(uid, version, app);
yro74fed972017-11-27 14:42:42 -0800667 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700668
669 return Status::ok();
670}
671
Dianne Hackborn3accca02013-09-20 09:32:11 -0700672Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) {
yro74fed972017-11-27 14:42:42 -0800673 VLOG("StatsService::informOnePackage was called");
David Chende701692017-10-05 13:16:02 -0700674
675 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
676 return Status::fromExceptionCode(Status::EX_SECURITY,
677 "Only system uid can call informOnePackage");
678 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700679 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700680 return Status::ok();
681}
682
683Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
yro74fed972017-11-27 14:42:42 -0800684 VLOG("StatsService::informOnePackageRemoved was called");
David Chende701692017-10-05 13:16:02 -0700685
686 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
687 return Status::fromExceptionCode(Status::EX_SECURITY,
688 "Only system uid can call informOnePackageRemoved");
689 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700690 mUidMap->removeApp(app, uid);
yro01924022018-02-20 18:20:49 -0800691 mConfigManager->RemoveConfigs(uid);
David Chende701692017-10-05 13:16:02 -0700692 return Status::ok();
693}
694
Yao Chenef99c4f2017-09-22 16:26:54 -0700695Status StatsService::informAnomalyAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800696 VLOG("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700697
698 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
699 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700700 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700701 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800702
Yangster-mac330af582018-02-08 15:24:38 -0800703 uint64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800704 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
705 mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
706 if (alarmSet.size() > 0) {
Bookatz66fe0612018-02-07 18:51:48 -0800707 VLOG("Found an anomaly alarm that fired.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800708 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
Bookatz66fe0612018-02-07 18:51:48 -0800709 } else {
710 VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
711 }
Bookatz1b0b1142017-09-08 11:58:42 -0700712 return Status::ok();
713}
714
Yangster-mac932ecec2018-02-01 10:23:52 -0800715Status StatsService::informAlarmForSubscriberTriggeringFired() {
716 VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
717
718 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
719 return Status::fromExceptionCode(
720 Status::EX_SECURITY,
721 "Only system uid can call informAlarmForSubscriberTriggeringFired");
722 }
723
724 uint64_t currentTimeSec = time(nullptr);
725 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
726 mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
727 if (alarmSet.size() > 0) {
728 VLOG("Found periodic alarm fired.");
729 mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
730 } else {
731 ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
732 }
733 return Status::ok();
734}
735
Yao Chenef99c4f2017-09-22 16:26:54 -0700736Status StatsService::informPollAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800737 VLOG("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700738
739 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
740 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700741 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700742 }
743
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700744 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700745
yro74fed972017-11-27 14:42:42 -0800746 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700747
748 return Status::ok();
749}
750
Yao Chenef99c4f2017-09-22 16:26:54 -0700751Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700752 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
753 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700754 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700755 }
756
757 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800758 VLOG("StatsService::systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700759
Bookatzb487b552017-09-18 11:26:01 -0700760 sayHiToStatsCompanion();
761
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700762 return Status::ok();
763}
764
yro947fbce2017-11-15 22:50:23 -0800765Status StatsService::writeDataToDisk() {
766 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
767 return Status::fromExceptionCode(Status::EX_SECURITY,
768 "Only system uid can call systemRunning");
769 }
770
yro74fed972017-11-27 14:42:42 -0800771 VLOG("StatsService::writeDataToDisk");
yro947fbce2017-11-15 22:50:23 -0800772
773 mProcessor->WriteDataToDisk();
774
775 return Status::ok();
776}
777
Yao Chenef99c4f2017-09-22 16:26:54 -0700778void StatsService::sayHiToStatsCompanion() {
779 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
780 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700781 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
782 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800783 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700784 statsCompanion->statsdReady();
785 } else {
yro74fed972017-11-27 14:42:42 -0800786 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700787 }
788}
789
Yao Chenef99c4f2017-09-22 16:26:54 -0700790sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700791 sp<IStatsCompanionService> statsCompanion = nullptr;
792 // Get statscompanion service from service manager
793 const sp<IServiceManager> sm(defaultServiceManager());
794 if (sm != nullptr) {
795 const String16 name("statscompanion");
796 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
797 if (statsCompanion == nullptr) {
798 ALOGW("statscompanion service unavailable!");
799 return nullptr;
800 }
801 }
802 return statsCompanion;
803}
804
Yao Chenef99c4f2017-09-22 16:26:54 -0700805Status StatsService::statsCompanionReady() {
yro74fed972017-11-27 14:42:42 -0800806 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700807
808 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
809 return Status::fromExceptionCode(Status::EX_SECURITY,
810 "Only system uid can call statsCompanionReady");
811 }
812
813 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
814 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700815 return Status::fromExceptionCode(
816 Status::EX_NULL_POINTER,
817 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700818 }
yro74fed972017-11-27 14:42:42 -0800819 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800820 IInterface::asBinder(statsCompanion)->linkToDeath(
821 new CompanionDeathRecipient(mAnomalyAlarmMonitor, mPeriodicAlarmMonitor));
822 mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion);
823 mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800824 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700825 return Status::ok();
826}
827
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700828void StatsService::Startup() {
829 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700830}
831
Yangster-macd40053e2018-01-09 16:29:22 -0800832void StatsService::OnLogEvent(LogEvent* event) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700833 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700834}
835
Yangster-mac94e197c2018-01-02 16:03:03 -0800836Status StatsService::getData(int64_t key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700837 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800838 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800839 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800840 ConfigKey configKey(ipc->getCallingUid(), key);
David Chen926fc752018-02-23 13:31:43 -0800841 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), output);
David Chenadaf8b32017-11-03 15:42:08 -0700842 return Status::ok();
843 } else {
844 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
845 }
yro31eb67b2017-10-24 13:33:21 -0700846}
847
David Chen2e8f3802017-11-22 10:56:48 -0800848Status StatsService::getMetadata(vector<uint8_t>* output) {
849 IPCThreadState* ipc = IPCThreadState::self();
850 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
851 ipc->getCallingUid());
852 if (checkCallingPermission(String16(kPermissionDump))) {
853 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
854 return Status::ok();
855 } else {
856 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
857 }
858}
859
Yangster-mac94e197c2018-01-02 16:03:03 -0800860Status StatsService::addConfiguration(int64_t key,
David Chenadaf8b32017-11-03 15:42:08 -0700861 const vector <uint8_t>& config,
David Chenadaf8b32017-11-03 15:42:08 -0700862 bool* success) {
863 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800864 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800865 ConfigKey configKey(ipc->getCallingUid(), key);
David Chenadaf8b32017-11-03 15:42:08 -0700866 StatsdConfig cfg;
Yangster-macbbd056a2018-01-22 13:37:02 -0800867 if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) {
David Chen7d8aa4d2017-12-27 13:37:01 -0800868 *success = false;
869 return Status::ok();
870 }
David Chenadaf8b32017-11-03 15:42:08 -0700871 mConfigManager->UpdateConfig(configKey, cfg);
David Chen661f7912018-01-22 17:46:24 -0800872 *success = true;
873 return Status::ok();
874 } else {
875 *success = false;
876 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
877 }
878}
879
880Status StatsService::removeDataFetchOperation(int64_t key, bool* success) {
881 IPCThreadState* ipc = IPCThreadState::self();
882 if (checkCallingPermission(String16(kPermissionDump))) {
883 ConfigKey configKey(ipc->getCallingUid(), key);
884 mConfigManager->RemoveConfigReceiver(configKey);
885 *success = true;
886 return Status::ok();
887 } else {
888 *success = false;
889 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
890 }
891}
892
893Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender,
894 bool* success) {
895 IPCThreadState* ipc = IPCThreadState::self();
896 if (checkCallingPermission(String16(kPermissionDump))) {
897 ConfigKey configKey(ipc->getCallingUid(), key);
898 mConfigManager->SetConfigReceiver(configKey, intentSender);
David Chenadaf8b32017-11-03 15:42:08 -0700899 *success = true;
900 return Status::ok();
901 } else {
Yao Chenaa39bc72017-12-01 11:16:50 -0800902 *success = false;
David Chenadaf8b32017-11-03 15:42:08 -0700903 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700904 }
yro31eb67b2017-10-24 13:33:21 -0700905}
906
Yangster-mac94e197c2018-01-02 16:03:03 -0800907Status StatsService::removeConfiguration(int64_t key, bool* success) {
David Chenadaf8b32017-11-03 15:42:08 -0700908 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800909 if (checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800910 ConfigKey configKey(ipc->getCallingUid(), key);
911 mConfigManager->RemoveConfig(configKey);
912 SubscriberReporter::getInstance().removeConfig(configKey);
Yao Chenaa39bc72017-12-01 11:16:50 -0800913 *success = true;
David Chenadaf8b32017-11-03 15:42:08 -0700914 return Status::ok();
915 } else {
916 *success = false;
917 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700918 }
yro31eb67b2017-10-24 13:33:21 -0700919}
920
Bookatzc6977972018-01-16 16:55:05 -0800921Status StatsService::setBroadcastSubscriber(int64_t configId,
922 int64_t subscriberId,
923 const sp<android::IBinder>& intentSender,
924 bool* success) {
925 VLOG("StatsService::setBroadcastSubscriber called.");
926 IPCThreadState* ipc = IPCThreadState::self();
927 if (checkCallingPermission(String16(kPermissionDump))) {
928 ConfigKey configKey(ipc->getCallingUid(), configId);
929 SubscriberReporter::getInstance()
930 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
931 *success = true;
932 return Status::ok();
933 } else {
934 *success = false;
935 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
936 }
937}
938
939Status StatsService::unsetBroadcastSubscriber(int64_t configId,
940 int64_t subscriberId,
941 bool* success) {
942 VLOG("StatsService::unsetBroadcastSubscriber called.");
943 IPCThreadState* ipc = IPCThreadState::self();
944 if (checkCallingPermission(String16(kPermissionDump))) {
945 ConfigKey configKey(ipc->getCallingUid(), configId);
946 SubscriberReporter::getInstance()
947 .unsetBroadcastSubscriber(configKey, subscriberId);
948 *success = true;
949 return Status::ok();
950 } else {
951 *success = false;
952 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
953 }
954}
955
956
David Chen1d7b0cd2017-11-15 14:20:04 -0800957void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700958}
959
Yao Chenef99c4f2017-09-22 16:26:54 -0700960} // namespace statsd
961} // namespace os
962} // namespace android