blob: 103004da36571a29cc3e22dad3acfe64bc72f225 [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
109 // TODO: Make the max size smaller for user builds.
110 mMaxSize = 100 * 1024 * 1024;
111 mMaxCount = 100;
112
Yi Jinadd11e92017-07-30 16:10:07 -0700113 // string ends up with '/' is a directory
114 String8 dir = String8(directory);
115 if (directory[dir.size() - 1] != '/') dir += "/";
116 mIncidentDirectory = dir.string();
117
Joe Onorato1754d742016-11-21 17:51:35 -0800118 // There can't be two at the same time because it's on one thread.
119 mStartTime = time(NULL);
Yi Jinadd11e92017-07-30 16:10:07 -0700120 strftime(buf, sizeof(buf), "incident-%Y%m%d-%H%M%S", localtime(&mStartTime));
121 mFilename = mIncidentDirectory + buf;
Joe Onorato1754d742016-11-21 17:51:35 -0800122}
123
Yi Jinb592e3b2018-02-01 15:17:04 -0800124Reporter::~Reporter() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800125
Yi Jin4e843102018-02-14 15:36:18 -0800126Reporter::run_report_status_t Reporter::runReport(size_t* reportByteSize) {
Joe Onorato1754d742016-11-21 17:51:35 -0800127 status_t err = NO_ERROR;
128 bool needMainFd = false;
129 int mainFd = -1;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800130 int mainDest = -1;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700131 HeaderSection headers;
Yi Jin329130b2018-02-09 16:47:47 -0800132 MetadataSection metadataSection;
Joe Onorato1754d742016-11-21 17:51:35 -0800133
134 // See if we need the main file
Yi Jinb592e3b2018-02-01 15:17:04 -0800135 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800136 if ((*it)->fd < 0 && mainFd < 0) {
137 needMainFd = true;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800138 mainDest = (*it)->args.dest();
Joe Onorato1754d742016-11-21 17:51:35 -0800139 break;
140 }
141 }
142 if (needMainFd) {
143 // Create the directory
Yi Jinadd11e92017-07-30 16:10:07 -0700144 if (!isTest) err = create_directory(mIncidentDirectory);
Joe Onorato1754d742016-11-21 17:51:35 -0800145 if (err != NO_ERROR) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700146 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800147 }
148
149 // If there are too many files in the directory (for whatever reason),
150 // delete the oldest ones until it's under the limit. Doing this first
151 // does mean that we can go over, so the max size is not a hard limit.
Yi Jinadd11e92017-07-30 16:10:07 -0700152 if (!isTest) clean_directory(mIncidentDirectory, mMaxSize, mMaxCount);
Joe Onorato1754d742016-11-21 17:51:35 -0800153
154 // Open the file.
155 err = create_file(&mainFd);
156 if (err != NO_ERROR) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700157 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800158 }
159
160 // Add to the set
161 batch.setMainFd(mainFd);
Yi Jin3ec5cc72018-01-26 13:42:43 -0800162 batch.setMainDest(mainDest);
Joe Onorato1754d742016-11-21 17:51:35 -0800163 }
164
165 // Tell everyone that we're starting.
Yi Jinb592e3b2018-02-01 15:17:04 -0800166 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800167 if ((*it)->listener != NULL) {
168 (*it)->listener->onReportStarted();
169 }
170 }
171
172 // Write the incident headers
Yi Jinedfd5bb2017-09-06 17:09:11 -0700173 headers.Execute(&batch);
Joe Onorato1754d742016-11-21 17:51:35 -0800174
175 // For each of the report fields, see if we need it, and if so, execute the command
176 // and report to those that care that we're doing it.
Yi Jinb592e3b2018-02-01 15:17:04 -0800177 for (const Section** section = SECTION_LIST; *section; section++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800178 const int id = (*section)->id;
Yi Jinadd11e92017-07-30 16:10:07 -0700179 if (this->batch.containsSection(id)) {
Yi Jinf32af482017-08-11 15:00:49 -0700180 ALOGD("Taking incident report section %d '%s'", id, (*section)->name.string());
Yi Jinb592e3b2018-02-01 15:17:04 -0800181 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800182 if ((*it)->listener != NULL && (*it)->args.containsSection(id)) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800183 (*it)->listener->onReportSectionStatus(
184 id, IIncidentReportStatusListener::STATUS_STARTING);
Joe Onorato1754d742016-11-21 17:51:35 -0800185 }
186 }
187
188 // Execute - go get the data and write it into the file descriptors.
Yi Jin86dce412018-03-07 11:36:57 -0800189 IncidentMetadata::SectionStats* stats = batch.sectionStats(id);
Yi Jin329130b2018-02-09 16:47:47 -0800190 int64_t startTime = uptimeMillis();
Joe Onorato1754d742016-11-21 17:51:35 -0800191 err = (*section)->Execute(&batch);
Yi Jin329130b2018-02-09 16:47:47 -0800192 int64_t endTime = uptimeMillis();
Yi Jin329130b2018-02-09 16:47:47 -0800193 stats->set_success(err == NO_ERROR);
194 stats->set_exec_duration_ms(endTime - startTime);
Joe Onorato1754d742016-11-21 17:51:35 -0800195 if (err != NO_ERROR) {
Yi Jina5c5e8a2017-09-27 18:24:58 -0700196 ALOGW("Incident section %s (%d) failed: %s. Stopping report.",
Yi Jinb592e3b2018-02-01 15:17:04 -0800197 (*section)->name.string(), id, strerror(-err));
Yi Jinedfd5bb2017-09-06 17:09:11 -0700198 goto DONE;
Joe Onorato1754d742016-11-21 17:51:35 -0800199 }
Yi Jin4e843102018-02-14 15:36:18 -0800200 (*reportByteSize) += stats->report_size_bytes();
Joe Onorato1754d742016-11-21 17:51:35 -0800201
Yi Jinb592e3b2018-02-01 15:17:04 -0800202 // Notify listener of starting
203 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800204 if ((*it)->listener != NULL && (*it)->args.containsSection(id)) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800205 (*it)->listener->onReportSectionStatus(
206 id, IIncidentReportStatusListener::STATUS_FINISHED);
Joe Onorato1754d742016-11-21 17:51:35 -0800207 }
208 }
Yi Jinf32af482017-08-11 15:00:49 -0700209 ALOGD("Finish incident report section %d '%s'", id, (*section)->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800210 }
211 }
212
Yi Jinedfd5bb2017-09-06 17:09:11 -0700213DONE:
Yi Jin329130b2018-02-09 16:47:47 -0800214 // Reports the metdadata when taking the incident report.
215 if (!isTest) metadataSection.Execute(&batch);
216
Joe Onorato1754d742016-11-21 17:51:35 -0800217 // Close the file.
218 if (mainFd >= 0) {
219 close(mainFd);
220 }
221
222 // Tell everyone that we're done.
Yi Jinb592e3b2018-02-01 15:17:04 -0800223 for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) {
Joe Onorato1754d742016-11-21 17:51:35 -0800224 if ((*it)->listener != NULL) {
225 if (err == NO_ERROR) {
226 (*it)->listener->onReportFinished();
227 } else {
228 (*it)->listener->onReportFailed();
229 }
230 }
231 }
232
233 // Put the report into dropbox.
234 if (needMainFd && err == NO_ERROR) {
235 sp<DropBoxManager> dropbox = new DropBoxManager();
236 Status status = dropbox->addFile(String16("incident"), mFilename, 0);
237 ALOGD("Incident report done. dropbox status=%s\n", status.toString8().string());
238 if (!status.isOk()) {
239 return REPORT_NEEDS_DROPBOX;
240 }
241
242 // If the status was ok, delete the file. If not, leave it around until the next
243 // boot or the next checkin. If the directory gets too big older files will
244 // be rotated out.
Yi Jinb592e3b2018-02-01 15:17:04 -0800245 if (!isTest) unlink(mFilename.c_str());
Joe Onorato1754d742016-11-21 17:51:35 -0800246 }
247
248 return REPORT_FINISHED;
249}
250
251/**
252 * Create our output file and set the access permissions to -rw-rw----
253 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800254status_t Reporter::create_file(int* fd) {
Joe Onorato1754d742016-11-21 17:51:35 -0800255 const char* filename = mFilename.c_str();
256
Yi Jinadd11e92017-07-30 16:10:07 -0700257 *fd = open(filename, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0660);
Joe Onorato1754d742016-11-21 17:51:35 -0800258 if (*fd < 0) {
259 ALOGE("Couldn't open incident file: %s (%s)", filename, strerror(errno));
260 return -errno;
261 }
262
263 // Override umask. Not super critical. If it fails go on with life.
264 chmod(filename, 0660);
265
Yi Jin4bab3a12018-01-10 16:50:59 -0800266 if (chown(filename, AID_INCIDENTD, AID_INCIDENTD)) {
Joe Onorato1754d742016-11-21 17:51:35 -0800267 ALOGE("Unable to change ownership of incident file %s: %s\n", filename, strerror(errno));
268 status_t err = -errno;
269 unlink(mFilename.c_str());
270 return err;
271 }
272
273 return NO_ERROR;
274}
275
Yi Jinb592e3b2018-02-01 15:17:04 -0800276Reporter::run_report_status_t Reporter::upload_backlog() {
Joe Onorato1754d742016-11-21 17:51:35 -0800277 DIR* dir;
278 struct dirent* entry;
279 struct stat st;
Yi Jinadd11e92017-07-30 16:10:07 -0700280 status_t err;
Joe Onorato1754d742016-11-21 17:51:35 -0800281
Yi Jinf32af482017-08-11 15:00:49 -0700282 ALOGD("Start uploading backlogs in %s", INCIDENT_DIRECTORY);
Yi Jinadd11e92017-07-30 16:10:07 -0700283 if ((err = create_directory(INCIDENT_DIRECTORY)) != NO_ERROR) {
284 ALOGE("directory doesn't exist: %s", strerror(-err));
285 return REPORT_FINISHED;
286 }
287
288 if ((dir = opendir(INCIDENT_DIRECTORY)) == NULL) {
289 ALOGE("Couldn't open incident directory: %s", INCIDENT_DIRECTORY);
Joe Onorato1754d742016-11-21 17:51:35 -0800290 return REPORT_NEEDS_DROPBOX;
291 }
292
Joe Onorato1754d742016-11-21 17:51:35 -0800293 sp<DropBoxManager> dropbox = new DropBoxManager();
294
295 // Enumerate, count and add up size
Yi Jinf32af482017-08-11 15:00:49 -0700296 int count = 0;
Joe Onorato1754d742016-11-21 17:51:35 -0800297 while ((entry = readdir(dir)) != NULL) {
298 if (entry->d_name[0] == '.') {
299 continue;
300 }
Yi Jinadd11e92017-07-30 16:10:07 -0700301 String8 filename = String8(INCIDENT_DIRECTORY) + entry->d_name;
Joe Onorato1754d742016-11-21 17:51:35 -0800302 if (stat(filename.string(), &st) != 0) {
303 ALOGE("Unable to stat file %s", filename.string());
304 continue;
305 }
306 if (!S_ISREG(st.st_mode)) {
307 continue;
308 }
309
310 Status status = dropbox->addFile(String16("incident"), filename.string(), 0);
311 ALOGD("Incident report done. dropbox status=%s\n", status.toString8().string());
312 if (!status.isOk()) {
313 return REPORT_NEEDS_DROPBOX;
314 }
315
316 // If the status was ok, delete the file. If not, leave it around until the next
317 // boot or the next checkin. If the directory gets too big older files will
318 // be rotated out.
319 unlink(filename.string());
Yi Jinf32af482017-08-11 15:00:49 -0700320 count++;
Joe Onorato1754d742016-11-21 17:51:35 -0800321 }
Yi Jinf32af482017-08-11 15:00:49 -0700322 ALOGD("Successfully uploaded %d files to Dropbox.", count);
Joe Onorato1754d742016-11-21 17:51:35 -0800323 closedir(dir);
324
325 return REPORT_FINISHED;
326}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700327
328} // namespace incidentd
329} // namespace os
330} // namespace android