blob: 573b7c9e8eb2dd6582529c114a528d40151ecdfb [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") {
450 // Read stream into buffer.
451 string buffer;
452 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
453 fprintf(err, "Error reading stream for StatsConfig.\n");
454 return UNKNOWN_ERROR;
455 }
456
457 // Parse buffer.
458 StatsdConfig config;
459 if (!config.ParseFromString(buffer)) {
460 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
461 return UNKNOWN_ERROR;
462 }
463
464 // Add / update the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800465 mConfigManager->UpdateConfig(ConfigKey(uid, StrToInt64(name)), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700466 } else {
yro74fed972017-11-27 14:42:42 -0800467 if (argCount == 2) {
468 cmd_remove_all_configs(out);
469 } else {
470 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800471 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800472 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700473 }
474
475 return NO_ERROR;
476 }
David Chen0656b7a2017-09-13 15:53:39 -0700477 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700478 print_cmd_help(out);
479 return UNKNOWN_ERROR;
480}
481
Yao Chen729093d2017-10-16 10:33:26 -0700482status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
483 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800484 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700485 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800486 bool proto = false;
Yao Chen729093d2017-10-16 10:33:26 -0700487 int uid;
488 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800489 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
490 proto = true;
491 argCount -= 1;
492 }
Yao Chen729093d2017-10-16 10:33:26 -0700493 if (argCount == 2) {
494 // Automatically pick the UID
495 uid = IPCThreadState::self()->getCallingUid();
496 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a3792017-10-30 22:57:06 -0700497 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700498 good = true;
499 } else if (argCount == 3) {
500 // If it's a userdebug or eng build, then the shell user can
501 // impersonate other uids.
502 if (mEngBuild) {
503 const char* s = args[1].c_str();
504 if (*s != '\0') {
505 char* end = NULL;
506 uid = strtol(s, &end, 0);
507 if (*end == '\0') {
508 name.assign(args[2].c_str(), args[2].size());
509 good = true;
510 }
511 }
512 } else {
513 fprintf(out,
514 "The metrics can only be dumped for other UIDs on eng or userdebug "
515 "builds.\n");
516 }
517 }
518 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800519 vector<uint8_t> data;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800520 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), time(nullptr) * NS_PER_SEC,
521 &data);
Yao Chen729093d2017-10-16 10:33:26 -0700522 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
Chenjie Yub236c862017-11-28 22:20:44 -0800523 if (proto) {
524 for (size_t i = 0; i < data.size(); i ++) {
525 fprintf(out, "%c", data[i]);
526 }
527 } else {
528 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
529 fprintf(out, "See the StatsLogReport in logcat...\n");
530 }
Yao Chen729093d2017-10-16 10:33:26 -0700531 return android::OK;
532 } else {
533 // If arg parsing failed, print the help text and return an error.
534 print_cmd_help(out);
535 return UNKNOWN_ERROR;
536 }
537 } else {
538 fprintf(out, "Log processor does not exist...\n");
539 return UNKNOWN_ERROR;
540 }
541}
542
Yao Chenb3561512017-11-21 18:07:17 -0800543status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800544 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
545 for (const ConfigKey& key : configs) {
546 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
547 mProcessor->GetMetricsSize(key));
548 }
Yao Chenb3561512017-11-21 18:07:17 -0800549 StatsdStats& statsdStats = StatsdStats::getInstance();
Yao Chenf5acabe2018-01-17 14:10:34 -0800550 statsdStats.dumpStats(out);
David Chen1d7b0cd2017-11-15 14:20:04 -0800551 return NO_ERROR;
552}
553
Yao Chend10f7b12017-12-18 12:53:50 -0800554status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) {
555 if (args.size() > 1) {
556 string pkg;
557 pkg.assign(args[1].c_str(), args[1].size());
558 auto uids = mUidMap->getAppUid(pkg);
559 fprintf(out, "%s -> [ ", pkg.c_str());
560 for (const auto& uid : uids) {
561 fprintf(out, "%d ", uid);
562 }
563 fprintf(out, "]\n");
564 } else {
565 mUidMap->printUidMap(out);
566 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700567 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700568}
569
yro947fbce2017-11-15 22:50:23 -0800570status_t StatsService::cmd_write_data_to_disk(FILE* out) {
571 fprintf(out, "Writing data to disk\n");
572 mProcessor->WriteDataToDisk();
573 return NO_ERROR;
574}
575
David Chen0b5c90c2018-01-25 16:51:49 -0800576status_t StatsService::cmd_log_app_breadcrumb(FILE* out, const Vector<String8>& args) {
Bookatzb223c4e2018-02-01 15:35:04 -0800577 bool good = false;
578 int32_t uid;
579 int32_t label;
580 int32_t state;
581 const int argCount = args.size();
582 if (argCount == 3) {
583 // Automatically pick the UID
584 uid = IPCThreadState::self()->getCallingUid();
585 label = atoi(args[1].c_str());
586 state = atoi(args[2].c_str());
587 good = true;
588 } else if (argCount == 4) {
589 uid = atoi(args[1].c_str());
590 // If it's a userdebug or eng build, then the shell user can impersonate other uids.
591 // Otherwise, the uid must match the actual caller's uid.
592 if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) {
593 label = atoi(args[2].c_str());
594 state = atoi(args[3].c_str());
595 good = true;
596 } else {
597 fprintf(out,
David Chenb639d142018-02-14 17:29:54 -0800598 "Selecting a UID for writing AppBreadcrumb can only be done for other UIDs "
599 "on eng or userdebug builds.\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800600 }
601 }
602 if (good) {
David Chen0b5c90c2018-01-25 16:51:49 -0800603 fprintf(out, "Logging AppBreadcrumbReported(%d, %d, %d) to statslog.\n", uid, label, state);
604 android::util::stats_write(android::util::APP_BREADCRUMB_REPORTED, uid, label, state);
Bookatzb223c4e2018-02-01 15:35:04 -0800605 } else {
606 print_cmd_help(out);
607 return UNKNOWN_ERROR;
608 }
609 return NO_ERROR;
610}
611
David Chen1481fe12017-10-16 13:16:34 -0700612status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
613 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700614 vector<shared_ptr<LogEvent> > stats;
615 if (mStatsPullerManager.Pull(s, &stats)) {
616 for (const auto& it : stats) {
617 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
618 }
619 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
620 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700621 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700622 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700623}
624
yro74fed972017-11-27 14:42:42 -0800625status_t StatsService::cmd_remove_all_configs(FILE* out) {
626 fprintf(out, "Removing all configs...\n");
627 VLOG("StatsService::cmd_remove_all_configs was called");
628 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800629 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800630 return NO_ERROR;
631}
632
Yao Chen8d9989b2017-11-18 18:54:50 -0800633status_t StatsService::cmd_dump_memory_info(FILE* out) {
634 std::string s = dumpMemInfo(100);
635 fprintf(out, "Memory Info\n");
636 fprintf(out, "%s", s.c_str());
637 return NO_ERROR;
638}
639
Chenjie Yue72252b2018-02-01 13:19:35 -0800640status_t StatsService::cmd_clear_puller_cache(FILE* out) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800641 IPCThreadState* ipc = IPCThreadState::self();
Yangster-mac932ecec2018-02-01 10:23:52 -0800642 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i",
643 ipc->getCallingPid(), ipc->getCallingUid());
Chenjie Yufa22d652018-02-05 14:37:48 -0800644 if (checkCallingPermission(String16(kPermissionDump))) {
645 int cleared = mStatsPullerManager.ForceClearPullerCache();
646 fprintf(out, "Puller removed %d cached data!\n", cleared);
647 return NO_ERROR;
648 } else {
649 return PERMISSION_DENIED;
650 }
Chenjie Yue72252b2018-02-01 13:19:35 -0800651}
652
Dianne Hackborn3accca02013-09-20 09:32:11 -0700653Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
David Chende701692017-10-05 13:16:02 -0700654 const vector<String16>& app) {
yro74fed972017-11-27 14:42:42 -0800655 VLOG("StatsService::informAllUidData was called");
David Chende701692017-10-05 13:16:02 -0700656
657 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
658 return Status::fromExceptionCode(Status::EX_SECURITY,
659 "Only system uid can call informAllUidData");
660 }
661
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700662 mUidMap->updateMap(uid, version, app);
yro74fed972017-11-27 14:42:42 -0800663 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700664
665 return Status::ok();
666}
667
Dianne Hackborn3accca02013-09-20 09:32:11 -0700668Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) {
yro74fed972017-11-27 14:42:42 -0800669 VLOG("StatsService::informOnePackage was called");
David Chende701692017-10-05 13:16:02 -0700670
671 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
672 return Status::fromExceptionCode(Status::EX_SECURITY,
673 "Only system uid can call informOnePackage");
674 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700675 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700676 return Status::ok();
677}
678
679Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
yro74fed972017-11-27 14:42:42 -0800680 VLOG("StatsService::informOnePackageRemoved was called");
David Chende701692017-10-05 13:16:02 -0700681
682 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
683 return Status::fromExceptionCode(Status::EX_SECURITY,
684 "Only system uid can call informOnePackageRemoved");
685 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700686 mUidMap->removeApp(app, uid);
yro01924022018-02-20 18:20:49 -0800687 mConfigManager->RemoveConfigs(uid);
David Chende701692017-10-05 13:16:02 -0700688 return Status::ok();
689}
690
Yao Chenef99c4f2017-09-22 16:26:54 -0700691Status StatsService::informAnomalyAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800692 VLOG("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700693
694 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
695 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700696 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700697 }
Yangster-mac932ecec2018-02-01 10:23:52 -0800698
Yangster-mac330af582018-02-08 15:24:38 -0800699 uint64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mac932ecec2018-02-01 10:23:52 -0800700 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
701 mAnomalyAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
702 if (alarmSet.size() > 0) {
Bookatz66fe0612018-02-07 18:51:48 -0800703 VLOG("Found an anomaly alarm that fired.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800704 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
Bookatz66fe0612018-02-07 18:51:48 -0800705 } else {
706 VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
707 }
Bookatz1b0b1142017-09-08 11:58:42 -0700708 return Status::ok();
709}
710
Yangster-mac932ecec2018-02-01 10:23:52 -0800711Status StatsService::informAlarmForSubscriberTriggeringFired() {
712 VLOG("StatsService::informAlarmForSubscriberTriggeringFired was called");
713
714 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
715 return Status::fromExceptionCode(
716 Status::EX_SECURITY,
717 "Only system uid can call informAlarmForSubscriberTriggeringFired");
718 }
719
720 uint64_t currentTimeSec = time(nullptr);
721 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet =
722 mPeriodicAlarmMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
723 if (alarmSet.size() > 0) {
724 VLOG("Found periodic alarm fired.");
725 mProcessor->onPeriodicAlarmFired(currentTimeSec * NS_PER_SEC, alarmSet);
726 } else {
727 ALOGW("Cannot find an periodic alarm that fired. Perhaps it was recently cancelled.");
728 }
729 return Status::ok();
730}
731
Yao Chenef99c4f2017-09-22 16:26:54 -0700732Status StatsService::informPollAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800733 VLOG("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700734
735 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
736 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700737 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700738 }
739
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700740 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700741
yro74fed972017-11-27 14:42:42 -0800742 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700743
744 return Status::ok();
745}
746
Yao Chenef99c4f2017-09-22 16:26:54 -0700747Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700748 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
749 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700750 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700751 }
752
753 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800754 VLOG("StatsService::systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700755
Bookatzb487b552017-09-18 11:26:01 -0700756 sayHiToStatsCompanion();
757
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700758 return Status::ok();
759}
760
yro947fbce2017-11-15 22:50:23 -0800761Status StatsService::writeDataToDisk() {
762 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
763 return Status::fromExceptionCode(Status::EX_SECURITY,
764 "Only system uid can call systemRunning");
765 }
766
yro74fed972017-11-27 14:42:42 -0800767 VLOG("StatsService::writeDataToDisk");
yro947fbce2017-11-15 22:50:23 -0800768
769 mProcessor->WriteDataToDisk();
770
771 return Status::ok();
772}
773
Yao Chenef99c4f2017-09-22 16:26:54 -0700774void StatsService::sayHiToStatsCompanion() {
775 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
776 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700777 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
778 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800779 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700780 statsCompanion->statsdReady();
781 } else {
yro74fed972017-11-27 14:42:42 -0800782 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700783 }
784}
785
Yao Chenef99c4f2017-09-22 16:26:54 -0700786sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700787 sp<IStatsCompanionService> statsCompanion = nullptr;
788 // Get statscompanion service from service manager
789 const sp<IServiceManager> sm(defaultServiceManager());
790 if (sm != nullptr) {
791 const String16 name("statscompanion");
792 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
793 if (statsCompanion == nullptr) {
794 ALOGW("statscompanion service unavailable!");
795 return nullptr;
796 }
797 }
798 return statsCompanion;
799}
800
Yao Chenef99c4f2017-09-22 16:26:54 -0700801Status StatsService::statsCompanionReady() {
yro74fed972017-11-27 14:42:42 -0800802 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700803
804 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
805 return Status::fromExceptionCode(Status::EX_SECURITY,
806 "Only system uid can call statsCompanionReady");
807 }
808
809 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
810 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700811 return Status::fromExceptionCode(
812 Status::EX_NULL_POINTER,
813 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700814 }
yro74fed972017-11-27 14:42:42 -0800815 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Yangster-mac932ecec2018-02-01 10:23:52 -0800816 IInterface::asBinder(statsCompanion)->linkToDeath(
817 new CompanionDeathRecipient(mAnomalyAlarmMonitor, mPeriodicAlarmMonitor));
818 mAnomalyAlarmMonitor->setStatsCompanionService(statsCompanion);
819 mPeriodicAlarmMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800820 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700821 return Status::ok();
822}
823
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700824void StatsService::Startup() {
825 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700826}
827
Yangster-macd40053e2018-01-09 16:29:22 -0800828void StatsService::OnLogEvent(LogEvent* event) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700829 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700830}
831
Yangster-mac94e197c2018-01-02 16:03:03 -0800832Status StatsService::getData(int64_t key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700833 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800834 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800835 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800836 ConfigKey configKey(ipc->getCallingUid(), key);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800837 mProcessor->onDumpReport(configKey, time(nullptr) * NS_PER_SEC, output);
David Chenadaf8b32017-11-03 15:42:08 -0700838 return Status::ok();
839 } else {
840 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
841 }
yro31eb67b2017-10-24 13:33:21 -0700842}
843
David Chen2e8f3802017-11-22 10:56:48 -0800844Status StatsService::getMetadata(vector<uint8_t>* output) {
845 IPCThreadState* ipc = IPCThreadState::self();
846 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
847 ipc->getCallingUid());
848 if (checkCallingPermission(String16(kPermissionDump))) {
849 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
850 return Status::ok();
851 } else {
852 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
853 }
854}
855
Yangster-mac94e197c2018-01-02 16:03:03 -0800856Status StatsService::addConfiguration(int64_t key,
David Chenadaf8b32017-11-03 15:42:08 -0700857 const vector <uint8_t>& config,
David Chenadaf8b32017-11-03 15:42:08 -0700858 bool* success) {
859 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800860 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800861 ConfigKey configKey(ipc->getCallingUid(), key);
David Chenadaf8b32017-11-03 15:42:08 -0700862 StatsdConfig cfg;
Yangster-macbbd056a2018-01-22 13:37:02 -0800863 if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) {
David Chen7d8aa4d2017-12-27 13:37:01 -0800864 *success = false;
865 return Status::ok();
866 }
David Chenadaf8b32017-11-03 15:42:08 -0700867 mConfigManager->UpdateConfig(configKey, cfg);
David Chen661f7912018-01-22 17:46:24 -0800868 *success = true;
869 return Status::ok();
870 } else {
871 *success = false;
872 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
873 }
874}
875
876Status StatsService::removeDataFetchOperation(int64_t key, bool* success) {
877 IPCThreadState* ipc = IPCThreadState::self();
878 if (checkCallingPermission(String16(kPermissionDump))) {
879 ConfigKey configKey(ipc->getCallingUid(), key);
880 mConfigManager->RemoveConfigReceiver(configKey);
881 *success = true;
882 return Status::ok();
883 } else {
884 *success = false;
885 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
886 }
887}
888
889Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender,
890 bool* success) {
891 IPCThreadState* ipc = IPCThreadState::self();
892 if (checkCallingPermission(String16(kPermissionDump))) {
893 ConfigKey configKey(ipc->getCallingUid(), key);
894 mConfigManager->SetConfigReceiver(configKey, intentSender);
David Chenadaf8b32017-11-03 15:42:08 -0700895 *success = true;
896 return Status::ok();
897 } else {
Yao Chenaa39bc72017-12-01 11:16:50 -0800898 *success = false;
David Chenadaf8b32017-11-03 15:42:08 -0700899 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700900 }
yro31eb67b2017-10-24 13:33:21 -0700901}
902
Yangster-mac94e197c2018-01-02 16:03:03 -0800903Status StatsService::removeConfiguration(int64_t key, bool* success) {
David Chenadaf8b32017-11-03 15:42:08 -0700904 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800905 if (checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800906 ConfigKey configKey(ipc->getCallingUid(), key);
907 mConfigManager->RemoveConfig(configKey);
908 SubscriberReporter::getInstance().removeConfig(configKey);
Yao Chenaa39bc72017-12-01 11:16:50 -0800909 *success = true;
David Chenadaf8b32017-11-03 15:42:08 -0700910 return Status::ok();
911 } else {
912 *success = false;
913 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700914 }
yro31eb67b2017-10-24 13:33:21 -0700915}
916
Bookatzc6977972018-01-16 16:55:05 -0800917Status StatsService::setBroadcastSubscriber(int64_t configId,
918 int64_t subscriberId,
919 const sp<android::IBinder>& intentSender,
920 bool* success) {
921 VLOG("StatsService::setBroadcastSubscriber called.");
922 IPCThreadState* ipc = IPCThreadState::self();
923 if (checkCallingPermission(String16(kPermissionDump))) {
924 ConfigKey configKey(ipc->getCallingUid(), configId);
925 SubscriberReporter::getInstance()
926 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
927 *success = true;
928 return Status::ok();
929 } else {
930 *success = false;
931 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
932 }
933}
934
935Status StatsService::unsetBroadcastSubscriber(int64_t configId,
936 int64_t subscriberId,
937 bool* success) {
938 VLOG("StatsService::unsetBroadcastSubscriber called.");
939 IPCThreadState* ipc = IPCThreadState::self();
940 if (checkCallingPermission(String16(kPermissionDump))) {
941 ConfigKey configKey(ipc->getCallingUid(), configId);
942 SubscriberReporter::getInstance()
943 .unsetBroadcastSubscriber(configKey, subscriberId);
944 *success = true;
945 return Status::ok();
946 } else {
947 *success = false;
948 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
949 }
950}
951
952
David Chen1d7b0cd2017-11-15 14:20:04 -0800953void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700954}
955
Yao Chenef99c4f2017-09-22 16:26:54 -0700956} // namespace statsd
957} // namespace os
958} // namespace android