blob: bb47653f03b0772687ea81ace629add2eb90c441 [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/**
55 * Watches for the death of the stats companion (system process).
56 */
57class CompanionDeathRecipient : public IBinder::DeathRecipient {
58public:
59 CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor);
60 virtual void binderDied(const wp<IBinder>& who);
61
62private:
63 const sp<AnomalyMonitor> mAnomalyMonitor;
64};
65
66CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor)
67 : mAnomalyMonitor(anomalyMonitor) {
68}
69
70void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) {
71 ALOGW("statscompanion service died");
72 mAnomalyMonitor->setStatsCompanionService(nullptr);
Bookatzc6977972018-01-16 16:55:05 -080073 SubscriberReporter::getInstance().setStatsCompanionService(nullptr);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070074}
75
76// ======================================================================
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070077StatsService::StatsService(const sp<Looper>& handlerLooper)
Bookatz1d0136d2017-12-01 11:13:32 -080078 : mAnomalyMonitor(new AnomalyMonitor(MIN_DIFF_TO_UPDATE_REGISTERED_ALARM_SECS))
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070079{
Joe Onorato9fc9edf2017-10-15 20:08:52 -070080 mUidMap = new UidMap();
Chenjie Yu80f91122018-01-31 20:24:50 -080081 StatsPuller::SetUidMap(mUidMap);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070082 mConfigManager = new ConfigManager();
Yangster-mac330af582018-02-08 15:24:38 -080083 mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, getElapsedRealtimeSec(),
84 [this](const ConfigKey& key) {
85 sp<IStatsCompanionService> sc = getStatsCompanionService();
86 auto receiver = mConfigManager->GetConfigReceiver(key);
87 if (sc == nullptr) {
88 VLOG("Could not find StatsCompanionService");
89 } else if (receiver == nullptr) {
90 VLOG("Statscompanion could not find a broadcast receiver for %s",
91 key.ToString().c_str());
92 } else {
93 sc->sendDataBroadcast(receiver);
94 }
David Chen1d7b0cd2017-11-15 14:20:04 -080095 }
Yangster-mac330af582018-02-08 15:24:38 -080096 );
Joe Onorato9fc9edf2017-10-15 20:08:52 -070097
98 mConfigManager->AddListener(mProcessor);
99
100 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700101}
102
Yao Chenef99c4f2017-09-22 16:26:54 -0700103StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700104}
105
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700106void StatsService::init_system_properties() {
107 mEngBuild = false;
108 const prop_info* buildType = __system_property_find("ro.build.type");
109 if (buildType != NULL) {
110 __system_property_read_callback(buildType, init_build_type_callback, this);
111 }
David Chen0656b7a2017-09-13 15:53:39 -0700112}
113
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700114void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
115 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700116 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700117 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
118 }
119}
120
121/**
122 * Implement our own because the default binder implementation isn't
123 * properly handling SHELL_COMMAND_TRANSACTION.
124 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700125status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
126 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700127 status_t err;
128
129 switch (code) {
130 case SHELL_COMMAND_TRANSACTION: {
131 int in = data.readFileDescriptor();
132 int out = data.readFileDescriptor();
133 int err = data.readFileDescriptor();
134 int argc = data.readInt32();
135 Vector<String8> args;
136 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
137 args.add(String8(data.readString16()));
138 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700139 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
140 sp<IResultReceiver> resultReceiver =
141 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700142
143 FILE* fin = fdopen(in, "r");
144 FILE* fout = fdopen(out, "w");
145 FILE* ferr = fdopen(err, "w");
146
147 if (fin == NULL || fout == NULL || ferr == NULL) {
148 resultReceiver->send(NO_MEMORY);
149 } else {
150 err = command(fin, fout, ferr, args);
151 resultReceiver->send(err);
152 }
153
154 if (fin != NULL) {
155 fflush(fin);
156 fclose(fin);
157 }
158 if (fout != NULL) {
159 fflush(fout);
160 fclose(fout);
161 }
162 if (fout != NULL) {
163 fflush(ferr);
164 fclose(ferr);
165 }
166
167 return NO_ERROR;
168 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700169 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700170 }
171}
172
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700173/**
174 * Write debugging data about statsd.
175 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700176status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700177 FILE* out = fdopen(fd, "w");
178 if (out == NULL) {
179 return NO_MEMORY; // the fd is already open
180 }
181
Yao Chen884c8c12018-01-26 10:36:25 -0800182 bool verbose = false;
183 if (args.size() > 0 && !args[0].compare(String16("-v"))) {
184 verbose = true;
185 }
186
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700187 // TODO: Proto format for incident reports
Yao Chen884c8c12018-01-26 10:36:25 -0800188 dump_impl(out, verbose);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700189
190 fclose(out);
191 return NO_ERROR;
192}
193
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700194/**
195 * Write debugging data about statsd in text format.
196 */
Yao Chen884c8c12018-01-26 10:36:25 -0800197void StatsService::dump_impl(FILE* out, bool verbose) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800198 StatsdStats::getInstance().dumpStats(out);
Yao Chen884c8c12018-01-26 10:36:25 -0800199 mProcessor->dumpStates(out, verbose);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700200}
201
202/**
203 * Implementation of the adb shell cmd stats command.
204 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700205status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700206 // TODO: Permission check
207
208 const int argCount = args.size();
209 if (argCount >= 1) {
210 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700211 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700212 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700213 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700214
David Chende701692017-10-05 13:16:02 -0700215 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800216 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700217 }
Yao Chen729093d2017-10-16 10:33:26 -0700218
219 if (!args[0].compare(String8("dump-report"))) {
220 return cmd_dump_report(out, err, args);
221 }
David Chen1481fe12017-10-16 13:16:34 -0700222
223 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
224 return cmd_print_pulled_metrics(out, args);
225 }
David Chenadaf8b32017-11-03 15:42:08 -0700226
227 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800228 return cmd_trigger_broadcast(out, args);
229 }
230
231 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800232 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700233 }
yro87d983c2017-11-14 21:31:43 -0800234
Yao Chen8d9989b2017-11-18 18:54:50 -0800235 if (!args[0].compare(String8("meminfo"))) {
236 return cmd_dump_memory_info(out);
237 }
yro947fbce2017-11-15 22:50:23 -0800238
239 if (!args[0].compare(String8("write-to-disk"))) {
240 return cmd_write_data_to_disk(out);
241 }
Bookatzb223c4e2018-02-01 15:35:04 -0800242
243 if (!args[0].compare(String8("log-app-hook"))) {
244 return cmd_log_app_hook(out, args);
245 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800246
247 if (!args[0].compare(String8("clear-puller-cache"))) {
248 return cmd_clear_puller_cache(out);
249 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700250 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700251
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700252 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700253 return NO_ERROR;
254}
255
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700256void StatsService::print_cmd_help(FILE* out) {
257 fprintf(out,
258 "usage: adb shell cmd stats print-stats-log [tag_required] "
259 "[timestamp_nsec_optional]\n");
260 fprintf(out, "\n");
261 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800262 fprintf(out, "usage: adb shell cmd stats meminfo\n");
263 fprintf(out, "\n");
264 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
265 fprintf(out, " # adb shell stop\n");
266 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
267 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
268 fprintf(out, " # adb shell start\n");
269 fprintf(out, "\n");
270 fprintf(out, "\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800271 fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700272 fprintf(out, "\n");
273 fprintf(out, " Prints the UID, app name, version mapping.\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800274 fprintf(out, " PKG Optional package name to print the uids of the package\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700275 fprintf(out, "\n");
276 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800277 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700278 fprintf(out, "\n");
279 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
280 fprintf(out, "\n");
281 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800282 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
283 fprintf(out, "\n");
284 fprintf(out, " Flushes all data on memory to disk.\n");
285 fprintf(out, "\n");
286 fprintf(out, "\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800287 fprintf(out, "usage: adb shell cmd stats log-app-hook [UID] LABEL STATE\n");
288 fprintf(out, " Writes an AppHook event to the statslog buffer.\n");
289 fprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
290 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
291 fprintf(out, " uid is used.\n");
292 fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
293 fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
294 fprintf(out, "\n");
295 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800296 fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700297 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
298 fprintf(out, "\n");
299 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
yro74fed972017-11-27 14:42:42 -0800300 fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
301 fprintf(out, " provided, then all configs will be removed from memory and disk.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700302 fprintf(out, "\n");
303 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
yro74fed972017-11-27 14:42:42 -0800304 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700305 fprintf(out, " uid is used.\n");
306 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700307 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800308 fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
309 fprintf(out, "\n be removed from memory and disk!\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700310 fprintf(out, "\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800311 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700312 fprintf(out, " Dump all metric data for a configuration.\n");
313 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
314 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
315 fprintf(out, " calling uid is used.\n");
316 fprintf(out, " NAME The name of the configuration\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800317 fprintf(out, " --proto Print proto binary.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700318 fprintf(out, "\n");
319 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800320 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
321 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
322 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
323 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
324 fprintf(out, " calling uid is used.\n");
325 fprintf(out, " NAME The name of the configuration\n");
326 fprintf(out, "\n");
327 fprintf(out, "\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800328 fprintf(out, "usage: adb shell cmd stats print-stats\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800329 fprintf(out, " Prints some basic stats.\n");
Chenjie Yufa22d652018-02-05 14:37:48 -0800330 fprintf(out, "\n");
331 fprintf(out, "\n");
332 fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
333 fprintf(out, " Clear cached puller data.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700334}
335
David Chen1d7b0cd2017-11-15 14:20:04 -0800336status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
337 string name;
338 bool good = false;
339 int uid;
340 const int argCount = args.size();
341 if (argCount == 2) {
342 // Automatically pick the UID
343 uid = IPCThreadState::self()->getCallingUid();
344 // TODO: What if this isn't a binder call? Should we fail?
345 name.assign(args[1].c_str(), args[1].size());
346 good = true;
347 } else if (argCount == 3) {
348 // If it's a userdebug or eng build, then the shell user can
349 // impersonate other uids.
350 if (mEngBuild) {
351 const char* s = args[1].c_str();
352 if (*s != '\0') {
353 char* end = NULL;
354 uid = strtol(s, &end, 0);
355 if (*end == '\0') {
356 name.assign(args[2].c_str(), args[2].size());
357 good = true;
358 }
359 }
360 } else {
361 fprintf(out,
362 "The metrics can only be dumped for other UIDs on eng or userdebug "
363 "builds.\n");
364 }
365 }
366 if (!good) {
367 print_cmd_help(out);
368 return UNKNOWN_ERROR;
369 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800370 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name)));
yro4d889e62017-11-17 15:44:48 -0800371 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen661f7912018-01-22 17:46:24 -0800372 if (sc == nullptr) {
373 VLOG("Could not access statsCompanion");
374 } else if (receiver == nullptr) {
375 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str())
376 } else {
377 sc->sendDataBroadcast(receiver);
yro74fed972017-11-27 14:42:42 -0800378 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
379 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800380 }
381
David Chenadaf8b32017-11-03 15:42:08 -0700382 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700383}
384
385status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
386 const int argCount = args.size();
387 if (argCount >= 2) {
388 if (args[1] == "update" || args[1] == "remove") {
389 bool good = false;
390 int uid = -1;
391 string name;
392
393 if (argCount == 3) {
394 // Automatically pick the UID
395 uid = IPCThreadState::self()->getCallingUid();
396 // TODO: What if this isn't a binder call? Should we fail?
397 name.assign(args[2].c_str(), args[2].size());
398 good = true;
399 } else if (argCount == 4) {
400 // If it's a userdebug or eng build, then the shell user can
401 // impersonate other uids.
402 if (mEngBuild) {
403 const char* s = args[2].c_str();
404 if (*s != '\0') {
405 char* end = NULL;
406 uid = strtol(s, &end, 0);
407 if (*end == '\0') {
408 name.assign(args[3].c_str(), args[3].size());
409 good = true;
410 }
411 }
412 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700413 fprintf(err,
414 "The config can only be set for other UIDs on eng or userdebug "
415 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700416 }
yroe5f82922018-01-22 18:37:27 -0800417 } else if (argCount == 2 && args[1] == "remove") {
418 good = true;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700419 }
420
421 if (!good) {
422 // If arg parsing failed, print the help text and return an error.
423 print_cmd_help(out);
424 return UNKNOWN_ERROR;
425 }
426
427 if (args[1] == "update") {
428 // Read stream into buffer.
429 string buffer;
430 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
431 fprintf(err, "Error reading stream for StatsConfig.\n");
432 return UNKNOWN_ERROR;
433 }
434
435 // Parse buffer.
436 StatsdConfig config;
437 if (!config.ParseFromString(buffer)) {
438 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
439 return UNKNOWN_ERROR;
440 }
441
442 // Add / update the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800443 mConfigManager->UpdateConfig(ConfigKey(uid, StrToInt64(name)), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700444 } else {
yro74fed972017-11-27 14:42:42 -0800445 if (argCount == 2) {
446 cmd_remove_all_configs(out);
447 } else {
448 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800449 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800450 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700451 }
452
453 return NO_ERROR;
454 }
David Chen0656b7a2017-09-13 15:53:39 -0700455 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700456 print_cmd_help(out);
457 return UNKNOWN_ERROR;
458}
459
Yao Chen729093d2017-10-16 10:33:26 -0700460status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
461 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800462 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700463 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800464 bool proto = false;
Yao Chen729093d2017-10-16 10:33:26 -0700465 int uid;
466 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800467 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
468 proto = true;
469 argCount -= 1;
470 }
Yao Chen729093d2017-10-16 10:33:26 -0700471 if (argCount == 2) {
472 // Automatically pick the UID
473 uid = IPCThreadState::self()->getCallingUid();
474 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a3792017-10-30 22:57:06 -0700475 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700476 good = true;
477 } else if (argCount == 3) {
478 // If it's a userdebug or eng build, then the shell user can
479 // impersonate other uids.
480 if (mEngBuild) {
481 const char* s = args[1].c_str();
482 if (*s != '\0') {
483 char* end = NULL;
484 uid = strtol(s, &end, 0);
485 if (*end == '\0') {
486 name.assign(args[2].c_str(), args[2].size());
487 good = true;
488 }
489 }
490 } else {
491 fprintf(out,
492 "The metrics can only be dumped for other UIDs on eng or userdebug "
493 "builds.\n");
494 }
495 }
496 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800497 vector<uint8_t> data;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800498 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), time(nullptr) * NS_PER_SEC,
499 &data);
Yao Chen729093d2017-10-16 10:33:26 -0700500 // TODO: print the returned StatsLogReport to file instead of printing to logcat.
Chenjie Yub236c862017-11-28 22:20:44 -0800501 if (proto) {
502 for (size_t i = 0; i < data.size(); i ++) {
503 fprintf(out, "%c", data[i]);
504 }
505 } else {
506 fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str());
507 fprintf(out, "See the StatsLogReport in logcat...\n");
508 }
Yao Chen729093d2017-10-16 10:33:26 -0700509 return android::OK;
510 } else {
511 // If arg parsing failed, print the help text and return an error.
512 print_cmd_help(out);
513 return UNKNOWN_ERROR;
514 }
515 } else {
516 fprintf(out, "Log processor does not exist...\n");
517 return UNKNOWN_ERROR;
518 }
519}
520
Yao Chenb3561512017-11-21 18:07:17 -0800521status_t StatsService::cmd_print_stats(FILE* out, const Vector<String8>& args) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800522 vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys();
523 for (const ConfigKey& key : configs) {
524 fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(),
525 mProcessor->GetMetricsSize(key));
526 }
Yao Chenb3561512017-11-21 18:07:17 -0800527 StatsdStats& statsdStats = StatsdStats::getInstance();
Yao Chenf5acabe2018-01-17 14:10:34 -0800528 statsdStats.dumpStats(out);
David Chen1d7b0cd2017-11-15 14:20:04 -0800529 return NO_ERROR;
530}
531
Yao Chend10f7b12017-12-18 12:53:50 -0800532status_t StatsService::cmd_print_uid_map(FILE* out, const Vector<String8>& args) {
533 if (args.size() > 1) {
534 string pkg;
535 pkg.assign(args[1].c_str(), args[1].size());
536 auto uids = mUidMap->getAppUid(pkg);
537 fprintf(out, "%s -> [ ", pkg.c_str());
538 for (const auto& uid : uids) {
539 fprintf(out, "%d ", uid);
540 }
541 fprintf(out, "]\n");
542 } else {
543 mUidMap->printUidMap(out);
544 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700545 return NO_ERROR;
David Chen0656b7a2017-09-13 15:53:39 -0700546}
547
yro947fbce2017-11-15 22:50:23 -0800548status_t StatsService::cmd_write_data_to_disk(FILE* out) {
549 fprintf(out, "Writing data to disk\n");
550 mProcessor->WriteDataToDisk();
551 return NO_ERROR;
552}
553
Bookatzb223c4e2018-02-01 15:35:04 -0800554status_t StatsService::cmd_log_app_hook(FILE* out, const Vector<String8>& args) {
555 bool good = false;
556 int32_t uid;
557 int32_t label;
558 int32_t state;
559 const int argCount = args.size();
560 if (argCount == 3) {
561 // Automatically pick the UID
562 uid = IPCThreadState::self()->getCallingUid();
563 label = atoi(args[1].c_str());
564 state = atoi(args[2].c_str());
565 good = true;
566 } else if (argCount == 4) {
567 uid = atoi(args[1].c_str());
568 // If it's a userdebug or eng build, then the shell user can impersonate other uids.
569 // Otherwise, the uid must match the actual caller's uid.
570 if (mEngBuild || (uid >= 0 && (uid_t)uid == IPCThreadState::self()->getCallingUid())) {
571 label = atoi(args[2].c_str());
572 state = atoi(args[3].c_str());
573 good = true;
574 } else {
575 fprintf(out,
576 "Selecting a UID for writing AppHook can only be dumped for other UIDs on eng"
577 " or userdebug builds.\n");
578 }
579 }
580 if (good) {
581 fprintf(out, "Logging AppHook(%d, %d, %d) to statslog.\n", uid, label, state);
582 android::util::stats_write(android::util::APP_HOOK, uid, label, state);
583 } else {
584 print_cmd_help(out);
585 return UNKNOWN_ERROR;
586 }
587 return NO_ERROR;
588}
589
David Chen1481fe12017-10-16 13:16:34 -0700590status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) {
591 int s = atoi(args[1].c_str());
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700592 vector<shared_ptr<LogEvent> > stats;
593 if (mStatsPullerManager.Pull(s, &stats)) {
594 for (const auto& it : stats) {
595 fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str());
596 }
597 fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size());
598 return NO_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700599 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700600 return UNKNOWN_ERROR;
David Chen1481fe12017-10-16 13:16:34 -0700601}
602
yro74fed972017-11-27 14:42:42 -0800603status_t StatsService::cmd_remove_all_configs(FILE* out) {
604 fprintf(out, "Removing all configs...\n");
605 VLOG("StatsService::cmd_remove_all_configs was called");
606 mConfigManager->RemoveAllConfigs();
yro947fbce2017-11-15 22:50:23 -0800607 StorageManager::deleteAllFiles(STATS_SERVICE_DIR);
yro87d983c2017-11-14 21:31:43 -0800608 return NO_ERROR;
609}
610
Yao Chen8d9989b2017-11-18 18:54:50 -0800611status_t StatsService::cmd_dump_memory_info(FILE* out) {
612 std::string s = dumpMemInfo(100);
613 fprintf(out, "Memory Info\n");
614 fprintf(out, "%s", s.c_str());
615 return NO_ERROR;
616}
617
Chenjie Yue72252b2018-02-01 13:19:35 -0800618status_t StatsService::cmd_clear_puller_cache(FILE* out) {
Chenjie Yufa22d652018-02-05 14:37:48 -0800619 IPCThreadState* ipc = IPCThreadState::self();
620 VLOG("StatsService::cmd_clear_puller_cache with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
621 if (checkCallingPermission(String16(kPermissionDump))) {
622 int cleared = mStatsPullerManager.ForceClearPullerCache();
623 fprintf(out, "Puller removed %d cached data!\n", cleared);
624 return NO_ERROR;
625 } else {
626 return PERMISSION_DENIED;
627 }
Chenjie Yue72252b2018-02-01 13:19:35 -0800628}
629
Dianne Hackborn3accca02013-09-20 09:32:11 -0700630Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
David Chende701692017-10-05 13:16:02 -0700631 const vector<String16>& app) {
yro74fed972017-11-27 14:42:42 -0800632 VLOG("StatsService::informAllUidData was called");
David Chende701692017-10-05 13:16:02 -0700633
634 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
635 return Status::fromExceptionCode(Status::EX_SECURITY,
636 "Only system uid can call informAllUidData");
637 }
638
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700639 mUidMap->updateMap(uid, version, app);
yro74fed972017-11-27 14:42:42 -0800640 VLOG("StatsService::informAllUidData succeeded");
David Chende701692017-10-05 13:16:02 -0700641
642 return Status::ok();
643}
644
Dianne Hackborn3accca02013-09-20 09:32:11 -0700645Status StatsService::informOnePackage(const String16& app, int32_t uid, int64_t version) {
yro74fed972017-11-27 14:42:42 -0800646 VLOG("StatsService::informOnePackage was called");
David Chende701692017-10-05 13:16:02 -0700647
648 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
649 return Status::fromExceptionCode(Status::EX_SECURITY,
650 "Only system uid can call informOnePackage");
651 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700652 mUidMap->updateApp(app, uid, version);
David Chende701692017-10-05 13:16:02 -0700653 return Status::ok();
654}
655
656Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) {
yro74fed972017-11-27 14:42:42 -0800657 VLOG("StatsService::informOnePackageRemoved was called");
David Chende701692017-10-05 13:16:02 -0700658
659 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
660 return Status::fromExceptionCode(Status::EX_SECURITY,
661 "Only system uid can call informOnePackageRemoved");
662 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700663 mUidMap->removeApp(app, uid);
David Chende701692017-10-05 13:16:02 -0700664 return Status::ok();
665}
666
Yao Chenef99c4f2017-09-22 16:26:54 -0700667Status StatsService::informAnomalyAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800668 VLOG("StatsService::informAnomalyAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700669
670 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
671 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700672 "Only system uid can call informAnomalyAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700673 }
Yangster-mac330af582018-02-08 15:24:38 -0800674 uint64_t currentTimeSec = getElapsedRealtimeSec();
Yangster-mace2cd6d52017-11-09 20:38:30 -0800675 std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet =
Bookatzcc5adef22017-11-21 14:36:23 -0800676 mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
Bookatz66fe0612018-02-07 18:51:48 -0800677 if (anomalySet.size() > 0) {
678 VLOG("Found an anomaly alarm that fired.");
679 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, anomalySet);
680 } else {
681 VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
682 }
Bookatz1b0b1142017-09-08 11:58:42 -0700683 return Status::ok();
684}
685
Yao Chenef99c4f2017-09-22 16:26:54 -0700686Status StatsService::informPollAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800687 VLOG("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700688
689 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
690 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700691 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700692 }
693
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700694 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700695
yro74fed972017-11-27 14:42:42 -0800696 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700697
698 return Status::ok();
699}
700
Yao Chenef99c4f2017-09-22 16:26:54 -0700701Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700702 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
703 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700704 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700705 }
706
707 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800708 VLOG("StatsService::systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700709
Bookatzb487b552017-09-18 11:26:01 -0700710 sayHiToStatsCompanion();
711
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700712 return Status::ok();
713}
714
yro947fbce2017-11-15 22:50:23 -0800715Status StatsService::writeDataToDisk() {
716 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
717 return Status::fromExceptionCode(Status::EX_SECURITY,
718 "Only system uid can call systemRunning");
719 }
720
yro74fed972017-11-27 14:42:42 -0800721 VLOG("StatsService::writeDataToDisk");
yro947fbce2017-11-15 22:50:23 -0800722
723 mProcessor->WriteDataToDisk();
724
725 return Status::ok();
726}
727
Yao Chenef99c4f2017-09-22 16:26:54 -0700728void StatsService::sayHiToStatsCompanion() {
729 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
730 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700731 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
732 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800733 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700734 statsCompanion->statsdReady();
735 } else {
yro74fed972017-11-27 14:42:42 -0800736 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700737 }
738}
739
Yao Chenef99c4f2017-09-22 16:26:54 -0700740sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700741 sp<IStatsCompanionService> statsCompanion = nullptr;
742 // Get statscompanion service from service manager
743 const sp<IServiceManager> sm(defaultServiceManager());
744 if (sm != nullptr) {
745 const String16 name("statscompanion");
746 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
747 if (statsCompanion == nullptr) {
748 ALOGW("statscompanion service unavailable!");
749 return nullptr;
750 }
751 }
752 return statsCompanion;
753}
754
Yao Chenef99c4f2017-09-22 16:26:54 -0700755Status StatsService::statsCompanionReady() {
yro74fed972017-11-27 14:42:42 -0800756 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700757
758 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
759 return Status::fromExceptionCode(Status::EX_SECURITY,
760 "Only system uid can call statsCompanionReady");
761 }
762
763 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
764 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700765 return Status::fromExceptionCode(
766 Status::EX_NULL_POINTER,
767 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700768 }
yro74fed972017-11-27 14:42:42 -0800769 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700770 IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor));
Bookatzb487b552017-09-18 11:26:01 -0700771 mAnomalyMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800772 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700773
774 return Status::ok();
775}
776
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700777void StatsService::Startup() {
778 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700779}
780
Yangster-macd40053e2018-01-09 16:29:22 -0800781void StatsService::OnLogEvent(LogEvent* event) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700782 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700783}
784
Yangster-mac94e197c2018-01-02 16:03:03 -0800785Status StatsService::getData(int64_t key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700786 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800787 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800788 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800789 ConfigKey configKey(ipc->getCallingUid(), key);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800790 mProcessor->onDumpReport(configKey, time(nullptr) * NS_PER_SEC, output);
David Chenadaf8b32017-11-03 15:42:08 -0700791 return Status::ok();
792 } else {
793 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
794 }
yro31eb67b2017-10-24 13:33:21 -0700795}
796
David Chen2e8f3802017-11-22 10:56:48 -0800797Status StatsService::getMetadata(vector<uint8_t>* output) {
798 IPCThreadState* ipc = IPCThreadState::self();
799 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
800 ipc->getCallingUid());
801 if (checkCallingPermission(String16(kPermissionDump))) {
802 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
803 return Status::ok();
804 } else {
805 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
806 }
807}
808
Yangster-mac94e197c2018-01-02 16:03:03 -0800809Status StatsService::addConfiguration(int64_t key,
David Chenadaf8b32017-11-03 15:42:08 -0700810 const vector <uint8_t>& config,
David Chenadaf8b32017-11-03 15:42:08 -0700811 bool* success) {
812 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800813 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800814 ConfigKey configKey(ipc->getCallingUid(), key);
David Chenadaf8b32017-11-03 15:42:08 -0700815 StatsdConfig cfg;
Yangster-macbbd056a2018-01-22 13:37:02 -0800816 if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) {
David Chen7d8aa4d2017-12-27 13:37:01 -0800817 *success = false;
818 return Status::ok();
819 }
David Chenadaf8b32017-11-03 15:42:08 -0700820 mConfigManager->UpdateConfig(configKey, cfg);
David Chen661f7912018-01-22 17:46:24 -0800821 *success = true;
822 return Status::ok();
823 } else {
824 *success = false;
825 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
826 }
827}
828
829Status StatsService::removeDataFetchOperation(int64_t key, bool* success) {
830 IPCThreadState* ipc = IPCThreadState::self();
831 if (checkCallingPermission(String16(kPermissionDump))) {
832 ConfigKey configKey(ipc->getCallingUid(), key);
833 mConfigManager->RemoveConfigReceiver(configKey);
834 *success = true;
835 return Status::ok();
836 } else {
837 *success = false;
838 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
839 }
840}
841
842Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender,
843 bool* success) {
844 IPCThreadState* ipc = IPCThreadState::self();
845 if (checkCallingPermission(String16(kPermissionDump))) {
846 ConfigKey configKey(ipc->getCallingUid(), key);
847 mConfigManager->SetConfigReceiver(configKey, intentSender);
David Chenadaf8b32017-11-03 15:42:08 -0700848 *success = true;
849 return Status::ok();
850 } else {
Yao Chenaa39bc72017-12-01 11:16:50 -0800851 *success = false;
David Chenadaf8b32017-11-03 15:42:08 -0700852 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700853 }
yro31eb67b2017-10-24 13:33:21 -0700854}
855
Yangster-mac94e197c2018-01-02 16:03:03 -0800856Status StatsService::removeConfiguration(int64_t key, bool* success) {
David Chenadaf8b32017-11-03 15:42:08 -0700857 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800858 if (checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800859 ConfigKey configKey(ipc->getCallingUid(), key);
860 mConfigManager->RemoveConfig(configKey);
861 SubscriberReporter::getInstance().removeConfig(configKey);
Yao Chenaa39bc72017-12-01 11:16:50 -0800862 *success = true;
David Chenadaf8b32017-11-03 15:42:08 -0700863 return Status::ok();
864 } else {
865 *success = false;
866 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700867 }
yro31eb67b2017-10-24 13:33:21 -0700868}
869
Bookatzc6977972018-01-16 16:55:05 -0800870Status StatsService::setBroadcastSubscriber(int64_t configId,
871 int64_t subscriberId,
872 const sp<android::IBinder>& intentSender,
873 bool* success) {
874 VLOG("StatsService::setBroadcastSubscriber called.");
875 IPCThreadState* ipc = IPCThreadState::self();
876 if (checkCallingPermission(String16(kPermissionDump))) {
877 ConfigKey configKey(ipc->getCallingUid(), configId);
878 SubscriberReporter::getInstance()
879 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
880 *success = true;
881 return Status::ok();
882 } else {
883 *success = false;
884 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
885 }
886}
887
888Status StatsService::unsetBroadcastSubscriber(int64_t configId,
889 int64_t subscriberId,
890 bool* success) {
891 VLOG("StatsService::unsetBroadcastSubscriber called.");
892 IPCThreadState* ipc = IPCThreadState::self();
893 if (checkCallingPermission(String16(kPermissionDump))) {
894 ConfigKey configKey(ipc->getCallingUid(), configId);
895 SubscriberReporter::getInstance()
896 .unsetBroadcastSubscriber(configKey, subscriberId);
897 *success = true;
898 return Status::ok();
899 } else {
900 *success = false;
901 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
902 }
903}
904
905
David Chen1d7b0cd2017-11-15 14:20:04 -0800906void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700907}
908
Yao Chenef99c4f2017-09-22 16:26:54 -0700909} // namespace statsd
910} // namespace os
911} // namespace android