| 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" | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 22 | #include "PrivacyFilter.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 |  | 
| Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 27 | #include <android/os/IncidentReportArgs.h> | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 28 | #include <binder/IPCThreadState.h> | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 29 | #include <binder/IResultReceiver.h> | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 30 | #include <binder/IServiceManager.h> | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 31 | #include <binder/IShellCallback.h> | 
| Mike Ma | 3505666 | 2018-12-06 13:32:59 -0800 | [diff] [blame] | 32 | #include <log/log.h> | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 33 | #include <private/android_filesystem_config.h> | 
 | 34 | #include <utils/Looper.h> | 
| Yao Chen | cbafce9 | 2019-04-01 15:56:44 -0700 | [diff] [blame] | 35 | #include <thread> | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 36 |  | 
 | 37 | #include <unistd.h> | 
 | 38 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 39 | enum { | 
 | 40 |     WHAT_TAKE_REPORT = 1, | 
 | 41 |     WHAT_SEND_BROADCASTS = 2 | 
 | 42 | }; | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 43 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 44 | #define DEFAULT_DELAY_NS (1000000000LL) | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 45 |  | 
| Rafal Slawik | 1334f1a | 2022-01-12 17:13:33 +0000 | [diff] [blame] | 46 | #define DEFAULT_BYTES_SIZE_LIMIT (400 * 1024 * 1024)        // 400MB | 
| Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 47 | #define DEFAULT_REFACTORY_PERIOD_MS (24 * 60 * 60 * 1000)  // 1 Day | 
 | 48 |  | 
| Mike Ma | 5a57d79 | 2019-08-21 14:52:46 -0700 | [diff] [blame] | 49 | // Skip these sections (for dumpstate only) | 
| Kevin Jeon | 4ee5ba59e | 2022-07-18 20:26:38 +0000 | [diff] [blame] | 50 | // Skip logs (1100 - 1108), traces (1200 - 1202), dumpsys (3000 - 3024, 3027 - 3056, 4000 - 4001) | 
 | 51 | // because they are already in the bug report. | 
| Mike Ma | 5a57d79 | 2019-08-21 14:52:46 -0700 | [diff] [blame] | 52 | #define SKIPPED_DUMPSTATE_SECTIONS { \ | 
 | 53 |             1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, /* Logs */ \ | 
| Kevin Jeon | 4ee5ba59e | 2022-07-18 20:26:38 +0000 | [diff] [blame] | 54 |             1200, 1201, 1202, /* Native, hal, java traces */ \ | 
| Kevin Jeon | b814366 | 2022-10-10 16:57:53 +0000 | [diff] [blame] | 55 |             3018, /* dumpsys meminfo*/ } | 
| Mike Ma | 85434ec | 2018-11-27 10:32:31 -0800 | [diff] [blame] | 56 |  | 
| Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 57 | namespace android { | 
 | 58 | namespace os { | 
 | 59 | namespace incidentd { | 
 | 60 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 61 | String16 const APPROVE_INCIDENT_REPORTS("android.permission.APPROVE_INCIDENT_REPORTS"); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 62 | String16 const DUMP_PERMISSION("android.permission.DUMP"); | 
 | 63 | String16 const USAGE_STATS_PERMISSION("android.permission.PACKAGE_USAGE_STATS"); | 
 | 64 |  | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 65 | static Status checkIncidentPermissions(const IncidentReportArgs& args) { | 
| Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 66 |     uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 67 |     pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
| Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 68 |     if (callingUid == AID_ROOT || callingUid == AID_SHELL) { | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 69 |         // Root and shell are ok. | 
 | 70 |         return Status::ok(); | 
 | 71 |     } | 
 | 72 |  | 
 | 73 |     if (checkCallingPermission(APPROVE_INCIDENT_REPORTS)) { | 
 | 74 |         // Permission controller (this is a singleton permission that is always granted | 
 | 75 |         // exactly for PermissionController) is allowed to access incident reports | 
 | 76 |         // so it can show the user info about what they are approving. | 
| Yi Jin | 437aa6e | 2018-01-10 11:34:26 -0800 | [diff] [blame] | 77 |         return Status::ok(); | 
 | 78 |     } | 
 | 79 |  | 
| Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 80 |     // checking calling permission. | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 81 |     if (!checkCallingPermission(DUMP_PERMISSION)) { | 
 | 82 |         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] | 83 |               callingPid, callingUid); | 
 | 84 |         return Status::fromExceptionCode( | 
 | 85 |                 Status::EX_SECURITY, | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 86 |                 "Calling process does not have permission: android.permission.DUMP"); | 
 | 87 |     } | 
 | 88 |     if (!checkCallingPermission(USAGE_STATS_PERMISSION)) { | 
 | 89 |         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] | 90 |               callingPid, callingUid); | 
 | 91 |         return Status::fromExceptionCode( | 
 | 92 |                 Status::EX_SECURITY, | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 93 |                 "Calling process does not have permission: android.permission.USAGE_STATS"); | 
 | 94 |     } | 
| Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 95 |  | 
 | 96 |     // checking calling request uid permission. | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 97 |     switch (args.getPrivacyPolicy()) { | 
 | 98 |         case PRIVACY_POLICY_LOCAL: | 
| Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 99 |             if (callingUid != AID_SHELL && callingUid != AID_ROOT) { | 
 | 100 |                 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] | 101 |                       callingPid, callingUid); | 
 | 102 |                 return Status::fromExceptionCode( | 
 | 103 |                         Status::EX_SECURITY, | 
 | 104 |                         "Calling process does not have permission to get local data."); | 
| Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 105 |             } | 
| Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 106 |             break; | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 107 |         case PRIVACY_POLICY_EXPLICIT: | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 108 |             if (callingUid != AID_SHELL && callingUid != AID_ROOT && callingUid != AID_STATSD && | 
| Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 109 |                     callingUid != AID_SYSTEM) { | 
| Yi Jin | afb3606 | 2018-01-31 19:14:25 -0800 | [diff] [blame] | 110 |                 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] | 111 |                       callingPid, callingUid); | 
 | 112 |                 return Status::fromExceptionCode( | 
 | 113 |                         Status::EX_SECURITY, | 
 | 114 |                         "Calling process does not have permission to get explicit data."); | 
| Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 115 |             } | 
| Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 116 |             break; | 
| Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 117 |     } | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 118 |     return Status::ok(); | 
 | 119 | } | 
| Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 120 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 121 | static string build_uri(const string& pkg, const string& cls, const string& id) { | 
| Yao Chen | cbafce9 | 2019-04-01 15:56:44 -0700 | [diff] [blame] | 122 |     return "content://android.os.IncidentManager/pending?pkg=" | 
 | 123 |         + pkg + "&receiver=" + cls + "&r=" + id; | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 124 | } | 
 | 125 |  | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 126 | // ================================================================================ | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 127 | ReportHandler::ReportHandler(const sp<WorkDirectory>& workDirectory, | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 128 |                              const sp<Broadcaster>& broadcaster, | 
 | 129 |                              const sp<Looper>& handlerLooper, | 
 | 130 |                              const sp<Throttler>& throttler, | 
 | 131 |                              const vector<BringYourOwnSection*>& registeredSections) | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 132 |         :mLock(), | 
 | 133 |          mWorkDirectory(workDirectory), | 
 | 134 |          mBroadcaster(broadcaster), | 
 | 135 |          mHandlerLooper(handlerLooper), | 
 | 136 |          mBacklogDelay(DEFAULT_DELAY_NS), | 
 | 137 |          mThrottler(throttler), | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 138 |          mRegisteredSections(registeredSections), | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 139 |          mBatch(new ReportBatch()) { | 
 | 140 | } | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 141 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 142 | ReportHandler::~ReportHandler() { | 
 | 143 | } | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 144 |  | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 145 | void ReportHandler::handleMessage(const Message& message) { | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 146 |     switch (message.what) { | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 147 |         case WHAT_TAKE_REPORT: | 
 | 148 |             take_report(); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 149 |             break; | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 150 |         case WHAT_SEND_BROADCASTS: | 
 | 151 |             send_broadcasts(); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 152 |             break; | 
 | 153 |     } | 
 | 154 | } | 
 | 155 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 156 | void ReportHandler::schedulePersistedReport(const IncidentReportArgs& args) { | 
| Mike Ma | b98050f | 2020-03-30 13:37:31 -0700 | [diff] [blame] | 157 |     unique_lock<mutex> lock(mLock); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 158 |     mBatch->addPersistedReport(args); | 
 | 159 |     mHandlerLooper->removeMessages(this, WHAT_TAKE_REPORT); | 
 | 160 |     mHandlerLooper->sendMessage(this, Message(WHAT_TAKE_REPORT)); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 161 | } | 
 | 162 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 163 | void ReportHandler::scheduleStreamingReport(const IncidentReportArgs& args, | 
 | 164 |         const sp<IIncidentReportStatusListener>& listener, int streamFd) { | 
| Mike Ma | b98050f | 2020-03-30 13:37:31 -0700 | [diff] [blame] | 165 |     unique_lock<mutex> lock(mLock); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 166 |     mBatch->addStreamingReport(args, listener, streamFd); | 
 | 167 |     mHandlerLooper->removeMessages(this, WHAT_TAKE_REPORT); | 
 | 168 |     mHandlerLooper->sendMessage(this, Message(WHAT_TAKE_REPORT)); | 
 | 169 | } | 
 | 170 |  | 
 | 171 | void ReportHandler::scheduleSendBacklog() { | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 172 |     unique_lock<mutex> lock(mLock); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 173 |     mBacklogDelay = DEFAULT_DELAY_NS; | 
 | 174 |     schedule_send_broadcasts_locked(); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 175 | } | 
 | 176 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 177 | void ReportHandler::schedule_send_broadcasts_locked() { | 
 | 178 |     mHandlerLooper->removeMessages(this, WHAT_SEND_BROADCASTS); | 
 | 179 |     mHandlerLooper->sendMessageDelayed(mBacklogDelay, this, Message(WHAT_SEND_BROADCASTS)); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 180 | } | 
 | 181 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 182 | void ReportHandler::take_report() { | 
| Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 183 |     // Cycle the batch and throttle. | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 184 |     sp<ReportBatch> batch; | 
 | 185 |     { | 
 | 186 |         unique_lock<mutex> lock(mLock); | 
| Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 187 |         batch = mThrottler->filterBatch(mBatch); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 188 |     } | 
 | 189 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 190 |     if (batch->empty()) { | 
 | 191 |         // Nothing to do. | 
 | 192 |         return; | 
 | 193 |     } | 
 | 194 |  | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 195 |     sp<Reporter> reporter = new Reporter(mWorkDirectory, batch, mRegisteredSections); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 196 |  | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 197 |     // Take the report, which might take a while. More requests might queue | 
 | 198 |     // up while we're doing this, and we'll handle them in their next batch. | 
 | 199 |     // TODO: We should further rate-limit the reports to no more than N per time-period. | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 200 |     // TODO: Move this inside reporter. | 
| Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 201 |     size_t reportByteSize = 0; | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 202 |     reporter->runReport(&reportByteSize); | 
 | 203 |  | 
| Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 204 |     // Tell the throttler how big it was, for the next throttling. | 
 | 205 |     // TODO: This still isn't ideal. The throttler really should just track the | 
 | 206 |     // persisted reqeusts, but changing Reporter::runReport() to track that individually | 
 | 207 |     // will be a big change. | 
 | 208 |     if (batch->hasPersistedReports()) { | 
 | 209 |         mThrottler->addReportSize(reportByteSize); | 
 | 210 |     } | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 211 |  | 
 | 212 |     // Kick off the next steps, one of which is to send any new or otherwise remaining | 
 | 213 |     // approvals, and one of which is to send any new or remaining broadcasts. | 
 | 214 |     { | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 215 |         unique_lock<mutex> lock(mLock); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 216 |         schedule_send_broadcasts_locked(); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 217 |     } | 
 | 218 | } | 
 | 219 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 220 | void ReportHandler::send_broadcasts() { | 
 | 221 |     Broadcaster::broadcast_status_t result = mBroadcaster->sendBroadcasts(); | 
 | 222 |     if (result == Broadcaster::BROADCASTS_FINISHED) { | 
 | 223 |         // We're done. | 
 | 224 |         unique_lock<mutex> lock(mLock); | 
 | 225 |         mBacklogDelay = DEFAULT_DELAY_NS; | 
 | 226 |     } else if (result == Broadcaster::BROADCASTS_REPEAT) { | 
 | 227 |         // It worked, but there are more. | 
 | 228 |         unique_lock<mutex> lock(mLock); | 
 | 229 |         mBacklogDelay = DEFAULT_DELAY_NS; | 
 | 230 |         schedule_send_broadcasts_locked(); | 
 | 231 |     } else if (result == Broadcaster::BROADCASTS_BACKOFF) { | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 232 |         // There was a failure. Exponential backoff. | 
 | 233 |         unique_lock<mutex> lock(mLock); | 
 | 234 |         mBacklogDelay *= 2; | 
 | 235 |         ALOGI("Error sending to dropbox. Trying again in %lld minutes", | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 236 |               (mBacklogDelay / (1000000000LL * 60))); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 237 |         schedule_send_broadcasts_locked(); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 238 |     } | 
 | 239 | } | 
 | 240 |  | 
 | 241 | // ================================================================================ | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 242 | IncidentService::IncidentService(const sp<Looper>& handlerLooper) { | 
 | 243 |     mThrottler = new Throttler(DEFAULT_BYTES_SIZE_LIMIT, DEFAULT_REFACTORY_PERIOD_MS); | 
 | 244 |     mWorkDirectory = new WorkDirectory(); | 
 | 245 |     mBroadcaster = new Broadcaster(mWorkDirectory); | 
 | 246 |     mHandler = new ReportHandler(mWorkDirectory, mBroadcaster, handlerLooper, | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 247 |             mThrottler, mRegisteredSections); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 248 |     mBroadcaster->setHandler(mHandler); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 249 | } | 
 | 250 |  | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 251 | IncidentService::~IncidentService() {} | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 252 |  | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 253 | Status IncidentService::reportIncident(const IncidentReportArgs& args) { | 
| Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 254 |     IncidentReportArgs argsCopy(args); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 255 |  | 
| Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 256 |     // Validate that the privacy policy is one of the real ones. | 
 | 257 |     // If it isn't, clamp it to the next more restrictive real one. | 
 | 258 |     argsCopy.setPrivacyPolicy(cleanup_privacy_policy(args.getPrivacyPolicy())); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 259 |  | 
 | 260 |     // TODO: Check that the broadcast recevier has the proper permissions | 
 | 261 |     // TODO: Maybe we should consider relaxing the permissions if it's going to | 
 | 262 |     // dropbox, but definitely not if it's going to the broadcaster. | 
| Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 263 |     Status status = checkIncidentPermissions(args); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 264 |     if (!status.isOk()) { | 
 | 265 |         return status; | 
 | 266 |     } | 
 | 267 |  | 
| Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 268 |     // If they asked for the LOCAL privacy policy, give them EXPLICT.  LOCAL has to | 
 | 269 |     // be streamed. (This only applies to shell/root, because everyone else would have | 
 | 270 |     // been rejected by checkIncidentPermissions()). | 
 | 271 |     if (argsCopy.getPrivacyPolicy() < PRIVACY_POLICY_EXPLICIT) { | 
 | 272 |         ALOGI("Demoting privacy policy to EXPLICT for persisted report."); | 
 | 273 |         argsCopy.setPrivacyPolicy(PRIVACY_POLICY_EXPLICIT); | 
 | 274 |     } | 
 | 275 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 276 |     // If they didn't specify a component, use dropbox. | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 277 |     if (argsCopy.receiverPkg().length() == 0 && argsCopy.receiverCls().length() == 0) { | 
 | 278 |         argsCopy.setReceiverPkg(DROPBOX_SENTINEL.getPackageName()); | 
 | 279 |         argsCopy.setReceiverCls(DROPBOX_SENTINEL.getClassName()); | 
 | 280 |     } | 
 | 281 |  | 
 | 282 |     mHandler->schedulePersistedReport(argsCopy); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 283 |  | 
 | 284 |     return Status::ok(); | 
 | 285 | } | 
 | 286 |  | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 287 | Status IncidentService::reportIncidentToStream(const IncidentReportArgs& args, | 
 | 288 |                                                const sp<IIncidentReportStatusListener>& listener, | 
| Jiyong Park | b8ba234 | 2019-11-25 11:03:38 +0900 | [diff] [blame] | 289 |                                                unique_fd stream) { | 
| Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 290 |     IncidentReportArgs argsCopy(args); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 291 |  | 
 | 292 |     // Streaming reports can not also be broadcast. | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 293 |     argsCopy.setReceiverPkg(""); | 
 | 294 |     argsCopy.setReceiverCls(""); | 
 | 295 |  | 
| Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 296 |     // Validate that the privacy policy is one of the real ones. | 
 | 297 |     // If it isn't, clamp it to the next more restrictive real one. | 
 | 298 |     argsCopy.setPrivacyPolicy(cleanup_privacy_policy(args.getPrivacyPolicy())); | 
 | 299 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 300 |     Status status = checkIncidentPermissions(argsCopy); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 301 |     if (!status.isOk()) { | 
 | 302 |         return status; | 
 | 303 |     } | 
 | 304 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 305 |     // The ReportRequest takes ownership of the fd, so we need to dup it. | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 306 |     int fd = dup(stream.get()); | 
 | 307 |     if (fd < 0) { | 
 | 308 |         return Status::fromStatusT(-errno); | 
 | 309 |     } | 
 | 310 |  | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 311 |     mHandler->scheduleStreamingReport(argsCopy, listener, fd); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 312 |  | 
 | 313 |     return Status::ok(); | 
 | 314 | } | 
 | 315 |  | 
