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 | */ |
| 16 | |
| 17 | #define LOG_TAG "incidentd" |
| 18 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 19 | #include "FdBuffer.h" |
| 20 | #include "Privacy.h" |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 21 | #include "PrivacyBuffer.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | #include "Section.h" |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 23 | |
| 24 | #include "io_util.h" |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 25 | #include "section_list.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 26 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 27 | #include <android/util/protobuf.h> |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 28 | #include <private/android_filesystem_config.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 29 | #include <binder/IServiceManager.h> |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 30 | #include <map> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | #include <mutex> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 32 | #include <wait.h> |
| 33 | #include <unistd.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 34 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 35 | using namespace android::util; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 36 | using namespace std; |
| 37 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 38 | // special section ids |
| 39 | const int FIELD_ID_INCIDENT_HEADER = 1; |
| 40 | |
| 41 | // incident section parameters |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 42 | const int WAIT_MAX = 5; |
| 43 | const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000}; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 44 | const char* INCIDENT_HELPER = "/system/bin/incident_helper"; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 45 | |
| 46 | static pid_t |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 47 | fork_execute_incident_helper(const int id, const char* name, Fpipe& p2cPipe, Fpipe& c2pPipe) |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 48 | { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 49 | const char* ihArgs[] { INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL }; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 50 | |
| 51 | // fork used in multithreaded environment, avoid adding unnecessary code in child process |
| 52 | pid_t pid = fork(); |
| 53 | if (pid == 0) { |
| 54 | // child process executes incident helper as nobody |
| 55 | if (setgid(AID_NOBODY) == -1) { |
| 56 | ALOGW("%s can't change gid: %s", name, strerror(errno)); |
| 57 | _exit(EXIT_FAILURE); |
| 58 | } |
| 59 | if (setuid(AID_NOBODY) == -1) { |
| 60 | ALOGW("%s can't change uid: %s", name, strerror(errno)); |
| 61 | _exit(EXIT_FAILURE); |
| 62 | } |
| 63 | |
| 64 | if (dup2(p2cPipe.readFd(), STDIN_FILENO) != 0 || !p2cPipe.close() || |
| 65 | dup2(c2pPipe.writeFd(), STDOUT_FILENO) != 1 || !c2pPipe.close()) { |
| 66 | ALOGW("%s can't setup stdin and stdout for incident helper", name); |
| 67 | _exit(EXIT_FAILURE); |
| 68 | } |
| 69 | |
| 70 | execv(INCIDENT_HELPER, const_cast<char**>(ihArgs)); |
| 71 | |
| 72 | ALOGW("%s failed in incident helper process: %s", name, strerror(errno)); |
| 73 | _exit(EXIT_FAILURE); // always exits with failure if any |
| 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 | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 82 | static status_t kill_child(pid_t pid) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 83 | int status; |
| 84 | kill(pid, SIGKILL); |
| 85 | if (waitpid(pid, &status, 0) == -1) return -1; |
| 86 | return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status); |
| 87 | } |
| 88 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 89 | static status_t wait_child(pid_t pid) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 90 | int status; |
| 91 | bool died = false; |
| 92 | // wait for child to report status up to 1 seconds |
| 93 | for(int loop = 0; !died && loop < WAIT_MAX; loop++) { |
| 94 | if (waitpid(pid, &status, WNOHANG) == pid) died = true; |
| 95 | // sleep for 0.2 second |
| 96 | nanosleep(&WAIT_INTERVAL_NS, NULL); |
| 97 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 98 | if (!died) return kill_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 99 | return WIFEXITED(status) == 0 ? NO_ERROR : -WEXITSTATUS(status); |
| 100 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 101 | // ================================================================================ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 102 | static const Privacy* |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 103 | get_privacy_of_section(int id) |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 104 | { |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 105 | int l = 0; |
| 106 | int r = PRIVACY_POLICY_COUNT - 1; |
| 107 | while (l <= r) { |
| 108 | int mid = (l + r) >> 1; |
| 109 | const Privacy* p = PRIVACY_POLICY_LIST[mid]; |
| 110 | |
| 111 | if (p->field_id < (uint32_t)id) { |
| 112 | l = mid + 1; |
| 113 | } else if (p->field_id > (uint32_t)id) { |
| 114 | r = mid - 1; |
| 115 | } else { |
| 116 | return p; |
| 117 | } |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 118 | } |
| 119 | return NULL; |
| 120 | } |
| 121 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 122 | // ================================================================================ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 123 | static status_t |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 124 | write_section_header(int fd, int sectionId, size_t size) |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 125 | { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 126 | uint8_t buf[20]; |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 127 | uint8_t *p = write_length_delimited_tag_header(buf, sectionId, size); |
| 128 | return write_all(fd, buf, p-buf); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static status_t |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 132 | write_report_requests(const int id, const FdBuffer& buffer, ReportRequestSet* requests) |
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 | status_t err = -EBADF; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 135 | EncodedBuffer::iterator data = buffer.data(); |
| 136 | PrivacyBuffer privacyBuffer(get_privacy_of_section(id), data); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 137 | int writeable = 0; |
| 138 | |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 139 | // The streaming ones, group requests by spec in order to save unnecessary strip operations |
| 140 | map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 141 | for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) { |
| 142 | sp<ReportRequest> request = *it; |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 143 | if (!request->ok() || !request->args.containsSection(id)) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 144 | continue; // skip invalid request |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 145 | } |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 146 | PrivacySpec spec = new_spec_from_args(request->args.dest()); |
| 147 | requestsBySpec[spec].push_back(request); |
| 148 | } |
| 149 | |
| 150 | for (map<PrivacySpec, vector<sp<ReportRequest>>>::iterator mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) { |
| 151 | PrivacySpec spec = mit->first; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 152 | err = privacyBuffer.strip(spec); |
| 153 | if (err != NO_ERROR) return err; // it means the privacyBuffer data is corrupted. |
| 154 | if (privacyBuffer.size() == 0) continue; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 155 | |
| 156 | for (vector<sp<ReportRequest>>::iterator it = mit->second.begin(); it != mit->second.end(); it++) { |
| 157 | sp<ReportRequest> request = *it; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 158 | err = write_section_header(request->fd, id, privacyBuffer.size()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 159 | if (err != NO_ERROR) { request->err = err; continue; } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 160 | err = privacyBuffer.flush(request->fd); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 161 | if (err != NO_ERROR) { request->err = err; continue; } |
| 162 | writeable++; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 163 | ALOGD("Section %d flushed %zu bytes to fd %d with spec %d", id, privacyBuffer.size(), request->fd, spec.dest); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 164 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 165 | privacyBuffer.clear(); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // The dropbox file |
| 169 | if (requests->mainFd() >= 0) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 170 | err = privacyBuffer.strip(get_default_dropbox_spec()); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 171 | if (err != NO_ERROR) return err; // the buffer data is corrupted. |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 172 | if (privacyBuffer.size() == 0) goto DONE; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 173 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 174 | err = write_section_header(requests->mainFd(), id, privacyBuffer.size()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 175 | if (err != NO_ERROR) { requests->setMainFd(-1); goto DONE; } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 176 | err = privacyBuffer.flush(requests->mainFd()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 177 | if (err != NO_ERROR) { requests->setMainFd(-1); goto DONE; } |
| 178 | writeable++; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame^] | 179 | ALOGD("Section %d flushed %zu bytes to dropbox %d", id, privacyBuffer.size(), requests->mainFd()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 180 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 181 | |
| 182 | DONE: |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 183 | // only returns error if there is no fd to write to. |
| 184 | return writeable > 0 ? NO_ERROR : err; |
| 185 | } |
| 186 | |
| 187 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 188 | Section::Section(int i, const int64_t timeoutMs) |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 189 | :id(i), |
| 190 | timeoutMs(timeoutMs) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 191 | { |
| 192 | } |
| 193 | |
| 194 | Section::~Section() |
| 195 | { |
| 196 | } |
| 197 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 198 | // ================================================================================ |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 199 | HeaderSection::HeaderSection() |
| 200 | :Section(FIELD_ID_INCIDENT_HEADER, 0) |
| 201 | { |
| 202 | } |
| 203 | |
| 204 | HeaderSection::~HeaderSection() |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | status_t |
| 209 | HeaderSection::Execute(ReportRequestSet* requests) const |
| 210 | { |
| 211 | for (ReportRequestSet::iterator it=requests->begin(); it!=requests->end(); it++) { |
| 212 | const sp<ReportRequest> request = *it; |
| 213 | const vector<vector<int8_t>>& headers = request->args.headers(); |
| 214 | |
| 215 | for (vector<vector<int8_t>>::const_iterator buf=headers.begin(); buf!=headers.end(); buf++) { |
| 216 | if (buf->empty()) continue; |
| 217 | |
| 218 | // So the idea is only requests with negative fd are written to dropbox file. |
| 219 | int fd = request->fd >= 0 ? request->fd : requests->mainFd(); |
| 220 | write_section_header(fd, FIELD_ID_INCIDENT_HEADER, buf->size()); |
| 221 | write_all(fd, (uint8_t const*)buf->data(), buf->size()); |
| 222 | // If there was an error now, there will be an error later and we will remove |
| 223 | // it from the list then. |
| 224 | } |
| 225 | } |
| 226 | return NO_ERROR; |
| 227 | } |
| 228 | |
| 229 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 230 | FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs) |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 231 | :Section(id, timeoutMs), |
| 232 | mFilename(filename) |
| 233 | { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 234 | name = filename; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | FileSection::~FileSection() {} |
| 238 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 239 | status_t |
| 240 | FileSection::Execute(ReportRequestSet* requests) const |
| 241 | { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 242 | // read from mFilename first, make sure the file is available |
| 243 | // 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] | 244 | int fd = open(mFilename, O_RDONLY | O_CLOEXEC); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 245 | if (fd == -1) { |
| 246 | ALOGW("FileSection '%s' failed to open file", this->name.string()); |
| 247 | return -errno; |
| 248 | } |
| 249 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 250 | FdBuffer buffer; |
| 251 | Fpipe p2cPipe; |
| 252 | Fpipe c2pPipe; |
| 253 | // initiate pipes to pass data to/from incident_helper |
| 254 | if (!p2cPipe.init() || !c2pPipe.init()) { |
| 255 | ALOGW("FileSection '%s' failed to setup pipes", this->name.string()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 256 | return -errno; |
| 257 | } |
| 258 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 259 | 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] | 260 | if (pid == -1) { |
| 261 | ALOGW("FileSection '%s' failed to fork", this->name.string()); |
| 262 | return -errno; |
| 263 | } |
| 264 | |
| 265 | // parent process |
| 266 | status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(), |
| 267 | this->timeoutMs); |
| 268 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
| 269 | ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s, kill: %s", |
| 270 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false", |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 271 | strerror(-kill_child(pid))); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 272 | return readStatus; |
| 273 | } |
| 274 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 275 | status_t ihStatus = wait_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 276 | if (ihStatus != NO_ERROR) { |
| 277 | ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), strerror(-ihStatus)); |
| 278 | return ihStatus; |
| 279 | } |
| 280 | |
| 281 | ALOGD("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(), |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 282 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 283 | status_t err = write_report_requests(this->id, buffer, requests); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 284 | if (err != NO_ERROR) { |
| 285 | ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 286 | return err; |
| 287 | } |
| 288 | |
| 289 | return NO_ERROR; |
| 290 | } |
| 291 | |
| 292 | // ================================================================================ |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 293 | struct WorkerThreadData : public virtual RefBase |
| 294 | { |
| 295 | const WorkerThreadSection* section; |
| 296 | int fds[2]; |
| 297 | |
| 298 | // Lock protects these fields |
| 299 | mutex lock; |
| 300 | bool workerDone; |
| 301 | status_t workerError; |
| 302 | |
| 303 | WorkerThreadData(const WorkerThreadSection* section); |
| 304 | virtual ~WorkerThreadData(); |
| 305 | |
| 306 | int readFd() { return fds[0]; } |
| 307 | int writeFd() { return fds[1]; } |
| 308 | }; |
| 309 | |
| 310 | WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec) |
| 311 | :section(sec), |
| 312 | workerDone(false), |
| 313 | workerError(NO_ERROR) |
| 314 | { |
| 315 | fds[0] = -1; |
| 316 | fds[1] = -1; |
| 317 | } |
| 318 | |
| 319 | WorkerThreadData::~WorkerThreadData() |
| 320 | { |
| 321 | } |
| 322 | |
| 323 | // ================================================================================ |
| 324 | WorkerThreadSection::WorkerThreadSection(int id) |
| 325 | :Section(id) |
| 326 | { |
| 327 | } |
| 328 | |
| 329 | WorkerThreadSection::~WorkerThreadSection() |
| 330 | { |
| 331 | } |
| 332 | |
| 333 | static void* |
| 334 | worker_thread_func(void* cookie) |
| 335 | { |
| 336 | WorkerThreadData* data = (WorkerThreadData*)cookie; |
| 337 | status_t err = data->section->BlockingCall(data->writeFd()); |
| 338 | |
| 339 | { |
| 340 | unique_lock<mutex> lock(data->lock); |
| 341 | data->workerDone = true; |
| 342 | data->workerError = err; |
| 343 | } |
| 344 | |
| 345 | close(data->writeFd()); |
| 346 | data->decStrong(data->section); |
| 347 | // data might be gone now. don't use it after this point in this thread. |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | status_t |
| 352 | WorkerThreadSection::Execute(ReportRequestSet* requests) const |
| 353 | { |
| 354 | status_t err = NO_ERROR; |
| 355 | pthread_t thread; |
| 356 | pthread_attr_t attr; |
| 357 | bool timedOut = false; |
| 358 | FdBuffer buffer; |
| 359 | |
| 360 | // Data shared between this thread and the worker thread. |
| 361 | sp<WorkerThreadData> data = new WorkerThreadData(this); |
| 362 | |
| 363 | // Create the pipe |
| 364 | err = pipe(data->fds); |
| 365 | if (err != 0) { |
| 366 | return -errno; |
| 367 | } |
| 368 | |
| 369 | // The worker thread needs a reference and we can't let the count go to zero |
| 370 | // if that thread is slow to start. |
| 371 | data->incStrong(this); |
| 372 | |
| 373 | // Create the thread |
| 374 | err = pthread_attr_init(&attr); |
| 375 | if (err != 0) { |
| 376 | return -err; |
| 377 | } |
| 378 | // TODO: Do we need to tweak thread priority? |
| 379 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 380 | if (err != 0) { |
| 381 | pthread_attr_destroy(&attr); |
| 382 | return -err; |
| 383 | } |
| 384 | err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get()); |
| 385 | if (err != 0) { |
| 386 | pthread_attr_destroy(&attr); |
| 387 | return -err; |
| 388 | } |
| 389 | pthread_attr_destroy(&attr); |
| 390 | |
| 391 | // 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] | 392 | err = buffer.read(data->readFd(), this->timeoutMs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 393 | if (err != NO_ERROR) { |
| 394 | // TODO: Log this error into the incident report. |
| 395 | ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(), |
| 396 | strerror(-err)); |
| 397 | } |
| 398 | |
| 399 | // Done with the read fd. The worker thread closes the write one so |
| 400 | // we never race and get here first. |
| 401 | close(data->readFd()); |
| 402 | |
| 403 | // If the worker side is finished, then return its error (which may overwrite |
| 404 | // our possible error -- but it's more interesting anyway). If not, then we timed out. |
| 405 | { |
| 406 | unique_lock<mutex> lock(data->lock); |
| 407 | if (!data->workerDone) { |
| 408 | // We timed out |
| 409 | timedOut = true; |
| 410 | } else { |
| 411 | if (data->workerError != NO_ERROR) { |
| 412 | err = data->workerError; |
| 413 | // TODO: Log this error into the incident report. |
| 414 | ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(), |
| 415 | strerror(-err)); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (timedOut || buffer.timedOut()) { |
| 421 | ALOGW("WorkerThreadSection '%s' timed out", this->name.string()); |
| 422 | return NO_ERROR; |
| 423 | } |
| 424 | |
| 425 | if (buffer.truncated()) { |
| 426 | // TODO: Log this into the incident report. |
| 427 | } |
| 428 | |
| 429 | // TODO: There was an error with the command or buffering. Report that. For now |
| 430 | // just exit with a log messasge. |
| 431 | if (err != NO_ERROR) { |
| 432 | ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(), |
| 433 | strerror(-err)); |
| 434 | return NO_ERROR; |
| 435 | } |
| 436 | |
| 437 | // Write the data that was collected |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 438 | ALOGD("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(), |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 439 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 440 | err = write_report_requests(this->id, buffer, requests); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 441 | if (err != NO_ERROR) { |
| 442 | ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err)); |
| 443 | return err; |
| 444 | } |
| 445 | |
| 446 | return NO_ERROR; |
| 447 | } |
| 448 | |
| 449 | // ================================================================================ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 450 | void |
| 451 | CommandSection::init(const char* command, va_list args) |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 452 | { |
| 453 | va_list copied_args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 454 | int numOfArgs = 0; |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 455 | |
| 456 | va_copy(copied_args, args); |
| 457 | while(va_arg(copied_args, const char*) != NULL) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 458 | numOfArgs++; |
| 459 | } |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 460 | va_end(copied_args); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 461 | |
| 462 | // allocate extra 1 for command and 1 for NULL terminator |
| 463 | mCommand = (const char**)malloc(sizeof(const char*) * (numOfArgs + 2)); |
| 464 | |
| 465 | mCommand[0] = command; |
| 466 | name = command; |
| 467 | for (int i=0; i<numOfArgs; i++) { |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 468 | const char* arg = va_arg(args, const char*); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 469 | mCommand[i+1] = arg; |
| 470 | name += " "; |
| 471 | name += arg; |
| 472 | } |
| 473 | mCommand[numOfArgs+1] = NULL; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...) |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 477 | :Section(id, timeoutMs) |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 478 | { |
| 479 | va_list args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 480 | va_start(args, command); |
| 481 | init(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 482 | va_end(args); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 483 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 484 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 485 | CommandSection::CommandSection(int id, const char* command, ...) |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 486 | :Section(id) |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 487 | { |
| 488 | va_list args; |
| 489 | va_start(args, command); |
| 490 | init(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 491 | va_end(args); |
| 492 | } |
| 493 | |
| 494 | CommandSection::~CommandSection() |
| 495 | { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 496 | free(mCommand); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | status_t |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 500 | CommandSection::Execute(ReportRequestSet* requests) const |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 501 | { |
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()) { |
| 520 | ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(), strerror(errno)); |
| 521 | _exit(EXIT_FAILURE); |
| 522 | } |
| 523 | execv(this->mCommand[0], (char *const *) this->mCommand); |
| 524 | int err = errno; // record command error code |
| 525 | ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(), strerror(errno)); |
| 526 | _exit(err); // exit with command error code |
| 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()) { |
| 537 | ALOGW("CommandSection '%s' failed to read data from incident helper: %s, " |
| 538 | "timedout: %s, kill command: %s, kill incident helper: %s", |
| 539 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false", |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 540 | strerror(-kill_child(cmdPid)), strerror(-kill_child(ihPid))); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 541 | return readStatus; |
| 542 | } |
| 543 | |
| 544 | // TODO: wait for command here has one trade-off: the failed status of command won't be detected until |
| 545 | // buffer timeout, but it has advatage on starting the data stream earlier. |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 546 | status_t cmdStatus = wait_child(cmdPid); |
| 547 | status_t ihStatus = wait_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 548 | if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) { |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 549 | ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident helper: %s", |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 550 | this->name.string(), strerror(-cmdStatus), strerror(-ihStatus)); |
| 551 | return cmdStatus != NO_ERROR ? cmdStatus : ihStatus; |
| 552 | } |
| 553 | |
| 554 | ALOGD("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(), |
| 555 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 556 | status_t err = write_report_requests(this->id, buffer, requests); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 557 | if (err != NO_ERROR) { |
| 558 | ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 559 | return err; |
| 560 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 561 | return NO_ERROR; |
| 562 | } |
| 563 | |
| 564 | // ================================================================================ |
| 565 | DumpsysSection::DumpsysSection(int id, const char* service, ...) |
| 566 | :WorkerThreadSection(id), |
| 567 | mService(service) |
| 568 | { |
| 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 | |
| 586 | DumpsysSection::~DumpsysSection() |
| 587 | { |
| 588 | } |
| 589 | |
| 590 | status_t |
| 591 | DumpsysSection::BlockingCall(int pipeWriteFd) const |
| 592 | { |
| 593 | // checkService won't wait for the service to show up like getService will. |
| 594 | sp<IBinder> service = defaultServiceManager()->checkService(mService); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 595 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 596 | if (service == NULL) { |
| 597 | // Returning an error interrupts the entire incident report, so just |
| 598 | // log the failure. |
| 599 | // TODO: have a meta record inside the report that would log this |
| 600 | // failure inside the report, because the fact that we can't find |
| 601 | // the service is good data in and of itself. This is running in |
| 602 | // another thread so lock that carefully... |
| 603 | ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string()); |
| 604 | return NO_ERROR; |
| 605 | } |
| 606 | |
| 607 | service->dump(pipeWriteFd, mArgs); |
| 608 | |
| 609 | return NO_ERROR; |
| 610 | } |