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 | |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame^] | 25 | #include <android-base/properties.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 26 | #include <android/os/DropBoxManager.h> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 27 | #include <private/android_filesystem_config.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 28 | #include <utils/SystemClock.h> |
| 29 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 30 | #include <dirent.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | #include <errno.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 32 | #include <fcntl.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame^] | 35 | #include <string> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * The directory where the incident reports are stored. |
| 39 | */ |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 40 | static const char* INCIDENT_DIRECTORY = "/data/misc/incidents/"; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 41 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | namespace os { |
| 44 | namespace incidentd { |
| 45 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 46 | // ================================================================================ |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 47 | ReportRequest::ReportRequest(const IncidentReportArgs& a, |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 48 | const sp<IIncidentReportStatusListener>& l, int f) |
| 49 | : args(a), listener(l), fd(f), err(NO_ERROR) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 50 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 51 | ReportRequest::~ReportRequest() { |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 52 | if (fd >= 0) { |
| 53 | // clean up the opened file descriptor |
| 54 | close(fd); |
| 55 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 58 | bool ReportRequest::ok() { return fd >= 0 && err == NO_ERROR; } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 59 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 60 | // ================================================================================ |
| 61 | ReportRequestSet::ReportRequestSet() |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 62 | : mRequests(), mSections(), mMainFd(-1), mMainDest(-1), mMetadata(), mSectionStats() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 63 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 64 | ReportRequestSet::~ReportRequestSet() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 65 | |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 66 | // 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] | 67 | void ReportRequestSet::add(const sp<ReportRequest>& request) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 68 | mRequests.push_back(request); |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 69 | mSections.merge(request->args); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 70 | mMetadata.set_request_size(mMetadata.request_size() + 1); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 73 | void ReportRequestSet::setMainFd(int fd) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 74 | mMainFd = fd; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 75 | mMetadata.set_use_dropbox(fd > 0); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 78 | void ReportRequestSet::setMainDest(int dest) { |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 79 | mMainDest = dest; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 80 | PrivacySpec spec = PrivacySpec::new_spec(dest); |
| 81 | switch (spec.dest) { |
| 82 | case android::os::DEST_AUTOMATIC: |
| 83 | mMetadata.set_dest(IncidentMetadata_Destination_AUTOMATIC); |
| 84 | break; |
| 85 | case android::os::DEST_EXPLICIT: |
| 86 | mMetadata.set_dest(IncidentMetadata_Destination_EXPLICIT); |
| 87 | break; |
| 88 | case android::os::DEST_LOCAL: |
| 89 | mMetadata.set_dest(IncidentMetadata_Destination_LOCAL); |
| 90 | break; |
| 91 | } |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 94 | bool ReportRequestSet::containsSection(int id) { return mSections.containsSection(id); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 95 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 96 | IncidentMetadata::SectionStats* ReportRequestSet::sectionStats(int id) { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 97 | if (mSectionStats.find(id) == mSectionStats.end()) { |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 98 | IncidentMetadata::SectionStats stats; |
| 99 | stats.set_id(id); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 100 | mSectionStats[id] = stats; |
| 101 | } |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 102 | return &mSectionStats[id]; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 105 | // ================================================================================ |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 106 | Reporter::Reporter() : Reporter(INCIDENT_DIRECTORY) { isTest = false; }; |
| 107 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 108 | Reporter::Reporter(const char* directory) : batch() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 109 | char buf[100]; |
| 110 | |
Yi Jin | 91d4330 | 2018-04-12 11:06:57 -0700 | [diff] [blame] | 111 | 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] | 112 | mMaxCount = 100; |
| 113 | |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 114 | // string ends up with '/' is a directory |
| 115 | String8 dir = String8(directory); |
| 116 | if (directory[dir.size() - 1] != '/') dir += "/"; |
| 117 | mIncidentDirectory = dir.string(); |
| 118 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 119 | // There can't be two at the same time because it's on one thread. |
| 120 | mStartTime = time(NULL); |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 121 | strftime(buf, sizeof(buf), "incident-%Y%m%d-%H%M%S", localtime(&mStartTime)); |
| 122 | mFilename = mIncidentDirectory + buf; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 125 | Reporter::~Reporter() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 126 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 127 | Reporter::run_report_status_t Reporter::runReport(size_t* reportByteSize) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 128 | status_t err = NO_ERROR; |
| 129 | bool needMainFd = false; |
| 130 | int mainFd = -1; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 131 | int mainDest = -1; |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 132 | HeaderSection headers; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 133 | MetadataSection metadataSection; |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame^] | 134 | std::string buildType = android::base::GetProperty("ro.build.type", ""); |
| 135 | const bool isUserdebugOrEng = buildType == "userdebug" || buildType == "eng"; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 136 | |
| 137 | // See if we need the main file |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 138 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 139 | if ((*it)->fd < 0 && mainFd < 0) { |
| 140 | needMainFd = true; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 141 | mainDest = (*it)->args.dest(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | } |
| 145 | if (needMainFd) { |
| 146 | // Create the directory |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 147 | if (!isTest) err = create_directory(mIncidentDirectory); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 148 | if (err != NO_ERROR) { |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 149 | goto DONE; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // If there are too many files in the directory (for whatever reason), |
| 153 | // delete the oldest ones until it's under the limit. Doing this first |
| 154 | // 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] | 155 | if (!isTest) clean_directory(mIncidentDirectory, mMaxSize, mMaxCount); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 156 | |
| 157 | // Open the file. |
| 158 | err = create_file(&mainFd); |
| 159 | if (err != NO_ERROR) { |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 160 | goto DONE; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Add to the set |
| 164 | batch.setMainFd(mainFd); |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 165 | batch.setMainDest(mainDest); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Tell everyone that we're starting. |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 169 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 170 | if ((*it)->listener != NULL) { |
| 171 | (*it)->listener->onReportStarted(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Write the incident headers |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 176 | headers.Execute(&batch); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 177 | |
| 178 | // For each of the report fields, see if we need it, and if so, execute the command |
| 179 | // and report to those that care that we're doing it. |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 180 | for (const Section** section = SECTION_LIST; *section; section++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 181 | const int id = (*section)->id; |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame^] | 182 | if ((*section)->userdebugAndEngOnly && !isUserdebugOrEng) { |
| 183 | ALOGD("Skipping incident report section %d '%s' because it's limited to userdebug/eng", |
| 184 | id, (*section)->name.string()); |
| 185 | continue; |
| 186 | } |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 187 | if (this->batch.containsSection(id)) { |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 188 | ALOGD("Taking incident report section %d '%s'", id, (*section)->name.string()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 189 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 190 | if ((*it)->listener != NULL && (*it)->args.containsSection(id)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 191 | (*it)->listener->onReportSectionStatus( |
| 192 | id, IIncidentReportStatusListener::STATUS_STARTING); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | // Execute - go get the data and write it into the file descriptors. |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 197 | IncidentMetadata::SectionStats* stats = batch.sectionStats(id); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 198 | int64_t startTime = uptimeMillis(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 199 | err = (*section)->Execute(&batch); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 200 | int64_t endTime = uptimeMillis(); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 201 | stats->set_success(err == NO_ERROR); |
| 202 | stats->set_exec_duration_ms(endTime - startTime); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 203 | if (err != NO_ERROR) { |
Yi Jin | a5c5e8a | 2017-09-27 18:24:58 -0700 | [diff] [blame] | 204 | ALOGW("Incident section %s (%d) failed: %s. Stopping report.", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 205 | (*section)->name.string(), id, strerror(-err)); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 206 | goto DONE; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 207 | } |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 208 | (*reportByteSize) += stats->report_size_bytes(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 209 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 210 | // Notify listener of starting |
| 211 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 212 | if ((*it)->listener != NULL && (*it)->args.containsSection(id)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 213 | (*it)->listener->onReportSectionStatus( |
| 214 | id, IIncidentReportStatusListener::STATUS_FINISHED); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 217 | ALOGD("Finish incident report section %d '%s'", id, (*section)->name.string()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 221 | DONE: |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 222 | // Reports the metdadata when taking the incident report. |
| 223 | if (!isTest) metadataSection.Execute(&batch); |
| 224 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 225 | // Close the file. |
| 226 | if (mainFd >= 0) { |
| 227 | close(mainFd); |
| 228 | } |
| 229 | |
| 230 | // Tell everyone that we're done. |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 231 | for (ReportRequestSet::iterator it = batch.begin(); it != batch.end(); it++) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 232 | if ((*it)->listener != NULL) { |
| 233 | if (err == NO_ERROR) { |
| 234 | (*it)->listener->onReportFinished(); |
| 235 | } else { |
| 236 | (*it)->listener->onReportFailed(); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Put the report into dropbox. |
| 242 | if (needMainFd && err == NO_ERROR) { |
| 243 | sp<DropBoxManager> dropbox = new DropBoxManager(); |
| 244 | Status status = dropbox->addFile(String16("incident"), mFilename, 0); |
| 245 | ALOGD("Incident report done. dropbox status=%s\n", status.toString8().string()); |
| 246 | if (!status.isOk()) { |
| 247 | return REPORT_NEEDS_DROPBOX; |
| 248 | } |
| 249 | |
| 250 | // If the status was ok, delete the file. If not, leave it around until the next |
| 251 | // boot or the next checkin. If the directory gets too big older files will |
| 252 | // be rotated out. |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 253 | if (!isTest) unlink(mFilename.c_str()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | return REPORT_FINISHED; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Create our output file and set the access permissions to -rw-rw---- |
| 261 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 262 | status_t Reporter::create_file(int* fd) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 263 | const char* filename = mFilename.c_str(); |
| 264 | |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 265 | *fd = open(filename, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0660); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 266 | if (*fd < 0) { |
| 267 | ALOGE("Couldn't open incident file: %s (%s)", filename, strerror(errno)); |
| 268 | return -errno; |
| 269 | } |
| 270 | |
| 271 | // Override umask. Not super critical. If it fails go on with life. |
| 272 | chmod(filename, 0660); |
| 273 | |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 274 | if (chown(filename, AID_INCIDENTD, AID_INCIDENTD)) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 275 | ALOGE("Unable to change ownership of incident file %s: %s\n", filename, strerror(errno)); |
| 276 | status_t err = -errno; |
| 277 | unlink(mFilename.c_str()); |
| 278 | return err; |
| 279 | } |
| 280 | |
| 281 | return NO_ERROR; |
| 282 | } |
| 283 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 284 | Reporter::run_report_status_t Reporter::upload_backlog() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 285 | DIR* dir; |
| 286 | struct dirent* entry; |
| 287 | struct stat st; |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 288 | status_t err; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 289 | |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 290 | ALOGD("Start uploading backlogs in %s", INCIDENT_DIRECTORY); |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 291 | if ((err = create_directory(INCIDENT_DIRECTORY)) != NO_ERROR) { |
| 292 | ALOGE("directory doesn't exist: %s", strerror(-err)); |
| 293 | return REPORT_FINISHED; |
| 294 | } |
| 295 | |
| 296 | if ((dir = opendir(INCIDENT_DIRECTORY)) == NULL) { |
| 297 | ALOGE("Couldn't open incident directory: %s", INCIDENT_DIRECTORY); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 298 | return REPORT_NEEDS_DROPBOX; |
| 299 | } |
| 300 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 301 | sp<DropBoxManager> dropbox = new DropBoxManager(); |
| 302 | |
| 303 | // Enumerate, count and add up size |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 304 | int count = 0; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 305 | while ((entry = readdir(dir)) != NULL) { |
| 306 | if (entry->d_name[0] == '.') { |
| 307 | continue; |
| 308 | } |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 309 | String8 filename = String8(INCIDENT_DIRECTORY) + entry->d_name; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 310 | if (stat(filename.string(), &st) != 0) { |
| 311 | ALOGE("Unable to stat file %s", filename.string()); |
| 312 | continue; |
| 313 | } |
| 314 | if (!S_ISREG(st.st_mode)) { |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | Status status = dropbox->addFile(String16("incident"), filename.string(), 0); |
| 319 | ALOGD("Incident report done. dropbox status=%s\n", status.toString8().string()); |
| 320 | if (!status.isOk()) { |
| 321 | return REPORT_NEEDS_DROPBOX; |
| 322 | } |
| 323 | |
| 324 | // If the status was ok, delete the file. If not, leave it around until the next |
| 325 | // boot or the next checkin. If the directory gets too big older files will |
| 326 | // be rotated out. |
| 327 | unlink(filename.string()); |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 328 | count++; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 329 | } |
Yi Jin | f32af48 | 2017-08-11 15:00:49 -0700 | [diff] [blame] | 330 | ALOGD("Successfully uploaded %d files to Dropbox.", count); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 331 | closedir(dir); |
| 332 | |
| 333 | return REPORT_FINISHED; |
| 334 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 335 | |
| 336 | } // namespace incidentd |
| 337 | } // namespace os |
| 338 | } // namespace android |