blob: b92e897786b1449d5ec6978bc6993f3feae5974e [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"
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 Chen661f7912018-01-22 17:46:24 -080087 } else if (receiver == nullptr) {
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 {
David Chen661f7912018-01-22 17:46:24 -080091 sc->sendDataBroadcast(receiver);
David Chen1d7b0cd2017-11-15 14:20:04 -080092 }
yro31eb67b2017-10-24 13:33:21 -070093 });
Joe Onorato9fc9edf2017-10-15 20:08:52 -070094
95 mConfigManager->AddListener(mProcessor);
96
97 init_system_properties();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070098}
99
Yao Chenef99c4f2017-09-22 16:26:54 -0700100StatsService::~StatsService() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700101}
102
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700103void StatsService::init_system_properties() {
104 mEngBuild = false;
105 const prop_info* buildType = __system_property_find("ro.build.type");
106 if (buildType != NULL) {
107 __system_property_read_callback(buildType, init_build_type_callback, this);
108 }
David Chen0656b7a2017-09-13 15:53:39 -0700109}
110
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700111void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value,
112 uint32_t serial) {
Yao Chen729093d2017-10-16 10:33:26 -0700113 if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700114 reinterpret_cast<StatsService*>(cookie)->mEngBuild = true;
115 }
116}
117
118/**
119 * Implement our own because the default binder implementation isn't
120 * properly handling SHELL_COMMAND_TRANSACTION.
121 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700122status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
123 uint32_t flags) {
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700124 switch (code) {
125 case SHELL_COMMAND_TRANSACTION: {
126 int in = data.readFileDescriptor();
127 int out = data.readFileDescriptor();
128 int err = data.readFileDescriptor();
129 int argc = data.readInt32();
130 Vector<String8> args;
131 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
132 args.add(String8(data.readString16()));
133 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700134 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
135 sp<IResultReceiver> resultReceiver =
136 IResultReceiver::asInterface(data.readStrongBinder());
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700137
138 FILE* fin = fdopen(in, "r");
139 FILE* fout = fdopen(out, "w");
140 FILE* ferr = fdopen(err, "w");
141
142 if (fin == NULL || fout == NULL || ferr == NULL) {
143 resultReceiver->send(NO_MEMORY);
144 } else {
145 err = command(fin, fout, ferr, args);
146 resultReceiver->send(err);
147 }
148
149 if (fin != NULL) {
150 fflush(fin);
151 fclose(fin);
152 }
153 if (fout != NULL) {
154 fflush(fout);
155 fclose(fout);
156 }
157 if (fout != NULL) {
158 fflush(ferr);
159 fclose(ferr);
160 }
161
162 return NO_ERROR;
163 }
Yao Chenef99c4f2017-09-22 16:26:54 -0700164 default: { return BnStatsManager::onTransact(code, data, reply, flags); }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700165 }
166}
167
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700168/**
169 * Write debugging data about statsd.
170 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700171status_t StatsService::dump(int fd, const Vector<String16>& args) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700172 FILE* out = fdopen(fd, "w");
173 if (out == NULL) {
174 return NO_MEMORY; // the fd is already open
175 }
176
Yao Chen884c8c12018-01-26 10:36:25 -0800177 bool verbose = false;
178 if (args.size() > 0 && !args[0].compare(String16("-v"))) {
179 verbose = true;
180 }
181
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700182 // TODO: Proto format for incident reports
Yao Chen884c8c12018-01-26 10:36:25 -0800183 dump_impl(out, verbose);
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700184
185 fclose(out);
186 return NO_ERROR;
187}
188
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700189/**
190 * Write debugging data about statsd in text format.
191 */
Yao Chen884c8c12018-01-26 10:36:25 -0800192void StatsService::dump_impl(FILE* out, bool verbose) {
Yao Chenf5acabe2018-01-17 14:10:34 -0800193 StatsdStats::getInstance().dumpStats(out);
Yao Chen884c8c12018-01-26 10:36:25 -0800194 mProcessor->dumpStates(out, verbose);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700195}
196
197/**
198 * Implementation of the adb shell cmd stats command.
199 */
Yao Chenef99c4f2017-09-22 16:26:54 -0700200status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700201 // TODO: Permission check
202
203 const int argCount = args.size();
204 if (argCount >= 1) {
205 // adb shell cmd stats config ...
David Chen0656b7a2017-09-13 15:53:39 -0700206 if (!args[0].compare(String8("config"))) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700207 return cmd_config(in, out, err, args);
David Chen0656b7a2017-09-13 15:53:39 -0700208 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700209
David Chende701692017-10-05 13:16:02 -0700210 if (!args[0].compare(String8("print-uid-map"))) {
Yao Chend10f7b12017-12-18 12:53:50 -0800211 return cmd_print_uid_map(out, args);
David Chende701692017-10-05 13:16:02 -0700212 }
Yao Chen729093d2017-10-16 10:33:26 -0700213
214 if (!args[0].compare(String8("dump-report"))) {
215 return cmd_dump_report(out, err, args);
216 }
David Chen1481fe12017-10-16 13:16:34 -0700217
218 if (!args[0].compare(String8("pull-source")) && args.size() > 1) {
219 return cmd_print_pulled_metrics(out, args);
220 }
David Chenadaf8b32017-11-03 15:42:08 -0700221
222 if (!args[0].compare(String8("send-broadcast"))) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800223 return cmd_trigger_broadcast(out, args);
224 }
225
226 if (!args[0].compare(String8("print-stats"))) {
Yao Chenb3561512017-11-21 18:07:17 -0800227 return cmd_print_stats(out, args);
David Chenadaf8b32017-11-03 15:42:08 -0700228 }
yro87d983c2017-11-14 21:31:43 -0800229
Yao Chen8d9989b2017-11-18 18:54:50 -0800230 if (!args[0].compare(String8("meminfo"))) {
231 return cmd_dump_memory_info(out);
232 }
yro947fbce2017-11-15 22:50:23 -0800233
234 if (!args[0].compare(String8("write-to-disk"))) {
235 return cmd_write_data_to_disk(out);
236 }
Bookatzb223c4e2018-02-01 15:35:04 -0800237
238 if (!args[0].compare(String8("log-app-hook"))) {
239 return cmd_log_app_hook(out, args);
240 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800241
242 if (!args[0].compare(String8("clear-puller-cache"))) {
243 return cmd_clear_puller_cache(out);
244 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700245 }
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700246
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700247 print_cmd_help(out);
Joe Onorato2cbc2cc2017-08-30 17:03:23 -0700248 return NO_ERROR;
249}
250
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700251void StatsService::print_cmd_help(FILE* out) {
252 fprintf(out,
253 "usage: adb shell cmd stats print-stats-log [tag_required] "
254 "[timestamp_nsec_optional]\n");
255 fprintf(out, "\n");
256 fprintf(out, "\n");
Yao Chen8d9989b2017-11-18 18:54:50 -0800257 fprintf(out, "usage: adb shell cmd stats meminfo\n");
258 fprintf(out, "\n");
259 fprintf(out, " Prints the malloc debug information. You need to run the following first: \n");
260 fprintf(out, " # adb shell stop\n");
261 fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n");
262 fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n");
263 fprintf(out, " # adb shell start\n");
264 fprintf(out, "\n");
265 fprintf(out, "\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800266 fprintf(out, "usage: adb shell cmd stats print-uid-map [PKG]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700267 fprintf(out, "\n");
268 fprintf(out, " Prints the UID, app name, version mapping.\n");
Yao Chend10f7b12017-12-18 12:53:50 -0800269 fprintf(out, " PKG Optional package name to print the uids of the package\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700270 fprintf(out, "\n");
271 fprintf(out, "\n");
yro3fca5ba2017-11-17 13:22:52 -0800272 fprintf(out, "usage: adb shell cmd stats pull-source [int] \n");
David Chen1481fe12017-10-16 13:16:34 -0700273 fprintf(out, "\n");
274 fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n");
275 fprintf(out, "\n");
276 fprintf(out, "\n");
yro947fbce2017-11-15 22:50:23 -0800277 fprintf(out, "usage: adb shell cmd stats write-to-disk \n");
278 fprintf(out, "\n");
279 fprintf(out, " Flushes all data on memory to disk.\n");
280 fprintf(out, "\n");
281 fprintf(out, "\n");
Bookatzb223c4e2018-02-01 15:35:04 -0800282 fprintf(out, "usage: adb shell cmd stats log-app-hook [UID] LABEL STATE\n");
283 fprintf(out, " Writes an AppHook event to the statslog buffer.\n");
284 fprintf(out, " UID The uid to use. It is only possible to pass a UID\n");
285 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
286 fprintf(out, " uid is used.\n");
287 fprintf(out, " LABEL Integer in [0, 15], as per atoms.proto.\n");
288 fprintf(out, " STATE Integer in [0, 3], as per atoms.proto.\n");
289 fprintf(out, "\n");
290 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800291 fprintf(out, "usage: adb shell cmd stats config remove [UID] [NAME]\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700292 fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n");
293 fprintf(out, "\n");
294 fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n");
yro74fed972017-11-27 14:42:42 -0800295 fprintf(out, " wire-encoded protobuf format and passed via stdin. If no UID and name is\n");
296 fprintf(out, " provided, then all configs will be removed from memory and disk.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700297 fprintf(out, "\n");
298 fprintf(out, " UID The uid to use. It is only possible to pass the UID\n");
yro74fed972017-11-27 14:42:42 -0800299 fprintf(out, " parameter on eng builds. If UID is omitted the calling\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700300 fprintf(out, " uid is used.\n");
301 fprintf(out, " NAME The per-uid name to use\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700302 fprintf(out, "\n");
yro74fed972017-11-27 14:42:42 -0800303 fprintf(out, "\n *Note: If both UID and NAME are omitted then all configs will\n");
304 fprintf(out, "\n be removed from memory and disk!\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700305 fprintf(out, "\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800306 fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME [--proto]\n");
Yao Chen5154a3792017-10-30 22:57:06 -0700307 fprintf(out, " Dump all metric data for a configuration.\n");
308 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
309 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
310 fprintf(out, " calling uid is used.\n");
311 fprintf(out, " NAME The name of the configuration\n");
Chenjie Yub236c862017-11-28 22:20:44 -0800312 fprintf(out, " --proto Print proto binary.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700313 fprintf(out, "\n");
314 fprintf(out, "\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800315 fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n");
316 fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n");
317 fprintf(out, " UID The uid of the configuration. It is only possible to pass\n");
318 fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n");
319 fprintf(out, " calling uid is used.\n");
320 fprintf(out, " NAME The name of the configuration\n");
321 fprintf(out, "\n");
322 fprintf(out, "\n");
Yao Chenf5acabe2018-01-17 14:10:34 -0800323 fprintf(out, "usage: adb shell cmd stats print-stats\n");
David Chen1d7b0cd2017-11-15 14:20:04 -0800324 fprintf(out, " Prints some basic stats.\n");
Chenjie Yufa22d652018-02-05 14:37:48 -0800325 fprintf(out, "\n");
326 fprintf(out, "\n");
327 fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
328 fprintf(out, " Clear cached puller data.\n");
David Chenadaf8b32017-11-03 15:42:08 -0700329}
330
David Chen1d7b0cd2017-11-15 14:20:04 -0800331status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
332 string name;
333 bool good = false;
334 int uid;
335 const int argCount = args.size();
336 if (argCount == 2) {
337 // Automatically pick the UID
338 uid = IPCThreadState::self()->getCallingUid();
339 // TODO: What if this isn't a binder call? Should we fail?
340 name.assign(args[1].c_str(), args[1].size());
341 good = true;
342 } else if (argCount == 3) {
343 // If it's a userdebug or eng build, then the shell user can
344 // impersonate other uids.
345 if (mEngBuild) {
346 const char* s = args[1].c_str();
347 if (*s != '\0') {
348 char* end = NULL;
349 uid = strtol(s, &end, 0);
350 if (*end == '\0') {
351 name.assign(args[2].c_str(), args[2].size());
352 good = true;
353 }
354 }
355 } else {
356 fprintf(out,
357 "The metrics can only be dumped for other UIDs on eng or userdebug "
358 "builds.\n");
359 }
360 }
361 if (!good) {
362 print_cmd_help(out);
363 return UNKNOWN_ERROR;
364 }
Yangster-mac94e197c2018-01-02 16:03:03 -0800365 auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, StrToInt64(name)));
yro4d889e62017-11-17 15:44:48 -0800366 sp<IStatsCompanionService> sc = getStatsCompanionService();
David Chen661f7912018-01-22 17:46:24 -0800367 if (sc == nullptr) {
368 VLOG("Could not access statsCompanion");
369 } else if (receiver == nullptr) {
370 VLOG("Could not find receiver for %s, %s", args[1].c_str(), args[2].c_str())
371 } else {
372 sc->sendDataBroadcast(receiver);
yro74fed972017-11-27 14:42:42 -0800373 VLOG("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(),
374 args[2].c_str());
yro4d889e62017-11-17 15:44:48 -0800375 }
376
David Chenadaf8b32017-11-03 15:42:08 -0700377 return NO_ERROR;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700378}
379
380status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
381 const int argCount = args.size();
382 if (argCount >= 2) {
383 if (args[1] == "update" || args[1] == "remove") {
384 bool good = false;
385 int uid = -1;
386 string name;
387
388 if (argCount == 3) {
389 // Automatically pick the UID
390 uid = IPCThreadState::self()->getCallingUid();
391 // TODO: What if this isn't a binder call? Should we fail?
392 name.assign(args[2].c_str(), args[2].size());
393 good = true;
394 } else if (argCount == 4) {
395 // If it's a userdebug or eng build, then the shell user can
396 // impersonate other uids.
397 if (mEngBuild) {
398 const char* s = args[2].c_str();
399 if (*s != '\0') {
400 char* end = NULL;
401 uid = strtol(s, &end, 0);
402 if (*end == '\0') {
403 name.assign(args[3].c_str(), args[3].size());
404 good = true;
405 }
406 }
407 } else {
Yao Chen729093d2017-10-16 10:33:26 -0700408 fprintf(err,
409 "The config can only be set for other UIDs on eng or userdebug "
410 "builds.\n");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700411 }
yroe5f82922018-01-22 18:37:27 -0800412 } else if (argCount == 2 && args[1] == "remove") {
413 good = true;
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700414 }
415
416 if (!good) {
417 // If arg parsing failed, print the help text and return an error.
418 print_cmd_help(out);
419 return UNKNOWN_ERROR;
420 }
421
422 if (args[1] == "update") {
423 // Read stream into buffer.
424 string buffer;
425 if (!android::base::ReadFdToString(fileno(in), &buffer)) {
426 fprintf(err, "Error reading stream for StatsConfig.\n");
427 return UNKNOWN_ERROR;
428 }
429
430 // Parse buffer.
431 StatsdConfig config;
432 if (!config.ParseFromString(buffer)) {
433 fprintf(err, "Error parsing proto stream for StatsConfig.\n");
434 return UNKNOWN_ERROR;
435 }
436
437 // Add / update the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800438 mConfigManager->UpdateConfig(ConfigKey(uid, StrToInt64(name)), config);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700439 } else {
yro74fed972017-11-27 14:42:42 -0800440 if (argCount == 2) {
441 cmd_remove_all_configs(out);
442 } else {
443 // Remove the config.
Yangster-mac94e197c2018-01-02 16:03:03 -0800444 mConfigManager->RemoveConfig(ConfigKey(uid, StrToInt64(name)));
yro74fed972017-11-27 14:42:42 -0800445 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700446 }
447
448 return NO_ERROR;
449 }
David Chen0656b7a2017-09-13 15:53:39 -0700450 }
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700451 print_cmd_help(out);
452 return UNKNOWN_ERROR;
453}
454
Yao Chen729093d2017-10-16 10:33:26 -0700455status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) {
456 if (mProcessor != nullptr) {
Chenjie Yub236c862017-11-28 22:20:44 -0800457 int argCount = args.size();
Yao Chen729093d2017-10-16 10:33:26 -0700458 bool good = false;
Chenjie Yub236c862017-11-28 22:20:44 -0800459 bool proto = false;
Yao Chen729093d2017-10-16 10:33:26 -0700460 int uid;
461 string name;
Chenjie Yub236c862017-11-28 22:20:44 -0800462 if (!std::strcmp("--proto", args[argCount-1].c_str())) {
463 proto = true;
464 argCount -= 1;
465 }
Yao Chen729093d2017-10-16 10:33:26 -0700466 if (argCount == 2) {
467 // Automatically pick the UID
468 uid = IPCThreadState::self()->getCallingUid();
469 // TODO: What if this isn't a binder call? Should we fail?
Yao Chen5154a3792017-10-30 22:57:06 -0700470 name.assign(args[1].c_str(), args[1].size());
Yao Chen729093d2017-10-16 10:33:26 -0700471 good = true;
472 } else if (argCount == 3) {
473 // If it's a userdebug or eng build, then the shell user can
474 // impersonate other uids.
475 if (mEngBuild) {
476 const char* s = args[1].c_str();
477 if (*s != '\0') {
478 char* end = NULL;
479 uid = strtol(s, &end, 0);
480 if (*end == '\0') {
481 name.assign(args[2].c_str(), args[2].size());
482 good = true;
483 }
484 }
485 } else {
486 fprintf(out,
487 "The metrics can only be dumped for other UIDs on eng or userdebug "
488 "builds.\n");
489 }
490 }
491 if (good) {
David Chen1d7b0cd2017-11-15 14:20:04 -0800492 vector<uint8_t> data;
Yao Chen8a8d16c2018-02-08 14:50:40 -0800493 mProcessor->onDumpReport(ConfigKey(uid, StrToInt64(name)), time(nullptr) * NS_PER_SEC,
494 &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
Bookatz66fe0612018-02-07 18:51:48 -0800670 // TODO: This may be a bug. time(nullptr) can be off (wrt AlarmManager's time) and cause us to
671 // miss the alarm! Eventually we will switch to using elapsedRealTime everywhere,
672 // which may hopefully fix the problem, so we'll leave this alone for now.
Bookatzcc5adef22017-11-21 14:36:23 -0800673 uint64_t currentTimeSec = time(nullptr);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800674 std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet =
Bookatzcc5adef22017-11-21 14:36:23 -0800675 mAnomalyMonitor->popSoonerThan(static_cast<uint32_t>(currentTimeSec));
Bookatz66fe0612018-02-07 18:51:48 -0800676 if (anomalySet.size() > 0) {
677 VLOG("Found an anomaly alarm that fired.");
678 mProcessor->onAnomalyAlarmFired(currentTimeSec * NS_PER_SEC, anomalySet);
679 } else {
680 VLOG("Cannot find an anomaly alarm that fired. Perhaps it was recently cancelled.");
681 }
Bookatz1b0b1142017-09-08 11:58:42 -0700682 return Status::ok();
683}
684
Yao Chenef99c4f2017-09-22 16:26:54 -0700685Status StatsService::informPollAlarmFired() {
yro74fed972017-11-27 14:42:42 -0800686 VLOG("StatsService::informPollAlarmFired was called");
Bookatz1b0b1142017-09-08 11:58:42 -0700687
688 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
689 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700690 "Only system uid can call informPollAlarmFired");
Bookatz1b0b1142017-09-08 11:58:42 -0700691 }
692
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700693 mStatsPullerManager.OnAlarmFired();
Chenjie Yub3dda412017-10-24 13:41:59 -0700694
yro74fed972017-11-27 14:42:42 -0800695 VLOG("StatsService::informPollAlarmFired succeeded");
Bookatz1b0b1142017-09-08 11:58:42 -0700696
697 return Status::ok();
698}
699
Yao Chenef99c4f2017-09-22 16:26:54 -0700700Status StatsService::systemRunning() {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700701 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
702 return Status::fromExceptionCode(Status::EX_SECURITY,
Yao Chenef99c4f2017-09-22 16:26:54 -0700703 "Only system uid can call systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700704 }
705
706 // When system_server is up and running, schedule the dropbox task to run.
yro74fed972017-11-27 14:42:42 -0800707 VLOG("StatsService::systemRunning");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700708
Bookatzb487b552017-09-18 11:26:01 -0700709 sayHiToStatsCompanion();
710
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700711 return Status::ok();
712}
713
yro947fbce2017-11-15 22:50:23 -0800714Status StatsService::writeDataToDisk() {
715 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
716 return Status::fromExceptionCode(Status::EX_SECURITY,
717 "Only system uid can call systemRunning");
718 }
719
yro74fed972017-11-27 14:42:42 -0800720 VLOG("StatsService::writeDataToDisk");
yro947fbce2017-11-15 22:50:23 -0800721
722 mProcessor->WriteDataToDisk();
723
724 return Status::ok();
725}
726
Yao Chenef99c4f2017-09-22 16:26:54 -0700727void StatsService::sayHiToStatsCompanion() {
728 // TODO: This method needs to be private. It is temporarily public and unsecured for testing
729 // purposes.
Bookatzb487b552017-09-18 11:26:01 -0700730 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
731 if (statsCompanion != nullptr) {
yro74fed972017-11-27 14:42:42 -0800732 VLOG("Telling statsCompanion that statsd is ready");
Bookatzb487b552017-09-18 11:26:01 -0700733 statsCompanion->statsdReady();
734 } else {
yro74fed972017-11-27 14:42:42 -0800735 VLOG("Could not access statsCompanion");
Bookatzb487b552017-09-18 11:26:01 -0700736 }
737}
738
Yao Chenef99c4f2017-09-22 16:26:54 -0700739sp<IStatsCompanionService> StatsService::getStatsCompanionService() {
Bookatz906a35c2017-09-20 15:26:44 -0700740 sp<IStatsCompanionService> statsCompanion = nullptr;
741 // Get statscompanion service from service manager
742 const sp<IServiceManager> sm(defaultServiceManager());
743 if (sm != nullptr) {
744 const String16 name("statscompanion");
745 statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name));
746 if (statsCompanion == nullptr) {
747 ALOGW("statscompanion service unavailable!");
748 return nullptr;
749 }
750 }
751 return statsCompanion;
752}
753
Yao Chenef99c4f2017-09-22 16:26:54 -0700754Status StatsService::statsCompanionReady() {
yro74fed972017-11-27 14:42:42 -0800755 VLOG("StatsService::statsCompanionReady was called");
Bookatzb487b552017-09-18 11:26:01 -0700756
757 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
758 return Status::fromExceptionCode(Status::EX_SECURITY,
759 "Only system uid can call statsCompanionReady");
760 }
761
762 sp<IStatsCompanionService> statsCompanion = getStatsCompanionService();
763 if (statsCompanion == nullptr) {
Yao Chenef99c4f2017-09-22 16:26:54 -0700764 return Status::fromExceptionCode(
765 Status::EX_NULL_POINTER,
766 "statscompanion unavailable despite it contacting statsd!");
Bookatzb487b552017-09-18 11:26:01 -0700767 }
yro74fed972017-11-27 14:42:42 -0800768 VLOG("StatsService::statsCompanionReady linking to statsCompanion.");
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700769 IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor));
Bookatzb487b552017-09-18 11:26:01 -0700770 mAnomalyMonitor->setStatsCompanionService(statsCompanion);
Bookatzc6977972018-01-16 16:55:05 -0800771 SubscriberReporter::getInstance().setStatsCompanionService(statsCompanion);
Bookatzb487b552017-09-18 11:26:01 -0700772
773 return Status::ok();
774}
775
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700776void StatsService::Startup() {
777 mConfigManager->Startup();
Bookatz906a35c2017-09-20 15:26:44 -0700778}
779
Yangster-macd40053e2018-01-09 16:29:22 -0800780void StatsService::OnLogEvent(LogEvent* event) {
Joe Onoratoc4dfae52017-10-17 23:38:21 -0700781 mProcessor->OnLogEvent(event);
Bookatz906a35c2017-09-20 15:26:44 -0700782}
783
Yangster-mac94e197c2018-01-02 16:03:03 -0800784Status StatsService::getData(int64_t key, vector<uint8_t>* output) {
David Chenadaf8b32017-11-03 15:42:08 -0700785 IPCThreadState* ipc = IPCThreadState::self();
yro74fed972017-11-27 14:42:42 -0800786 VLOG("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), ipc->getCallingUid());
Yao Chen7ee94152017-11-17 09:44:40 -0800787 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800788 ConfigKey configKey(ipc->getCallingUid(), key);
Yao Chen8a8d16c2018-02-08 14:50:40 -0800789 mProcessor->onDumpReport(configKey, time(nullptr) * NS_PER_SEC, output);
David Chenadaf8b32017-11-03 15:42:08 -0700790 return Status::ok();
791 } else {
792 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
793 }
yro31eb67b2017-10-24 13:33:21 -0700794}
795
David Chen2e8f3802017-11-22 10:56:48 -0800796Status StatsService::getMetadata(vector<uint8_t>* output) {
797 IPCThreadState* ipc = IPCThreadState::self();
798 VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
799 ipc->getCallingUid());
800 if (checkCallingPermission(String16(kPermissionDump))) {
801 StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
802 return Status::ok();
803 } else {
804 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
805 }
806}
807
Yangster-mac94e197c2018-01-02 16:03:03 -0800808Status StatsService::addConfiguration(int64_t key,
David Chenadaf8b32017-11-03 15:42:08 -0700809 const vector <uint8_t>& config,
David Chenadaf8b32017-11-03 15:42:08 -0700810 bool* success) {
811 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800812 if (checkCallingPermission(String16(kPermissionDump))) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800813 ConfigKey configKey(ipc->getCallingUid(), key);
David Chenadaf8b32017-11-03 15:42:08 -0700814 StatsdConfig cfg;
Yangster-macbbd056a2018-01-22 13:37:02 -0800815 if (config.empty() || !cfg.ParseFromArray(&config[0], config.size())) {
David Chen7d8aa4d2017-12-27 13:37:01 -0800816 *success = false;
817 return Status::ok();
818 }
David Chenadaf8b32017-11-03 15:42:08 -0700819 mConfigManager->UpdateConfig(configKey, cfg);
David Chen661f7912018-01-22 17:46:24 -0800820 *success = true;
821 return Status::ok();
822 } else {
823 *success = false;
824 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
825 }
826}
827
828Status StatsService::removeDataFetchOperation(int64_t key, bool* success) {
829 IPCThreadState* ipc = IPCThreadState::self();
830 if (checkCallingPermission(String16(kPermissionDump))) {
831 ConfigKey configKey(ipc->getCallingUid(), key);
832 mConfigManager->RemoveConfigReceiver(configKey);
833 *success = true;
834 return Status::ok();
835 } else {
836 *success = false;
837 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
838 }
839}
840
841Status StatsService::setDataFetchOperation(int64_t key, const sp<android::IBinder>& intentSender,
842 bool* success) {
843 IPCThreadState* ipc = IPCThreadState::self();
844 if (checkCallingPermission(String16(kPermissionDump))) {
845 ConfigKey configKey(ipc->getCallingUid(), key);
846 mConfigManager->SetConfigReceiver(configKey, intentSender);
David Chenadaf8b32017-11-03 15:42:08 -0700847 *success = true;
848 return Status::ok();
849 } else {
Yao Chenaa39bc72017-12-01 11:16:50 -0800850 *success = false;
David Chenadaf8b32017-11-03 15:42:08 -0700851 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700852 }
yro31eb67b2017-10-24 13:33:21 -0700853}
854
Yangster-mac94e197c2018-01-02 16:03:03 -0800855Status StatsService::removeConfiguration(int64_t key, bool* success) {
David Chenadaf8b32017-11-03 15:42:08 -0700856 IPCThreadState* ipc = IPCThreadState::self();
Yao Chen7ee94152017-11-17 09:44:40 -0800857 if (checkCallingPermission(String16(kPermissionDump))) {
Bookatzc6977972018-01-16 16:55:05 -0800858 ConfigKey configKey(ipc->getCallingUid(), key);
859 mConfigManager->RemoveConfig(configKey);
860 SubscriberReporter::getInstance().removeConfig(configKey);
Yao Chenaa39bc72017-12-01 11:16:50 -0800861 *success = true;
David Chenadaf8b32017-11-03 15:42:08 -0700862 return Status::ok();
863 } else {
864 *success = false;
865 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
yro31eb67b2017-10-24 13:33:21 -0700866 }
yro31eb67b2017-10-24 13:33:21 -0700867}
868
Bookatzc6977972018-01-16 16:55:05 -0800869Status StatsService::setBroadcastSubscriber(int64_t configId,
870 int64_t subscriberId,
871 const sp<android::IBinder>& intentSender,
872 bool* success) {
873 VLOG("StatsService::setBroadcastSubscriber called.");
874 IPCThreadState* ipc = IPCThreadState::self();
875 if (checkCallingPermission(String16(kPermissionDump))) {
876 ConfigKey configKey(ipc->getCallingUid(), configId);
877 SubscriberReporter::getInstance()
878 .setBroadcastSubscriber(configKey, subscriberId, intentSender);
879 *success = true;
880 return Status::ok();
881 } else {
882 *success = false;
883 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
884 }
885}
886
887Status StatsService::unsetBroadcastSubscriber(int64_t configId,
888 int64_t subscriberId,
889 bool* success) {
890 VLOG("StatsService::unsetBroadcastSubscriber called.");
891 IPCThreadState* ipc = IPCThreadState::self();
892 if (checkCallingPermission(String16(kPermissionDump))) {
893 ConfigKey configKey(ipc->getCallingUid(), configId);
894 SubscriberReporter::getInstance()
895 .unsetBroadcastSubscriber(configKey, subscriberId);
896 *success = true;
897 return Status::ok();
898 } else {
899 *success = false;
900 return Status::fromExceptionCode(binder::Status::EX_SECURITY);
901 }
902}
903
904
David Chen1d7b0cd2017-11-15 14:20:04 -0800905void StatsService::binderDied(const wp <IBinder>& who) {
yro31eb67b2017-10-24 13:33:21 -0700906}
907
Yao Chenef99c4f2017-09-22 16:26:54 -0700908} // namespace statsd
909} // namespace os
910} // namespace android