Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | */ |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 16 | #define DEBUG false |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 17 | #include "Log.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 18 | |
| 19 | #include "IncidentService.h" |
| 20 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 21 | #include "FdBuffer.h" |
| 22 | #include "PrivacyBuffer.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 23 | #include "Reporter.h" |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 24 | #include "incidentd_util.h" |
| 25 | #include "section_list.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 26 | |
| 27 | #include <binder/IPCThreadState.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 28 | #include <binder/IResultReceiver.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 29 | #include <binder/IServiceManager.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 30 | #include <binder/IShellCallback.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | #include <cutils/log.h> |
| 32 | #include <private/android_filesystem_config.h> |
| 33 | #include <utils/Looper.h> |
| 34 | |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | using namespace android; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 38 | using namespace android::base; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 39 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 40 | enum { WHAT_RUN_REPORT = 1, WHAT_SEND_BACKLOG_TO_DROPBOX = 2 }; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 41 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 42 | #define DEFAULT_BACKLOG_DELAY_NS (1000000000LL) |
| 43 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 44 | #define DEFAULT_BYTES_SIZE_LIMIT (20 * 1024 * 1024) // 20MB |
| 45 | #define DEFAULT_REFACTORY_PERIOD_MS (24 * 60 * 60 * 1000) // 1 Day |
| 46 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 47 | // ================================================================================ |
| 48 | String16 const DUMP_PERMISSION("android.permission.DUMP"); |
| 49 | String16 const USAGE_STATS_PERMISSION("android.permission.PACKAGE_USAGE_STATS"); |
| 50 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 51 | static Status checkIncidentPermissions(const IncidentReportArgs& args) { |
Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 52 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 53 | pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 54 | if (callingUid == AID_ROOT || callingUid == AID_SHELL) { |
| 55 | // root doesn't have permission.DUMP if don't do this! |
| 56 | return Status::ok(); |
| 57 | } |
| 58 | |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 59 | // checking calling permission. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 60 | if (!checkCallingPermission(DUMP_PERMISSION)) { |
| 61 | ALOGW("Calling pid %d and uid %d does not have permission: android.permission.DUMP", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 62 | callingPid, callingUid); |
| 63 | return Status::fromExceptionCode( |
| 64 | Status::EX_SECURITY, |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 65 | "Calling process does not have permission: android.permission.DUMP"); |
| 66 | } |
| 67 | if (!checkCallingPermission(USAGE_STATS_PERMISSION)) { |
| 68 | ALOGW("Calling pid %d and uid %d does not have permission: android.permission.USAGE_STATS", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 69 | callingPid, callingUid); |
| 70 | return Status::fromExceptionCode( |
| 71 | Status::EX_SECURITY, |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 72 | "Calling process does not have permission: android.permission.USAGE_STATS"); |
| 73 | } |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 74 | |
| 75 | // checking calling request uid permission. |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 76 | switch (args.dest()) { |
| 77 | case DEST_LOCAL: |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 78 | if (callingUid != AID_SHELL && callingUid != AID_ROOT) { |
| 79 | ALOGW("Calling pid %d and uid %d does not have permission to get local data.", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 80 | callingPid, callingUid); |
| 81 | return Status::fromExceptionCode( |
| 82 | Status::EX_SECURITY, |
| 83 | "Calling process does not have permission to get local data."); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 84 | } |
| 85 | case DEST_EXPLICIT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 86 | if (callingUid != AID_SHELL && callingUid != AID_ROOT && callingUid != AID_STATSD && |
| 87 | callingUid != AID_SYSTEM) { |
Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 88 | ALOGW("Calling pid %d and uid %d does not have permission to get explicit data.", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 89 | callingPid, callingUid); |
| 90 | return Status::fromExceptionCode( |
| 91 | Status::EX_SECURITY, |
| 92 | "Calling process does not have permission to get explicit data."); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 95 | return Status::ok(); |
| 96 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 97 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 98 | ReportRequestQueue::ReportRequestQueue() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 99 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 100 | ReportRequestQueue::~ReportRequestQueue() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 101 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 102 | void ReportRequestQueue::addRequest(const sp<ReportRequest>& request) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 103 | unique_lock<mutex> lock(mLock); |
| 104 | mQueue.push_back(request); |
| 105 | } |
| 106 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 107 | sp<ReportRequest> ReportRequestQueue::getNextRequest() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 108 | unique_lock<mutex> lock(mLock); |
| 109 | if (mQueue.empty()) { |
| 110 | return NULL; |
| 111 | } else { |
| 112 | sp<ReportRequest> front(mQueue.front()); |
| 113 | mQueue.pop_front(); |
| 114 | return front; |
| 115 | } |
| 116 | } |
| 117 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 118 | // ================================================================================ |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 119 | ReportHandler::ReportHandler(const sp<Looper>& handlerLooper, const sp<ReportRequestQueue>& queue, |
| 120 | const sp<Throttler>& throttler) |
| 121 | : mBacklogDelay(DEFAULT_BACKLOG_DELAY_NS), |
| 122 | mHandlerLooper(handlerLooper), |
| 123 | mQueue(queue), |
| 124 | mThrottler(throttler) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 125 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 126 | ReportHandler::~ReportHandler() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 127 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 128 | void ReportHandler::handleMessage(const Message& message) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 129 | switch (message.what) { |
| 130 | case WHAT_RUN_REPORT: |
| 131 | run_report(); |
| 132 | break; |
| 133 | case WHAT_SEND_BACKLOG_TO_DROPBOX: |
| 134 | send_backlog_to_dropbox(); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 139 | void ReportHandler::scheduleRunReport(const sp<ReportRequest>& request) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 140 | mQueue->addRequest(request); |
| 141 | mHandlerLooper->removeMessages(this, WHAT_RUN_REPORT); |
| 142 | mHandlerLooper->sendMessage(this, Message(WHAT_RUN_REPORT)); |
| 143 | } |
| 144 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 145 | void ReportHandler::scheduleSendBacklogToDropbox() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 146 | unique_lock<mutex> lock(mLock); |
| 147 | mBacklogDelay = DEFAULT_BACKLOG_DELAY_NS; |
| 148 | schedule_send_backlog_to_dropbox_locked(); |
| 149 | } |
| 150 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 151 | void ReportHandler::schedule_send_backlog_to_dropbox_locked() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 152 | mHandlerLooper->removeMessages(this, WHAT_SEND_BACKLOG_TO_DROPBOX); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 153 | mHandlerLooper->sendMessageDelayed(mBacklogDelay, this, Message(WHAT_SEND_BACKLOG_TO_DROPBOX)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 156 | void ReportHandler::run_report() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 157 | sp<Reporter> reporter = new Reporter(); |
| 158 | |
| 159 | // Merge all of the requests into one that has all of the |
| 160 | // requested fields. |
| 161 | while (true) { |
| 162 | sp<ReportRequest> request = mQueue->getNextRequest(); |
| 163 | if (request == NULL) { |
| 164 | break; |
| 165 | } |
| 166 | reporter->batch.add(request); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 169 | if (mThrottler->shouldThrottle()) { |
| 170 | ALOGW("RunReport got throttled."); |
| 171 | return; |
| 172 | } |
| 173 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 174 | // Take the report, which might take a while. More requests might queue |
| 175 | // up while we're doing this, and we'll handle them in their next batch. |
| 176 | // TODO: We should further rate-limit the reports to no more than N per time-period. |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 177 | size_t reportByteSize = 0; |
| 178 | Reporter::run_report_status_t reportStatus = reporter->runReport(&reportByteSize); |
| 179 | mThrottler->addReportSize(reportByteSize); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 180 | if (reportStatus == Reporter::REPORT_NEEDS_DROPBOX) { |
| 181 | unique_lock<mutex> lock(mLock); |
| 182 | schedule_send_backlog_to_dropbox_locked(); |
| 183 | } |
| 184 | } |
| 185 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 186 | void ReportHandler::send_backlog_to_dropbox() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 187 | if (Reporter::upload_backlog() == Reporter::REPORT_NEEDS_DROPBOX) { |
| 188 | // There was a failure. Exponential backoff. |
| 189 | unique_lock<mutex> lock(mLock); |
| 190 | mBacklogDelay *= 2; |
| 191 | ALOGI("Error sending to dropbox. Trying again in %lld minutes", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 192 | (mBacklogDelay / (1000000000LL * 60))); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 193 | schedule_send_backlog_to_dropbox_locked(); |
| 194 | } else { |
| 195 | mBacklogDelay = DEFAULT_BACKLOG_DELAY_NS; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // ================================================================================ |
| 200 | IncidentService::IncidentService(const sp<Looper>& handlerLooper) |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 201 | : mQueue(new ReportRequestQueue()), |
| 202 | mThrottler(new Throttler(DEFAULT_BYTES_SIZE_LIMIT, DEFAULT_REFACTORY_PERIOD_MS)) { |
| 203 | mHandler = new ReportHandler(handlerLooper, mQueue, mThrottler); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 206 | IncidentService::~IncidentService() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 207 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 208 | Status IncidentService::reportIncident(const IncidentReportArgs& args) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 209 | ALOGI("reportIncident"); |
| 210 | |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 211 | Status status = checkIncidentPermissions(args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 212 | if (!status.isOk()) { |
| 213 | return status; |
| 214 | } |
| 215 | |
| 216 | mHandler->scheduleRunReport(new ReportRequest(args, NULL, -1)); |
| 217 | |
| 218 | return Status::ok(); |
| 219 | } |
| 220 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 221 | Status IncidentService::reportIncidentToStream(const IncidentReportArgs& args, |
| 222 | const sp<IIncidentReportStatusListener>& listener, |
| 223 | const unique_fd& stream) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 224 | ALOGI("reportIncidentToStream"); |
| 225 | |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 226 | Status status = checkIncidentPermissions(args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 227 | if (!status.isOk()) { |
| 228 | return status; |
| 229 | } |
| 230 | |
| 231 | int fd = dup(stream.get()); |
| 232 | if (fd < 0) { |
| 233 | return Status::fromStatusT(-errno); |
| 234 | } |
| 235 | |
| 236 | mHandler->scheduleRunReport(new ReportRequest(args, listener, fd)); |
| 237 | |
| 238 | return Status::ok(); |
| 239 | } |
| 240 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 241 | Status IncidentService::systemRunning() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 242 | if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { |
| 243 | return Status::fromExceptionCode(Status::EX_SECURITY, |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 244 | "Only system uid can call systemRunning"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 245 | } |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 246 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 247 | // When system_server is up and running, schedule the dropbox task to run. |
| 248 | mHandler->scheduleSendBacklogToDropbox(); |
| 249 | |
| 250 | return Status::ok(); |
| 251 | } |
| 252 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 253 | /** |
| 254 | * Implement our own because the default binder implementation isn't |
| 255 | * properly handling SHELL_COMMAND_TRANSACTION. |
| 256 | */ |
| 257 | status_t IncidentService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 258 | uint32_t flags) { |
| 259 | status_t err; |
| 260 | |
| 261 | switch (code) { |
| 262 | case SHELL_COMMAND_TRANSACTION: { |
| 263 | int in = data.readFileDescriptor(); |
| 264 | int out = data.readFileDescriptor(); |
| 265 | int err = data.readFileDescriptor(); |
| 266 | int argc = data.readInt32(); |
| 267 | Vector<String8> args; |
| 268 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 269 | args.add(String8(data.readString16())); |
| 270 | } |
| 271 | sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder()); |
| 272 | sp<IResultReceiver> resultReceiver = |
| 273 | IResultReceiver::asInterface(data.readStrongBinder()); |
| 274 | |
| 275 | FILE* fin = fdopen(in, "r"); |
| 276 | FILE* fout = fdopen(out, "w"); |
| 277 | FILE* ferr = fdopen(err, "w"); |
| 278 | |
| 279 | if (fin == NULL || fout == NULL || ferr == NULL) { |
| 280 | resultReceiver->send(NO_MEMORY); |
| 281 | } else { |
| 282 | err = command(fin, fout, ferr, args); |
| 283 | resultReceiver->send(err); |
| 284 | } |
| 285 | |
| 286 | if (fin != NULL) { |
| 287 | fflush(fin); |
| 288 | fclose(fin); |
| 289 | } |
| 290 | if (fout != NULL) { |
| 291 | fflush(fout); |
| 292 | fclose(fout); |
| 293 | } |
| 294 | if (fout != NULL) { |
| 295 | fflush(ferr); |
| 296 | fclose(ferr); |
| 297 | } |
| 298 | |
| 299 | return NO_ERROR; |
| 300 | } |
| 301 | default: { return BnIncidentManager::onTransact(code, data, reply, flags); } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | status_t IncidentService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 306 | const int argCount = args.size(); |
| 307 | |
| 308 | if (argCount >= 1) { |
| 309 | if (!args[0].compare(String8("privacy"))) { |
| 310 | return cmd_privacy(in, out, err, args); |
| 311 | } |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 312 | if (!args[0].compare(String8("throttler"))) { |
| 313 | mThrottler->dump(out); |
| 314 | return NO_ERROR; |
| 315 | } |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 316 | } |
| 317 | return cmd_help(out); |
| 318 | } |
| 319 | |
| 320 | status_t IncidentService::cmd_help(FILE* out) { |
| 321 | fprintf(out, "usage: adb shell cmd incident privacy print <section_id>\n"); |
| 322 | fprintf(out, "usage: adb shell cmd incident privacy parse <section_id> < proto.txt\n"); |
| 323 | fprintf(out, " Prints/parses for the section id.\n"); |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 324 | fprintf(out, "\n"); |
| 325 | fprintf(out, "usage: adb shell cmd incident throttler\n"); |
| 326 | fprintf(out, " Prints the current throttler state\n"); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 327 | return NO_ERROR; |
| 328 | } |
| 329 | |
| 330 | static void printPrivacy(const Privacy* p, FILE* out, String8 indent) { |
| 331 | if (p == NULL) return; |
| 332 | fprintf(out, "%sid:%d, type:%d, dest:%d\n", indent.string(), p->field_id, p->type, p->dest); |
| 333 | if (p->children == NULL) return; |
| 334 | for (int i = 0; p->children[i] != NULL; i++) { // NULL-terminated. |
| 335 | printPrivacy(p->children[i], out, indent + " "); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | status_t IncidentService::cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { |
| 340 | const int argCount = args.size(); |
| 341 | if (argCount >= 3) { |
| 342 | String8 opt = args[1]; |
| 343 | int sectionId = atoi(args[2].string()); |
| 344 | |
| 345 | const Privacy* p = get_privacy_of_section(sectionId); |
| 346 | if (p == NULL) { |
| 347 | fprintf(err, "Can't find section id %d\n", sectionId); |
| 348 | return NO_ERROR; |
| 349 | } |
| 350 | fprintf(err, "Get privacy for %d\n", sectionId); |
| 351 | if (opt == "print") { |
| 352 | printPrivacy(p, out, String8("")); |
| 353 | } else if (opt == "parse") { |
| 354 | FdBuffer buf; |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame^] | 355 | status_t error = buf.read(fileno(in), 60000); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 356 | if (error != NO_ERROR) { |
| 357 | fprintf(err, "Error reading from stdin\n"); |
| 358 | return error; |
| 359 | } |
| 360 | fprintf(err, "Read %zu bytes\n", buf.size()); |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 361 | PrivacyBuffer pBuf(p, buf.data()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 362 | |
| 363 | PrivacySpec spec = PrivacySpec::new_spec(argCount > 3 ? atoi(args[3]) : -1); |
| 364 | error = pBuf.strip(spec); |
| 365 | if (error != NO_ERROR) { |
| 366 | fprintf(err, "Error strip pii fields with spec %d\n", spec.dest); |
| 367 | return error; |
| 368 | } |
| 369 | return pBuf.flush(fileno(out)); |
| 370 | } |
| 371 | } else { |
| 372 | return cmd_help(out); |
| 373 | } |
| 374 | return NO_ERROR; |
| 375 | } |