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