| 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 |  */ | 
 | 16 |  | 
 | 17 | #define LOG_TAG "incidentd" | 
 | 18 |  | 
 | 19 | #include "IncidentService.h" | 
 | 20 |  | 
 | 21 | #include "Reporter.h" | 
 | 22 |  | 
 | 23 | #include <binder/IPCThreadState.h> | 
 | 24 | #include <binder/IServiceManager.h> | 
 | 25 | #include <cutils/log.h> | 
 | 26 | #include <private/android_filesystem_config.h> | 
 | 27 | #include <utils/Looper.h> | 
 | 28 |  | 
 | 29 | #include <unistd.h> | 
 | 30 |  | 
 | 31 | using namespace android; | 
 | 32 |  | 
 | 33 | enum { | 
 | 34 |     WHAT_RUN_REPORT = 1, | 
 | 35 |     WHAT_SEND_BACKLOG_TO_DROPBOX = 2 | 
 | 36 | }; | 
 | 37 |  | 
 | 38 | //#define DEFAULT_BACKLOG_DELAY_NS (1000000000LL * 60 * 5) | 
 | 39 | #define DEFAULT_BACKLOG_DELAY_NS (1000000000LL) | 
 | 40 |  | 
 | 41 | // ================================================================================ | 
 | 42 | String16 const DUMP_PERMISSION("android.permission.DUMP"); | 
 | 43 | String16 const USAGE_STATS_PERMISSION("android.permission.PACKAGE_USAGE_STATS"); | 
 | 44 |  | 
 | 45 | static Status | 
 | 46 | checkIncidentPermissions() | 
 | 47 | { | 
 | 48 |     if (!checkCallingPermission(DUMP_PERMISSION)) { | 
 | 49 |         ALOGW("Calling pid %d and uid %d does not have permission: android.permission.DUMP", | 
 | 50 |                 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); | 
 | 51 |         return Status::fromExceptionCode(Status::EX_SECURITY, | 
 | 52 |                 "Calling process does not have permission: android.permission.DUMP"); | 
 | 53 |     } | 
 | 54 |     if (!checkCallingPermission(USAGE_STATS_PERMISSION)) { | 
 | 55 |         ALOGW("Calling pid %d and uid %d does not have permission: android.permission.USAGE_STATS", | 
 | 56 |                 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); | 
 | 57 |         return Status::fromExceptionCode(Status::EX_SECURITY, | 
 | 58 |                 "Calling process does not have permission: android.permission.USAGE_STATS"); | 
 | 59 |     } | 
 | 60 |     return Status::ok(); | 
 | 61 | } | 
 | 62 |  | 
 | 63 |  | 
 | 64 | // ================================================================================ | 
 | 65 | ReportRequestQueue::ReportRequestQueue() | 
 | 66 | { | 
 | 67 | } | 
 | 68 |  | 
 | 69 | ReportRequestQueue::~ReportRequestQueue() | 
 | 70 | { | 
 | 71 | } | 
 | 72 |  | 
 | 73 | void | 
 | 74 | ReportRequestQueue::addRequest(const sp<ReportRequest>& request)  | 
 | 75 | { | 
 | 76 |     unique_lock<mutex> lock(mLock); | 
 | 77 |     mQueue.push_back(request); | 
 | 78 | } | 
 | 79 |  | 
 | 80 | sp<ReportRequest> | 
 | 81 | ReportRequestQueue::getNextRequest() | 
 | 82 | { | 
 | 83 |     unique_lock<mutex> lock(mLock); | 
 | 84 |     if (mQueue.empty()) { | 
 | 85 |         return NULL; | 
 | 86 |     } else { | 
 | 87 |         sp<ReportRequest> front(mQueue.front()); | 
 | 88 |         mQueue.pop_front(); | 
 | 89 |         return front; | 
 | 90 |     } | 
 | 91 | } | 
 | 92 |  | 
 | 93 |  | 
 | 94 | // ================================================================================ | 
 | 95 | ReportHandler::ReportHandler(const sp<Looper>& handlerLooper, const sp<ReportRequestQueue>& queue) | 
 | 96 |     :mBacklogDelay(DEFAULT_BACKLOG_DELAY_NS), | 
 | 97 |      mHandlerLooper(handlerLooper), | 
 | 98 |      mQueue(queue) | 
 | 99 | { | 
 | 100 | } | 
 | 101 |  | 
 | 102 | ReportHandler::~ReportHandler() | 
 | 103 | { | 
 | 104 | } | 
 | 105 |  | 
 | 106 | void | 
 | 107 | ReportHandler::handleMessage(const Message& message) | 
 | 108 | { | 
 | 109 |     switch (message.what) { | 
 | 110 |         case WHAT_RUN_REPORT: | 
 | 111 |             run_report(); | 
 | 112 |             break; | 
 | 113 |         case WHAT_SEND_BACKLOG_TO_DROPBOX: | 
 | 114 |             send_backlog_to_dropbox(); | 
 | 115 |             break; | 
 | 116 |     } | 
 | 117 | } | 
 | 118 |  | 
 | 119 | void | 
 | 120 | ReportHandler::scheduleRunReport(const sp<ReportRequest>& request) | 
 | 121 | { | 
 | 122 |     mQueue->addRequest(request); | 
 | 123 |     mHandlerLooper->removeMessages(this, WHAT_RUN_REPORT); | 
 | 124 |     mHandlerLooper->sendMessage(this, Message(WHAT_RUN_REPORT)); | 
 | 125 | } | 
 | 126 |  | 
 | 127 | void | 
 | 128 | ReportHandler::scheduleSendBacklogToDropbox() | 
 | 129 | { | 
 | 130 |     unique_lock<mutex> lock(mLock); | 
 | 131 |     mBacklogDelay = DEFAULT_BACKLOG_DELAY_NS; | 
 | 132 |     schedule_send_backlog_to_dropbox_locked(); | 
 | 133 | } | 
 | 134 |  | 
 | 135 | void | 
 | 136 | ReportHandler::schedule_send_backlog_to_dropbox_locked() | 
 | 137 | { | 
 | 138 |     mHandlerLooper->removeMessages(this, WHAT_SEND_BACKLOG_TO_DROPBOX); | 
 | 139 |     mHandlerLooper->sendMessageDelayed(mBacklogDelay, this, | 
 | 140 |             Message(WHAT_SEND_BACKLOG_TO_DROPBOX)); | 
 | 141 | } | 
 | 142 |  | 
 | 143 | void | 
 | 144 | ReportHandler::run_report() | 
 | 145 | { | 
 | 146 |     sp<Reporter> reporter = new Reporter(); | 
 | 147 |  | 
 | 148 |     // Merge all of the requests into one that has all of the | 
 | 149 |     // requested fields. | 
 | 150 |     while (true) { | 
 | 151 |         sp<ReportRequest> request = mQueue->getNextRequest(); | 
 | 152 |         if (request == NULL) { | 
 | 153 |             break; | 
 | 154 |         } | 
 | 155 |         reporter->batch.add(request); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 156 |     } | 
 | 157 |  | 
 | 158 |     // Take the report, which might take a while. More requests might queue | 
 | 159 |     // up while we're doing this, and we'll handle them in their next batch. | 
 | 160 |     // TODO: We should further rate-limit the reports to no more than N per time-period. | 
 | 161 |     Reporter::run_report_status_t reportStatus = reporter->runReport(); | 
 | 162 |     if (reportStatus == Reporter::REPORT_NEEDS_DROPBOX) { | 
 | 163 |         unique_lock<mutex> lock(mLock); | 
 | 164 |         schedule_send_backlog_to_dropbox_locked(); | 
 | 165 |     } | 
 | 166 | } | 
 | 167 |  | 
 | 168 | void | 
 | 169 | ReportHandler::send_backlog_to_dropbox() | 
 | 170 | { | 
 | 171 |     if (Reporter::upload_backlog() == Reporter::REPORT_NEEDS_DROPBOX) { | 
 | 172 |         // There was a failure. Exponential backoff. | 
 | 173 |         unique_lock<mutex> lock(mLock); | 
 | 174 |         mBacklogDelay *= 2; | 
 | 175 |         ALOGI("Error sending to dropbox. Trying again in %lld minutes", | 
 | 176 |                 (mBacklogDelay / (1000000000LL * 60))); | 
 | 177 |         schedule_send_backlog_to_dropbox_locked(); | 
 | 178 |     } else { | 
 | 179 |         mBacklogDelay = DEFAULT_BACKLOG_DELAY_NS; | 
 | 180 |     } | 
 | 181 | } | 
 | 182 |  | 
 | 183 | // ================================================================================ | 
 | 184 | IncidentService::IncidentService(const sp<Looper>& handlerLooper) | 
 | 185 |     :mQueue(new ReportRequestQueue()) | 
 | 186 | { | 
 | 187 |     mHandler = new ReportHandler(handlerLooper, mQueue); | 
 | 188 | } | 
 | 189 |  | 
 | 190 | IncidentService::~IncidentService() | 
 | 191 | { | 
 | 192 | } | 
 | 193 |  | 
 | 194 | Status | 
 | 195 | IncidentService::reportIncident(const IncidentReportArgs& args) | 
 | 196 | { | 
 | 197 |     ALOGI("reportIncident"); | 
 | 198 |  | 
 | 199 |     Status status = checkIncidentPermissions(); | 
 | 200 |     if (!status.isOk()) { | 
 | 201 |         return status; | 
 | 202 |     } | 
 | 203 |  | 
 | 204 |     mHandler->scheduleRunReport(new ReportRequest(args, NULL, -1)); | 
 | 205 |  | 
 | 206 |     return Status::ok(); | 
 | 207 | } | 
 | 208 |  | 
 | 209 | Status | 
 | 210 | IncidentService::reportIncidentToStream(const IncidentReportArgs& args, | 
 | 211 |             const sp<IIncidentReportStatusListener>& listener, const unique_fd& stream) | 
 | 212 | { | 
 | 213 |     ALOGI("reportIncidentToStream"); | 
 | 214 |  | 
 | 215 |     Status status = checkIncidentPermissions(); | 
 | 216 |     if (!status.isOk()) { | 
 | 217 |         return status; | 
 | 218 |     } | 
 | 219 |  | 
 | 220 |     int fd = dup(stream.get()); | 
 | 221 |     if (fd < 0) { | 
 | 222 |         return Status::fromStatusT(-errno); | 
 | 223 |     } | 
 | 224 |  | 
 | 225 |     mHandler->scheduleRunReport(new ReportRequest(args, listener, fd)); | 
 | 226 |  | 
 | 227 |     return Status::ok(); | 
 | 228 | } | 
 | 229 |  | 
 | 230 | Status | 
 | 231 | IncidentService::systemRunning() | 
 | 232 | { | 
 | 233 |     if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) { | 
 | 234 |         return Status::fromExceptionCode(Status::EX_SECURITY, | 
 | 235 |                 "Only system uid can call systemRunning"); | 
 | 236 |     } | 
| Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 237 |  | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 238 |     // When system_server is up and running, schedule the dropbox task to run. | 
 | 239 |     mHandler->scheduleSendBacklogToDropbox(); | 
 | 240 |  | 
 | 241 |     return Status::ok(); | 
 | 242 | } | 
 | 243 |  |