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 "Reporter.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 20 | |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 21 | #include "Privacy.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | #include "report_directory.h" |
| 23 | #include "section_list.h" |
| 24 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 25 | #include <android/os/DropBoxManager.h> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 27 | #include <utils/SystemClock.h> |
| 28 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 29 | #include <dirent.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 30 | #include <errno.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 31 | #include <fcntl.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * The directory where the incident reports are stored. |
| 37 | */ |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 38 | static const char* INCIDENT_DIRECTORY = "/data/misc/incidents/"; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 39 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 40 | // ================================================================================ |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 41 | ReportRequest::ReportRequest(const IncidentReportArgs& a, |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 42 | const sp<IIncidentReportStatusListener>& l, int f) |
| 43 | : args(a), listener(l), fd(f), err(NO_ERROR) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 44 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 45 | ReportRequest::~ReportRequest() { |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 46 | if (fd >= 0) { |
| 47 | // clean up the opened file descriptor |
| 48 | close(fd); |
| 49 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 52 | bool ReportRequest::ok() { return fd >= 0 && err == NO_ERROR; } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 53 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 54 | // ================================================================================ |
| 55 | ReportRequestSet::ReportRequestSet() |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 56 | : mRequests(), mSections(), mMainFd(-1), mMainDest(-1), mMetadata(), mSectionStats() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 57 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 58 | ReportRequestSet::~ReportRequestSet() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 59 | |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 60 | // TODO: dedup on exact same args and fd, report the status back to listener! |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 61 | void ReportRequestSet::add(const sp<ReportRequest>& request) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 62 | mRequests.push_back(request); |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 63 | mSections.merge(request->args); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 64 | mMetadata.set_request_size(mMetadata.request_size() + 1); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 67 | void ReportRequestSet::setMainFd(int fd) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 68 | mMainFd = fd; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 69 | mMetadata.set_use_dropbox(fd > 0); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 72 | void ReportRequestSet::setMainDest(int dest) { |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 73 | mMainDest = dest; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 74 | 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 Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 88 | bool ReportRequestSet::containsSection(int id) { return mSections.containsSection(id); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 89 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 90 | IncidentMetadata::SectionStats* ReportRequestSet::sectionStats(int id) { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 91 | if (mSectionStats.find(id) == mSectionStats.end()) { |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 92 | IncidentMetadata::SectionStats stats; |
| 93 | stats.set_id(id); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 94 | mSectionStats[id] = stats; |
| 95 | } |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 96 | return &mSectionStats[id]; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 99 | // ================================================================================ |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 100 | Reporter::Reporter() : Reporter(INCIDENT_DIRECTORY) { isTest = false; }; |
| 101 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 102 | Reporter::Reporter(const char* directory) : batch() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 103 | char buf[100]; |
| 104 | |
| 105 | // TODO: Make the max size smaller for user builds. |
| 106 | mMaxSize = 100 * 1024 * 1024; |
| 107 | mMaxCount = 100; |
| 108 | |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 109 | // 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 114 | // There can't be two at the same time because it's on one thread. |
| 115 | mStartTime = time(NULL); |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 116 | strftime(buf, sizeof(buf), "incident-%Y%m%d-%H%M%S", localtime(&mStartTime)); |
| 117 | mFilename = mIncidentDirectory + buf; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 120 | Reporter::~Reporter() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 121 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 122 | Reporter::run_report_status_t Reporter::runReport(size_t* reportByteSize) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 123 | status_t err = NO_ERROR; |
| 124 | bool needMainFd = false; |
| 125 | int mainFd = -1; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 126 | int mainDest = -1; |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 127 | HeaderSection headers; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 128 | MetadataSection metadataSection; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 129 | |
| 130 | // See if we need the main file |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 131 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 132 | if ((*it)->fd < 0 && mainFd < 0) { |
| 133 | needMainFd = true; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 134 | mainDest = (*it)->args.dest(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | } |
| 138 | if (needMainFd) { |
| 139 | // Create the directory |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 140 | if (!isTest) err = create_directory(mIncidentDirectory); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 141 | if (err != NO_ERROR) { |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 142 | goto DONE; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 143 | } |
| 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 Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 148 | if (!isTest) clean_directory(mIncidentDirectory, mMaxSize, mMaxCount); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 149 | |
| 150 | // Open the file. |
| 151 | err = create_file(&mainFd); |
| 152 | if (err != NO_ERROR) { |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 153 | goto DONE; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Add to the set |
| 157 | batch.setMainFd(mainFd); |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 158 | batch.setMainDest(mainDest); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Tell everyone that we're starting. |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 162 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 163 | if ((*it)->listener != NULL) { |
| 164 | (*it)->listener->onReportStarted(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Write the incident headers |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 169 | headers.Execute(&batch); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 170 | |
| 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 Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 173 | for (const Section** section = SECTION_LIST; *section; section++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 174 | const int id = (*section)->id; |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 175 | if (this->batch.containsSection(id)) { |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 176 | ALOGD("Taking incident report section %d '%s'", id, (*section)->name.string()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 177 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 178 | if ((*it)->listener != NULL && (*it)->args.containsSection(id)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 179 | (*it)->listener->onReportSectionStatus( |
| 180 | id, IIncidentReportStatusListener::STATUS_STARTING); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // Execute - go get the data and write it into the file descriptors. |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 185 | IncidentMetadata::SectionStats* stats = batch.sectionStats(id); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 186 | int64_t startTime = uptimeMillis(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 187 | err = (*section)->Execute(&batch); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 188 | int64_t endTime = uptimeMillis(); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 189 | stats->set_success(err == NO_ERROR); |
| 190 | stats->set_exec_duration_ms(endTime - startTime); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 191 | if (err != NO_ERROR) { |
Yi Jin | a5c5e8a | 2017-09-27 18:24:58 -0700 | [diff] [blame] | 192 | ALOGW("Incident section %s (%d) failed: %s. Stopping report.", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 193 | (*section)->name.string(), id, strerror(-err)); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 194 | goto DONE; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 195 | } |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 196 | (*reportByteSize) += stats->report_size_bytes(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 197 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 198 | // Notify listener of starting |
| 199 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 200 | if ((*it)->listener != NULL && (*it)->args.containsSection(id)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 201 | (*it)->listener->onReportSectionStatus( |
| 202 | id, IIncidentReportStatusListener::STATUS_FINISHED); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 205 | ALOGD("Finish incident report section %d '%s'", id, (*section)->name.string()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 209 | DONE: |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 210 | // Reports the metdadata when taking the incident report. |
| 211 | if (!isTest) metadataSection.Execute(&batch); |
| 212 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 213 | // Close the file. |
| 214 | if (mainFd >= 0) { |
| 215 | close(mainFd); |
| 216 | } |
| 217 | |
| 218 | // Tell everyone that we're done. |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 219 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 220 | 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 Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 241 | if (!isTest) unlink(mFilename.c_str()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return REPORT_FINISHED; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Create our output file and set the access permissions to -rw-rw---- |
| 249 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 250 | status_t Reporter::create_file(int* fd) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 251 | const char* filename = mFilename.c_str(); |
| 252 | |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 253 | *fd = open(filename, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0660); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 254 | 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 Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 262 | if (chown(filename, AID_INCIDENTD, AID_INCIDENTD)) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 263 | 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 Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 272 | Reporter::run_report_status_t Reporter::upload_backlog() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 273 | DIR* dir; |
| 274 | struct dirent* entry; |
| 275 | struct stat st; |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 276 | status_t err; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 277 | |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 278 | ALOGD("Start uploading backlogs in %s", INCIDENT_DIRECTORY); |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 279 | 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 Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 286 | return REPORT_NEEDS_DROPBOX; |
| 287 | } |
| 288 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 289 | sp<DropBoxManager> dropbox = new DropBoxManager(); |
| 290 | |
| 291 | // Enumerate, count and add up size |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 292 | int count = 0; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 293 | while ((entry = readdir(dir)) != NULL) { |
| 294 | if (entry->d_name[0] == '.') { |
| 295 | continue; |
| 296 | } |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 297 | String8 filename = String8(INCIDENT_DIRECTORY) + entry->d_name; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 298 | 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 Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 316 | count++; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 317 | } |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 318 | ALOGD("Successfully uploaded %d files to Dropbox.", count); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 319 | closedir(dir); |
| 320 | |
| 321 | return REPORT_FINISHED; |
| 322 | } |