Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
yro | 0feae94 | 2017-11-15 14:38:48 -0800 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 3 | * |
| 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 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 17 | #define DEBUG true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 19 | |
| 20 | #include "StatsService.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 22 | #include "config/ConfigKey.h" |
| 23 | #include "config/ConfigManager.h" |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 24 | #include "guardrail/MemoryLeakTrackUtil.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | #include "storage/DropboxReader.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 26 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 27 | #include <android-base/file.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 28 | #include <binder/IPCThreadState.h> |
| 29 | #include <binder/IServiceManager.h> |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 30 | #include <dirent.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 31 | #include <frameworks/base/cmds/statsd/src/statsd_config.pb.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 32 | #include <private/android_filesystem_config.h> |
| 33 | #include <utils/Looper.h> |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 34 | #include <utils/String16.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 35 | #include <stdio.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 36 | #include <stdlib.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 37 | #include <sys/system_properties.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 38 | #include <unistd.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 39 | |
| 40 | using namespace android; |
| 41 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | namespace os { |
| 44 | namespace statsd { |
| 45 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 46 | constexpr const char* kPermissionDump = "android.permission.DUMP"; |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 47 | #define STATS_SERVICE_DIR "/data/system/stats-service" |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 48 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 49 | // ====================================================================== |
| 50 | /** |
| 51 | * Watches for the death of the stats companion (system process). |
| 52 | */ |
| 53 | class CompanionDeathRecipient : public IBinder::DeathRecipient { |
| 54 | public: |
| 55 | CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor); |
| 56 | virtual void binderDied(const wp<IBinder>& who); |
| 57 | |
| 58 | private: |
| 59 | const sp<AnomalyMonitor> mAnomalyMonitor; |
| 60 | }; |
| 61 | |
| 62 | CompanionDeathRecipient::CompanionDeathRecipient(const sp<AnomalyMonitor>& anomalyMonitor) |
| 63 | : mAnomalyMonitor(anomalyMonitor) { |
| 64 | } |
| 65 | |
| 66 | void CompanionDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 67 | ALOGW("statscompanion service died"); |
| 68 | mAnomalyMonitor->setStatsCompanionService(nullptr); |
| 69 | } |
| 70 | |
| 71 | // ====================================================================== |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 72 | StatsService::StatsService(const sp<Looper>& handlerLooper) |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 73 | : mAnomalyMonitor(new AnomalyMonitor(2)) // TODO: Put this comment somewhere better |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 74 | { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 75 | mUidMap = new UidMap(); |
| 76 | mConfigManager = new ConfigManager(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 77 | mProcessor = new StatsLogProcessor(mUidMap, mAnomalyMonitor, [this](const ConfigKey& key) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 78 | auto sc = getStatsCompanionService(); |
| 79 | auto receiver = mConfigManager->GetConfigReceiver(key); |
| 80 | if (sc == nullptr) { |
| 81 | ALOGD("Could not find StatsCompanionService"); |
| 82 | } else if (receiver.first.size() == 0) { |
| 83 | ALOGD("Statscompanion could not find a broadcast receiver for %s", |
| 84 | key.ToString().c_str()); |
| 85 | } else { |
| 86 | sc->sendBroadcast(String16(receiver.first.c_str()), |
| 87 | String16(receiver.second.c_str())); |
| 88 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 89 | }); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 90 | |
| 91 | mConfigManager->AddListener(mProcessor); |
| 92 | |
| 93 | init_system_properties(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 96 | StatsService::~StatsService() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 99 | void StatsService::init_system_properties() { |
| 100 | mEngBuild = false; |
| 101 | const prop_info* buildType = __system_property_find("ro.build.type"); |
| 102 | if (buildType != NULL) { |
| 103 | __system_property_read_callback(buildType, init_build_type_callback, this); |
| 104 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 107 | void StatsService::init_build_type_callback(void* cookie, const char* /*name*/, const char* value, |
| 108 | uint32_t serial) { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 109 | if (0 == strcmp("eng", value) || 0 == strcmp("userdebug", value)) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 110 | reinterpret_cast<StatsService*>(cookie)->mEngBuild = true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Implement our own because the default binder implementation isn't |
| 116 | * properly handling SHELL_COMMAND_TRANSACTION. |
| 117 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 118 | status_t StatsService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 119 | uint32_t flags) { |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 120 | status_t err; |
| 121 | |
| 122 | switch (code) { |
| 123 | case SHELL_COMMAND_TRANSACTION: { |
| 124 | int in = data.readFileDescriptor(); |
| 125 | int out = data.readFileDescriptor(); |
| 126 | int err = data.readFileDescriptor(); |
| 127 | int argc = data.readInt32(); |
| 128 | Vector<String8> args; |
| 129 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 130 | args.add(String8(data.readString16())); |
| 131 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 132 | sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder()); |
| 133 | sp<IResultReceiver> resultReceiver = |
| 134 | IResultReceiver::asInterface(data.readStrongBinder()); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 135 | |
| 136 | FILE* fin = fdopen(in, "r"); |
| 137 | FILE* fout = fdopen(out, "w"); |
| 138 | FILE* ferr = fdopen(err, "w"); |
| 139 | |
| 140 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 141 | resultReceiver->send(NO_MEMORY); |
| 142 | } else { |
| 143 | err = command(fin, fout, ferr, args); |
| 144 | resultReceiver->send(err); |
| 145 | } |
| 146 | |
| 147 | if (fin != NULL) { |
| 148 | fflush(fin); |
| 149 | fclose(fin); |
| 150 | } |
| 151 | if (fout != NULL) { |
| 152 | fflush(fout); |
| 153 | fclose(fout); |
| 154 | } |
| 155 | if (fout != NULL) { |
| 156 | fflush(ferr); |
| 157 | fclose(ferr); |
| 158 | } |
| 159 | |
| 160 | return NO_ERROR; |
| 161 | } |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 162 | default: { return BnStatsManager::onTransact(code, data, reply, flags); } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Write debugging data about statsd. |
| 168 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 169 | status_t StatsService::dump(int fd, const Vector<String16>& args) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 170 | FILE* out = fdopen(fd, "w"); |
| 171 | if (out == NULL) { |
| 172 | return NO_MEMORY; // the fd is already open |
| 173 | } |
| 174 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 175 | // TODO: Proto format for incident reports |
| 176 | dump_impl(out); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 177 | |
| 178 | fclose(out); |
| 179 | return NO_ERROR; |
| 180 | } |
| 181 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Write debugging data about statsd in text format. |
| 184 | */ |
| 185 | void StatsService::dump_impl(FILE* out) { |
| 186 | mConfigManager->Dump(out); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Implementation of the adb shell cmd stats command. |
| 191 | */ |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 192 | status_t StatsService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 193 | // TODO: Permission check |
| 194 | |
| 195 | const int argCount = args.size(); |
| 196 | if (argCount >= 1) { |
| 197 | // adb shell cmd stats config ... |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 198 | if (!args[0].compare(String8("config"))) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 199 | return cmd_config(in, out, err, args); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 200 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 201 | |
| 202 | // adb shell cmd stats print-stats-log |
| 203 | if (!args[0].compare(String8("print-stats-log")) && args.size() > 1) { |
| 204 | return cmd_print_stats_log(out, args); |
| 205 | } |
| 206 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 207 | if (!args[0].compare(String8("print-uid-map"))) { |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 208 | return cmd_print_uid_map(out); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 209 | } |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 210 | |
| 211 | if (!args[0].compare(String8("dump-report"))) { |
| 212 | return cmd_dump_report(out, err, args); |
| 213 | } |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 214 | |
| 215 | if (!args[0].compare(String8("pull-source")) && args.size() > 1) { |
| 216 | return cmd_print_pulled_metrics(out, args); |
| 217 | } |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 218 | |
| 219 | if (!args[0].compare(String8("send-broadcast"))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 220 | return cmd_trigger_broadcast(out, args); |
| 221 | } |
| 222 | |
| 223 | if (!args[0].compare(String8("print-stats"))) { |
| 224 | return cmd_print_stats(out); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 225 | } |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 226 | |
| 227 | if (!args[0].compare(String8("clear-config"))) { |
| 228 | return cmd_remove_config_files(out); |
| 229 | } |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 230 | |
| 231 | if (!args[0].compare(String8("meminfo"))) { |
| 232 | return cmd_dump_memory_info(out); |
| 233 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 234 | } |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 235 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 236 | print_cmd_help(out); |
Joe Onorato | 2cbc2cc | 2017-08-30 17:03:23 -0700 | [diff] [blame] | 237 | return NO_ERROR; |
| 238 | } |
| 239 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 240 | void StatsService::print_cmd_help(FILE* out) { |
| 241 | fprintf(out, |
| 242 | "usage: adb shell cmd stats print-stats-log [tag_required] " |
| 243 | "[timestamp_nsec_optional]\n"); |
| 244 | fprintf(out, "\n"); |
| 245 | fprintf(out, "\n"); |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 246 | fprintf(out, "usage: adb shell cmd stats meminfo\n"); |
| 247 | fprintf(out, "\n"); |
| 248 | fprintf(out, " Prints the malloc debug information. You need to run the following first: \n"); |
| 249 | fprintf(out, " # adb shell stop\n"); |
| 250 | fprintf(out, " # adb shell setprop libc.debug.malloc.program statsd \n"); |
| 251 | fprintf(out, " # adb shell setprop libc.debug.malloc.options backtrace \n"); |
| 252 | fprintf(out, " # adb shell start\n"); |
| 253 | fprintf(out, "\n"); |
| 254 | fprintf(out, "\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 255 | fprintf(out, "usage: adb shell cmd stats print-uid-map \n"); |
| 256 | fprintf(out, "\n"); |
| 257 | fprintf(out, " Prints the UID, app name, version mapping.\n"); |
| 258 | fprintf(out, "\n"); |
| 259 | fprintf(out, "\n"); |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 260 | fprintf(out, "usage: adb shell cmd stats clear-config \n"); |
| 261 | fprintf(out, "\n"); |
| 262 | fprintf(out, " Removes all configs from disk.\n"); |
| 263 | fprintf(out, "\n"); |
| 264 | fprintf(out, "\n"); |
yro | 3fca5ba | 2017-11-17 13:22:52 -0800 | [diff] [blame] | 265 | fprintf(out, "usage: adb shell cmd stats pull-source [int] \n"); |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 266 | fprintf(out, "\n"); |
| 267 | fprintf(out, " Prints the output of a pulled metrics source (int indicates source)\n"); |
| 268 | fprintf(out, "\n"); |
| 269 | fprintf(out, "\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 270 | fprintf(out, "usage: adb shell cmd stats config remove [UID] NAME\n"); |
| 271 | fprintf(out, "usage: adb shell cmd stats config update [UID] NAME\n"); |
| 272 | fprintf(out, "\n"); |
| 273 | fprintf(out, " Adds, updates or removes a configuration. The proto should be in\n"); |
| 274 | fprintf(out, " wire-encoded protobuf format and passed via stdin.\n"); |
| 275 | fprintf(out, "\n"); |
| 276 | fprintf(out, " UID The uid to use. It is only possible to pass the UID\n"); |
| 277 | fprintf(out, " parameter on eng builds. If UID is omitted the calling\n"); |
| 278 | fprintf(out, " uid is used.\n"); |
| 279 | fprintf(out, " NAME The per-uid name to use\n"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 280 | fprintf(out, "\n"); |
| 281 | fprintf(out, "\n"); |
| 282 | fprintf(out, "usage: adb shell cmd stats dump-report [UID] NAME\n"); |
| 283 | fprintf(out, " Dump all metric data for a configuration.\n"); |
| 284 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 285 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 286 | fprintf(out, " calling uid is used.\n"); |
| 287 | fprintf(out, " NAME The name of the configuration\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 288 | fprintf(out, "\n"); |
| 289 | fprintf(out, "\n"); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 290 | fprintf(out, "usage: adb shell cmd stats send-broadcast [UID] NAME\n"); |
| 291 | fprintf(out, " Send a broadcast that triggers the subscriber to fetch metrics.\n"); |
| 292 | fprintf(out, " UID The uid of the configuration. It is only possible to pass\n"); |
| 293 | fprintf(out, " the UID parameter on eng builds. If UID is omitted the\n"); |
| 294 | fprintf(out, " calling uid is used.\n"); |
| 295 | fprintf(out, " NAME The name of the configuration\n"); |
| 296 | fprintf(out, "\n"); |
| 297 | fprintf(out, "\n"); |
| 298 | fprintf(out, "usage: adb shell cmd stats print-stats\n"); |
| 299 | fprintf(out, " Prints some basic stats.\n"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 300 | } |
| 301 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 302 | status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) { |
| 303 | string name; |
| 304 | bool good = false; |
| 305 | int uid; |
| 306 | const int argCount = args.size(); |
| 307 | if (argCount == 2) { |
| 308 | // Automatically pick the UID |
| 309 | uid = IPCThreadState::self()->getCallingUid(); |
| 310 | // TODO: What if this isn't a binder call? Should we fail? |
| 311 | name.assign(args[1].c_str(), args[1].size()); |
| 312 | good = true; |
| 313 | } else if (argCount == 3) { |
| 314 | // If it's a userdebug or eng build, then the shell user can |
| 315 | // impersonate other uids. |
| 316 | if (mEngBuild) { |
| 317 | const char* s = args[1].c_str(); |
| 318 | if (*s != '\0') { |
| 319 | char* end = NULL; |
| 320 | uid = strtol(s, &end, 0); |
| 321 | if (*end == '\0') { |
| 322 | name.assign(args[2].c_str(), args[2].size()); |
| 323 | good = true; |
| 324 | } |
| 325 | } |
| 326 | } else { |
| 327 | fprintf(out, |
| 328 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 329 | "builds.\n"); |
| 330 | } |
| 331 | } |
| 332 | if (!good) { |
| 333 | print_cmd_help(out); |
| 334 | return UNKNOWN_ERROR; |
| 335 | } |
| 336 | auto receiver = mConfigManager->GetConfigReceiver(ConfigKey(uid, name)); |
yro | 4d889e6 | 2017-11-17 15:44:48 -0800 | [diff] [blame] | 337 | sp<IStatsCompanionService> sc = getStatsCompanionService(); |
| 338 | if (sc != nullptr) { |
| 339 | sc->sendBroadcast(String16(receiver.first.c_str()), String16(receiver.second.c_str())); |
| 340 | ALOGD("StatsService::trigger broadcast succeeded to %s, %s", args[1].c_str(), |
| 341 | args[2].c_str()); |
| 342 | } else { |
| 343 | ALOGD("Could not access statsCompanion"); |
| 344 | } |
| 345 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 346 | return NO_ERROR; |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | status_t StatsService::cmd_config(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 350 | const int argCount = args.size(); |
| 351 | if (argCount >= 2) { |
| 352 | if (args[1] == "update" || args[1] == "remove") { |
| 353 | bool good = false; |
| 354 | int uid = -1; |
| 355 | string name; |
| 356 | |
| 357 | if (argCount == 3) { |
| 358 | // Automatically pick the UID |
| 359 | uid = IPCThreadState::self()->getCallingUid(); |
| 360 | // TODO: What if this isn't a binder call? Should we fail? |
| 361 | name.assign(args[2].c_str(), args[2].size()); |
| 362 | good = true; |
| 363 | } else if (argCount == 4) { |
| 364 | // If it's a userdebug or eng build, then the shell user can |
| 365 | // impersonate other uids. |
| 366 | if (mEngBuild) { |
| 367 | const char* s = args[2].c_str(); |
| 368 | if (*s != '\0') { |
| 369 | char* end = NULL; |
| 370 | uid = strtol(s, &end, 0); |
| 371 | if (*end == '\0') { |
| 372 | name.assign(args[3].c_str(), args[3].size()); |
| 373 | good = true; |
| 374 | } |
| 375 | } |
| 376 | } else { |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 377 | fprintf(err, |
| 378 | "The config can only be set for other UIDs on eng or userdebug " |
| 379 | "builds.\n"); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | if (!good) { |
| 384 | // If arg parsing failed, print the help text and return an error. |
| 385 | print_cmd_help(out); |
| 386 | return UNKNOWN_ERROR; |
| 387 | } |
| 388 | |
| 389 | if (args[1] == "update") { |
| 390 | // Read stream into buffer. |
| 391 | string buffer; |
| 392 | if (!android::base::ReadFdToString(fileno(in), &buffer)) { |
| 393 | fprintf(err, "Error reading stream for StatsConfig.\n"); |
| 394 | return UNKNOWN_ERROR; |
| 395 | } |
| 396 | |
| 397 | // Parse buffer. |
| 398 | StatsdConfig config; |
| 399 | if (!config.ParseFromString(buffer)) { |
| 400 | fprintf(err, "Error parsing proto stream for StatsConfig.\n"); |
| 401 | return UNKNOWN_ERROR; |
| 402 | } |
| 403 | |
| 404 | // Add / update the config. |
| 405 | mConfigManager->UpdateConfig(ConfigKey(uid, name), config); |
| 406 | } else { |
| 407 | // Remove the config. |
| 408 | mConfigManager->RemoveConfig(ConfigKey(uid, name)); |
| 409 | } |
| 410 | |
| 411 | return NO_ERROR; |
| 412 | } |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 413 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 414 | print_cmd_help(out); |
| 415 | return UNKNOWN_ERROR; |
| 416 | } |
| 417 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 418 | status_t StatsService::cmd_dump_report(FILE* out, FILE* err, const Vector<String8>& args) { |
| 419 | if (mProcessor != nullptr) { |
| 420 | const int argCount = args.size(); |
| 421 | bool good = false; |
| 422 | int uid; |
| 423 | string name; |
| 424 | if (argCount == 2) { |
| 425 | // Automatically pick the UID |
| 426 | uid = IPCThreadState::self()->getCallingUid(); |
| 427 | // TODO: What if this isn't a binder call? Should we fail? |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 428 | name.assign(args[1].c_str(), args[1].size()); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 429 | good = true; |
| 430 | } else if (argCount == 3) { |
| 431 | // If it's a userdebug or eng build, then the shell user can |
| 432 | // impersonate other uids. |
| 433 | if (mEngBuild) { |
| 434 | const char* s = args[1].c_str(); |
| 435 | if (*s != '\0') { |
| 436 | char* end = NULL; |
| 437 | uid = strtol(s, &end, 0); |
| 438 | if (*end == '\0') { |
| 439 | name.assign(args[2].c_str(), args[2].size()); |
| 440 | good = true; |
| 441 | } |
| 442 | } |
| 443 | } else { |
| 444 | fprintf(out, |
| 445 | "The metrics can only be dumped for other UIDs on eng or userdebug " |
| 446 | "builds.\n"); |
| 447 | } |
| 448 | } |
| 449 | if (good) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 450 | vector<uint8_t> data; |
| 451 | mProcessor->onDumpReport(ConfigKey(uid, name), &data); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 452 | // TODO: print the returned StatsLogReport to file instead of printing to logcat. |
| 453 | fprintf(out, "Dump report for Config [%d,%s]\n", uid, name.c_str()); |
| 454 | fprintf(out, "See the StatsLogReport in logcat...\n"); |
| 455 | return android::OK; |
| 456 | } else { |
| 457 | // If arg parsing failed, print the help text and return an error. |
| 458 | print_cmd_help(out); |
| 459 | return UNKNOWN_ERROR; |
| 460 | } |
| 461 | } else { |
| 462 | fprintf(out, "Log processor does not exist...\n"); |
| 463 | return UNKNOWN_ERROR; |
| 464 | } |
| 465 | } |
| 466 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 467 | status_t StatsService::cmd_print_stats(FILE* out) { |
| 468 | vector<ConfigKey> configs = mConfigManager->GetAllConfigKeys(); |
| 469 | for (const ConfigKey& key : configs) { |
| 470 | fprintf(out, "Config %s uses %zu bytes\n", key.ToString().c_str(), |
| 471 | mProcessor->GetMetricsSize(key)); |
| 472 | } |
| 473 | return NO_ERROR; |
| 474 | } |
| 475 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 476 | status_t StatsService::cmd_print_stats_log(FILE* out, const Vector<String8>& args) { |
| 477 | long msec = 0; |
| 478 | |
| 479 | if (args.size() > 2) { |
| 480 | msec = strtol(args[2].string(), NULL, 10); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 481 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 482 | return DropboxReader::readStatsLogs(out, args[1].string(), msec); |
| 483 | } |
| 484 | |
| 485 | status_t StatsService::cmd_print_uid_map(FILE* out) { |
| 486 | mUidMap->printUidMap(out); |
| 487 | return NO_ERROR; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 488 | } |
| 489 | |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 490 | status_t StatsService::cmd_print_pulled_metrics(FILE* out, const Vector<String8>& args) { |
| 491 | int s = atoi(args[1].c_str()); |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 492 | vector<shared_ptr<LogEvent> > stats; |
| 493 | if (mStatsPullerManager.Pull(s, &stats)) { |
| 494 | for (const auto& it : stats) { |
| 495 | fprintf(out, "Pull from %d: %s\n", s, it->ToString().c_str()); |
| 496 | } |
| 497 | fprintf(out, "Pull from %d: Received %zu elements\n", s, stats.size()); |
| 498 | return NO_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 499 | } |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 500 | return UNKNOWN_ERROR; |
David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 501 | } |
| 502 | |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 503 | status_t StatsService::cmd_remove_config_files(FILE* out) { |
| 504 | fprintf(out, "Trying to remove config files...\n"); |
| 505 | unique_ptr<DIR, decltype(&closedir)> dir(opendir(STATS_SERVICE_DIR), closedir); |
| 506 | if (dir == NULL) { |
| 507 | fprintf(out, "No existing config files found exiting...\n"); |
| 508 | return NO_ERROR; |
| 509 | } |
| 510 | |
| 511 | dirent* de; |
| 512 | while ((de = readdir(dir.get()))) { |
| 513 | char* name = de->d_name; |
| 514 | if (name[0] == '.') continue; |
| 515 | string file_name = StringPrintf("%s/%s", STATS_SERVICE_DIR, name); |
| 516 | fprintf(out, "Deleting file %s\n", file_name.c_str()); |
| 517 | if (remove(file_name.c_str())) { |
| 518 | fprintf(out, "Error deleting file %s\n", file_name.c_str()); |
| 519 | } |
| 520 | } |
| 521 | return NO_ERROR; |
| 522 | } |
| 523 | |
Yao Chen | 8d9989b | 2017-11-18 18:54:50 -0800 | [diff] [blame] | 524 | status_t StatsService::cmd_dump_memory_info(FILE* out) { |
| 525 | std::string s = dumpMemInfo(100); |
| 526 | fprintf(out, "Memory Info\n"); |
| 527 | fprintf(out, "%s", s.c_str()); |
| 528 | return NO_ERROR; |
| 529 | } |
| 530 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 531 | Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version, |
| 532 | const vector<String16>& app) { |
| 533 | if (DEBUG) ALOGD("StatsService::informAllUidData was called"); |
| 534 | |
| 535 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 536 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 537 | "Only system uid can call informAllUidData"); |
| 538 | } |
| 539 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 540 | mUidMap->updateMap(uid, version, app); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 541 | if (DEBUG) ALOGD("StatsService::informAllUidData succeeded"); |
| 542 | |
| 543 | return Status::ok(); |
| 544 | } |
| 545 | |
| 546 | Status StatsService::informOnePackage(const String16& app, int32_t uid, int32_t version) { |
| 547 | if (DEBUG) ALOGD("StatsService::informOnePackage was called"); |
| 548 | |
| 549 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 550 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 551 | "Only system uid can call informOnePackage"); |
| 552 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 553 | mUidMap->updateApp(app, uid, version); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 554 | return Status::ok(); |
| 555 | } |
| 556 | |
| 557 | Status StatsService::informOnePackageRemoved(const String16& app, int32_t uid) { |
| 558 | if (DEBUG) ALOGD("StatsService::informOnePackageRemoved was called"); |
| 559 | |
| 560 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 561 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 562 | "Only system uid can call informOnePackageRemoved"); |
| 563 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 564 | mUidMap->removeApp(app, uid); |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 565 | return Status::ok(); |
| 566 | } |
| 567 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 568 | Status StatsService::informAnomalyAlarmFired() { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 569 | if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 570 | |
| 571 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 572 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 573 | "Only system uid can call informAnomalyAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 576 | if (DEBUG) ALOGD("StatsService::informAnomalyAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 577 | // TODO: check through all counters/timers and see if an anomaly has indeed occurred. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 578 | uint64_t currentTimeNs = time(nullptr) * NS_PER_SEC; |
| 579 | std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet = |
| 580 | mAnomalyMonitor->onAlarmFired(currentTimeNs); |
| 581 | mProcessor->onAnomalyAlarmFired(currentTimeNs, anomalySet); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 582 | return Status::ok(); |
| 583 | } |
| 584 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 585 | Status StatsService::informPollAlarmFired() { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 586 | if (DEBUG) ALOGD("StatsService::informPollAlarmFired was called"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 587 | |
| 588 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 589 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 590 | "Only system uid can call informPollAlarmFired"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 593 | mStatsPullerManager.OnAlarmFired(); |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 594 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 595 | if (DEBUG) ALOGD("StatsService::informPollAlarmFired succeeded"); |
Bookatz | 1b0b114 | 2017-09-08 11:58:42 -0700 | [diff] [blame] | 596 | |
| 597 | return Status::ok(); |
| 598 | } |
| 599 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 600 | Status StatsService::systemRunning() { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 601 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 602 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 603 | "Only system uid can call systemRunning"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // When system_server is up and running, schedule the dropbox task to run. |
| 607 | ALOGD("StatsService::systemRunning"); |
| 608 | |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 609 | sayHiToStatsCompanion(); |
| 610 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 611 | return Status::ok(); |
| 612 | } |
| 613 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 614 | void StatsService::sayHiToStatsCompanion() { |
| 615 | // TODO: This method needs to be private. It is temporarily public and unsecured for testing |
| 616 | // purposes. |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 617 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 618 | if (statsCompanion != nullptr) { |
| 619 | if (DEBUG) ALOGD("Telling statsCompanion that statsd is ready"); |
| 620 | statsCompanion->statsdReady(); |
| 621 | } else { |
| 622 | if (DEBUG) ALOGD("Could not access statsCompanion"); |
| 623 | } |
| 624 | } |
| 625 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 626 | sp<IStatsCompanionService> StatsService::getStatsCompanionService() { |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 627 | sp<IStatsCompanionService> statsCompanion = nullptr; |
| 628 | // Get statscompanion service from service manager |
| 629 | const sp<IServiceManager> sm(defaultServiceManager()); |
| 630 | if (sm != nullptr) { |
| 631 | const String16 name("statscompanion"); |
| 632 | statsCompanion = interface_cast<IStatsCompanionService>(sm->checkService(name)); |
| 633 | if (statsCompanion == nullptr) { |
| 634 | ALOGW("statscompanion service unavailable!"); |
| 635 | return nullptr; |
| 636 | } |
| 637 | } |
| 638 | return statsCompanion; |
| 639 | } |
| 640 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 641 | Status StatsService::statsCompanionReady() { |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 642 | if (DEBUG) ALOGD("StatsService::statsCompanionReady was called"); |
| 643 | |
| 644 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 645 | return Status::fromExceptionCode(Status::EX_SECURITY, |
| 646 | "Only system uid can call statsCompanionReady"); |
| 647 | } |
| 648 | |
| 649 | sp<IStatsCompanionService> statsCompanion = getStatsCompanionService(); |
| 650 | if (statsCompanion == nullptr) { |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 651 | return Status::fromExceptionCode( |
| 652 | Status::EX_NULL_POINTER, |
| 653 | "statscompanion unavailable despite it contacting statsd!"); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 654 | } |
| 655 | if (DEBUG) ALOGD("StatsService::statsCompanionReady linking to statsCompanion."); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 656 | IInterface::asBinder(statsCompanion)->linkToDeath(new CompanionDeathRecipient(mAnomalyMonitor)); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 657 | mAnomalyMonitor->setStatsCompanionService(statsCompanion); |
| 658 | |
| 659 | return Status::ok(); |
| 660 | } |
| 661 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 662 | void StatsService::Startup() { |
| 663 | mConfigManager->Startup(); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 666 | void StatsService::OnLogEvent(const LogEvent& event) { |
| 667 | mProcessor->OnLogEvent(event); |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame^] | 670 | Status StatsService::getData(const String16& key, vector<uint8_t>* output) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 671 | IPCThreadState* ipc = IPCThreadState::self(); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 672 | ALOGD("StatsService::getData with Pid %i, Uid %i", ipc->getCallingPid(), |
| 673 | ipc->getCallingUid()); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 674 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 675 | string keyStr = string(String8(key).string()); |
| 676 | ConfigKey configKey(ipc->getCallingUid(), keyStr); |
| 677 | mProcessor->onDumpReport(configKey, output); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 678 | return Status::ok(); |
| 679 | } else { |
| 680 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
| 681 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 682 | } |
| 683 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 684 | Status StatsService::addConfiguration(const String16& key, |
| 685 | const vector <uint8_t>& config, |
| 686 | const String16& package, const String16& cls, |
| 687 | bool* success) { |
| 688 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 689 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 690 | string keyString = string(String8(key).string()); |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 691 | ConfigKey configKey(ipc->getCallingUid(), keyString); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 692 | StatsdConfig cfg; |
| 693 | cfg.ParseFromArray(&config[0], config.size()); |
| 694 | mConfigManager->UpdateConfig(configKey, cfg); |
| 695 | mConfigManager->SetConfigReceiver(configKey, string(String8(package).string()), |
| 696 | string(String8(cls).string())); |
| 697 | *success = true; |
| 698 | return Status::ok(); |
| 699 | } else { |
| 700 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 701 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 702 | } |
| 703 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 704 | Status StatsService::removeConfiguration(const String16& key, bool* success) { |
| 705 | IPCThreadState* ipc = IPCThreadState::self(); |
Yao Chen | 7ee9415 | 2017-11-17 09:44:40 -0800 | [diff] [blame] | 706 | if (checkCallingPermission(String16(kPermissionDump))) { |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 707 | string keyStr = string(String8(key).string()); |
| 708 | mConfigManager->RemoveConfig(ConfigKey(ipc->getCallingUid(), keyStr)); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 709 | return Status::ok(); |
| 710 | } else { |
| 711 | *success = false; |
| 712 | return Status::fromExceptionCode(binder::Status::EX_SECURITY); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 713 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 714 | } |
| 715 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 716 | void StatsService::binderDied(const wp <IBinder>& who) { |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 719 | } // namespace statsd |
| 720 | } // namespace os |
| 721 | } // namespace android |