blob: fc8a6db59e4bb60d5646c9e13cfd80a86730b28d [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 Jin0a3406f2017-06-22 19:23:11 -070040// ================================================================================
Joe Onorato1754d742016-11-21 17:51:35 -080041ReportRequest::ReportRequest(const IncidentReportArgs& a,
Yi Jinb592e3b2018-02-01 15:17:04 -080042 const sp<IIncidentReportStatusListener>& l, int f)
43 : args(a), listener(l), fd(f), err(NO_ERROR) {}
Joe Onorato1754d742016-11-21 17:51:35 -080044
Yi Jinb592e3b2018-02-01 15:17:04 -080045ReportRequest::~ReportRequest() {
Yi Jin22769e02017-10-16 14:42:50 -070046 if (fd >= 0) {
47 // clean up the opened file descriptor
48 close(fd);
49 }
Joe Onorato1754d742016-11-21 17:51:35 -080050}
51
Yi Jinb592e3b2018-02-01 15:17:04 -080052bool ReportRequest::ok() { return fd >= 0 && err == NO_ERROR; }
Yi Jinedfd5bb2017-09-06 17:09:11 -070053
Joe Onorato1754d742016-11-21 17:51:35 -080054// ================================================================================
55ReportRequestSet::ReportRequestSet()
Yi Jinb592e3b2018-02-01 15:17:04 -080056 : mRequests(), mSections(), mMainFd(-1), mMainDest(-1), mMetadata(), mSectionStats() {}
Joe Onorato1754d742016-11-21 17:51:35 -080057
Yi Jinb592e3b2018-02-01 15:17:04 -080058ReportRequestSet::~ReportRequestSet() {}
Joe Onorato1754d742016-11-21 17:51:35 -080059
Yi Jinadd11e92017-07-30 16:10:07 -070060// TODO: dedup on exact same args and fd, report the status back to listener!
Yi Jinb592e3b2018-02-01 15:17:04 -080061void ReportRequestSet::add(const sp<ReportRequest>& request) {
Joe Onorato1754d742016-11-21 17:51:35 -080062 mRequests.push_back(request);
Yi Jinadd11e92017-07-30 16:10:07 -070063 mSections.merge(request->args);
Yi Jin329130b2018-02-09 16:47:47 -080064 mMetadata.set_request_size(mMetadata.request_size() + 1);
Joe Onorato1754d742016-11-21 17:51:35 -080065}
66
Yi Jinb592e3b2018-02-01 15:17:04 -080067void ReportRequestSet::setMainFd(int fd) {
Joe Onorato1754d742016-11-21 17:51:35 -080068 mMainFd = fd;
Yi Jin329130b2018-02-09 16:47:47 -080069 mMetadata.set_use_dropbox(fd > 0);
Joe Onorato1754d742016-11-21 17:51:35 -080070}
71
Yi Jinb592e3b2018-02-01 15:17:04 -080072void ReportRequestSet::setMainDest(int dest) {
Yi Jin3ec5cc72018-01-26 13:42:43 -080073 mMainDest = dest;
Yi Jin329130b2018-02-09 16:47:47 -080074 PrivacySpec spec = PrivacySpec::new_spec(dest);
75 switch (spec.dest) {
76 case android::os::DEST_AUTOMATIC:
77 mMetadata.set_dest(IncidentMetadata_Destination_AUTOMATIC);
78 break;
79 case android::os::DEST_EXPLICIT:
80 mMetadata.set_dest(IncidentMetadata_Destination_EXPLICIT);
81 break;
82 case android::os::DEST_LOCAL:
83 mMetadata.set_dest(IncidentMetadata_Destination_LOCAL);
84 break;
85 }
Yi Jin3ec5cc72018-01-26 13:42:43 -080086}
87
Yi Jinb592e3b2018-02-01 15:17:04 -080088bool ReportRequestSet::containsSection(int id) { return mSections.containsSection(id); }
Joe Onorato1754d742016-11-21 17:51:35 -080089
Yi Jinb592e3b2018-02-01 15:17:04 -080090IncidentMetadata::SectionStats* ReportRequestSet::sectionStats(int id) {
Yi Jin329130b2018-02-09 16:47:47 -080091 if (mSectionStats.find(id) == mSectionStats.end()) {
Yi Jin86dce412018-03-07 11:36:57 -080092 IncidentMetadata::SectionStats stats;
93 stats.set_id(id);
Yi Jin329130b2018-02-09 16:47:47 -080094 mSectionStats[id] = stats;
95 }
Yi Jin86dce412018-03-07 11:36:57 -080096 return &mSectionStats[id];
Yi Jin329130b2018-02-09 16:47:47 -080097}
98
Joe Onorato1754d742016-11-21 17:51:35 -080099// ================================================================================
Yi Jinadd11e92017-07-30 16:10:07 -0700100Reporter::Reporter() : Reporter(INCIDENT_DIRECTORY) { isTest = false; };
101
Yi Jinb592e3b2018-02-01 15:17:04 -0800102Reporter::Reporter(const char* directory) : batch() {
Joe Onorato1754d742016-11-21 17:51:35 -0800103 char buf[100];
104
105 // TODO: Make the max size smaller for user builds.
106 mMaxSize = 100 * 1024 * 1024;
107 mMaxCount = 100;
108
Yi Jinadd11e92017-07-30 16:10:07 -0700109 // string ends up with '/' is a directory
110 String8 dir = String8(directory);
111 if (directory[dir.size() - 1] != '/') dir += "/";
112 mIncidentDirectory = dir.string();
113
Joe Onorato1754d742016-11-21 17:51:35 -0800114 // There can't be two at the same time because it's on one thread.
115 mStartTime = time(NULL);
Yi Jinadd11e92017-07-30 16:10:07 -0700116 strftime(buf, sizeof(buf), "incident-%Y%m%d-%H%M%S", localtime(&mStartTime));
117 mFilename = mIncidentDirectory + buf;
Joe Onorato1754d742016-11-21 17:51:35 -0800118}
119
Yi Jinb592e3b2018-02-01 15:17:04 -0800120Reporter::~Reporter() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800121
Yi Jin4e843102018-02-14 15:36:18 -0800122Reporter::run_report_status_t Reporter::runReport(size_t* reportByteSize) {
Joe Onorato1754d742016-11-21 17:51:35 -0800123 status_t err = NO_ERROR;
124 bool needMainFd = false;
125 int mainFd = -1;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800126 int mainDest = -1;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700127 HeaderSection headers;
Yi Jin329130b2018-02-09 16:47:47 -0800128 MetadataSection metadataSection;
Joe Onorato1754d742016-11-21 17:51:35 -0800129
130 // See if we need the main file
Yi Jinb592e3b2018-02-01 15:17:04 -0800131 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800132 if ((*it)->fd < 0 && mainFd < 0) {
133 needMainFd = true;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800134 mainDest = (*it)->args.dest();
Joe Onorato1754d742016-11-21 17:51:35 -0800135 break;
136 }
137 }
138 if (needMainFd) {
139 // Create the directory
Yi Jinadd11e92017-07-30 16:10:07 -0700140 if (!isTest) err = create_directory(mIncidentDirectory);
Joe Onorato1754d742016-11-21 17:51:35 -0800141 if (err != NO_ERROR) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700142 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800143 }
144
145 // If there are too many files in the directory (for whatever reason),
146 // delete the oldest ones until it's under the limit. Doing this first
147 // does mean that we can go over, so the max size is not a hard limit.
Yi Jinadd11e92017-07-30 16:10:07 -0700148 if (!isTest) clean_directory(mIncidentDirectory, mMaxSize, mMaxCount);
Joe Onorato1754d742016-11-21 17:51:35 -0800149
150 // Open the file.
151 err = create_file(&mainFd);
152 if (err != NO_ERROR) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700153 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800154 }
155
156 // Add to the set
157 batch.setMainFd(mainFd);
Yi Jin3ec5cc72018-01-26 13:42:43 -0800158 batch.setMainDest(mainDest);
Joe Onorato1754d742016-11-21 17:51:35 -0800159 }
160
161 // Tell everyone that we're starting.
Yi Jinb592e3b2018-02-01 15:17:04 -0800162 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800163 if ((*it)->listener != NULL) {
164 (*it)->listener->onReportStarted();
165 }
166 }
167
168 // Write the incident headers
Yi Jinedfd5bb2017-09-06 17:09:11 -0700169 headers.Execute(&batch);
Joe Onorato1754d742016-11-21 17:51:35 -0800170
171 // For each of the report fields, see if we need it, and if so, execute the command
172 // and report to those that care that we're doing it.
Yi Jinb592e3b2018-02-01 15:17:04 -0800173 for (const Section** section = SECTION_LIST; *section; section++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800174 const int id = (*section)->id;
Yi Jinadd11e92017-07-30 16:10:07 -0700175 if (this->batch.containsSection(id)) {
Yi Jinf32af482017-08-11 15:00:49 -0700176 ALOGD("Taking incident report section %d '%s'", id, (*section)->name.string());
Yi Jinb592e3b2018-02-01 15:17:04 -0800177 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800178 if ((*it)->listener != NULL && (*it)->args.containsSection(id)) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800179 (*it)->listener->onReportSectionStatus(
180 id, IIncidentReportStatusListener::STATUS_STARTING);
Joe Onorato1754d742016-11-21 17:51:35 -0800181 }
182 }
183
184 // Execute - go get the data and write it into the file descriptors.
Yi Jin86dce412018-03-07 11:36:57 -0800185 IncidentMetadata::SectionStats* stats = batch.sectionStats(id);
Yi Jin329130b2018-02-09 16:47:47 -0800186 int64_t startTime = uptimeMillis();
Joe Onorato1754d742016-11-21 17:51:35 -0800187 err = (*section)->Execute(&batch);
Yi Jin329130b2018-02-09 16:47:47 -0800188 int64_t endTime = uptimeMillis();
Yi Jin329130b2018-02-09 16:47:47 -0800189 stats->set_success(err == NO_ERROR);
190 stats->set_exec_duration_ms(endTime - startTime);
Joe Onorato1754d742016-11-21 17:51:35 -0800191 if (err != NO_ERROR) {
Yi Jina5c5e8a2017-09-27 18:24:58 -0700192 ALOGW("Incident section %s (%d) failed: %s. Stopping report.",
Yi Jinb592e3b2018-02-01 15:17:04 -0800193 (*section)->name.string(), id, strerror(-err));
Yi Jinedfd5bb2017-09-06 17:09:11 -0700194 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800195 }
Yi Jin4e843102018-02-14 15:36:18 -0800196 (*reportByteSize) += stats->report_size_bytes();
Joe Onorato1754d742016-11-21 17:51:35 -0800197
Yi Jinb592e3b2018-02-01 15:17:04 -0800198 // Notify listener of starting
199 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800200 if ((*it)->listener != NULL && (*it)->args.containsSection(id)) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800201 (*it)->listener->onReportSectionStatus(
202 id, IIncidentReportStatusListener::STATUS_FINISHED);
Joe Onorato1754d742016-11-21 17:51:35 -0800203 }
204 }
Yi Jinf32af482017-08-11 15:00:49 -0700205 ALOGD("Finish incident report section %d '%s'", id, (*section)->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800206 }
207 }
208
Yi Jinedfd5bb2017-09-06 17:09:11 -0700209DONE:
Yi Jin329130b2018-02-09 16:47:47 -0800210 // Reports the metdadata when taking the incident report.
211 if (!isTest) metadataSection.Execute(&batch);
212
Joe Onorato1754d742016-11-21 17:51:35 -0800213 // Close the file.
214 if (mainFd >= 0) {
215 close(mainFd);
216 }
217
218 // Tell everyone that we're done.
Yi Jinb592e3b2018-02-01 15:17:04 -0800219 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800220 if ((*it)->listener != NULL) {
221 if (err == NO_ERROR) {
222 (*it)->listener->onReportFinished();
223 } else {
224 (*it)->listener->onReportFailed();
225 }
226 }
227 }
228
229 // Put the report into dropbox.
230 if (needMainFd && err == NO_ERROR) {
231 sp<DropBoxManager> dropbox = new DropBoxManager();
232 Status status = dropbox->addFile(String16("incident"), mFilename, 0);
233 ALOGD("Incident report done. dropbox status=%s\n", status.toString8().string());
234 if (!status.isOk()) {
235 return REPORT_NEEDS_DROPBOX;
236 }
237
238 // If the status was ok, delete the file. If not, leave it around until the next
239 // boot or the next checkin. If the directory gets too big older files will
240 // be rotated out.
Yi Jinb592e3b2018-02-01 15:17:04 -0800241 if (!isTest) unlink(mFilename.c_str());
Joe Onorato1754d742016-11-21 17:51:35 -0800242 }
243
244 return REPORT_FINISHED;
245}
246
247/**
248 * Create our output file and set the access permissions to -rw-rw----
249 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800250status_t Reporter::create_file(int* fd) {
Joe Onorato1754d742016-11-21 17:51:35 -0800251 const char* filename = mFilename.c_str();
252
Yi Jinadd11e92017-07-30 16:10:07 -0700253 *fd = open(filename, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0660);
Joe Onorato1754d742016-11-21 17:51:35 -0800254 if (*fd < 0) {
255 ALOGE("Couldn't open incident file: %s (%s)", filename, strerror(errno));
256 return -errno;
257 }
258
259 // Override umask. Not super critical. If it fails go on with life.
260 chmod(filename, 0660);
261
Yi Jin4bab3a12018-01-10 16:50:59 -0800262 if (chown(filename, AID_INCIDENTD, AID_INCIDENTD)) {
Joe Onorato1754d742016-11-21 17:51:35 -0800263 ALOGE("Unable to change ownership of incident file %s: %s\n", filename, strerror(errno));
264 status_t err = -errno;
265 unlink(mFilename.c_str());
266 return err;
267 }
268
269 return NO_ERROR;
270}
271
Yi Jinb592e3b2018-02-01 15:17:04 -0800272Reporter::run_report_status_t Reporter::upload_backlog() {
Joe Onorato1754d742016-11-21 17:51:35 -0800273 DIR* dir;
274 struct dirent* entry;
275 struct stat st;
Yi Jinadd11e92017-07-30 16:10:07 -0700276 status_t err;
Joe Onorato1754d742016-11-21 17:51:35 -0800277
Yi Jinf32af482017-08-11 15:00:49 -0700278 ALOGD("Start uploading backlogs in %s", INCIDENT_DIRECTORY);
Yi Jinadd11e92017-07-30 16:10:07 -0700279 if ((err = create_directory(INCIDENT_DIRECTORY)) != NO_ERROR) {
280 ALOGE("directory doesn't exist: %s", strerror(-err));
281 return REPORT_FINISHED;
282 }
283
284 if ((dir = opendir(INCIDENT_DIRECTORY)) == NULL) {
285 ALOGE("Couldn't open incident directory: %s", INCIDENT_DIRECTORY);
Joe Onorato1754d742016-11-21 17:51:35 -0800286 return REPORT_NEEDS_DROPBOX;
287 }
288
Joe Onorato1754d742016-11-21 17:51:35 -0800289 sp<DropBoxManager> dropbox = new DropBoxManager();
290
291 // Enumerate, count and add up size
Yi Jinf32af482017-08-11 15:00:49 -0700292 int count = 0;
Joe Onorato1754d742016-11-21 17:51:35 -0800293 while ((entry = readdir(dir)) != NULL) {
294 if (entry->d_name[0] == '.') {
295 continue;
296 }
Yi Jinadd11e92017-07-30 16:10:07 -0700297 String8 filename = String8(INCIDENT_DIRECTORY) + entry->d_name;
Joe Onorato1754d742016-11-21 17:51:35 -0800298 if (stat(filename.string(), &st) != 0) {
299 ALOGE("Unable to stat file %s", filename.string());
300 continue;
301 }
302 if (!S_ISREG(st.st_mode)) {
303 continue;
304 }
305
306 Status status = dropbox->addFile(String16("incident"), filename.string(), 0);
307 ALOGD("Incident report done. dropbox status=%s\n", status.toString8().string());
308 if (!status.isOk()) {
309 return REPORT_NEEDS_DROPBOX;
310 }
311
312 // If the status was ok, delete the file. If not, leave it around until the next
313 // boot or the next checkin. If the directory gets too big older files will
314 // be rotated out.
315 unlink(filename.string());
Yi Jinf32af482017-08-11 15:00:49 -0700316 count++;
Joe Onorato1754d742016-11-21 17:51:35 -0800317 }
Yi Jinf32af482017-08-11 15:00:49 -0700318 ALOGD("Successfully uploaded %d files to Dropbox.", count);
Joe Onorato1754d742016-11-21 17:51:35 -0800319 closedir(dir);
320
321 return REPORT_FINISHED;
322}