blob: 0e90f2a914ff6d5acece869b27d529b6c95ea4eb [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();
80 mConfigManager = new ConfigManager();
Yangster-mac20877162017-12-22 17:19:39 -080081 mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, time(nullptr), [this](const ConfigKey& key) {
yro947fbce2017-11-15 22:50:23 -080082 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen1d7b0cd2017-11-15 14:20:04 -080083 auto receiver = mConfigManager->GetConfigReceiver(key);
84 if (sc == nullptr) {
yro74fed972017-11-27 14:42:42 -080085 VLOG("Could not find StatsCompanionService");
David Chen661f7912018-01-22 17:46:24 -080086 } else if (receiver == nullptr) {
yro74fed972017-11-27 14:42:42 -080087 VLOG("Statscompanion could not find a broadcast receiver for %s",
88 key.ToString().c_str());
David Chen1d7b0cd2017-11-15 14:20:04 -080089 } else {
David Chen661f7912018-01-22 17:46:24 -080090 sc->sendDataBroadcast(receiver);
David Chen1d7b0cd2017-11-15 14:20:04 -080091 }
yro31eb67b2017-10-24 13:33:21 -070092 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -070093
94 mConfigManager->AddListener(mProcessor);
95
96 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070097}
98
Yao Chenef99c4f2017-09-22 16:26:54 -070099StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700100}
101
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700102void StatsService::init_system_properties() {
103 mEngBuild = false;
104 const prop_info* buildType = __system_property_find("ro.build.type");
105 if (buildType != NULL) {
106 __system_property_read_callback(buildType, init_build_type_callback, this);
107 }
David Chen0656b7a2017-09-13 15:53:39 -0700108}
109
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700110void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
111 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700112 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700113 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
114 }
115}
116
117/**
118 * Implement our own because the default binder implementation isn't
119 * properly handling SHELL_COMMAND_TRANSACTION.
120 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700121status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
122 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700123 status_t err;
124
125 switch (code) {
126 case SHELL_COMMAND_TRANSACTION: {
127 int in = data.readFileDescriptor();
128 int out = data.readFileDescriptor();
129 int err = data.readFileDescriptor();
130 int argc = data.readInt32();
131 Vector<String8> args;
132 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
133 args.add(String8(data.readString16()));
134 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700135 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
136 sp<IResultReceiver> resultReceiver =
137 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700138
139 FILE* fin = fdopen(in, "r");
140 FILE* fout = fdopen(out, "w");
141 FILE* ferr = fdopen(err, "w");
142
143 if (fin == NULL || fout == NULL || ferr == NULL) {
144 resultReceiver->send(NO_MEMORY);
145 } else {
146 err = command(fin, fout, ferr, args);
147 resultReceiver->send(err);
148 }
149
150 if (fin != NULL) {
151 fflush(fin);
152 fclose(fin);
153 }
154 if (fout != NULL) {
155 fflush(fout);
156 fclose(fout);
157 }
158 if (fout != NULL) {
159 fflush(ferr);
160 fclose(ferr);
161 }
162
163 return NO_ERROR;
164 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700165 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700166 }
167}
168
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700169/**
170 * Write debugging data about statsd.
171 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700172status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700173 FILE* out = fdopen(fd, "w");
174 if (out == NULL) {
175 return NO_MEMORY; // the fd is already open
176 }
177
Yao Chen884c8c12018-01-26 10:36:25 -0800178 bool verbose = false;
179 if (args.size() > 0 && !args[0].compare(String16("-v"))) {
180 verbose = true;
181 }
182
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700183 // TODO: Proto format for incident reports
Yao Chen884c8c12018-01-26 10:36:25 -0800184 dump_impl(out, verbose);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700185
186 fclose(out);
187 return NO_ERROR;
188}
189
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700190/**
191 * Write debugging data about statsd in text format.
192 */
Yao Chen884c8c12018-01-26 10:36:25 -0800193void StatsService::dump_impl(FILE* out, bool verbose) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800194 StatsdStats::getInstance().dumpStats(out);
Yao Chen884c8c12018-01-26 10:36:25 -0800195 mProcessor->dumpStates(out, verbose);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700196}
197
198/**
199 * Implementation of the adb shell cmd stats command.
200 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700201status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700202 // TODO: Permission check
203
204 const int argCount = args.size();
205 if (argCount >= 1) {
206 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700207 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700208 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700209 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700210
David Chende701692017-10-05 13:16:02 -0700211 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800212 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700213 }
Yao Chen729093d2017-10-16 10:33:26 -0700214
215 if (!args[0].compare(String8("dump-report"))) {
216 return cmd_dump_report(out, err, args);
217 }
David Chen1481fe12017-10-16 13:16:34 -0700218
219 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
220 return cmd_print_pulled_metrics(out, args);
221 }
David Chenadaf8b32017-11-03 15:42:08 -0700222
223 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800224 return cmd_trigger_broadcast(out, args);
225 }
226
227 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800228 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700229 }
yro87d983c2017-11-14 21:31:43 -0800230
Yao Chen8d9989b2017-11-18 18:54:50 -0800231 if (!args[0].compare(String8("meminfo"))) {
232 return cmd_dump_memory_info(out);
233 }
yro947fbce2017-11-15 22:50:23 -0800234
235 if (!args[0].compare(String8("write-to-disk"))) {
236 return cmd_write_data_to_disk(out);
237 }
Bookatzb223c4e2018-02-01 15:35:04 -0800238
239 if (!args[0].compare(String8("log-app-hook"))) {
240 return cmd_log_app_hook(out, args);
241 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800242
243 if (!args[0].compare(String8("clear-puller-cache"))) {
244 return cmd_clear_puller_cache(out);
245 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700246 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700247
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700248 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700249 return NO_ERROR;
250}
251
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700252void StatsService::print_cmd_help(FILE* out) {
253 fprintf(out,
254 "usage: adb shell cmd stats print-stats-log [tag_required] "
255 "[timestamp_nsec_optional]\n");
256 fprintf(out, "\n");
257 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800258 fprintf(out, "usage: adb shell cmd stats meminfo\n");
259 fprintf(out, "\n");
260 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
261 fprintf(out, " # adb shell stop\n");
262 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
263 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
264 fprintf(out, " # adb shell start\n");
265 fprintf(out, "\n");
266 fprintf(out, "\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800267 fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700268 fprintf(out, "\n");
269 fprintf(out, " Prints the UID, app name, version mapping.\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800270 fprintf(out, " PKG Optional package name to print the uids of the package\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700271 fprintf(out, "\n");
272 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800273 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700274 fprintf(out, "\n");
275 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
276 fprintf(out, "\n");
277 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800278 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
279 fprintf(out, "\n");
280 fprintf(out, " Flushes all data on memory to disk.\n");
281 fprintf(out, "\n");
282 fprintf(out, "\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800283 fprintf(out, "usage: adb shell cmd stats log-app-hook [UID] LABEL STATE\n");
284 fprintf(out, " Writes an AppHook event to the statslog buffer.\n");
285 fprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
286 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
287 fprintf(out, " uid is used.\n");
288 fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
289 fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
290 fprintf(out, "\n");
291 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800292 fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700293 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
294 fprintf(out, "\n");
295 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
yro74fed972017-11-27 14:42:42 -0800296 fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
297 fprintf(out, " provided, then all configs will be removed from memory and disk.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700298 fprintf(out, "\n");
299 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
yro74fed972017-11-27 14:42:42 -0800300 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700301 fprintf(out, " uid is used.\n");
302 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a372017-10-30 22:57:06 -0700303 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800304 fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
305 fprintf(out, "\n be removed from memory and disk!\n");
Yao Chen5154a372017-10-30 22:57:06 -0700306 fprintf(out, "\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800307 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n");
Yao Chen5154a372017-10-30 22:57:06 -0700308 fprintf(out, " Dump all metric data for a configuration.\n");
309 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
310 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
311 fprintf(out, " calling uid is used.\n");
312 fprintf(out, " NAME The name of the configuration\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800313 fprintf(out, " --proto Print proto binary.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700314 fprintf(out, "\n");
315 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800316 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
317 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
318 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
319 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
320 fprintf(out, " calling uid is used.\n");
321 fprintf(out, " NAME The name of the configuration\n");
322 fprintf(out, "\n");
323 fprintf(out, "\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800324 fprintf(out, "usage: adb shell cmd stats print-stats\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800325 fprintf(out, " Prints some basic stats.\n");
Chenjie Yufa22d652018-02-05 14:37:48 -0800326 fprintf(out, "\n");
327 fprintf(out, "\n");
328 fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
329 fprintf(out, " Clear cached puller data.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700330}
331
David Chen1d7b0cd2017-11-15 14:20:04 -0800332status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
333 string name;
334 bool good = false;
335 int uid;
336 const int argCount = args.size();
337 if (argCount == 2) {
338 // Automatically pick the UID
339 uid = IPCThreadState::self()->getCallingUid();
340 // TODO: What if this isn't a binder call? Should we fail?
341 name.assign(args[1].c_str(), args[1].size());
342 good = true;
343 } else if (argCount == 3) {
344 // If it's a userdebug or eng build, then the shell user can
345 // impersonate other uids.
346 if (mEngBuild) {
347 const char* s = args[1].c_str();
348 if (*s != '\0') {
349 char* end = NULL;
350 uid = strtol(s, &end, 0);
351 if (*end == '\0') {
352 name.assign(args[2].c_str(), args[2].size());
353 good = true;
354 }
355 }
356 } else {
357 fprintf(out,
358 "The metrics can only be dumped for other UIDs on eng or userdebug "
359 "builds.\n");
360 }
361 }
362 if (!good) {
363 print_cmd_help(out);
364 return UNKNOWN_ERROR;
365 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800366 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name)));
yro4d889e62017-11-17 15:44:48 -0800367 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen661f7912018-01-22 17:46:24 -0800368 if (sc == nullptr) {
369 VLOG("Could not access statsCompanion");
370 } else if (receiver == nullptr) {
371 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str())
372 } else {
373 sc->sendDataBroadcast(receiver);
yro74fed972017-11-27 14:42:42 -0800374 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
375 args[2].c_str());
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 Chen5154a372017-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");
Bookatzcc5adef2017-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 =
Bookatzcc5adef2017-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,
David Chenadaf8b32017-11-03 15:42:08 -0700803 bool* success) {
804 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800805 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800806 ConfigKey configKey(ipc->getCallingUid(), key);
David Chenadaf8b32017-11-03 15:42:08 -0700807 StatsdConfig cfg;
Yangster-macbbd056a2018-01-22 13:37:02 -0800808 if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) {
David Chen7d8aa4d2017-12-27 13:37:01 -0800809 *success = false;
810 return Status::ok();
811 }
David Chenadaf8b32017-11-03 15:42:08 -0700812 mConfigManager->UpdateConfig(configKey, cfg);
David Chen661f7912018-01-22 17:46:24 -0800813 *success = true;
814 return Status::ok();
815 } else {
816 *success = false;
817 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
818 }
819}
820
821Status StatsService::removeDataFetchOperation(int64_t key, bool* success) {
822 IPCThreadState* ipc = IPCThreadState::self();
823 if (checkCallingPermission(String16(kPermissionDump))) {
824 ConfigKey configKey(ipc->getCallingUid(), key);
825 mConfigManager->RemoveConfigReceiver(configKey);
826 *success = true;
827 return Status::ok();
828 } else {
829 *success = false;
830 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
831 }
832}
833
834Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender,
835 bool* success) {
836 IPCThreadState* ipc = IPCThreadState::self();
837 if (checkCallingPermission(String16(kPermissionDump))) {
838 ConfigKey configKey(ipc->getCallingUid(), key);
839 mConfigManager->SetConfigReceiver(configKey, intentSender);
David Chenadaf8b32017-11-03 15:42:08 -0700840 *success = true;
841 return Status::ok();
842 } else {
Yao Chenaa39bc72017-12-01 11:16:50 -0800843 *success = false;
David Chenadaf8b32017-11-03 15:42:08 -0700844 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700845 }
yro31eb67b2017-10-24 13:33:21 -0700846}
847
Yangster-mac94e197c2018-01-02 16:03:03 -0800848Status StatsService::removeConfiguration(int64_t key, bool* success) {
David Chenadaf8b32017-11-03 15:42:08 -0700849 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800850 if (checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800851 ConfigKey configKey(ipc->getCallingUid(), key);
852 mConfigManager->RemoveConfig(configKey);
853 SubscriberReporter::getInstance().removeConfig(configKey);
Yao Chenaa39bc72017-12-01 11:16:50 -0800854 *success = true;
David Chenadaf8b32017-11-03 15:42:08 -0700855 return Status::ok();
856 } else {
857 *success = false;
858 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700859 }
yro31eb67b2017-10-24 13:33:21 -0700860}
861
Bookatzc6977972018-01-16 16:55:05 -0800862Status StatsService::setBroadcastSubscriber(int64_t configId,
863 int64_t subscriberId,
864 const sp<android::IBinder>& intentSender,
865 bool* success) {
866 VLOG("StatsService::setBroadcastSubscriber called.");
867 IPCThreadState* ipc = IPCThreadState::self();
868 if (checkCallingPermission(String16(kPermissionDump))) {
869 ConfigKey configKey(ipc->getCallingUid(), configId);
870 SubscriberReporter::getInstance()
871 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
872 *success = true;
873 return Status::ok();
874 } else {
875 *success = false;
876 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
877 }
878}
879
880Status StatsService::unsetBroadcastSubscriber(int64_t configId,
881 int64_t subscriberId,
882 bool* success) {
883 VLOG("StatsService::unsetBroadcastSubscriber called.");
884 IPCThreadState* ipc = IPCThreadState::self();
885 if (checkCallingPermission(String16(kPermissionDump))) {
886 ConfigKey configKey(ipc->getCallingUid(), configId);
887 SubscriberReporter::getInstance()
888 .unsetBroadcastSubscriber(configKey, subscriberId);
889 *success = true;
890 return Status::ok();
891 } else {
892 *success = false;
893 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
894 }
895}
896
897
David Chen1d7b0cd2017-11-15 14:20:04 -0800898void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700899}
900
Yao Chenef99c4f2017-09-22 16:26:54 -0700901} // namespace statsd
902} // namespace os
903} // namespace android