blob: ee4f434aa6a21b7138672e1766fcde263b3b9461 [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 Chen8d9989b2017-11-18 18:54:50 -080025#include "guardrail/MemoryLeakTrackUtil.h"
Yao Chenb3561512017-11-21 18:07:17 -080026#include "guardrail/StatsdStats.h"
yro947fbce2017-11-15 22:50:23 -080027#include "storage/StorageManager.h"
Bookatzc6977972018-01-16 16:55:05 -080028#include "subscriber/SubscriberReporter.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070029
David Chen0656b7a2017-09-13 15:53:39 -070030#include <android-base/file.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
yro87d983c2017-11-14 21:31:43 -080033#include <dirent.h>
David Chen0656b7a2017-09-13 15:53:39 -070034#include <frameworks/base/cmds/statsd/src/statsd_config.pb.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070035#include <private/android_filesystem_config.h>
36#include <utils/Looper.h>
Joe Onorato2cbc2cc2017-08-30 17:03:23 -070037#include <utils/String16.h>
Bookatzb223c4e2018-02-01 15:35:04 -080038#include <statslog.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070039#include <stdio.h>
Yao Chen482d2722017-09-12 13:25:43 -070040#include <stdlib.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070041#include <sys/system_properties.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070042#include <unistd.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070043
44using namespace android;
45
Bookatz906a35c2017-09-20 15:26:44 -070046namespace android {
47namespace os {
48namespace statsd {
49
David Chenadaf8b32017-11-03 15:42:08 -070050constexpr const char* kPermissionDump = "android.permission.DUMP";
yro03faf092017-12-12 00:17:50 -080051#define STATS_SERVICE_DIR "/data/misc/stats-service"
David Chenadaf8b32017-11-03 15:42:08 -070052
Joe Onorato9fc9edf2017-10-15 20:08:52 -070053/**
54 * Watches for the death of the stats companion (system process).
55 */
56class CompanionDeathRecipient : public IBinder::DeathRecipient {
57public:
Yangster-mac932ecec2018-02-01 10:23:52 -080058 CompanionDeathRecipient(const sp<AlarmMonitor>& anomalyAlarmMonitor,
59 const sp<AlarmMonitor>& periodicAlarmMonitor) :
60 mAnomalyAlarmMonitor(anomalyAlarmMonitor),
61 mPeriodicAlarmMonitor(periodicAlarmMonitor) {}
Joe Onorato9fc9edf2017-10-15 20:08:52 -070062 virtual void binderDied(const wp<IBinder>& who);
63
64private:
Yangster-mac932ecec2018-02-01 10:23:52 -080065 sp<AlarmMonitor> mAnomalyAlarmMonitor;
66 sp<AlarmMonitor> mPeriodicAlarmMonitor;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070067};
68
Joe Onorato9fc9edf2017-10-15 20:08:52 -070069void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) {
70 ALOGW("statscompanion service died");
Yangster-mac932ecec2018-02-01 10:23:52 -080071 mAnomalyAlarmMonitor->setStatsCompanionService(nullptr);
72 mPeriodicAlarmMonitor->setStatsCompanionService(nullptr);
Bookatzc6977972018-01-16 16:55:05 -080073 SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070074}
75
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070076StatsService::StatsService(const sp<Looper>& handlerLooper)
Yangster-mac932ecec2018-02-01 10:23:52 -080077 : mAnomalyAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
78 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
79 if (sc != nullptr) {
80 sc->setAnomalyAlarm(timeMillis);
81 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
82 }
83 },
84 [](const sp<IStatsCompanionService>& sc) {
85 if (sc != nullptr) {
86 sc->cancelAnomalyAlarm();
87 StatsdStats::getInstance().noteRegisteredAnomalyAlarmChanged();
88 }
89 })),
90 mPeriodicAlarmMonitor(new AlarmMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS,
91 [](const sp<IStatsCompanionService>& sc, int64_t timeMillis) {
92 if (sc != nullptr) {
93 sc->setAlarmForSubscriberTriggering(timeMillis);
94 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
95 }
96 },
97 [](const sp<IStatsCompanionService>& sc) {
98 if (sc != nullptr) {
99 sc->cancelAlarmForSubscriberTriggering();
100 StatsdStats::getInstance().noteRegisteredPeriodicAlarmChanged();
101 }
102
103 })) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700104 mUidMap = new UidMap();
Chenjie Yu80f91122018-01-31 20:24:50 -0800105 StatsPuller::SetUidMap(mUidMap);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700106 mConfigManager = new ConfigManager();
Yangster-mac932ecec2018-02-01 10:23:52 -0800107 mProcessor = new StatsLogProcessor(mUidMap, mAnomalyAlarmMonitor, mPeriodicAlarmMonitor,
108 getElapsedRealtimeSec(), [this](const ConfigKey& key) {
109 sp<IStatsCompanionService> sc = getStatsCompanionService();
110 auto receiver = mConfigManager->GetConfigReceiver(key);
111 if (sc == nullptr) {
112 VLOG("Could not find StatsCompanionService");
113 } else if (receiver == nullptr) {
114 VLOG("Statscompanion could not find a broadcast receiver for %s",
115 key.ToString().c_str());
116 } else {
117 sc->sendDataBroadcast(receiver);
David Chen1d7b0cd2017-11-15 14:20:04 -0800118 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800119 }
Yangster-mac330af582018-02-08 15:24:38 -0800120 );
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700121
122 mConfigManager->AddListener(mProcessor);
123
124 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700125}
126
Yao Chenef99c4f2017-09-22 16:26:54 -0700127StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700128}
129
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700130void StatsService::init_system_properties() {
131 mEngBuild = false;
132 const prop_info* buildType = __system_property_find("ro.build.type");
133 if (buildType != NULL) {
134 __system_property_read_callback(buildType, init_build_type_callback, this);
135 }
David Chen0656b7a2017-09-13 15:53:39 -0700136}
137
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700138void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
139 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700140 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700141 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
142 }
143}
144
145/**
146 * Implement our own because the default binder implementation isn't
147 * properly handling SHELL_COMMAND_TRANSACTION.
148 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700149status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
150 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700151 switch (code) {
152 case SHELL_COMMAND_TRANSACTION: {
153 int in = data.readFileDescriptor();
154 int out = data.readFileDescriptor();
155 int err = data.readFileDescriptor();
156 int argc = data.readInt32();
157 Vector<String8> args;
158 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
159 args.add(String8(data.readString16()));
160 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700161 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
162 sp<IResultReceiver> resultReceiver =
163 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700164
165 FILE* fin = fdopen(in, "r");
166 FILE* fout = fdopen(out, "w");
167 FILE* ferr = fdopen(err, "w");
168
169 if (fin == NULL || fout == NULL || ferr == NULL) {
170 resultReceiver->send(NO_MEMORY);
171 } else {
172 err = command(fin, fout, ferr, args);
173 resultReceiver->send(err);
174 }
175
176 if (fin != NULL) {
177 fflush(fin);
178 fclose(fin);
179 }
180 if (fout != NULL) {
181 fflush(fout);
182 fclose(fout);
183 }
184 if (fout != NULL) {
185 fflush(ferr);
186 fclose(ferr);
187 }
188
189 return NO_ERROR;
190 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700191 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700192 }
193}
194
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700195/**
196 * Write debugging data about statsd.
197 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700198status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700199 FILE* out = fdopen(fd, "w");
200 if (out == NULL) {
201 return NO_MEMORY; // the fd is already open
202 }
203
Yao Chen884c8c12018-01-26 10:36:25 -0800204 bool verbose = false;
205 if (args.size() > 0 && !args[0].compare(String16("-v"))) {
206 verbose = true;
207 }
208
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700209 // TODO: Proto format for incident reports
Yao Chen884c8c12018-01-26 10:36:25 -0800210 dump_impl(out, verbose);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700211
212 fclose(out);
213 return NO_ERROR;
214}
215
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700216/**
217 * Write debugging data about statsd in text format.
218 */
Yao Chen884c8c12018-01-26 10:36:25 -0800219void StatsService::dump_impl(FILE* out, bool verbose) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800220 StatsdStats::getInstance().dumpStats(out);
Yao Chen884c8c12018-01-26 10:36:25 -0800221 mProcessor->dumpStates(out, verbose);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700222}
223
224/**
225 * Implementation of the adb shell cmd stats command.
226 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700227status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700228 // TODO: Permission check
229
230 const int argCount = args.size();
231 if (argCount >= 1) {
232 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700233 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700234 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700235 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700236
David Chende701692017-10-05 13:16:02 -0700237 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800238 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700239 }
Yao Chen729093d2017-10-16 10:33:26 -0700240
241 if (!args[0].compare(String8("dump-report"))) {
242 return cmd_dump_report(out, err, args);
243 }
David Chen1481fe12017-10-16 13:16:34 -0700244
245 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
246 return cmd_print_pulled_metrics(out, args);
247 }
David Chenadaf8b32017-11-03 15:42:08 -0700248
249 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800250 return cmd_trigger_broadcast(out, args);
251 }
252
253 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800254 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700255 }
yro87d983c2017-11-14 21:31:43 -0800256
Yao Chen8d9989b2017-11-18 18:54:50 -0800257 if (!args[0].compare(String8("meminfo"))) {
258 return cmd_dump_memory_info(out);
259 }
yro947fbce2017-11-15 22:50:23 -0800260
261 if (!args[0].compare(String8("write-to-disk"))) {
262 return cmd_write_data_to_disk(out);
263 }
Bookatzb223c4e2018-02-01 15:35:04 -0800264
David Chen0b5c90c2018-01-25 16:51:49 -0800265 if (!args[0].compare(String8("log-app-breadcrumb"))) {
266 return cmd_log_app_breadcrumb(out, args);
Bookatzb223c4e2018-02-01 15:35:04 -0800267 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800268
269 if (!args[0].compare(String8("clear-puller-cache"))) {
270 return cmd_clear_puller_cache(out);
271 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700272 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700273
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700274 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700275 return NO_ERROR;
276}
277
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700278void StatsService::print_cmd_help(FILE* out) {
279 fprintf(out,
280 "usage: adb shell cmd stats print-stats-log [tag_required] "
281 "[timestamp_nsec_optional]\n");
282 fprintf(out, "\n");
283 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800284 fprintf(out, "usage: adb shell cmd stats meminfo\n");
285 fprintf(out, "\n");
286 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
287 fprintf(out, " # adb shell stop\n");
288 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
289 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
290 fprintf(out, " # adb shell start\n");
291 fprintf(out, "\n");
292 fprintf(out, "\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800293 fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700294 fprintf(out, "\n");
295 fprintf(out, " Prints the UID, app name, version mapping.\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800296 fprintf(out, " PKG Optional package name to print the uids of the package\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700297 fprintf(out, "\n");
298 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800299 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700300 fprintf(out, "\n");
301 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
302 fprintf(out, "\n");
303 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800304 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
305 fprintf(out, "\n");
306 fprintf(out, " Flushes all data on memory to disk.\n");
307 fprintf(out, "\n");
308 fprintf(out, "\n");
David Chen0b5c90c2018-01-25 16:51:49 -0800309 fprintf(out, "usage: adb shell cmd stats log-app-breadcrumb [UID] LABEL STATE\n");
310 fprintf(out, " Writes an AppBreadcrumbReported event to the statslog buffer.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800311 fprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
312 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
313 fprintf(out, " uid is used.\n");
314 fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
315 fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
316 fprintf(out, "\n");
317 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800318 fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700319 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
320 fprintf(out, "\n");
321 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
yro74fed972017-11-27 14:42:42 -0800322 fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
323 fprintf(out, " provided, then all configs will be removed from memory and disk.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700324 fprintf(out, "\n");
325 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
yro74fed972017-11-27 14:42:42 -0800326 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700327 fprintf(out, " uid is used.\n");
328 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700329 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800330 fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
331 fprintf(out, "\n be removed from memory and disk!\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700332 fprintf(out, "\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800333 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700334 fprintf(out, " Dump all metric data for a configuration.\n");
335 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
336 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
337 fprintf(out, " calling uid is used.\n");
338 fprintf(out, " NAME The name of the configuration\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800339 fprintf(out, " --proto Print proto binary.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700340 fprintf(out, "\n");
341 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800342 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
343 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
344 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
345 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
346 fprintf(out, " calling uid is used.\n");
347 fprintf(out, " NAME The name of the configuration\n");
348 fprintf(out, "\n");
349 fprintf(out, "\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800350 fprintf(out, "usage: adb shell cmd stats print-stats\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800351 fprintf(out, " Prints some basic stats.\n");
Chenjie Yufa22d652018-02-05 14:37:48 -0800352 fprintf(out, "\n");
353 fprintf(out, "\n");
354 fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
355 fprintf(out, " Clear cached puller data.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700356}
357
David Chen1d7b0cd2017-11-15 14:20:04 -0800358status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
359 string name;
360 bool good = false;
361 int uid;
362 const int argCount = args.size();
363 if (argCount == 2) {
364 // Automatically pick the UID
365 uid = IPCThreadState::self()->getCallingUid();
366 // TODO: What if this isn't a binder call? Should we fail?
367 name.assign(args[1].c_str(), args[1].size());
368 good = true;
369 } else if (argCount == 3) {
370 // If it's a userdebug or eng build, then the shell user can
371 // impersonate other uids.
372 if (mEngBuild) {
373 const char* s = args[1].c_str();
374 if (*s != '\0') {
375 char* end = NULL;
376 uid = strtol(s, &end, 0);
377 if (*end == '\0') {
378 name.assign(args[2].c_str(), args[2].size());
379 good = true;
380 }
381 }
382 } else {
383 fprintf(out,
384 "The metrics can only be dumped for other UIDs on eng or userdebug "
385 "builds.\n");
386 }
387 }
388 if (!good) {
389 print_cmd_help(out);
390 return UNKNOWN_ERROR;
391 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800392 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name)));
yro4d889e62017-11-17 15:44:48 -0800393 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen661f7912018-01-22 17:46:24 -0800394 if (sc == nullptr) {
395 VLOG("Could not access statsCompanion");
396 } else if (receiver == nullptr) {
397 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str())
398 } else {
399 sc->sendDataBroadcast(receiver);
yro74fed972017-11-27 14:42:42 -0800400 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
401 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800402 }
403
David Chenadaf8b32017-11-03 15:42:08 -0700404 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700405}
406
407status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
408 const int argCount = args.size();
409 if (argCount >= 2) {
410 if (args[1] == "update" || args[1] == "remove") {
411 bool good = false;
412 int uid = -1;
413 string name;
414
415 if (argCount == 3) {
416 // Automatically pick the UID
417 uid = IPCThreadState::self()->getCallingUid();
418 // TODO: What if this isn't a binder call? Should we fail?
419 name.assign(args[2].c_str(), args[2].size());
420 good = true;
421 } else if (argCount == 4) {
422 // If it's a userdebug or eng build, then the shell user can
423 // impersonate other uids.
424 if (mEngBuild) {
425 const char* s = args[2].c_str();
426 if (*s != '\0') {
427 char* end = NULL;
428 uid = strtol(s, &end, 0);
429 if (*end == '\0') {
430 name.assign(args[3].c_str(), args[3].size());
431 good = true;
432 }
433 }
434 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700435 fprintf(err,
436 "The config can only be set for other UIDs on eng or userdebug "
437 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700438 }
yroe5f82922018-01-22 18:37:27 -0800439 } else if (argCount == 2 && args[1] == "remove") {
440 good = true;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700441 }
442
443 if (!good) {
444 // If arg parsing failed, print the help text and return an error.
445 print_cmd_help(out);
446 return UNKNOWN_ERROR;
447 }
448
449 if (args[1] == "update") {
yro255f72e2018-02-26 15:15:17 -0800450 char* endp;
451 int64_t configID = strtoll(name.c_str(), &endp, 10);
452 if (endp == name.c_str() || *endp != '\0') {
453 fprintf(err, "Error parsing config ID.\n");
454 return UNKNOWN_ERROR;
455 }
456
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700457 // Read stream into buffer.
458 string buffer;
459 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
460 fprintf(err, "Error reading stream for StatsConfig.\n");
461 return UNKNOWN_ERROR;
462 }
463
464 // Parse buffer.
465 StatsdConfig config;
466 if (!config.ParseFromString(buffer)) {
467 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
468 return UNKNOWN_ERROR;
469 }
470
471 // Add / update the config.
yro255f72e2018-02-26 15:15:17 -0800472 mConfigManager->UpdateConfig(ConfigKey(uid, configID), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700473 } else {
yro74fed972017-11-27 14:42:42 -0800474 if (argCount == 2) {
475 cmd_remove_all_configs(out);
476 } else {
477 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800478 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800479 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700480 }
481
482 return NO_ERROR;
483 }
David Chen0656b7a2017-09-13 15:53:39 -0700484 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700485 print_cmd_help(out);
486 return UNKNOWN_ERROR;
487}
488
Yao Chen729093d2017-10-16 10:33:26 -0700489status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
490 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800491 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700492 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800493 bool proto = false;
Yao Chen729093d2017-10-16 10:33:26 -0700494 int uid;
495 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800496 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
497 proto = true;
498 argCount -= 1;
499 }
Yao Chen729093d2017-10-16 10:33:26 -0700500 if (argCount == 2) {
501 // Automatically pick the UID
502 uid = IPCThreadState::self()->getCallingUid();
503 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a3792017-10-30 22:57:06 -0700504 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700505 good = true;
506 } else if (argCount == 3) {
507 // If it's a userdebug or eng build, then the shell user can
508 // impersonate other uids.
509 if (mEngBuild) {
510 const char* s = args[1].c_str();
511 if (*s != '\0') {
512 char* end = NULL;
513 uid = strtol(s, &end, 0);
514 if (*end == '\0') {
515 name.assign(args[2].c_str(), args[2].size());
516 good = true;
517 }
518 }
519 } else {
520 fprintf(out,
521 "The metrics can only be dumped for other UIDs on eng or userdebug "
522 "builds.\n");
523 }
524 }
525 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800526 vector<uint8_t> data;
David Chen926fc752018-02-23 13:31:43 -0800527 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), getElapsedRealtimeNs(),
Yao Chen8a8d16c2018-02-08 14:50:40 -0800528 &data);
Yao Chen729093d2017-10-16 10:33:26 -0700529 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
Chenjie Yub236c862017-11-28 22:20:44 -0800530 if (proto) {
531 for (size_t i = 0; i < data.size(); i ++) {
532 fprintf(out, "%c", data[i]);
533 }
534 } else {
535 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
536 fprintf(out, "See the StatsLogReport in logcat...\n");
537 }
Yao Chen729093d2017-10-16 10:33:26 -0700538 return android::OK;
539 } else {
540 // If arg parsing failed, print the help text and return an error.
541 print_cmd_help(out);
542 return UNKNOWN_ERROR;
543 }
544 } else {
545 fprintf(out, "Log processor does not exist...\n");
546 return UNKNOWN_ERROR;
547 }
548}
549
Yao Chenb3561512017-11-21 18:07:17 -0800550status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800551 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
552 for (const ConfigKey& key : configs) {
553 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
554 mProcessor->GetMetricsSize(key));
555 }
Yao Chenb3561512017-11-21 18:07:17 -0800556 StatsdStats& statsdStats = StatsdStats::getInstance();
Yao Chenf5acabe2018-01-17 14:10:34 -0800557 statsdStats.dumpStats(out);
David Chen1d7b0cd2017-11-15 14:20:04 -0800558 return NO_ERROR;
559}
560
Yao Chend10f7b12017-12-18 12:53:50 -0800561status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) {
562 if (args.size() > 1) {
563 string pkg;
564 pkg.assign(args[1].c_str(), args[1].size());
565 auto uids = mUidMap->getAppUid(pkg);
566 fprintf(out, "%s -> [ ", pkg.c_str());
567 for (const auto& uid : uids) {
568 fprintf(out, "%d ", uid);
569 }
570 fprintf(out, "]\n");
571 } else {
572 mUidMap->printUidMap(out);
573 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700574 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700575}
576
yro947fbce2017-11-15 22:50:23 -0800577status_t StatsService::cmd_write_data_to_disk(FILE* out) {
578 fprintf(out, "Writing data to disk\n");
579 mProcessor->WriteDataToDisk();
580 return NO_ERROR;
581}
582
David Chen0b5c90c2018-01-25 16:51:49 -0800583status_t StatsService::cmd_log_app_breadcrumb(FILE* out, const Vector<String8>& args) {
Bookatzb223c4e2018-02-01 15:35:04 -0800584 bool good = false;
585 int32_t uid;
586 int32_t label;
587 int32_t state;
588 const int argCount = args.size();
589 if (argCount == 3) {
590 // Automatically pick the UID
591 uid = IPCThreadState::self()->getCallingUid();
592 label = atoi(args[1].c_str());
593 state = atoi(args[2].c_str());
594 good = true;
595 } else if (argCount == 4) {
596 uid = atoi(args[1].c_str());
597 // If it's a userdebug or eng build, then the shell user can impersonate other uids.
598 // Otherwise, the uid must match the actual caller's uid.
599 if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) {
600 label = atoi(args[2].c_str());
601 state = atoi(args[3].c_str());
602 good = true;
603 } else {
604 fprintf(out,
David Chenb639d142018-02-14 17:29:54 -0800605 "Selecting a UID for writing AppBreadcrumb can only be done for other UIDs "
606 "on eng or userdebug builds.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800607 }
608 }
609 if (good) {
David Chen0b5c90c2018-01-25 16:51:49 -0800610 fprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state);
611 android::util::stats_write(android::util::APP_BREADCRUMB_REPORTED, uid, label, state);
Bookatzb223c4e2018-02-01 15:35:04 -0800612 } else {
613 print_cmd_help(out);
614 return UNKNOWN_ERROR;
615 }
616 return NO_ERROR;
617}
618
David Chen1481fe12017-10-16 13:16:34 -0700619status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
620 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700621 vector<shared_ptr<LogEvent> > stats;
622 if (mStatsPullerManager.Pull(s, &stats)) {
623 for (const auto& it : stats) {
624 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
625 }
626 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
627 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700628 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700629 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700630}
631
yro74fed972017-11-27 14:42:42 -0800632status_t StatsService::cmd_remove_all_configs(FILE* out) {
633 fprintf(out, "Removing all configs...\n");
634 VLOG("StatsService::cmd_remove_all_configs was called");
635 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800636 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800637 return NO_ERROR;
638}
639
Yao Chen8d9989b2017-11-18 18:54:50 -0800640status_t StatsService::cmd_dump_memory_info(FILE* out) {
641 std::string s = dumpMemInfo(100);
642 fprintf(out, "Memory Info\n");
643 fprintf(out, "%s", s.c_str());
644 return NO_ERROR;
645}
646
Chenjie Yue72252b2018-02-01 13:19:35 -0800647status_t StatsService::cmd_clear_puller_cache(FILE* out) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800648 IPCThreadState* ipc = IPCThreadState::self();
Yangster-mac932ecec2018-02-01 10:23:52 -0800649 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i",
650 ipc->getCallingPid(), ipc->getCallingUid());
Chenjie Yufa22d652018-02-05 14:37:48 -0800651 if (checkCallingPermission(String16(kPermissionDump))) {
652 int cleared = mStatsPullerManager.ForceClearPullerCache();
653 fprintf(out, "Puller removed %d cached data!\n", cleared);
654 return NO_ERROR;
655 } else {
656 return PERMISSION_DENIED;
657 }
Chenjie Yue72252b2018-02-01 13:19:35 -0800658}
659
Dianne Hackborn3accca02013-09-20 09:32:11 -0700660Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
David Chende701692017-10-05 13:16:02 -0700661 const vector<String16>& app) {
yro74fed972017-11-27 14:42:42 -0800662 VLOG("StatsService::informAllUidData was called");
David Chende701692017-10-05 13:16:02 -0700663
664 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
665 return Status::fromExceptionCode(Status::EX_SECURITY,
666 "Only system uid can call informAllUidData");
667 }
668
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700669 mUidMap->updateMap(uid, version, app);
yro74fed972017-11-27 14:42:42 -0800670 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700671
672 return Status::ok();
673}
674
Dianne Hackborn3accca02013-09-20 09:32:11 -0700675Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) {
yro74fed972017-11-27 14:42:42 -0800676 VLOG("StatsService::informOnePackage was called");
David Chende701692017-10-05 13:16:02 -0700677
678 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
679 return Status::fromExceptionCode(Status::EX_SECURITY,
680 "Only system uid can call informOnePackage");
681 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700682 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700683 return Status::ok();
684}
685
686Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
yro74fed972017-11-27 14:42:42 -0800687 VLOG("StatsService::informOnePackageRemoved was called");
David Chende701692017-10-05 13:16:02 -0700688
689 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
690 return Status::fromExceptionCode(Status::EX_SECURITY,
691 "Only system uid can call informOnePackageRemoved");
692 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700693 mUidMap->removeApp(app, uid);
yro01924022018-02-20 18:20:49 -0800694 mConfigManager->RemoveConfigs(uid);
David Chende701692017-10-05 13:16:02 -0700695 return Status::ok();
696}
697
Yao Chenef99c4f2017-09-22 16:26:54 -0700698Status StatsService::informAnomalyAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800699 VLOG("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700700
701 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
702 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700703 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700704 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800705
Yangster-mac330af582018-02-08 15:24:38 -0800706 uint64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800707 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
708 mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
709 if (alarmSet.size() > 0) {
Bookatz66fe0612018-02-07 18:51:48 -0800710 VLOG("Found an anomaly alarm that fired.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800711 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
Bookatz66fe0612018-02-07 18:51:48 -0800712 } else {
713 VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
714 }
Bookatz1b0b1142017-09-08 11:58:42 -0700715 return Status::ok();
716}
717
Yangster-mac932ecec2018-02-01 10:23:52 -0800718Status StatsService::informAlarmForSubscriberTriggeringFired() {
719 VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
720
721 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
722 return Status::fromExceptionCode(
723 Status::EX_SECURITY,
724 "Only system uid can call informAlarmForSubscriberTriggeringFired");
725 }
726
727 uint64_t currentTimeSec = time(nullptr);
728 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
729 mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
730 if (alarmSet.size() > 0) {
731 VLOG("Found periodic alarm fired.");
732 mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
733 } else {
734 ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
735 }
736 return Status::ok();
737}
738
Yao Chenef99c4f2017-09-22 16:26:54 -0700739Status StatsService::informPollAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800740 VLOG("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700741
742 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
743 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700744 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700745 }
746
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700747 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700748
yro74fed972017-11-27 14:42:42 -0800749 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700750
751 return Status::ok();
752}
753
Yao Chenef99c4f2017-09-22 16:26:54 -0700754Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700755 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
756 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700757 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700758 }
759
760 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800761 VLOG("StatsService::systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700762
Bookatzb487b552017-09-18 11:26:01 -0700763 sayHiToStatsCompanion();
764
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700765 return Status::ok();
766}
767
yro947fbce2017-11-15 22:50:23 -0800768Status StatsService::writeDataToDisk() {
769 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
770 return Status::fromExceptionCode(Status::EX_SECURITY,
771 "Only system uid can call systemRunning");
772 }
773
yro74fed972017-11-27 14:42:42 -0800774 VLOG("StatsService::writeDataToDisk");
yro947fbce2017-11-15 22:50:23 -0800775
776 mProcessor->WriteDataToDisk();
777
778 return Status::ok();
779}
780
Yao Chenef99c4f2017-09-22 16:26:54 -0700781void StatsService::sayHiToStatsCompanion() {
782 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
783 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700784 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
785 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800786 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700787 statsCompanion->statsdReady();
788 } else {
yro74fed972017-11-27 14:42:42 -0800789 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700790 }
791}
792
Yao Chenef99c4f2017-09-22 16:26:54 -0700793sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700794 sp<IStatsCompanionService> statsCompanion = nullptr;
795 // Get statscompanion service from service manager
796 const sp<IServiceManager> sm(defaultServiceManager());
797 if (sm != nullptr) {
798 const String16 name("statscompanion");
799 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
800 if (statsCompanion == nullptr) {
801 ALOGW("statscompanion service unavailable!");
802 return nullptr;
803 }
804 }
805 return statsCompanion;
806}
807
Yao Chenef99c4f2017-09-22 16:26:54 -0700808Status StatsService::statsCompanionReady() {
yro74fed972017-11-27 14:42:42 -0800809 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700810
811 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
812 return Status::fromExceptionCode(Status::EX_SECURITY,
813 "Only system uid can call statsCompanionReady");
814 }
815
816 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
817 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700818 return Status::fromExceptionCode(
819 Status::EX_NULL_POINTER,
820 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700821 }
yro74fed972017-11-27 14:42:42 -0800822 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800823 IInterface::asBinder(statsCompanion)->linkToDeath(
824 new CompanionDeathRecipient(mAnomalyAlarmMonitor, mPeriodicAlarmMonitor));
825 mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion);
826 mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800827 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700828 return Status::ok();
829}
830
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700831void StatsService::Startup() {
832 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700833}
834
Yangster-macd40053e2018-01-09 16:29:22 -0800835void StatsService::OnLogEvent(LogEvent* event) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700836 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700837}
838
Yangster-mac94e197c2018-01-02 16:03:03 -0800839Status StatsService::getData(int64_t key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700840 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800841 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800842 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800843 ConfigKey configKey(ipc->getCallingUid(), key);
David Chen926fc752018-02-23 13:31:43 -0800844 mProcessor->onDumpReport(configKey, getElapsedRealtimeNs(), output);
David Chenadaf8b32017-11-03 15:42:08 -0700845 return Status::ok();
846 } else {
847 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
848 }
yro31eb67b2017-10-24 13:33:21 -0700849}
850
David Chen2e8f3802017-11-22 10:56:48 -0800851Status StatsService::getMetadata(vector<uint8_t>* output) {
852 IPCThreadState* ipc = IPCThreadState::self();
853 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
854 ipc->getCallingUid());
855 if (checkCallingPermission(String16(kPermissionDump))) {
856 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
857 return Status::ok();
858 } else {
859 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
860 }
861}
862
Yangster-mac94e197c2018-01-02 16:03:03 -0800863Status StatsService::addConfiguration(int64_t key,
David Chenadaf8b32017-11-03 15:42:08 -0700864 const vector <uint8_t>& config,
David Chenadaf8b32017-11-03 15:42:08 -0700865 bool* success) {
866 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800867 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800868 ConfigKey configKey(ipc->getCallingUid(), key);
David Chenadaf8b32017-11-03 15:42:08 -0700869 StatsdConfig cfg;
Yangster-macbbd056a2018-01-22 13:37:02 -0800870 if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) {
David Chen7d8aa4d2017-12-27 13:37:01 -0800871 *success = false;
872 return Status::ok();
873 }
David Chenadaf8b32017-11-03 15:42:08 -0700874 mConfigManager->UpdateConfig(configKey, cfg);
David Chen661f7912018-01-22 17:46:24 -0800875 *success = true;
876 return Status::ok();
877 } else {
878 *success = false;
879 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
880 }
881}
882
883Status StatsService::removeDataFetchOperation(int64_t key, bool* success) {
884 IPCThreadState* ipc = IPCThreadState::self();
885 if (checkCallingPermission(String16(kPermissionDump))) {
886 ConfigKey configKey(ipc->getCallingUid(), key);
887 mConfigManager->RemoveConfigReceiver(configKey);
888 *success = true;
889 return Status::ok();
890 } else {
891 *success = false;
892 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
893 }
894}
895
896Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender,
897 bool* success) {
898 IPCThreadState* ipc = IPCThreadState::self();
899 if (checkCallingPermission(String16(kPermissionDump))) {
900 ConfigKey configKey(ipc->getCallingUid(), key);
901 mConfigManager->SetConfigReceiver(configKey, intentSender);
David Chenadaf8b32017-11-03 15:42:08 -0700902 *success = true;
903 return Status::ok();
904 } else {
Yao Chenaa39bc72017-12-01 11:16:50 -0800905 *success = false;
David Chenadaf8b32017-11-03 15:42:08 -0700906 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700907 }
yro31eb67b2017-10-24 13:33:21 -0700908}
909
Yangster-mac94e197c2018-01-02 16:03:03 -0800910Status StatsService::removeConfiguration(int64_t key, bool* success) {
David Chenadaf8b32017-11-03 15:42:08 -0700911 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800912 if (checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800913 ConfigKey configKey(ipc->getCallingUid(), key);
914 mConfigManager->RemoveConfig(configKey);
915 SubscriberReporter::getInstance().removeConfig(configKey);
Yao Chenaa39bc72017-12-01 11:16:50 -0800916 *success = true;
David Chenadaf8b32017-11-03 15:42:08 -0700917 return Status::ok();
918 } else {
919 *success = false;
920 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700921 }
yro31eb67b2017-10-24 13:33:21 -0700922}
923
Bookatzc6977972018-01-16 16:55:05 -0800924Status StatsService::setBroadcastSubscriber(int64_t configId,
925 int64_t subscriberId,
926 const sp<android::IBinder>& intentSender,
927 bool* success) {
928 VLOG("StatsService::setBroadcastSubscriber called.");
929 IPCThreadState* ipc = IPCThreadState::self();
930 if (checkCallingPermission(String16(kPermissionDump))) {
931 ConfigKey configKey(ipc->getCallingUid(), configId);
932 SubscriberReporter::getInstance()
933 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
934 *success = true;
935 return Status::ok();
936 } else {
937 *success = false;
938 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
939 }
940}
941
942Status StatsService::unsetBroadcastSubscriber(int64_t configId,
943 int64_t subscriberId,
944 bool* success) {
945 VLOG("StatsService::unsetBroadcastSubscriber called.");
946 IPCThreadState* ipc = IPCThreadState::self();
947 if (checkCallingPermission(String16(kPermissionDump))) {
948 ConfigKey configKey(ipc->getCallingUid(), configId);
949 SubscriberReporter::getInstance()
950 .unsetBroadcastSubscriber(configKey, subscriberId);
951 *success = true;
952 return Status::ok();
953 } else {
954 *success = false;
955 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
956 }
957}
958
959
David Chen1d7b0cd2017-11-15 14:20:04 -0800960void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700961}
962
Yao Chenef99c4f2017-09-22 16:26:54 -0700963} // namespace statsd
964} // namespace os
965} // namespace android