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