blob: 297a0711f6867054e1c85f5ef15da2a4eb82ea0f [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 "Reporter.h"
Joe Onorato1754d742016-11-21 17:51:35 -080020
Yi Jin329130b2018-02-09 16:47:47 -080021#include "Privacy.h"
Joe Onorato1754d742016-11-21 17:51:35 -080022#include "report_directory.h"
23#include "section_list.h"
24
Joe Onorato1754d742016-11-21 17:51:35 -080025#include <android/os/DropBoxManager.h>
Yi Jin0a3406f2017-06-22 19:23:11 -070026#include <private/android_filesystem_config.h>
Joe Onorato1754d742016-11-21 17:51:35 -080027#include <utils/SystemClock.h>
28
Joe Onorato1754d742016-11-21 17:51:35 -080029#include <dirent.h>
Joe Onorato1754d742016-11-21 17:51:35 -080030#include <errno.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080031#include <fcntl.h>
32#include <sys/stat.h>
33#include <sys/types.h>
Joe Onorato1754d742016-11-21 17:51:35 -080034
35/**
36 * The directory where the incident reports are stored.
37 */
Yi Jinadd11e92017-07-30 16:10:07 -070038static const char* INCIDENT_DIRECTORY = "/data/misc/incidents/";
Joe Onorato1754d742016-11-21 17:51:35 -080039
Yi Jin6cacbcb2018-03-30 14:04:52 -070040namespace android {
41namespace os {
42namespace incidentd {
43
Yi Jin0a3406f2017-06-22 19:23:11 -070044// ================================================================================
Joe Onorato1754d742016-11-21 17:51:35 -080045ReportRequest::ReportRequest(const IncidentReportArgs& a,
Yi Jinb592e3b2018-02-01 15:17:04 -080046 const sp<IIncidentReportStatusListener>& l, int f)
47 : args(a), listener(l), fd(f), err(NO_ERROR) {}
Joe Onorato1754d742016-11-21 17:51:35 -080048
Yi Jinb592e3b2018-02-01 15:17:04 -080049ReportRequest::~ReportRequest() {
Yi Jin22769e02017-10-16 14:42:50 -070050 if (fd >= 0) {
51 // clean up the opened file descriptor
52 close(fd);
53 }
Joe Onorato1754d742016-11-21 17:51:35 -080054}
55
Yi Jinb592e3b2018-02-01 15:17:04 -080056bool ReportRequest::ok() { return fd >= 0 && err == NO_ERROR; }
Yi Jinedfd5bb2017-09-06 17:09:11 -070057
Joe Onorato1754d742016-11-21 17:51:35 -080058// ================================================================================
59ReportRequestSet::ReportRequestSet()
Yi Jinb592e3b2018-02-01 15:17:04 -080060 : mRequests(), mSections(), mMainFd(-1), mMainDest(-1), mMetadata(), mSectionStats() {}
Joe Onorato1754d742016-11-21 17:51:35 -080061
Yi Jinb592e3b2018-02-01 15:17:04 -080062ReportRequestSet::~ReportRequestSet() {}
Joe Onorato1754d742016-11-21 17:51:35 -080063
Yi Jinadd11e92017-07-30 16:10:07 -070064// TODO: dedup on exact same args and fd, report the status back to listener!
Yi Jinb592e3b2018-02-01 15:17:04 -080065void ReportRequestSet::add(const sp<ReportRequest>& request) {
Joe Onorato1754d742016-11-21 17:51:35 -080066 mRequests.push_back(request);
Yi Jinadd11e92017-07-30 16:10:07 -070067 mSections.merge(request->args);
Yi Jin329130b2018-02-09 16:47:47 -080068 mMetadata.set_request_size(mMetadata.request_size() + 1);
Joe Onorato1754d742016-11-21 17:51:35 -080069}
70
Yi Jinb592e3b2018-02-01 15:17:04 -080071void ReportRequestSet::setMainFd(int fd) {
Joe Onorato1754d742016-11-21 17:51:35 -080072 mMainFd = fd;
Yi Jin329130b2018-02-09 16:47:47 -080073 mMetadata.set_use_dropbox(fd > 0);
Joe Onorato1754d742016-11-21 17:51:35 -080074}
75
Yi Jinb592e3b2018-02-01 15:17:04 -080076void ReportRequestSet::setMainDest(int dest) {
Yi Jin3ec5cc72018-01-26 13:42:43 -080077 mMainDest = dest;
Yi Jin329130b2018-02-09 16:47:47 -080078 PrivacySpec spec = PrivacySpec::new_spec(dest);
79 switch (spec.dest) {
80 case android::os::DEST_AUTOMATIC:
81 mMetadata.set_dest(IncidentMetadata_Destination_AUTOMATIC);
82 break;
83 case android::os::DEST_EXPLICIT:
84 mMetadata.set_dest(IncidentMetadata_Destination_EXPLICIT);
85 break;
86 case android::os::DEST_LOCAL:
87 mMetadata.set_dest(IncidentMetadata_Destination_LOCAL);
88 break;
89 }
Yi Jin3ec5cc72018-01-26 13:42:43 -080090}
91
Yi Jinb592e3b2018-02-01 15:17:04 -080092bool ReportRequestSet::containsSection(int id) { return mSections.containsSection(id); }
Joe Onorato1754d742016-11-21 17:51:35 -080093
Yi Jinb592e3b2018-02-01 15:17:04 -080094IncidentMetadata::SectionStats* ReportRequestSet::sectionStats(int id) {
Yi Jin329130b2018-02-09 16:47:47 -080095 if (mSectionStats.find(id) == mSectionStats.end()) {
Yi Jin86dce412018-03-07 11:36:57 -080096 IncidentMetadata::SectionStats stats;
97 stats.set_id(id);
Yi Jin329130b2018-02-09 16:47:47 -080098 mSectionStats[id] = stats;
99 }
Yi Jin86dce412018-03-07 11:36:57 -0800100 return &mSectionStats[id];
Yi Jin329130b2018-02-09 16:47:47 -0800101}
102
Joe Onorato1754d742016-11-21 17:51:35 -0800103// ================================================================================
Yi Jinadd11e92017-07-30 16:10:07 -0700104Reporter::Reporter() : Reporter(INCIDENT_DIRECTORY) { isTest = false; };
105
Yi Jinb592e3b2018-02-01 15:17:04 -0800106Reporter::Reporter(const char* directory) : batch() {
Joe Onorato1754d742016-11-21 17:51:35 -0800107 char buf[100];
108
Yi Jin91d43302018-04-12 11:06:57 -0700109 mMaxSize = 30 * 1024 * 1024; // incident reports can take up to 30MB on disk
Joe Onorato1754d742016-11-21 17:51:35 -0800110 mMaxCount = 100;
111
Yi Jinadd11e92017-07-30 16:10:07 -0700112 // string ends up with '/' is a directory
113 String8 dir = String8(directory);
114 if (directory[dir.size() - 1] != '/') dir += "/";
115 mIncidentDirectory = dir.string();
116
Joe Onorato1754d742016-11-21 17:51:35 -0800117 // There can't be two at the same time because it's on one thread.
118 mStartTime = time(NULL);
Yi Jinadd11e92017-07-30 16:10:07 -0700119 strftime(buf, sizeof(buf), "incident-%Y%m%d-%H%M%S", localtime(&mStartTime));
120 mFilename = mIncidentDirectory + buf;
Joe Onorato1754d742016-11-21 17:51:35 -0800121}
122
Yi Jinb592e3b2018-02-01 15:17:04 -0800123Reporter::~Reporter() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800124
Yi Jin4e843102018-02-14 15:36:18 -0800125Reporter::run_report_status_t Reporter::runReport(size_t* reportByteSize) {
Joe Onorato1754d742016-11-21 17:51:35 -0800126 status_t err = NO_ERROR;
127 bool needMainFd = false;
128 int mainFd = -1;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800129 int mainDest = -1;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700130 HeaderSection headers;
Yi Jin329130b2018-02-09 16:47:47 -0800131 MetadataSection metadataSection;
Joe Onorato1754d742016-11-21 17:51:35 -0800132
133 // See if we need the main file
Yi Jinb592e3b2018-02-01 15:17:04 -0800134 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800135 if ((*it)->fd < 0 && mainFd < 0) {
136 needMainFd = true;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800137 mainDest = (*it)->args.dest();
Joe Onorato1754d742016-11-21 17:51:35 -0800138 break;
139 }
140 }
141 if (needMainFd) {
142 // Create the directory
Yi Jinadd11e92017-07-30 16:10:07 -0700143 if (!isTest) err = create_directory(mIncidentDirectory);
Joe Onorato1754d742016-11-21 17:51:35 -0800144 if (err != NO_ERROR) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700145 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800146 }
147
148 // If there are too many files in the directory (for whatever reason),
149 // delete the oldest ones until it's under the limit. Doing this first
150 // does mean that we can go over, so the max size is not a hard limit.
Yi Jinadd11e92017-07-30 16:10:07 -0700151 if (!isTest) clean_directory(mIncidentDirectory, mMaxSize, mMaxCount);
Joe Onorato1754d742016-11-21 17:51:35 -0800152
153 // Open the file.
154 err = create_file(&mainFd);
155 if (err != NO_ERROR) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700156 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800157 }
158
159 // Add to the set
160 batch.setMainFd(mainFd);
Yi Jin3ec5cc72018-01-26 13:42:43 -0800161 batch.setMainDest(mainDest);
Joe Onorato1754d742016-11-21 17:51:35 -0800162 }
163
164 // Tell everyone that we're starting.
Yi Jinb592e3b2018-02-01 15:17:04 -0800165 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800166 if ((*it)->listener != NULL) {
167 (*it)->listener->onReportStarted();
168 }
169 }
170
171 // Write the incident headers
Yi Jinedfd5bb2017-09-06 17:09:11 -0700172 headers.Execute(&batch);
Joe Onorato1754d742016-11-21 17:51:35 -0800173
174 // For each of the report fields, see if we need it, and if so, execute the command
175 // and report to those that care that we're doing it.
Yi Jinb592e3b2018-02-01 15:17:04 -0800176 for (const Section** section = SECTION_LIST; *section; section++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800177 const int id = (*section)->id;
Yi Jinadd11e92017-07-30 16:10:07 -0700178 if (this->batch.containsSection(id)) {
Yi Jinf32af482017-08-11 15:00:49 -0700179 ALOGD("Taking incident report section %d '%s'", id, (*section)->name.string());
Yi Jinb592e3b2018-02-01 15:17:04 -0800180 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800181 if ((*it)->listener != NULL && (*it)->args.containsSection(id)) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800182 (*it)->listener->onReportSectionStatus(
183 id, IIncidentReportStatusListener::STATUS_STARTING);
Joe Onorato1754d742016-11-21 17:51:35 -0800184 }
185 }
186
187 // Execute - go get the data and write it into the file descriptors.
Yi Jin86dce412018-03-07 11:36:57 -0800188 IncidentMetadata::SectionStats* stats = batch.sectionStats(id);
Yi Jin329130b2018-02-09 16:47:47 -0800189 int64_t startTime = uptimeMillis();
Joe Onorato1754d742016-11-21 17:51:35 -0800190 err = (*section)->Execute(&batch);
Yi Jin329130b2018-02-09 16:47:47 -0800191 int64_t endTime = uptimeMillis();
Yi Jin329130b2018-02-09 16:47:47 -0800192 stats->set_success(err == NO_ERROR);
193 stats->set_exec_duration_ms(endTime - startTime);
Joe Onorato1754d742016-11-21 17:51:35 -0800194 if (err != NO_ERROR) {
Yi Jina5c5e8a2017-09-27 18:24:58 -0700195 ALOGW("Incident section %s (%d) failed: %s. Stopping report.",
Yi Jinb592e3b2018-02-01 15:17:04 -0800196 (*section)->name.string(), id, strerror(-err));
Yi Jinedfd5bb2017-09-06 17:09:11 -0700197 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800198 }
Yi Jin4e843102018-02-14 15:36:18 -0800199 (*reportByteSize) += stats->report_size_bytes();
Joe Onorato1754d742016-11-21 17:51:35 -0800200
Yi Jinb592e3b2018-02-01 15:17:04 -0800201 // Notify listener of starting
202 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800203 if ((*it)->listener != NULL && (*it)->args.containsSection(id)) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800204 (*it)->listener->onReportSectionStatus(
205 id, IIncidentReportStatusListener::STATUS_FINISHED);
Joe Onorato1754d742016-11-21 17:51:35 -0800206 }
207 }
Yi Jinf32af482017-08-11 15:00:49 -0700208 ALOGD("Finish incident report section %d '%s'", id, (*section)->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800209 }
210 }
211
Yi Jinedfd5bb2017-09-06 17:09:11 -0700212DONE:
Yi Jin329130b2018-02-09 16:47:47 -0800213 // Reports the metdadata when taking the incident report.
214 if (!isTest) metadataSection.Execute(&batch);
215
Joe Onorato1754d742016-11-21 17:51:35 -0800216 // Close the file.
217 if (mainFd >= 0) {
218 close(mainFd);
219 }
220
221 // Tell everyone that we're done.
Yi Jinb592e3b2018-02-01 15:17:04 -0800222 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800223 if ((*it)->listener != NULL) {
224 if (err == NO_ERROR) {
225 (*it)->listener->onReportFinished();
226 } else {
227 (*it)->listener->onReportFailed();
228 }
229 }
230 }
231
232 // Put the report into dropbox.
233 if (needMainFd && err == NO_ERROR) {
234 sp<DropBoxManager> dropbox = new DropBoxManager();
235 Status status = dropbox->addFile(String16("incident"), mFilename, 0);
236 ALOGD("Incident report done. dropbox status=%s\n", status.toString8().string());
237 if (!status.isOk()) {
238 return REPORT_NEEDS_DROPBOX;
239 }
240
241 // If the status was ok, delete the file. If not, leave it around until the next
242 // boot or the next checkin. If the directory gets too big older files will
243 // be rotated out.
Yi Jinb592e3b2018-02-01 15:17:04 -0800244 if (!isTest) unlink(mFilename.c_str());
Joe Onorato1754d742016-11-21 17:51:35 -0800245 }
246
247 return REPORT_FINISHED;
248}
249
250/**
251 * Create our output file and set the access permissions to -rw-rw----
252 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800253status_t Reporter::create_file(int* fd) {
Joe Onorato1754d742016-11-21 17:51:35 -0800254 const char* filename = mFilename.c_str();
255
Yi Jinadd11e92017-07-30 16:10:07 -0700256 *fd = open(filename, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0660);
Joe Onorato1754d742016-11-21 17:51:35 -0800257 if (*fd < 0) {
258 ALOGE("Couldn't open incident file: %s (%s)", filename, strerror(errno));
259 return -errno;
260 }
261
262 // Override umask. Not super critical. If it fails go on with life.
263 chmod(filename, 0660);
264
Yi Jin4bab3a12018-01-10 16:50:59 -0800265 if (chown(filename, AID_INCIDENTD, AID_INCIDENTD)) {
Joe Onorato1754d742016-11-21 17:51:35 -0800266 ALOGE("Unable to change ownership of incident file %s: %s\n", filename, strerror(errno));
267 status_t err = -errno;
268 unlink(mFilename.c_str());
269 return err;
270 }
271
272 return NO_ERROR;
273}
274
Yi Jinb592e3b2018-02-01 15:17:04 -0800275Reporter::run_report_status_t Reporter::upload_backlog() {
Joe Onorato1754d742016-11-21 17:51:35 -0800276 DIR* dir;
277 struct dirent* entry;
278 struct stat st;
Yi Jinadd11e92017-07-30 16:10:07 -0700279 status_t err;
Joe Onorato1754d742016-11-21 17:51:35 -0800280
Yi Jinf32af482017-08-11 15:00:49 -0700281 ALOGD("Start uploading backlogs in %s", INCIDENT_DIRECTORY);
Yi Jinadd11e92017-07-30 16:10:07 -0700282 if ((err = create_directory(INCIDENT_DIRECTORY)) != NO_ERROR) {
283 ALOGE("directory doesn't exist: %s", strerror(-err));
284 return REPORT_FINISHED;
285 }
286
287 if ((dir = opendir(INCIDENT_DIRECTORY)) == NULL) {
288 ALOGE("Couldn't open incident directory: %s", INCIDENT_DIRECTORY);
Joe Onorato1754d742016-11-21 17:51:35 -0800289 return REPORT_NEEDS_DROPBOX;
290 }
291
Joe Onorato1754d742016-11-21 17:51:35 -0800292 sp<DropBoxManager> dropbox = new DropBoxManager();
293
294 // Enumerate, count and add up size
Yi Jinf32af482017-08-11 15:00:49 -0700295 int count = 0;
Joe Onorato1754d742016-11-21 17:51:35 -0800296 while ((entry = readdir(dir)) != NULL) {
297 if (entry->d_name[0] == '.') {
298 continue;
299 }
Yi Jinadd11e92017-07-30 16:10:07 -0700300 String8 filename = String8(INCIDENT_DIRECTORY) + entry->d_name;
Joe Onorato1754d742016-11-21 17:51:35 -0800301 if (stat(filename.string(), &st) != 0) {
302 ALOGE("Unable to stat file %s", filename.string());
303 continue;
304 }
305 if (!S_ISREG(st.st_mode)) {
306 continue;
307 }
308
309 Status status = dropbox->addFile(String16("incident"), filename.string(), 0);
310 ALOGD("Incident report done. dropbox status=%s\n", status.toString8().string());
311 if (!status.isOk()) {
312 return REPORT_NEEDS_DROPBOX;
313 }
314
315 // If the status was ok, delete the file. If not, leave it around until the next
316 // boot or the next checkin. If the directory gets too big older files will
317 // be rotated out.
318 unlink(filename.string());
Yi Jinf32af482017-08-11 15:00:49 -0700319 count++;
Joe Onorato1754d742016-11-21 17:51:35 -0800320 }
Yi Jinf32af482017-08-11 15:00:49 -0700321 ALOGD("Successfully uploaded %d files to Dropbox.", count);
Joe Onorato1754d742016-11-21 17:51:35 -0800322 closedir(dir);
323
324 return REPORT_FINISHED;
325}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700326
327} // namespace incidentd
328} // namespace os
329} // namespace android