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