blob: 71b89a2c67171c016c4144b657e4657e1efc03ec [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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 Jin4e843102018-02-14 15:36:18 -080016#define DEBUG false
Yi Jinb592e3b2018-02-01 15:17:04 -080017#include "Log.h"
Joe Onorato1754d742016-11-21 17:51:35 -080018
19#include "IncidentService.h"
20
Yi Jinb592e3b2018-02-01 15:17:04 -080021#include "FdBuffer.h"
Joe Onorato99598ee2019-02-11 15:55:13 +000022#include "PrivacyFilter.h"
Joe Onorato1754d742016-11-21 17:51:35 -080023#include "Reporter.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080024#include "incidentd_util.h"
25#include "section_list.h"
Joe Onorato1754d742016-11-21 17:51:35 -080026
Mike Ma85434ec2018-11-27 10:32:31 -080027#include <android/os/IncidentReportArgs.h>
Joe Onorato1754d742016-11-21 17:51:35 -080028#include <binder/IPCThreadState.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080029#include <binder/IResultReceiver.h>
Joe Onorato1754d742016-11-21 17:51:35 -080030#include <binder/IServiceManager.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080031#include <binder/IShellCallback.h>
Mike Ma35056662018-12-06 13:32:59 -080032#include <log/log.h>
Joe Onorato1754d742016-11-21 17:51:35 -080033#include <private/android_filesystem_config.h>
34#include <utils/Looper.h>
Yao Chencbafce92019-04-01 15:56:44 -070035#include <thread>
Joe Onorato1754d742016-11-21 17:51:35 -080036
37#include <unistd.h>
38
Joe Onorato99598ee2019-02-11 15:55:13 +000039enum {
40 WHAT_TAKE_REPORT = 1,
41 WHAT_SEND_BROADCASTS = 2
42};
Joe Onorato1754d742016-11-21 17:51:35 -080043
Joe Onorato99598ee2019-02-11 15:55:13 +000044#define DEFAULT_DELAY_NS (1000000000LL)
Joe Onorato1754d742016-11-21 17:51:35 -080045
Yi Jin4e843102018-02-14 15:36:18 -080046#define DEFAULT_BYTES_SIZE_LIMIT (20 * 1024 * 1024) // 20MB
47#define DEFAULT_REFACTORY_PERIOD_MS (24 * 60 * 60 * 1000) // 1 Day
48
Mike Ma28381692018-12-04 15:46:29 -080049// Skip these sections for dumpstate only. Dumpstate allows 10s max for each service to dump.
50// Skip logs (1100 - 1108) and traces (1200 - 1202) because they are already in the bug report.
51// Skip 3018 because it takes too long.
Mike Ma85434ec2018-11-27 10:32:31 -080052#define SKIPPED_SECTIONS { 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, /* Logs */ \
53 1200, 1201, 1202, /* Native, hal, java traces */ \
Mike Ma85434ec2018-11-27 10:32:31 -080054 3018 /* "meminfo -a --proto" */ }
55
Yi Jin6cacbcb2018-03-30 14:04:52 -070056namespace android {
57namespace os {
58namespace incidentd {
59
Joe Onorato99598ee2019-02-11 15:55:13 +000060String16 const APPROVE_INCIDENT_REPORTS("android.permission.APPROVE_INCIDENT_REPORTS");
Joe Onorato1754d742016-11-21 17:51:35 -080061String16 const DUMP_PERMISSION("android.permission.DUMP");
62String16 const USAGE_STATS_PERMISSION("android.permission.PACKAGE_USAGE_STATS");
63
Yi Jinb592e3b2018-02-01 15:17:04 -080064static Status checkIncidentPermissions(const IncidentReportArgs& args) {
Yi Jin437aa6e2018-01-10 11:34:26 -080065 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Yi Jinafb36062018-01-31 19:14:25 -080066 pid_t callingPid = IPCThreadState::self()->getCallingPid();
Yi Jin437aa6e2018-01-10 11:34:26 -080067 if (callingUid == AID_ROOT || callingUid == AID_SHELL) {
Joe Onorato99598ee2019-02-11 15:55:13 +000068 // Root and shell are ok.
69 return Status::ok();
70 }
71
72 if (checkCallingPermission(APPROVE_INCIDENT_REPORTS)) {
73 // Permission controller (this is a singleton permission that is always granted
74 // exactly for PermissionController) is allowed to access incident reports
75 // so it can show the user info about what they are approving.
Yi Jin437aa6e2018-01-10 11:34:26 -080076 return Status::ok();
77 }
78
Yi Jin4bab3a12018-01-10 16:50:59 -080079 // checking calling permission.
Joe Onorato1754d742016-11-21 17:51:35 -080080 if (!checkCallingPermission(DUMP_PERMISSION)) {
81 ALOGW("Calling pid %d and uid %d does not have permission: android.permission.DUMP",
Yi Jinb592e3b2018-02-01 15:17:04 -080082 callingPid, callingUid);
83 return Status::fromExceptionCode(
84 Status::EX_SECURITY,
Joe Onorato1754d742016-11-21 17:51:35 -080085 "Calling process does not have permission: android.permission.DUMP");
86 }
87 if (!checkCallingPermission(USAGE_STATS_PERMISSION)) {
88 ALOGW("Calling pid %d and uid %d does not have permission: android.permission.USAGE_STATS",
Yi Jinb592e3b2018-02-01 15:17:04 -080089 callingPid, callingUid);
90 return Status::fromExceptionCode(
91 Status::EX_SECURITY,
Joe Onorato1754d742016-11-21 17:51:35 -080092 "Calling process does not have permission: android.permission.USAGE_STATS");
93 }
Yi Jin4bab3a12018-01-10 16:50:59 -080094
95 // checking calling request uid permission.
Joe Onorato99598ee2019-02-11 15:55:13 +000096 switch (args.getPrivacyPolicy()) {
97 case PRIVACY_POLICY_LOCAL:
Yi Jinafb36062018-01-31 19:14:25 -080098 if (callingUid != AID_SHELL && callingUid != AID_ROOT) {
99 ALOGW("Calling pid %d and uid %d does not have permission to get local data.",
Yi Jinb592e3b2018-02-01 15:17:04 -0800100 callingPid, callingUid);
101 return Status::fromExceptionCode(
102 Status::EX_SECURITY,
103 "Calling process does not have permission to get local data.");
Yi Jin4bab3a12018-01-10 16:50:59 -0800104 }
Bookatzda9b8d02018-11-14 13:14:45 -0800105 break;
Joe Onorato99598ee2019-02-11 15:55:13 +0000106 case PRIVACY_POLICY_EXPLICIT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800107 if (callingUid != AID_SHELL && callingUid != AID_ROOT && callingUid != AID_STATSD &&
Bookatzda9b8d02018-11-14 13:14:45 -0800108 callingUid != AID_SYSTEM) {
Yi Jinafb36062018-01-31 19:14:25 -0800109 ALOGW("Calling pid %d and uid %d does not have permission to get explicit data.",
Yi Jinb592e3b2018-02-01 15:17:04 -0800110 callingPid, callingUid);
111 return Status::fromExceptionCode(
112 Status::EX_SECURITY,
113 "Calling process does not have permission to get explicit data.");
Yi Jin4bab3a12018-01-10 16:50:59 -0800114 }
Bookatzda9b8d02018-11-14 13:14:45 -0800115 break;
Yi Jin4bab3a12018-01-10 16:50:59 -0800116 }
Joe Onorato1754d742016-11-21 17:51:35 -0800117 return Status::ok();
118}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700119
Joe Onorato99598ee2019-02-11 15:55:13 +0000120static string build_uri(const string& pkg, const string& cls, const string& id) {
Yao Chencbafce92019-04-01 15:56:44 -0700121 return "content://android.os.IncidentManager/pending?pkg="
122 + pkg + "&receiver=" + cls + "&r=" + id;
Joe Onorato1754d742016-11-21 17:51:35 -0800123}
124
Joe Onorato1754d742016-11-21 17:51:35 -0800125// ================================================================================
Joe Onorato99598ee2019-02-11 15:55:13 +0000126ReportHandler::ReportHandler(const sp<WorkDirectory>& workDirectory,
127 const sp<Broadcaster>& broadcaster, const sp<Looper>& handlerLooper,
128 const sp<Throttler>& throttler)
129 :mLock(),
130 mWorkDirectory(workDirectory),
131 mBroadcaster(broadcaster),
132 mHandlerLooper(handlerLooper),
133 mBacklogDelay(DEFAULT_DELAY_NS),
134 mThrottler(throttler),
135 mBatch(new ReportBatch()) {
136}
Joe Onorato1754d742016-11-21 17:51:35 -0800137
Joe Onorato99598ee2019-02-11 15:55:13 +0000138ReportHandler::~ReportHandler() {
139}
Joe Onorato1754d742016-11-21 17:51:35 -0800140
Yi Jinb592e3b2018-02-01 15:17:04 -0800141void ReportHandler::handleMessage(const Message& message) {
Joe Onorato1754d742016-11-21 17:51:35 -0800142 switch (message.what) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000143 case WHAT_TAKE_REPORT:
144 take_report();
Joe Onorato1754d742016-11-21 17:51:35 -0800145 break;
Joe Onorato99598ee2019-02-11 15:55:13 +0000146 case WHAT_SEND_BROADCASTS:
147 send_broadcasts();
Joe Onorato1754d742016-11-21 17:51:35 -0800148 break;
149 }
150}
151
Joe Onorato99598ee2019-02-11 15:55:13 +0000152void ReportHandler::schedulePersistedReport(const IncidentReportArgs& args) {
153 mBatch->addPersistedReport(args);
154 mHandlerLooper->removeMessages(this, WHAT_TAKE_REPORT);
155 mHandlerLooper->sendMessage(this, Message(WHAT_TAKE_REPORT));
Joe Onorato1754d742016-11-21 17:51:35 -0800156}
157
Joe Onorato99598ee2019-02-11 15:55:13 +0000158void ReportHandler::scheduleStreamingReport(const IncidentReportArgs& args,
159 const sp<IIncidentReportStatusListener>& listener, int streamFd) {
160 mBatch->addStreamingReport(args, listener, streamFd);
161 mHandlerLooper->removeMessages(this, WHAT_TAKE_REPORT);
162 mHandlerLooper->sendMessage(this, Message(WHAT_TAKE_REPORT));
163}
164
165void ReportHandler::scheduleSendBacklog() {
Joe Onorato1754d742016-11-21 17:51:35 -0800166 unique_lock<mutex> lock(mLock);
Joe Onorato99598ee2019-02-11 15:55:13 +0000167 mBacklogDelay = DEFAULT_DELAY_NS;
168 schedule_send_broadcasts_locked();
Joe Onorato1754d742016-11-21 17:51:35 -0800169}
170
Joe Onorato99598ee2019-02-11 15:55:13 +0000171void ReportHandler::schedule_send_broadcasts_locked() {
172 mHandlerLooper->removeMessages(this, WHAT_SEND_BROADCASTS);
173 mHandlerLooper->sendMessageDelayed(mBacklogDelay, this, Message(WHAT_SEND_BROADCASTS));
Joe Onorato1754d742016-11-21 17:51:35 -0800174}
175
Joe Onorato99598ee2019-02-11 15:55:13 +0000176void ReportHandler::take_report() {
Joe Onoratoe5472052019-04-24 16:27:33 -0700177 // Cycle the batch and throttle.
Joe Onorato99598ee2019-02-11 15:55:13 +0000178 sp<ReportBatch> batch;
179 {
180 unique_lock<mutex> lock(mLock);
Joe Onoratoe5472052019-04-24 16:27:33 -0700181 batch = mThrottler->filterBatch(mBatch);
Joe Onorato1754d742016-11-21 17:51:35 -0800182 }
183
Joe Onorato99598ee2019-02-11 15:55:13 +0000184 if (batch->empty()) {
185 // Nothing to do.
186 return;
187 }
188
189 sp<Reporter> reporter = new Reporter(mWorkDirectory, batch);
190
Joe Onorato1754d742016-11-21 17:51:35 -0800191 // Take the report, which might take a while. More requests might queue
192 // up while we're doing this, and we'll handle them in their next batch.
193 // TODO: We should further rate-limit the reports to no more than N per time-period.
Joe Onorato99598ee2019-02-11 15:55:13 +0000194 // TODO: Move this inside reporter.
Yi Jin4e843102018-02-14 15:36:18 -0800195 size_t reportByteSize = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000196 reporter->runReport(&reportByteSize);
197
Joe Onoratoe5472052019-04-24 16:27:33 -0700198 // Tell the throttler how big it was, for the next throttling.
199 // TODO: This still isn't ideal. The throttler really should just track the
200 // persisted reqeusts, but changing Reporter::runReport() to track that individually
201 // will be a big change.
202 if (batch->hasPersistedReports()) {
203 mThrottler->addReportSize(reportByteSize);
204 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000205
206 // Kick off the next steps, one of which is to send any new or otherwise remaining
207 // approvals, and one of which is to send any new or remaining broadcasts.
208 {
Joe Onorato1754d742016-11-21 17:51:35 -0800209 unique_lock<mutex> lock(mLock);
Joe Onorato99598ee2019-02-11 15:55:13 +0000210 schedule_send_broadcasts_locked();
Joe Onorato1754d742016-11-21 17:51:35 -0800211 }
212}
213
Joe Onorato99598ee2019-02-11 15:55:13 +0000214void ReportHandler::send_broadcasts() {
215 Broadcaster::broadcast_status_t result = mBroadcaster->sendBroadcasts();
216 if (result == Broadcaster::BROADCASTS_FINISHED) {
217 // We're done.
218 unique_lock<mutex> lock(mLock);
219 mBacklogDelay = DEFAULT_DELAY_NS;
220 } else if (result == Broadcaster::BROADCASTS_REPEAT) {
221 // It worked, but there are more.
222 unique_lock<mutex> lock(mLock);
223 mBacklogDelay = DEFAULT_DELAY_NS;
224 schedule_send_broadcasts_locked();
225 } else if (result == Broadcaster::BROADCASTS_BACKOFF) {
Joe Onorato1754d742016-11-21 17:51:35 -0800226 // There was a failure. Exponential backoff.
227 unique_lock<mutex> lock(mLock);
228 mBacklogDelay *= 2;
229 ALOGI("Error sending to dropbox. Trying again in %lld minutes",
Yi Jinb592e3b2018-02-01 15:17:04 -0800230 (mBacklogDelay / (1000000000LL * 60)));
Joe Onorato99598ee2019-02-11 15:55:13 +0000231 schedule_send_broadcasts_locked();
Joe Onorato1754d742016-11-21 17:51:35 -0800232 }
233}
234
235// ================================================================================
Joe Onorato99598ee2019-02-11 15:55:13 +0000236IncidentService::IncidentService(const sp<Looper>& handlerLooper) {
237 mThrottler = new Throttler(DEFAULT_BYTES_SIZE_LIMIT, DEFAULT_REFACTORY_PERIOD_MS);
238 mWorkDirectory = new WorkDirectory();
239 mBroadcaster = new Broadcaster(mWorkDirectory);
240 mHandler = new ReportHandler(mWorkDirectory, mBroadcaster, handlerLooper,
241 mThrottler);
242 mBroadcaster->setHandler(mHandler);
Joe Onorato1754d742016-11-21 17:51:35 -0800243}
244
Yi Jinb592e3b2018-02-01 15:17:04 -0800245IncidentService::~IncidentService() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800246
Yi Jinb592e3b2018-02-01 15:17:04 -0800247Status IncidentService::reportIncident(const IncidentReportArgs& args) {
Joe Onoratoe5472052019-04-24 16:27:33 -0700248 IncidentReportArgs argsCopy(args);
Joe Onorato1754d742016-11-21 17:51:35 -0800249
Joe Onoratoe5472052019-04-24 16:27:33 -0700250 // Validate that the privacy policy is one of the real ones.
251 // If it isn't, clamp it to the next more restrictive real one.
252 argsCopy.setPrivacyPolicy(cleanup_privacy_policy(args.getPrivacyPolicy()));
Joe Onorato99598ee2019-02-11 15:55:13 +0000253
254 // TODO: Check that the broadcast recevier has the proper permissions
255 // TODO: Maybe we should consider relaxing the permissions if it's going to
256 // dropbox, but definitely not if it's going to the broadcaster.
Yi Jin4bab3a12018-01-10 16:50:59 -0800257 Status status = checkIncidentPermissions(args);
Joe Onorato1754d742016-11-21 17:51:35 -0800258 if (!status.isOk()) {
259 return status;
260 }
261
Joe Onoratoe5472052019-04-24 16:27:33 -0700262 // If they asked for the LOCAL privacy policy, give them EXPLICT. LOCAL has to
263 // be streamed. (This only applies to shell/root, because everyone else would have
264 // been rejected by checkIncidentPermissions()).
265 if (argsCopy.getPrivacyPolicy() < PRIVACY_POLICY_EXPLICIT) {
266 ALOGI("Demoting privacy policy to EXPLICT for persisted report.");
267 argsCopy.setPrivacyPolicy(PRIVACY_POLICY_EXPLICIT);
268 }
269
Joe Onorato99598ee2019-02-11 15:55:13 +0000270 // If they didn't specify a component, use dropbox.
Joe Onorato99598ee2019-02-11 15:55:13 +0000271 if (argsCopy.receiverPkg().length() == 0 && argsCopy.receiverCls().length() == 0) {
272 argsCopy.setReceiverPkg(DROPBOX_SENTINEL.getPackageName());
273 argsCopy.setReceiverCls(DROPBOX_SENTINEL.getClassName());
274 }
275
276 mHandler->schedulePersistedReport(argsCopy);
Joe Onorato1754d742016-11-21 17:51:35 -0800277
278 return Status::ok();
279}
280
Yi Jinb592e3b2018-02-01 15:17:04 -0800281Status IncidentService::reportIncidentToStream(const IncidentReportArgs& args,
282 const sp<IIncidentReportStatusListener>& listener,
283 const unique_fd& stream) {
Joe Onoratoe5472052019-04-24 16:27:33 -0700284 IncidentReportArgs argsCopy(args);
Joe Onorato99598ee2019-02-11 15:55:13 +0000285
286 // Streaming reports can not also be broadcast.
Joe Onorato99598ee2019-02-11 15:55:13 +0000287 argsCopy.setReceiverPkg("");
288 argsCopy.setReceiverCls("");
289
Joe Onoratoe5472052019-04-24 16:27:33 -0700290 // Validate that the privacy policy is one of the real ones.
291 // If it isn't, clamp it to the next more restrictive real one.
292 argsCopy.setPrivacyPolicy(cleanup_privacy_policy(args.getPrivacyPolicy()));
293
Joe Onorato99598ee2019-02-11 15:55:13 +0000294 Status status = checkIncidentPermissions(argsCopy);
Joe Onorato1754d742016-11-21 17:51:35 -0800295 if (!status.isOk()) {
296 return status;
297 }
298
Joe Onorato99598ee2019-02-11 15:55:13 +0000299 // The ReportRequest takes ownership of the fd, so we need to dup it.
Joe Onorato1754d742016-11-21 17:51:35 -0800300 int fd = dup(stream.get());
301 if (fd < 0) {
302 return Status::fromStatusT(-errno);
303 }
304
Joe Onorato99598ee2019-02-11 15:55:13 +0000305 mHandler->scheduleStreamingReport(argsCopy, listener, fd);
Joe Onorato1754d742016-11-21 17:51:35 -0800306
307 return Status::ok();
308}
309
Yi Jinb592e3b2018-02-01 15:17:04 -0800310Status IncidentService::systemRunning() {
Joe Onorato1754d742016-11-21 17:51:35 -0800311 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
312 return Status::fromExceptionCode(Status::EX_SECURITY,
Yi Jinb592e3b2018-02-01 15:17:04 -0800313 "Only system uid can call systemRunning");
Joe Onorato1754d742016-11-21 17:51:35 -0800314 }
Yi Jinadd11e92017-07-30 16:10:07 -0700315
Joe Onorato1754d742016-11-21 17:51:35 -0800316 // When system_server is up and running, schedule the dropbox task to run.
Joe Onorato99598ee2019-02-11 15:55:13 +0000317 mBroadcaster->reset();
318 mHandler->scheduleSendBacklog();
319
320 return Status::ok();
321}
322
323Status IncidentService::getIncidentReportList(const String16& pkg16, const String16& cls16,
324 vector<String16>* result) {
325 status_t err;
326 const string pkg(String8(pkg16).string());
327 const string cls(String8(cls16).string());
328
329 // List the reports
330 vector<sp<ReportFile>> all;
331 err = mWorkDirectory->getReports(&all, 0);
332 if (err != NO_ERROR) {
333 return Status::fromStatusT(err);
334 }
335
336 // Find the ones that match pkg and cls.
337 for (sp<ReportFile>& file: all) {
338 err = file->loadEnvelope();
339 if (err != NO_ERROR) {
340 continue;
341 }
342 const ReportFileProto& envelope = file->getEnvelope();
343 size_t reportCount = envelope.report_size();
344 for (int reportIndex = 0; reportIndex < reportCount; reportIndex++) {
345 const ReportFileProto_Report& report = envelope.report(reportIndex);
346 if (pkg == report.pkg() && cls == report.cls()) {
347 result->push_back(String16(build_uri(pkg, cls, file->getId()).c_str()));
348 break;
349 }
350 }
351 }
352
353 return Status::ok();
354}
355
356Status IncidentService::getIncidentReport(const String16& pkg16, const String16& cls16,
357 const String16& id16, IncidentManager::IncidentReport* result) {
358 status_t err;
359
360 const string pkg(String8(pkg16).string());
361 const string cls(String8(cls16).string());
362 const string id(String8(id16).string());
363
364 IncidentReportArgs args;
365 sp<ReportFile> file = mWorkDirectory->getReport(pkg, cls, id, &args);
366 if (file != nullptr) {
Yao Chencbafce92019-04-01 15:56:44 -0700367 // Create pipe
368 int fds[2];
369 if (pipe(fds) != 0) {
370 ALOGW("Error opening pipe to filter incident report: %s",
371 file->getDataFileName().c_str());
Joe Onorato99598ee2019-02-11 15:55:13 +0000372 return Status::ok();
373 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000374 result->setTimestampNs(file->getTimestampNs());
375 result->setPrivacyPolicy(file->getEnvelope().privacy_policy());
Yao Chencbafce92019-04-01 15:56:44 -0700376 result->takeFileDescriptor(fds[0]);
377 int writeFd = fds[1];
378 // spawn a thread to write the data. Release the writeFd ownership to the thread.
379 thread th([file, writeFd, args]() { file->startFilteringData(writeFd, args); });
380
381 th.detach();
Joe Onorato99598ee2019-02-11 15:55:13 +0000382 }
383
384 return Status::ok();
385}
386
387Status IncidentService::deleteIncidentReports(const String16& pkg16, const String16& cls16,
388 const String16& id16) {
389 const string pkg(String8(pkg16).string());
390 const string cls(String8(cls16).string());
391 const string id(String8(id16).string());
392
393 sp<ReportFile> file = mWorkDirectory->getReport(pkg, cls, id, nullptr);
394 if (file != nullptr) {
395 mWorkDirectory->commit(file, pkg, cls);
396 }
397 mBroadcaster->clearBroadcasts(pkg, cls, id);
398
399 return Status::ok();
400}
401
402Status IncidentService::deleteAllIncidentReports(const String16& pkg16) {
403 const string pkg(String8(pkg16).string());
404
405 mWorkDirectory->commitAll(pkg);
406 mBroadcaster->clearPackageBroadcasts(pkg);
Joe Onorato1754d742016-11-21 17:51:35 -0800407
408 return Status::ok();
409}
410
Yi Jinb592e3b2018-02-01 15:17:04 -0800411/**
412 * Implement our own because the default binder implementation isn't
413 * properly handling SHELL_COMMAND_TRANSACTION.
414 */
415status_t IncidentService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
416 uint32_t flags) {
417 status_t err;
418
419 switch (code) {
420 case SHELL_COMMAND_TRANSACTION: {
421 int in = data.readFileDescriptor();
422 int out = data.readFileDescriptor();
423 int err = data.readFileDescriptor();
424 int argc = data.readInt32();
425 Vector<String8> args;
426 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
427 args.add(String8(data.readString16()));
428 }
429 sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder());
430 sp<IResultReceiver> resultReceiver =
431 IResultReceiver::asInterface(data.readStrongBinder());
432
433 FILE* fin = fdopen(in, "r");
434 FILE* fout = fdopen(out, "w");
435 FILE* ferr = fdopen(err, "w");
436
437 if (fin == NULL || fout == NULL || ferr == NULL) {
438 resultReceiver->send(NO_MEMORY);
439 } else {
440 err = command(fin, fout, ferr, args);
441 resultReceiver->send(err);
442 }
443
444 if (fin != NULL) {
445 fflush(fin);
446 fclose(fin);
447 }
448 if (fout != NULL) {
449 fflush(fout);
450 fclose(fout);
451 }
452 if (fout != NULL) {
453 fflush(ferr);
454 fclose(ferr);
455 }
456
457 return NO_ERROR;
Bookatzda9b8d02018-11-14 13:14:45 -0800458 } break;
Yi Jinb592e3b2018-02-01 15:17:04 -0800459 default: { return BnIncidentManager::onTransact(code, data, reply, flags); }
460 }
461}
462
463status_t IncidentService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
464 const int argCount = args.size();
465
466 if (argCount >= 1) {
467 if (!args[0].compare(String8("privacy"))) {
468 return cmd_privacy(in, out, err, args);
469 }
Yi Jin4e843102018-02-14 15:36:18 -0800470 if (!args[0].compare(String8("throttler"))) {
471 mThrottler->dump(out);
472 return NO_ERROR;
473 }
Yi Jin908c02f2018-06-22 16:51:40 -0700474 if (!args[0].compare(String8("section"))) {
475 int id = atoi(args[1]);
476 int idx = 0;
477 while (SECTION_LIST[idx] != NULL) {
478 const Section* section = SECTION_LIST[idx];
479 if (section->id == id) {
480 fprintf(out, "Section[%d] %s\n", id, section->name.string());
481 break;
482 }
483 idx++;
484 }
485 return NO_ERROR;
486 }
Yi Jinb592e3b2018-02-01 15:17:04 -0800487 }
488 return cmd_help(out);
489}
490
491status_t IncidentService::cmd_help(FILE* out) {
492 fprintf(out, "usage: adb shell cmd incident privacy print <section_id>\n");
493 fprintf(out, "usage: adb shell cmd incident privacy parse <section_id> < proto.txt\n");
Yi Jin908c02f2018-06-22 16:51:40 -0700494 fprintf(out, " Prints/parses for the section id.\n\n");
495 fprintf(out, "usage: adb shell cmd incident section <section_id>\n");
496 fprintf(out, " Prints section id and its name.\n\n");
Yi Jin4e843102018-02-14 15:36:18 -0800497 fprintf(out, "usage: adb shell cmd incident throttler\n");
498 fprintf(out, " Prints the current throttler state\n");
Yi Jinb592e3b2018-02-01 15:17:04 -0800499 return NO_ERROR;
500}
501
502static void printPrivacy(const Privacy* p, FILE* out, String8 indent) {
503 if (p == NULL) return;
Joe Onorato99598ee2019-02-11 15:55:13 +0000504 fprintf(out, "%sid:%d, type:%d, dest:%d\n", indent.string(), p->field_id, p->type, p->policy);
Yi Jinb592e3b2018-02-01 15:17:04 -0800505 if (p->children == NULL) return;
506 for (int i = 0; p->children[i] != NULL; i++) { // NULL-terminated.
507 printPrivacy(p->children[i], out, indent + " ");
508 }
509}
510
511status_t IncidentService::cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000512 (void)in;
513
Yi Jinb592e3b2018-02-01 15:17:04 -0800514 const int argCount = args.size();
515 if (argCount >= 3) {
516 String8 opt = args[1];
517 int sectionId = atoi(args[2].string());
518
519 const Privacy* p = get_privacy_of_section(sectionId);
520 if (p == NULL) {
521 fprintf(err, "Can't find section id %d\n", sectionId);
522 return NO_ERROR;
523 }
524 fprintf(err, "Get privacy for %d\n", sectionId);
525 if (opt == "print") {
526 printPrivacy(p, out, String8(""));
527 } else if (opt == "parse") {
Joe Onorato99598ee2019-02-11 15:55:13 +0000528 /*
Yi Jinb592e3b2018-02-01 15:17:04 -0800529 FdBuffer buf;
Yi Jine3dab2d2018-03-22 16:56:39 -0700530 status_t error = buf.read(fileno(in), 60000);
Yi Jinb592e3b2018-02-01 15:17:04 -0800531 if (error != NO_ERROR) {
532 fprintf(err, "Error reading from stdin\n");
533 return error;
534 }
535 fprintf(err, "Read %zu bytes\n", buf.size());
Joe Onorato99598ee2019-02-11 15:55:13 +0000536 PrivacyFilter pBuf(p, buf.data());
Yi Jinb592e3b2018-02-01 15:17:04 -0800537
538 PrivacySpec spec = PrivacySpec::new_spec(argCount > 3 ? atoi(args[3]) : -1);
539 error = pBuf.strip(spec);
540 if (error != NO_ERROR) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000541 fprintf(err, "Error strip pii fields with spec %d\n", spec.policy);
Yi Jinb592e3b2018-02-01 15:17:04 -0800542 return error;
543 }
544 return pBuf.flush(fileno(out));
Joe Onorato99598ee2019-02-11 15:55:13 +0000545 */
546 return -1;
Yi Jinb592e3b2018-02-01 15:17:04 -0800547 }
548 } else {
549 return cmd_help(out);
550 }
551 return NO_ERROR;
552}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700553
Mike Ma85434ec2018-11-27 10:32:31 -0800554status_t IncidentService::dump(int fd, const Vector<String16>& args) {
555 if (std::find(args.begin(), args.end(), String16("--proto")) == args.end()) {
556 ALOGD("Skip dumping incident. Only proto format is supported.");
557 dprintf(fd, "Incident dump only supports proto version.\n");
558 return NO_ERROR;
559 }
560
561 ALOGD("Dump incident proto");
562 IncidentReportArgs incidentArgs;
Joe Onorato99598ee2019-02-11 15:55:13 +0000563 incidentArgs.setPrivacyPolicy(PRIVACY_POLICY_EXPLICIT);
Mike Ma85434ec2018-11-27 10:32:31 -0800564 int skipped[] = SKIPPED_SECTIONS;
565 for (const Section** section = SECTION_LIST; *section; section++) {
566 const int id = (*section)->id;
567 if (std::find(std::begin(skipped), std::end(skipped), id) == std::end(skipped)) {
568 incidentArgs.addSection(id);
569 }
570 }
571
572 if (!checkIncidentPermissions(incidentArgs).isOk()) {
573 return PERMISSION_DENIED;
574 }
575
Joe Onorato99598ee2019-02-11 15:55:13 +0000576 // The ReportRequest takes ownership of the fd, so we need to dup it.
Mike Ma85434ec2018-11-27 10:32:31 -0800577 int fd1 = dup(fd);
578 if (fd1 < 0) {
579 return -errno;
580 }
581
Joe Onorato99598ee2019-02-11 15:55:13 +0000582 // TODO: Remove this. Someone even dumpstate, wanting to get an incident report
583 // should use the API. That will take making dumpstated call the API, which is a
584 // good thing. It also means it won't be subject to the timeout.
585 mHandler->scheduleStreamingReport(incidentArgs, NULL, fd1);
Mike Ma85434ec2018-11-27 10:32:31 -0800586
587 return NO_ERROR;
588}
589
Yi Jin6cacbcb2018-03-30 14:04:52 -0700590} // namespace incidentd
591} // namespace os
592} // namespace android