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 "Section.h" |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 20 | |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 21 | #include <wait.h> |
| 22 | |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 23 | #include <mutex> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 24 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 25 | #include <android-base/file.h> |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 26 | #include <android/util/protobuf.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 28 | #include <log/log_event_list.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 29 | #include <log/log_read.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 30 | #include <log/logprint.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 31 | #include <private/android_logger.h> |
| 32 | |
| 33 | #include "FdBuffer.h" |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 34 | #include "Privacy.h" |
| 35 | #include "PrivacyBuffer.h" |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 36 | #include "frameworks/base/core/proto/android/os/data.proto.h" |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 37 | #include "frameworks/base/core/proto/android/util/log.proto.h" |
| 38 | #include "incidentd_util.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 39 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 40 | using namespace android::base; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 41 | using namespace android::util; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 42 | using namespace std; |
| 43 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 44 | // special section ids |
| 45 | const int FIELD_ID_INCIDENT_HEADER = 1; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 46 | const int FIELD_ID_INCIDENT_METADATA = 2; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 47 | |
| 48 | // incident section parameters |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 49 | const int WAIT_MAX = 5; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 50 | const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000}; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 51 | const char INCIDENT_HELPER[] = "/system/bin/incident_helper"; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 52 | const char GZIP[] = "/system/bin/gzip"; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 53 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 54 | static pid_t fork_execute_incident_helper(const int id, Fpipe* p2cPipe, Fpipe* c2pPipe) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 55 | const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL}; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 56 | return fork_execute_cmd(INCIDENT_HELPER, const_cast<char**>(ihArgs), p2cPipe, c2pPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 59 | // ================================================================================ |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 60 | static status_t statusCode(int status) { |
| 61 | if (WIFSIGNALED(status)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 62 | VLOG("return by signal: %s", strerror(WTERMSIG(status))); |
| 63 | return -WTERMSIG(status); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 64 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 65 | VLOG("return by exit: %s", strerror(WEXITSTATUS(status))); |
| 66 | return -WEXITSTATUS(status); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 67 | } |
| 68 | return NO_ERROR; |
| 69 | } |
| 70 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 71 | static status_t kill_child(pid_t pid) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 72 | int status; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 73 | VLOG("try to kill child process %d", pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 74 | kill(pid, SIGKILL); |
| 75 | if (waitpid(pid, &status, 0) == -1) return -1; |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 76 | return statusCode(status); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 79 | static status_t wait_child(pid_t pid) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 80 | int status; |
| 81 | bool died = false; |
| 82 | // wait for child to report status up to 1 seconds |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 83 | for (int loop = 0; !died && loop < WAIT_MAX; loop++) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 84 | if (waitpid(pid, &status, WNOHANG) == pid) died = true; |
| 85 | // sleep for 0.2 second |
| 86 | nanosleep(&WAIT_INTERVAL_NS, NULL); |
| 87 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 88 | if (!died) return kill_child(pid); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 89 | return statusCode(status); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 90 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 91 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 92 | static status_t write_section_header(int fd, int sectionId, size_t size) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 93 | uint8_t buf[20]; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 94 | uint8_t* p = write_length_delimited_tag_header(buf, sectionId, size); |
| 95 | return WriteFully(fd, buf, p - buf) ? NO_ERROR : -errno; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 98 | static status_t write_report_requests(const int id, const FdBuffer& buffer, |
| 99 | ReportRequestSet* requests) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 100 | status_t err = -EBADF; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 101 | EncodedBuffer::iterator data = buffer.data(); |
| 102 | PrivacyBuffer privacyBuffer(get_privacy_of_section(id), data); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 103 | int writeable = 0; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 104 | auto stats = requests->sectionStats(id); |
| 105 | |
| 106 | stats->set_dump_size_bytes(data.size()); |
| 107 | stats->set_dump_duration_ms(buffer.durationMs()); |
| 108 | stats->set_timed_out(buffer.timedOut()); |
| 109 | stats->set_is_truncated(buffer.truncated()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 110 | |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 111 | // The streaming ones, group requests by spec in order to save unnecessary strip operations |
| 112 | map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 113 | for (auto it = requests->begin(); it != requests->end(); it++) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 114 | sp<ReportRequest> request = *it; |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 115 | if (!request->ok() || !request->args.containsSection(id)) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 116 | continue; // skip invalid request |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 117 | } |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 118 | PrivacySpec spec = PrivacySpec::new_spec(request->args.dest()); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 119 | requestsBySpec[spec].push_back(request); |
| 120 | } |
| 121 | |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 122 | for (auto mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 123 | PrivacySpec spec = mit->first; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 124 | err = privacyBuffer.strip(spec); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 125 | if (err != NO_ERROR) return err; // it means the privacyBuffer data is corrupted. |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 126 | if (privacyBuffer.size() == 0) continue; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 127 | |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 128 | for (auto it = mit->second.begin(); it != mit->second.end(); it++) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 129 | sp<ReportRequest> request = *it; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 130 | err = write_section_header(request->fd, id, privacyBuffer.size()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 131 | if (err != NO_ERROR) { |
| 132 | request->err = err; |
| 133 | continue; |
| 134 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 135 | err = privacyBuffer.flush(request->fd); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 136 | if (err != NO_ERROR) { |
| 137 | request->err = err; |
| 138 | continue; |
| 139 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 140 | writeable++; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 141 | VLOG("Section %d flushed %zu bytes to fd %d with spec %d", id, privacyBuffer.size(), |
| 142 | request->fd, spec.dest); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 143 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 144 | privacyBuffer.clear(); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // The dropbox file |
| 148 | if (requests->mainFd() >= 0) { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 149 | PrivacySpec spec = PrivacySpec::new_spec(requests->mainDest()); |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 150 | err = privacyBuffer.strip(spec); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 151 | if (err != NO_ERROR) return err; // the buffer data is corrupted. |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 152 | if (privacyBuffer.size() == 0) goto DONE; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 153 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 154 | err = write_section_header(requests->mainFd(), id, privacyBuffer.size()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 155 | if (err != NO_ERROR) { |
| 156 | requests->setMainFd(-1); |
| 157 | goto DONE; |
| 158 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 159 | err = privacyBuffer.flush(requests->mainFd()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 160 | if (err != NO_ERROR) { |
| 161 | requests->setMainFd(-1); |
| 162 | goto DONE; |
| 163 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 164 | writeable++; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 165 | VLOG("Section %d flushed %zu bytes to dropbox %d with spec %d", id, privacyBuffer.size(), |
| 166 | requests->mainFd(), spec.dest); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 167 | stats->set_report_size_bytes(privacyBuffer.size()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 168 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 169 | |
| 170 | DONE: |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 171 | // only returns error if there is no fd to write to. |
| 172 | return writeable > 0 ? NO_ERROR : err; |
| 173 | } |
| 174 | |
| 175 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 176 | Section::Section(int i, const int64_t timeoutMs) : id(i), timeoutMs(timeoutMs) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 177 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 178 | Section::~Section() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 179 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 180 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 181 | HeaderSection::HeaderSection() : Section(FIELD_ID_INCIDENT_HEADER, 0) {} |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 182 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 183 | HeaderSection::~HeaderSection() {} |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 184 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 185 | status_t HeaderSection::Execute(ReportRequestSet* requests) const { |
| 186 | for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) { |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 187 | const sp<ReportRequest> request = *it; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 188 | const vector<vector<uint8_t>>& headers = request->args.headers(); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 189 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 190 | for (vector<vector<uint8_t>>::const_iterator buf = headers.begin(); buf != headers.end(); |
| 191 | buf++) { |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 192 | if (buf->empty()) continue; |
| 193 | |
| 194 | // So the idea is only requests with negative fd are written to dropbox file. |
| 195 | int fd = request->fd >= 0 ? request->fd : requests->mainFd(); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 196 | write_section_header(fd, id, buf->size()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 197 | WriteFully(fd, (uint8_t const*)buf->data(), buf->size()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 198 | // If there was an error now, there will be an error later and we will remove |
| 199 | // it from the list then. |
| 200 | } |
| 201 | } |
| 202 | return NO_ERROR; |
| 203 | } |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 204 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 205 | MetadataSection::MetadataSection() : Section(FIELD_ID_INCIDENT_METADATA, 0) {} |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 206 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 207 | MetadataSection::~MetadataSection() {} |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 208 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 209 | status_t MetadataSection::Execute(ReportRequestSet* requests) const { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 210 | std::string metadataBuf; |
| 211 | requests->metadata().SerializeToString(&metadataBuf); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 212 | for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 213 | const sp<ReportRequest> request = *it; |
| 214 | if (metadataBuf.empty() || request->fd < 0 || request->err != NO_ERROR) { |
| 215 | continue; |
| 216 | } |
| 217 | write_section_header(request->fd, id, metadataBuf.size()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 218 | if (!WriteFully(request->fd, (uint8_t const*)metadataBuf.data(), metadataBuf.size())) { |
| 219 | ALOGW("Failed to write metadata to fd %d", request->fd); |
| 220 | // we don't fail if we can't write to a single request's fd. |
| 221 | } |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 222 | } |
| 223 | if (requests->mainFd() >= 0 && !metadataBuf.empty()) { |
| 224 | write_section_header(requests->mainFd(), id, metadataBuf.size()); |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 225 | if (!WriteFully(requests->mainFd(), (uint8_t const*)metadataBuf.data(), |
| 226 | metadataBuf.size())) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 227 | ALOGW("Failed to write metadata to dropbox fd %d", requests->mainFd()); |
| 228 | return -1; |
| 229 | } |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 230 | } |
| 231 | return NO_ERROR; |
| 232 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 233 | // ================================================================================ |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 234 | static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; } |
| 235 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 236 | FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 237 | : Section(id, timeoutMs), mFilename(filename) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 238 | name = filename; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 239 | mIsSysfs = isSysfs(filename); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | FileSection::~FileSection() {} |
| 243 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 244 | status_t FileSection::Execute(ReportRequestSet* requests) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 245 | // read from mFilename first, make sure the file is available |
| 246 | // add O_CLOEXEC to make sure it is closed when exec incident helper |
George Burgess IV | 6f9735b | 2017-08-03 16:08:29 -0700 | [diff] [blame] | 247 | int fd = open(mFilename, O_RDONLY | O_CLOEXEC); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 248 | if (fd == -1) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 249 | ALOGW("FileSection '%s' failed to open file", this->name.string()); |
| 250 | return -errno; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 253 | FdBuffer buffer; |
| 254 | Fpipe p2cPipe; |
| 255 | Fpipe c2pPipe; |
| 256 | // initiate pipes to pass data to/from incident_helper |
| 257 | if (!p2cPipe.init() || !c2pPipe.init()) { |
| 258 | ALOGW("FileSection '%s' failed to setup pipes", this->name.string()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 259 | return -errno; |
| 260 | } |
| 261 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 262 | pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 263 | if (pid == -1) { |
| 264 | ALOGW("FileSection '%s' failed to fork", this->name.string()); |
| 265 | return -errno; |
| 266 | } |
| 267 | |
| 268 | // parent process |
| 269 | status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 270 | this->timeoutMs, mIsSysfs); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 271 | close(fd); // close the fd anyway. |
| 272 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 273 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 274 | ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 275 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 276 | kill_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 277 | return readStatus; |
| 278 | } |
| 279 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 280 | status_t ihStatus = wait_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 281 | if (ihStatus != NO_ERROR) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 282 | ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), |
| 283 | strerror(-ihStatus)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 284 | return ihStatus; |
| 285 | } |
| 286 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 287 | VLOG("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(), |
| 288 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 289 | status_t err = write_report_requests(this->id, buffer, requests); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 290 | if (err != NO_ERROR) { |
| 291 | ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 292 | return err; |
| 293 | } |
| 294 | |
| 295 | return NO_ERROR; |
| 296 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 297 | // ================================================================================ |
| 298 | GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) { |
| 299 | name = "gzip "; |
| 300 | name += filename; |
| 301 | va_list args; |
| 302 | va_start(args, filename); |
| 303 | mFilenames = varargs(filename, args); |
| 304 | va_end(args); |
| 305 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 306 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 307 | GZipSection::~GZipSection() {} |
| 308 | |
| 309 | status_t GZipSection::Execute(ReportRequestSet* requests) const { |
| 310 | // Reads the files in order, use the first available one. |
| 311 | int index = 0; |
| 312 | int fd = -1; |
| 313 | while (mFilenames[index] != NULL) { |
| 314 | fd = open(mFilenames[index], O_RDONLY | O_CLOEXEC); |
| 315 | if (fd != -1) { |
| 316 | break; |
| 317 | } |
| 318 | ALOGW("GZipSection failed to open file %s", mFilenames[index]); |
| 319 | index++; // look at the next file. |
| 320 | } |
| 321 | VLOG("GZipSection is using file %s, fd=%d", mFilenames[index], fd); |
| 322 | if (fd == -1) return -1; |
| 323 | |
| 324 | FdBuffer buffer; |
| 325 | Fpipe p2cPipe; |
| 326 | Fpipe c2pPipe; |
| 327 | // initiate pipes to pass data to/from gzip |
| 328 | if (!p2cPipe.init() || !c2pPipe.init()) { |
| 329 | ALOGW("GZipSection '%s' failed to setup pipes", this->name.string()); |
| 330 | return -errno; |
| 331 | } |
| 332 | |
| 333 | const char* gzipArgs[]{GZIP, NULL}; |
| 334 | pid_t pid = fork_execute_cmd(GZIP, const_cast<char**>(gzipArgs), &p2cPipe, &c2pPipe); |
| 335 | if (pid == -1) { |
| 336 | ALOGW("GZipSection '%s' failed to fork", this->name.string()); |
| 337 | return -errno; |
| 338 | } |
| 339 | // parent process |
| 340 | |
| 341 | // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using |
| 342 | // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream. |
| 343 | EncodedBuffer* internalBuffer = buffer.getInternalBuffer(); |
| 344 | internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED); |
| 345 | String8 usedFile(mFilenames[index]); |
| 346 | internalBuffer->writeRawVarint32(usedFile.size()); |
| 347 | for (size_t i = 0; i < usedFile.size(); i++) { |
| 348 | internalBuffer->writeRawByte(mFilenames[index][i]); |
| 349 | } |
| 350 | internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA, |
| 351 | WIRE_TYPE_LENGTH_DELIMITED); |
| 352 | size_t editPos = internalBuffer->wp()->pos(); |
| 353 | internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size. |
| 354 | size_t dataBeginAt = internalBuffer->wp()->pos(); |
| 355 | VLOG("GZipSection '%s' editPos=%zd, dataBeginAt=%zd", this->name.string(), editPos, |
| 356 | dataBeginAt); |
| 357 | |
| 358 | status_t readStatus = buffer.readProcessedDataInStream( |
| 359 | fd, p2cPipe.writeFd(), c2pPipe.readFd(), this->timeoutMs, isSysfs(mFilenames[index])); |
| 360 | close(fd); // close the fd anyway. |
| 361 | |
| 362 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
| 363 | ALOGW("GZipSection '%s' failed to read data from gzip: %s, timedout: %s", |
| 364 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
| 365 | kill_child(pid); |
| 366 | return readStatus; |
| 367 | } |
| 368 | |
| 369 | status_t gzipStatus = wait_child(pid); |
| 370 | if (gzipStatus != NO_ERROR) { |
| 371 | ALOGW("GZipSection '%s' abnormal child process: %s", this->name.string(), |
| 372 | strerror(-gzipStatus)); |
| 373 | return gzipStatus; |
| 374 | } |
| 375 | // Revisit the actual size from gzip result and edit the internal buffer accordingly. |
| 376 | size_t dataSize = buffer.size() - dataBeginAt; |
| 377 | internalBuffer->wp()->rewind()->move(editPos); |
| 378 | internalBuffer->writeRawVarint32(dataSize); |
| 379 | internalBuffer->copy(dataBeginAt, dataSize); |
| 380 | VLOG("GZipSection '%s' wrote %zd bytes in %d ms, dataSize=%zd", this->name.string(), |
| 381 | buffer.size(), (int)buffer.durationMs(), dataSize); |
| 382 | status_t err = write_report_requests(this->id, buffer, requests); |
| 383 | if (err != NO_ERROR) { |
| 384 | ALOGW("GZipSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 385 | return err; |
| 386 | } |
| 387 | |
| 388 | return NO_ERROR; |
| 389 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 390 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 391 | struct WorkerThreadData : public virtual RefBase { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 392 | const WorkerThreadSection* section; |
| 393 | int fds[2]; |
| 394 | |
| 395 | // Lock protects these fields |
| 396 | mutex lock; |
| 397 | bool workerDone; |
| 398 | status_t workerError; |
| 399 | |
| 400 | WorkerThreadData(const WorkerThreadSection* section); |
| 401 | virtual ~WorkerThreadData(); |
| 402 | |
| 403 | int readFd() { return fds[0]; } |
| 404 | int writeFd() { return fds[1]; } |
| 405 | }; |
| 406 | |
| 407 | WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 408 | : section(sec), workerDone(false), workerError(NO_ERROR) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 409 | fds[0] = -1; |
| 410 | fds[1] = -1; |
| 411 | } |
| 412 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 413 | WorkerThreadData::~WorkerThreadData() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 414 | |
| 415 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 416 | WorkerThreadSection::WorkerThreadSection(int id) : Section(id) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 417 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 418 | WorkerThreadSection::~WorkerThreadSection() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 419 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 420 | static void* worker_thread_func(void* cookie) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 421 | WorkerThreadData* data = (WorkerThreadData*)cookie; |
| 422 | status_t err = data->section->BlockingCall(data->writeFd()); |
| 423 | |
| 424 | { |
| 425 | unique_lock<mutex> lock(data->lock); |
| 426 | data->workerDone = true; |
| 427 | data->workerError = err; |
| 428 | } |
| 429 | |
| 430 | close(data->writeFd()); |
| 431 | data->decStrong(data->section); |
| 432 | // data might be gone now. don't use it after this point in this thread. |
| 433 | return NULL; |
| 434 | } |
| 435 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 436 | status_t WorkerThreadSection::Execute(ReportRequestSet* requests) const { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 437 | status_t err = NO_ERROR; |
| 438 | pthread_t thread; |
| 439 | pthread_attr_t attr; |
| 440 | bool timedOut = false; |
| 441 | FdBuffer buffer; |
| 442 | |
| 443 | // Data shared between this thread and the worker thread. |
| 444 | sp<WorkerThreadData> data = new WorkerThreadData(this); |
| 445 | |
| 446 | // Create the pipe |
| 447 | err = pipe(data->fds); |
| 448 | if (err != 0) { |
| 449 | return -errno; |
| 450 | } |
| 451 | |
| 452 | // The worker thread needs a reference and we can't let the count go to zero |
| 453 | // if that thread is slow to start. |
| 454 | data->incStrong(this); |
| 455 | |
| 456 | // Create the thread |
| 457 | err = pthread_attr_init(&attr); |
| 458 | if (err != 0) { |
| 459 | return -err; |
| 460 | } |
| 461 | // TODO: Do we need to tweak thread priority? |
| 462 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 463 | if (err != 0) { |
| 464 | pthread_attr_destroy(&attr); |
| 465 | return -err; |
| 466 | } |
| 467 | err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get()); |
| 468 | if (err != 0) { |
| 469 | pthread_attr_destroy(&attr); |
| 470 | return -err; |
| 471 | } |
| 472 | pthread_attr_destroy(&attr); |
| 473 | |
| 474 | // Loop reading until either the timeout or the worker side is done (i.e. eof). |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 475 | err = buffer.read(data->readFd(), this->timeoutMs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 476 | if (err != NO_ERROR) { |
| 477 | // TODO: Log this error into the incident report. |
| 478 | ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 479 | strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | // Done with the read fd. The worker thread closes the write one so |
| 483 | // we never race and get here first. |
| 484 | close(data->readFd()); |
| 485 | |
| 486 | // If the worker side is finished, then return its error (which may overwrite |
| 487 | // our possible error -- but it's more interesting anyway). If not, then we timed out. |
| 488 | { |
| 489 | unique_lock<mutex> lock(data->lock); |
| 490 | if (!data->workerDone) { |
| 491 | // We timed out |
| 492 | timedOut = true; |
| 493 | } else { |
| 494 | if (data->workerError != NO_ERROR) { |
| 495 | err = data->workerError; |
| 496 | // TODO: Log this error into the incident report. |
| 497 | ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 498 | strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (timedOut || buffer.timedOut()) { |
| 504 | ALOGW("WorkerThreadSection '%s' timed out", this->name.string()); |
| 505 | return NO_ERROR; |
| 506 | } |
| 507 | |
| 508 | if (buffer.truncated()) { |
| 509 | // TODO: Log this into the incident report. |
| 510 | } |
| 511 | |
| 512 | // TODO: There was an error with the command or buffering. Report that. For now |
| 513 | // just exit with a log messasge. |
| 514 | if (err != NO_ERROR) { |
| 515 | ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 516 | strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 517 | return NO_ERROR; |
| 518 | } |
| 519 | |
| 520 | // Write the data that was collected |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 521 | VLOG("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(), |
| 522 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 523 | err = write_report_requests(this->id, buffer, requests); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 524 | if (err != NO_ERROR) { |
| 525 | ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err)); |
| 526 | return err; |
| 527 | } |
| 528 | |
| 529 | return NO_ERROR; |
| 530 | } |
| 531 | |
| 532 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 533 | CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 534 | : Section(id, timeoutMs) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 535 | name = command; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 536 | va_list args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 537 | va_start(args, command); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 538 | mCommand = varargs(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 539 | va_end(args); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 540 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 541 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 542 | CommandSection::CommandSection(int id, const char* command, ...) : Section(id) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 543 | name = command; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 544 | va_list args; |
| 545 | va_start(args, command); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 546 | mCommand = varargs(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 547 | va_end(args); |
| 548 | } |
| 549 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 550 | CommandSection::~CommandSection() { free(mCommand); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 551 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 552 | status_t CommandSection::Execute(ReportRequestSet* requests) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 553 | FdBuffer buffer; |
| 554 | Fpipe cmdPipe; |
| 555 | Fpipe ihPipe; |
| 556 | |
| 557 | if (!cmdPipe.init() || !ihPipe.init()) { |
| 558 | ALOGW("CommandSection '%s' failed to setup pipes", this->name.string()); |
| 559 | return -errno; |
| 560 | } |
| 561 | |
| 562 | pid_t cmdPid = fork(); |
| 563 | if (cmdPid == -1) { |
| 564 | ALOGW("CommandSection '%s' failed to fork", this->name.string()); |
| 565 | return -errno; |
| 566 | } |
| 567 | // child process to execute the command as root |
| 568 | if (cmdPid == 0) { |
| 569 | // replace command's stdout with ihPipe's write Fd |
| 570 | if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 571 | ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(), |
| 572 | strerror(errno)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 573 | _exit(EXIT_FAILURE); |
| 574 | } |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 575 | execvp(this->mCommand[0], (char* const*)this->mCommand); |
| 576 | int err = errno; // record command error code |
| 577 | ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(), |
| 578 | strerror(errno)); |
| 579 | _exit(err); // exit with command error code |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 580 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 581 | pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 582 | if (ihPid == -1) { |
| 583 | ALOGW("CommandSection '%s' failed to fork", this->name.string()); |
| 584 | return -errno; |
| 585 | } |
| 586 | |
| 587 | close(cmdPipe.writeFd()); |
| 588 | status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs); |
| 589 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 590 | ALOGW("CommandSection '%s' failed to read data from incident helper: %s, timedout: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 591 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 592 | kill_child(cmdPid); |
| 593 | kill_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 594 | return readStatus; |
| 595 | } |
| 596 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 597 | // TODO: wait for command here has one trade-off: the failed status of command won't be detected |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame^] | 598 | // until buffer timeout, but it has advatage on starting the data stream earlier. |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 599 | status_t cmdStatus = wait_child(cmdPid); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 600 | status_t ihStatus = wait_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 601 | if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 602 | ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident " |
| 603 | "helper: %s", |
| 604 | this->name.string(), strerror(-cmdStatus), strerror(-ihStatus)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 605 | return cmdStatus != NO_ERROR ? cmdStatus : ihStatus; |
| 606 | } |
| 607 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 608 | VLOG("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(), |
| 609 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 610 | status_t err = write_report_requests(this->id, buffer, requests); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 611 | if (err != NO_ERROR) { |
| 612 | ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 613 | return err; |
| 614 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 615 | return NO_ERROR; |
| 616 | } |
| 617 | |
| 618 | // ================================================================================ |
| 619 | DumpsysSection::DumpsysSection(int id, const char* service, ...) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 620 | : WorkerThreadSection(id), mService(service) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 621 | name = "dumpsys "; |
| 622 | name += service; |
| 623 | |
| 624 | va_list args; |
| 625 | va_start(args, service); |
| 626 | while (true) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 627 | const char* arg = va_arg(args, const char*); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 628 | if (arg == NULL) { |
| 629 | break; |
| 630 | } |
| 631 | mArgs.add(String16(arg)); |
| 632 | name += " "; |
| 633 | name += arg; |
| 634 | } |
| 635 | va_end(args); |
| 636 | } |
| 637 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 638 | DumpsysSection::~DumpsysSection() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 639 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 640 | status_t DumpsysSection::BlockingCall(int pipeWriteFd) const { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 641 | // checkService won't wait for the service to show up like getService will. |
| 642 | sp<IBinder> service = defaultServiceManager()->checkService(mService); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 643 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 644 | if (service == NULL) { |
| 645 | // Returning an error interrupts the entire incident report, so just |
| 646 | // log the failure. |
| 647 | // TODO: have a meta record inside the report that would log this |
| 648 | // failure inside the report, because the fact that we can't find |
| 649 | // the service is good data in and of itself. This is running in |
| 650 | // another thread so lock that carefully... |
| 651 | ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string()); |
| 652 | return NO_ERROR; |
| 653 | } |
| 654 | |
| 655 | service->dump(pipeWriteFd, mArgs); |
| 656 | |
| 657 | return NO_ERROR; |
| 658 | } |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 659 | |
| 660 | // ================================================================================ |
| 661 | // initialization only once in Section.cpp. |
| 662 | map<log_id_t, log_time> LogSection::gLastLogsRetrieved; |
| 663 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 664 | LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 665 | name += "logcat "; |
| 666 | name += android_log_id_to_name(logID); |
| 667 | switch (logID) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 668 | case LOG_ID_EVENTS: |
| 669 | case LOG_ID_STATS: |
| 670 | case LOG_ID_SECURITY: |
| 671 | mBinary = true; |
| 672 | break; |
| 673 | default: |
| 674 | mBinary = false; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 678 | LogSection::~LogSection() {} |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 679 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 680 | static size_t trimTail(char const* buf, size_t len) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 681 | while (len > 0) { |
| 682 | char c = buf[len - 1]; |
| 683 | if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') { |
| 684 | len--; |
| 685 | } else { |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | return len; |
| 690 | } |
| 691 | |
| 692 | static inline int32_t get4LE(uint8_t const* src) { |
| 693 | return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 694 | } |
| 695 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 696 | status_t LogSection::BlockingCall(int pipeWriteFd) const { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 697 | status_t err = NO_ERROR; |
| 698 | // Open log buffer and getting logs since last retrieved time if any. |
| 699 | unique_ptr<logger_list, void (*)(logger_list*)> loggers( |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 700 | gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end() |
| 701 | ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0) |
| 702 | : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, |
| 703 | gLastLogsRetrieved[mLogID], 0), |
| 704 | android_logger_list_free); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 705 | |
| 706 | if (android_logger_open(loggers.get(), mLogID) == NULL) { |
| 707 | ALOGW("LogSection %s: Can't get logger.", this->name.string()); |
| 708 | return err; |
| 709 | } |
| 710 | |
| 711 | log_msg msg; |
| 712 | log_time lastTimestamp(0); |
| 713 | |
| 714 | ProtoOutputStream proto; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 715 | while (true) { // keeps reading until logd buffer is fully read. |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 716 | status_t err = android_logger_list_read(loggers.get(), &msg); |
| 717 | // err = 0 - no content, unexpected connection drop or EOF. |
| 718 | // err = +ive number - size of retrieved data from logger |
| 719 | // err = -ive number, OS supplied error _except_ for -EAGAIN |
| 720 | // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data. |
| 721 | if (err <= 0) { |
| 722 | if (err != -EAGAIN) { |
| 723 | ALOGE("LogSection %s: fails to read a log_msg.\n", this->name.string()); |
| 724 | } |
| 725 | break; |
| 726 | } |
| 727 | if (mBinary) { |
| 728 | // remove the first uint32 which is tag's index in event log tags |
| 729 | android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 730 | msg.len() - sizeof(uint32_t)); |
| 731 | ; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 732 | android_log_list_element elem; |
| 733 | |
| 734 | lastTimestamp.tv_sec = msg.entry_v1.sec; |
| 735 | lastTimestamp.tv_nsec = msg.entry_v1.nsec; |
| 736 | |
| 737 | // format a BinaryLogEntry |
| 738 | long long token = proto.start(LogProto::BINARY_LOGS); |
| 739 | proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec); |
| 740 | proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 741 | proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 742 | proto.write(BinaryLogEntry::PID, msg.entry_v1.pid); |
| 743 | proto.write(BinaryLogEntry::TID, msg.entry_v1.tid); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 744 | proto.write(BinaryLogEntry::TAG_INDEX, |
| 745 | get4LE(reinterpret_cast<uint8_t const*>(msg.msg()))); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 746 | do { |
| 747 | elem = android_log_read_next(context); |
| 748 | long long elemToken = proto.start(BinaryLogEntry::ELEMS); |
| 749 | switch (elem.type) { |
| 750 | case EVENT_TYPE_INT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 751 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 752 | BinaryLogEntry::Elem::EVENT_TYPE_INT); |
| 753 | proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 754 | break; |
| 755 | case EVENT_TYPE_LONG: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 756 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 757 | BinaryLogEntry::Elem::EVENT_TYPE_LONG); |
| 758 | proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 759 | break; |
| 760 | case EVENT_TYPE_STRING: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 761 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 762 | BinaryLogEntry::Elem::EVENT_TYPE_STRING); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 763 | proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len); |
| 764 | break; |
| 765 | case EVENT_TYPE_FLOAT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 766 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 767 | BinaryLogEntry::Elem::EVENT_TYPE_FLOAT); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 768 | proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32); |
| 769 | break; |
| 770 | case EVENT_TYPE_LIST: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 771 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 772 | BinaryLogEntry::Elem::EVENT_TYPE_LIST); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 773 | break; |
| 774 | case EVENT_TYPE_LIST_STOP: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 775 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 776 | BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 777 | break; |
| 778 | case EVENT_TYPE_UNKNOWN: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 779 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 780 | BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | proto.end(elemToken); |
| 784 | } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete); |
| 785 | proto.end(token); |
| 786 | if (context) { |
| 787 | android_log_destroy(&context); |
| 788 | } |
| 789 | } else { |
| 790 | AndroidLogEntry entry; |
| 791 | err = android_log_processLogBuffer(&msg.entry_v1, &entry); |
| 792 | if (err != NO_ERROR) { |
| 793 | ALOGE("LogSection %s: fails to process to an entry.\n", this->name.string()); |
| 794 | break; |
| 795 | } |
| 796 | lastTimestamp.tv_sec = entry.tv_sec; |
| 797 | lastTimestamp.tv_nsec = entry.tv_nsec; |
| 798 | |
| 799 | // format a TextLogEntry |
| 800 | long long token = proto.start(LogProto::TEXT_LOGS); |
| 801 | proto.write(TextLogEntry::SEC, (long long)entry.tv_sec); |
| 802 | proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec); |
| 803 | proto.write(TextLogEntry::PRIORITY, (int)entry.priority); |
| 804 | proto.write(TextLogEntry::UID, entry.uid); |
| 805 | proto.write(TextLogEntry::PID, entry.pid); |
| 806 | proto.write(TextLogEntry::TID, entry.tid); |
| 807 | proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen)); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 808 | proto.write(TextLogEntry::LOG, entry.message, |
| 809 | trimTail(entry.message, entry.messageLen)); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 810 | proto.end(token); |
| 811 | } |
| 812 | } |
| 813 | gLastLogsRetrieved[mLogID] = lastTimestamp; |
| 814 | proto.flush(pipeWriteFd); |
| 815 | return err; |
| 816 | } |