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