| Jiyong Park | b8ba234 | 2019-11-25 11:03:38 +0900 | [diff] [blame] | 316 | Status IncidentService::reportIncidentToDumpstate(unique_fd stream, | 
| Mike Ma | 5a57d79 | 2019-08-21 14:52:46 -0700 | [diff] [blame] | 317 |         const sp<IIncidentReportStatusListener>& listener) { | 
 | 318 |     uid_t caller = IPCThreadState::self()->getCallingUid(); | 
 | 319 |     if (caller != AID_ROOT && caller != AID_SHELL) { | 
 | 320 |         ALOGW("Calling uid %d does not have permission: only ROOT or SHELL allowed", caller); | 
 | 321 |         return Status::fromExceptionCode(Status::EX_SECURITY, "Only ROOT or SHELL allowed"); | 
 | 322 |     } | 
 | 323 |  | 
 | 324 |     ALOGD("Stream incident report to dumpstate"); | 
 | 325 |     IncidentReportArgs incidentArgs; | 
 | 326 |     // Privacy policy for dumpstate incident reports is always EXPLICIT. | 
 | 327 |     incidentArgs.setPrivacyPolicy(PRIVACY_POLICY_EXPLICIT); | 
 | 328 |  | 
 | 329 |     int skipped[] = SKIPPED_DUMPSTATE_SECTIONS; | 
 | 330 |     for (const Section** section = SECTION_LIST; *section; section++) { | 
 | 331 |         const int id = (*section)->id; | 
 | 332 |         if (std::find(std::begin(skipped), std::end(skipped), id) == std::end(skipped) | 
 | 333 |                 && !section_requires_specific_mention(id)) { | 
 | 334 |             incidentArgs.addSection(id); | 
 | 335 |         } | 
 | 336 |     } | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 337 |     for (const Section* section : mRegisteredSections) { | 
 | 338 |         if (!section_requires_specific_mention(section->id)) { | 
 | 339 |             incidentArgs.addSection(section->id); | 
 | 340 |         } | 
 | 341 |     } | 
| Mike Ma | 5a57d79 | 2019-08-21 14:52:46 -0700 | [diff] [blame] | 342 |  | 
 | 343 |     // The ReportRequest takes ownership of the fd, so we need to dup it. | 
 | 344 |     int fd = dup(stream.get()); | 
 | 345 |     if (fd < 0) { | 
 | 346 |         return Status::fromStatusT(-errno); | 
 | 347 |     } | 
 | 348 |  | 
 | 349 |     mHandler->scheduleStreamingReport(incidentArgs, listener, fd); | 
 | 350 |  | 
 | 351 |     return Status::ok(); | 
 | 352 | } | 
 | 353 |  | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 354 | Status IncidentService::registerSection(const int id, const String16& name16, | 
 | 355 |         const sp<IIncidentDumpCallback>& callback) { | 
| Wenjie Zhou | 751c7c9 | 2020-05-14 12:49:19 -0700 | [diff] [blame] | 356 |     const String8 name = String8(name16); | 
| Mike Ma | c2ab45a | 2020-01-17 06:11:24 +0000 | [diff] [blame] | 357 |     const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Wenjie Zhou | 751c7c9 | 2020-05-14 12:49:19 -0700 | [diff] [blame] | 358 |     ALOGI("Uid %d registers section %d '%s'", callingUid, id, name.c_str()); | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 359 |     if (callback == nullptr) { | 
 | 360 |         return Status::fromExceptionCode(Status::EX_NULL_POINTER); | 
 | 361 |     } | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 362 |     for (int i = 0; i < mRegisteredSections.size(); i++) { | 
 | 363 |         if (mRegisteredSections.at(i)->id == id) { | 
 | 364 |             if (mRegisteredSections.at(i)->uid != callingUid) { | 
 | 365 |                 ALOGW("Error registering section %d: calling uid does not match", id); | 
 | 366 |                 return Status::fromExceptionCode(Status::EX_SECURITY); | 
 | 367 |             } | 
| Wenjie Zhou | 751c7c9 | 2020-05-14 12:49:19 -0700 | [diff] [blame] | 368 |             mRegisteredSections.at(i) = new BringYourOwnSection(id, name.c_str(), callingUid, callback); | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 369 |             return Status::ok(); | 
 | 370 |         } | 
 | 371 |     } | 
| Wenjie Zhou | 751c7c9 | 2020-05-14 12:49:19 -0700 | [diff] [blame] | 372 |     mRegisteredSections.push_back(new BringYourOwnSection(id, name.c_str(), callingUid, callback)); | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 373 |     return Status::ok(); | 
 | 374 | } | 
 | 375 |  | 
 | 376 | Status IncidentService::unregisterSection(const int id) { | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 377 |     uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Mike Ma | c2ab45a | 2020-01-17 06:11:24 +0000 | [diff] [blame] | 378 |     ALOGI("Uid %d unregisters section %d", callingUid, id); | 
 | 379 |  | 
| Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 380 |     for (auto it = mRegisteredSections.begin(); it != mRegisteredSections.end(); it++) { | 
 | 381 |         if ((*it)->id == id) { | 
 | 382 |             if ((*it)->uid != callingUid) { | 
 | 383 |                 ALOGW("Error unregistering section %d: calling uid does not match", id); | 
 | 384 |                 return Status::fromExceptionCode(Status::EX_SECURITY); | 
 | 385 |             } | 
 | 386 |             mRegisteredSections.erase(it); | 
 | 387 |             return Status::ok(); | 
 | 388 |         } | 
 | 389 |     } | 
 | 390 |     ALOGW("Section %d not found", id); | 
 | 391 |     return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); | 
 | 392 | } | 
 | 393 |  | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 394 | Status IncidentService::systemRunning() { | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 395 |     if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { | 
 | 396 |         return Status::fromExceptionCode(Status::EX_SECURITY, | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 397 |                                          "Only system uid can call systemRunning"); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 398 |     } | 
| Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 399 |  | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 400 |     // When system_server is up and running, schedule the dropbox task to run. | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 401 |     mBroadcaster->reset(); | 
 | 402 |     mHandler->scheduleSendBacklog(); | 
 | 403 |  | 
 | 404 |     return Status::ok(); | 
 | 405 | } | 
 | 406 |  | 
 | 407 | Status IncidentService::getIncidentReportList(const String16& pkg16, const String16& cls16, | 
 | 408 |             vector<String16>* result) { | 
 | 409 |     status_t err; | 
 | 410 |     const string pkg(String8(pkg16).string()); | 
 | 411 |     const string cls(String8(cls16).string()); | 
 | 412 |  | 
 | 413 |     // List the reports | 
 | 414 |     vector<sp<ReportFile>> all; | 
 | 415 |     err = mWorkDirectory->getReports(&all, 0); | 
 | 416 |     if (err != NO_ERROR) { | 
 | 417 |         return Status::fromStatusT(err); | 
 | 418 |     } | 
 | 419 |  | 
 | 420 |     // Find the ones that match pkg and cls. | 
 | 421 |     for (sp<ReportFile>& file: all) { | 
 | 422 |         err = file->loadEnvelope(); | 
 | 423 |         if (err != NO_ERROR) { | 
 | 424 |             continue; | 
 | 425 |         } | 
 | 426 |         const ReportFileProto& envelope = file->getEnvelope(); | 
 | 427 |         size_t reportCount = envelope.report_size(); | 
 | 428 |         for (int reportIndex = 0; reportIndex < reportCount; reportIndex++) { | 
 | 429 |             const ReportFileProto_Report& report = envelope.report(reportIndex); | 
 | 430 |             if (pkg == report.pkg() && cls == report.cls()) { | 
 | 431 |                 result->push_back(String16(build_uri(pkg, cls, file->getId()).c_str())); | 
 | 432 |                 break; | 
 | 433 |             } | 
 | 434 |         } | 
 | 435 |     } | 
 | 436 |  | 
 | 437 |     return Status::ok(); | 
 | 438 | } | 
 | 439 |  | 
 | 440 | Status IncidentService::getIncidentReport(const String16& pkg16, const String16& cls16, | 
 | 441 |             const String16& id16, IncidentManager::IncidentReport* result) { | 
 | 442 |     status_t err; | 
 | 443 |  | 
 | 444 |     const string pkg(String8(pkg16).string()); | 
 | 445 |     const string cls(String8(cls16).string()); | 
 | 446 |     const string id(String8(id16).string()); | 
 | 447 |  | 
 | 448 |     IncidentReportArgs args; | 
 | 449 |     sp<ReportFile> file = mWorkDirectory->getReport(pkg, cls, id, &args); | 
 | 450 |     if (file != nullptr) { | 
| Yao Chen | cbafce9 | 2019-04-01 15:56:44 -0700 | [diff] [blame] | 451 |         // Create pipe | 
 | 452 |         int fds[2]; | 
 | 453 |         if (pipe(fds) != 0) { | 
 | 454 |             ALOGW("Error opening pipe to filter incident report: %s", | 
 | 455 |                   file->getDataFileName().c_str()); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 456 |             return Status::ok(); | 
 | 457 |         } | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 458 |         result->setTimestampNs(file->getTimestampNs()); | 
 | 459 |         result->setPrivacyPolicy(file->getEnvelope().privacy_policy()); | 
| Yao Chen | cbafce9 | 2019-04-01 15:56:44 -0700 | [diff] [blame] | 460 |         result->takeFileDescriptor(fds[0]); | 
 | 461 |         int writeFd = fds[1]; | 
 | 462 |         // spawn a thread to write the data. Release the writeFd ownership to the thread. | 
 | 463 |         thread th([file, writeFd, args]() { file->startFilteringData(writeFd, args); }); | 
 | 464 |  | 
 | 465 |         th.detach(); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 466 |     } | 
 | 467 |  | 
 | 468 |     return Status::ok(); | 
 | 469 | } | 
 | 470 |  | 
 | 471 | Status IncidentService::deleteIncidentReports(const String16& pkg16, const String16& cls16, | 
 | 472 |             const String16& id16) { | 
 | 473 |     const string pkg(String8(pkg16).string()); | 
 | 474 |     const string cls(String8(cls16).string()); | 
 | 475 |     const string id(String8(id16).string()); | 
 | 476 |  | 
 | 477 |     sp<ReportFile> file = mWorkDirectory->getReport(pkg, cls, id, nullptr); | 
 | 478 |     if (file != nullptr) { | 
 | 479 |         mWorkDirectory->commit(file, pkg, cls); | 
 | 480 |     } | 
 | 481 |     mBroadcaster->clearBroadcasts(pkg, cls, id); | 
 | 482 |  | 
 | 483 |     return Status::ok(); | 
 | 484 | } | 
 | 485 |  | 
 | 486 | Status IncidentService::deleteAllIncidentReports(const String16& pkg16) { | 
 | 487 |     const string pkg(String8(pkg16).string()); | 
 | 488 |  | 
 | 489 |     mWorkDirectory->commitAll(pkg); | 
 | 490 |     mBroadcaster->clearPackageBroadcasts(pkg); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 491 |  | 
 | 492 |     return Status::ok(); | 
 | 493 | } | 
 | 494 |  | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 495 | /** | 
 | 496 |  * Implement our own because the default binder implementation isn't | 
 | 497 |  * properly handling SHELL_COMMAND_TRANSACTION. | 
 | 498 |  */ | 
 | 499 | status_t IncidentService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, | 
 | 500 |                                      uint32_t flags) { | 
 | 501 |     status_t err; | 
 | 502 |  | 
 | 503 |     switch (code) { | 
 | 504 |         case SHELL_COMMAND_TRANSACTION: { | 
 | 505 |             int in = data.readFileDescriptor(); | 
 | 506 |             int out = data.readFileDescriptor(); | 
 | 507 |             int err = data.readFileDescriptor(); | 
 | 508 |             int argc = data.readInt32(); | 
 | 509 |             Vector<String8> args; | 
 | 510 |             for (int i = 0; i < argc && data.dataAvail() > 0; i++) { | 
 | 511 |                 args.add(String8(data.readString16())); | 
 | 512 |             } | 
 | 513 |             sp<IShellCallback> shellCallback = IShellCallback::asInterface(data.readStrongBinder()); | 
 | 514 |             sp<IResultReceiver> resultReceiver = | 
 | 515 |                     IResultReceiver::asInterface(data.readStrongBinder()); | 
 | 516 |  | 
 | 517 |             FILE* fin = fdopen(in, "r"); | 
 | 518 |             FILE* fout = fdopen(out, "w"); | 
 | 519 |             FILE* ferr = fdopen(err, "w"); | 
 | 520 |  | 
 | 521 |             if (fin == NULL || fout == NULL || ferr == NULL) { | 
 | 522 |                 resultReceiver->send(NO_MEMORY); | 
 | 523 |             } else { | 
 | 524 |                 err = command(fin, fout, ferr, args); | 
 | 525 |                 resultReceiver->send(err); | 
 | 526 |             } | 
 | 527 |  | 
 | 528 |             if (fin != NULL) { | 
 | 529 |                 fflush(fin); | 
 | 530 |                 fclose(fin); | 
 | 531 |             } | 
 | 532 |             if (fout != NULL) { | 
 | 533 |                 fflush(fout); | 
 | 534 |                 fclose(fout); | 
 | 535 |             } | 
 | 536 |             if (fout != NULL) { | 
 | 537 |                 fflush(ferr); | 
 | 538 |                 fclose(ferr); | 
 | 539 |             } | 
 | 540 |  | 
 | 541 |             return NO_ERROR; | 
| Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 542 |         } break; | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 543 |         default: { return BnIncidentManager::onTransact(code, data, reply, flags); } | 
 | 544 |     } | 
 | 545 | } | 
 | 546 |  | 
 | 547 | status_t IncidentService::command(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { | 
 | 548 |     const int argCount = args.size(); | 
 | 549 |  | 
 | 550 |     if (argCount >= 1) { | 
 | 551 |         if (!args[0].compare(String8("privacy"))) { | 
 | 552 |             return cmd_privacy(in, out, err, args); | 
 | 553 |         } | 
| Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 554 |         if (!args[0].compare(String8("throttler"))) { | 
 | 555 |             mThrottler->dump(out); | 
 | 556 |             return NO_ERROR; | 
 | 557 |         } | 
| Yi Jin | 908c02f | 2018-06-22 16:51:40 -0700 | [diff] [blame] | 558 |         if (!args[0].compare(String8("section"))) { | 
| Howard Ro | 8ff5315 | 2020-08-18 17:13:40 -0700 | [diff] [blame] | 559 |             if (argCount == 1) { | 
 | 560 |                 fprintf(out, "Not enough arguments for section\n"); | 
 | 561 |                 return NO_ERROR; | 
 | 562 |             } | 
| Yi Jin | 908c02f | 2018-06-22 16:51:40 -0700 | [diff] [blame] | 563 |             int id = atoi(args[1]); | 
 | 564 |             int idx = 0; | 
 | 565 |             while (SECTION_LIST[idx] != NULL) { | 
 | 566 |                 const Section* section = SECTION_LIST[idx]; | 
 | 567 |                 if (section->id == id) { | 
 | 568 |                     fprintf(out, "Section[%d] %s\n", id, section->name.string()); | 
 | 569 |                     break; | 
 | 570 |                 } | 
 | 571 |                 idx++; | 
 | 572 |             } | 
 | 573 |             return NO_ERROR; | 
 | 574 |         } | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 575 |     } | 
 | 576 |     return cmd_help(out); | 
 | 577 | } | 
 | 578 |  | 
 | 579 | status_t IncidentService::cmd_help(FILE* out) { | 
 | 580 |     fprintf(out, "usage: adb shell cmd incident privacy print <section_id>\n"); | 
 | 581 |     fprintf(out, "usage: adb shell cmd incident privacy parse <section_id> < proto.txt\n"); | 
| Yi Jin | 908c02f | 2018-06-22 16:51:40 -0700 | [diff] [blame] | 582 |     fprintf(out, "    Prints/parses for the section id.\n\n"); | 
 | 583 |     fprintf(out, "usage: adb shell cmd incident section <section_id>\n"); | 
 | 584 |     fprintf(out, "    Prints section id and its name.\n\n"); | 
| Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 585 |     fprintf(out, "usage: adb shell cmd incident throttler\n"); | 
 | 586 |     fprintf(out, "    Prints the current throttler state\n"); | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 587 |     return NO_ERROR; | 
 | 588 | } | 
 | 589 |  | 
 | 590 | static void printPrivacy(const Privacy* p, FILE* out, String8 indent) { | 
 | 591 |     if (p == NULL) return; | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 592 |     fprintf(out, "%sid:%d, type:%d, dest:%d\n", indent.string(), p->field_id, p->type, p->policy); | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 593 |     if (p->children == NULL) return; | 
 | 594 |     for (int i = 0; p->children[i] != NULL; i++) {  // NULL-terminated. | 
 | 595 |         printPrivacy(p->children[i], out, indent + "  "); | 
 | 596 |     } | 
 | 597 | } | 
 | 598 |  | 
 | 599 | status_t IncidentService::cmd_privacy(FILE* in, FILE* out, FILE* err, Vector<String8>& args) { | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 600 |     (void)in; | 
 | 601 |  | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 602 |     const int argCount = args.size(); | 
 | 603 |     if (argCount >= 3) { | 
 | 604 |         String8 opt = args[1]; | 
 | 605 |         int sectionId = atoi(args[2].string()); | 
 | 606 |  | 
 | 607 |         const Privacy* p = get_privacy_of_section(sectionId); | 
 | 608 |         if (p == NULL) { | 
 | 609 |             fprintf(err, "Can't find section id %d\n", sectionId); | 
 | 610 |             return NO_ERROR; | 
 | 611 |         } | 
 | 612 |         fprintf(err, "Get privacy for %d\n", sectionId); | 
 | 613 |         if (opt == "print") { | 
 | 614 |             printPrivacy(p, out, String8("")); | 
 | 615 |         } else if (opt == "parse") { | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 616 |             /* | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 617 |             FdBuffer buf; | 
| Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 618 |             status_t error = buf.read(fileno(in), 60000); | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 619 |             if (error != NO_ERROR) { | 
 | 620 |                 fprintf(err, "Error reading from stdin\n"); | 
 | 621 |                 return error; | 
 | 622 |             } | 
 | 623 |             fprintf(err, "Read %zu bytes\n", buf.size()); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 624 |             PrivacyFilter pBuf(p, buf.data()); | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 625 |  | 
 | 626 |             PrivacySpec spec = PrivacySpec::new_spec(argCount > 3 ? atoi(args[3]) : -1); | 
 | 627 |             error = pBuf.strip(spec); | 
 | 628 |             if (error != NO_ERROR) { | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 629 |                 fprintf(err, "Error strip pii fields with spec %d\n", spec.policy); | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 630 |                 return error; | 
 | 631 |             } | 
 | 632 |             return pBuf.flush(fileno(out)); | 
| Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 633 |             */ | 
 | 634 |             return -1; | 
| Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 635 |         } | 
 | 636 |     } else { | 
 | 637 |         return cmd_help(out); | 
 | 638 |     } | 
 | 639 |     return NO_ERROR; | 
 | 640 | } | 
| Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 641 |  | 
 | 642 | }  // namespace incidentd | 
 | 643 | }  // namespace os | 
 | 644 | }  // namespace android |