blob: f24bb04344238d558074a9fe1646ea254c678779 [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
yro74fed972017-11-27 14:42:42 -080017#define DEBUG true // 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"
Yao Chen8d9989b2017-11-18 18:54:50 -080021#include "android-base/stringprintf.h"
David Chenadaf8b32017-11-03 15:42:08 -070022#include "config/ConfigKey.h"
23#include "config/ConfigManager.h"
Yao Chen8d9989b2017-11-18 18:54:50 -080024#include "guardrail/MemoryLeakTrackUtil.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/**
54 * Watches for the death of the stats companion (system process).
55 */
56class CompanionDeathRecipient : public IBinder::DeathRecipient {
57public:
58 CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor);
59 virtual void binderDied(const wp<IBinder>& who);
60
61private:
62 const sp<AnomalyMonitor> mAnomalyMonitor;
63};
64
65CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor)
66 : mAnomalyMonitor(anomalyMonitor) {
67}
68
69void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) {
70 ALOGW("statscompanion service died");
71 mAnomalyMonitor->setStatsCompanionService(nullptr);
Bookatzc6977972018-01-16 16:55:05 -080072 SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070073}
74
75// ======================================================================
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070076StatsService::StatsService(const sp<Looper>& handlerLooper)
Bookatz1d0136d2017-12-01 11:13:32 -080077 : mAnomalyMonitor(new AnomalyMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS))
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070078{
Joe Onorato9fc9edf2017-10-15 20:08:52 -070079 mUidMap = new UidMap();
Chenjie Yu80f91122018-01-31 20:24:50 -080080 StatsPuller::SetUidMap(mUidMap);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070081 mConfigManager = new ConfigManager();
Yangster-mac20877162017-12-22 17:19:39 -080082 mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, time(nullptr), [this](const ConfigKey& key) {
yro947fbce2017-11-15 22:50:23 -080083 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen1d7b0cd2017-11-15 14:20:04 -080084 auto receiver = mConfigManager->GetConfigReceiver(key);
85 if (sc == nullptr) {
yro74fed972017-11-27 14:42:42 -080086 VLOG("Could not find StatsCompanionService");
David Chen1d7b0cd2017-11-15 14:20:04 -080087 } else if (receiver.first.size() == 0) {
yro74fed972017-11-27 14:42:42 -080088 VLOG("Statscompanion could not find a broadcast receiver for %s",
89 key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -080090 } else {
91 sc->sendBroadcast(String16(receiver.first.c_str()),
92 String16(receiver.second.c_str()));
93 }
yro31eb67b2017-10-24 13:33:21 -070094 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -070095
96 mConfigManager->AddListener(mProcessor);
97
98 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070099}
100
Yao Chenef99c4f2017-09-22 16:26:54 -0700101StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700102}
103
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700104void StatsService::init_system_properties() {
105 mEngBuild = false;
106 const prop_info* buildType = __system_property_find("ro.build.type");
107 if (buildType != NULL) {
108 __system_property_read_callback(buildType, init_build_type_callback, this);
109 }
David Chen0656b7a2017-09-13 15:53:39 -0700110}
111
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700112void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
113 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700114 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700115 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
116 }
117}
118
119/**
120 * Implement our own because the default binder implementation isn't
121 * properly handling SHELL_COMMAND_TRANSACTION.
122 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700123status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
124 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700125 status_t err;
126
127 switch (code) {
128 case SHELL_COMMAND_TRANSACTION: {
129 int in = data.readFileDescriptor();
130 int out = data.readFileDescriptor();
131 int err = data.readFileDescriptor();
132 int argc = data.readInt32();
133 Vector<String8> args;
134 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
135 args.add(String8(data.readString16()));
136 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700137 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
138 sp<IResultReceiver> resultReceiver =
139 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700140
141 FILE* fin = fdopen(in, "r");
142 FILE* fout = fdopen(out, "w");
143 FILE* ferr = fdopen(err, "w");
144
145 if (fin == NULL || fout == NULL || ferr == NULL) {
146 resultReceiver->send(NO_MEMORY);
147 } else {
148 err = command(fin, fout, ferr, args);
149 resultReceiver->send(err);
150 }
151
152 if (fin != NULL) {
153 fflush(fin);
154 fclose(fin);
155 }
156 if (fout != NULL) {
157 fflush(fout);
158 fclose(fout);
159 }
160 if (fout != NULL) {
161 fflush(ferr);
162 fclose(ferr);
163 }
164
165 return NO_ERROR;
166 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700167 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700168 }
169}
170
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700171/**
172 * Write debugging data about statsd.
173 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700174status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700175 FILE* out = fdopen(fd, "w");
176 if (out == NULL) {
177 return NO_MEMORY; // the fd is already open
178 }
179
Yao Chen884c8c12018-01-26 10:36:25 -0800180 bool verbose = false;
181 if (args.size() > 0 && !args[0].compare(String16("-v"))) {
182 verbose = true;
183 }
184
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700185 // TODO: Proto format for incident reports
Yao Chen884c8c12018-01-26 10:36:25 -0800186 dump_impl(out, verbose);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700187
188 fclose(out);
189 return NO_ERROR;
190}
191
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700192/**
193 * Write debugging data about statsd in text format.
194 */
Yao Chen884c8c12018-01-26 10:36:25 -0800195void StatsService::dump_impl(FILE* out, bool verbose) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800196 StatsdStats::getInstance().dumpStats(out);
Yao Chen884c8c12018-01-26 10:36:25 -0800197 mProcessor->dumpStates(out, verbose);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700198}
199
200/**
201 * Implementation of the adb shell cmd stats command.
202 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700203status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700204 // TODO: Permission check
205
206 const int argCount = args.size();
207 if (argCount >= 1) {
208 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700209 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700210 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700211 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700212
David Chende701692017-10-05 13:16:02 -0700213 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800214 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700215 }
Yao Chen729093d2017-10-16 10:33:26 -0700216
217 if (!args[0].compare(String8("dump-report"))) {
218 return cmd_dump_report(out, err, args);
219 }
David Chen1481fe12017-10-16 13:16:34 -0700220
221 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
222 return cmd_print_pulled_metrics(out, args);
223 }
David Chenadaf8b32017-11-03 15:42:08 -0700224
225 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800226 return cmd_trigger_broadcast(out, args);
227 }
228
229 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800230 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700231 }
yro87d983c2017-11-14 21:31:43 -0800232
Yao Chen8d9989b2017-11-18 18:54:50 -0800233 if (!args[0].compare(String8("meminfo"))) {
234 return cmd_dump_memory_info(out);
235 }
yro947fbce2017-11-15 22:50:23 -0800236
237 if (!args[0].compare(String8("write-to-disk"))) {
238 return cmd_write_data_to_disk(out);
239 }
Bookatzb223c4e2018-02-01 15:35:04 -0800240
241 if (!args[0].compare(String8("log-app-hook"))) {
242 return cmd_log_app_hook(out, args);
243 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800244
245 if (!args[0].compare(String8("clear-puller-cache"))) {
246 return cmd_clear_puller_cache(out);
247 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700248 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700249
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700250 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700251 return NO_ERROR;
252}
253
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700254void StatsService::print_cmd_help(FILE* out) {
255 fprintf(out,
256 "usage: adb shell cmd stats print-stats-log [tag_required] "
257 "[timestamp_nsec_optional]\n");
258 fprintf(out, "\n");
259 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800260 fprintf(out, "usage: adb shell cmd stats meminfo\n");
261 fprintf(out, "\n");
262 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
263 fprintf(out, " # adb shell stop\n");
264 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
265 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
266 fprintf(out, " # adb shell start\n");
267 fprintf(out, "\n");
268 fprintf(out, "\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800269 fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700270 fprintf(out, "\n");
271 fprintf(out, " Prints the UID, app name, version mapping.\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800272 fprintf(out, " PKG Optional package name to print the uids of the package\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700273 fprintf(out, "\n");
274 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800275 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700276 fprintf(out, "\n");
277 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
278 fprintf(out, "\n");
279 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800280 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
281 fprintf(out, "\n");
282 fprintf(out, " Flushes all data on memory to disk.\n");
283 fprintf(out, "\n");
284 fprintf(out, "\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800285 fprintf(out, "usage: adb shell cmd stats log-app-hook [UID] LABEL STATE\n");
286 fprintf(out, " Writes an AppHook event to the statslog buffer.\n");
287 fprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
288 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
289 fprintf(out, " uid is used.\n");
290 fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
291 fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
292 fprintf(out, "\n");
293 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800294 fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700295 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
296 fprintf(out, "\n");
297 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
yro74fed972017-11-27 14:42:42 -0800298 fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
299 fprintf(out, " provided, then all configs will be removed from memory and disk.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700300 fprintf(out, "\n");
301 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
yro74fed972017-11-27 14:42:42 -0800302 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700303 fprintf(out, " uid is used.\n");
304 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700305 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800306 fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
307 fprintf(out, "\n be removed from memory and disk!\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700308 fprintf(out, "\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800309 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700310 fprintf(out, " Dump all metric data for a configuration.\n");
311 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
312 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
313 fprintf(out, " calling uid is used.\n");
314 fprintf(out, " NAME The name of the configuration\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800315 fprintf(out, " --proto Print proto binary.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700316 fprintf(out, "\n");
317 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800318 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
319 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
320 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
321 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
322 fprintf(out, " calling uid is used.\n");
323 fprintf(out, " NAME The name of the configuration\n");
324 fprintf(out, "\n");
325 fprintf(out, "\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800326 fprintf(out, "usage: adb shell cmd stats print-stats\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800327 fprintf(out, " Prints some basic stats.\n");
Chenjie Yufa22d652018-02-05 14:37:48 -0800328 fprintf(out, "\n");
329 fprintf(out, "\n");
330 fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
331 fprintf(out, " Clear cached puller data.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700332}
333
David Chen1d7b0cd2017-11-15 14:20:04 -0800334status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
335 string name;
336 bool good = false;
337 int uid;
338 const int argCount = args.size();
339 if (argCount == 2) {
340 // Automatically pick the UID
341 uid = IPCThreadState::self()->getCallingUid();
342 // TODO: What if this isn't a binder call? Should we fail?
343 name.assign(args[1].c_str(), args[1].size());
344 good = true;
345 } else if (argCount == 3) {
346 // If it's a userdebug or eng build, then the shell user can
347 // impersonate other uids.
348 if (mEngBuild) {
349 const char* s = args[1].c_str();
350 if (*s != '\0') {
351 char* end = NULL;
352 uid = strtol(s, &end, 0);
353 if (*end == '\0') {
354 name.assign(args[2].c_str(), args[2].size());
355 good = true;
356 }
357 }
358 } else {
359 fprintf(out,
360 "The metrics can only be dumped for other UIDs on eng or userdebug "
361 "builds.\n");
362 }
363 }
364 if (!good) {
365 print_cmd_help(out);
366 return UNKNOWN_ERROR;
367 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800368 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name)));
yro4d889e62017-11-17 15:44:48 -0800369 sp<IStatsCompanionService> sc = getStatsCompanionService();
370 if (sc != nullptr) {
371 sc->sendBroadcast(String16(receiver.first.c_str()), String16(receiver.second.c_str()));
yro74fed972017-11-27 14:42:42 -0800372 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
373 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800374 } else {
yro74fed972017-11-27 14:42:42 -0800375 VLOG("Could not access statsCompanion");
yro4d889e62017-11-17 15:44:48 -0800376 }
377
David Chenadaf8b32017-11-03 15:42:08 -0700378 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700379}
380
381status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
382 const int argCount = args.size();
383 if (argCount >= 2) {
384 if (args[1] == "update" || args[1] == "remove") {
385 bool good = false;
386 int uid = -1;
387 string name;
388
389 if (argCount == 3) {
390 // Automatically pick the UID
391 uid = IPCThreadState::self()->getCallingUid();
392 // TODO: What if this isn't a binder call? Should we fail?
393 name.assign(args[2].c_str(), args[2].size());
394 good = true;
395 } else if (argCount == 4) {
396 // If it's a userdebug or eng build, then the shell user can
397 // impersonate other uids.
398 if (mEngBuild) {
399 const char* s = args[2].c_str();
400 if (*s != '\0') {
401 char* end = NULL;
402 uid = strtol(s, &end, 0);
403 if (*end == '\0') {
404 name.assign(args[3].c_str(), args[3].size());
405 good = true;
406 }
407 }
408 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700409 fprintf(err,
410 "The config can only be set for other UIDs on eng or userdebug "
411 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700412 }
yroe5f82922018-01-22 18:37:27 -0800413 } else if (argCount == 2 && args[1] == "remove") {
414 good = true;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700415 }
416
417 if (!good) {
418 // If arg parsing failed, print the help text and return an error.
419 print_cmd_help(out);
420 return UNKNOWN_ERROR;
421 }
422
423 if (args[1] == "update") {
424 // Read stream into buffer.
425 string buffer;
426 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
427 fprintf(err, "Error reading stream for StatsConfig.\n");
428 return UNKNOWN_ERROR;
429 }
430
431 // Parse buffer.
432 StatsdConfig config;
433 if (!config.ParseFromString(buffer)) {
434 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
435 return UNKNOWN_ERROR;
436 }
437
438 // Add / update the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800439 mConfigManager->UpdateConfig(ConfigKey(uid, StrToInt64(name)), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700440 } else {
yro74fed972017-11-27 14:42:42 -0800441 if (argCount == 2) {
442 cmd_remove_all_configs(out);
443 } else {
444 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800445 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800446 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700447 }
448
449 return NO_ERROR;
450 }
David Chen0656b7a2017-09-13 15:53:39 -0700451 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700452 print_cmd_help(out);
453 return UNKNOWN_ERROR;
454}
455
Yao Chen729093d2017-10-16 10:33:26 -0700456status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
457 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800458 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700459 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800460 bool proto = false;
Yao Chen729093d2017-10-16 10:33:26 -0700461 int uid;
462 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800463 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
464 proto = true;
465 argCount -= 1;
466 }
Yao Chen729093d2017-10-16 10:33:26 -0700467 if (argCount == 2) {
468 // Automatically pick the UID
469 uid = IPCThreadState::self()->getCallingUid();
470 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a3792017-10-30 22:57:06 -0700471 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700472 good = true;
473 } else if (argCount == 3) {
474 // If it's a userdebug or eng build, then the shell user can
475 // impersonate other uids.
476 if (mEngBuild) {
477 const char* s = args[1].c_str();
478 if (*s != '\0') {
479 char* end = NULL;
480 uid = strtol(s, &end, 0);
481 if (*end == '\0') {
482 name.assign(args[2].c_str(), args[2].size());
483 good = true;
484 }
485 }
486 } else {
487 fprintf(out,
488 "The metrics can only be dumped for other UIDs on eng or userdebug "
489 "builds.\n");
490 }
491 }
492 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800493 vector<uint8_t> data;
Yangster-mac94e197c2018-01-02 16:03:03 -0800494 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), &data);
Yao Chen729093d2017-10-16 10:33:26 -0700495 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
Chenjie Yub236c862017-11-28 22:20:44 -0800496 if (proto) {
497 for (size_t i = 0; i < data.size(); i ++) {
498 fprintf(out, "%c", data[i]);
499 }
500 } else {
501 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
502 fprintf(out, "See the StatsLogReport in logcat...\n");
503 }
Yao Chen729093d2017-10-16 10:33:26 -0700504 return android::OK;
505 } else {
506 // If arg parsing failed, print the help text and return an error.
507 print_cmd_help(out);
508 return UNKNOWN_ERROR;
509 }
510 } else {
511 fprintf(out, "Log processor does not exist...\n");
512 return UNKNOWN_ERROR;
513 }
514}
515
Yao Chenb3561512017-11-21 18:07:17 -0800516status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800517 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
518 for (const ConfigKey& key : configs) {
519 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
520 mProcessor->GetMetricsSize(key));
521 }
Yao Chenb3561512017-11-21 18:07:17 -0800522 StatsdStats& statsdStats = StatsdStats::getInstance();
Yao Chenf5acabe2018-01-17 14:10:34 -0800523 statsdStats.dumpStats(out);
David Chen1d7b0cd2017-11-15 14:20:04 -0800524 return NO_ERROR;
525}
526
Yao Chend10f7b12017-12-18 12:53:50 -0800527status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) {
528 if (args.size() > 1) {
529 string pkg;
530 pkg.assign(args[1].c_str(), args[1].size());
531 auto uids = mUidMap->getAppUid(pkg);
532 fprintf(out, "%s -> [ ", pkg.c_str());
533 for (const auto& uid : uids) {
534 fprintf(out, "%d ", uid);
535 }
536 fprintf(out, "]\n");
537 } else {
538 mUidMap->printUidMap(out);
539 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700540 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700541}
542
yro947fbce2017-11-15 22:50:23 -0800543status_t StatsService::cmd_write_data_to_disk(FILE* out) {
544 fprintf(out, "Writing data to disk\n");
545 mProcessor->WriteDataToDisk();
546 return NO_ERROR;
547}
548
Bookatzb223c4e2018-02-01 15:35:04 -0800549status_t StatsService::cmd_log_app_hook(FILE* out, const Vector<String8>& args) {
550 bool good = false;
551 int32_t uid;
552 int32_t label;
553 int32_t state;
554 const int argCount = args.size();
555 if (argCount == 3) {
556 // Automatically pick the UID
557 uid = IPCThreadState::self()->getCallingUid();
558 label = atoi(args[1].c_str());
559 state = atoi(args[2].c_str());
560 good = true;
561 } else if (argCount == 4) {
562 uid = atoi(args[1].c_str());
563 // If it's a userdebug or eng build, then the shell user can impersonate other uids.
564 // Otherwise, the uid must match the actual caller's uid.
565 if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) {
566 label = atoi(args[2].c_str());
567 state = atoi(args[3].c_str());
568 good = true;
569 } else {
570 fprintf(out,
571 "Selecting a UID for writing AppHook can only be dumped for other UIDs on eng"
572 " or userdebug builds.\n");
573 }
574 }
575 if (good) {
576 fprintf(out, "Logging AppHook(%d, %d, %d) to statslog.\n", uid, label, state);
577 android::util::stats_write(android::util::APP_HOOK, uid, label, state);
578 } else {
579 print_cmd_help(out);
580 return UNKNOWN_ERROR;
581 }
582 return NO_ERROR;
583}
584
David Chen1481fe12017-10-16 13:16:34 -0700585status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
586 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700587 vector<shared_ptr<LogEvent> > stats;
588 if (mStatsPullerManager.Pull(s, &stats)) {
589 for (const auto& it : stats) {
590 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
591 }
592 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
593 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700594 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700595 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700596}
597
yro74fed972017-11-27 14:42:42 -0800598status_t StatsService::cmd_remove_all_configs(FILE* out) {
599 fprintf(out, "Removing all configs...\n");
600 VLOG("StatsService::cmd_remove_all_configs was called");
601 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800602 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800603 return NO_ERROR;
604}
605
Yao Chen8d9989b2017-11-18 18:54:50 -0800606status_t StatsService::cmd_dump_memory_info(FILE* out) {
607 std::string s = dumpMemInfo(100);
608 fprintf(out, "Memory Info\n");
609 fprintf(out, "%s", s.c_str());
610 return NO_ERROR;
611}
612
Chenjie Yue72252b2018-02-01 13:19:35 -0800613status_t StatsService::cmd_clear_puller_cache(FILE* out) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800614 IPCThreadState* ipc = IPCThreadState::self();
615 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
616 if (checkCallingPermission(String16(kPermissionDump))) {
617 int cleared = mStatsPullerManager.ForceClearPullerCache();
618 fprintf(out, "Puller removed %d cached data!\n", cleared);
619 return NO_ERROR;
620 } else {
621 return PERMISSION_DENIED;
622 }
Chenjie Yue72252b2018-02-01 13:19:35 -0800623}
624
Dianne Hackborn3accca02013-09-20 09:32:11 -0700625Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
David Chende701692017-10-05 13:16:02 -0700626 const vector<String16>& app) {
yro74fed972017-11-27 14:42:42 -0800627 VLOG("StatsService::informAllUidData was called");
David Chende701692017-10-05 13:16:02 -0700628
629 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
630 return Status::fromExceptionCode(Status::EX_SECURITY,
631 "Only system uid can call informAllUidData");
632 }
633
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700634 mUidMap->updateMap(uid, version, app);
yro74fed972017-11-27 14:42:42 -0800635 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700636
637 return Status::ok();
638}
639
Dianne Hackborn3accca02013-09-20 09:32:11 -0700640Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) {
yro74fed972017-11-27 14:42:42 -0800641 VLOG("StatsService::informOnePackage was called");
David Chende701692017-10-05 13:16:02 -0700642
643 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
644 return Status::fromExceptionCode(Status::EX_SECURITY,
645 "Only system uid can call informOnePackage");
646 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700647 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700648 return Status::ok();
649}
650
651Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
yro74fed972017-11-27 14:42:42 -0800652 VLOG("StatsService::informOnePackageRemoved was called");
David Chende701692017-10-05 13:16:02 -0700653
654 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
655 return Status::fromExceptionCode(Status::EX_SECURITY,
656 "Only system uid can call informOnePackageRemoved");
657 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700658 mUidMap->removeApp(app, uid);
David Chende701692017-10-05 13:16:02 -0700659 return Status::ok();
660}
661
Yao Chenef99c4f2017-09-22 16:26:54 -0700662Status StatsService::informAnomalyAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800663 VLOG("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700664
665 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
666 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700667 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700668 }
669
yro74fed972017-11-27 14:42:42 -0800670 VLOG("StatsService::informAnomalyAlarmFired succeeded");
Bookatzcc5adef22017-11-21 14:36:23 -0800671 uint64_t currentTimeSec = time(nullptr);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800672 std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet =
Bookatzcc5adef22017-11-21 14:36:23 -0800673 mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
674 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, anomalySet);
Bookatz1b0b1142017-09-08 11:58:42 -0700675 return Status::ok();
676}
677
Yao Chenef99c4f2017-09-22 16:26:54 -0700678Status StatsService::informPollAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800679 VLOG("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700680
681 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
682 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700683 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700684 }
685
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700686 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700687
yro74fed972017-11-27 14:42:42 -0800688 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700689
690 return Status::ok();
691}
692
Yao Chenef99c4f2017-09-22 16:26:54 -0700693Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700694 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 systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700697 }
698
699 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800700 VLOG("StatsService::systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700701
Bookatzb487b552017-09-18 11:26:01 -0700702 sayHiToStatsCompanion();
703
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700704 return Status::ok();
705}
706
yro947fbce2017-11-15 22:50:23 -0800707Status StatsService::writeDataToDisk() {
708 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
709 return Status::fromExceptionCode(Status::EX_SECURITY,
710 "Only system uid can call systemRunning");
711 }
712
yro74fed972017-11-27 14:42:42 -0800713 VLOG("StatsService::writeDataToDisk");
yro947fbce2017-11-15 22:50:23 -0800714
715 mProcessor->WriteDataToDisk();
716
717 return Status::ok();
718}
719
Yao Chenef99c4f2017-09-22 16:26:54 -0700720void StatsService::sayHiToStatsCompanion() {
721 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
722 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700723 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
724 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800725 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700726 statsCompanion->statsdReady();
727 } else {
yro74fed972017-11-27 14:42:42 -0800728 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700729 }
730}
731
Yao Chenef99c4f2017-09-22 16:26:54 -0700732sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700733 sp<IStatsCompanionService> statsCompanion = nullptr;
734 // Get statscompanion service from service manager
735 const sp<IServiceManager> sm(defaultServiceManager());
736 if (sm != nullptr) {
737 const String16 name("statscompanion");
738 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
739 if (statsCompanion == nullptr) {
740 ALOGW("statscompanion service unavailable!");
741 return nullptr;
742 }
743 }
744 return statsCompanion;
745}
746
Yao Chenef99c4f2017-09-22 16:26:54 -0700747Status StatsService::statsCompanionReady() {
yro74fed972017-11-27 14:42:42 -0800748 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700749
750 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
751 return Status::fromExceptionCode(Status::EX_SECURITY,
752 "Only system uid can call statsCompanionReady");
753 }
754
755 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
756 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700757 return Status::fromExceptionCode(
758 Status::EX_NULL_POINTER,
759 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700760 }
yro74fed972017-11-27 14:42:42 -0800761 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700762 IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor));
Bookatzb487b552017-09-18 11:26:01 -0700763 mAnomalyMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800764 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700765
766 return Status::ok();
767}
768
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700769void StatsService::Startup() {
770 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700771}
772
Yangster-macd40053e2018-01-09 16:29:22 -0800773void StatsService::OnLogEvent(LogEvent* event) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700774 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700775}
776
Yangster-mac94e197c2018-01-02 16:03:03 -0800777Status StatsService::getData(int64_t key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700778 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800779 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800780 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800781 ConfigKey configKey(ipc->getCallingUid(), key);
David Chen1d7b0cd2017-11-15 14:20:04 -0800782 mProcessor->onDumpReport(configKey, output);
David Chenadaf8b32017-11-03 15:42:08 -0700783 return Status::ok();
784 } else {
785 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
786 }
yro31eb67b2017-10-24 13:33:21 -0700787}
788
David Chen2e8f3802017-11-22 10:56:48 -0800789Status StatsService::getMetadata(vector<uint8_t>* output) {
790 IPCThreadState* ipc = IPCThreadState::self();
791 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
792 ipc->getCallingUid());
793 if (checkCallingPermission(String16(kPermissionDump))) {
794 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
795 return Status::ok();
796 } else {
797 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
798 }
799}
800
Yangster-mac94e197c2018-01-02 16:03:03 -0800801Status StatsService::addConfiguration(int64_t key,
David Chenadaf8b32017-11-03 15:42:08 -0700802 const vector <uint8_t>& config,
803 const String16& package, const String16& cls,
804 bool* success) {
805 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800806 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800807 ConfigKey configKey(ipc->getCallingUid(), key);
David Chenadaf8b32017-11-03 15:42:08 -0700808 StatsdConfig cfg;
Yangster-macbbd056a2018-01-22 13:37:02 -0800809 if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) {
David Chen7d8aa4d2017-12-27 13:37:01 -0800810 *success = false;
811 return Status::ok();
812 }
David Chenadaf8b32017-11-03 15:42:08 -0700813 mConfigManager->UpdateConfig(configKey, cfg);
814 mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()),
815 string(String8(cls).string()));
816 *success = true;
817 return Status::ok();
818 } else {
Yao Chenaa39bc72017-12-01 11:16:50 -0800819 *success = false;
David Chenadaf8b32017-11-03 15:42:08 -0700820 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700821 }
yro31eb67b2017-10-24 13:33:21 -0700822}
823
Yangster-mac94e197c2018-01-02 16:03:03 -0800824Status StatsService::removeConfiguration(int64_t key, bool* success) {
David Chenadaf8b32017-11-03 15:42:08 -0700825 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800826 if (checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800827 ConfigKey configKey(ipc->getCallingUid(), key);
828 mConfigManager->RemoveConfig(configKey);
829 SubscriberReporter::getInstance().removeConfig(configKey);
Yao Chenaa39bc72017-12-01 11:16:50 -0800830 *success = true;
David Chenadaf8b32017-11-03 15:42:08 -0700831 return Status::ok();
832 } else {
833 *success = false;
834 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700835 }
yro31eb67b2017-10-24 13:33:21 -0700836}
837
Bookatzc6977972018-01-16 16:55:05 -0800838Status StatsService::setBroadcastSubscriber(int64_t configId,
839 int64_t subscriberId,
840 const sp<android::IBinder>& intentSender,
841 bool* success) {
842 VLOG("StatsService::setBroadcastSubscriber called.");
843 IPCThreadState* ipc = IPCThreadState::self();
844 if (checkCallingPermission(String16(kPermissionDump))) {
845 ConfigKey configKey(ipc->getCallingUid(), configId);
846 SubscriberReporter::getInstance()
847 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
848 *success = true;
849 return Status::ok();
850 } else {
851 *success = false;
852 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
853 }
854}
855
856Status StatsService::unsetBroadcastSubscriber(int64_t configId,
857 int64_t subscriberId,
858 bool* success) {
859 VLOG("StatsService::unsetBroadcastSubscriber called.");
860 IPCThreadState* ipc = IPCThreadState::self();
861 if (checkCallingPermission(String16(kPermissionDump))) {
862 ConfigKey configKey(ipc->getCallingUid(), configId);
863 SubscriberReporter::getInstance()
864 .unsetBroadcastSubscriber(configKey, subscriberId);
865 *success = true;
866 return Status::ok();
867 } else {
868 *success = false;
869 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
870 }
871}
872
873
David Chen1d7b0cd2017-11-15 14:20:04 -0800874void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700875}
876
Yao Chenef99c4f2017-09-22 16:26:54 -0700877} // namespace statsd
878} // namespace os
879} // namespace android