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