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